(gnus-refer-thread-limit): Increase default to 500.
[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   "j" gnus-article-jump-to-part
2091   "c" gnus-article-copy-part
2092   "C" gnus-article-view-part-as-charset
2093   "e" gnus-article-view-part-externally
2094   "E" gnus-article-encrypt-body
2095   "i" gnus-article-inline-part
2096   "|" gnus-article-pipe-part)
2097
2098 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2099   "p" gnus-summary-mark-as-processable
2100   "u" gnus-summary-unmark-as-processable
2101   "U" gnus-summary-unmark-all-processable
2102   "v" gnus-uu-mark-over
2103   "s" gnus-uu-mark-series
2104   "r" gnus-uu-mark-region
2105   "g" gnus-uu-unmark-region
2106   "R" gnus-uu-mark-by-regexp
2107   "G" gnus-uu-unmark-by-regexp
2108   "t" gnus-uu-mark-thread
2109   "T" gnus-uu-unmark-thread
2110   "a" gnus-uu-mark-all
2111   "b" gnus-uu-mark-buffer
2112   "S" gnus-uu-mark-sparse
2113   "k" gnus-summary-kill-process-mark
2114   "y" gnus-summary-yank-process-mark
2115   "w" gnus-summary-save-process-mark
2116   "i" gnus-uu-invert-processable)
2117
2118 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2119   ;;"x" gnus-uu-extract-any
2120   "m" gnus-summary-save-parts
2121   "u" gnus-uu-decode-uu
2122   "U" gnus-uu-decode-uu-and-save
2123   "s" gnus-uu-decode-unshar
2124   "S" gnus-uu-decode-unshar-and-save
2125   "o" gnus-uu-decode-save
2126   "O" gnus-uu-decode-save
2127   "b" gnus-uu-decode-binhex
2128   "B" gnus-uu-decode-binhex
2129   "p" gnus-uu-decode-postscript
2130   "P" gnus-uu-decode-postscript-and-save)
2131
2132 (gnus-define-keys
2133     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2134   "u" gnus-uu-decode-uu-view
2135   "U" gnus-uu-decode-uu-and-save-view
2136   "s" gnus-uu-decode-unshar-view
2137   "S" gnus-uu-decode-unshar-and-save-view
2138   "o" gnus-uu-decode-save-view
2139   "O" gnus-uu-decode-save-view
2140   "b" gnus-uu-decode-binhex-view
2141   "B" gnus-uu-decode-binhex-view
2142   "p" gnus-uu-decode-postscript-view
2143   "P" gnus-uu-decode-postscript-and-save-view)
2144
2145 (defvar gnus-article-post-menu nil)
2146
2147 (defconst gnus-summary-menu-maxlen 20)
2148
2149 (defun gnus-summary-menu-split (menu)
2150   ;; If we have lots of elements, divide them into groups of 20
2151   ;; and make a pane (or submenu) for each one.
2152   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2153       (let ((menu menu) sublists next
2154             (i 1))
2155         (while menu
2156           ;; Pull off the next gnus-summary-menu-maxlen elements
2157           ;; and make them the next element of sublist.
2158           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2159           (if next
2160               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2161                       nil))
2162           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2163                                              (aref (car (last menu)) 0)) menu)
2164                                sublists))
2165           (setq i (1+ i))
2166           (setq menu next))
2167         (nreverse sublists))
2168     ;; Few elements--put them all in one pane.
2169     menu))
2170
2171 (defun gnus-summary-make-menu-bar ()
2172   (gnus-turn-off-edit-menu 'summary)
2173
2174   (unless (boundp 'gnus-summary-misc-menu)
2175
2176     (easy-menu-define
2177       gnus-summary-kill-menu gnus-summary-mode-map ""
2178       (cons
2179        "Score"
2180        (nconc
2181         (list
2182          ["Customize" gnus-score-customize t])
2183         (gnus-make-score-map 'increase)
2184         (gnus-make-score-map 'lower)
2185         '(("Mark"
2186            ["Kill below" gnus-summary-kill-below t]
2187            ["Mark above" gnus-summary-mark-above t]
2188            ["Tick above" gnus-summary-tick-above t]
2189            ["Clear above" gnus-summary-clear-above t])
2190           ["Current score" gnus-summary-current-score t]
2191           ["Set score" gnus-summary-set-score t]
2192           ["Switch current score file..." gnus-score-change-score-file t]
2193           ["Set mark below..." gnus-score-set-mark-below t]
2194           ["Set expunge below..." gnus-score-set-expunge-below t]
2195           ["Edit current score file" gnus-score-edit-current-scores t]
2196           ["Edit score file" gnus-score-edit-file t]
2197           ["Trace score" gnus-score-find-trace t]
2198           ["Find words" gnus-score-find-favourite-words t]
2199           ["Rescore buffer" gnus-summary-rescore t]
2200           ["Increase score..." gnus-summary-increase-score t]
2201           ["Lower score..." gnus-summary-lower-score t]))))
2202
2203     ;; Define both the Article menu in the summary buffer and the
2204     ;; equivalent Commands menu in the article buffer here for
2205     ;; consistency.
2206     (let ((innards
2207            `(("Hide"
2208               ["All" gnus-article-hide t]
2209               ["Headers" gnus-article-hide-headers t]
2210               ["Signature" gnus-article-hide-signature t]
2211               ["Citation" gnus-article-hide-citation t]
2212               ["List identifiers" gnus-article-hide-list-identifiers t]
2213               ["Banner" gnus-article-strip-banner t]
2214               ["Boring headers" gnus-article-hide-boring-headers t])
2215              ("Highlight"
2216               ["All" gnus-article-highlight t]
2217               ["Headers" gnus-article-highlight-headers t]
2218               ["Signature" gnus-article-highlight-signature t]
2219               ["Citation" gnus-article-highlight-citation t])
2220              ("MIME"
2221               ["Words" gnus-article-decode-mime-words t]
2222               ["Charset" gnus-article-decode-charset t]
2223               ["QP" gnus-article-de-quoted-unreadable t]
2224               ["Base64" gnus-article-de-base64-unreadable t]
2225               ["View MIME buttons" gnus-summary-display-buttonized t]
2226               ["View all" gnus-mime-view-all-parts t]
2227               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2228               ["Encrypt body" gnus-article-encrypt-body
2229                :active (not (gnus-group-read-only-p))
2230                ,@(if (featurep 'xemacs) nil
2231                    '(:help "Encrypt the message body on disk"))]
2232               ["Extract all parts..." gnus-summary-save-parts t]
2233               ("Multipart"
2234                ["Repair multipart" gnus-summary-repair-multipart t]
2235                ["Pipe part..." gnus-article-pipe-part t]
2236                ["Inline part" gnus-article-inline-part t]
2237                ["Encrypt body" gnus-article-encrypt-body
2238                 :active (not (gnus-group-read-only-p))
2239                ,@(if (featurep 'xemacs) nil
2240                    '(:help "Encrypt the message body on disk"))]
2241                ["View part externally" gnus-article-view-part-externally t]
2242                ["View part with charset..." gnus-article-view-part-as-charset t]
2243                ["Copy part" gnus-article-copy-part t]
2244                ["Save part..." gnus-article-save-part t]
2245                ["View part" gnus-article-view-part t]))
2246              ("Date"
2247               ["Local" gnus-article-date-local t]
2248               ["ISO8601" gnus-article-date-iso8601 t]
2249               ["UT" gnus-article-date-ut t]
2250               ["Original" gnus-article-date-original t]
2251               ["Lapsed" gnus-article-date-lapsed t]
2252               ["User-defined" gnus-article-date-user t])
2253              ("Display"
2254               ["Remove images" gnus-article-remove-images t]
2255               ["Toggle smiley" gnus-treat-smiley t]
2256               ["Show X-Face" gnus-article-display-x-face t]
2257               ["Show picons in From" gnus-treat-from-picon t]
2258               ["Show picons in mail headers" gnus-treat-mail-picon t]
2259               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2260               ("View as different encoding"
2261                ,@(gnus-summary-menu-split
2262                   (mapcar
2263                    (lambda (cs)
2264                      ;; Since easymenu under Emacs doesn't allow
2265                      ;; lambda forms for menu commands, we should
2266                      ;; provide intern'ed function symbols.
2267                      (let ((command (intern (format "\
2268 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2269                        (fset command
2270                              `(lambda ()
2271                                 (interactive)
2272                                 (let ((gnus-summary-show-article-charset-alist
2273                                        '((1 . ,cs))))
2274                                   (gnus-summary-show-article 1))))
2275                        `[,(symbol-name cs) ,command t]))
2276                    (sort (if (fboundp 'coding-system-list)
2277                              (coding-system-list)
2278                            (mapcar 'car mm-mime-mule-charset-alist))
2279                          'string<)))))
2280              ("Washing"
2281               ("Remove Blanks"
2282                ["Leading" gnus-article-strip-leading-blank-lines t]
2283                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2284                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2285                ["All of the above" gnus-article-strip-blank-lines t]
2286                ["All" gnus-article-strip-all-blank-lines t]
2287                ["Leading space" gnus-article-strip-leading-space t]
2288                ["Trailing space" gnus-article-strip-trailing-space t]
2289                ["Leading space in headers"
2290                 gnus-article-remove-leading-whitespace t])
2291               ["Overstrike" gnus-article-treat-overstrike t]
2292               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2293               ["Emphasis" gnus-article-emphasize t]
2294               ["Word wrap" gnus-article-fill-cited-article t]
2295               ["Fill long lines" gnus-article-fill-long-lines t]
2296               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2297               ["Remove CR" gnus-article-remove-cr t]
2298               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2299               ["Base64" gnus-article-de-base64-unreadable t]
2300               ["Rot 13" gnus-summary-caesar-message
2301                ,@(if (featurep 'xemacs) '(t)
2302                    '(:help "\"Caesar rotate\" article by 13"))]
2303               ["De-IDNA" gnus-summary-idna-message t]
2304               ["Morse decode" gnus-summary-morse-message t]
2305               ["Unix pipe..." gnus-summary-pipe-message t]
2306               ["Add buttons" gnus-article-add-buttons t]
2307               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2308               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2309               ["Verbose header" gnus-summary-verbose-headers t]
2310               ["Toggle header" gnus-summary-toggle-header t]
2311               ["Unfold headers" gnus-article-treat-unfold-headers t]
2312               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2313               ["Html" gnus-article-wash-html t]
2314               ["Unsplit URLs" gnus-article-unsplit-urls t]
2315               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2316               ["Decode HZ" gnus-article-decode-HZ t]
2317               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2318               ("(Outlook) Deuglify"
2319                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2320                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2321                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2322                ["Full (Outlook) deuglify"
2323                 gnus-article-outlook-deuglify-article t])
2324               )
2325              ("Output"
2326               ["Save in default format..." gnus-summary-save-article
2327                ,@(if (featurep 'xemacs) '(t)
2328                    '(:help "Save article using default method"))]
2329               ["Save in file..." gnus-summary-save-article-file
2330                ,@(if (featurep 'xemacs) '(t)
2331                    '(:help "Save article in file"))]
2332               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2333               ["Save in MH folder..." gnus-summary-save-article-folder t]
2334               ["Save in VM folder..." gnus-summary-save-article-vm t]
2335               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2336               ["Save body in file..." gnus-summary-save-article-body-file t]
2337               ["Pipe through a filter..." gnus-summary-pipe-output t]
2338               ["Add to SOUP packet" gnus-soup-add-article t]
2339               ["Print with Muttprint..." gnus-summary-muttprint t]
2340               ["Print" gnus-summary-print-article
2341                ,@(if (featurep 'xemacs) '(t)
2342                    '(:help "Generate and print a PostScript image"))])
2343              ("Copy, move,... (Backend)"
2344               ,@(if (featurep 'xemacs) nil
2345                   '(:help "Copying, moving, expiring articles..."))
2346               ["Respool article..." gnus-summary-respool-article t]
2347               ["Move article..." gnus-summary-move-article
2348                (gnus-check-backend-function
2349                 'request-move-article gnus-newsgroup-name)]
2350               ["Copy article..." gnus-summary-copy-article t]
2351               ["Crosspost article..." gnus-summary-crosspost-article
2352                (gnus-check-backend-function
2353                 'request-replace-article gnus-newsgroup-name)]
2354               ["Import file..." gnus-summary-import-article
2355                (gnus-check-backend-function
2356                 'request-accept-article gnus-newsgroup-name)]
2357               ["Create article..." gnus-summary-create-article
2358                (gnus-check-backend-function
2359                 'request-accept-article gnus-newsgroup-name)]
2360               ["Check if posted" gnus-summary-article-posted-p t]
2361               ["Edit article" gnus-summary-edit-article
2362                (not (gnus-group-read-only-p))]
2363               ["Delete article" gnus-summary-delete-article
2364                (gnus-check-backend-function
2365                 'request-expire-articles gnus-newsgroup-name)]
2366               ["Query respool" gnus-summary-respool-query t]
2367               ["Trace respool" gnus-summary-respool-trace t]
2368               ["Delete expirable articles" gnus-summary-expire-articles-now
2369                (gnus-check-backend-function
2370                 'request-expire-articles gnus-newsgroup-name)])
2371              ("Extract"
2372               ["Uudecode" gnus-uu-decode-uu
2373                ,@(if (featurep 'xemacs) '(t)
2374                    '(:help "Decode uuencoded article(s)"))]
2375               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2376               ["Unshar" gnus-uu-decode-unshar t]
2377               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2378               ["Save" gnus-uu-decode-save t]
2379               ["Binhex" gnus-uu-decode-binhex t]
2380               ["Postscript" gnus-uu-decode-postscript t]
2381               ["All MIME parts" gnus-summary-save-parts t])
2382              ("Cache"
2383               ["Enter article" gnus-cache-enter-article t]
2384               ["Remove article" gnus-cache-remove-article t])
2385              ["Translate" gnus-article-babel t]
2386              ["Select article buffer" gnus-summary-select-article-buffer t]
2387              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2388              ["Isearch article..." gnus-summary-isearch-article t]
2389              ["Beginning of the article" gnus-summary-beginning-of-article t]
2390              ["End of the article" gnus-summary-end-of-article t]
2391              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2392              ["Fetch referenced articles" gnus-summary-refer-references t]
2393              ["Fetch current thread" gnus-summary-refer-thread t]
2394              ["Fetch article with id..." gnus-summary-refer-article t]
2395              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2396              ["Redisplay" gnus-summary-show-article t]
2397              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2398       (easy-menu-define
2399         gnus-summary-article-menu gnus-summary-mode-map ""
2400         (cons "Article" innards))
2401
2402       (if (not (keymapp gnus-summary-article-menu))
2403           (easy-menu-define
2404             gnus-article-commands-menu gnus-article-mode-map ""
2405             (cons "Commands" innards))
2406         ;; in Emacs, don't share menu.
2407         (setq gnus-article-commands-menu
2408               (copy-keymap gnus-summary-article-menu))
2409         (define-key gnus-article-mode-map [menu-bar commands]
2410           (cons "Commands" gnus-article-commands-menu))))
2411
2412     (easy-menu-define
2413       gnus-summary-thread-menu gnus-summary-mode-map ""
2414       '("Threads"
2415         ["Find all messages in thread" gnus-summary-refer-thread t]
2416         ["Toggle threading" gnus-summary-toggle-threads t]
2417         ["Hide threads" gnus-summary-hide-all-threads t]
2418         ["Show threads" gnus-summary-show-all-threads t]
2419         ["Hide thread" gnus-summary-hide-thread t]
2420         ["Show thread" gnus-summary-show-thread t]
2421         ["Go to next thread" gnus-summary-next-thread t]
2422         ["Go to previous thread" gnus-summary-prev-thread t]
2423         ["Go down thread" gnus-summary-down-thread t]
2424         ["Go up thread" gnus-summary-up-thread t]
2425         ["Top of thread" gnus-summary-top-thread t]
2426         ["Mark thread as read" gnus-summary-kill-thread t]
2427         ["Lower thread score" gnus-summary-lower-thread t]
2428         ["Raise thread score" gnus-summary-raise-thread t]
2429         ["Rethread current" gnus-summary-rethread-current t]))
2430
2431     (easy-menu-define
2432       gnus-summary-post-menu gnus-summary-mode-map ""
2433       `("Post"
2434         ["Send a message (mail or news)" gnus-summary-post-news
2435          ,@(if (featurep 'xemacs) '(t)
2436              '(:help "Compose a new message (mail or news)"))]
2437         ["Followup" gnus-summary-followup
2438          ,@(if (featurep 'xemacs) '(t)
2439              '(:help "Post followup to this article"))]
2440         ["Followup and yank" gnus-summary-followup-with-original
2441          ,@(if (featurep 'xemacs) '(t)
2442              '(:help "Post followup to this article, quoting its contents"))]
2443         ["Supersede article" gnus-summary-supersede-article t]
2444         ["Cancel article" gnus-summary-cancel-article
2445          ,@(if (featurep 'xemacs) '(t)
2446              '(:help "Cancel an article you posted"))]
2447         ["Reply" gnus-summary-reply t]
2448         ["Reply and yank" gnus-summary-reply-with-original t]
2449         ["Wide reply" gnus-summary-wide-reply t]
2450         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2451          ,@(if (featurep 'xemacs) '(t)
2452              '(:help "Mail a reply, quoting this article"))]
2453         ["Very wide reply" gnus-summary-very-wide-reply t]
2454         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2455          ,@(if (featurep 'xemacs) '(t)
2456              '(:help "Mail a very wide reply, quoting this article"))]
2457         ["Mail forward" gnus-summary-mail-forward t]
2458         ["Post forward" gnus-summary-post-forward t]
2459         ["Digest and mail" gnus-uu-digest-mail-forward t]
2460         ["Digest and post" gnus-uu-digest-post-forward t]
2461         ["Resend message" gnus-summary-resend-message t]
2462         ["Resend message edit" gnus-summary-resend-message-edit t]
2463         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2464         ["Send a mail" gnus-summary-mail-other-window t]
2465         ["Create a local message" gnus-summary-news-other-window t]
2466         ["Uuencode and post" gnus-uu-post-news
2467          ,@(if (featurep 'xemacs) '(t)
2468              '(:help "Post a uuencoded article"))]
2469         ["Followup via news" gnus-summary-followup-to-mail t]
2470         ["Followup via news and yank"
2471          gnus-summary-followup-to-mail-with-original t]
2472         ;;("Draft"
2473         ;;["Send" gnus-summary-send-draft t]
2474         ;;["Send bounced" gnus-resend-bounced-mail t])
2475         ))
2476
2477     (cond
2478      ((not (keymapp gnus-summary-post-menu))
2479       (setq gnus-article-post-menu gnus-summary-post-menu))
2480      ((not gnus-article-post-menu)
2481       ;; Don't share post menu.
2482       (setq gnus-article-post-menu
2483             (copy-keymap gnus-summary-post-menu))))
2484     (define-key gnus-article-mode-map [menu-bar post]
2485       (cons "Post" gnus-article-post-menu))
2486
2487     (easy-menu-define
2488       gnus-summary-misc-menu gnus-summary-mode-map ""
2489       `("Gnus"
2490         ("Mark Read"
2491          ["Mark as read" gnus-summary-mark-as-read-forward t]
2492          ["Mark same subject and select"
2493           gnus-summary-kill-same-subject-and-select t]
2494          ["Mark same subject" gnus-summary-kill-same-subject t]
2495          ["Catchup" gnus-summary-catchup
2496           ,@(if (featurep 'xemacs) '(t)
2497               '(:help "Mark unread articles in this group as read"))]
2498          ["Catchup all" gnus-summary-catchup-all t]
2499          ["Catchup to here" gnus-summary-catchup-to-here t]
2500          ["Catchup from here" gnus-summary-catchup-from-here t]
2501          ["Catchup region" gnus-summary-mark-region-as-read
2502           (gnus-mark-active-p)]
2503          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2504         ("Mark Various"
2505          ["Tick" gnus-summary-tick-article-forward t]
2506          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2507          ["Remove marks" gnus-summary-clear-mark-forward t]
2508          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2509          ["Set bookmark" gnus-summary-set-bookmark t]
2510          ["Remove bookmark" gnus-summary-remove-bookmark t])
2511         ("Limit to"
2512          ["Marks..." gnus-summary-limit-to-marks t]
2513          ["Subject..." gnus-summary-limit-to-subject t]
2514          ["Author..." gnus-summary-limit-to-author t]
2515          ["Recipient..." gnus-summary-limit-to-recipient t]
2516          ["Age..." gnus-summary-limit-to-age t]
2517          ["Extra..." gnus-summary-limit-to-extra t]
2518          ["Score..." gnus-summary-limit-to-score t]
2519          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2520          ["Unread" gnus-summary-limit-to-unread t]
2521          ["Unseen" gnus-summary-limit-to-unseen t]
2522          ["Replied" gnus-summary-limit-to-replied t]
2523          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2524          ["Next articles" gnus-summary-limit-to-articles t]
2525          ["Pop limit" gnus-summary-pop-limit t]
2526          ["Show dormant" gnus-summary-limit-include-dormant t]
2527          ["Hide childless dormant"
2528           gnus-summary-limit-exclude-childless-dormant t]
2529          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2530          ["Hide marked" gnus-summary-limit-exclude-marks t]
2531          ["Show expunged" gnus-summary-limit-include-expunged t])
2532         ("Process Mark"
2533          ["Set mark" gnus-summary-mark-as-processable t]
2534          ["Remove mark" gnus-summary-unmark-as-processable t]
2535          ["Remove all marks" gnus-summary-unmark-all-processable t]
2536          ["Invert marks" gnus-uu-invert-processable t]
2537          ["Mark above" gnus-uu-mark-over t]
2538          ["Mark series" gnus-uu-mark-series t]
2539          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2540          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2541          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2542          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2543          ["Mark all" gnus-uu-mark-all t]
2544          ["Mark buffer" gnus-uu-mark-buffer t]
2545          ["Mark sparse" gnus-uu-mark-sparse t]
2546          ["Mark thread" gnus-uu-mark-thread t]
2547          ["Unmark thread" gnus-uu-unmark-thread t]
2548          ("Process Mark Sets"
2549           ["Kill" gnus-summary-kill-process-mark t]
2550           ["Yank" gnus-summary-yank-process-mark
2551            gnus-newsgroup-process-stack]
2552           ["Save" gnus-summary-save-process-mark t]
2553           ["Run command on marked..." gnus-summary-universal-argument t]))
2554         ("Scroll article"
2555          ["Page forward" gnus-summary-next-page
2556           ,@(if (featurep 'xemacs) '(t)
2557               '(:help "Show next page of article"))]
2558          ["Page backward" gnus-summary-prev-page
2559           ,@(if (featurep 'xemacs) '(t)
2560               '(:help "Show previous page of article"))]
2561          ["Line forward" gnus-summary-scroll-up t])
2562         ("Move"
2563          ["Next unread article" gnus-summary-next-unread-article t]
2564          ["Previous unread article" gnus-summary-prev-unread-article t]
2565          ["Next article" gnus-summary-next-article t]
2566          ["Previous article" gnus-summary-prev-article t]
2567          ["Next unread subject" gnus-summary-next-unread-subject t]
2568          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2569          ["Next article same subject" gnus-summary-next-same-subject t]
2570          ["Previous article same subject" gnus-summary-prev-same-subject t]
2571          ["First unread article" gnus-summary-first-unread-article t]
2572          ["Best unread article" gnus-summary-best-unread-article t]
2573          ["Go to subject number..." gnus-summary-goto-subject t]
2574          ["Go to article number..." gnus-summary-goto-article t]
2575          ["Go to the last article" gnus-summary-goto-last-article t]
2576          ["Pop article off history" gnus-summary-pop-article t])
2577         ("Sort"
2578          ["Sort by number" gnus-summary-sort-by-number t]
2579          ["Sort by author" gnus-summary-sort-by-author t]
2580          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2581          ["Sort by subject" gnus-summary-sort-by-subject t]
2582          ["Sort by date" gnus-summary-sort-by-date t]
2583          ["Sort by score" gnus-summary-sort-by-score t]
2584          ["Sort by lines" gnus-summary-sort-by-lines t]
2585          ["Sort by characters" gnus-summary-sort-by-chars t]
2586          ["Randomize" gnus-summary-sort-by-random t]
2587          ["Original sort" gnus-summary-sort-by-original t])
2588         ("Help"
2589          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2590          ["Describe group" gnus-summary-describe-group t]
2591          ["Fetch charter" gnus-group-fetch-charter
2592           ,@(if (featurep 'xemacs) nil
2593               '(:help "Display the charter of the current group"))]
2594          ["Fetch control message" gnus-group-fetch-control
2595           ,@(if (featurep 'xemacs) nil
2596               '(:help "Display the archived control message for the current group"))]
2597          ["Read manual" gnus-info-find-node t])
2598         ("Modes"
2599          ["Pick and read" gnus-pick-mode t]
2600          ["Binary" gnus-binary-mode t])
2601         ("Regeneration"
2602          ["Regenerate" gnus-summary-prepare t]
2603          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2604          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2605          ["Toggle threading" gnus-summary-toggle-threads t])
2606         ["See old articles" gnus-summary-insert-old-articles t]
2607         ["See new articles" gnus-summary-insert-new-articles t]
2608         ["Filter articles..." gnus-summary-execute-command t]
2609         ["Run command on articles..." gnus-summary-universal-argument t]
2610         ["Search articles forward..." gnus-summary-search-article-forward t]
2611         ["Search articles backward..." gnus-summary-search-article-backward t]
2612         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2613         ["Expand window" gnus-summary-expand-window t]
2614         ["Expire expirable articles" gnus-summary-expire-articles
2615          (gnus-check-backend-function
2616           'request-expire-articles gnus-newsgroup-name)]
2617         ["Edit local kill file" gnus-summary-edit-local-kill t]
2618         ["Edit main kill file" gnus-summary-edit-global-kill t]
2619         ["Edit group parameters" gnus-summary-edit-parameters t]
2620         ["Customize group parameters" gnus-summary-customize-parameters t]
2621         ["Send a bug report" gnus-bug t]
2622         ("Exit"
2623          ["Catchup and exit" gnus-summary-catchup-and-exit
2624           ,@(if (featurep 'xemacs) '(t)
2625               '(:help "Mark unread articles in this group as read, then exit"))]
2626          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2627          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2628          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2629          ["Exit group" gnus-summary-exit
2630           ,@(if (featurep 'xemacs) '(t)
2631               '(:help "Exit current group, return to group selection mode"))]
2632          ["Exit group without updating" gnus-summary-exit-no-update t]
2633          ["Exit and goto next group" gnus-summary-next-group t]
2634          ["Exit and goto prev group" gnus-summary-prev-group t]
2635          ["Reselect group" gnus-summary-reselect-current-group t]
2636          ["Rescan group" gnus-summary-rescan-group t]
2637          ["Update dribble" gnus-summary-save-newsrc t])))
2638
2639     (gnus-run-hooks 'gnus-summary-menu-hook)))
2640
2641 (defvar gnus-summary-tool-bar-map nil)
2642
2643 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2644 ;; affect _new_ message buffers.  We might add a function that walks thru all
2645 ;; summary-mode buffers and force the update.
2646 (defun gnus-summary-tool-bar-update (&optional symbol value)
2647   "Update summary mode toolbar.
2648 Setter function for custom variables."
2649   (setq-default gnus-summary-tool-bar-map nil)
2650   (when symbol
2651     ;; When used as ":set" function:
2652     (set-default symbol value))
2653   (when (gnus-buffer-live-p gnus-summary-buffer)
2654     (with-current-buffer gnus-summary-buffer
2655       (gnus-summary-make-tool-bar))))
2656
2657 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2658                                      'gnus-summary-tool-bar-gnome
2659                                    'gnus-summary-tool-bar-retro)
2660   "Specifies the Gnus summary tool bar.
2661
2662 It can be either a list or a symbol refering to a list.  See
2663 `gmm-tool-bar-from-list' for the format of the list.  The
2664 default key map is `gnus-summary-mode-map'.
2665
2666 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2667 `gnus-summary-tool-bar-retro'."
2668   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2669                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2670                  (repeat :tag "User defined list" gmm-tool-bar-item)
2671                  (symbol))
2672   :version "23.0" ;; No Gnus
2673   :initialize 'custom-initialize-default
2674   :set 'gnus-summary-tool-bar-update
2675   :group 'gnus-summary)
2676
2677 (defcustom gnus-summary-tool-bar-gnome
2678   '((gnus-summary-post-news "mail/compose" nil)
2679     (gnus-summary-insert-new-articles "mail/inbox" nil
2680                                       :visible (or (not gnus-agent)
2681                                                    gnus-plugged))
2682     (gnus-summary-reply-with-original "mail/reply")
2683     (gnus-summary-reply "mail/reply" nil :visible nil)
2684     (gnus-summary-followup-with-original "mail/reply-all")
2685     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2686     (gnus-summary-mail-forward "mail/forward")
2687     (gnus-summary-save-article "mail/save")
2688     (gnus-summary-search-article-forward "search" nil :visible nil)
2689     (gnus-summary-print-article "print")
2690     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2691     ;; Some new commands that may need more suitable icons:
2692     (gnus-summary-save-newsrc "save" nil :visible nil)
2693     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2694     (gnus-summary-prev-article "left-arrow")
2695     (gnus-summary-next-article "right-arrow")
2696     (gnus-summary-next-page "next-page")
2697     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2698     ;;
2699     ;; Maybe some sort-by-... could be added:
2700     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2701     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2702     (gnus-summary-mark-as-expirable
2703      "delete" nil
2704      :visible (gnus-check-backend-function 'request-expire-articles
2705                                            gnus-newsgroup-name))
2706     (gnus-summary-mark-as-spam
2707      "mail/spam" t
2708      :visible (and (fboundp 'spam-group-ham-contents-p)
2709                    (spam-group-ham-contents-p gnus-newsgroup-name))
2710      :help "Mark as spam")
2711     (gnus-summary-mark-as-read-forward
2712      "mail/not-spam" nil
2713      :visible (and (fboundp 'spam-group-spam-contents-p)
2714                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2715     ;;
2716     (gnus-summary-exit "exit")
2717     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2718     (gnus-info-find-node "help"))
2719   "List of functions for the summary tool bar (GNOME style).
2720
2721 See `gmm-tool-bar-from-list' for the format of the list."
2722   :type '(repeat gmm-tool-bar-item)
2723   :version "23.0" ;; No Gnus
2724   :initialize 'custom-initialize-default
2725   :set 'gnus-summary-tool-bar-update
2726   :group 'gnus-summary)
2727
2728 (defcustom gnus-summary-tool-bar-retro
2729   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2730     (gnus-summary-next-unread-article "gnus/next-ur")
2731     (gnus-summary-post-news "gnus/post")
2732     (gnus-summary-followup-with-original "gnus/fuwo")
2733     (gnus-summary-followup "gnus/followup")
2734     (gnus-summary-reply-with-original "gnus/reply-wo")
2735     (gnus-summary-reply "gnus/reply")
2736     (gnus-summary-caesar-message "gnus/rot13")
2737     (gnus-uu-decode-uu "gnus/uu-decode")
2738     (gnus-summary-save-article-file "gnus/save-aif")
2739     (gnus-summary-save-article "gnus/save-art")
2740     (gnus-uu-post-news "gnus/uu-post")
2741     (gnus-summary-catchup "gnus/catchup")
2742     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2743     (gnus-summary-exit "gnus/exit-summ")
2744     ;; Some new command that may need more suitable icons:
2745     (gnus-summary-print-article "gnus/print" nil :visible nil)
2746     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2747     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2748     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2749     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2750     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2751     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2752     ;;
2753     (gnus-info-find-node "gnus/help" nil :visible nil))
2754   "List of functions for the summary tool bar (retro look).
2755
2756 See `gmm-tool-bar-from-list' for the format of the list."
2757   :type '(repeat gmm-tool-bar-item)
2758   :version "23.0" ;; No Gnus
2759   :initialize 'custom-initialize-default
2760   :set 'gnus-summary-tool-bar-update
2761   :group 'gnus-summary)
2762
2763 (defcustom gnus-summary-tool-bar-zap-list t
2764   "List of icon items from the global tool bar.
2765 These items are not displayed in the Gnus summary mode tool bar.
2766
2767 See `gmm-tool-bar-from-list' for the format of the list."
2768   :type 'gmm-tool-bar-zap-list
2769   :version "23.0" ;; No Gnus
2770   :initialize 'custom-initialize-default
2771   :set 'gnus-summary-tool-bar-update
2772   :group 'gnus-summary)
2773
2774 (defvar image-load-path)
2775
2776 (defun gnus-summary-make-tool-bar (&optional force)
2777   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2778 When FORCE, rebuild the tool bar."
2779   (when (and (not (featurep 'xemacs))
2780              (boundp 'tool-bar-mode)
2781              tool-bar-mode
2782              (or (not gnus-summary-tool-bar-map) force))
2783     (let* ((load-path
2784             (gmm-image-load-path-for-library "gnus"
2785                                              "mail/save.xpm"
2786                                              nil t))
2787            (image-load-path (cons (car load-path)
2788                                   (when (boundp 'image-load-path)
2789                                     image-load-path)))
2790            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2791                                         gnus-summary-tool-bar-zap-list
2792                                         'gnus-summary-mode-map)))
2793       (when map
2794         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2795         ;; uses it's value.
2796         (setq gnus-summary-tool-bar-map map))))
2797   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2798
2799 (defun gnus-score-set-default (var value)
2800   "A version of set that updates the GNU Emacs menu-bar."
2801   (set var value)
2802   ;; It is the message that forces the active status to be updated.
2803   (message ""))
2804
2805 (defun gnus-make-score-map (type)
2806   "Make a summary score map of type TYPE."
2807   (if t
2808       nil
2809     (let ((headers '(("author" "from" string)
2810                      ("subject" "subject" string)
2811                      ("article body" "body" string)
2812                      ("article head" "head" string)
2813                      ("xref" "xref" string)
2814                      ("extra header" "extra" string)
2815                      ("lines" "lines" number)
2816                      ("followups to author" "followup" string)))
2817           (types '((number ("less than" <)
2818                            ("greater than" >)
2819                            ("equal" =))
2820                    (string ("substring" s)
2821                            ("exact string" e)
2822                            ("fuzzy string" f)
2823                            ("regexp" r))))
2824           (perms '(("temporary" (current-time-string))
2825                    ("permanent" nil)
2826                    ("immediate" now)))
2827           header)
2828       (list
2829        (apply
2830         'nconc
2831         (list
2832          (if (eq type 'lower)
2833              "Lower score"
2834            "Increase score"))
2835         (let (outh)
2836           (while headers
2837             (setq header (car headers))
2838             (setq outh
2839                   (cons
2840                    (apply
2841                     'nconc
2842                     (list (car header))
2843                     (let ((ts (cdr (assoc (nth 2 header) types)))
2844                           outt)
2845                       (while ts
2846                         (setq outt
2847                               (cons
2848                                (apply
2849                                 'nconc
2850                                 (list (caar ts))
2851                                 (let ((ps perms)
2852                                       outp)
2853                                   (while ps
2854                                     (setq outp
2855                                           (cons
2856                                            (vector
2857                                             (caar ps)
2858                                             (list
2859                                              'gnus-summary-score-entry
2860                                              (nth 1 header)
2861                                              (if (or (string= (nth 1 header)
2862                                                               "head")
2863                                                      (string= (nth 1 header)
2864                                                               "body"))
2865                                                  ""
2866                                                (list 'gnus-summary-header
2867                                                      (nth 1 header)))
2868                                              (list 'quote (nth 1 (car ts)))
2869                                              (list 'gnus-score-delta-default
2870                                                    nil)
2871                                              (nth 1 (car ps))
2872                                              t)
2873                                             t)
2874                                            outp))
2875                                     (setq ps (cdr ps)))
2876                                   (list (nreverse outp))))
2877                                outt))
2878                         (setq ts (cdr ts)))
2879                       (list (nreverse outt))))
2880                    outh))
2881             (setq headers (cdr headers)))
2882           (list (nreverse outh))))))))
2883
2884 \f
2885
2886 (defun gnus-summary-mode (&optional group)
2887   "Major mode for reading articles.
2888
2889 All normal editing commands are switched off.
2890 \\<gnus-summary-mode-map>
2891 Each line in this buffer represents one article.  To read an
2892 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2893 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2894 respectively.
2895
2896 You can also post articles and send mail from this buffer.  To
2897 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2898 of an article, type `\\[gnus-summary-reply]'.
2899
2900 There are approx. one gazillion commands you can execute in this
2901 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2902
2903 The following commands are available:
2904
2905 \\{gnus-summary-mode-map}"
2906   (interactive)
2907   (kill-all-local-variables)
2908   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2909     (gnus-summary-make-local-variables))
2910   (gnus-summary-make-local-variables)
2911   (setq gnus-newsgroup-name group)
2912   (when (gnus-visual-p 'summary-menu 'menu)
2913     (gnus-summary-make-menu-bar)
2914     (gnus-summary-make-tool-bar))
2915   (gnus-make-thread-indent-array)
2916   (gnus-simplify-mode-line)
2917   (setq major-mode 'gnus-summary-mode)
2918   (setq mode-name "Summary")
2919   (make-local-variable 'minor-mode-alist)
2920   (use-local-map gnus-summary-mode-map)
2921   (buffer-disable-undo)
2922   (setq buffer-read-only t              ;Disable modification
2923         show-trailing-whitespace nil)
2924   (setq truncate-lines t)
2925   (setq selective-display t)
2926   (setq selective-display-ellipses t)   ;Display `...'
2927   (gnus-summary-set-display-table)
2928   (gnus-set-default-directory)
2929   (make-local-variable 'gnus-summary-line-format)
2930   (make-local-variable 'gnus-summary-line-format-spec)
2931   (make-local-variable 'gnus-summary-dummy-line-format)
2932   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2933   (make-local-variable 'gnus-summary-mark-positions)
2934   (gnus-make-local-hook 'pre-command-hook)
2935   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2936   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2937   (turn-on-gnus-mailing-list-mode)
2938   (mm-enable-multibyte)
2939   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2940   (gnus-update-summary-mark-positions))
2941
2942 (defun gnus-summary-make-local-variables ()
2943   "Make all the local summary buffer variables."
2944   (let (global)
2945     (dolist (local gnus-summary-local-variables)
2946       (if (consp local)
2947           (progn
2948             (if (eq (cdr local) 'global)
2949                 ;; Copy the global value of the variable.
2950                 (setq global (symbol-value (car local)))
2951               ;; Use the value from the list.
2952               (setq global (eval (cdr local))))
2953             (set (make-local-variable (car local)) global))
2954         ;; Simple nil-valued local variable.
2955         (set (make-local-variable local) nil)))))
2956
2957 (defun gnus-summary-clear-local-variables ()
2958   (let ((locals gnus-summary-local-variables))
2959     (while locals
2960       (if (consp (car locals))
2961           (and (symbolp (caar locals))
2962                (set (caar locals) nil))
2963         (and (symbolp (car locals))
2964              (set (car locals) nil)))
2965       (setq locals (cdr locals)))))
2966
2967 ;; Summary data functions.
2968
2969 (defmacro gnus-data-number (data)
2970   `(car ,data))
2971
2972 (defmacro gnus-data-set-number (data number)
2973   `(setcar ,data ,number))
2974
2975 (defmacro gnus-data-mark (data)
2976   `(nth 1 ,data))
2977
2978 (defmacro gnus-data-set-mark (data mark)
2979   `(setcar (nthcdr 1 ,data) ,mark))
2980
2981 (defmacro gnus-data-pos (data)
2982   `(nth 2 ,data))
2983
2984 (defmacro gnus-data-set-pos (data pos)
2985   `(setcar (nthcdr 2 ,data) ,pos))
2986
2987 (defmacro gnus-data-header (data)
2988   `(nth 3 ,data))
2989
2990 (defmacro gnus-data-set-header (data header)
2991   `(setf (nth 3 ,data) ,header))
2992
2993 (defmacro gnus-data-level (data)
2994   `(nth 4 ,data))
2995
2996 (defmacro gnus-data-unread-p (data)
2997   `(= (nth 1 ,data) gnus-unread-mark))
2998
2999 (defmacro gnus-data-read-p (data)
3000   `(/= (nth 1 ,data) gnus-unread-mark))
3001
3002 (defmacro gnus-data-pseudo-p (data)
3003   `(consp (nth 3 ,data)))
3004
3005 (defmacro gnus-data-find (number)
3006   `(assq ,number gnus-newsgroup-data))
3007
3008 (defmacro gnus-data-find-list (number &optional data)
3009   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3010      (memq (assq ,number bdata)
3011            bdata)))
3012
3013 (defmacro gnus-data-make (number mark pos header level)
3014   `(list ,number ,mark ,pos ,header ,level))
3015
3016 (defun gnus-data-enter (after-article number mark pos header level offset)
3017   (let ((data (gnus-data-find-list after-article)))
3018     (unless data
3019       (error "No such article: %d" after-article))
3020     (setcdr data (cons (gnus-data-make number mark pos header level)
3021                        (cdr data)))
3022     (setq gnus-newsgroup-data-reverse nil)
3023     (gnus-data-update-list (cddr data) offset)))
3024
3025 (defun gnus-data-enter-list (after-article list &optional offset)
3026   (when list
3027     (let ((data (and after-article (gnus-data-find-list after-article)))
3028           (ilist list))
3029       (if (not (or data
3030                    after-article))
3031           (let ((odata gnus-newsgroup-data))
3032             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3033             (when offset
3034               (gnus-data-update-list odata offset)))
3035         ;; Find the last element in the list to be spliced into the main
3036         ;; list.
3037         (setq list (last list))
3038         (if (not data)
3039             (progn
3040               (setcdr list gnus-newsgroup-data)
3041               (setq gnus-newsgroup-data ilist)
3042               (when offset
3043                 (gnus-data-update-list (cdr list) offset)))
3044           (setcdr list (cdr data))
3045           (setcdr data ilist)
3046           (when offset
3047             (gnus-data-update-list (cdr list) offset))))
3048       (setq gnus-newsgroup-data-reverse nil))))
3049
3050 (defun gnus-data-remove (article &optional offset)
3051   (let ((data gnus-newsgroup-data))
3052     (if (= (gnus-data-number (car data)) article)
3053         (progn
3054           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3055                 gnus-newsgroup-data-reverse nil)
3056           (when offset
3057             (gnus-data-update-list gnus-newsgroup-data offset)))
3058       (while (cdr data)
3059         (when (= (gnus-data-number (cadr data)) article)
3060           (setcdr data (cddr data))
3061           (when offset
3062             (gnus-data-update-list (cdr data) offset))
3063           (setq data nil
3064                 gnus-newsgroup-data-reverse nil))
3065         (setq data (cdr data))))))
3066
3067 (defmacro gnus-data-list (backward)
3068   `(if ,backward
3069        (or gnus-newsgroup-data-reverse
3070            (setq gnus-newsgroup-data-reverse
3071                  (reverse gnus-newsgroup-data)))
3072      gnus-newsgroup-data))
3073
3074 (defun gnus-data-update-list (data offset)
3075   "Add OFFSET to the POS of all data entries in DATA."
3076   (setq gnus-newsgroup-data-reverse nil)
3077   (while data
3078     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3079     (setq data (cdr data))))
3080
3081 (defun gnus-summary-article-pseudo-p (article)
3082   "Say whether this article is a pseudo article or not."
3083   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3084
3085 (defmacro gnus-summary-article-sparse-p (article)
3086   "Say whether this article is a sparse article or not."
3087   `(memq ,article gnus-newsgroup-sparse))
3088
3089 (defmacro gnus-summary-article-ancient-p (article)
3090   "Say whether this article is a sparse article or not."
3091   `(memq ,article gnus-newsgroup-ancient))
3092
3093 (defun gnus-article-parent-p (number)
3094   "Say whether this article is a parent or not."
3095   (let ((data (gnus-data-find-list number)))
3096     (and (cdr data)              ; There has to be an article after...
3097          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3098             (gnus-data-level (nth 1 data))))))
3099
3100 (defun gnus-article-children (number)
3101   "Return a list of all children to NUMBER."
3102   (let* ((data (gnus-data-find-list number))
3103          (level (gnus-data-level (car data)))
3104          children)
3105     (setq data (cdr data))
3106     (while (and data
3107                 (= (gnus-data-level (car data)) (1+ level)))
3108       (push (gnus-data-number (car data)) children)
3109       (setq data (cdr data)))
3110     children))
3111
3112 (defmacro gnus-summary-skip-intangible ()
3113   "If the current article is intangible, then jump to a different article."
3114   '(let ((to (get-text-property (point) 'gnus-intangible)))
3115      (and to (gnus-summary-goto-subject to))))
3116
3117 (defmacro gnus-summary-article-intangible-p ()
3118   "Say whether this article is intangible or not."
3119   '(get-text-property (point) 'gnus-intangible))
3120
3121 (defun gnus-article-read-p (article)
3122   "Say whether ARTICLE is read or not."
3123   (not (or (memq article gnus-newsgroup-marked)
3124            (memq article gnus-newsgroup-spam-marked)
3125            (memq article gnus-newsgroup-unreads)
3126            (memq article gnus-newsgroup-unselected)
3127            (memq article gnus-newsgroup-dormant))))
3128
3129 ;; Some summary mode macros.
3130
3131 (defmacro gnus-summary-article-number ()
3132   "The article number of the article on the current line.
3133 If there isn't an article number here, then we return the current
3134 article number."
3135   '(progn
3136      (gnus-summary-skip-intangible)
3137      (or (get-text-property (point) 'gnus-number)
3138          (gnus-summary-last-subject))))
3139
3140 (defmacro gnus-summary-article-header (&optional number)
3141   "Return the header of article NUMBER."
3142   `(gnus-data-header (gnus-data-find
3143                       ,(or number '(gnus-summary-article-number)))))
3144
3145 (defmacro gnus-summary-thread-level (&optional number)
3146   "Return the level of thread that starts with article NUMBER."
3147   `(if (and (eq gnus-summary-make-false-root 'dummy)
3148             (get-text-property (point) 'gnus-intangible))
3149        0
3150      (gnus-data-level (gnus-data-find
3151                        ,(or number '(gnus-summary-article-number))))))
3152
3153 (defmacro gnus-summary-article-mark (&optional number)
3154   "Return the mark of article NUMBER."
3155   `(gnus-data-mark (gnus-data-find
3156                     ,(or number '(gnus-summary-article-number)))))
3157
3158 (defmacro gnus-summary-article-pos (&optional number)
3159   "Return the position of the line of article NUMBER."
3160   `(gnus-data-pos (gnus-data-find
3161                    ,(or number '(gnus-summary-article-number)))))
3162
3163 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3164 (defmacro gnus-summary-article-subject (&optional number)
3165   "Return current subject string or nil if nothing."
3166   `(let ((headers
3167           ,(if number
3168                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3169              '(gnus-data-header (assq (gnus-summary-article-number)
3170                                       gnus-newsgroup-data)))))
3171      (and headers
3172           (vectorp headers)
3173           (mail-header-subject headers))))
3174
3175 (defmacro gnus-summary-article-score (&optional number)
3176   "Return current article score."
3177   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3178                   gnus-newsgroup-scored))
3179        gnus-summary-default-score 0))
3180
3181 (defun gnus-summary-article-children (&optional number)
3182   "Return a list of article numbers that are children of article NUMBER."
3183   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3184          (level (gnus-data-level (car data)))
3185          l children)
3186     (while (and (setq data (cdr data))
3187                 (> (setq l (gnus-data-level (car data))) level))
3188       (and (= (1+ level) l)
3189            (push (gnus-data-number (car data))
3190                  children)))
3191     (nreverse children)))
3192
3193 (defun gnus-summary-article-parent (&optional number)
3194   "Return the article number of the parent of article NUMBER."
3195   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3196                                     (gnus-data-list t)))
3197          (level (gnus-data-level (car data))))
3198     (if (zerop level)
3199         ()                              ; This is a root.
3200       ;; We search until we find an article with a level less than
3201       ;; this one.  That function has to be the parent.
3202       (while (and (setq data (cdr data))
3203                   (not (< (gnus-data-level (car data)) level))))
3204       (and data (gnus-data-number (car data))))))
3205
3206 (defun gnus-unread-mark-p (mark)
3207   "Say whether MARK is the unread mark."
3208   (= mark gnus-unread-mark))
3209
3210 (defun gnus-read-mark-p (mark)
3211   "Say whether MARK is one of the marks that mark as read.
3212 This is all marks except unread, ticked, dormant, and expirable."
3213   (not (or (= mark gnus-unread-mark)
3214            (= mark gnus-ticked-mark)
3215            (= mark gnus-spam-mark)
3216            (= mark gnus-dormant-mark)
3217            (= mark gnus-expirable-mark))))
3218
3219 (defmacro gnus-article-mark (number)
3220   "Return the MARK of article NUMBER.
3221 This macro should only be used when computing the mark the \"first\"
3222 time; i.e., when generating the summary lines.  After that,
3223 `gnus-summary-article-mark' should be used to examine the
3224 marks of articles."
3225   `(cond
3226     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3227     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3228     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3229     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3230     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3231     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3232     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3233     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3234            gnus-ancient-mark))))
3235
3236 ;; Saving hidden threads.
3237
3238 (defmacro gnus-save-hidden-threads (&rest forms)
3239   "Save hidden threads, eval FORMS, and restore the hidden threads."
3240   (let ((config (make-symbol "config")))
3241     `(let ((,config (gnus-hidden-threads-configuration)))
3242        (unwind-protect
3243            (save-excursion
3244              ,@forms)
3245          (gnus-restore-hidden-threads-configuration ,config)))))
3246 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3247 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3248
3249 (defun gnus-data-compute-positions ()
3250   "Compute the positions of all articles."
3251   (setq gnus-newsgroup-data-reverse nil)
3252   (let ((data gnus-newsgroup-data))
3253     (save-excursion
3254       (gnus-save-hidden-threads
3255         (gnus-summary-show-all-threads)
3256         (goto-char (point-min))
3257         (while data
3258           (while (get-text-property (point) 'gnus-intangible)
3259             (forward-line 1))
3260           (gnus-data-set-pos (car data) (+ (point) 3))
3261           (setq data (cdr data))
3262           (forward-line 1))))))
3263
3264 (defun gnus-hidden-threads-configuration ()
3265   "Return the current hidden threads configuration."
3266   (save-excursion
3267     (let (config)
3268       (goto-char (point-min))
3269       (while (search-forward "\r" nil t)
3270         (push (1- (point)) config))
3271       config)))
3272
3273 (defun gnus-restore-hidden-threads-configuration (config)
3274   "Restore hidden threads configuration from CONFIG."
3275   (save-excursion
3276     (let (point buffer-read-only)
3277       (while (setq point (pop config))
3278         (when (and (< point (point-max))
3279                    (goto-char point)
3280                    (eq (char-after) ?\n))
3281           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3282
3283 ;; Various summary mode internalish functions.
3284
3285 (defun gnus-mouse-pick-article (e)
3286   (interactive "e")
3287   (mouse-set-point e)
3288   (gnus-summary-next-page nil t))
3289
3290 (defun gnus-summary-set-display-table ()
3291   "Change the display table.
3292 Odd characters have a tendency to mess
3293 up nicely formatted displays - we make all possible glyphs
3294 display only a single character."
3295
3296   ;; We start from the standard display table, if any.
3297   (let ((table (or (copy-sequence standard-display-table)
3298                    (make-display-table)))
3299         (i 32))
3300     ;; Nix out all the control chars...
3301     (while (>= (setq i (1- i)) 0)
3302       (aset table i [??]))
3303    ;; ... but not newline and cr, of course.  (cr is necessary for the
3304     ;; selective display).
3305     (aset table ?\n nil)
3306     (aset table ?\r nil)
3307     ;; We keep TAB as well.
3308     (aset table ?\t nil)
3309     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3310     ;; Emacs 23 (unicode), that are not set already.
3311     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3312                  160
3313                256)))
3314       (while (>= (setq i (1- i)) 127)
3315         ;; Only modify if the entry is nil.
3316         (unless (aref table i)
3317           (aset table i [??]))))
3318     (setq buffer-display-table table)))
3319
3320 (defun gnus-summary-set-article-display-arrow (pos)
3321   "Update the overlay arrow to point to line at position POS."
3322   (when (and gnus-summary-display-arrow
3323              (boundp 'overlay-arrow-position)
3324              (boundp 'overlay-arrow-string))
3325     (save-excursion
3326       (goto-char pos)
3327       (beginning-of-line)
3328       (unless overlay-arrow-position
3329         (setq overlay-arrow-position (make-marker)))
3330       (setq overlay-arrow-string "=>"
3331             overlay-arrow-position (set-marker overlay-arrow-position
3332                                                (point)
3333                                                (current-buffer))))))
3334
3335 (defun gnus-summary-setup-buffer (group)
3336   "Initialize summary buffer."
3337   (let ((buffer (gnus-summary-buffer-name group))
3338         (dead-name (concat "*Dead Summary "
3339                            (gnus-group-decoded-name group) "*")))
3340     ;; If a dead summary buffer exists, we kill it.
3341     (when (gnus-buffer-live-p dead-name)
3342       (gnus-kill-buffer dead-name))
3343     (if (get-buffer buffer)
3344         (progn
3345           (set-buffer buffer)
3346           (setq gnus-summary-buffer (current-buffer))
3347           (not gnus-newsgroup-prepared))
3348       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3349       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3350       (gnus-summary-mode group)
3351       (when gnus-carpal
3352         (gnus-carpal-setup-buffer 'summary))
3353       (unless gnus-single-article-buffer
3354         (make-local-variable 'gnus-article-buffer)
3355         (make-local-variable 'gnus-article-current)
3356         (make-local-variable 'gnus-original-article-buffer))
3357       (setq gnus-newsgroup-name group)
3358       ;; Set any local variables in the group parameters.
3359       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3360       t)))
3361
3362 (defun gnus-set-global-variables ()
3363   "Set the global equivalents of the buffer-local variables.
3364 They are set to the latest values they had.  These reflect the summary
3365 buffer that was in action when the last article was fetched."
3366   (when (eq major-mode 'gnus-summary-mode)
3367     (setq gnus-summary-buffer (current-buffer))
3368     (let ((name gnus-newsgroup-name)
3369           (marked gnus-newsgroup-marked)
3370           (spam gnus-newsgroup-spam-marked)
3371           (unread gnus-newsgroup-unreads)
3372           (headers gnus-current-headers)
3373           (data gnus-newsgroup-data)
3374           (summary gnus-summary-buffer)
3375           (article-buffer gnus-article-buffer)
3376           (original gnus-original-article-buffer)
3377           (gac gnus-article-current)
3378           (reffed gnus-reffed-article-number)
3379           (score-file gnus-current-score-file)
3380           (default-charset gnus-newsgroup-charset)
3381           vlist)
3382       (let ((locals gnus-newsgroup-variables))
3383         (while locals
3384           (if (consp (car locals))
3385               (push (eval (caar locals)) vlist)
3386             (push (eval (car locals)) vlist))
3387           (setq locals (cdr locals)))
3388         (setq vlist (nreverse vlist)))
3389       (with-current-buffer gnus-group-buffer
3390         (setq gnus-newsgroup-name name
3391               gnus-newsgroup-marked marked
3392               gnus-newsgroup-spam-marked spam
3393               gnus-newsgroup-unreads unread
3394               gnus-current-headers headers
3395               gnus-newsgroup-data data
3396               gnus-article-current gac
3397               gnus-summary-buffer summary
3398               gnus-article-buffer article-buffer
3399               gnus-original-article-buffer original
3400               gnus-reffed-article-number reffed
3401               gnus-current-score-file score-file
3402               gnus-newsgroup-charset default-charset)
3403         (let ((locals gnus-newsgroup-variables))
3404           (while locals
3405             (if (consp (car locals))
3406                 (set (caar locals) (pop vlist))
3407               (set (car locals) (pop vlist)))
3408             (setq locals (cdr locals))))
3409         ;; The article buffer also has local variables.
3410         (when (gnus-buffer-live-p gnus-article-buffer)
3411           (set-buffer gnus-article-buffer)
3412           (setq gnus-summary-buffer summary))))))
3413
3414 (defun gnus-summary-article-unread-p (article)
3415   "Say whether ARTICLE is unread or not."
3416   (memq article gnus-newsgroup-unreads))
3417
3418 (defun gnus-summary-first-article-p (&optional article)
3419   "Return whether ARTICLE is the first article in the buffer."
3420   (if (not (setq article (or article (gnus-summary-article-number))))
3421       nil
3422     (eq article (caar gnus-newsgroup-data))))
3423
3424 (defun gnus-summary-last-article-p (&optional article)
3425   "Return whether ARTICLE is the last article in the buffer."
3426   (if (not (setq article (or article (gnus-summary-article-number))))
3427       ;; All non-existent numbers are the last article.  :-)
3428       t
3429     (not (cdr (gnus-data-find-list article)))))
3430
3431 (defun gnus-make-thread-indent-array ()
3432   (let ((n 200))
3433     (unless (and gnus-thread-indent-array
3434                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3435       (setq gnus-thread-indent-array (make-vector 201 "")
3436             gnus-thread-indent-array-level gnus-thread-indent-level)
3437       (while (>= n 0)
3438         (aset gnus-thread-indent-array n
3439               (make-string (* n gnus-thread-indent-level) ? ))
3440         (setq n (1- n))))))
3441
3442 (defun gnus-update-summary-mark-positions ()
3443   "Compute where the summary marks are to go."
3444   (save-excursion
3445     (when (gnus-buffer-exists-p gnus-summary-buffer)
3446       (set-buffer gnus-summary-buffer))
3447     (let ((spec gnus-summary-line-format-spec)
3448           pos)
3449       (save-excursion
3450         (gnus-set-work-buffer)
3451         (let ((gnus-tmp-unread ?Z)
3452               (gnus-replied-mark ?Z)
3453               (gnus-score-below-mark ?Z)
3454               (gnus-score-over-mark ?Z)
3455               (gnus-undownloaded-mark ?Z)
3456               (gnus-summary-line-format-spec spec)
3457               (gnus-newsgroup-downloadable '(0))
3458               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3459               case-fold-search ignores)
3460           ;; Here, all marks are bound to Z.
3461           (gnus-summary-insert-line header
3462                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3463           (goto-char (point-min))
3464           ;; Memorize the positions of the same characters as dummy marks.
3465           (while (re-search-forward "[A-D]" nil t)
3466             (push (point) ignores))
3467           (erase-buffer)
3468           ;; We use A-D as dummy marks in order to know column positions
3469           ;; where marks should be inserted.
3470           (setq gnus-tmp-unread ?A
3471                 gnus-replied-mark ?B
3472                 gnus-score-below-mark ?C
3473                 gnus-score-over-mark ?C
3474                 gnus-undownloaded-mark ?D)
3475           (gnus-summary-insert-line header
3476                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3477           ;; Ignore characters which aren't dummy marks.
3478           (dolist (p ignores)
3479             (delete-region (goto-char (1- p)) p)
3480             (insert ?Z))
3481           (goto-char (point-min))
3482           (setq pos (list (cons 'unread
3483                                 (and (search-forward "A" nil t)
3484                                      (- (point) (point-min) 1)))))
3485           (goto-char (point-min))
3486           (push (cons 'replied (and (search-forward "B" nil t)
3487                                     (- (point) (point-min) 1)))
3488                 pos)
3489           (goto-char (point-min))
3490           (push (cons 'score (and (search-forward "C" nil t)
3491                                   (- (point) (point-min) 1)))
3492                 pos)
3493           (goto-char (point-min))
3494           (push (cons 'download (and (search-forward "D" nil t)
3495                                      (- (point) (point-min) 1)))
3496                 pos)))
3497       (setq gnus-summary-mark-positions pos))))
3498
3499 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3500   "Insert a dummy root in the summary buffer."
3501   (beginning-of-line)
3502   (gnus-add-text-properties
3503    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3504    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3505
3506 (defun gnus-summary-extract-address-component (from)
3507   (or (car (funcall gnus-extract-address-components from))
3508       from))
3509
3510 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3511   (let ((mail-parse-charset gnus-newsgroup-charset)
3512         ; Is it really necessary to do this next part for each summary line?
3513         ; Luckily, doesn't seem to slow things down much.
3514         (mail-parse-ignored-charsets
3515          (with-current-buffer gnus-summary-buffer
3516            gnus-newsgroup-ignored-charsets)))
3517     (or
3518      (and gnus-ignored-from-addresses
3519           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3520           (let ((extra-headers (mail-header-extra header))
3521                 to
3522                 newsgroups)
3523             (cond
3524              ((setq to (cdr (assq 'To extra-headers)))
3525               (concat gnus-summary-to-prefix
3526                       (inline
3527                         (gnus-summary-extract-address-component
3528                          (funcall gnus-decode-encoded-word-function to)))))
3529              ((setq newsgroups
3530                     (or
3531                      (cdr (assq 'Newsgroups extra-headers))
3532                      (and
3533                       (memq 'Newsgroups gnus-extra-headers)
3534                       (eq (car (gnus-find-method-for-group
3535                                 gnus-newsgroup-name)) 'nntp)
3536                       (gnus-group-real-name gnus-newsgroup-name))))
3537               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3538      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3539
3540 (defun gnus-summary-insert-line (gnus-tmp-header
3541                                  gnus-tmp-level gnus-tmp-current
3542                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3543                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3544                                  &optional gnus-tmp-dummy gnus-tmp-score
3545                                  gnus-tmp-process)
3546   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3547          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3548          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3549          (gnus-tmp-score-char
3550           (if (or (null gnus-summary-default-score)
3551                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3552                       gnus-summary-zcore-fuzz))
3553               ?                         ;Whitespace
3554             (if (< gnus-tmp-score gnus-summary-default-score)
3555                 gnus-score-below-mark gnus-score-over-mark)))
3556          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3557          (gnus-tmp-replied
3558           (cond (gnus-tmp-process gnus-process-mark)
3559                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3560                  gnus-cached-mark)
3561                 (gnus-tmp-replied gnus-replied-mark)
3562                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3563                  gnus-forwarded-mark)
3564                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3565                  gnus-saved-mark)
3566                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3567                  gnus-recent-mark)
3568                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3569                  gnus-unseen-mark)
3570                 (t gnus-no-mark)))
3571          (gnus-tmp-downloaded
3572           (cond (undownloaded
3573                  gnus-undownloaded-mark)
3574                 (gnus-newsgroup-agentized
3575                  gnus-downloaded-mark)
3576                 (t
3577                  gnus-no-mark)))
3578          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3579          (gnus-tmp-name
3580           (cond
3581            ((string-match "<[^>]+> *$" gnus-tmp-from)
3582             (let ((beg (match-beginning 0)))
3583               (or (and (string-match "^\".+\"" gnus-tmp-from)
3584                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3585                   (substring gnus-tmp-from 0 beg))))
3586            ((string-match "(.+)" gnus-tmp-from)
3587             (substring gnus-tmp-from
3588                        (1+ (match-beginning 0)) (1- (match-end 0))))
3589            (t gnus-tmp-from)))
3590          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3591          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3592          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3593          (buffer-read-only nil))
3594     (when (string= gnus-tmp-name "")
3595       (setq gnus-tmp-name gnus-tmp-from))
3596     (unless (numberp gnus-tmp-lines)
3597       (setq gnus-tmp-lines -1))
3598     (if (= gnus-tmp-lines -1)
3599         (setq gnus-tmp-lines "?")
3600       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3601       (gnus-put-text-property
3602      (point)
3603      (progn (eval gnus-summary-line-format-spec) (point))
3604        'gnus-number gnus-tmp-number)
3605     (when (gnus-visual-p 'summary-highlight 'highlight)
3606       (forward-line -1)
3607       (gnus-run-hooks 'gnus-summary-update-hook)
3608       (forward-line 1))))
3609
3610 (defun gnus-summary-update-line (&optional dont-update)
3611   "Update summary line after change."
3612   (when (and gnus-summary-default-score
3613              (not gnus-summary-inhibit-highlight))
3614     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3615            (article (gnus-summary-article-number))
3616            (score (gnus-summary-article-score article)))
3617       (unless dont-update
3618         (if (and gnus-summary-mark-below
3619                  (< (gnus-summary-article-score)
3620                     gnus-summary-mark-below))
3621             ;; This article has a low score, so we mark it as read.
3622             (when (memq article gnus-newsgroup-unreads)
3623               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3624           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3625             ;; This article was previously marked as read on account
3626             ;; of a low score, but now it has risen, so we mark it as
3627             ;; unread.
3628             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3629         (gnus-summary-update-mark
3630          (if (or (null gnus-summary-default-score)
3631                  (<= (abs (- score gnus-summary-default-score))
3632                      gnus-summary-zcore-fuzz))
3633              ?                          ;Whitespace
3634            (if (< score gnus-summary-default-score)
3635                gnus-score-below-mark gnus-score-over-mark))
3636          'score))
3637       ;; Do visual highlighting.
3638       (when (gnus-visual-p 'summary-highlight 'highlight)
3639         (gnus-run-hooks 'gnus-summary-update-hook)))))
3640
3641 (defvar gnus-tmp-new-adopts nil)
3642
3643 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3644   "Return the number of articles in THREAD.
3645 This may be 0 in some cases -- if none of the articles in
3646 the thread are to be displayed."
3647   (let* ((number
3648          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3649           (cond
3650            ((not (listp thread))
3651             1)
3652            ((and (consp thread) (cdr thread))
3653             (apply
3654              '+ 1 (mapcar
3655                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3656            ((null thread)
3657             1)
3658            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3659             1)
3660            (t 0))))
3661     (when (and level (zerop level) gnus-tmp-new-adopts)
3662       (incf number
3663             (apply '+ (mapcar
3664                        'gnus-summary-number-of-articles-in-thread
3665                        gnus-tmp-new-adopts))))
3666     (if char
3667         (if (> number 1) gnus-not-empty-thread-mark
3668           gnus-empty-thread-mark)
3669       number)))
3670
3671 (defsubst gnus-summary-line-message-size (head)
3672   "Return pretty-printed version of message size.
3673 This function is intended to be used in
3674 `gnus-summary-line-format-alist'."
3675   (let ((c (or (mail-header-chars head) -1)))
3676     (cond ((< c 0) "n/a")               ; chars not available
3677           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3678           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3679           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3680           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3681
3682
3683 (defun gnus-summary-set-local-parameters (group)
3684   "Go through the local params of GROUP and set all variable specs in that list."
3685   (let ((vars '(quit-config)))          ; Ignore quit-config.
3686     (dolist (elem (gnus-group-find-parameter group))
3687       (and (consp elem)                 ; Has to be a cons.
3688            (consp (cdr elem))           ; The cdr has to be a list.
3689            (symbolp (car elem))         ; Has to be a symbol in there.
3690            (not (memq (car elem) vars))
3691            (ignore-errors               ; So we set it.
3692              (push (car elem) vars)
3693              (make-local-variable (car elem))
3694              (set (car elem) (eval (nth 1 elem))))))))
3695
3696 (defun gnus-summary-read-group (group &optional show-all no-article
3697                                       kill-buffer no-display backward
3698                                       select-articles)
3699   "Start reading news in newsgroup GROUP.
3700 If SHOW-ALL is non-nil, already read articles are also listed.
3701 If NO-ARTICLE is non-nil, no article is selected initially.
3702 If NO-DISPLAY, don't generate a summary buffer."
3703   (let (result)
3704     (while (and group
3705                 (null (setq result
3706                             (let ((gnus-auto-select-next nil))
3707                               (or (gnus-summary-read-group-1
3708                                    group show-all no-article
3709                                    kill-buffer no-display
3710                                    select-articles)
3711                                   (setq show-all nil
3712                                         select-articles nil)))))
3713                 (eq gnus-auto-select-next 'quietly))
3714       (set-buffer gnus-group-buffer)
3715       ;; The entry function called above goes to the next
3716       ;; group automatically, so we go two groups back
3717       ;; if we are searching for the previous group.
3718       (when backward
3719         (gnus-group-prev-unread-group 2))
3720       (if (not (equal group (gnus-group-group-name)))
3721           (setq group (gnus-group-group-name))
3722         (setq group nil)))
3723     result))
3724
3725 (defun gnus-summary-read-group-1 (group show-all no-article
3726                                         kill-buffer no-display
3727                                         &optional select-articles)
3728   ;; Killed foreign groups can't be entered.
3729   ;;  (when (and (not (gnus-group-native-p group))
3730   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3731   ;;    (error "Dead non-native groups can't be entered"))
3732   (gnus-message 5 "Retrieving newsgroup: %s..."
3733                 (gnus-group-decoded-name group))
3734   (let* ((new-group (gnus-summary-setup-buffer group))
3735          (quit-config (gnus-group-quit-config group))
3736          (did-select (and new-group (gnus-select-newsgroup
3737                                      group show-all select-articles))))
3738     (cond
3739      ;; This summary buffer exists already, so we just select it.
3740      ((not new-group)
3741       (gnus-set-global-variables)
3742       (when kill-buffer
3743         (gnus-kill-or-deaden-summary kill-buffer))
3744       (gnus-configure-windows 'summary 'force)
3745       (gnus-set-mode-line 'summary)
3746       (gnus-summary-position-point)
3747       (message "")
3748       t)
3749      ;; We couldn't select this group.
3750      ((null did-select)
3751       (when (and (eq major-mode 'gnus-summary-mode)
3752                  (not (equal (current-buffer) kill-buffer)))
3753         (kill-buffer (current-buffer))
3754         (if (not quit-config)
3755             (progn
3756               ;; Update the info -- marks might need to be removed,
3757               ;; for instance.
3758               (gnus-summary-update-info)
3759               (set-buffer gnus-group-buffer)
3760               (gnus-group-jump-to-group group)
3761               (gnus-group-next-unread-group 1))
3762           (gnus-handle-ephemeral-exit quit-config)))
3763       (let ((grpinfo (gnus-get-info group)))
3764         (if (null (gnus-info-read grpinfo))
3765             (gnus-message 3 "Group %s contains no messages"
3766                           (gnus-group-decoded-name group))
3767           (gnus-message 3 "Can't select group")))
3768       nil)
3769      ;; The user did a `C-g' while prompting for number of articles,
3770      ;; so we exit this group.
3771      ((eq did-select 'quit)
3772       (and (eq major-mode 'gnus-summary-mode)
3773            (not (equal (current-buffer) kill-buffer))
3774            (kill-buffer (current-buffer)))
3775       (when kill-buffer
3776         (gnus-kill-or-deaden-summary kill-buffer))
3777       (if (not quit-config)
3778           (progn
3779             (set-buffer gnus-group-buffer)
3780             (gnus-group-jump-to-group group)
3781             (gnus-group-next-unread-group 1)
3782             (gnus-configure-windows 'group 'force))
3783         (gnus-handle-ephemeral-exit quit-config))
3784       ;; Finally signal the quit.
3785       (signal 'quit nil))
3786      ;; The group was successfully selected.
3787      (t
3788       (gnus-set-global-variables)
3789       ;; Save the active value in effect when the group was entered.
3790       (setq gnus-newsgroup-active
3791             (gnus-copy-sequence
3792              (gnus-active gnus-newsgroup-name)))
3793       ;; You can change the summary buffer in some way with this hook.
3794       (gnus-run-hooks 'gnus-select-group-hook)
3795       (when (memq 'summary (gnus-update-format-specifications
3796                             nil 'summary 'summary-mode 'summary-dummy))
3797         ;; The format specification for the summary line was updated,
3798         ;; so we need to update the mark positions as well.
3799         (gnus-update-summary-mark-positions))
3800       ;; Do score processing.
3801       (when gnus-use-scoring
3802         (gnus-possibly-score-headers))
3803       ;; Check whether to fill in the gaps in the threads.
3804       (when gnus-build-sparse-threads
3805         (gnus-build-sparse-threads))
3806       ;; Find the initial limit.
3807       (if show-all
3808           (let ((gnus-newsgroup-dormant nil))
3809             (gnus-summary-initial-limit show-all))
3810         (gnus-summary-initial-limit show-all))
3811       ;; Generate the summary buffer.
3812       (unless no-display
3813         (gnus-summary-prepare))
3814       (when gnus-use-trees
3815         (gnus-tree-open group)
3816         (setq gnus-summary-highlight-line-function
3817               'gnus-tree-highlight-article))
3818       ;; If the summary buffer is empty, but there are some low-scored
3819       ;; articles or some excluded dormants, we include these in the
3820       ;; buffer.
3821       (when (and (zerop (buffer-size))
3822                  (not no-display))
3823         (cond (gnus-newsgroup-dormant
3824                (gnus-summary-limit-include-dormant))
3825               ((and gnus-newsgroup-scored show-all)
3826                (gnus-summary-limit-include-expunged t))))
3827       ;; Function `gnus-apply-kill-file' must be called in this hook.
3828       (gnus-run-hooks 'gnus-apply-kill-hook)
3829       (if (and (zerop (buffer-size))
3830                (not no-display))
3831           (progn
3832             ;; This newsgroup is empty.
3833             (gnus-summary-catchup-and-exit nil t)
3834             (gnus-message 6 "No unread news")
3835             (when kill-buffer
3836               (gnus-kill-or-deaden-summary kill-buffer))
3837             ;; Return nil from this function.
3838             nil)
3839         ;; Hide conversation thread subtrees.  We cannot do this in
3840         ;; gnus-summary-prepare-hook since kill processing may not
3841         ;; work with hidden articles.
3842         (gnus-summary-maybe-hide-threads)
3843         (when kill-buffer
3844           (gnus-kill-or-deaden-summary kill-buffer))
3845         (gnus-summary-auto-select-subject)
3846         ;; Show first unread article if requested.
3847         (if (and (not no-article)
3848                  (not no-display)
3849                  gnus-newsgroup-unreads
3850                  gnus-auto-select-first)
3851             (progn
3852               (gnus-configure-windows 'summary)
3853               (let ((art (gnus-summary-article-number)))
3854                 (unless (and (not gnus-plugged)
3855                              (or (memq art gnus-newsgroup-undownloaded)
3856                                  (memq art gnus-newsgroup-downloadable)))
3857                   (gnus-summary-goto-article art))))
3858           ;; Don't select any articles.
3859           (gnus-summary-position-point)
3860           (gnus-configure-windows 'summary 'force)
3861           (gnus-set-mode-line 'summary))
3862         (when (and gnus-auto-center-group
3863                    (get-buffer-window gnus-group-buffer t))
3864           ;; Gotta use windows, because recenter does weird stuff if
3865           ;; the current buffer ain't the displayed window.
3866           (let ((owin (selected-window)))
3867             (select-window (get-buffer-window gnus-group-buffer t))
3868             (when (gnus-group-goto-group group)
3869               (recenter))
3870             (select-window owin)))
3871         ;; Mark this buffer as "prepared".
3872         (setq gnus-newsgroup-prepared t)
3873         (gnus-run-hooks 'gnus-summary-prepared-hook)
3874         (unless (gnus-ephemeral-group-p group)
3875           (gnus-group-update-group group))
3876         t)))))
3877
3878 (defun gnus-summary-auto-select-subject ()
3879   "Select the subject line on initial group entry."
3880   (goto-char (point-min))
3881   (cond
3882    ((eq gnus-auto-select-subject 'best)
3883     (gnus-summary-best-unread-subject))
3884    ((eq gnus-auto-select-subject 'unread)
3885     (gnus-summary-first-unread-subject))
3886    ((eq gnus-auto-select-subject 'unseen)
3887     (gnus-summary-first-unseen-subject))
3888    ((eq gnus-auto-select-subject 'unseen-or-unread)
3889     (gnus-summary-first-unseen-or-unread-subject))
3890    ((eq gnus-auto-select-subject 'first)
3891     ;; Do nothing.
3892     )
3893    ((functionp gnus-auto-select-subject)
3894     (funcall gnus-auto-select-subject))))
3895
3896 (defun gnus-summary-prepare ()
3897   "Generate the summary buffer."
3898   (interactive)
3899   (let ((buffer-read-only nil))
3900     (erase-buffer)
3901     (setq gnus-newsgroup-data nil
3902           gnus-newsgroup-data-reverse nil)
3903     (gnus-run-hooks 'gnus-summary-generate-hook)
3904     ;; Generate the buffer, either with threads or without.
3905     (when gnus-newsgroup-headers
3906       (gnus-summary-prepare-threads
3907        (if gnus-show-threads
3908            (gnus-sort-gathered-threads
3909             (funcall gnus-summary-thread-gathering-function
3910                      (gnus-sort-threads
3911                       (gnus-cut-threads (gnus-make-threads)))))
3912          ;; Unthreaded display.
3913          (gnus-sort-articles gnus-newsgroup-headers))))
3914     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3915     ;; Call hooks for modifying summary buffer.
3916     (goto-char (point-min))
3917     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3918
3919 (defsubst gnus-general-simplify-subject (subject)
3920   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3921   (setq subject
3922         (cond
3923          ;; Truncate the subject.
3924          (gnus-simplify-subject-functions
3925           (gnus-map-function gnus-simplify-subject-functions subject))
3926          ((numberp gnus-summary-gather-subject-limit)
3927           (setq subject (gnus-simplify-subject-re subject))
3928           (if (> (length subject) gnus-summary-gather-subject-limit)
3929               (substring subject 0 gnus-summary-gather-subject-limit)
3930             subject))
3931          ;; Fuzzily simplify it.
3932          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3933           (gnus-simplify-subject-fuzzy subject))
3934          ;; Just remove the leading "Re:".
3935          (t
3936           (gnus-simplify-subject-re subject))))
3937
3938   (if (and gnus-summary-gather-exclude-subject
3939            (string-match gnus-summary-gather-exclude-subject subject))
3940       nil                         ; This article shouldn't be gathered
3941     subject))
3942
3943 (defun gnus-summary-simplify-subject-query ()
3944   "Query where the respool algorithm would put this article."
3945   (interactive)
3946   (gnus-summary-select-article)
3947   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3948
3949 (defun gnus-gather-threads-by-subject (threads)
3950   "Gather threads by looking at Subject headers."
3951   (if (not gnus-summary-make-false-root)
3952       threads
3953     (let ((hashtb (gnus-make-hashtable 1024))
3954           (prev threads)
3955           (result threads)
3956           subject hthread whole-subject)
3957       (while threads
3958         (setq subject (gnus-general-simplify-subject
3959                        (setq whole-subject (mail-header-subject
3960                                             (caar threads)))))
3961         (when subject
3962           (if (setq hthread (gnus-gethash subject hashtb))
3963               (progn
3964                 ;; We enter a dummy root into the thread, if we
3965                 ;; haven't done that already.
3966                 (unless (stringp (caar hthread))
3967                   (setcar hthread (list whole-subject (car hthread))))
3968                 ;; We add this new gathered thread to this gathered
3969                 ;; thread.
3970                 (setcdr (car hthread)
3971                         (nconc (cdar hthread) (list (car threads))))
3972                 ;; Remove it from the list of threads.
3973                 (setcdr prev (cdr threads))
3974                 (setq threads prev))
3975             ;; Enter this thread into the hash table.
3976             (gnus-sethash subject
3977                           (if gnus-summary-make-false-root-always
3978                               (progn
3979                                 ;; If you want a dummy root above all
3980                                 ;; threads...
3981                                 (setcar threads (list whole-subject
3982                                                       (car threads)))
3983                                 threads)
3984                             threads)
3985                           hashtb)))
3986         (setq prev threads)
3987         (setq threads (cdr threads)))
3988       result)))
3989
3990 (defun gnus-gather-threads-by-references (threads)
3991   "Gather threads by looking at References headers."
3992   (let ((idhashtb (gnus-make-hashtable 1024))
3993         (thhashtb (gnus-make-hashtable 1024))
3994         (prev threads)
3995         (result threads)
3996         ids references id gthread gid entered ref)
3997     (while threads
3998       (when (setq references (mail-header-references (caar threads)))
3999         (setq id (mail-header-id (caar threads))
4000               ids (inline (gnus-split-references references))
4001               entered nil)
4002         (while (setq ref (pop ids))
4003           (setq ids (delete ref ids))
4004           (if (not (setq gid (gnus-gethash ref idhashtb)))
4005               (progn
4006                 (gnus-sethash ref id idhashtb)
4007                 (gnus-sethash id threads thhashtb))
4008             (setq gthread (gnus-gethash gid thhashtb))
4009             (unless entered
4010               ;; We enter a dummy root into the thread, if we
4011               ;; haven't done that already.
4012               (unless (stringp (caar gthread))
4013                 (setcar gthread (list (mail-header-subject (caar gthread))
4014                                       (car gthread))))
4015               ;; We add this new gathered thread to this gathered
4016               ;; thread.
4017               (setcdr (car gthread)
4018                       (nconc (cdar gthread) (list (car threads)))))
4019             ;; Add it into the thread hash table.
4020             (gnus-sethash id gthread thhashtb)
4021             (setq entered t)
4022             ;; Remove it from the list of threads.
4023             (setcdr prev (cdr threads))
4024             (setq threads prev))))
4025       (setq prev threads)
4026       (setq threads (cdr threads)))
4027     result))
4028
4029 (defun gnus-sort-gathered-threads (threads)
4030   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4031   (let ((result threads))
4032     (while threads
4033       (when (stringp (caar threads))
4034         (setcdr (car threads)
4035                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4036       (setq threads (cdr threads)))
4037     result))
4038
4039 (defun gnus-thread-loop-p (root thread)
4040   "Say whether ROOT is in THREAD."
4041   (let ((stack (list thread))
4042         (infloop 0)
4043         th)
4044     (while (setq thread (pop stack))
4045       (setq th (cdr thread))
4046       (while (and th
4047                   (not (eq (caar th) root)))
4048         (pop th))
4049       (if th
4050           ;; We have found a loop.
4051           (let (ref-dep)
4052             (setcdr thread (delq (car th) (cdr thread)))
4053             (if (boundp (setq ref-dep (intern "none"
4054                                               gnus-newsgroup-dependencies)))
4055                 (setcdr (symbol-value ref-dep)
4056                         (nconc (cdr (symbol-value ref-dep))
4057                                (list (car th))))
4058               (set ref-dep (list nil (car th))))
4059             (setq infloop 1
4060                   stack nil))
4061         ;; Push all the subthreads onto the stack.
4062         (push (cdr thread) stack)))
4063     infloop))
4064
4065 (defun gnus-make-threads ()
4066   "Go through the dependency hashtb and find the roots.  Return all threads."
4067   (let (threads)
4068     (while (catch 'infloop
4069              (mapatoms
4070               (lambda (refs)
4071                 ;; Deal with self-referencing References loops.
4072                 (when (and (car (symbol-value refs))
4073                            (not (zerop
4074                                  (apply
4075                                   '+
4076                                   (mapcar
4077                                    (lambda (thread)
4078                                      (gnus-thread-loop-p
4079                                       (car (symbol-value refs)) thread))
4080                                    (cdr (symbol-value refs)))))))
4081                   (setq threads nil)
4082                   (throw 'infloop t))
4083                 (unless (car (symbol-value refs))
4084                   ;; These threads do not refer back to any other
4085                   ;; articles, so they're roots.
4086                   (setq threads (append (cdr (symbol-value refs)) threads))))
4087               gnus-newsgroup-dependencies)))
4088     threads))
4089
4090 ;; Build the thread tree.
4091 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4092   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4093
4094 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4095 if it was already present.
4096
4097 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4098 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4099 Message-IDs will be renamed to a unique Message-ID before being
4100 entered.
4101
4102 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4103   (let* ((id (mail-header-id header))
4104          (id-dep (and id (intern id dependencies)))
4105          parent-id ref ref-dep ref-header replaced)
4106     ;; Enter this `header' in the `dependencies' table.
4107     (cond
4108      ((not id-dep)
4109       (setq header nil))
4110      ;; The first two cases do the normal part: enter a new `header'
4111      ;; in the `dependencies' table.
4112      ((not (boundp id-dep))
4113       (set id-dep (list header)))
4114      ((null (car (symbol-value id-dep)))
4115       (setcar (symbol-value id-dep) header))
4116
4117      ;; From here the `header' was already present in the
4118      ;; `dependencies' table.
4119      (force-new
4120       ;; Overrides an existing entry;
4121       ;; just set the header part of the entry.
4122       (setcar (symbol-value id-dep) header)
4123       (setq replaced t))
4124
4125      ;; Renames the existing `header' to a unique Message-ID.
4126      ((not gnus-summary-ignore-duplicates)
4127       ;; An article with this Message-ID has already been seen.
4128       ;; We rename the Message-ID.
4129       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4130            (list header))
4131       (mail-header-set-id header id))
4132
4133      ;; The last case ignores an existing entry, except it adds any
4134      ;; additional Xrefs (in case the two articles came from different
4135      ;; servers.
4136      ;; Also sets `header' to `nil' meaning that the `dependencies'
4137      ;; table was *not* modified.
4138      (t
4139       (mail-header-set-xref
4140        (car (symbol-value id-dep))
4141        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4142                    "")
4143                (or (mail-header-xref header) "")))
4144       (setq header nil)))
4145
4146     (when (and header (not replaced))
4147       ;; First check that we are not creating a References loop.
4148       (setq parent-id (gnus-parent-id (mail-header-references header)))
4149       (setq ref parent-id)
4150       (while (and ref
4151                   (setq ref-dep (intern-soft ref dependencies))
4152                   (boundp ref-dep)
4153                   (setq ref-header (car (symbol-value ref-dep))))
4154         (if (string= id ref)
4155             ;; Yuk!  This is a reference loop.  Make the article be a
4156             ;; root article.
4157             (progn
4158               (mail-header-set-references (car (symbol-value id-dep)) "none")
4159               (setq ref nil)
4160               (setq parent-id nil))
4161           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4162       (setq ref-dep (intern (or parent-id "none") dependencies))
4163       (if (boundp ref-dep)
4164           (setcdr (symbol-value ref-dep)
4165                   (nconc (cdr (symbol-value ref-dep))
4166                          (list (symbol-value id-dep))))
4167         (set ref-dep (list nil (symbol-value id-dep)))))
4168     header))
4169
4170 (defun gnus-extract-message-id-from-in-reply-to (string)
4171   (if (string-match "<[^>]+>" string)
4172       (substring string (match-beginning 0) (match-end 0))
4173     nil))
4174
4175 (defun gnus-build-sparse-threads ()
4176   (let ((headers gnus-newsgroup-headers)
4177         (mail-parse-charset gnus-newsgroup-charset)
4178         (gnus-summary-ignore-duplicates t)
4179         header references generation relations
4180         subject child end new-child date)
4181     ;; First we create an alist of generations/relations, where
4182     ;; generations is how much we trust the relation, and the relation
4183     ;; is parent/child.
4184     (gnus-message 7 "Making sparse threads...")
4185     (save-excursion
4186       (nnheader-set-temp-buffer " *gnus sparse threads*")
4187       (while (setq header (pop headers))
4188         (when (and (setq references (mail-header-references header))
4189                    (not (string= references "")))
4190           (insert references)
4191           (setq child (mail-header-id header)
4192                 subject (mail-header-subject header)
4193                 date (mail-header-date header)
4194                 generation 0)
4195           (while (search-backward ">" nil t)
4196             (setq end (1+ (point)))
4197             (when (search-backward "<" nil t)
4198               (setq new-child (buffer-substring (point) end))
4199               (push (list (incf generation)
4200                           child (setq child new-child)
4201                           subject date)
4202                     relations)))
4203           (when child
4204             (push (list (1+ generation) child nil subject) relations))
4205           (erase-buffer)))
4206       (kill-buffer (current-buffer)))
4207     ;; Sort over trustworthiness.
4208     (mapcar
4209      (lambda (relation)
4210        (when (gnus-dependencies-add-header
4211               (make-full-mail-header
4212                gnus-reffed-article-number
4213                (nth 3 relation) "" (or (nth 4 relation) "")
4214                (nth 1 relation)
4215                (or (nth 2 relation) "") 0 0 "")
4216               gnus-newsgroup-dependencies nil)
4217          (push gnus-reffed-article-number gnus-newsgroup-limit)
4218          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4219          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4220                gnus-newsgroup-reads)
4221          (decf gnus-reffed-article-number)))
4222      (sort relations 'car-less-than-car))
4223     (gnus-message 7 "Making sparse threads...done")))
4224
4225 (defun gnus-build-old-threads ()
4226   ;; Look at all the articles that refer back to old articles, and
4227   ;; fetch the headers for the articles that aren't there.  This will
4228   ;; build complete threads - if the roots haven't been expired by the
4229   ;; server, that is.
4230   (let ((mail-parse-charset gnus-newsgroup-charset)
4231         id heads)
4232     (mapatoms
4233      (lambda (refs)
4234        (when (not (car (symbol-value refs)))
4235          (setq heads (cdr (symbol-value refs)))
4236          (while heads
4237            (if (memq (mail-header-number (caar heads))
4238                      gnus-newsgroup-dormant)
4239                (setq heads (cdr heads))
4240              (setq id (symbol-name refs))
4241              (while (and (setq id (gnus-build-get-header id))
4242                          (not (car (gnus-id-to-thread id)))))
4243              (setq heads nil)))))
4244      gnus-newsgroup-dependencies)))
4245
4246 (defsubst gnus-remove-odd-characters (string)
4247   "Translate STRING into something that doesn't contain weird characters."
4248   (mm-subst-char-in-string
4249    ?\r ?\-
4250    (mm-subst-char-in-string ?\n ?\- string t) t))
4251
4252 ;; This function has to be called with point after the article number
4253 ;; on the beginning of the line.
4254 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4255   (let ((eol (point-at-eol))
4256         (buffer (current-buffer))
4257         header references in-reply-to)
4258
4259     ;; overview: [num subject from date id refs chars lines misc]
4260     (unwind-protect
4261         (let (x)
4262           (narrow-to-region (point) eol)
4263           (unless (eobp)
4264             (forward-char))
4265
4266           (setq header
4267                 (make-full-mail-header
4268                  number                 ; number
4269                  (condition-case ()     ; subject
4270                      (gnus-remove-odd-characters
4271                       (funcall gnus-decode-encoded-word-function
4272                                (setq x (nnheader-nov-field))))
4273                    (error x))
4274                  (condition-case ()     ; from
4275                      (gnus-remove-odd-characters
4276                       (funcall gnus-decode-encoded-word-function
4277                                (setq x (nnheader-nov-field))))
4278                    (error x))
4279                  (nnheader-nov-field)   ; date
4280                  (nnheader-nov-read-message-id number)  ; id
4281                  (setq references (nnheader-nov-field)) ; refs
4282                  (nnheader-nov-read-integer) ; chars
4283                  (nnheader-nov-read-integer) ; lines
4284                  (unless (eobp)
4285                    (if (looking-at "Xref: ")
4286                        (goto-char (match-end 0)))
4287                    (nnheader-nov-field)) ; Xref
4288                  (nnheader-nov-parse-extra)))) ; extra
4289
4290       (widen))
4291
4292     (when (and (string= references "")
4293                (setq in-reply-to (mail-header-extra header))
4294                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4295       (mail-header-set-references
4296        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4297
4298     (when gnus-alter-header-function
4299       (funcall gnus-alter-header-function header))
4300     (gnus-dependencies-add-header header dependencies force-new)))
4301
4302 (defun gnus-build-get-header (id)
4303   "Look through the buffer of NOV lines and find the header to ID.
4304 Enter this line into the dependencies hash table, and return
4305 the id of the parent article (if any)."
4306   (let ((deps gnus-newsgroup-dependencies)
4307         found header)
4308     (prog1
4309         (save-excursion
4310           (set-buffer nntp-server-buffer)
4311           (let ((case-fold-search nil))
4312             (goto-char (point-min))
4313             (while (and (not found)
4314                         (search-forward id nil t))
4315               (beginning-of-line)
4316               (setq found (looking-at
4317                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4318                                    (regexp-quote id))))
4319               (or found (beginning-of-line 2)))
4320             (when found
4321               (beginning-of-line)
4322               (and
4323                (setq header (gnus-nov-parse-line
4324                              (read (current-buffer)) deps))
4325                (gnus-parent-id (mail-header-references header))))))
4326       (when header
4327         (let ((number (mail-header-number header)))
4328           (push number gnus-newsgroup-limit)
4329           (push header gnus-newsgroup-headers)
4330           (if (memq number gnus-newsgroup-unselected)
4331               (progn
4332                 (setq gnus-newsgroup-unreads
4333                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4334                                                number))
4335                 (setq gnus-newsgroup-unselected
4336                       (delq number gnus-newsgroup-unselected)))
4337             (push number gnus-newsgroup-ancient)))))))
4338
4339 (defun gnus-build-all-threads ()
4340   "Read all the headers."
4341   (let ((gnus-summary-ignore-duplicates t)
4342         (mail-parse-charset gnus-newsgroup-charset)
4343         (dependencies gnus-newsgroup-dependencies)
4344         header article)
4345     (save-excursion
4346       (set-buffer nntp-server-buffer)
4347       (let ((case-fold-search nil))
4348         (goto-char (point-min))
4349         (while (not (eobp))
4350           (ignore-errors
4351             (setq article (read (current-buffer))
4352                   header (gnus-nov-parse-line article dependencies)))
4353           (when header
4354             (with-current-buffer gnus-summary-buffer
4355               (push header gnus-newsgroup-headers)
4356               (if (memq (setq article (mail-header-number header))
4357                         gnus-newsgroup-unselected)
4358                   (progn
4359                     (setq gnus-newsgroup-unreads
4360                           (gnus-add-to-sorted-list
4361                            gnus-newsgroup-unreads article))
4362                     (setq gnus-newsgroup-unselected
4363                           (delq article gnus-newsgroup-unselected)))
4364                 (push article gnus-newsgroup-ancient)))
4365             (forward-line 1)))))))
4366
4367 (defun gnus-summary-update-article-line (article header)
4368   "Update the line for ARTICLE using HEADER."
4369   (let* ((id (mail-header-id header))
4370          (thread (gnus-id-to-thread id)))
4371     (unless thread
4372       (error "Article in no thread"))
4373     ;; Update the thread.
4374     (setcar thread header)
4375     (gnus-summary-goto-subject article)
4376     (let* ((datal (gnus-data-find-list article))
4377            (data (car datal))
4378            (buffer-read-only nil)
4379            (level (gnus-summary-thread-level)))
4380       (gnus-delete-line)
4381       (let ((inserted (- (point)
4382                          (progn
4383                            (gnus-summary-insert-line
4384                             header level nil
4385                             (memq article gnus-newsgroup-undownloaded)
4386                             (gnus-article-mark article)
4387                             (memq article gnus-newsgroup-replied)
4388                             (memq article gnus-newsgroup-expirable)
4389                             ;; Only insert the Subject string when it's different
4390                             ;; from the previous Subject string.
4391                             (if (and
4392                                  gnus-show-threads
4393                                  (gnus-subject-equal
4394                                   (condition-case ()
4395                                       (mail-header-subject
4396                                        (gnus-data-header
4397                                         (cadr
4398                                          (gnus-data-find-list
4399                                           article
4400                                           (gnus-data-list t)))))
4401                                     ;; Error on the side of excessive subjects.
4402                                     (error ""))
4403                                   (mail-header-subject header)))
4404                                 ""
4405                               (mail-header-subject header))
4406                             nil (cdr (assq article gnus-newsgroup-scored))
4407                             (memq article gnus-newsgroup-processable))
4408                            (point)))))
4409         (when (cdr datal)
4410           (gnus-data-update-list
4411            (cdr datal)
4412            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4413
4414 (defun gnus-summary-update-article (article &optional iheader)
4415   "Update ARTICLE in the summary buffer."
4416   (set-buffer gnus-summary-buffer)
4417   (let* ((header (gnus-summary-article-header article))
4418          (id (mail-header-id header))
4419          (data (gnus-data-find article))
4420          (thread (gnus-id-to-thread id))
4421          (references (mail-header-references header))
4422          (parent
4423           (gnus-id-to-thread
4424            (or (gnus-parent-id
4425                 (when (and references
4426                            (not (equal "" references)))
4427                   references))
4428                "none")))
4429          (buffer-read-only nil)
4430          (old (car thread)))
4431     (when thread
4432       (unless iheader
4433         (setcar thread nil)
4434         (when parent
4435           (delq thread parent)))
4436       (if (gnus-summary-insert-subject id header)
4437           ;; Set the (possibly) new article number in the data structure.
4438           (gnus-data-set-number data (gnus-id-to-article id))
4439         (setcar thread old)
4440         nil))))
4441
4442 (defun gnus-rebuild-thread (id &optional line)
4443   "Rebuild the thread containing ID.
4444 If LINE, insert the rebuilt thread starting on line LINE."
4445   (let ((buffer-read-only nil)
4446         old-pos current thread data)
4447     (if (not gnus-show-threads)
4448         (setq thread (list (car (gnus-id-to-thread id))))
4449       ;; Get the thread this article is part of.
4450       (setq thread (gnus-remove-thread id)))
4451     (setq old-pos (point-at-bol))
4452     (setq current (save-excursion
4453                     (and (re-search-backward "[\r\n]" nil t)
4454                          (gnus-summary-article-number))))
4455     ;; If this is a gathered thread, we have to go some re-gathering.
4456     (when (stringp (car thread))
4457       (let ((subject (car thread))
4458             roots thr)
4459         (setq thread (cdr thread))
4460         (while thread
4461           (unless (memq (setq thr (gnus-id-to-thread
4462                                    (gnus-root-id
4463                                     (mail-header-id (caar thread)))))
4464                         roots)
4465             (push thr roots))
4466           (setq thread (cdr thread)))
4467         ;; We now have all (unique) roots.
4468         (if (= (length roots) 1)
4469             ;; All the loose roots are now one solid root.
4470             (setq thread (car roots))
4471           (setq thread (cons subject (gnus-sort-threads roots))))))
4472     (let (threads)
4473       ;; We then insert this thread into the summary buffer.
4474       (when line
4475         (goto-char (point-min))
4476         (forward-line (1- line)))
4477       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4478         (if gnus-show-threads
4479             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4480           (gnus-summary-prepare-unthreaded thread))
4481         (setq data (nreverse gnus-newsgroup-data))
4482         (setq threads gnus-newsgroup-threads))
4483       ;; We splice the new data into the data structure.
4484       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4485       ;;!!! then we want to insert at the beginning of the buffer.
4486       ;;!!! That happens to be true with Gnus now, but that may
4487       ;;!!! change in the future.  Perhaps.
4488       (gnus-data-enter-list
4489        (if line nil current) data (- (point) old-pos))
4490       (setq gnus-newsgroup-threads
4491             (nconc threads gnus-newsgroup-threads))
4492       (gnus-data-compute-positions))))
4493
4494 (defun gnus-number-to-header (number)
4495   "Return the header for article NUMBER."
4496   (let ((headers gnus-newsgroup-headers))
4497     (while (and headers
4498                 (not (= number (mail-header-number (car headers)))))
4499       (pop headers))
4500     (when headers
4501       (car headers))))
4502
4503 (defun gnus-parent-headers (in-headers &optional generation)
4504   "Return the headers of the GENERATIONeth parent of HEADERS."
4505   (unless generation
4506     (setq generation 1))
4507   (let ((parent t)
4508         (headers in-headers)
4509         references)
4510     (while (and parent
4511                 (not (zerop generation))
4512                 (setq references (mail-header-references headers)))
4513       (setq headers (if (and references
4514                              (setq parent (gnus-parent-id references)))
4515                         (car (gnus-id-to-thread parent))
4516                       nil))
4517       (decf generation))
4518     (and (not (eq headers in-headers))
4519          headers)))
4520
4521 (defun gnus-id-to-thread (id)
4522   "Return the (sub-)thread where ID appears."
4523   (gnus-gethash id gnus-newsgroup-dependencies))
4524
4525 (defun gnus-id-to-article (id)
4526   "Return the article number of ID."
4527   (let ((thread (gnus-id-to-thread id)))
4528     (when (and thread
4529                (car thread))
4530       (mail-header-number (car thread)))))
4531
4532 (defun gnus-id-to-header (id)
4533   "Return the article headers of ID."
4534   (car (gnus-id-to-thread id)))
4535
4536 (defun gnus-article-displayed-root-p (article)
4537   "Say whether ARTICLE is a root(ish) article."
4538   (let ((level (gnus-summary-thread-level article))
4539         (refs (mail-header-references  (gnus-summary-article-header article)))
4540         particle)
4541     (cond
4542      ((null level) nil)
4543      ((zerop level) t)
4544      ((null refs) t)
4545      ((null (gnus-parent-id refs)) t)
4546      ((and (= 1 level)
4547            (null (setq particle (gnus-id-to-article
4548                                  (gnus-parent-id refs))))
4549            (null (gnus-summary-thread-level particle)))))))
4550
4551 (defun gnus-root-id (id)
4552   "Return the id of the root of the thread where ID appears."
4553   (let (last-id prev)
4554     (while (and id (setq prev (car (gnus-id-to-thread id))))
4555       (setq last-id id
4556             id (gnus-parent-id (mail-header-references prev))))
4557     last-id))
4558
4559 (defun gnus-articles-in-thread (thread)
4560   "Return the list of articles in THREAD."
4561   (cons (mail-header-number (car thread))
4562         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4563
4564 (defun gnus-remove-thread (id &optional dont-remove)
4565   "Remove the thread that has ID in it."
4566   (let (headers thread last-id)
4567     ;; First go up in this thread until we find the root.
4568     (setq last-id (gnus-root-id id)
4569           headers (message-flatten-list (gnus-id-to-thread last-id)))
4570     ;; We have now found the real root of this thread.  It might have
4571     ;; been gathered into some loose thread, so we have to search
4572     ;; through the threads to find the thread we wanted.
4573     (let ((threads gnus-newsgroup-threads)
4574           sub)
4575       (while threads
4576         (setq sub (car threads))
4577         (if (stringp (car sub))
4578             ;; This is a gathered thread, so we look at the roots
4579             ;; below it to find whether this article is in this
4580             ;; gathered root.
4581             (progn
4582               (setq sub (cdr sub))
4583               (while sub
4584                 (when (member (caar sub) headers)
4585                   (setq thread (car threads)
4586                         threads nil
4587                         sub nil))
4588                 (setq sub (cdr sub))))
4589           ;; It's an ordinary thread, so we check it.
4590           (when (eq (car sub) (car headers))
4591             (setq thread sub
4592                   threads nil)))
4593         (setq threads (cdr threads)))
4594       ;; If this article is in no thread, then it's a root.
4595       (if thread
4596           (unless dont-remove
4597             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4598         (setq thread (gnus-id-to-thread last-id)))
4599       (when thread
4600         (prog1
4601             thread                      ; We return this thread.
4602           (unless dont-remove
4603             (if (stringp (car thread))
4604                 (progn
4605                   ;; If we use dummy roots, then we have to remove the
4606                   ;; dummy root as well.
4607                   (when (eq gnus-summary-make-false-root 'dummy)
4608                     ;; We go to the dummy root by going to
4609                     ;; the first sub-"thread", and then one line up.
4610                     (gnus-summary-goto-article
4611                      (mail-header-number (caadr thread)))
4612                     (forward-line -1)
4613                     (gnus-delete-line)
4614                     (gnus-data-compute-positions))
4615                   (setq thread (cdr thread))
4616                   (while thread
4617                     (gnus-remove-thread-1 (car thread))
4618                     (setq thread (cdr thread))))
4619               (gnus-remove-thread-1 thread))))))))
4620
4621 (defun gnus-remove-thread-1 (thread)
4622   "Remove the thread THREAD recursively."
4623   (let ((number (mail-header-number (pop thread)))
4624         d)
4625     (setq thread (reverse thread))
4626     (while thread
4627       (gnus-remove-thread-1 (pop thread)))
4628     (when (setq d (gnus-data-find number))
4629       (goto-char (gnus-data-pos d))
4630       (gnus-summary-show-thread)
4631       (gnus-data-remove
4632        number
4633        (- (point-at-bol)
4634           (prog1
4635               (1+ (point-at-eol))
4636             (gnus-delete-line)))))))
4637
4638 (defun gnus-sort-threads-1 (threads func)
4639   (sort (mapcar (lambda (thread)
4640                   (cons (car thread)
4641                         (and (cdr thread)
4642                              (gnus-sort-threads-1 (cdr thread) func))))
4643                 threads) func))
4644
4645 (defun gnus-sort-threads (threads)
4646   "Sort THREADS."
4647   (if (not gnus-thread-sort-functions)
4648       threads
4649     (gnus-message 8 "Sorting threads...")
4650     (let ((max-lisp-eval-depth 5000))
4651       (prog1 (gnus-sort-threads-1
4652          threads
4653          (gnus-make-sort-function gnus-thread-sort-functions))
4654         (gnus-message 8 "Sorting threads...done")))))
4655
4656 (defun gnus-sort-articles (articles)
4657   "Sort ARTICLES."
4658   (when gnus-article-sort-functions
4659     (gnus-message 7 "Sorting articles...")
4660     (prog1
4661         (setq gnus-newsgroup-headers
4662               (sort articles (gnus-make-sort-function
4663                               gnus-article-sort-functions)))
4664       (gnus-message 7 "Sorting articles...done"))))
4665
4666 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4667 (defmacro gnus-thread-header (thread)
4668   "Return header of first article in THREAD.
4669 Note that THREAD must never, ever be anything else than a variable -
4670 using some other form will lead to serious barfage."
4671   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4672   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4673   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4674         (vector thread) 2))
4675
4676 (defsubst gnus-article-sort-by-number (h1 h2)
4677   "Sort articles by article number."
4678   (< (mail-header-number h1)
4679      (mail-header-number h2)))
4680
4681 (defun gnus-thread-sort-by-number (h1 h2)
4682   "Sort threads by root article number."
4683   (gnus-article-sort-by-number
4684    (gnus-thread-header h1) (gnus-thread-header h2)))
4685
4686 (defsubst gnus-article-sort-by-random (h1 h2)
4687   "Sort articles by article number."
4688   (zerop (random 2)))
4689
4690 (defun gnus-thread-sort-by-random (h1 h2)
4691   "Sort threads by root article number."
4692   (gnus-article-sort-by-random
4693    (gnus-thread-header h1) (gnus-thread-header h2)))
4694
4695 (defsubst gnus-article-sort-by-lines (h1 h2)
4696   "Sort articles by article Lines header."
4697   (< (mail-header-lines h1)
4698      (mail-header-lines h2)))
4699
4700 (defun gnus-thread-sort-by-lines (h1 h2)
4701   "Sort threads by root article Lines header."
4702   (gnus-article-sort-by-lines
4703    (gnus-thread-header h1) (gnus-thread-header h2)))
4704
4705 (defsubst gnus-article-sort-by-chars (h1 h2)
4706   "Sort articles by octet length."
4707   (< (mail-header-chars h1)
4708      (mail-header-chars h2)))
4709
4710 (defun gnus-thread-sort-by-chars (h1 h2)
4711   "Sort threads by root article octet length."
4712   (gnus-article-sort-by-chars
4713    (gnus-thread-header h1) (gnus-thread-header h2)))
4714
4715 (defsubst gnus-article-sort-by-author (h1 h2)
4716   "Sort articles by root author."
4717   (string-lessp
4718    (let ((extract (funcall
4719                    gnus-extract-address-components
4720                    (mail-header-from h1))))
4721      (or (car extract) (cadr extract) ""))
4722    (let ((extract (funcall
4723                    gnus-extract-address-components
4724                    (mail-header-from h2))))
4725      (or (car extract) (cadr extract) ""))))
4726
4727 (defun gnus-thread-sort-by-author (h1 h2)
4728   "Sort threads by root author."
4729   (gnus-article-sort-by-author
4730    (gnus-thread-header h1)  (gnus-thread-header h2)))
4731
4732 (defsubst gnus-article-sort-by-recipient (h1 h2)
4733   "Sort articles by recipient."
4734   (string-lessp
4735    (let ((extract (funcall
4736                    gnus-extract-address-components
4737                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4738      (or (car extract) (cadr extract)))
4739    (let ((extract (funcall
4740                    gnus-extract-address-components
4741                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4742      (or (car extract) (cadr extract)))))
4743
4744 (defun gnus-thread-sort-by-recipient (h1 h2)
4745   "Sort threads by root recipient."
4746   (gnus-article-sort-by-recipient
4747    (gnus-thread-header h1) (gnus-thread-header h2)))
4748
4749 (defsubst gnus-article-sort-by-subject (h1 h2)
4750   "Sort articles by root subject."
4751   (string-lessp
4752    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4753    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4754
4755 (defun gnus-thread-sort-by-subject (h1 h2)
4756   "Sort threads by root subject."
4757   (gnus-article-sort-by-subject
4758    (gnus-thread-header h1) (gnus-thread-header h2)))
4759
4760 (defsubst gnus-article-sort-by-date (h1 h2)
4761   "Sort articles by root article date."
4762   (time-less-p
4763    (gnus-date-get-time (mail-header-date h1))
4764    (gnus-date-get-time (mail-header-date h2))))
4765
4766 (defun gnus-thread-sort-by-date (h1 h2)
4767   "Sort threads by root article date."
4768   (gnus-article-sort-by-date
4769    (gnus-thread-header h1) (gnus-thread-header h2)))
4770
4771 (defsubst gnus-article-sort-by-score (h1 h2)
4772   "Sort articles by root article score.
4773 Unscored articles will be counted as having a score of zero."
4774   (> (or (cdr (assq (mail-header-number h1)
4775                     gnus-newsgroup-scored))
4776          gnus-summary-default-score 0)
4777      (or (cdr (assq (mail-header-number h2)
4778                     gnus-newsgroup-scored))
4779          gnus-summary-default-score 0)))
4780
4781 (defun gnus-thread-sort-by-score (h1 h2)
4782   "Sort threads by root article score."
4783   (gnus-article-sort-by-score
4784    (gnus-thread-header h1) (gnus-thread-header h2)))
4785
4786 (defun gnus-thread-sort-by-total-score (h1 h2)
4787   "Sort threads by the sum of all scores in the thread.
4788 Unscored articles will be counted as having a score of zero."
4789   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4790
4791 (defun gnus-thread-total-score (thread)
4792   ;; This function find the total score of THREAD.
4793   (cond
4794    ((null thread)
4795     0)
4796    ((consp thread)
4797     (if (stringp (car thread))
4798         (apply gnus-thread-score-function 0
4799                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4800       (gnus-thread-total-score-1 thread)))
4801    (t
4802     (gnus-thread-total-score-1 (list thread)))))
4803
4804 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4805   "Sort threads such that the thread with the most recently arrived article comes first."
4806   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4807
4808 (defun gnus-thread-highest-number (thread)
4809   "Return the highest article number in THREAD."
4810   (apply 'max (mapcar (lambda (header)
4811                         (mail-header-number header))
4812                       (message-flatten-list thread))))
4813
4814 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4815   "Sort threads such that the thread with the most recently dated article comes first."
4816   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4817
4818 (defun gnus-thread-latest-date (thread)
4819   "Return the highest article date in THREAD."
4820   (let ((previous-time 0))
4821     (apply 'max
4822            (mapcar
4823             (lambda (header)
4824               (setq previous-time
4825                     (condition-case ()
4826                         (time-to-seconds (mail-header-parse-date
4827                                           (mail-header-date header)))
4828                       (error previous-time))))
4829             (sort
4830              (message-flatten-list thread)
4831              (lambda (h1 h2)
4832                (< (mail-header-number h1)
4833                   (mail-header-number h2))))))))
4834
4835 (defun gnus-thread-total-score-1 (root)
4836   ;; This function find the total score of the thread below ROOT.
4837   (setq root (car root))
4838   (apply gnus-thread-score-function
4839          (or (append
4840               (mapcar 'gnus-thread-total-score
4841                       (cdr (gnus-id-to-thread (mail-header-id root))))
4842               (when (> (mail-header-number root) 0)
4843                 (list (or (cdr (assq (mail-header-number root)
4844                                      gnus-newsgroup-scored))
4845                           gnus-summary-default-score 0))))
4846              (list gnus-summary-default-score)
4847              '(0))))
4848
4849 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4850 (defvar gnus-tmp-prev-subject nil)
4851 (defvar gnus-tmp-false-parent nil)
4852 (defvar gnus-tmp-root-expunged nil)
4853 (defvar gnus-tmp-dummy-line nil)
4854
4855 (eval-when-compile (defvar gnus-tmp-header))
4856 (defun gnus-extra-header (type &optional header)
4857   "Return the extra header of TYPE."
4858   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4859       ""))
4860
4861 (defvar gnus-tmp-thread-tree-header-string "")
4862
4863 (defcustom gnus-sum-thread-tree-root "> "
4864   "With %B spec, used for the root of a thread.
4865 If nil, use subject instead."
4866   :version "22.1"
4867   :type '(radio (const :format "%v  " nil) string)
4868   :group 'gnus-thread)
4869
4870 (defcustom gnus-sum-thread-tree-false-root "> "
4871   "With %B spec, used for a false root of a thread.
4872 If nil, use subject instead."
4873   :version "22.1"
4874   :type '(radio (const :format "%v  " nil) string)
4875   :group 'gnus-thread)
4876
4877 (defcustom gnus-sum-thread-tree-single-indent ""
4878   "With %B spec, used for a thread with just one message.
4879 If nil, use subject instead."
4880   :version "22.1"
4881   :type '(radio (const :format "%v  " nil) string)
4882   :group 'gnus-thread)
4883
4884 (defcustom gnus-sum-thread-tree-vertical "| "
4885   "With %B spec, used for drawing a vertical line."
4886   :version "22.1"
4887   :type 'string
4888   :group 'gnus-thread)
4889
4890 (defcustom gnus-sum-thread-tree-indent "  "
4891   "With %B spec, used for indenting."
4892   :version "22.1"
4893   :type 'string
4894   :group 'gnus-thread)
4895
4896 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4897   "With %B spec, used for a leaf with brothers."
4898   :version "22.1"
4899   :type 'string
4900   :group 'gnus-thread)
4901
4902 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4903   "With %B spec, used for a leaf without brothers."
4904   :version "22.1"
4905   :type 'string
4906   :group 'gnus-thread)
4907
4908 (defun gnus-summary-prepare-threads (threads)
4909   "Prepare summary buffer from THREADS and indentation LEVEL.
4910 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4911 or a straight list of headers."
4912   (gnus-message 7 "Generating summary...")
4913
4914   (setq gnus-newsgroup-threads threads)
4915   (beginning-of-line)
4916
4917   (let ((gnus-tmp-level 0)
4918         (default-score (or gnus-summary-default-score 0))
4919         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4920         (building-line-count gnus-summary-display-while-building)
4921         (building-count (integerp gnus-summary-display-while-building))
4922         thread number subject stack state gnus-tmp-gathered beg-match
4923         new-roots gnus-tmp-new-adopts thread-end simp-subject
4924         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4925         gnus-tmp-replied gnus-tmp-subject-or-nil
4926         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4927         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4928         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4929         tree-stack)
4930
4931     (setq gnus-tmp-prev-subject nil
4932           gnus-tmp-thread-tree-header-string "")
4933
4934     (if (vectorp (car threads))
4935         ;; If this is a straight (sic) list of headers, then a
4936         ;; threaded summary display isn't required, so we just create
4937         ;; an unthreaded one.
4938         (gnus-summary-prepare-unthreaded threads)
4939
4940       ;; Do the threaded display.
4941
4942       (if gnus-summary-display-while-building
4943           (switch-to-buffer (buffer-name)))
4944       (while (or threads stack gnus-tmp-new-adopts new-roots)
4945
4946         (if (and (= gnus-tmp-level 0)
4947                  (or (not stack)
4948                      (= (caar stack) 0))
4949                  (not gnus-tmp-false-parent)
4950                  (or gnus-tmp-new-adopts new-roots))
4951             (if gnus-tmp-new-adopts
4952                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4953                       thread (list (car gnus-tmp-new-adopts))
4954                       gnus-tmp-header (caar thread)
4955                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4956               (when new-roots
4957                 (setq thread (list (car new-roots))
4958                       gnus-tmp-header (caar thread)
4959                       new-roots (cdr new-roots))))
4960
4961           (if threads
4962               ;; If there are some threads, we do them before the
4963               ;; threads on the stack.
4964               (setq thread threads
4965                     gnus-tmp-header (caar thread))
4966             ;; There were no current threads, so we pop something off
4967             ;; the stack.
4968             (setq state (car stack)
4969                   gnus-tmp-level (car state)
4970                   tree-stack (cadr state)
4971                   thread (caddr state)
4972                   stack (cdr stack)
4973                   gnus-tmp-header (caar thread))))
4974
4975         (setq gnus-tmp-false-parent nil)
4976         (setq gnus-tmp-root-expunged nil)
4977         (setq thread-end nil)
4978
4979         (if (stringp gnus-tmp-header)
4980             ;; The header is a dummy root.
4981             (cond
4982              ((eq gnus-summary-make-false-root 'adopt)
4983               ;; We let the first article adopt the rest.
4984               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4985                                                (cddar thread)))
4986               (setq gnus-tmp-gathered
4987                     (nconc (mapcar
4988                             (lambda (h) (mail-header-number (car h)))
4989                             (cddar thread))
4990                            gnus-tmp-gathered))
4991               (setq thread (cons (list (caar thread)
4992                                        (cadar thread))
4993                                  (cdr thread)))
4994               (setq gnus-tmp-level -1
4995                     gnus-tmp-false-parent t))
4996              ((eq gnus-summary-make-false-root 'empty)
4997               ;; We print adopted articles with empty subject fields.
4998               (setq gnus-tmp-gathered
4999                     (nconc (mapcar
5000                             (lambda (h) (mail-header-number (car h)))
5001                             (cddar thread))
5002                            gnus-tmp-gathered))
5003               (setq gnus-tmp-level -1))
5004              ((eq gnus-summary-make-false-root 'dummy)
5005               ;; We remember that we probably want to output a dummy
5006               ;; root.
5007               (setq gnus-tmp-dummy-line gnus-tmp-header)
5008               (setq gnus-tmp-prev-subject gnus-tmp-header))
5009              (t
5010               ;; We do not make a root for the gathered
5011               ;; sub-threads at all.
5012               (setq gnus-tmp-level -1)))
5013
5014           (setq number (mail-header-number gnus-tmp-header)
5015                 subject (mail-header-subject gnus-tmp-header)
5016                 simp-subject (gnus-simplify-subject-fully subject))
5017
5018           (cond
5019            ;; If the thread has changed subject, we might want to make
5020            ;; this subthread into a root.
5021            ((and (null gnus-thread-ignore-subject)
5022                  (not (zerop gnus-tmp-level))
5023                  gnus-tmp-prev-subject
5024                  (not (string= gnus-tmp-prev-subject simp-subject)))
5025             (setq new-roots (nconc new-roots (list (car thread)))
5026                   thread-end t
5027                   gnus-tmp-header nil))
5028            ;; If the article lies outside the current limit,
5029            ;; then we do not display it.
5030            ((not (memq number gnus-newsgroup-limit))
5031             (setq gnus-tmp-gathered
5032                   (nconc (mapcar
5033                           (lambda (h) (mail-header-number (car h)))
5034                           (cdar thread))
5035                          gnus-tmp-gathered))
5036             (setq gnus-tmp-new-adopts (if (cdar thread)
5037                                           (append gnus-tmp-new-adopts
5038                                                   (cdar thread))
5039                                         gnus-tmp-new-adopts)
5040                   thread-end t
5041                   gnus-tmp-header nil)
5042             (when (zerop gnus-tmp-level)
5043               (setq gnus-tmp-root-expunged t)))
5044            ;; Perhaps this article is to be marked as read?
5045            ((and gnus-summary-mark-below
5046                  (< (or (cdr (assq number gnus-newsgroup-scored))
5047                         default-score)
5048                     gnus-summary-mark-below)
5049                  ;; Don't touch sparse articles.
5050                  (not (gnus-summary-article-sparse-p number))
5051                  (not (gnus-summary-article-ancient-p number)))
5052             (setq gnus-newsgroup-unreads
5053                   (delq number gnus-newsgroup-unreads))
5054             (if gnus-newsgroup-auto-expire
5055                 (setq gnus-newsgroup-expirable
5056                       (gnus-add-to-sorted-list
5057                        gnus-newsgroup-expirable number))
5058               (push (cons number gnus-low-score-mark)
5059                     gnus-newsgroup-reads))))
5060
5061           (when gnus-tmp-header
5062             ;; We may have an old dummy line to output before this
5063             ;; article.
5064             (when (and gnus-tmp-dummy-line
5065                        (gnus-subject-equal
5066                         gnus-tmp-dummy-line
5067                         (mail-header-subject gnus-tmp-header)))
5068               (gnus-summary-insert-dummy-line
5069                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5070               (setq gnus-tmp-dummy-line nil))
5071
5072             ;; Compute the mark.
5073             (setq gnus-tmp-unread (gnus-article-mark number))
5074
5075             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5076                                   gnus-tmp-header gnus-tmp-level)
5077                   gnus-newsgroup-data)
5078
5079             ;; Actually insert the line.
5080             (setq
5081              gnus-tmp-subject-or-nil
5082              (cond
5083               ((and gnus-thread-ignore-subject
5084                     gnus-tmp-prev-subject
5085                     (not (string= gnus-tmp-prev-subject simp-subject)))
5086                subject)
5087               ((zerop gnus-tmp-level)
5088                (if (and (eq gnus-summary-make-false-root 'empty)
5089                         (memq number gnus-tmp-gathered)
5090                         gnus-tmp-prev-subject
5091                         (string= gnus-tmp-prev-subject simp-subject))
5092                    gnus-summary-same-subject
5093                  subject))
5094               (t gnus-summary-same-subject)))
5095             (if (and (eq gnus-summary-make-false-root 'adopt)
5096                      (= gnus-tmp-level 1)
5097                      (memq number gnus-tmp-gathered))
5098                 (setq gnus-tmp-opening-bracket ?\<
5099                       gnus-tmp-closing-bracket ?\>)
5100               (setq gnus-tmp-opening-bracket ?\[
5101                     gnus-tmp-closing-bracket ?\]))
5102             (setq
5103              gnus-tmp-indentation
5104              (aref gnus-thread-indent-array gnus-tmp-level)
5105              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5106              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5107                                 gnus-summary-default-score 0)
5108              gnus-tmp-score-char
5109              (if (or (null gnus-summary-default-score)
5110                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5111                          gnus-summary-zcore-fuzz))
5112                  ?                      ;Whitespace
5113                (if (< gnus-tmp-score gnus-summary-default-score)
5114                    gnus-score-below-mark gnus-score-over-mark))
5115              gnus-tmp-replied
5116              (cond ((memq number gnus-newsgroup-processable)
5117                     gnus-process-mark)
5118                    ((memq number gnus-newsgroup-cached)
5119                     gnus-cached-mark)
5120                    ((memq number gnus-newsgroup-replied)
5121                     gnus-replied-mark)
5122                    ((memq number gnus-newsgroup-forwarded)
5123                     gnus-forwarded-mark)
5124                    ((memq number gnus-newsgroup-saved)
5125                     gnus-saved-mark)
5126                    ((memq number gnus-newsgroup-recent)
5127                     gnus-recent-mark)
5128                    ((memq number gnus-newsgroup-unseen)
5129                     gnus-unseen-mark)
5130                    (t gnus-no-mark))
5131              gnus-tmp-downloaded
5132              (cond ((memq number gnus-newsgroup-undownloaded)
5133                     gnus-undownloaded-mark)
5134                    (gnus-newsgroup-agentized
5135                     gnus-downloaded-mark)
5136                    (t
5137                     gnus-no-mark))
5138              gnus-tmp-from (mail-header-from gnus-tmp-header)
5139              gnus-tmp-name
5140              (cond
5141               ((string-match "<[^>]+> *$" gnus-tmp-from)
5142                (setq beg-match (match-beginning 0))
5143                (or (and (string-match "^\".+\"" gnus-tmp-from)
5144                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5145                    (substring gnus-tmp-from 0 beg-match)))
5146               ((string-match "(.+)" gnus-tmp-from)
5147                (substring gnus-tmp-from
5148                           (1+ (match-beginning 0)) (1- (match-end 0))))
5149               (t gnus-tmp-from))
5150
5151              ;; Do the %B string
5152              gnus-tmp-thread-tree-header-string
5153              (cond
5154               ((not gnus-show-threads) "")
5155               ((zerop gnus-tmp-level)
5156                (cond ((cdar thread)
5157                       (or gnus-sum-thread-tree-root subject))
5158                      (gnus-tmp-new-adopts
5159                       (or gnus-sum-thread-tree-false-root subject))
5160                      (t
5161                       (or gnus-sum-thread-tree-single-indent subject))))
5162               (t
5163                (concat (apply 'concat
5164                               (mapcar (lambda (item)
5165                                         (if (= item 1)
5166                                             gnus-sum-thread-tree-vertical
5167                                           gnus-sum-thread-tree-indent))
5168                                       (cdr (reverse tree-stack))))
5169                        (if (nth 1 thread)
5170                            gnus-sum-thread-tree-leaf-with-other
5171                          gnus-sum-thread-tree-single-leaf)))))
5172             (when (string= gnus-tmp-name "")
5173               (setq gnus-tmp-name gnus-tmp-from))
5174             (unless (numberp gnus-tmp-lines)
5175               (setq gnus-tmp-lines -1))
5176             (if (= gnus-tmp-lines -1)
5177                 (setq gnus-tmp-lines "?")
5178               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5179               (gnus-put-text-property
5180              (point)
5181              (progn (eval gnus-summary-line-format-spec) (point))
5182                'gnus-number number)
5183             (when gnus-visual-p
5184               (forward-line -1)
5185               (gnus-run-hooks 'gnus-summary-update-hook)
5186               (forward-line 1))
5187
5188             (setq gnus-tmp-prev-subject simp-subject)))
5189
5190         (when (nth 1 thread)
5191           (push (list (max 0 gnus-tmp-level)
5192                       (copy-sequence tree-stack)
5193                       (nthcdr 1 thread))
5194                 stack))
5195         (push (if (nth 1 thread) 1 0) tree-stack)
5196         (incf gnus-tmp-level)
5197         (setq threads (if thread-end nil (cdar thread)))
5198         (if gnus-summary-display-while-building
5199             (if building-count
5200                 (progn
5201                   ;; use a set frequency
5202                   (setq building-line-count (1- building-line-count))
5203                   (when (= building-line-count 0)
5204                     (sit-for 0)
5205                     (setq building-line-count
5206                           gnus-summary-display-while-building)))
5207               ;; always
5208               (sit-for 0)))
5209         (unless threads
5210           (setq gnus-tmp-level 0)))))
5211   (gnus-message 7 "Generating summary...done"))
5212
5213 (defun gnus-summary-prepare-unthreaded (headers)
5214   "Generate an unthreaded summary buffer based on HEADERS."
5215   (let (header number mark)
5216
5217     (beginning-of-line)
5218
5219     (while headers
5220       ;; We may have to root out some bad articles...
5221       (when (memq (setq number (mail-header-number
5222                                 (setq header (pop headers))))
5223                   gnus-newsgroup-limit)
5224         ;; Mark article as read when it has a low score.
5225         (when (and gnus-summary-mark-below
5226                    (< (or (cdr (assq number gnus-newsgroup-scored))
5227                           gnus-summary-default-score 0)
5228                       gnus-summary-mark-below)
5229                    (not (gnus-summary-article-ancient-p number)))
5230           (setq gnus-newsgroup-unreads
5231                 (delq number gnus-newsgroup-unreads))
5232           (if gnus-newsgroup-auto-expire
5233               (push number gnus-newsgroup-expirable)
5234             (push (cons number gnus-low-score-mark)
5235                   gnus-newsgroup-reads)))
5236
5237         (setq mark (gnus-article-mark number))
5238         (push (gnus-data-make number mark (1+ (point)) header 0)
5239               gnus-newsgroup-data)
5240         (gnus-summary-insert-line
5241          header 0 number
5242          (memq number gnus-newsgroup-undownloaded)
5243          mark (memq number gnus-newsgroup-replied)
5244          (memq number gnus-newsgroup-expirable)
5245          (mail-header-subject header) nil
5246          (cdr (assq number gnus-newsgroup-scored))
5247          (memq number gnus-newsgroup-processable))))))
5248
5249 (defun gnus-summary-remove-list-identifiers ()
5250   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5251   (let ((regexp (if (consp gnus-list-identifiers)
5252                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5253                   gnus-list-identifiers))
5254         changed subject)
5255     (when regexp
5256       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5257       (dolist (header gnus-newsgroup-headers)
5258         (setq subject (mail-header-subject header)
5259               changed nil)
5260         (while (string-match regexp subject)
5261           (setq subject
5262                 (concat (substring subject 0 (match-beginning 1))
5263                         (substring subject (match-end 0)))
5264                 changed t))
5265         (when changed
5266           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5267             (setq subject
5268                   (concat (substring subject 0 (match-beginning 1))
5269                           (substring subject (match-end 1)))))
5270           (mail-header-set-subject header subject))))))
5271
5272 (defun gnus-fetch-headers (articles)
5273   "Fetch headers of ARTICLES."
5274   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5275     (gnus-message 5 "Fetching headers for %s..." name)
5276     (prog1
5277         (if (eq 'nov
5278                 (setq gnus-headers-retrieved-by
5279                       (gnus-retrieve-headers
5280                        articles gnus-newsgroup-name
5281                        ;; We might want to fetch old headers, but
5282                        ;; not if there is only 1 article.
5283                        (and (or (and
5284                                  (not (eq gnus-fetch-old-headers 'some))
5285                                  (not (numberp gnus-fetch-old-headers)))
5286                                 (> (length articles) 1))
5287                             gnus-fetch-old-headers))))
5288             (gnus-get-newsgroup-headers-xover
5289              articles nil nil gnus-newsgroup-name t)
5290           (gnus-get-newsgroup-headers))
5291       (gnus-message 5 "Fetching headers for %s...done" name))))
5292
5293 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5294   "Select newsgroup GROUP.
5295 If READ-ALL is non-nil, all articles in the group are selected.
5296 If SELECT-ARTICLES, only select those articles from GROUP."
5297   (let* ((entry (gnus-group-entry group))
5298          ;;!!! Dirty hack; should be removed.
5299          (gnus-summary-ignore-duplicates
5300           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5301               t
5302             gnus-summary-ignore-duplicates))
5303          (info (nth 2 entry))
5304          articles fetched-articles cached)
5305
5306     (unless (gnus-check-server
5307              (set (make-local-variable 'gnus-current-select-method)
5308                   (gnus-find-method-for-group group)))
5309       (error "Couldn't open server"))
5310
5311     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5312         (gnus-activate-group group)     ; Or we can activate it...
5313         (progn                          ; Or we bug out.
5314           (when (equal major-mode 'gnus-summary-mode)
5315             (gnus-kill-buffer (current-buffer)))
5316           (error "Couldn't activate group %s: %s"
5317                  group (gnus-status-message group))))
5318
5319     (unless (gnus-request-group group t)
5320       (when (equal major-mode 'gnus-summary-mode)
5321         (gnus-kill-buffer (current-buffer)))
5322       (error "Couldn't request group %s: %s"
5323              group (gnus-status-message group)))
5324
5325     (when gnus-agent
5326       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5327
5328       (setq gnus-summary-use-undownloaded-faces
5329             (gnus-agent-find-parameter
5330              group
5331              'agent-enable-undownloaded-faces)))
5332
5333     (setq gnus-newsgroup-name group
5334           gnus-newsgroup-unselected nil
5335           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5336
5337     (let ((display (gnus-group-find-parameter group 'display)))
5338       (setq gnus-newsgroup-display
5339             (cond
5340              ((not (zerop (or (car-safe read-all) 0)))
5341               ;; The user entered the group with C-u SPC/RET, let's show
5342               ;; all articles.
5343               'gnus-not-ignore)
5344              ((eq display 'all)
5345               'gnus-not-ignore)
5346              ((arrayp display)
5347               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5348              ((numberp display)
5349               ;; The following is probably the "correct" solution, but
5350               ;; it makes Gnus fetch all headers and then limit the
5351               ;; articles (which is slow), so instead we hack the
5352               ;; select-articles parameter instead. -- Simon Josefsson
5353               ;; <jas@kth.se>
5354               ;;
5355               ;; (gnus-byte-compile
5356               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5357               ;;                         display)))))
5358               (setq select-articles
5359                     (gnus-uncompress-range
5360                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5361                              (if (> tmp 0)
5362                                  tmp
5363                                1))
5364                            (cdr (gnus-active group)))))
5365               nil)
5366              (t
5367               nil))))
5368
5369     (gnus-summary-setup-default-charset)
5370
5371     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5372     (when (gnus-virtual-group-p group)
5373       (setq cached gnus-newsgroup-cached))
5374
5375     (setq gnus-newsgroup-unreads
5376           (gnus-sorted-ndifference
5377            (gnus-sorted-ndifference gnus-newsgroup-unreads
5378                                     gnus-newsgroup-marked)
5379            gnus-newsgroup-dormant))
5380
5381     (setq gnus-newsgroup-processable nil)
5382
5383     (gnus-update-read-articles group gnus-newsgroup-unreads)
5384
5385     ;; Adjust and set lists of article marks.
5386     (when info
5387       (gnus-adjust-marked-articles info))
5388     (if (setq articles select-articles)
5389         (setq gnus-newsgroup-unselected
5390               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5391       (setq articles (gnus-articles-to-read group read-all)))
5392
5393     (cond
5394      ((null articles)
5395       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5396       'quit)
5397      ((eq articles 0) nil)
5398      (t
5399       ;; Init the dependencies hash table.
5400       (setq gnus-newsgroup-dependencies
5401             (gnus-make-hashtable (length articles)))
5402       (gnus-set-global-variables)
5403       ;; Retrieve the headers and read them in.
5404
5405       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5406
5407       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5408       (when cached
5409         (setq gnus-newsgroup-cached cached))
5410
5411       ;; Suppress duplicates?
5412       (when gnus-suppress-duplicates
5413         (gnus-dup-suppress-articles))
5414
5415       ;; Set the initial limit.
5416       (setq gnus-newsgroup-limit (copy-sequence articles))
5417       ;; Remove canceled articles from the list of unread articles.
5418       (setq fetched-articles
5419             (mapcar (lambda (headers) (mail-header-number headers))
5420                     gnus-newsgroup-headers))
5421       (setq gnus-newsgroup-articles fetched-articles)
5422       (setq gnus-newsgroup-unreads
5423             (gnus-sorted-nintersection
5424              gnus-newsgroup-unreads fetched-articles))
5425       (gnus-compute-unseen-list)
5426
5427       ;; Removed marked articles that do not exist.
5428       (gnus-update-missing-marks
5429        (gnus-sorted-difference articles fetched-articles))
5430       ;; We might want to build some more threads first.
5431       (when (and gnus-fetch-old-headers
5432                  (eq gnus-headers-retrieved-by 'nov))
5433         (if (eq gnus-fetch-old-headers 'invisible)
5434             (gnus-build-all-threads)
5435           (gnus-build-old-threads)))
5436       ;; Let the Gnus agent mark articles as read.
5437       (when gnus-agent
5438         (gnus-agent-get-undownloaded-list))
5439       ;; Remove list identifiers from subject
5440       (when gnus-list-identifiers
5441         (gnus-summary-remove-list-identifiers))
5442       ;; Check whether auto-expire is to be done in this group.
5443       (setq gnus-newsgroup-auto-expire
5444             (gnus-group-auto-expirable-p group))
5445       ;; Set up the article buffer now, if necessary.
5446       (unless gnus-single-article-buffer
5447         (gnus-article-setup-buffer))
5448       ;; First and last article in this newsgroup.
5449       (when gnus-newsgroup-headers
5450         (setq gnus-newsgroup-begin
5451               (mail-header-number (car gnus-newsgroup-headers))
5452               gnus-newsgroup-end
5453               (mail-header-number
5454                (gnus-last-element gnus-newsgroup-headers))))
5455       ;; GROUP is successfully selected.
5456       (or gnus-newsgroup-headers t)))))
5457
5458 (defun gnus-compute-unseen-list ()
5459   ;; The `seen' marks are treated specially.
5460   (if (not gnus-newsgroup-seen)
5461       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5462     (setq gnus-newsgroup-unseen
5463           (gnus-inverse-list-range-intersection
5464            gnus-newsgroup-articles gnus-newsgroup-seen))))
5465
5466 (defun gnus-summary-display-make-predicate (display)
5467   (require 'gnus-agent)
5468   (when (= (length display) 1)
5469     (setq display (car display)))
5470   (unless gnus-summary-display-cache
5471     (dolist (elem (append '((unread . unread)
5472                             (read . read)
5473                             (unseen . unseen))
5474                           gnus-article-mark-lists))
5475       (push (cons (cdr elem)
5476                   (gnus-byte-compile
5477                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5478             gnus-summary-display-cache)))
5479   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5480         (gnus-category-predicate-cache gnus-summary-display-cache))
5481     (gnus-get-predicate display)))
5482
5483 ;; Uses the dynamically bound `number' variable.
5484 (eval-when-compile
5485   (defvar number))
5486 (defun gnus-article-marked-p (type &optional article)
5487   (let ((article (or article number)))
5488     (cond
5489      ((eq type 'tick)
5490       (memq article gnus-newsgroup-marked))
5491      ((eq type 'spam)
5492       (memq article gnus-newsgroup-spam-marked))
5493      ((eq type 'unsend)
5494       (memq article gnus-newsgroup-unsendable))
5495      ((eq type 'undownload)
5496       (memq article gnus-newsgroup-undownloaded))
5497      ((eq type 'download)
5498       (memq article gnus-newsgroup-downloadable))
5499      ((eq type 'unread)
5500       (memq article gnus-newsgroup-unreads))
5501      ((eq type 'read)
5502       (memq article gnus-newsgroup-reads))
5503      ((eq type 'dormant)
5504       (memq article gnus-newsgroup-dormant) )
5505      ((eq type 'expire)
5506       (memq article gnus-newsgroup-expirable))
5507      ((eq type 'reply)
5508       (memq article gnus-newsgroup-replied))
5509      ((eq type 'killed)
5510       (memq article gnus-newsgroup-killed))
5511      ((eq type 'bookmark)
5512       (assq article gnus-newsgroup-bookmarks))
5513      ((eq type 'score)
5514       (assq article gnus-newsgroup-scored))
5515      ((eq type 'save)
5516       (memq article gnus-newsgroup-saved))
5517      ((eq type 'cache)
5518       (memq article gnus-newsgroup-cached))
5519      ((eq type 'forward)
5520       (memq article gnus-newsgroup-forwarded))
5521      ((eq type 'seen)
5522       (not (memq article gnus-newsgroup-unseen)))
5523      ((eq type 'recent)
5524       (memq article gnus-newsgroup-recent))
5525      (t t))))
5526
5527 (defun gnus-articles-to-read (group &optional read-all)
5528   "Find out what articles the user wants to read."
5529   (let* ((articles
5530           ;; Select all articles if `read-all' is non-nil, or if there
5531           ;; are no unread articles.
5532           (if (or read-all
5533                   (and (zerop (length gnus-newsgroup-marked))
5534                        (zerop (length gnus-newsgroup-unreads)))
5535                   ;; Fetch all if the predicate is non-nil.
5536                   gnus-newsgroup-display)
5537               ;; We want to select the headers for all the articles in
5538               ;; the group, so we select either all the active
5539               ;; articles in the group, or (if that's nil), the
5540               ;; articles in the cache.
5541               (or
5542                (gnus-uncompress-range (gnus-active group))
5543                (gnus-cache-articles-in-group group))
5544             ;; Select only the "normal" subset of articles.
5545             (gnus-sorted-nunion
5546              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5547              gnus-newsgroup-unreads)))
5548          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5549          (scored (length scored-list))
5550          (number (length articles))
5551          (marked (+ (length gnus-newsgroup-marked)
5552                     (length gnus-newsgroup-dormant)))
5553          (select
5554           (cond
5555            ((numberp read-all)
5556             read-all)
5557            ((numberp gnus-newsgroup-display)
5558             gnus-newsgroup-display)
5559            (t
5560             (condition-case ()
5561                 (cond
5562                  ((and (or (<= scored marked) (= scored number))
5563                        (numberp gnus-large-newsgroup)
5564                        (> number gnus-large-newsgroup))
5565                   (let* ((cursor-in-echo-area nil)
5566                          (initial (gnus-parameter-large-newsgroup-initial
5567                                    gnus-newsgroup-name))
5568                          (input
5569                           (read-string
5570                            (format
5571                             "How many articles from %s (%s %d): "
5572                             (gnus-limit-string
5573                              (gnus-group-decoded-name gnus-newsgroup-name)
5574                              35)
5575                             (if initial "max" "default")
5576                             number)
5577                            (if initial
5578                                (cons (number-to-string initial)
5579                                      0)))))
5580                     (if (string-match "^[ \t]*$" input) number input)))
5581                  ((and (> scored marked) (< scored number)
5582                        (> (- scored number) 20))
5583                   (let ((input
5584                          (read-string
5585                           (format "%s %s (%d scored, %d total): "
5586                                   "How many articles from"
5587                                   (gnus-group-decoded-name group)
5588                                   scored number))))
5589                     (if (string-match "^[ \t]*$" input)
5590                         number input)))
5591                  (t number))
5592               (quit
5593                (message "Quit getting the articles to read")
5594                nil))))))
5595     (setq select (if (stringp select) (string-to-number select) select))
5596     (if (or (null select) (zerop select))
5597         select
5598       (if (and (not (zerop scored)) (<= (abs select) scored))
5599           (progn
5600             (setq articles (sort scored-list '<))
5601             (setq number (length articles)))
5602         (setq articles (copy-sequence articles)))
5603
5604       (when (< (abs select) number)
5605         (if (< select 0)
5606             ;; Select the N oldest articles.
5607             (setcdr (nthcdr (1- (abs select)) articles) nil)
5608           ;; Select the N most recent articles.
5609           (setq articles (nthcdr (- number select) articles))))
5610       (setq gnus-newsgroup-unselected
5611             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5612       (when gnus-alter-articles-to-read-function
5613         (setq articles
5614               (sort
5615                (funcall gnus-alter-articles-to-read-function
5616                         gnus-newsgroup-name articles)
5617                '<)))
5618       articles)))
5619
5620 (defun gnus-killed-articles (killed articles)
5621   (let (out)
5622     (while articles
5623       (when (inline (gnus-member-of-range (car articles) killed))
5624         (push (car articles) out))
5625       (setq articles (cdr articles)))
5626     out))
5627
5628 (defun gnus-uncompress-marks (marks)
5629   "Uncompress the mark ranges in MARKS."
5630   (let ((uncompressed '(score bookmark))
5631         out)
5632     (while marks
5633       (if (memq (caar marks) uncompressed)
5634           (push (car marks) out)
5635         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5636       (setq marks (cdr marks)))
5637     out))
5638
5639 (defun gnus-article-mark-to-type (mark)
5640   "Return the type of MARK."
5641   (or (cadr (assq mark gnus-article-special-mark-lists))
5642       'list))
5643
5644 (defun gnus-article-unpropagatable-p (mark)
5645   "Return whether MARK should be propagated to back end."
5646   (memq mark gnus-article-unpropagated-mark-lists))
5647
5648 (defun gnus-adjust-marked-articles (info)
5649   "Set all article lists and remove all marks that are no longer valid."
5650   (let* ((marked-lists (gnus-info-marks info))
5651          (active (gnus-active (gnus-info-group info)))
5652          (min (car active))
5653          (max (cdr active))
5654          (types gnus-article-mark-lists)
5655          marks var articles article mark mark-type
5656          bgn end)
5657
5658     (dolist (marks marked-lists)
5659       (setq mark (car marks)
5660             mark-type (gnus-article-mark-to-type mark)
5661             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5662
5663       ;; We set the variable according to the type of the marks list,
5664       ;; and then adjust the marks to a subset of the active articles.
5665       (cond
5666        ;; Adjust "simple" lists - compressed yet unsorted
5667        ((eq mark-type 'list)
5668         ;; Simultaneously uncompress and clip to active range
5669         ;; See gnus-uncompress-range for a description of possible marks
5670         (let (l lh)
5671           (if (not (cadr marks))
5672               (set var nil)
5673             (setq articles (if (numberp (cddr marks))
5674                                (list (cdr marks))
5675                              (cdr marks))
5676                   lh (cons nil nil)
5677                   l lh)
5678
5679             (while (setq article (pop articles))
5680               (cond ((consp article)
5681                      (setq bgn (max (car article) min)
5682                            end (min (cdr article) max))
5683                      (while (<= bgn end)
5684                        (setq l (setcdr l (cons bgn nil))
5685                              bgn (1+ bgn))))
5686                     ((and (<= min article)
5687                           (>= max article))
5688                      (setq l (setcdr l (cons article nil))))))
5689             (set var (cdr lh)))))
5690        ;; Adjust assocs.
5691        ((eq mark-type 'tuple)
5692         (set var (setq articles (cdr marks)))
5693         (when (not (listp (cdr (symbol-value var))))
5694           (set var (list (symbol-value var))))
5695         (when (not (listp (cdr articles)))
5696           (setq articles (list articles)))
5697         (while articles
5698           (when (or (not (consp (setq article (pop articles))))
5699                     (< (car article) min)
5700                     (> (car article) max))
5701             (set var (delq article (symbol-value var))))))
5702        ;; Adjust ranges (sloppily).
5703        ((eq mark-type 'range)
5704         (cond
5705          ((eq mark 'seen)
5706           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5707           ;; It should be (seen (NUM1 . NUM2)).
5708           (when (numberp (cddr marks))
5709             (setcdr marks (list (cdr marks))))
5710           (setq articles (cdr marks))
5711           (while (and articles
5712                       (or (and (consp (car articles))
5713                                (> min (cdar articles)))
5714                           (and (numberp (car articles))
5715                                (> min (car articles)))))
5716             (pop articles))
5717           (set var articles))))))))
5718
5719 (defun gnus-update-missing-marks (missing)
5720   "Go through the list of MISSING articles and remove them from the mark lists."
5721   (when missing
5722     (let (var m)
5723       ;; Go through all types.
5724       (dolist (elem gnus-article-mark-lists)
5725         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5726           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5727           (when (symbol-value var)
5728             ;; This list has articles.  So we delete all missing
5729             ;; articles from it.
5730             (setq m missing)
5731             (while m
5732               (set var (delq (pop m) (symbol-value var))))))))))
5733
5734 (defun gnus-update-marks ()
5735   "Enter the various lists of marked articles into the newsgroup info list."
5736   (let ((types gnus-article-mark-lists)
5737         (info (gnus-get-info gnus-newsgroup-name))
5738         type list newmarked symbol delta-marks)
5739     (when info
5740       ;; Add all marks lists to the list of marks lists.
5741       (while (setq type (pop types))
5742         (setq list (symbol-value
5743                     (setq symbol
5744                           (intern (format "gnus-newsgroup-%s" (car type))))))
5745
5746         (when list
5747           ;; Get rid of the entries of the articles that have the
5748           ;; default score.
5749           (when (and (eq (cdr type) 'score)
5750                      gnus-save-score
5751                      list)
5752             (let* ((arts list)
5753                    (prev (cons nil list))
5754                    (all prev))
5755               (while arts
5756                 (if (or (not (consp (car arts)))
5757                         (= (cdar arts) gnus-summary-default-score))
5758                     (setcdr prev (cdr arts))
5759                   (setq prev arts))
5760                 (setq arts (cdr arts)))
5761               (setq list (cdr all)))))
5762
5763         (when (eq (cdr type) 'seen)
5764           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5765
5766         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5767           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5768
5769         (when (and (gnus-check-backend-function
5770                     'request-set-mark gnus-newsgroup-name)
5771                    (not (gnus-article-unpropagatable-p (cdr type))))
5772           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5773                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5774                  (add (gnus-remove-from-range
5775                        (gnus-copy-sequence list) old)))
5776             (when add
5777               (push (list add 'add (list (cdr type))) delta-marks))
5778             (when del
5779               (push (list del 'del (list (cdr type))) delta-marks))))
5780
5781         (when list
5782           (push (cons (cdr type) list) newmarked)))
5783
5784       (when delta-marks
5785         (unless (gnus-check-group gnus-newsgroup-name)
5786           (error "Can't open server for %s" gnus-newsgroup-name))
5787         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5788
5789       ;; Enter these new marks into the info of the group.
5790       (if (nthcdr 3 info)
5791           (setcar (nthcdr 3 info) newmarked)
5792         ;; Add the marks lists to the end of the info.
5793         (when newmarked
5794           (setcdr (nthcdr 2 info) (list newmarked))))
5795
5796       ;; Cut off the end of the info if there's nothing else there.
5797       (let ((i 5))
5798         (while (and (> i 2)
5799                     (not (nth i info)))
5800           (when (nthcdr (decf i) info)
5801             (setcdr (nthcdr i info) nil)))))))
5802
5803 (defun gnus-set-mode-line (where)
5804   "Set the mode line of the article or summary buffers.
5805 If WHERE is `summary', the summary mode line format will be used."
5806   ;; Is this mode line one we keep updated?
5807   (when (and (memq where gnus-updated-mode-lines)
5808              (symbol-value
5809               (intern (format "gnus-%s-mode-line-format-spec" where))))
5810     (let (mode-string)
5811       (save-excursion
5812         ;; We evaluate this in the summary buffer since these
5813         ;; variables are buffer-local to that buffer.
5814         (set-buffer gnus-summary-buffer)
5815        ;; We bind all these variables that are used in the `eval' form
5816         ;; below.
5817         (let* ((mformat (symbol-value
5818                          (intern
5819                           (format "gnus-%s-mode-line-format-spec" where))))
5820                (gnus-tmp-group-name (gnus-group-decoded-name
5821                                      gnus-newsgroup-name))
5822                (gnus-tmp-article-number (or gnus-current-article 0))
5823                (gnus-tmp-unread gnus-newsgroup-unreads)
5824                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5825                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5826                (gnus-tmp-unread-and-unselected
5827                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5828                             (zerop gnus-tmp-unselected))
5829                        "")
5830                       ((zerop gnus-tmp-unselected)
5831                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5832                       (t (format "{%d(+%d) more}"
5833                                  gnus-tmp-unread-and-unticked
5834                                  gnus-tmp-unselected))))
5835                (gnus-tmp-subject
5836                 (if (and gnus-current-headers
5837                          (vectorp gnus-current-headers))
5838                     (gnus-mode-string-quote
5839                      (mail-header-subject gnus-current-headers))
5840                   ""))
5841                bufname-length max-len
5842                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5843           (setq mode-string (eval mformat))
5844           (setq bufname-length (if (string-match "%b" mode-string)
5845                                    (- (length
5846                                        (buffer-name
5847                                         (if (eq where 'summary)
5848                                             nil
5849                                           (get-buffer gnus-article-buffer))))
5850                                       2)
5851                                  0))
5852           (setq max-len (max 4 (if gnus-mode-non-string-length
5853                                    (- (window-width)
5854                                       gnus-mode-non-string-length
5855                                       bufname-length)
5856                                  (length mode-string))))
5857           ;; We might have to chop a bit of the string off...
5858           (when (> (length mode-string) max-len)
5859             (setq mode-string
5860                   (concat (truncate-string-to-width mode-string (- max-len 3))
5861                           "...")))
5862           ;; Pad the mode string a bit.
5863           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5864       ;; Update the mode line.
5865       (setq mode-line-buffer-identification
5866             (gnus-mode-line-buffer-identification (list mode-string)))
5867       (set-buffer-modified-p t))))
5868
5869 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5870   "Go through the HEADERS list and add all Xrefs to a hash table.
5871 The resulting hash table is returned, or nil if no Xrefs were found."
5872   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5873          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5874          (xref-hashtb (gnus-make-hashtable))
5875          start group entry number xrefs header)
5876     (while headers
5877       (setq header (pop headers))
5878       (when (and (setq xrefs (mail-header-xref header))
5879                  (not (memq (setq number (mail-header-number header))
5880                             unreads)))
5881         (setq start 0)
5882         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5883           (setq start (match-end 0))
5884           (setq group (if prefix
5885                           (concat prefix (substring xrefs (match-beginning 1)
5886                                                     (match-end 1)))
5887                         (substring xrefs (match-beginning 1) (match-end 1))))
5888           (setq number
5889                 (string-to-number (substring xrefs (match-beginning 2)
5890                                           (match-end 2))))
5891           (if (setq entry (gnus-gethash group xref-hashtb))
5892               (setcdr entry (cons number (cdr entry)))
5893             (gnus-sethash group (cons number nil) xref-hashtb)))))
5894     (and start xref-hashtb)))
5895
5896 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5897   "Look through all the headers and mark the Xrefs as read."
5898   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5899         name info xref-hashtb idlist method nth4)
5900     (save-excursion
5901       (set-buffer gnus-group-buffer)
5902       (when (setq xref-hashtb
5903                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5904         (mapatoms
5905          (lambda (group)
5906            (unless (string= from-newsgroup (setq name (symbol-name group)))
5907              (setq idlist (symbol-value group))
5908              ;; Dead groups are not updated.
5909              (and (prog1
5910                       (setq info (gnus-get-info name))
5911                     (when (stringp (setq nth4 (gnus-info-method info)))
5912                       (setq nth4 (gnus-server-to-method nth4))))
5913                   ;; Only do the xrefs if the group has the same
5914                   ;; select method as the group we have just read.
5915                   (or (gnus-methods-equal-p
5916                        nth4 (gnus-find-method-for-group from-newsgroup))
5917                       virtual
5918                       (equal nth4 (setq method (gnus-find-method-for-group
5919                                                 from-newsgroup)))
5920                       (and (equal (car nth4) (car method))
5921                            (equal (nth 1 nth4) (nth 1 method))))
5922                   gnus-use-cross-reference
5923                   (or (not (eq gnus-use-cross-reference t))
5924                       virtual
5925                       ;; Only do cross-references on subscribed
5926                       ;; groups, if that is what is wanted.
5927                       (<= (gnus-info-level info) gnus-level-subscribed))
5928                   (gnus-group-make-articles-read name idlist))))
5929          xref-hashtb)))))
5930
5931 (defun gnus-compute-read-articles (group articles)
5932   (let* ((entry (gnus-group-entry group))
5933          (info (nth 2 entry))
5934          (active (gnus-active group))
5935          ninfo)
5936     (when entry
5937       ;; First peel off all invalid article numbers.
5938       (when active
5939         (let ((ids articles)
5940               id first)
5941           (while (setq id (pop ids))
5942             (when (and first (> id (cdr active)))
5943               ;; We'll end up in this situation in one particular
5944               ;; obscure situation.  If you re-scan a group and get
5945               ;; a new article that is cross-posted to a different
5946               ;; group that has not been re-scanned, you might get
5947               ;; crossposted article that has a higher number than
5948               ;; Gnus believes possible.  So we re-activate this
5949               ;; group as well.  This might mean doing the
5950               ;; crossposting thingy will *increase* the number
5951               ;; of articles in some groups.  Tsk, tsk.
5952               (setq active (or (gnus-activate-group group) active)))
5953             (when (or (> id (cdr active))
5954                       (< id (car active)))
5955               (setq articles (delq id articles))))))
5956       ;; If the read list is nil, we init it.
5957       (if (and active
5958                (null (gnus-info-read info))
5959                (> (car active) 1))
5960           (setq ninfo (cons 1 (1- (car active))))
5961         (setq ninfo (gnus-info-read info)))
5962       ;; Then we add the read articles to the range.
5963       (gnus-add-to-range
5964        ninfo (setq articles (sort articles '<))))))
5965
5966 (defun gnus-group-make-articles-read (group articles)
5967   "Update the info of GROUP to say that ARTICLES are read."
5968   (let* ((num 0)
5969          (entry (gnus-group-entry group))
5970          (info (nth 2 entry))
5971          (active (gnus-active group))
5972          range)
5973     (when entry
5974       (setq range (gnus-compute-read-articles group articles))
5975       (with-current-buffer gnus-group-buffer
5976         (gnus-undo-register
5977           `(progn
5978              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5979              (gnus-info-set-read ',info ',(gnus-info-read info))
5980              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5981              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5982              (gnus-group-update-group ,group t))))
5983       ;; Add the read articles to the range.
5984       (gnus-info-set-read info range)
5985       (gnus-request-set-mark group (list (list range 'add '(read))))
5986       ;; Then we have to re-compute how many unread
5987       ;; articles there are in this group.
5988       (when active
5989         (cond
5990          ((not range)
5991           (setq num (- (1+ (cdr active)) (car active))))
5992          ((not (listp (cdr range)))
5993           (setq num (- (cdr active) (- (1+ (cdr range))
5994                                        (car range)))))
5995          (t
5996           (while range
5997             (if (numberp (car range))
5998                 (setq num (1+ num))
5999               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6000             (setq range (cdr range)))
6001           (setq num (- (cdr active) num))))
6002         ;; Update the number of unread articles.
6003         (setcar entry num)
6004         ;; Update the group buffer.
6005         (unless (gnus-ephemeral-group-p group)
6006           (gnus-group-update-group group t))))))
6007
6008 (defvar gnus-newsgroup-none-id 0)
6009
6010 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6011   (let ((cur nntp-server-buffer)
6012         (dependencies
6013          (or dependencies
6014              (with-current-buffer gnus-summary-buffer
6015                gnus-newsgroup-dependencies)))
6016         headers id end ref number
6017         (mail-parse-charset gnus-newsgroup-charset)
6018         (mail-parse-ignored-charsets
6019          (save-excursion (condition-case nil
6020                              (set-buffer gnus-summary-buffer)
6021                            (error))
6022                          gnus-newsgroup-ignored-charsets)))
6023     (save-excursion
6024       (set-buffer nntp-server-buffer)
6025       ;; Translate all TAB characters into SPACE characters.
6026       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6027       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6028       (ietf-drums-unfold-fws)
6029       (gnus-run-hooks 'gnus-parse-headers-hook)
6030       (let ((case-fold-search t)
6031             in-reply-to header p lines chars)
6032         (goto-char (point-min))
6033         ;; Search to the beginning of the next header.  Error messages
6034         ;; do not begin with 2 or 3.
6035         (while (re-search-forward "^[23][0-9]+ " nil t)
6036           (setq id nil
6037                 ref nil)
6038           ;; This implementation of this function, with nine
6039           ;; search-forwards instead of the one re-search-forward and
6040           ;; a case (which basically was the old function) is actually
6041           ;; about twice as fast, even though it looks messier.  You
6042           ;; can't have everything, I guess.  Speed and elegance
6043           ;; doesn't always go hand in hand.
6044           (setq
6045            header
6046            (vector
6047             ;; Number.
6048             (prog1
6049                 (setq number (read cur))
6050               (end-of-line)
6051               (setq p (point))
6052               (narrow-to-region (point)
6053                                 (or (and (search-forward "\n.\n" nil t)
6054                                          (- (point) 2))
6055                                     (point))))
6056             ;; Subject.
6057             (progn
6058               (goto-char p)
6059               (if (search-forward "\nsubject:" nil t)
6060                   (funcall gnus-decode-encoded-word-function
6061                            (nnheader-header-value))
6062                 "(none)"))
6063             ;; From.
6064             (progn
6065               (goto-char p)
6066               (if (search-forward "\nfrom:" nil t)
6067                   (funcall gnus-decode-encoded-word-function
6068                            (nnheader-header-value))
6069                 "(nobody)"))
6070             ;; Date.
6071             (progn
6072               (goto-char p)
6073               (if (search-forward "\ndate:" nil t)
6074                   (nnheader-header-value) ""))
6075             ;; Message-ID.
6076             (progn
6077               (goto-char p)
6078               (setq id (if (re-search-forward
6079                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6080                            ;; We do it this way to make sure the Message-ID
6081                            ;; is (somewhat) syntactically valid.
6082                            (buffer-substring (match-beginning 1)
6083                                              (match-end 1))
6084                          ;; If there was no message-id, we just fake one
6085                          ;; to make subsequent routines simpler.
6086                          (nnheader-generate-fake-message-id number))))
6087             ;; References.
6088             (progn
6089               (goto-char p)
6090               (if (search-forward "\nreferences:" nil t)
6091                   (progn
6092                     (setq end (point))
6093                     (prog1
6094                         (nnheader-header-value)
6095                       (setq ref
6096                             (buffer-substring
6097                              (progn
6098                                (end-of-line)
6099                                (search-backward ">" end t)
6100                                (1+ (point)))
6101                              (progn
6102                                (search-backward "<" end t)
6103                                (point))))))
6104                 ;; Get the references from the in-reply-to header if there
6105                 ;; were no references and the in-reply-to header looks
6106                 ;; promising.
6107                 (if (and (search-forward "\nin-reply-to:" nil t)
6108                          (setq in-reply-to (nnheader-header-value))
6109                          (string-match "<[^>]+>" in-reply-to))
6110                     (let (ref2)
6111                       (setq ref (substring in-reply-to (match-beginning 0)
6112                                            (match-end 0)))
6113                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6114                         (setq ref2 (substring in-reply-to (match-beginning 0)
6115                                               (match-end 0)))
6116                         (when (> (length ref2) (length ref))
6117                           (setq ref ref2)))
6118                       ref)
6119                   (setq ref nil))))
6120             ;; Chars.
6121             (progn
6122               (goto-char p)
6123               (if (search-forward "\nchars: " nil t)
6124                   (if (numberp (setq chars (ignore-errors (read cur))))
6125                       chars -1)
6126                 -1))
6127             ;; Lines.
6128             (progn
6129               (goto-char p)
6130               (if (search-forward "\nlines: " nil t)
6131                   (if (numberp (setq lines (ignore-errors (read cur))))
6132                       lines -1)
6133                 -1))
6134             ;; Xref.
6135             (progn
6136               (goto-char p)
6137               (and (search-forward "\nxref:" nil t)
6138                    (nnheader-header-value)))
6139             ;; Extra.
6140             (when gnus-extra-headers
6141               (let ((extra gnus-extra-headers)
6142                     out)
6143                 (while extra
6144                   (goto-char p)
6145                   (when (search-forward
6146                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6147                     (push (cons (car extra) (nnheader-header-value))
6148                           out))
6149                   (pop extra))
6150                 out))))
6151           (when (equal id ref)
6152             (setq ref nil))
6153
6154           (when gnus-alter-header-function
6155             (funcall gnus-alter-header-function header)
6156             (setq id (mail-header-id header)
6157                   ref (gnus-parent-id (mail-header-references header))))
6158
6159           (when (setq header
6160                       (gnus-dependencies-add-header
6161                        header dependencies force-new))
6162             (push header headers))
6163           (goto-char (point-max))
6164           (widen))
6165         (nreverse headers)))))
6166
6167 ;; Goes through the xover lines and returns a list of vectors
6168 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6169                                                   force-new dependencies
6170                                                   group also-fetch-heads)
6171   "Parse the news overview data in the server buffer.
6172 Return a list of headers that match SEQUENCE (see
6173 `nntp-retrieve-headers')."
6174   ;; Get the Xref when the users reads the articles since most/some
6175   ;; NNTP servers do not include Xrefs when using XOVER.
6176   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6177   (let ((mail-parse-charset gnus-newsgroup-charset)
6178         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6179         (cur nntp-server-buffer)
6180         (dependencies (or dependencies gnus-newsgroup-dependencies))
6181         (allp (cond
6182                ((eq gnus-read-all-available-headers t)
6183                 t)
6184                ((and (stringp gnus-read-all-available-headers)
6185                      group)
6186                 (string-match gnus-read-all-available-headers group))
6187                (t
6188                 nil)))
6189         number headers header)
6190     (save-excursion
6191       (set-buffer nntp-server-buffer)
6192       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6193       ;; Allow the user to mangle the headers before parsing them.
6194       (gnus-run-hooks 'gnus-parse-headers-hook)
6195       (goto-char (point-min))
6196       (gnus-parse-without-error
6197         (while (and (or sequence allp)
6198                     (not (eobp)))
6199           (setq number (read cur))
6200           (when (not allp)
6201             (while (and sequence
6202                         (< (car sequence) number))
6203               (setq sequence (cdr sequence))))
6204           (when (and (or allp
6205                          (and sequence
6206                               (eq number (car sequence))))
6207                      (progn
6208                        (setq sequence (cdr sequence))
6209                        (setq header (inline
6210                                       (gnus-nov-parse-line
6211                                        number dependencies force-new)))))
6212             (push header headers))
6213           (forward-line 1)))
6214       ;; A common bug in inn is that if you have posted an article and
6215       ;; then retrieves the active file, it will answer correctly --
6216       ;; the new article is included.  However, a NOV entry for the
6217       ;; article may not have been generated yet, so this may fail.
6218       ;; We work around this problem by retrieving the last few
6219       ;; headers using HEAD.
6220       (if (or (not also-fetch-heads)
6221               (not sequence))
6222           ;; We (probably) got all the headers.
6223           (nreverse headers)
6224         (let ((gnus-nov-is-evil t))
6225           (nconc
6226            (nreverse headers)
6227            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6228              (gnus-get-newsgroup-headers))))))))
6229
6230 (defun gnus-article-get-xrefs ()
6231   "Fill in the Xref value in `gnus-current-headers', if necessary.
6232 This is meant to be called in `gnus-article-internal-prepare-hook'."
6233   (let ((headers (with-current-buffer gnus-summary-buffer
6234                    gnus-current-headers)))
6235     (or (not gnus-use-cross-reference)
6236         (not headers)
6237         (and (mail-header-xref headers)
6238              (not (string= (mail-header-xref headers) "")))
6239         (let ((case-fold-search t)
6240               xref)
6241           (save-restriction
6242             (nnheader-narrow-to-headers)
6243             (goto-char (point-min))
6244             (when (or (and (not (eobp))
6245                            (eq (downcase (char-after)) ?x)
6246                            (looking-at "Xref:"))
6247                       (search-forward "\nXref:" nil t))
6248               (goto-char (1+ (match-end 0)))
6249               (setq xref (buffer-substring (point) (point-at-eol)))
6250               (mail-header-set-xref headers xref)))))))
6251
6252 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6253   "Find article ID and insert the summary line for that article.
6254 OLD-HEADER can either be a header or a line number to insert
6255 the subject line on."
6256   (let* ((line (and (numberp old-header) old-header))
6257          (old-header (and (vectorp old-header) old-header))
6258          (header (cond ((and old-header use-old-header)
6259                         old-header)
6260                        ((and (numberp id)
6261                              (gnus-number-to-header id))
6262                         (gnus-number-to-header id))
6263                        (t
6264                         (gnus-read-header id))))
6265          (number (and (numberp id) id))
6266          d)
6267     (when header
6268       ;; Rebuild the thread that this article is part of and go to the
6269       ;; article we have fetched.
6270       (when (and (not gnus-show-threads)
6271                  old-header)
6272         (when (and number
6273                    (setq d (gnus-data-find (mail-header-number old-header))))
6274           (goto-char (gnus-data-pos d))
6275           (gnus-data-remove
6276            number
6277            (- (point-at-bol)
6278               (prog1
6279                   (1+ (point-at-eol))
6280                 (gnus-delete-line))))))
6281       ;; Remove list identifiers from subject.
6282       (when gnus-list-identifiers
6283         (let ((gnus-newsgroup-headers (list header)))
6284           (gnus-summary-remove-list-identifiers)))
6285       (when old-header
6286         (mail-header-set-number header (mail-header-number old-header)))
6287       (setq gnus-newsgroup-sparse
6288             (delq (setq number (mail-header-number header))
6289                   gnus-newsgroup-sparse))
6290       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6291       (push number gnus-newsgroup-limit)
6292       (gnus-rebuild-thread (mail-header-id header) line)
6293       (gnus-summary-goto-subject number nil t))
6294     (when (and (numberp number)
6295                (> number 0))
6296       ;; We have to update the boundaries even if we can't fetch the
6297       ;; article if ID is a number -- so that the next `P' or `N'
6298       ;; command will fetch the previous (or next) article even
6299       ;; if the one we tried to fetch this time has been canceled.
6300       (when (> number gnus-newsgroup-end)
6301         (setq gnus-newsgroup-end number))
6302       (when (< number gnus-newsgroup-begin)
6303         (setq gnus-newsgroup-begin number))
6304       (setq gnus-newsgroup-unselected
6305             (delq number gnus-newsgroup-unselected)))
6306     ;; Report back a success?
6307     (and header (mail-header-number header))))
6308
6309 ;;; Process/prefix in the summary buffer
6310
6311 (defun gnus-summary-work-articles (n)
6312   "Return a list of articles to be worked upon.
6313 The prefix argument, the list of process marked articles, and the
6314 current article will be taken into consideration."
6315   (save-excursion
6316     (set-buffer gnus-summary-buffer)
6317     (cond
6318      (n
6319       ;; A numerical prefix has been given.
6320       (setq n (prefix-numeric-value n))
6321       (let ((backward (< n 0))
6322             (n (abs (prefix-numeric-value n)))
6323             articles article)
6324         (save-excursion
6325           (while
6326               (and (> n 0)
6327                    (push (setq article (gnus-summary-article-number))
6328                          articles)
6329                    (if backward
6330                        (gnus-summary-find-prev nil article)
6331                      (gnus-summary-find-next nil article)))
6332             (decf n)))
6333         (nreverse articles)))
6334      ((and (gnus-region-active-p) (mark))
6335       (message "region active")
6336       ;; Work on the region between point and mark.
6337       (let ((max (max (point) (mark)))
6338             articles article)
6339         (save-excursion
6340           (goto-char (min (point) (mark)))
6341           (while
6342               (and
6343                (push (setq article (gnus-summary-article-number)) articles)
6344                (gnus-summary-find-next nil article)
6345                (< (point) max)))
6346           (nreverse articles))))
6347      (gnus-newsgroup-processable
6348       ;; There are process-marked articles present.
6349       ;; Save current state.
6350       (gnus-summary-save-process-mark)
6351       ;; Return the list.
6352       (reverse gnus-newsgroup-processable))
6353      (t
6354       ;; Just return the current article.
6355       (list (gnus-summary-article-number))))))
6356
6357 (defmacro gnus-summary-iterate (arg &rest forms)
6358   "Iterate over the process/prefixed articles and do FORMS.
6359 ARG is the interactive prefix given to the command.  FORMS will be
6360 executed with point over the summary line of the articles."
6361   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6362     `(let ((,articles (gnus-summary-work-articles ,arg)))
6363        (while ,articles
6364          (gnus-summary-goto-subject (car ,articles))
6365          ,@forms
6366          (pop ,articles)))))
6367
6368 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6369 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6370
6371 (defun gnus-summary-save-process-mark ()
6372   "Push the current set of process marked articles on the stack."
6373   (interactive)
6374   (push (copy-sequence gnus-newsgroup-processable)
6375         gnus-newsgroup-process-stack))
6376
6377 (defun gnus-summary-kill-process-mark ()
6378   "Push the current set of process marked articles on the stack and unmark."
6379   (interactive)
6380   (gnus-summary-save-process-mark)
6381   (gnus-summary-unmark-all-processable))
6382
6383 (defun gnus-summary-yank-process-mark ()
6384   "Pop the last process mark state off the stack and restore it."
6385   (interactive)
6386   (unless gnus-newsgroup-process-stack
6387     (error "Empty mark stack"))
6388   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6389
6390 (defun gnus-summary-process-mark-set (set)
6391   "Make SET into the current process marked articles."
6392   (gnus-summary-unmark-all-processable)
6393   (mapc 'gnus-summary-set-process-mark set))
6394
6395 ;;; Searching and stuff
6396
6397 (defun gnus-summary-search-group (&optional backward use-level)
6398   "Search for next unread newsgroup.
6399 If optional argument BACKWARD is non-nil, search backward instead."
6400   (save-excursion
6401     (set-buffer gnus-group-buffer)
6402     (when (gnus-group-search-forward
6403            backward nil (if use-level (gnus-group-group-level) nil))
6404       (gnus-group-group-name))))
6405
6406 (defun gnus-summary-best-group (&optional exclude-group)
6407   "Find the name of the best unread group.
6408 If EXCLUDE-GROUP, do not go to this group."
6409   (with-current-buffer gnus-group-buffer
6410     (save-excursion
6411       (gnus-group-best-unread-group exclude-group))))
6412
6413 (defun gnus-summary-find-next (&optional unread article backward)
6414   (if backward
6415       (gnus-summary-find-prev unread article)
6416     (let* ((dummy (gnus-summary-article-intangible-p))
6417            (article (or article (gnus-summary-article-number)))
6418            (data (gnus-data-find-list article))
6419            result)
6420       (when (and (not dummy)
6421                  (or (not gnus-summary-check-current)
6422                      (not unread)
6423                      (not (gnus-data-unread-p (car data)))))
6424         (setq data (cdr data)))
6425       (when (setq result
6426                   (if unread
6427                       (progn
6428                         (while data
6429                           (unless (memq (gnus-data-number (car data))
6430                                         (cond
6431                                          ((eq gnus-auto-goto-ignores
6432                                               'always-undownloaded)
6433                                           gnus-newsgroup-undownloaded)
6434                                          (gnus-plugged
6435                                           nil)
6436                                          ((eq gnus-auto-goto-ignores
6437                                               'unfetched)
6438                                           gnus-newsgroup-unfetched)
6439                                          ((eq gnus-auto-goto-ignores
6440                                               'undownloaded)
6441                                           gnus-newsgroup-undownloaded)))
6442                             (when (gnus-data-unread-p (car data))
6443                               (setq result (car data)
6444                                     data nil)))
6445                           (setq data (cdr data)))
6446                         result)
6447                     (car data)))
6448         (goto-char (gnus-data-pos result))
6449         (gnus-data-number result)))))
6450
6451 (defun gnus-summary-find-prev (&optional unread article)
6452   (let* ((eobp (eobp))
6453          (article (or article (gnus-summary-article-number)))
6454          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6455          result)
6456     (when (and (not eobp)
6457                (or (not gnus-summary-check-current)
6458                    (not unread)
6459                    (not (gnus-data-unread-p (car data)))))
6460       (setq data (cdr data)))
6461     (when (setq result
6462                 (if unread
6463                     (progn
6464                       (while data
6465                         (unless (memq (gnus-data-number (car data))
6466                                       (cond
6467                                        ((eq gnus-auto-goto-ignores
6468                                             'always-undownloaded)
6469                                         gnus-newsgroup-undownloaded)
6470                                        (gnus-plugged
6471                                         nil)
6472                                        ((eq gnus-auto-goto-ignores
6473                                             'unfetched)
6474                                         gnus-newsgroup-unfetched)
6475                                        ((eq gnus-auto-goto-ignores
6476                                             'undownloaded)
6477                                         gnus-newsgroup-undownloaded)))
6478                           (when (gnus-data-unread-p (car data))
6479                             (setq result (car data)
6480                                   data nil)))
6481                         (setq data (cdr data)))
6482                       result)
6483                   (car data)))
6484       (goto-char (gnus-data-pos result))
6485       (gnus-data-number result))))
6486
6487 (defun gnus-summary-find-subject (subject &optional unread backward article)
6488   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6489          (article (or article (gnus-summary-article-number)))
6490          (articles (gnus-data-list backward))
6491          (arts (gnus-data-find-list article articles))
6492          result)
6493     (when (or (not gnus-summary-check-current)
6494               (not unread)
6495               (not (gnus-data-unread-p (car arts))))
6496       (setq arts (cdr arts)))
6497     (while arts
6498       (and (or (not unread)
6499                (gnus-data-unread-p (car arts)))
6500            (vectorp (gnus-data-header (car arts)))
6501            (gnus-subject-equal
6502             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6503            (setq result (car arts)
6504                  arts nil))
6505       (setq arts (cdr arts)))
6506     (and result
6507          (goto-char (gnus-data-pos result))
6508          (gnus-data-number result))))
6509
6510 (defun gnus-summary-search-forward (&optional unread subject backward)
6511   "Search forward for an article.
6512 If UNREAD, look for unread articles.  If SUBJECT, look for
6513 articles with that subject.  If BACKWARD, search backward instead."
6514   (cond (subject (gnus-summary-find-subject subject unread backward))
6515         (backward (gnus-summary-find-prev unread))
6516         (t (gnus-summary-find-next unread))))
6517
6518 (defun gnus-recenter (&optional n)
6519   "Center point in window and redisplay frame.
6520 Also do horizontal recentering."
6521   (interactive "P")
6522   (when (and gnus-auto-center-summary
6523              (not (eq gnus-auto-center-summary 'vertical)))
6524     (gnus-horizontal-recenter))
6525   (recenter n))
6526
6527 (defun gnus-summary-recenter ()
6528   "Center point in the summary window.
6529 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6530 displayed, no centering will be performed."
6531   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6532   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6533   (interactive)
6534   ;; The user has to want it.
6535   (when gnus-auto-center-summary
6536     (let* ((top (cond ((< (window-height) 4) 0)
6537                       ((< (window-height) 7) 1)
6538                       (t (if (numberp gnus-auto-center-summary)
6539                              gnus-auto-center-summary
6540                            (/ (1- (window-height)) 2)))))
6541            (height (1- (window-height)))
6542            (bottom (save-excursion (goto-char (point-max))
6543                                    (forward-line (- height))
6544                                    (point)))
6545            (window (get-buffer-window (current-buffer))))
6546       (when (get-buffer-window gnus-article-buffer)
6547         ;; Only do recentering when the article buffer is displayed,
6548         ;; Set the window start to either `bottom', which is the biggest
6549         ;; possible valid number, or the second line from the top,
6550         ;; whichever is the least.
6551         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6552           (if (> bottom top-pos)
6553               ;; Keep the second line from the top visible
6554               (set-window-start window top-pos t)
6555             ;; Try to keep the bottom line visible; if it's partially
6556             ;; obscured, either scroll one more line to make it fully
6557             ;; visible, or revert to using TOP-POS.
6558             (save-excursion
6559               (goto-char (point-max))
6560               (forward-line -1)
6561               (let ((last-line-start (point)))
6562                 (goto-char bottom)
6563                 (set-window-start window (point) t)
6564                 (when (not (pos-visible-in-window-p last-line-start window))
6565                   (forward-line 1)
6566                   (set-window-start window (min (point) top-pos) t)))))))
6567       ;; Do horizontal recentering while we're at it.
6568       (when (and (get-buffer-window (current-buffer) t)
6569                  (not (eq gnus-auto-center-summary 'vertical)))
6570         (let ((selected (selected-window)))
6571           (select-window (get-buffer-window (current-buffer) t))
6572           (gnus-summary-position-point)
6573           (gnus-horizontal-recenter)
6574           (select-window selected))))))
6575
6576 (defun gnus-summary-jump-to-group (newsgroup)
6577   "Move point to NEWSGROUP in group mode buffer."
6578   ;; Keep update point of group mode buffer if visible.
6579   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6580       (save-window-excursion
6581         ;; Take care of tree window mode.
6582         (when (get-buffer-window gnus-group-buffer)
6583           (pop-to-buffer gnus-group-buffer))
6584         (gnus-group-jump-to-group newsgroup))
6585     (save-excursion
6586       ;; Take care of tree window mode.
6587       (if (get-buffer-window gnus-group-buffer)
6588           (pop-to-buffer gnus-group-buffer)
6589         (set-buffer gnus-group-buffer))
6590       (gnus-group-jump-to-group newsgroup))))
6591
6592 ;; This function returns a list of article numbers based on the
6593 ;; difference between the ranges of read articles in this group and
6594 ;; the range of active articles.
6595 (defun gnus-list-of-unread-articles (group)
6596   (let* ((read (gnus-info-read (gnus-get-info group)))
6597          (active (or (gnus-active group) (gnus-activate-group group)))
6598          (last (or (cdr active)
6599                    (error "Group %s couldn't be activated " group)))
6600          first nlast unread)
6601     ;; If none are read, then all are unread.
6602     (if (not read)
6603         (setq first (car active))
6604       ;; If the range of read articles is a single range, then the
6605       ;; first unread article is the article after the last read
6606       ;; article.  Sounds logical, doesn't it?
6607       (if (and (not (listp (cdr read)))
6608                (or (< (car read) (car active))
6609                    (progn (setq read (list read))
6610                           nil)))
6611           (setq first (max (car active) (1+ (cdr read))))
6612         ;; `read' is a list of ranges.
6613         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6614                                   (caar read)))
6615                   1)
6616           (setq first (car active)))
6617         (while read
6618           (when first
6619             (while (< first nlast)
6620               (setq unread (cons first unread)
6621                     first (1+ first))))
6622           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6623           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6624           (setq read (cdr read)))))
6625     ;; And add the last unread articles.
6626     (while (<= first last)
6627       (setq unread (cons first unread)
6628             first (1+ first)))
6629     ;; Return the list of unread articles.
6630     (delq 0 (nreverse unread))))
6631
6632 (defun gnus-list-of-read-articles (group)
6633   "Return a list of unread, unticked and non-dormant articles."
6634   (let* ((info (gnus-get-info group))
6635          (marked (gnus-info-marks info))
6636          (active (gnus-active group)))
6637     (and info active
6638          (gnus-list-range-difference
6639           (gnus-list-range-difference
6640            (gnus-sorted-complement
6641             (gnus-uncompress-range active)
6642             (gnus-list-of-unread-articles group))
6643            (cdr (assq 'dormant marked)))
6644           (cdr (assq 'tick marked))))))
6645
6646 ;; This function returns a sequence of article numbers based on the
6647 ;; difference between the ranges of read articles in this group and
6648 ;; the range of active articles.
6649 (defun gnus-sequence-of-unread-articles (group)
6650   (let* ((read (gnus-info-read (gnus-get-info group)))
6651          (active (or (gnus-active group) (gnus-activate-group group)))
6652          (last (cdr active))
6653          first nlast unread)
6654     ;; If none are read, then all are unread.
6655     (if (not read)
6656         (setq first (car active))
6657       ;; If the range of read articles is a single range, then the
6658       ;; first unread article is the article after the last read
6659       ;; article.  Sounds logical, doesn't it?
6660       (if (and (not (listp (cdr read)))
6661                (or (< (car read) (car active))
6662                    (progn (setq read (list read))
6663                           nil)))
6664           (setq first (max (car active) (1+ (cdr read))))
6665         ;; `read' is a list of ranges.
6666         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6667                                   (caar read)))
6668                   1)
6669           (setq first (car active)))
6670         (while read
6671           (when first
6672             (push (cons first nlast) unread))
6673           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6674           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6675           (setq read (cdr read)))))
6676     ;; And add the last unread articles.
6677     (cond ((not (and first last))
6678            nil)
6679           ((< first last)
6680            (push (cons first last) unread))
6681           ((= first last)
6682            (push first unread)))
6683     ;; Return the sequence of unread articles.
6684     (delq 0 (nreverse unread))))
6685
6686 ;; Various summary commands
6687
6688 (defun gnus-summary-select-article-buffer ()
6689   "Reconfigure windows to show article buffer."
6690   (interactive)
6691   (if (not (gnus-buffer-live-p gnus-article-buffer))
6692       (error "There is no article buffer for this summary buffer")
6693     (gnus-configure-windows 'article)
6694     (select-window (get-buffer-window gnus-article-buffer))))
6695
6696 (defun gnus-summary-universal-argument (arg)
6697   "Perform any operation on all articles that are process/prefixed."
6698   (interactive "P")
6699   (let ((articles (gnus-summary-work-articles arg))
6700         func article)
6701     (if (eq
6702          (setq
6703           func
6704           (key-binding
6705            (read-key-sequence
6706             (substitute-command-keys
6707              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6708          'undefined)
6709         (gnus-error 1 "Undefined key")
6710       (save-excursion
6711         (while articles
6712           (gnus-summary-goto-subject (setq article (pop articles)))
6713           (let (gnus-newsgroup-processable)
6714             (command-execute func))
6715           (gnus-summary-remove-process-mark article)))))
6716   (gnus-summary-position-point))
6717
6718 (defun gnus-summary-toggle-truncation (&optional arg)
6719   "Toggle truncation of summary lines.
6720 With ARG, turn line truncation on if ARG is positive."
6721   (interactive "P")
6722   (setq truncate-lines
6723         (if (null arg) (not truncate-lines)
6724           (> (prefix-numeric-value arg) 0)))
6725   (redraw-display))
6726
6727 (defun gnus-summary-find-for-reselect ()
6728   "Return the number of an article to stay on across a reselect.
6729 The current article is considered, then following articles, then previous
6730 articles.  An article is sought which is not cancelled and isn't a temporary
6731 insertion from another group.  If there's no such then return a dummy 0."
6732   (let (found)
6733     (dolist (rev '(nil t))
6734       (unless found      ; don't demand the reverse list if we don't need it
6735         (let ((data (gnus-data-find-list
6736                      (gnus-summary-article-number) (gnus-data-list rev))))
6737           (while (and data (not found))
6738             (if (and (< 0 (gnus-data-number (car data)))
6739                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6740                 (setq found (gnus-data-number (car data))))
6741             (setq data (cdr data))))))
6742     (or found 0)))
6743
6744 (defun gnus-summary-reselect-current-group (&optional all rescan)
6745   "Exit and then reselect the current newsgroup.
6746 The prefix argument ALL means to select all articles."
6747   (interactive "P")
6748   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6749     (error "Ephemeral groups can't be reselected"))
6750   (let ((current-subject (gnus-summary-find-for-reselect))
6751         (group gnus-newsgroup-name))
6752     (setq gnus-newsgroup-begin nil)
6753     (gnus-summary-exit nil 'leave-hidden)
6754     ;; We have to adjust the point of group mode buffer because
6755     ;; point was moved to the next unread newsgroup by exiting.
6756     (gnus-summary-jump-to-group group)
6757     (when rescan
6758       (save-excursion
6759         (gnus-group-get-new-news-this-group 1)))
6760     (gnus-group-read-group all t)
6761     (gnus-summary-goto-subject current-subject nil t)))
6762
6763 (defun gnus-summary-rescan-group (&optional all)
6764   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6765   (interactive "P")
6766   (gnus-summary-reselect-current-group all t))
6767
6768 (defun gnus-summary-update-info (&optional non-destructive)
6769   (save-excursion
6770     (let ((group gnus-newsgroup-name))
6771       (when group
6772         (when gnus-newsgroup-kill-headers
6773           (setq gnus-newsgroup-killed
6774                 (gnus-compress-sequence
6775                  (gnus-sorted-union
6776                   (gnus-list-range-intersection
6777                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6778                   gnus-newsgroup-unreads)
6779                  t)))
6780         (unless (listp (cdr gnus-newsgroup-killed))
6781           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6782         (let ((headers gnus-newsgroup-headers))
6783           ;; Set the new ranges of read articles.
6784           (with-current-buffer gnus-group-buffer
6785             (gnus-undo-force-boundary))
6786           (gnus-update-read-articles
6787            group (gnus-sorted-union
6788                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6789           ;; Set the current article marks.
6790           (let ((gnus-newsgroup-scored
6791                  (if (and (not gnus-save-score)
6792                           (not non-destructive))
6793                      nil
6794                    gnus-newsgroup-scored)))
6795             (save-excursion
6796               (gnus-update-marks)))
6797           ;; Do the cross-ref thing.
6798           (when gnus-use-cross-reference
6799             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6800           ;; Do not switch windows but change the buffer to work.
6801           (set-buffer gnus-group-buffer)
6802           (unless (gnus-ephemeral-group-p group)
6803             (gnus-group-update-group group)))))))
6804
6805 (defun gnus-summary-save-newsrc (&optional force)
6806   "Save the current number of read/marked articles in the dribble buffer.
6807 The dribble buffer will then be saved.
6808 If FORCE (the prefix), also save the .newsrc file(s)."
6809   (interactive "P")
6810   (gnus-summary-update-info t)
6811   (if force
6812       (gnus-save-newsrc-file)
6813     (gnus-dribble-save)))
6814
6815 (defun gnus-summary-exit (&optional temporary leave-hidden)
6816   "Exit reading current newsgroup, and then return to group selection mode.
6817 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6818   (interactive)
6819   (gnus-set-global-variables)
6820   (when (gnus-buffer-live-p gnus-article-buffer)
6821     (save-excursion
6822       (set-buffer gnus-article-buffer)
6823       (mm-destroy-parts gnus-article-mime-handles)
6824       ;; Set it to nil for safety reason.
6825       (setq gnus-article-mime-handle-alist nil)
6826       (setq gnus-article-mime-handles nil)))
6827   (gnus-kill-save-kill-buffer)
6828   (gnus-async-halt-prefetch)
6829   (let* ((group gnus-newsgroup-name)
6830          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6831          (gnus-group-is-exiting-p t)
6832          (mode major-mode)
6833          (group-point nil)
6834          (buf (current-buffer)))
6835     (unless quit-config
6836       ;; Do adaptive scoring, and possibly save score files.
6837       (when gnus-newsgroup-adaptive
6838         (gnus-score-adaptive))
6839       (when gnus-use-scoring
6840         (gnus-score-save)))
6841     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6842     ;; If we have several article buffers, we kill them at exit.
6843     (unless gnus-single-article-buffer
6844       (gnus-kill-buffer gnus-original-article-buffer)
6845       (setq gnus-article-current nil))
6846     (when gnus-use-cache
6847       (gnus-cache-possibly-remove-articles)
6848       (gnus-cache-save-buffers))
6849     (gnus-async-prefetch-remove-group group)
6850     (when gnus-suppress-duplicates
6851       (gnus-dup-enter-articles))
6852     (when gnus-use-trees
6853       (gnus-tree-close group))
6854     (when gnus-use-cache
6855       (gnus-cache-write-active))
6856     ;; Remove entries for this group.
6857     (nnmail-purge-split-history (gnus-group-real-name group))
6858     ;; Make all changes in this group permanent.
6859     (unless quit-config
6860       (gnus-run-hooks 'gnus-exit-group-hook)
6861       (gnus-summary-update-info))
6862     (gnus-close-group group)
6863     ;; Make sure where we were, and go to next newsgroup.
6864     (set-buffer gnus-group-buffer)
6865     (unless quit-config
6866       (gnus-group-jump-to-group group))
6867     (gnus-run-hooks 'gnus-summary-exit-hook)
6868     (unless (or quit-config
6869                 ;; If this group has disappeared from the summary
6870                 ;; buffer, don't skip forwards.
6871                 (not (string= group (gnus-group-group-name))))
6872       (gnus-group-next-unread-group 1))
6873     (setq group-point (point))
6874     (if temporary
6875         nil                             ;Nothing to do.
6876       ;; If we have several article buffers, we kill them at exit.
6877       (unless gnus-single-article-buffer
6878         (gnus-kill-buffer gnus-article-buffer)
6879         (gnus-kill-buffer gnus-original-article-buffer)
6880         (setq gnus-article-current nil))
6881       (set-buffer buf)
6882       (if (not gnus-kill-summary-on-exit)
6883           (progn
6884             (gnus-deaden-summary)
6885             (setq mode nil))
6886         ;; We set all buffer-local variables to nil.  It is unclear why
6887         ;; this is needed, but if we don't, buffer-local variables are
6888         ;; not garbage-collected, it seems.  This would the lead to en
6889         ;; ever-growing Emacs.
6890         (gnus-summary-clear-local-variables)
6891         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6892           (gnus-summary-clear-local-variables))
6893         (when (get-buffer gnus-article-buffer)
6894           (bury-buffer gnus-article-buffer))
6895         ;; Return to group mode buffer.
6896         (when (eq mode 'gnus-summary-mode)
6897           (gnus-kill-buffer buf)))
6898       (setq gnus-current-select-method gnus-select-method)
6899       (set-buffer gnus-group-buffer)
6900       (if quit-config
6901           (gnus-handle-ephemeral-exit quit-config)
6902         (goto-char group-point)
6903         ;; If gnus-group-buffer is already displayed, make sure we also move
6904         ;; the cursor in the window that displays it.
6905         (let ((win (get-buffer-window (current-buffer) 0)))
6906           (if win (set-window-point win (point))))
6907         (unless leave-hidden
6908           (gnus-configure-windows 'group 'force)))
6909       ;; Clear the current group name.
6910       (unless quit-config
6911         (setq gnus-newsgroup-name nil)))))
6912
6913 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6914 (defun gnus-summary-exit-no-update (&optional no-questions)
6915   "Quit reading current newsgroup without updating read article info."
6916   (interactive)
6917   (let* ((group gnus-newsgroup-name)
6918          (gnus-group-is-exiting-p t)
6919          (gnus-group-is-exiting-without-update-p t)
6920          (quit-config (gnus-group-quit-config group)))
6921     (when (or no-questions
6922               gnus-expert-user
6923               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6924       (gnus-async-halt-prefetch)
6925       (run-hooks 'gnus-summary-prepare-exit-hook)
6926       (when (gnus-buffer-live-p gnus-article-buffer)
6927         (save-excursion
6928           (set-buffer gnus-article-buffer)
6929           (mm-destroy-parts gnus-article-mime-handles)
6930           ;; Set it to nil for safety reason.
6931           (setq gnus-article-mime-handle-alist nil)
6932           (setq gnus-article-mime-handles nil)))
6933       ;; If we have several article buffers, we kill them at exit.
6934       (unless gnus-single-article-buffer
6935         (gnus-kill-buffer gnus-article-buffer)
6936         (gnus-kill-buffer gnus-original-article-buffer)
6937         (setq gnus-article-current nil))
6938       (if (not gnus-kill-summary-on-exit)
6939           (gnus-deaden-summary)
6940         (gnus-close-group group)
6941         (gnus-summary-clear-local-variables)
6942         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6943           (gnus-summary-clear-local-variables))
6944         (gnus-kill-buffer gnus-summary-buffer))
6945       (unless gnus-single-article-buffer
6946         (setq gnus-article-current nil))
6947       (when gnus-use-trees
6948         (gnus-tree-close group))
6949       (gnus-async-prefetch-remove-group group)
6950       (when (get-buffer gnus-article-buffer)
6951         (bury-buffer gnus-article-buffer))
6952       ;; Return to the group buffer.
6953       (gnus-configure-windows 'group 'force)
6954       ;; Clear the current group name.
6955       (setq gnus-newsgroup-name nil)
6956       (unless (gnus-ephemeral-group-p group)
6957         (gnus-group-update-group group))
6958       (when (equal (gnus-group-group-name) group)
6959         (gnus-group-next-unread-group 1))
6960       (when quit-config
6961         (gnus-handle-ephemeral-exit quit-config)))))
6962
6963 (defun gnus-handle-ephemeral-exit (quit-config)
6964   "Handle movement when leaving an ephemeral group.
6965 The state which existed when entering the ephemeral is reset."
6966   (if (not (buffer-name (car quit-config)))
6967       (gnus-configure-windows 'group 'force)
6968     (set-buffer (car quit-config))
6969     (cond ((eq major-mode 'gnus-summary-mode)
6970            (gnus-set-global-variables))
6971           ((eq major-mode 'gnus-article-mode)
6972            (save-excursion
6973              ;; The `gnus-summary-buffer' variable may point
6974              ;; to the old summary buffer when using a single
6975              ;; article buffer.
6976              (unless (gnus-buffer-live-p gnus-summary-buffer)
6977                (set-buffer gnus-group-buffer))
6978              (set-buffer gnus-summary-buffer)
6979              (gnus-set-global-variables))))
6980     (if (or (eq (cdr quit-config) 'article)
6981             (eq (cdr quit-config) 'pick))
6982         (progn
6983           ;; The current article may be from the ephemeral group
6984           ;; thus it is best that we reload this article
6985           ;;
6986           ;; If we're exiting from a large digest, this can be
6987           ;; extremely slow.  So, it's better not to reload it. -- jh.
6988           ;;(gnus-summary-show-article)
6989           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6990               (gnus-configure-windows 'pick 'force)
6991             (gnus-configure-windows (cdr quit-config) 'force)))
6992       (gnus-configure-windows (cdr quit-config) 'force))
6993     (when (eq major-mode 'gnus-summary-mode)
6994       (gnus-summary-next-subject 1 nil t)
6995       (gnus-summary-recenter)
6996       (gnus-summary-position-point))))
6997
6998 ;;; Dead summaries.
6999
7000 (defvar gnus-dead-summary-mode-map nil)
7001
7002 (unless gnus-dead-summary-mode-map
7003   (setq gnus-dead-summary-mode-map (make-keymap))
7004   (suppress-keymap gnus-dead-summary-mode-map)
7005   (substitute-key-definition
7006    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7007   (dolist (key '("\C-d" "\r" "\177" [delete]))
7008     (define-key gnus-dead-summary-mode-map
7009       key 'gnus-summary-wake-up-the-dead))
7010   (dolist (key '("q" "Q"))
7011     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7012
7013 (defvar gnus-dead-summary-mode nil
7014   "Minor mode for Gnus summary buffers.")
7015
7016 (defun gnus-dead-summary-mode (&optional arg)
7017   "Minor mode for Gnus summary buffers."
7018   (interactive "P")
7019   (when (eq major-mode 'gnus-summary-mode)
7020     (make-local-variable 'gnus-dead-summary-mode)
7021     (setq gnus-dead-summary-mode
7022           (if (null arg) (not gnus-dead-summary-mode)
7023             (> (prefix-numeric-value arg) 0)))
7024     (when gnus-dead-summary-mode
7025       (add-minor-mode
7026        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7027
7028 (defun gnus-deaden-summary ()
7029   "Make the current summary buffer into a dead summary buffer."
7030   ;; Kill any previous dead summary buffer.
7031   (when (and gnus-dead-summary
7032              (buffer-name gnus-dead-summary))
7033     (with-current-buffer gnus-dead-summary
7034       (when gnus-dead-summary-mode
7035         (kill-buffer (current-buffer)))))
7036   ;; Make this the current dead summary.
7037   (setq gnus-dead-summary (current-buffer))
7038   (gnus-dead-summary-mode 1)
7039   (let ((name (buffer-name)))
7040     (when (string-match "Summary" name)
7041       (rename-buffer
7042        (concat (substring name 0 (match-beginning 0)) "Dead "
7043                (substring name (match-beginning 0)))
7044        t)
7045       (bury-buffer))))
7046
7047 (defun gnus-kill-or-deaden-summary (buffer)
7048   "Kill or deaden the summary BUFFER."
7049   (save-excursion
7050     (when (and (buffer-name buffer)
7051                (not gnus-single-article-buffer))
7052       (with-current-buffer buffer
7053         (gnus-kill-buffer gnus-article-buffer)
7054         (gnus-kill-buffer gnus-original-article-buffer)))
7055     (cond
7056      ;; Kill the buffer.
7057      (gnus-kill-summary-on-exit
7058       (when (and gnus-use-trees
7059                  (gnus-buffer-exists-p buffer))
7060         (save-excursion
7061           (set-buffer buffer)
7062           (gnus-tree-close gnus-newsgroup-name)))
7063       (gnus-kill-buffer buffer))
7064      ;; Deaden the buffer.
7065      ((gnus-buffer-exists-p buffer)
7066       (save-excursion
7067         (set-buffer buffer)
7068         (gnus-deaden-summary))))))
7069
7070 (defun gnus-summary-wake-up-the-dead (&rest args)
7071   "Wake up the dead summary buffer."
7072   (interactive)
7073   (gnus-dead-summary-mode -1)
7074   (let ((name (buffer-name)))
7075     (when (string-match "Dead " name)
7076       (rename-buffer
7077        (concat (substring name 0 (match-beginning 0))
7078                (substring name (match-end 0)))
7079        t)))
7080   (gnus-message 3 "This dead summary is now alive again"))
7081
7082 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7083 (defun gnus-summary-fetch-faq (&optional faq-dir)
7084   "Fetch the FAQ for the current group.
7085 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7086 in."
7087   (interactive
7088    (list
7089     (when current-prefix-arg
7090       (completing-read
7091        "FAQ dir: " (and (listp gnus-group-faq-directory)
7092                         (mapcar 'list
7093                                 gnus-group-faq-directory))))))
7094   (let (gnus-faq-buffer)
7095     (when (setq gnus-faq-buffer
7096                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7097       (gnus-configure-windows 'summary-faq))))
7098
7099 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7100 (defun gnus-summary-describe-group (&optional force)
7101   "Describe the current newsgroup."
7102   (interactive "P")
7103   (gnus-group-describe-group force gnus-newsgroup-name))
7104
7105 (defun gnus-summary-describe-briefly ()
7106   "Describe summary mode commands briefly."
7107   (interactive)
7108   (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")))
7109
7110 ;; Walking around group mode buffer from summary mode.
7111
7112 (defun gnus-summary-next-group (&optional no-article target-group backward)
7113   "Exit current newsgroup and then select next unread newsgroup.
7114 If prefix argument NO-ARTICLE is non-nil, no article is selected
7115 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7116 previous group instead."
7117   (interactive "P")
7118   ;; Stop pre-fetching.
7119   (gnus-async-halt-prefetch)
7120   (let ((current-group gnus-newsgroup-name)
7121         (current-buffer (current-buffer))
7122         entered)
7123     ;; First we semi-exit this group to update Xrefs and all variables.
7124     ;; We can't do a real exit, because the window conf must remain
7125     ;; the same in case the user is prompted for info, and we don't
7126     ;; want the window conf to change before that...
7127     (gnus-summary-exit t)
7128     (while (not entered)
7129       ;; Then we find what group we are supposed to enter.
7130       (set-buffer gnus-group-buffer)
7131       (gnus-group-jump-to-group current-group)
7132       (setq target-group
7133             (or target-group
7134                 (if (eq gnus-keep-same-level 'best)
7135                     (gnus-summary-best-group gnus-newsgroup-name)
7136                   (gnus-summary-search-group backward gnus-keep-same-level))))
7137       (if (not target-group)
7138           ;; There are no further groups, so we return to the group
7139           ;; buffer.
7140           (progn
7141             (gnus-message 5 "Returning to the group buffer")
7142             (setq entered t)
7143             (when (gnus-buffer-live-p current-buffer)
7144               (set-buffer current-buffer)
7145               (gnus-summary-exit))
7146             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7147         ;; We try to enter the target group.
7148         (gnus-group-jump-to-group target-group)
7149         (let ((unreads (gnus-group-group-unread)))
7150           (if (and (or (eq t unreads)
7151                        (and unreads (not (zerop unreads))))
7152                    (gnus-summary-read-group
7153                     target-group nil no-article
7154                     (and (buffer-name current-buffer) current-buffer)
7155                     nil backward))
7156               (setq entered t)
7157             (setq current-group target-group
7158                   target-group nil)))))))
7159
7160 (defun gnus-summary-prev-group (&optional no-article)
7161   "Exit current newsgroup and then select previous unread newsgroup.
7162 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7163   (interactive "P")
7164   (gnus-summary-next-group no-article nil t))
7165
7166 ;; Walking around summary lines.
7167
7168 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7169   "Go to the first subject satisfying any non-nil constraint.
7170 If UNREAD is non-nil, the article should be unread.
7171 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7172 If UNSEEN is non-nil, the article should be unseen.
7173 Returns the article selected or nil if there are no matching articles."
7174   (interactive "P")
7175   (cond
7176    ;; Empty summary.
7177    ((null gnus-newsgroup-data)
7178     (gnus-message 3 "No articles in the group")
7179     nil)
7180    ;; Pick the first article.
7181    ((not (or unread undownloaded unseen))
7182     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7183     (gnus-data-number (car gnus-newsgroup-data)))
7184    ;; Find the first unread article.
7185    (t
7186     (let ((data gnus-newsgroup-data))
7187       (while (and data
7188                   (let ((num (gnus-data-number (car data))))
7189                     (or (memq num gnus-newsgroup-unfetched)
7190                         (not (or (and unread
7191                                       (memq num gnus-newsgroup-unreads))
7192                                  (and undownloaded
7193                                       (memq num gnus-newsgroup-undownloaded))
7194                                  (and unseen
7195                                       (memq num gnus-newsgroup-unseen)))))))
7196         (setq data (cdr data)))
7197       (prog1
7198           (if data
7199               (progn
7200                 (goto-char (gnus-data-pos (car data)))
7201                 (gnus-data-number (car data)))
7202             (gnus-message 3 "No more%s articles"
7203                           (let* ((r (when unread " unread"))
7204                                  (d (when undownloaded " undownloaded"))
7205                                  (s (when unseen " unseen"))
7206                                  (l (delq nil (list r d s))))
7207                             (cond ((= 3 (length l))
7208                                    (concat r "," d ", or" s))
7209                                   ((= 2 (length l))
7210                                    (concat (car l) ", or" (cadr l)))
7211                                   ((= 1 (length l))
7212                                    (car l))
7213                                   (t
7214                                    ""))))
7215             nil
7216             )
7217         (gnus-summary-position-point))))))
7218
7219 (defun gnus-summary-next-subject (n &optional unread dont-display)
7220   "Go to next N'th summary line.
7221 If N is negative, go to the previous N'th subject line.
7222 If UNREAD is non-nil, only unread articles are selected.
7223 The difference between N and the actual number of steps taken is
7224 returned."
7225   (interactive "p")
7226   (let ((backward (< n 0))
7227         (n (abs n)))
7228     (while (and (> n 0)
7229                 (if backward
7230                     (gnus-summary-find-prev unread)
7231                   (gnus-summary-find-next unread)))
7232       (unless (zerop (setq n (1- n)))
7233         (gnus-summary-show-thread)))
7234     (when (/= 0 n)
7235       (gnus-message 7 "No more%s articles"
7236                     (if unread " unread" "")))
7237     (unless dont-display
7238       (gnus-summary-recenter)
7239       (gnus-summary-position-point))
7240     n))
7241
7242 (defun gnus-summary-next-unread-subject (n)
7243   "Go to next N'th unread summary line."
7244   (interactive "p")
7245   (gnus-summary-next-subject n t))
7246
7247 (defun gnus-summary-prev-subject (n &optional unread)
7248   "Go to previous N'th summary line.
7249 If optional argument UNREAD is non-nil, only unread article is selected."
7250   (interactive "p")
7251   (gnus-summary-next-subject (- n) unread))
7252
7253 (defun gnus-summary-prev-unread-subject (n)
7254   "Go to previous N'th unread summary line."
7255   (interactive "p")
7256   (gnus-summary-next-subject (- n) t))
7257
7258 (defun gnus-summary-goto-subjects (articles)
7259   "Insert the subject header for ARTICLES in the current buffer."
7260   (save-excursion
7261     (dolist (article articles)
7262       (gnus-summary-goto-subject article t)))
7263   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7264   (gnus-summary-position-point))
7265
7266 (defun gnus-summary-goto-subject (article &optional force silent)
7267   "Go the subject line of ARTICLE.
7268 If FORCE, also allow jumping to articles not currently shown."
7269   (interactive "nArticle number: ")
7270   (unless (numberp article)
7271     (error "Article %s is not a number" article))
7272   (let ((b (point))
7273         (data (gnus-data-find article)))
7274     ;; We read in the article if we have to.
7275     (and (not data)
7276          force
7277          (gnus-summary-insert-subject
7278           article
7279           (if (or (numberp force) (vectorp force)) force)
7280           t)
7281          (setq data (gnus-data-find article)))
7282     (goto-char b)
7283     (if (not data)
7284         (progn
7285           (unless silent
7286             (gnus-message 3 "Can't find article %d" article))
7287           nil)
7288       (let ((pt (gnus-data-pos data)))
7289         (goto-char pt)
7290         (gnus-summary-set-article-display-arrow pt))
7291       (gnus-summary-position-point)
7292       article)))
7293
7294 ;; Walking around summary lines with displaying articles.
7295
7296 (defun gnus-summary-expand-window (&optional arg)
7297   "Make the summary buffer take up the entire Emacs frame.
7298 Given a prefix, will force an `article' buffer configuration."
7299   (interactive "P")
7300   (if arg
7301       (gnus-configure-windows 'article 'force)
7302     (gnus-configure-windows 'summary 'force)))
7303
7304 (defun gnus-summary-display-article (article &optional all-header)
7305   "Display ARTICLE in article buffer."
7306   (when (gnus-buffer-live-p gnus-article-buffer)
7307     (with-current-buffer gnus-article-buffer
7308       (mm-enable-multibyte)))
7309   (gnus-set-global-variables)
7310   (when (gnus-buffer-live-p gnus-article-buffer)
7311     (with-current-buffer gnus-article-buffer
7312       (setq gnus-article-charset gnus-newsgroup-charset)
7313       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7314       (mm-enable-multibyte)))
7315   (if (null article)
7316       nil
7317     (prog1
7318         (if gnus-summary-display-article-function
7319             (funcall gnus-summary-display-article-function article all-header)
7320           (gnus-article-prepare article all-header))
7321       (gnus-run-hooks 'gnus-select-article-hook)
7322       (when (and gnus-current-article
7323                  (not (zerop gnus-current-article)))
7324         (gnus-summary-goto-subject gnus-current-article))
7325       (gnus-summary-recenter)
7326       (when (and gnus-use-trees gnus-show-threads)
7327         (gnus-possibly-generate-tree article)
7328         (gnus-highlight-selected-tree article))
7329       ;; Successfully display article.
7330       (gnus-article-set-window-start
7331        (cdr (assq article gnus-newsgroup-bookmarks))))))
7332
7333 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7334   "Select the current article.
7335 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7336 non-nil, the article will be re-fetched even if it already present in
7337 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7338 be displayed."
7339   ;; Make sure we are in the summary buffer to work around bbdb bug.
7340   (unless (eq major-mode 'gnus-summary-mode)
7341     (set-buffer gnus-summary-buffer))
7342   (let ((article (or article (gnus-summary-article-number)))
7343         (all-headers (not (not all-headers))) ;Must be t or nil.
7344         gnus-summary-display-article-function)
7345     (and (not pseudo)
7346          (gnus-summary-article-pseudo-p article)
7347          (error "This is a pseudo-article"))
7348     (save-excursion
7349       (set-buffer gnus-summary-buffer)
7350       (if (or (and gnus-single-article-buffer
7351                    (or (null gnus-current-article)
7352                        (null gnus-article-current)
7353                        (null (get-buffer gnus-article-buffer))
7354                        (not (eq article (cdr gnus-article-current)))
7355                        (not (equal (car gnus-article-current)
7356                                    gnus-newsgroup-name))))
7357               (and (not gnus-single-article-buffer)
7358                    (or (null gnus-current-article)
7359                        (not (eq gnus-current-article article))))
7360               force)
7361           ;; The requested article is different from the current article.
7362           (progn
7363             (gnus-summary-display-article article all-headers)
7364             (when (gnus-buffer-live-p gnus-article-buffer)
7365               (with-current-buffer gnus-article-buffer
7366                 (if (not gnus-article-decoded-p) ;; a local variable
7367                     (mm-disable-multibyte))))
7368             (gnus-article-set-window-start
7369              (cdr (assq article gnus-newsgroup-bookmarks)))
7370             article)
7371         'old))))
7372
7373 (defun gnus-summary-force-verify-and-decrypt ()
7374   "Display buttons for signed/encrypted parts and verify/decrypt them."
7375   (interactive)
7376   (let ((mm-verify-option 'known)
7377         (mm-decrypt-option 'known)
7378         (gnus-article-emulate-mime t)
7379         (gnus-buttonized-mime-types (append (list "multipart/signed"
7380                                                   "multipart/encrypted")
7381                                             gnus-buttonized-mime-types)))
7382     (gnus-summary-select-article nil 'force)))
7383
7384 (defun gnus-summary-set-current-mark (&optional current-mark)
7385   "Obsolete function."
7386   nil)
7387
7388 (defun gnus-summary-next-article (&optional unread subject backward push)
7389   "Select the next article.
7390 If UNREAD, only unread articles are selected.
7391 If SUBJECT, only articles with SUBJECT are selected.
7392 If BACKWARD, the previous article is selected instead of the next."
7393   (interactive "P")
7394   (cond
7395    ;; Is there such an article?
7396    ((and (gnus-summary-search-forward unread subject backward)
7397          (or (gnus-summary-display-article (gnus-summary-article-number))
7398              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7399     (gnus-summary-position-point))
7400    ;; If not, we try the first unread, if that is wanted.
7401    ((and subject
7402          gnus-auto-select-same
7403          (gnus-summary-first-unread-article))
7404     (gnus-summary-position-point)
7405     (gnus-message 6 "Wrapped"))
7406    ;; Try to get next/previous article not displayed in this group.
7407    ((and gnus-auto-extend-newsgroup
7408          (not unread) (not subject))
7409     (gnus-summary-goto-article
7410      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7411      nil (count-lines (point-min) (point))))
7412    ;; Go to next/previous group.
7413    (t
7414     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7415       (gnus-summary-jump-to-group gnus-newsgroup-name))
7416     (let ((cmd last-command-char)
7417           (point
7418            (with-current-buffer gnus-group-buffer
7419              (point)))
7420           (group
7421            (if (eq gnus-keep-same-level 'best)
7422                (gnus-summary-best-group gnus-newsgroup-name)
7423              (gnus-summary-search-group backward gnus-keep-same-level))))
7424       ;; For some reason, the group window gets selected.  We change
7425       ;; it back.
7426       (select-window (get-buffer-window (current-buffer)))
7427       ;; Select next unread newsgroup automagically.
7428       (cond
7429        ((or (not gnus-auto-select-next)
7430             (not cmd))
7431         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7432        ((or (eq gnus-auto-select-next 'quietly)
7433             (and (eq gnus-auto-select-next 'slightly-quietly)
7434                  push)
7435             (and (eq gnus-auto-select-next 'almost-quietly)
7436                  (gnus-summary-last-article-p)))
7437         ;; Select quietly.
7438         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7439             (gnus-summary-exit)
7440           (gnus-message 7 "No more%s articles (%s)..."
7441                         (if unread " unread" "")
7442                         (if group (concat "selecting " group)
7443                           "exiting"))
7444           (gnus-summary-next-group nil group backward)))
7445        (t
7446         (when (gnus-key-press-event-p last-input-event)
7447           (gnus-summary-walk-group-buffer
7448            gnus-newsgroup-name cmd unread backward point))))))))
7449
7450 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7451   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7452                       (?\C-p (gnus-group-prev-unread-group 1))))
7453         (cursor-in-echo-area t)
7454         keve key group ended prompt)
7455     (save-excursion
7456       (set-buffer gnus-group-buffer)
7457       (goto-char start)
7458       (setq group
7459             (if (eq gnus-keep-same-level 'best)
7460                 (gnus-summary-best-group gnus-newsgroup-name)
7461               (gnus-summary-search-group backward gnus-keep-same-level))))
7462     (while (not ended)
7463       (setq prompt
7464             (format
7465              "No more%s articles%s " (if unread " unread" "")
7466              (if (and group
7467                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7468                  (format " (Type %s for %s [%s])"
7469                          (single-key-description cmd)
7470                          (gnus-group-decoded-name group)
7471                          (gnus-group-unread group))
7472                (format " (Type %s to exit %s)"
7473                        (single-key-description cmd)
7474                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7475       ;; Confirm auto selection.
7476       (setq key (car (setq keve (gnus-read-event-char prompt)))
7477             ended t)
7478       (cond
7479        ((assq key keystrokes)
7480         (let ((obuf (current-buffer)))
7481           (switch-to-buffer gnus-group-buffer)
7482           (when group
7483             (gnus-group-jump-to-group group))
7484           (eval (cadr (assq key keystrokes)))
7485           (setq group (gnus-group-group-name))
7486           (switch-to-buffer obuf))
7487         (setq ended nil))
7488        ((equal key cmd)
7489         (if (or (not group)
7490                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7491             (gnus-summary-exit)
7492           (gnus-summary-next-group nil group backward)))
7493        (t
7494         (push (cdr keve) unread-command-events))))))
7495
7496 (defun gnus-summary-next-unread-article ()
7497   "Select unread article after current one."
7498   (interactive)
7499   (gnus-summary-next-article
7500    (or (not (eq gnus-summary-goto-unread 'never))
7501        (gnus-summary-last-article-p (gnus-summary-article-number)))
7502    (and gnus-auto-select-same
7503         (gnus-summary-article-subject))))
7504
7505 (defun gnus-summary-prev-article (&optional unread subject)
7506   "Select the article after the current one.
7507 If UNREAD is non-nil, only unread articles are selected."
7508   (interactive "P")
7509   (gnus-summary-next-article unread subject t))
7510
7511 (defun gnus-summary-prev-unread-article ()
7512   "Select unread article before current one."
7513   (interactive)
7514   (gnus-summary-prev-article
7515    (or (not (eq gnus-summary-goto-unread 'never))
7516        (gnus-summary-first-article-p (gnus-summary-article-number)))
7517    (and gnus-auto-select-same
7518         (gnus-summary-article-subject))))
7519
7520 (defun gnus-summary-next-page (&optional lines circular stop)
7521   "Show next page of the selected article.
7522 If at the end of the current article, select the next article.
7523 LINES says how many lines should be scrolled up.
7524
7525 If CIRCULAR is non-nil, go to the start of the article instead of
7526 selecting the next article when reaching the end of the current
7527 article.
7528
7529 If STOP is non-nil, just stop when reaching the end of the message.
7530
7531 Also see the variable `gnus-article-skip-boring'."
7532   (interactive "P")
7533   (setq gnus-summary-buffer (current-buffer))
7534   (gnus-set-global-variables)
7535   (let ((article (gnus-summary-article-number))
7536         (article-window (get-buffer-window gnus-article-buffer t))
7537         endp)
7538     ;; If the buffer is empty, we have no article.
7539     (unless article
7540       (error "No article to select"))
7541     (gnus-configure-windows 'article)
7542     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7543         (if (and (eq gnus-summary-goto-unread 'never)
7544                  (not (gnus-summary-last-article-p article)))
7545             (gnus-summary-next-article)
7546           (gnus-summary-next-unread-article))
7547       (if (or (null gnus-current-article)
7548               (null gnus-article-current)
7549               (/= article (cdr gnus-article-current))
7550               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7551           ;; Selected subject is different from current article's.
7552           (gnus-summary-display-article article)
7553         (when article-window
7554           (gnus-eval-in-buffer-window gnus-article-buffer
7555             (setq endp (or (gnus-article-next-page lines)
7556                            (gnus-article-only-boring-p))))
7557           (when endp
7558             (cond (stop
7559                    (gnus-message 3 "End of message"))
7560                   (circular
7561                    (gnus-summary-beginning-of-article))
7562                   (lines
7563                    (gnus-message 3 "End of message"))
7564                   ((null lines)
7565                    (if (and (eq gnus-summary-goto-unread 'never)
7566                             (not (gnus-summary-last-article-p article)))
7567                        (gnus-summary-next-article)
7568                      (gnus-summary-next-unread-article))))))))
7569     (gnus-summary-recenter)
7570     (gnus-summary-position-point)))
7571
7572 (defun gnus-summary-prev-page (&optional lines move)
7573   "Show previous page of selected article.
7574 Argument LINES specifies lines to be scrolled down.
7575 If MOVE, move to the previous unread article if point is at
7576 the beginning of the buffer."
7577   (interactive "P")
7578   (let ((article (gnus-summary-article-number))
7579         (article-window (get-buffer-window gnus-article-buffer t))
7580         endp)
7581     (gnus-configure-windows 'article)
7582     (if (or (null gnus-current-article)
7583             (null gnus-article-current)
7584             (/= article (cdr gnus-article-current))
7585             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7586         ;; Selected subject is different from current article's.
7587         (gnus-summary-display-article article)
7588       (gnus-summary-recenter)
7589       (when article-window
7590         (gnus-eval-in-buffer-window gnus-article-buffer
7591           (setq endp (gnus-article-prev-page lines)))
7592         (when (and move endp)
7593           (cond (lines
7594                  (gnus-message 3 "Beginning of message"))
7595                 ((null lines)
7596                  (if (and (eq gnus-summary-goto-unread 'never)
7597                           (not (gnus-summary-first-article-p article)))
7598                      (gnus-summary-prev-article)
7599                    (gnus-summary-prev-unread-article))))))))
7600   (gnus-summary-position-point))
7601
7602 (defun gnus-summary-prev-page-or-article (&optional lines)
7603   "Show previous page of selected article.
7604 Argument LINES specifies lines to be scrolled down.
7605 If at the beginning of the article, go to the next article."
7606   (interactive "P")
7607   (gnus-summary-prev-page lines t))
7608
7609 (defun gnus-summary-scroll-up (lines)
7610   "Scroll up (or down) one line current article.
7611 Argument LINES specifies lines to be scrolled up (or down if negative)."
7612   (interactive "p")
7613   (gnus-configure-windows 'article)
7614   (gnus-summary-show-thread)
7615   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7616     (gnus-eval-in-buffer-window gnus-article-buffer
7617       (cond ((> lines 0)
7618              (when (gnus-article-next-page lines)
7619                (gnus-message 3 "End of message")))
7620             ((< lines 0)
7621              (gnus-article-prev-page (- lines))))))
7622   (gnus-summary-recenter)
7623   (gnus-summary-position-point))
7624
7625 (defun gnus-summary-scroll-down (lines)
7626   "Scroll down (or up) one line current article.
7627 Argument LINES specifies lines to be scrolled down (or up if negative)."
7628   (interactive "p")
7629   (gnus-summary-scroll-up (- lines)))
7630
7631 (defun gnus-summary-next-same-subject ()
7632   "Select next article which has the same subject as current one."
7633   (interactive)
7634   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7635
7636 (defun gnus-summary-prev-same-subject ()
7637   "Select previous article which has the same subject as current one."
7638   (interactive)
7639   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7640
7641 (defun gnus-summary-next-unread-same-subject ()
7642   "Select next unread article which has the same subject as current one."
7643   (interactive)
7644   (gnus-summary-next-article t (gnus-summary-article-subject)))
7645
7646 (defun gnus-summary-prev-unread-same-subject ()
7647   "Select previous unread article which has the same subject as current one."
7648   (interactive)
7649   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7650
7651 (defun gnus-summary-first-unread-article ()
7652   "Select the first unread article.
7653 Return nil if there are no unread articles."
7654   (interactive)
7655   (prog1
7656       (when (gnus-summary-first-subject t)
7657         (gnus-summary-show-thread)
7658         (gnus-summary-first-subject t)
7659         (gnus-summary-display-article (gnus-summary-article-number)))
7660     (gnus-summary-position-point)))
7661
7662 (defun gnus-summary-first-unread-subject ()
7663   "Place the point on the subject line of the first unread article.
7664 Return nil if there are no unread articles."
7665   (interactive)
7666   (prog1
7667       (when (gnus-summary-first-subject t)
7668         (gnus-summary-show-thread)
7669         (gnus-summary-first-subject t))
7670     (gnus-summary-position-point)))
7671
7672 (defun gnus-summary-first-unseen-subject ()
7673   "Place the point on the subject line of the first unseen article.
7674 Return nil if there are no unseen articles."
7675   (interactive)
7676   (prog1
7677       (when (gnus-summary-first-subject nil nil t)
7678         (gnus-summary-show-thread)
7679         (gnus-summary-first-subject nil nil t))
7680     (gnus-summary-position-point)))
7681
7682 (defun gnus-summary-first-unseen-or-unread-subject ()
7683   "Place the point on the subject line of the first unseen article or,
7684 if all article have been seen, on the subject line of the first unread
7685 article."
7686   (interactive)
7687   (prog1
7688       (unless (when (gnus-summary-first-subject nil nil t)
7689                 (gnus-summary-show-thread)
7690                 (gnus-summary-first-subject nil nil t))
7691         (when (gnus-summary-first-subject t)
7692           (gnus-summary-show-thread)
7693           (gnus-summary-first-subject t)))
7694     (gnus-summary-position-point)))
7695
7696 (defun gnus-summary-first-article ()
7697   "Select the first article.
7698 Return nil if there are no articles."
7699   (interactive)
7700   (prog1
7701       (when (gnus-summary-first-subject)
7702         (gnus-summary-show-thread)
7703         (gnus-summary-first-subject)
7704         (gnus-summary-display-article (gnus-summary-article-number)))
7705     (gnus-summary-position-point)))
7706
7707 (defun gnus-summary-best-unread-article (&optional arg)
7708   "Select the unread article with the highest score.
7709 If given a prefix argument, select the next unread article that has a
7710 score higher than the default score."
7711   (interactive "P")
7712   (let ((article (if arg
7713                      (gnus-summary-better-unread-subject)
7714                    (gnus-summary-best-unread-subject))))
7715     (if article
7716         (gnus-summary-goto-article article)
7717       (error "No unread articles"))))
7718
7719 (defun gnus-summary-best-unread-subject ()
7720   "Select the unread subject with the highest score."
7721   (interactive)
7722   (let ((best -1000000)
7723         (data gnus-newsgroup-data)
7724         article score)
7725     (while data
7726       (and (gnus-data-unread-p (car data))
7727            (> (setq score
7728                     (gnus-summary-article-score (gnus-data-number (car data))))
7729               best)
7730            (setq best score
7731                  article (gnus-data-number (car data))))
7732       (setq data (cdr data)))
7733     (when article
7734       (gnus-summary-goto-subject article))
7735     (gnus-summary-position-point)
7736     article))
7737
7738 (defun gnus-summary-better-unread-subject ()
7739   "Select the first unread subject that has a score over the default score."
7740   (interactive)
7741   (let ((data gnus-newsgroup-data)
7742         article score)
7743     (while (and (setq article (gnus-data-number (car data)))
7744                 (or (gnus-data-read-p (car data))
7745                     (not (> (gnus-summary-article-score article)
7746                             gnus-summary-default-score))))
7747       (setq data (cdr data)))
7748     (when article
7749       (gnus-summary-goto-subject article))
7750     (gnus-summary-position-point)
7751     article))
7752
7753 (defun gnus-summary-last-subject ()
7754   "Go to the last displayed subject line in the group."
7755   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7756     (when article
7757       (gnus-summary-goto-subject article))))
7758
7759 (defun gnus-summary-goto-article (article &optional all-headers force)
7760   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7761 If ALL-HEADERS is non-nil, no header lines are hidden.
7762 If FORCE, go to the article even if it isn't displayed.  If FORCE
7763 is a number, it is the line the article is to be displayed on."
7764   (interactive
7765    (list
7766     (completing-read
7767      "Article number or Message-ID: "
7768      (mapcar (lambda (number) (list (int-to-string number)))
7769              gnus-newsgroup-limit))
7770     current-prefix-arg
7771     t))
7772   (prog1
7773       (if (and (stringp article)
7774                (string-match "@\\|%40" article))
7775           (gnus-summary-refer-article article)
7776         (when (stringp article)
7777           (setq article (string-to-number article)))
7778         (if (gnus-summary-goto-subject article force)
7779             (gnus-summary-display-article article all-headers)
7780           (gnus-message 4 "Couldn't go to article %s" article) nil))
7781     (gnus-summary-position-point)))
7782
7783 (defun gnus-summary-goto-last-article ()
7784   "Go to the previously read article."
7785   (interactive)
7786   (prog1
7787       (when gnus-last-article
7788         (gnus-summary-goto-article gnus-last-article nil t))
7789     (gnus-summary-position-point)))
7790
7791 (defun gnus-summary-pop-article (number)
7792   "Pop one article off the history and go to the previous.
7793 NUMBER articles will be popped off."
7794   (interactive "p")
7795   (let (to)
7796     (setq gnus-newsgroup-history
7797           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7798     (if to
7799         (gnus-summary-goto-article (car to) nil t)
7800       (error "Article history empty")))
7801   (gnus-summary-position-point))
7802
7803 ;; Summary commands and functions for limiting the summary buffer.
7804
7805 (defun gnus-summary-limit-to-articles (n)
7806   "Limit the summary buffer to the next N articles.
7807 If not given a prefix, use the process marked articles instead."
7808   (interactive "P")
7809   (prog1
7810       (let ((articles (gnus-summary-work-articles n)))
7811         (setq gnus-newsgroup-processable nil)
7812         (gnus-summary-limit articles))
7813     (gnus-summary-position-point)))
7814
7815 (defun gnus-summary-pop-limit (&optional total)
7816   "Restore the previous limit.
7817 If given a prefix, remove all limits."
7818   (interactive "P")
7819   (when total
7820     (setq gnus-newsgroup-limits
7821           (list (mapcar (lambda (h) (mail-header-number h))
7822                         gnus-newsgroup-headers))))
7823   (unless gnus-newsgroup-limits
7824     (error "No limit to pop"))
7825   (prog1
7826       (gnus-summary-limit nil 'pop)
7827     (gnus-summary-position-point)))
7828
7829 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7830   "Limit the summary buffer to articles that have subjects that match a regexp.
7831 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7832   (interactive
7833    (list (read-string (if current-prefix-arg
7834                           "Exclude subject (regexp): "
7835                         "Limit to subject (regexp): "))
7836          nil current-prefix-arg))
7837   (unless header
7838     (setq header "subject"))
7839   (when (not (equal "" subject))
7840     (prog1
7841         (let ((articles (gnus-summary-find-matching
7842                          (or header "subject") subject 'all nil nil
7843                          not-matching)))
7844           (unless articles
7845             (error "Found no matches for \"%s\"" subject))
7846           (gnus-summary-limit articles))
7847       (gnus-summary-position-point))))
7848
7849 (defun gnus-summary-limit-to-author (from &optional not-matching)
7850   "Limit the summary buffer to articles that have authors that match a regexp.
7851 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7852   (interactive
7853    (list (read-string (if current-prefix-arg
7854                           "Exclude author (regexp): "
7855                         "Limit to author (regexp): "))
7856          current-prefix-arg))
7857   (gnus-summary-limit-to-subject from "from" not-matching))
7858
7859 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7860   "Limit the summary buffer to articles with the given RECIPIENT.
7861
7862 If NOT-MATCHING, exclude RECIPIENT.
7863
7864 To and Cc headers are checked.  You need to include them in
7865 `nnmail-extra-headers'."
7866   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7867   (interactive
7868    (list (read-string (format "%s recipient (regexp): "
7869                               (if current-prefix-arg "Exclude" "Limit to")))
7870          current-prefix-arg))
7871   (when (not (equal "" recipient))
7872     (prog1 (let* ((to
7873                    (if (memq 'To nnmail-extra-headers)
7874                        (gnus-summary-find-matching
7875                         (cons 'extra 'To) recipient 'all nil nil
7876                         not-matching)
7877                      (gnus-message
7878                       1 "`To' isn't present in `nnmail-extra-headers'")
7879                      (sit-for 1)
7880                      nil))
7881                   (cc
7882                    (if (memq 'Cc nnmail-extra-headers)
7883                        (gnus-summary-find-matching
7884                         (cons 'extra 'Cc) recipient 'all nil nil
7885                         not-matching)
7886                      (gnus-message
7887                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7888                      (sit-for 1)
7889                      nil))
7890                   (articles
7891                    (if not-matching
7892                        ;; We need the numbers that are in both lists:
7893                        (mapcar (lambda (a)
7894                                  (and (memq a to) a))
7895                                cc)
7896                      (nconc to cc))))
7897              (unless articles
7898                (error "Found no matches for \"%s\"" recipient))
7899              (gnus-summary-limit articles))
7900       (gnus-summary-position-point))))
7901
7902 (defun gnus-summary-limit-to-age (age &optional younger-p)
7903   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7904 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7905 articles that are younger than AGE days."
7906   (interactive
7907    (let ((younger current-prefix-arg)
7908          (days-got nil)
7909          days)
7910      (while (not days-got)
7911        (setq days (if younger
7912                       (read-string "Limit to articles younger than (in days, older when negative): ")
7913                     (read-string
7914                      "Limit to articles older than (in days, younger when negative): ")))
7915        (when (> (length days) 0)
7916          (setq days (read days)))
7917        (if (numberp days)
7918            (progn
7919              (setq days-got t)
7920              (when (< days 0)
7921                (setq younger (not younger))
7922                (setq days (* days -1))))
7923          (message "Please enter a number.")
7924          (sleep-for 1)))
7925      (list days younger)))
7926   (prog1
7927       (let ((data gnus-newsgroup-data)
7928             (cutoff (days-to-time age))
7929             articles d date is-younger)
7930         (while (setq d (pop data))
7931           (when (and (vectorp (gnus-data-header d))
7932                      (setq date (mail-header-date (gnus-data-header d))))
7933             (setq is-younger (time-less-p
7934                               (time-since (condition-case ()
7935                                               (date-to-time date)
7936                                             (error '(0 0))))
7937                               cutoff))
7938             (when (if younger-p
7939                       is-younger
7940                     (not is-younger))
7941               (push (gnus-data-number d) articles))))
7942         (gnus-summary-limit (nreverse articles)))
7943     (gnus-summary-position-point)))
7944
7945 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7946   "Limit the summary buffer to articles that match an 'extra' header."
7947   (interactive
7948    (let ((header
7949           (intern
7950            (gnus-completing-read-with-default
7951             (symbol-name (car gnus-extra-headers))
7952             (if current-prefix-arg
7953                 "Exclude extra header"
7954               "Limit extra header")
7955             (mapcar (lambda (x)
7956                       (cons (symbol-name x) x))
7957                     gnus-extra-headers)
7958             nil
7959             t))))
7960      (list header
7961            (read-string (format "%s header %s (regexp): "
7962                                 (if current-prefix-arg "Exclude" "Limit to")
7963                                 header))
7964            current-prefix-arg)))
7965   (when (not (equal "" regexp))
7966     (prog1
7967         (let ((articles (gnus-summary-find-matching
7968                          (cons 'extra header) regexp 'all nil nil
7969                          not-matching)))
7970           (unless articles
7971             (error "Found no matches for \"%s\"" regexp))
7972           (gnus-summary-limit articles))
7973       (gnus-summary-position-point))))
7974
7975 (defun gnus-summary-limit-to-display-predicate ()
7976   "Limit the summary buffer to the predicated in the `display' group parameter."
7977   (interactive)
7978   (unless gnus-newsgroup-display
7979     (error "There is no `display' group parameter"))
7980   (let (articles)
7981     (dolist (number gnus-newsgroup-articles)
7982       (when (funcall gnus-newsgroup-display)
7983         (push number articles)))
7984     (gnus-summary-limit articles))
7985   (gnus-summary-position-point))
7986
7987 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7988 (make-obsolete
7989  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7990
7991 (defun gnus-summary-limit-to-unread (&optional all)
7992   "Limit the summary buffer to articles that are not marked as read.
7993 If ALL is non-nil, limit strictly to unread articles."
7994   (interactive "P")
7995   (if all
7996       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7997     (gnus-summary-limit-to-marks
7998      ;; Concat all the marks that say that an article is read and have
7999      ;; those removed.
8000      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8001            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8002            gnus-low-score-mark gnus-expirable-mark
8003            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8004            gnus-duplicate-mark gnus-souped-mark)
8005      'reverse)))
8006
8007 (defun gnus-summary-limit-to-bodies (match &optional reverse)
8008   "Limit the summary buffer to articles that have bodies that match MATCH.
8009 If REVERSE (the prefix), limit to articles that don't match."
8010   (interactive "sMatch body (regexp): \nP")
8011   (let ((articles nil)
8012         (gnus-select-article-hook nil)  ;Disable hook.
8013         (gnus-article-prepare-hook nil)
8014         (gnus-use-article-prefetch nil)
8015         (gnus-keep-backlog nil)
8016         (gnus-break-pages nil)
8017         (gnus-summary-display-arrow nil)
8018         (gnus-updated-mode-lines nil)
8019         (gnus-auto-center-summary nil)
8020         (gnus-display-mime-function nil))
8021     (dolist (data gnus-newsgroup-data)
8022       (let (gnus-mark-article-hook)
8023         (gnus-summary-select-article t t nil (gnus-data-number data)))
8024       (save-excursion
8025         (set-buffer gnus-article-buffer)
8026         (article-goto-body)
8027         (let* ((case-fold-search t)
8028                (found (re-search-forward match nil t)))
8029           (when (or (and found
8030                          (not reverse))
8031                     (and (not found)
8032                          reverse))
8033             (push (gnus-data-number data) articles)))))
8034     (if (not articles)
8035         (message "No messages matched")
8036       (gnus-summary-limit articles)))
8037   (gnus-summary-position-point))
8038
8039 (defun gnus-summary-limit-to-replied (&optional unreplied)
8040   "Limit the summary buffer to replied articles.
8041 If UNREPLIED (the prefix), limit to unreplied articles."
8042   (interactive "P")
8043   (if unreplied
8044       (gnus-summary-limit
8045        (gnus-set-difference gnus-newsgroup-articles
8046         gnus-newsgroup-replied))
8047     (gnus-summary-limit gnus-newsgroup-replied))
8048   (gnus-summary-position-point))
8049
8050 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8051 (make-obsolete 'gnus-summary-delete-marked-with
8052                'gnus-summary-limit-exclude-marks)
8053
8054 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8055   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8056 If REVERSE, limit the summary buffer to articles that are marked
8057 with MARKS.  MARKS can either be a string of marks or a list of marks.
8058 Returns how many articles were removed."
8059   (interactive "sMarks: ")
8060   (gnus-summary-limit-to-marks marks t))
8061
8062 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8063   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8064 If REVERSE (the prefix), limit the summary buffer to articles that are
8065 not marked with MARKS.  MARKS can either be a string of marks or a
8066 list of marks.
8067 Returns how many articles were removed."
8068   (interactive "sMarks: \nP")
8069   (prog1
8070       (let ((data gnus-newsgroup-data)
8071             (marks (if (listp marks) marks
8072                      (append marks nil))) ; Transform to list.
8073             articles)
8074         (while data
8075           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8076                   (memq (gnus-data-mark (car data)) marks))
8077             (push (gnus-data-number (car data)) articles))
8078           (setq data (cdr data)))
8079         (gnus-summary-limit articles))
8080     (gnus-summary-position-point)))
8081
8082 (defun gnus-summary-limit-to-score (score)
8083   "Limit to articles with score at or above SCORE."
8084   (interactive "NLimit to articles with score of at least: ")
8085   (let ((data gnus-newsgroup-data)
8086         articles)
8087     (while data
8088       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8089                 score)
8090         (push (gnus-data-number (car data)) articles))
8091       (setq data (cdr data)))
8092     (prog1
8093         (gnus-summary-limit articles)
8094       (gnus-summary-position-point))))
8095
8096 (defun gnus-summary-limit-to-unseen ()
8097   "Limit to unseen articles."
8098   (interactive)
8099   (prog1
8100       (gnus-summary-limit gnus-newsgroup-unseen)
8101     (gnus-summary-position-point)))
8102
8103 (defun gnus-summary-limit-include-thread (id)
8104   "Display all the hidden articles that is in the thread with ID in it.
8105 When called interactively, ID is the Message-ID of the current
8106 article."
8107   (interactive (list (mail-header-id (gnus-summary-article-header))))
8108   (let ((articles (gnus-articles-in-thread
8109                    (gnus-id-to-thread (gnus-root-id id)))))
8110     (prog1
8111         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8112       (gnus-summary-limit-include-matching-articles
8113        "subject"
8114        (regexp-quote (gnus-simplify-subject-re
8115                       (mail-header-subject (gnus-id-to-header id)))))
8116       (gnus-summary-position-point))))
8117
8118 (defun gnus-summary-limit-include-matching-articles (header regexp)
8119   "Display all the hidden articles that have HEADERs that match REGEXP."
8120   (interactive (list (read-string "Match on header: ")
8121                      (read-string "Regexp: ")))
8122   (let ((articles (gnus-find-matching-articles header regexp)))
8123     (prog1
8124         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8125       (gnus-summary-position-point))))
8126
8127 (defun gnus-summary-insert-dormant-articles ()
8128   "Insert all the dormant articles for this group into the current buffer."
8129   (interactive)
8130   (let ((gnus-verbose (max 6 gnus-verbose)))
8131     (if (not gnus-newsgroup-dormant)
8132         (gnus-message 3 "No cached articles for this group")
8133       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8134
8135 (defun gnus-summary-limit-include-dormant ()
8136   "Display all the hidden articles that are marked as dormant.
8137 Note that this command only works on a subset of the articles currently
8138 fetched for this group."
8139   (interactive)
8140   (unless gnus-newsgroup-dormant
8141     (error "There are no dormant articles in this group"))
8142   (prog1
8143       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8144     (gnus-summary-position-point)))
8145
8146 (defun gnus-summary-limit-exclude-dormant ()
8147   "Hide all dormant articles."
8148   (interactive)
8149   (prog1
8150       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8151     (gnus-summary-position-point)))
8152
8153 (defun gnus-summary-limit-exclude-childless-dormant ()
8154   "Hide all dormant articles that have no children."
8155   (interactive)
8156   (let ((data (gnus-data-list t))
8157         articles d children)
8158     ;; Find all articles that are either not dormant or have
8159     ;; children.
8160     (while (setq d (pop data))
8161       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8162                 (and (setq children
8163                            (gnus-article-children (gnus-data-number d)))
8164                      (let (found)
8165                        (while children
8166                          (when (memq (car children) articles)
8167                            (setq children nil
8168                                  found t))
8169                          (pop children))
8170                        found)))
8171         (push (gnus-data-number d) articles)))
8172     ;; Do the limiting.
8173     (prog1
8174         (gnus-summary-limit articles)
8175       (gnus-summary-position-point))))
8176
8177 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8178   "Mark all unread excluded articles as read.
8179 If ALL, mark even excluded ticked and dormants as read."
8180   (interactive "P")
8181   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8182   (let ((articles (gnus-sorted-ndifference
8183                    (sort
8184                     (mapcar (lambda (h) (mail-header-number h))
8185                             gnus-newsgroup-headers)
8186                     '<)
8187                    gnus-newsgroup-limit))
8188         article)
8189     (setq gnus-newsgroup-unreads
8190           (gnus-sorted-intersection gnus-newsgroup-unreads
8191                                     gnus-newsgroup-limit))
8192     (if all
8193         (setq gnus-newsgroup-dormant nil
8194               gnus-newsgroup-marked nil
8195               gnus-newsgroup-reads
8196               (nconc
8197                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8198                gnus-newsgroup-reads))
8199       (while (setq article (pop articles))
8200         (unless (or (memq article gnus-newsgroup-dormant)
8201                     (memq article gnus-newsgroup-marked))
8202           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8203
8204 (defun gnus-summary-limit (articles &optional pop)
8205   (if pop
8206       ;; We pop the previous limit off the stack and use that.
8207       (setq articles (car gnus-newsgroup-limits)
8208             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8209     ;; We use the new limit, so we push the old limit on the stack.
8210     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8211   ;; Set the limit.
8212   (setq gnus-newsgroup-limit articles)
8213   (let ((total (length gnus-newsgroup-data))
8214         (data (gnus-data-find-list (gnus-summary-article-number)))
8215         (gnus-summary-mark-below nil)   ; Inhibit this.
8216         found)
8217     ;; This will do all the work of generating the new summary buffer
8218     ;; according to the new limit.
8219     (gnus-summary-prepare)
8220     ;; Hide any threads, possibly.
8221     (gnus-summary-maybe-hide-threads)
8222     ;; Try to return to the article you were at, or one in the
8223     ;; neighborhood.
8224     (when data
8225       ;; We try to find some article after the current one.
8226       (while data
8227         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8228           (setq data nil
8229                 found t))
8230         (setq data (cdr data))))
8231     (unless found
8232       ;; If there is no data, that means that we were after the last
8233       ;; article.  The same goes when we can't find any articles
8234       ;; after the current one.
8235       (goto-char (point-max))
8236       (gnus-summary-find-prev))
8237     (gnus-set-mode-line 'summary)
8238     ;; We return how many articles were removed from the summary
8239     ;; buffer as a result of the new limit.
8240     (- total (length gnus-newsgroup-data))))
8241
8242 (defsubst gnus-invisible-cut-children (threads)
8243   (let ((num 0))
8244     (while threads
8245       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8246         (incf num))
8247       (pop threads))
8248     (< num 2)))
8249
8250 (defsubst gnus-cut-thread (thread)
8251   "Go forwards in the thread until we find an article that we want to display."
8252   (when (or (eq gnus-fetch-old-headers 'some)
8253             (eq gnus-fetch-old-headers 'invisible)
8254             (numberp gnus-fetch-old-headers)
8255             (eq gnus-build-sparse-threads 'some)
8256             (eq gnus-build-sparse-threads 'more))
8257     ;; Deal with old-fetched headers and sparse threads.
8258     (while (and
8259             thread
8260             (or
8261              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8262              (gnus-summary-article-ancient-p
8263               (mail-header-number (car thread))))
8264             (if (or (<= (length (cdr thread)) 1)
8265                     (eq gnus-fetch-old-headers 'invisible))
8266                 (setq gnus-newsgroup-limit
8267                       (delq (mail-header-number (car thread))
8268                             gnus-newsgroup-limit)
8269                       thread (cadr thread))
8270               (when (gnus-invisible-cut-children (cdr thread))
8271                 (let ((th (cdr thread)))
8272                   (while th
8273                     (if (memq (mail-header-number (caar th))
8274                               gnus-newsgroup-limit)
8275                         (setq thread (car th)
8276                               th nil)
8277                       (setq th (cdr th))))))))))
8278   thread)
8279
8280 (defun gnus-cut-threads (threads)
8281   "Cut off all uninteresting articles from the beginning of THREADS."
8282   (when (or (eq gnus-fetch-old-headers 'some)
8283             (eq gnus-fetch-old-headers 'invisible)
8284             (numberp gnus-fetch-old-headers)
8285             (eq gnus-build-sparse-threads 'some)
8286             (eq gnus-build-sparse-threads 'more))
8287     (let ((th threads))
8288       (while th
8289         (setcar th (gnus-cut-thread (car th)))
8290         (setq th (cdr th)))))
8291   ;; Remove nixed out threads.
8292   (delq nil threads))
8293
8294 (defun gnus-summary-initial-limit (&optional show-if-empty)
8295   "Figure out what the initial limit is supposed to be on group entry.
8296 This entails weeding out unwanted dormants, low-scored articles,
8297 fetch-old-headers verbiage, and so on."
8298   ;; Most groups have nothing to remove.
8299   (if (or gnus-inhibit-limiting
8300           (and (null gnus-newsgroup-dormant)
8301                (eq gnus-newsgroup-display 'gnus-not-ignore)
8302                (not (eq gnus-fetch-old-headers 'some))
8303                (not (numberp gnus-fetch-old-headers))
8304                (not (eq gnus-fetch-old-headers 'invisible))
8305                (null gnus-summary-expunge-below)
8306                (not (eq gnus-build-sparse-threads 'some))
8307                (not (eq gnus-build-sparse-threads 'more))
8308                (null gnus-thread-expunge-below)
8309                (not gnus-use-nocem)))
8310       ()                                ; Do nothing.
8311     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8312     (setq gnus-newsgroup-limit nil)
8313     (mapatoms
8314      (lambda (node)
8315        (unless (car (symbol-value node))
8316          ;; These threads have no parents -- they are roots.
8317          (let ((nodes (cdr (symbol-value node)))
8318                thread)
8319            (while nodes
8320              (if (and gnus-thread-expunge-below
8321                       (< (gnus-thread-total-score (car nodes))
8322                          gnus-thread-expunge-below))
8323                  (gnus-expunge-thread (pop nodes))
8324                (setq thread (pop nodes))
8325                (gnus-summary-limit-children thread))))))
8326      gnus-newsgroup-dependencies)
8327     ;; If this limitation resulted in an empty group, we might
8328     ;; pop the previous limit and use it instead.
8329     (when (and (not gnus-newsgroup-limit)
8330                show-if-empty)
8331       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8332     gnus-newsgroup-limit))
8333
8334 (defun gnus-summary-limit-children (thread)
8335   "Return 1 if this subthread is visible and 0 if it is not."
8336   ;; First we get the number of visible children to this thread.  This
8337   ;; is done by recursing down the thread using this function, so this
8338   ;; will really go down to a leaf article first, before slowly
8339   ;; working its way up towards the root.
8340   (when thread
8341     (let* ((max-lisp-eval-depth 5000)
8342            (children
8343            (if (cdr thread)
8344                (apply '+ (mapcar 'gnus-summary-limit-children
8345                                  (cdr thread)))
8346              0))
8347           (number (mail-header-number (car thread)))
8348           score)
8349       (if (and
8350            (not (memq number gnus-newsgroup-marked))
8351            (or
8352             ;; If this article is dormant and has absolutely no visible
8353             ;; children, then this article isn't visible.
8354             (and (memq number gnus-newsgroup-dormant)
8355                  (zerop children))
8356             ;; If this is "fetch-old-headered" and there is no
8357             ;; visible children, then we don't want this article.
8358             (and (or (eq gnus-fetch-old-headers 'some)
8359                      (numberp gnus-fetch-old-headers))
8360                  (gnus-summary-article-ancient-p number)
8361                  (zerop children))
8362             ;; If this is "fetch-old-headered" and `invisible', then
8363             ;; we don't want this article.
8364             (and (eq gnus-fetch-old-headers 'invisible)
8365                  (gnus-summary-article-ancient-p number))
8366             ;; If this is a sparsely inserted article with no children,
8367             ;; we don't want it.
8368             (and (eq gnus-build-sparse-threads 'some)
8369                  (gnus-summary-article-sparse-p number)
8370                  (zerop children))
8371             ;; If we use expunging, and this article is really
8372             ;; low-scored, then we don't want this article.
8373             (when (and gnus-summary-expunge-below
8374                        (< (setq score
8375                                 (or (cdr (assq number gnus-newsgroup-scored))
8376                                     gnus-summary-default-score))
8377                           gnus-summary-expunge-below))
8378               ;; We increase the expunge-tally here, but that has
8379               ;; nothing to do with the limits, really.
8380               (incf gnus-newsgroup-expunged-tally)
8381               ;; We also mark as read here, if that's wanted.
8382               (when (and gnus-summary-mark-below
8383                          (< score gnus-summary-mark-below))
8384                 (setq gnus-newsgroup-unreads
8385                       (delq number gnus-newsgroup-unreads))
8386                 (if gnus-newsgroup-auto-expire
8387                     (push number gnus-newsgroup-expirable)
8388                   (push (cons number gnus-low-score-mark)
8389                         gnus-newsgroup-reads)))
8390               t)
8391             ;; Do the `display' group parameter.
8392             (and gnus-newsgroup-display
8393                  (not (funcall gnus-newsgroup-display)))
8394             ;; Check NoCeM things.
8395             (when (and gnus-use-nocem
8396                        (gnus-nocem-unwanted-article-p
8397                         (mail-header-id (car thread))))
8398               (setq gnus-newsgroup-unreads
8399                     (delq number gnus-newsgroup-unreads))
8400               t)))
8401           ;; Nope, invisible article.
8402           0
8403         ;; Ok, this article is to be visible, so we add it to the limit
8404         ;; and return 1.
8405         (push number gnus-newsgroup-limit)
8406         1))))
8407
8408 (defun gnus-expunge-thread (thread)
8409   "Mark all articles in THREAD as read."
8410   (let* ((number (mail-header-number (car thread))))
8411     (incf gnus-newsgroup-expunged-tally)
8412     ;; We also mark as read here, if that's wanted.
8413     (setq gnus-newsgroup-unreads
8414           (delq number gnus-newsgroup-unreads))
8415     (if gnus-newsgroup-auto-expire
8416         (push number gnus-newsgroup-expirable)
8417       (push (cons number gnus-low-score-mark)
8418             gnus-newsgroup-reads)))
8419   ;; Go recursively through all subthreads.
8420   (mapcar 'gnus-expunge-thread (cdr thread)))
8421
8422 ;; Summary article oriented commands
8423
8424 (defun gnus-summary-refer-parent-article (n)
8425   "Refer parent article N times.
8426 If N is negative, go to ancestor -N instead.
8427 The difference between N and the number of articles fetched is returned."
8428   (interactive "p")
8429   (let ((skip 1)
8430         error header ref)
8431     (when (not (natnump n))
8432       (setq skip (abs n)
8433             n 1))
8434     (while (and (> n 0)
8435                 (not error))
8436       (setq header (gnus-summary-article-header))
8437       (if (and (eq (mail-header-number header)
8438                    (cdr gnus-article-current))
8439                (equal gnus-newsgroup-name
8440                       (car gnus-article-current)))
8441           ;; If we try to find the parent of the currently
8442           ;; displayed article, then we take a look at the actual
8443           ;; References header, since this is slightly more
8444           ;; reliable than the References field we got from the
8445           ;; server.
8446           (save-excursion
8447             (set-buffer gnus-original-article-buffer)
8448             (nnheader-narrow-to-headers)
8449             (unless (setq ref (message-fetch-field "references"))
8450               (when (setq ref (message-fetch-field "in-reply-to"))
8451                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8452             (widen))
8453         (setq ref
8454               ;; It's not the current article, so we take a bet on
8455               ;; the value we got from the server.
8456               (mail-header-references header)))
8457       (if (and ref
8458                (not (equal ref "")))
8459           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8460             (gnus-message 1 "Couldn't find parent"))
8461         (gnus-message 1 "No references in article %d"
8462                       (gnus-summary-article-number))
8463         (setq error t))
8464       (decf n))
8465     (gnus-summary-position-point)
8466     n))
8467
8468 (defun gnus-summary-refer-references ()
8469   "Fetch all articles mentioned in the References header.
8470 Return the number of articles fetched."
8471   (interactive)
8472   (let ((ref (mail-header-references (gnus-summary-article-header)))
8473         (current (gnus-summary-article-number))
8474         (n 0))
8475     (if (or (not ref)
8476             (equal ref ""))
8477         (error "No References in the current article")
8478       ;; For each Message-ID in the References header...
8479       (while (string-match "<[^>]*>" ref)
8480         (incf n)
8481         ;; ... fetch that article.
8482         (gnus-summary-refer-article
8483          (prog1 (match-string 0 ref)
8484            (setq ref (substring ref (match-end 0))))))
8485       (gnus-summary-goto-subject current)
8486       (gnus-summary-position-point)
8487       n)))
8488
8489 (defun gnus-summary-refer-thread (&optional limit)
8490   "Fetch all articles in the current thread.
8491 If LIMIT (the numerical prefix), fetch that many old headers instead
8492 of what's specified by the `gnus-refer-thread-limit' variable."
8493   (interactive "P")
8494   (let ((id (mail-header-id (gnus-summary-article-header)))
8495         (limit (if limit (prefix-numeric-value limit)
8496                  gnus-refer-thread-limit)))
8497     (unless (eq gnus-fetch-old-headers 'invisible)
8498       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8499       ;; Retrieve the headers and read them in.
8500       (if (eq (if (numberp limit)
8501                   (gnus-retrieve-headers
8502                    (list (min
8503                           (+ (mail-header-number
8504                               (gnus-summary-article-header))
8505                              limit)
8506                           gnus-newsgroup-end))
8507                    gnus-newsgroup-name (* limit 2))
8508                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8509                 ;; headers.
8510                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8511                                        gnus-newsgroup-name limit))
8512               'nov)
8513           (gnus-build-all-threads)
8514         (error "Can't fetch thread from back ends that don't support NOV"))
8515       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8516     (gnus-summary-limit-include-thread id)))
8517
8518 (defun gnus-summary-refer-article (message-id)
8519   "Fetch an article specified by MESSAGE-ID."
8520   (interactive "sMessage-ID: ")
8521   (when (and (stringp message-id)
8522              (not (zerop (length message-id))))
8523     (setq message-id (gnus-replace-in-string message-id " " ""))
8524     ;; Construct the correct Message-ID if necessary.
8525     ;; Suggested by tale@pawl.rpi.edu.
8526     (unless (string-match "^<" message-id)
8527       (setq message-id (concat "<" message-id)))
8528     (unless (string-match ">$" message-id)
8529       (setq message-id (concat message-id ">")))
8530     ;; People often post MIDs from URLs, so unhex it:
8531     (unless (string-match "@" message-id)
8532       (setq message-id (gnus-url-unhex-string message-id)))
8533     (let* ((header (gnus-id-to-header message-id))
8534            (sparse (and header
8535                         (gnus-summary-article-sparse-p
8536                          (mail-header-number header))
8537                         (memq (mail-header-number header)
8538                               gnus-newsgroup-limit)))
8539            number)
8540       (cond
8541        ;; If the article is present in the buffer we just go to it.
8542        ((and header
8543              (or (not (gnus-summary-article-sparse-p
8544                        (mail-header-number header)))
8545                  sparse))
8546         (prog1
8547             (gnus-summary-goto-article
8548              (mail-header-number header) nil t)
8549           (when sparse
8550             (gnus-summary-update-article (mail-header-number header)))))
8551        (t
8552         ;; We fetch the article.
8553         (catch 'found
8554           (dolist (gnus-override-method (gnus-refer-article-methods))
8555             (when (and (gnus-check-server gnus-override-method)
8556                        ;; Fetch the header,
8557                        (setq number (gnus-summary-insert-subject message-id)))
8558               ;; and display the article.
8559               (gnus-summary-select-article nil nil nil number)
8560               (throw 'found t)))
8561           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8562
8563 (defun gnus-refer-article-methods ()
8564   "Return a list of referable methods."
8565   (cond
8566    ;; No method, so we default to current and native.
8567    ((null gnus-refer-article-method)
8568     (list gnus-current-select-method gnus-select-method))
8569    ;; Current.
8570    ((eq 'current gnus-refer-article-method)
8571     (list gnus-current-select-method))
8572    ;; List of select methods.
8573    ((not (and (symbolp (car gnus-refer-article-method))
8574               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8575     (let (out)
8576       (dolist (method gnus-refer-article-method)
8577         (push (if (eq 'current method)
8578                   gnus-current-select-method
8579                 method)
8580               out))
8581       (nreverse out)))
8582    ;; One single select method.
8583    (t
8584     (list gnus-refer-article-method))))
8585
8586 (defun gnus-summary-edit-parameters ()
8587   "Edit the group parameters of the current group."
8588   (interactive)
8589   (gnus-group-edit-group gnus-newsgroup-name 'params))
8590
8591 (defun gnus-summary-customize-parameters ()
8592   "Customize the group parameters of the current group."
8593   (interactive)
8594   (gnus-group-customize gnus-newsgroup-name))
8595
8596 (defun gnus-summary-enter-digest-group (&optional force)
8597   "Enter an nndoc group based on the current article.
8598 If FORCE, force a digest interpretation.  If not, try
8599 to guess what the document format is."
8600   (interactive "P")
8601   (let ((conf gnus-current-window-configuration))
8602     (save-window-excursion
8603       (save-excursion
8604         (let (gnus-article-prepare-hook
8605               gnus-display-mime-function
8606               gnus-break-pages)
8607           (gnus-summary-select-article))))
8608     (setq gnus-current-window-configuration conf)
8609     (let* ((name (format "%s-%d"
8610                          (gnus-group-prefixed-name
8611                           gnus-newsgroup-name (list 'nndoc ""))
8612                          (with-current-buffer gnus-summary-buffer
8613                            gnus-current-article)))
8614            (ogroup gnus-newsgroup-name)
8615            (params (append (gnus-info-params (gnus-get-info ogroup))
8616                            (list (cons 'to-group ogroup))
8617                            (list (cons 'parent-group ogroup))
8618                            (list (cons 'save-article-group ogroup))))
8619            (case-fold-search t)
8620            (buf (current-buffer))
8621            dig to-address)
8622       (save-excursion
8623         (set-buffer gnus-original-article-buffer)
8624         ;; Have the digest group inherit the main mail address of
8625         ;; the parent article.
8626         (when (setq to-address (or (gnus-fetch-field "reply-to")
8627                                    (gnus-fetch-field "from")))
8628           (setq params (append
8629                         (list (cons 'to-address
8630                                     (funcall gnus-decode-encoded-word-function
8631                                              to-address))))))
8632         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8633         (insert-buffer-substring gnus-original-article-buffer)
8634         ;; Remove lines that may lead nndoc to misinterpret the
8635         ;; document type.
8636         (narrow-to-region
8637          (goto-char (point-min))
8638          (or (search-forward "\n\n" nil t) (point)))
8639         (goto-char (point-min))
8640         (delete-matching-lines "^Path:\\|^From ")
8641         (widen))
8642       (unwind-protect
8643           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8644                     (gnus-newsgroup-ephemeral-ignored-charsets
8645                      gnus-newsgroup-ignored-charsets))
8646                 (gnus-group-read-ephemeral-group
8647                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8648                               (nndoc-article-type
8649                                ,(if force 'mbox 'guess)))
8650                  t nil nil nil
8651                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8652                                                         "ADAPT")))))
8653               ;; Make all postings to this group go to the parent group.
8654               (nconc (gnus-info-params (gnus-get-info name))
8655                      params)
8656             ;; Couldn't select this doc group.
8657             (switch-to-buffer buf)
8658             (gnus-set-global-variables)
8659             (gnus-configure-windows 'summary)
8660             (gnus-message 3 "Article couldn't be entered?"))
8661         (kill-buffer dig)))))
8662
8663 (defun gnus-summary-read-document (n)
8664   "Open a new group based on the current article(s).
8665 This will allow you to read digests and other similar
8666 documents as newsgroups.
8667 Obeys the standard process/prefix convention."
8668   (interactive "P")
8669   (let* ((ogroup gnus-newsgroup-name)
8670          (params (append (gnus-info-params (gnus-get-info ogroup))
8671                          (list (cons 'to-group ogroup))))
8672          group egroup groups vgroup)
8673     (dolist (article (gnus-summary-work-articles n))
8674       (setq group (format "%s-%d" gnus-newsgroup-name article))
8675       (gnus-summary-remove-process-mark article)
8676       (when (gnus-summary-display-article article)
8677         (save-excursion
8678           (with-temp-buffer
8679             (insert-buffer-substring gnus-original-article-buffer)
8680             ;; Remove some headers that may lead nndoc to make
8681             ;; the wrong guess.
8682             (message-narrow-to-head)
8683             (goto-char (point-min))
8684             (delete-matching-lines "^Path:\\|^From ")
8685             (widen)
8686             (if (setq egroup
8687                       (gnus-group-read-ephemeral-group
8688                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8689                                      (nndoc-article-type guess))
8690                        t nil t))
8691                 (progn
8692             ;; Make all postings to this group go to the parent group.
8693                   (nconc (gnus-info-params (gnus-get-info egroup))
8694                          params)
8695                   (push egroup groups))
8696               ;; Couldn't select this doc group.
8697               (gnus-error 3 "Article couldn't be entered"))))))
8698     ;; Now we have selected all the documents.
8699     (cond
8700      ((not groups)
8701       (error "None of the articles could be interpreted as documents"))
8702      ((gnus-group-read-ephemeral-group
8703        (setq vgroup (format
8704                      "nnvirtual:%s-%s" gnus-newsgroup-name
8705                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8706        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8707        t
8708        (cons (current-buffer) 'summary)))
8709      (t
8710       (error "Couldn't select virtual nndoc group")))))
8711
8712 (defun gnus-summary-isearch-article (&optional regexp-p)
8713   "Do incremental search forward on the current article.
8714 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8715   (interactive "P")
8716   (gnus-summary-select-article)
8717   (gnus-configure-windows 'article)
8718   (gnus-eval-in-buffer-window gnus-article-buffer
8719     (save-restriction
8720       (widen)
8721       (isearch-forward regexp-p))))
8722
8723 (defun gnus-summary-repeat-search-article-forward ()
8724   "Repeat the previous search forwards."
8725   (interactive)
8726   (unless gnus-last-search-regexp
8727     (error "No previous search"))
8728   (gnus-summary-search-article-forward gnus-last-search-regexp))
8729
8730 (defun gnus-summary-repeat-search-article-backward ()
8731   "Repeat the previous search backwards."
8732   (interactive)
8733   (unless gnus-last-search-regexp
8734     (error "No previous search"))
8735   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8736
8737 (defun gnus-summary-search-article-forward (regexp &optional backward)
8738   "Search for an article containing REGEXP forward.
8739 If BACKWARD, search backward instead."
8740   (interactive
8741    (list (read-string
8742           (format "Search article %s (regexp%s): "
8743                   (if current-prefix-arg "backward" "forward")
8744                   (if gnus-last-search-regexp
8745                       (concat ", default " gnus-last-search-regexp)
8746                     "")))
8747          current-prefix-arg))
8748   (if (string-equal regexp "")
8749       (setq regexp (or gnus-last-search-regexp ""))
8750     (setq gnus-last-search-regexp regexp)
8751     (setq gnus-article-before-search gnus-current-article))
8752   ;; Intentionally set gnus-last-article.
8753   (setq gnus-last-article gnus-article-before-search)
8754   (let ((gnus-last-article gnus-last-article))
8755     (if (gnus-summary-search-article regexp backward)
8756         (gnus-summary-show-thread)
8757       (signal 'search-failed (list regexp)))))
8758
8759 (defun gnus-summary-search-article-backward (regexp)
8760   "Search for an article containing REGEXP backward."
8761   (interactive
8762    (list (read-string
8763           (format "Search article backward (regexp%s): "
8764                   (if gnus-last-search-regexp
8765                       (concat ", default " gnus-last-search-regexp)
8766                     "")))))
8767   (gnus-summary-search-article-forward regexp 'backward))
8768
8769 (defun gnus-summary-search-article (regexp &optional backward)
8770   "Search for an article containing REGEXP.
8771 Optional argument BACKWARD means do search for backward.
8772 `gnus-select-article-hook' is not called during the search."
8773   ;; We have to require this here to make sure that the following
8774   ;; dynamic binding isn't shadowed by autoloading.
8775   (require 'gnus-async)
8776   (require 'gnus-art)
8777   (let ((gnus-select-article-hook nil)  ;Disable hook.
8778         (gnus-article-prepare-hook nil)
8779         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8780         (gnus-use-article-prefetch nil)
8781         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8782         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8783         (gnus-visual nil)
8784         (gnus-keep-backlog nil)
8785         (gnus-break-pages nil)
8786         (gnus-summary-display-arrow nil)
8787         (gnus-updated-mode-lines nil)
8788         (gnus-auto-center-summary nil)
8789         (sum (current-buffer))
8790         (gnus-display-mime-function nil)
8791         (found nil)
8792         point)
8793     (gnus-save-hidden-threads
8794       (gnus-summary-select-article)
8795       (set-buffer gnus-article-buffer)
8796       (goto-char (window-point (get-buffer-window (current-buffer))))
8797       (when backward
8798         (forward-line -1))
8799       (while (not found)
8800         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8801         (if (if backward
8802                 (re-search-backward regexp nil t)
8803               (re-search-forward regexp nil t))
8804             ;; We found the regexp.
8805             (progn
8806               (setq found 'found)
8807               (beginning-of-line)
8808               (set-window-start
8809                (get-buffer-window (current-buffer))
8810                (point))
8811               (forward-line 1)
8812               (set-window-point
8813                (get-buffer-window (current-buffer))
8814                (point))
8815               (set-buffer sum)
8816               (setq point (point)))
8817           ;; We didn't find it, so we go to the next article.
8818           (set-buffer sum)
8819           (setq found 'not)
8820           (while (eq found 'not)
8821             (if (not (if backward (gnus-summary-find-prev)
8822                        (gnus-summary-find-next)))
8823                 ;; No more articles.
8824                 (setq found t)
8825               ;; Select the next article and adjust point.
8826               (unless (gnus-summary-article-sparse-p
8827                        (gnus-summary-article-number))
8828                 (setq found nil)
8829                 (gnus-summary-select-article)
8830                 (set-buffer gnus-article-buffer)
8831                 (widen)
8832                 (goto-char (if backward (point-max) (point-min))))))))
8833       (gnus-message 7 ""))
8834     ;; Return whether we found the regexp.
8835     (when (eq found 'found)
8836       (goto-char point)
8837       (gnus-summary-show-thread)
8838       (gnus-summary-goto-subject gnus-current-article)
8839       (gnus-summary-position-point)
8840       t)))
8841
8842 (defun gnus-find-matching-articles (header regexp)
8843   "Return a list of all articles that match REGEXP on HEADER.
8844 This search includes all articles in the current group that Gnus has
8845 fetched headers for, whether they are displayed or not."
8846   (let ((articles nil)
8847         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8848         (case-fold-search t))
8849     (dolist (header gnus-newsgroup-headers)
8850       (when (string-match regexp (funcall func header))
8851         (push (mail-header-number header) articles)))
8852     (nreverse articles)))
8853
8854 (defun gnus-summary-find-matching (header regexp &optional backward unread
8855                                           not-case-fold not-matching)
8856   "Return a list of all articles that match REGEXP on HEADER.
8857 The search stars on the current article and goes forwards unless
8858 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8859 If UNREAD is non-nil, only unread articles will
8860 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8861 in the comparisons. If NOT-MATCHING, return a list of all articles that
8862 not match REGEXP on HEADER."
8863   (let ((case-fold-search (not not-case-fold))
8864         articles d func)
8865     (if (consp header)
8866         (if (eq (car header) 'extra)
8867             (setq func
8868                   `(lambda (h)
8869                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8870                          "")))
8871           (error "%s is an invalid header" header))
8872       (unless (fboundp (intern (concat "mail-header-" header)))
8873         (error "%s is not a valid header" header))
8874       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8875     (dolist (d (if (eq backward 'all)
8876                    gnus-newsgroup-data
8877                  (gnus-data-find-list
8878                   (gnus-summary-article-number)
8879                   (gnus-data-list backward))))
8880       (when (and (or (not unread)       ; We want all articles...
8881                      (gnus-data-unread-p d)) ; Or just unreads.
8882                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8883                  (if not-matching
8884                      (not (string-match
8885                            regexp
8886                            (funcall func (gnus-data-header d))))
8887                    (string-match regexp
8888                                  (funcall func (gnus-data-header d)))))
8889         (push (gnus-data-number d) articles))) ; Success!
8890     (nreverse articles)))
8891
8892 (defun gnus-summary-execute-command (header regexp command &optional backward)
8893   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8894 If HEADER is an empty string (or nil), the match is done on the entire
8895 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8896   (interactive
8897    (list (let ((completion-ignore-case t))
8898            (completing-read
8899             "Header name: "
8900             (mapcar (lambda (header) (list (format "%s" header)))
8901                     (append
8902                      '("Number" "Subject" "From" "Lines" "Date"
8903                        "Message-ID" "Xref" "References" "Body")
8904                      gnus-extra-headers))
8905             nil 'require-match))
8906          (read-string "Regexp: ")
8907          (read-key-sequence "Command: ")
8908          current-prefix-arg))
8909   (when (equal header "Body")
8910     (setq header ""))
8911   ;; Hidden thread subtrees must be searched as well.
8912   (gnus-summary-show-all-threads)
8913   ;; We don't want to change current point nor window configuration.
8914   (save-excursion
8915     (save-window-excursion
8916       (let (gnus-visual
8917             gnus-treat-strip-trailing-blank-lines
8918             gnus-treat-strip-leading-blank-lines
8919             gnus-treat-strip-multiple-blank-lines
8920             gnus-treat-hide-boring-headers
8921             gnus-treat-fold-newsgroups
8922             gnus-article-prepare-hook)
8923         (gnus-message 6 "Executing %s..." (key-description command))
8924         ;; We'd like to execute COMMAND interactively so as to give arguments.
8925         (gnus-execute header regexp
8926                       `(call-interactively ',(key-binding command))
8927                       backward)
8928         (gnus-message 6 "Executing %s...done" (key-description command))))))
8929
8930 (defun gnus-summary-beginning-of-article ()
8931   "Scroll the article back to the beginning."
8932   (interactive)
8933   (gnus-summary-select-article)
8934   (gnus-configure-windows 'article)
8935   (gnus-eval-in-buffer-window gnus-article-buffer
8936     (widen)
8937     (goto-char (point-min))
8938     (when gnus-break-pages
8939       (gnus-narrow-to-page))))
8940
8941 (defun gnus-summary-end-of-article ()
8942   "Scroll to the end of the article."
8943   (interactive)
8944   (gnus-summary-select-article)
8945   (gnus-configure-windows 'article)
8946   (gnus-eval-in-buffer-window gnus-article-buffer
8947     (widen)
8948     (goto-char (point-max))
8949     (recenter -3)
8950     (when gnus-break-pages
8951       (when (re-search-backward page-delimiter nil t)
8952         (narrow-to-region (match-end 0) (point-max)))
8953       (gnus-narrow-to-page))))
8954
8955 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8956   "Truncate to LEN and quote all \"(\"'s in STRING."
8957   (gnus-replace-in-string (if (and len (> (length string) len))
8958                               (substring string 0 len)
8959                             string)
8960                           "[()]" "\\\\\\&"))
8961
8962 (defun gnus-summary-print-article (&optional filename n)
8963   "Generate and print a PostScript image of the process-marked (mail) articles.
8964
8965 If used interactively, print the current article if none are
8966 process-marked.  With prefix arg, prompt the user for the name of the
8967 file to save in.
8968
8969 When used from Lisp, accept two optional args FILENAME and N.  N means
8970 to print the next N articles.  If N is negative, print the N previous
8971 articles.  If N is nil and articles have been marked with the process
8972 mark, print these instead.
8973
8974 If the optional first argument FILENAME is nil, send the image to the
8975 printer.  If FILENAME is a string, save the PostScript image in a file with
8976 that name.  If FILENAME is a number, prompt the user for the name of the file
8977 to save in."
8978   (interactive (list (ps-print-preprint current-prefix-arg)))
8979   (dolist (article (gnus-summary-work-articles n))
8980     (gnus-summary-select-article nil nil 'pseudo article)
8981     (gnus-eval-in-buffer-window gnus-article-buffer
8982       (gnus-print-buffer))
8983     (gnus-summary-remove-process-mark article))
8984   (ps-despool filename))
8985
8986 (defun gnus-print-buffer ()
8987   (let ((buffer (generate-new-buffer " *print*")))
8988     (unwind-protect
8989         (progn
8990           (copy-to-buffer buffer (point-min) (point-max))
8991           (set-buffer buffer)
8992           (gnus-remove-text-with-property 'gnus-decoration)
8993           (when (gnus-visual-p 'article-highlight 'highlight)
8994             ;; Copy-to-buffer doesn't copy overlay.  So redo
8995             ;; highlight.
8996             (let ((gnus-article-buffer buffer))
8997               (gnus-article-highlight-citation t)
8998               (gnus-article-highlight-signature)
8999               (gnus-article-emphasize)
9000               (gnus-article-delete-invisible-text)))
9001           (let ((ps-left-header
9002                  (list
9003                   (concat "("
9004                           (gnus-summary-print-truncate-and-quote
9005                            (mail-header-subject gnus-current-headers)
9006                            66) ")")
9007                   (concat "("
9008                           (gnus-summary-print-truncate-and-quote
9009                            (mail-header-from gnus-current-headers)
9010                            45) ")")))
9011                 (ps-right-header
9012                  (list
9013                   "/pagenumberstring load"
9014                   (concat "("
9015                           (mail-header-date gnus-current-headers) ")"))))
9016             (gnus-run-hooks 'gnus-ps-print-hook)
9017             (save-excursion
9018               (if window-system
9019                   (ps-spool-buffer-with-faces)
9020                 (ps-spool-buffer)))))
9021       (kill-buffer buffer))))
9022
9023 (defun gnus-summary-show-article (&optional arg)
9024   "Force redisplaying of the current article.
9025 If ARG (the prefix) is a number, show the article with the charset
9026 defined in `gnus-summary-show-article-charset-alist', or the charset
9027 input.
9028 If ARG (the prefix) is non-nil and not a number, show the raw article
9029 without any article massaging functions being run.  Normally, the key
9030 strokes are `C-u g'."
9031   (interactive "P")
9032   (cond
9033    ((numberp arg)
9034     (gnus-summary-show-article t)
9035     (let ((gnus-newsgroup-charset
9036            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9037                (mm-read-coding-system
9038                 "View as charset: " ;; actually it is coding system.
9039                 (with-current-buffer gnus-article-buffer
9040                   (mm-detect-coding-region (point) (point-max))))))
9041           (gnus-newsgroup-ignored-charsets 'gnus-all))
9042       (gnus-summary-select-article nil 'force)
9043       (let ((deps gnus-newsgroup-dependencies)
9044             head header lines)
9045         (save-excursion
9046           (set-buffer gnus-original-article-buffer)
9047           (save-restriction
9048             (message-narrow-to-head)
9049             (setq head (buffer-string))
9050             (goto-char (point-min))
9051             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9052               (goto-char (point-max))
9053               (widen)
9054               (setq lines (1- (count-lines (point) (point-max))))))
9055           (with-temp-buffer
9056             (insert (format "211 %d Article retrieved.\n"
9057                             (cdr gnus-article-current)))
9058             (insert head)
9059             (if lines (insert (format "Lines: %d\n" lines)))
9060             (insert ".\n")
9061             (let ((nntp-server-buffer (current-buffer)))
9062               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9063         (gnus-data-set-header
9064          (gnus-data-find (cdr gnus-article-current))
9065          header)
9066         (gnus-summary-update-article-line
9067          (cdr gnus-article-current) header)
9068         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9069           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9070    ((not arg)
9071     ;; Select the article the normal way.
9072     (gnus-summary-select-article nil 'force))
9073    (t
9074     ;; We have to require this here to make sure that the following
9075     ;; dynamic binding isn't shadowed by autoloading.
9076     (require 'gnus-async)
9077     (require 'gnus-art)
9078     ;; Bind the article treatment functions to nil.
9079     (let ((gnus-have-all-headers t)
9080           gnus-article-prepare-hook
9081           gnus-article-decode-hook
9082           gnus-display-mime-function
9083           gnus-break-pages)
9084       ;; Destroy any MIME parts.
9085       (when (gnus-buffer-live-p gnus-article-buffer)
9086         (save-excursion
9087           (set-buffer gnus-article-buffer)
9088           (mm-destroy-parts gnus-article-mime-handles)
9089           ;; Set it to nil for safety reason.
9090           (setq gnus-article-mime-handle-alist nil)
9091           (setq gnus-article-mime-handles nil)))
9092       (gnus-summary-select-article nil 'force))))
9093   (gnus-summary-goto-subject gnus-current-article)
9094   (gnus-summary-position-point))
9095
9096 (defun gnus-summary-show-raw-article ()
9097   "Show the raw article without any article massaging functions being run."
9098   (interactive)
9099   (gnus-summary-show-article t))
9100
9101 (defun gnus-summary-verbose-headers (&optional arg)
9102   "Toggle permanent full header display.
9103 If ARG is a positive number, turn header display on.
9104 If ARG is a negative number, turn header display off."
9105   (interactive "P")
9106   (setq gnus-show-all-headers
9107         (cond ((or (not (numberp arg))
9108                    (zerop arg))
9109                (not gnus-show-all-headers))
9110               ((natnump arg)
9111                t)))
9112   (gnus-summary-show-article))
9113
9114 (defun gnus-summary-toggle-header (&optional arg)
9115   "Show the headers if they are hidden, or hide them if they are shown.
9116 If ARG is a positive number, show the entire header.
9117 If ARG is a negative number, hide the unwanted header lines."
9118   (interactive "P")
9119   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9120                      (get-buffer-window gnus-article-buffer t))))
9121     (with-current-buffer gnus-article-buffer
9122       (widen)
9123       (article-narrow-to-head)
9124       (let* ((buffer-read-only nil)
9125              (inhibit-point-motion-hooks t)
9126              (hidden (if (numberp arg)
9127                          (>= arg 0)
9128                        (or (not (looking-at "[^ \t\n]+:"))
9129                            (gnus-article-hidden-text-p 'headers))))
9130              s e)
9131         (delete-region (point-min) (point-max))
9132         (with-current-buffer gnus-original-article-buffer
9133           (goto-char (setq s (point-min)))
9134           (setq e (if (search-forward "\n\n" nil t)
9135                       (1- (point))
9136                     (point-max))))
9137         (insert-buffer-substring gnus-original-article-buffer s e)
9138         (run-hooks 'gnus-article-decode-hook)
9139         (if hidden
9140             (let ((gnus-treat-hide-headers nil)
9141                   (gnus-treat-hide-boring-headers nil))
9142               (gnus-delete-wash-type 'headers)
9143               (gnus-treat-article 'head))
9144           (gnus-treat-article 'head))
9145         (widen)
9146         (if window
9147             (set-window-start window (goto-char (point-min))))
9148         (if gnus-break-pages
9149             (gnus-narrow-to-page)
9150           (when (gnus-visual-p 'page-marker)
9151             (let ((buffer-read-only nil))
9152               (gnus-remove-text-with-property 'gnus-prev)
9153               (gnus-remove-text-with-property 'gnus-next))))
9154         (gnus-set-mode-line 'article)))))
9155
9156 (defun gnus-summary-show-all-headers ()
9157   "Make all header lines visible."
9158   (interactive)
9159   (gnus-summary-toggle-header 1))
9160
9161 (defun gnus-summary-caesar-message (&optional arg)
9162   "Caesar rotate the current article by 13.
9163 The numerical prefix specifies how many places to rotate each letter
9164 forward."
9165   (interactive "P")
9166   (gnus-summary-select-article)
9167   (let ((mail-header-separator ""))
9168     (gnus-eval-in-buffer-window gnus-article-buffer
9169       (save-restriction
9170         (widen)
9171         (let ((start (window-start))
9172               buffer-read-only)
9173           (message-caesar-buffer-body arg)
9174           (set-window-start (get-buffer-window (current-buffer)) start)))))
9175   ;; Create buttons and stuff...
9176   (gnus-treat-article nil))
9177
9178 (defun gnus-summary-idna-message (&optional arg)
9179   "Decode IDNA encoded domain names in the current articles.
9180 IDNA encoded domain names looks like `xn--bar'.  If a string
9181 remain unencoded after running this function, it is likely an
9182 invalid IDNA string (`xn--bar' is invalid).
9183
9184 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9185 installed for this command to work."
9186   (interactive "P")
9187   (if (not (and (condition-case nil (require 'idna)
9188                   (file-error))
9189                 (mm-coding-system-p 'utf-8)
9190                 (executable-find (symbol-value 'idna-program))))
9191       (gnus-message
9192        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9193     (gnus-summary-select-article)
9194     (let ((mail-header-separator ""))
9195       (gnus-eval-in-buffer-window gnus-article-buffer
9196         (save-restriction
9197           (widen)
9198           (let ((start (window-start))
9199                 buffer-read-only)
9200             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9201               (replace-match (idna-to-unicode (match-string 1))))
9202             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9203
9204 (defun gnus-summary-morse-message (&optional arg)
9205   "Morse decode the current article."
9206   (interactive "P")
9207   (gnus-summary-select-article)
9208   (let ((mail-header-separator ""))
9209     (gnus-eval-in-buffer-window gnus-article-buffer
9210       (save-excursion
9211         (save-restriction
9212           (widen)
9213           (let ((pos (window-start))
9214                 buffer-read-only)
9215             (goto-char (point-min))
9216             (when (message-goto-body)
9217               (gnus-narrow-to-body))
9218             (goto-char (point-min))
9219             (while (search-forward "·" (point-max) t)
9220               (replace-match "."))
9221             (unmorse-region (point-min) (point-max))
9222             (widen)
9223             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9224
9225 (defun gnus-summary-stop-page-breaking ()
9226   "Stop page breaking in the current article."
9227   (interactive)
9228   (gnus-summary-select-article)
9229   (gnus-eval-in-buffer-window gnus-article-buffer
9230     (widen)
9231     (when (gnus-visual-p 'page-marker)
9232       (let ((buffer-read-only nil))
9233         (gnus-remove-text-with-property 'gnus-prev)
9234         (gnus-remove-text-with-property 'gnus-next))
9235       (setq gnus-page-broken nil))))
9236
9237 (defun gnus-summary-move-article (&optional n to-newsgroup
9238                                             select-method action)
9239   "Move the current article to a different newsgroup.
9240 If N is a positive number, move the N next articles.
9241 If N is a negative number, move the N previous articles.
9242 If N is nil and any articles have been marked with the process mark,
9243 move those articles instead.
9244 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9245 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9246 re-spool using this method.
9247
9248 When called interactively with TO-NEWSGROUP being nil, the value of
9249 the variable `gnus-move-split-methods' is used for finding a default
9250 for the target newsgroup.
9251
9252 For this function to work, both the current newsgroup and the
9253 newsgroup that you want to move to have to support the `request-move'
9254 and `request-accept' functions.
9255
9256 ACTION can be either `move' (the default), `crosspost' or `copy'."
9257   (interactive "P")
9258   (unless action
9259     (setq action 'move))
9260   ;; Check whether the source group supports the required functions.
9261   (cond ((and (eq action 'move)
9262               (not (gnus-check-backend-function
9263                     'request-move-article gnus-newsgroup-name)))
9264          (error "The current group does not support article moving"))
9265         ((and (eq action 'crosspost)
9266               (not (gnus-check-backend-function
9267                     'request-replace-article gnus-newsgroup-name)))
9268          (error "The current group does not support article editing")))
9269   (let ((articles (gnus-summary-work-articles n))
9270         (prefix (if (gnus-check-backend-function
9271                      'request-move-article gnus-newsgroup-name)
9272                     (funcall gnus-move-group-prefix-function
9273                              gnus-newsgroup-name)
9274                   ""))
9275         (names '((move "Move" "Moving")
9276                  (copy "Copy" "Copying")
9277                  (crosspost "Crosspost" "Crossposting")))
9278         (copy-buf (save-excursion
9279                     (nnheader-set-temp-buffer " *copy article*")))
9280         art-group to-method new-xref article to-groups articles-to-update-marks)
9281     (unless (assq action names)
9282       (error "Unknown action %s" action))
9283     ;; Read the newsgroup name.
9284     (when (and (not to-newsgroup)
9285                (not select-method))
9286       (if (and gnus-move-split-methods
9287                (not
9288                 (and (memq gnus-current-article articles)
9289                      (gnus-buffer-live-p gnus-original-article-buffer))))
9290           ;; When `gnus-move-split-methods' is non-nil, we have to
9291           ;; select an article to give `gnus-read-move-group-name' an
9292           ;; opportunity to suggest an appropriate default.  However,
9293           ;; we needn't render or mark the article.
9294           (let ((gnus-display-mime-function nil)
9295                 (gnus-article-prepare-hook nil)
9296                 (gnus-mark-article-hook nil))
9297             (gnus-summary-select-article nil nil nil (car articles))))
9298       (setq to-newsgroup
9299             (gnus-read-move-group-name
9300              (cadr (assq action names))
9301              (symbol-value (intern (format "gnus-current-%s-group" action)))
9302              articles prefix))
9303       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9304     (setq to-method (or select-method
9305                         (gnus-server-to-method
9306                          (gnus-group-method to-newsgroup))))
9307     ;; Check the method we are to move this article to...
9308     (unless (gnus-check-backend-function
9309              'request-accept-article (car to-method))
9310       (error "%s does not support article copying" (car to-method)))
9311     (unless (gnus-check-server to-method)
9312       (error "Can't open server %s" (car to-method)))
9313     (gnus-message 6 "%s to %s: %s..."
9314                   (caddr (assq action names))
9315                   (or (car select-method) to-newsgroup) articles)
9316     (while articles
9317       (setq article (pop articles))
9318       (setq
9319        art-group
9320        (cond
9321         ;; Move the article.
9322         ((eq action 'move)
9323          ;; Remove this article from future suppression.
9324          (gnus-dup-unsuppress-article article)
9325          (let* ((from-method (gnus-find-method-for-group
9326                               gnus-newsgroup-name))
9327                 (to-method (gnus-find-method-for-group
9328                             to-newsgroup))
9329                 (move-is-internal (gnus-method-equal from-method to-method)))
9330          (gnus-request-move-article
9331           article                       ; Article to move
9332           gnus-newsgroup-name           ; From newsgroup
9333           (nth 1 (gnus-find-method-for-group
9334                   gnus-newsgroup-name)) ; Server
9335           (list 'gnus-request-accept-article
9336                 to-newsgroup (list 'quote select-method)
9337                 (not articles) t)       ; Accept form
9338           (not articles)                ; Only save nov last time
9339           move-is-internal)))           ; is this move internal?
9340         ;; Copy the article.
9341         ((eq action 'copy)
9342          (save-excursion
9343            (set-buffer copy-buf)
9344            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9345              (save-restriction
9346                (nnheader-narrow-to-headers)
9347                (dolist (hdr gnus-copy-article-ignored-headers)
9348                  (message-remove-header hdr t)))
9349              (gnus-request-accept-article
9350               to-newsgroup select-method (not articles) t))))
9351         ;; Crosspost the article.
9352         ((eq action 'crosspost)
9353          (let ((xref (message-tokenize-header
9354                       (mail-header-xref (gnus-summary-article-header article))
9355                       " ")))
9356            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9357                                   ":" (number-to-string article)))
9358            (unless xref
9359              (setq xref (list (system-name))))
9360            (setq new-xref
9361                  (concat
9362                   (mapconcat 'identity
9363                              (delete "Xref:" (delete new-xref xref))
9364                              " ")
9365                   " " new-xref))
9366            (save-excursion
9367              (set-buffer copy-buf)
9368              ;; First put the article in the destination group.
9369              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9370              (when (consp (setq art-group
9371                                 (gnus-request-accept-article
9372                                  to-newsgroup select-method (not articles))))
9373                (setq new-xref (concat new-xref " " (car art-group)
9374                                       ":"
9375                                       (number-to-string (cdr art-group))))
9376                ;; Now we have the new Xrefs header, so we insert
9377                ;; it and replace the new article.
9378                (nnheader-replace-header "Xref" new-xref)
9379                (gnus-request-replace-article
9380                 (cdr art-group) to-newsgroup (current-buffer))
9381                art-group))))))
9382       (cond
9383        ((not art-group)
9384         (gnus-message 1 "Couldn't %s article %s: %s"
9385                       (cadr (assq action names)) article
9386                       (nnheader-get-report (car to-method))))
9387        ((eq art-group 'junk)
9388         (when (eq action 'move)
9389           (gnus-summary-mark-article article gnus-canceled-mark)
9390           (gnus-message 4 "Deleted article %s" article)
9391           ;; run the delete hook
9392           (run-hook-with-args 'gnus-summary-article-delete-hook
9393                               action
9394                               (gnus-data-header
9395                                (assoc article (gnus-data-list nil)))
9396                               gnus-newsgroup-name nil
9397                               select-method)))
9398        (t
9399         (let* ((pto-group (gnus-group-prefixed-name
9400                            (car art-group) to-method))
9401                (info (gnus-get-info pto-group))
9402                (to-group (gnus-info-group info))
9403                to-marks)
9404           ;; Update the group that has been moved to.
9405           (when (and info
9406                      (memq action '(move copy)))
9407             (unless (member to-group to-groups)
9408               (push to-group to-groups))
9409
9410             (unless (memq article gnus-newsgroup-unreads)
9411               (push 'read to-marks)
9412               (gnus-info-set-read
9413                info (gnus-add-to-range (gnus-info-read info)
9414                                        (list (cdr art-group)))))
9415
9416             ;; See whether the article is to be put in the cache.
9417             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9418                              gnus-article-mark-lists
9419                            (delete '(expirable . expire)
9420                                    (copy-sequence gnus-article-mark-lists))))
9421                   (to-article (cdr art-group)))
9422
9423               ;; Enter the article into the cache in the new group,
9424               ;; if that is required.
9425               (when gnus-use-cache
9426                 (gnus-cache-possibly-enter-article
9427                  to-group to-article
9428                  (memq article gnus-newsgroup-marked)
9429                  (memq article gnus-newsgroup-dormant)
9430                  (memq article gnus-newsgroup-unreads)))
9431
9432               (when gnus-preserve-marks
9433                 ;; Copy any marks over to the new group.
9434                 (when (and (equal to-group gnus-newsgroup-name)
9435                            (not (memq article gnus-newsgroup-unreads)))
9436                   ;; Mark this article as read in this group.
9437                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9438                   (setcdr (gnus-active to-group) to-article)
9439                   (setcdr gnus-newsgroup-active to-article))
9440
9441                 (while marks
9442                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9443                     (when (memq article (symbol-value
9444                                          (intern (format "gnus-newsgroup-%s"
9445                                                          (caar marks)))))
9446                       (push (cdar marks) to-marks)
9447                       ;; If the other group is the same as this group,
9448                       ;; then we have to add the mark to the list.
9449                       (when (equal to-group gnus-newsgroup-name)
9450                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9451                              (cons to-article
9452                                    (symbol-value
9453                                     (intern (format "gnus-newsgroup-%s"
9454                                                     (caar marks)))))))
9455                       ;; Copy the marks to other group.
9456                       (gnus-add-marked-articles
9457                        to-group (cdar marks) (list to-article) info)))
9458                   (setq marks (cdr marks)))
9459
9460                 (gnus-request-set-mark
9461                  to-group (list (list (list to-article) 'add to-marks))))
9462
9463               (gnus-dribble-enter
9464                (concat "(gnus-group-set-info '"
9465                        (gnus-prin1-to-string (gnus-get-info to-group))
9466                        ")"))))
9467
9468           ;; Update the Xref header in this article to point to
9469           ;; the new crossposted article we have just created.
9470           (when (eq action 'crosspost)
9471             (save-excursion
9472               (set-buffer copy-buf)
9473               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9474               (nnheader-replace-header "Xref" new-xref)
9475               (gnus-request-replace-article
9476                article gnus-newsgroup-name (current-buffer))))
9477
9478           ;; run the move/copy/crosspost/respool hook
9479           (run-hook-with-args 'gnus-summary-article-move-hook
9480                               action
9481                               (gnus-data-header
9482                                (assoc article (gnus-data-list nil)))
9483                               gnus-newsgroup-name
9484                               to-newsgroup
9485                               select-method))
9486
9487         ;;;!!!Why is this necessary?
9488         (set-buffer gnus-summary-buffer)
9489         
9490         (gnus-summary-goto-subject article)
9491         (when (eq action 'move)
9492           (gnus-summary-mark-article article gnus-canceled-mark))))
9493       (push article articles-to-update-marks))
9494
9495     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9496     ;; Re-activate all groups that have been moved to.
9497     (save-excursion
9498       (set-buffer gnus-group-buffer)
9499       (let ((gnus-group-marked to-groups))
9500         (gnus-group-get-new-news-this-group nil t)))
9501     
9502     (gnus-kill-buffer copy-buf)
9503     (gnus-summary-position-point)
9504     (gnus-set-mode-line 'summary)))
9505
9506 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9507   "Copy the current article to some other group.
9508 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9509 When called interactively, if TO-NEWSGROUP is nil, use the value of
9510 the variable `gnus-move-split-methods' for finding a default target
9511 newsgroup.
9512 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9513 re-spool using this method."
9514   (interactive "P")
9515   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9516
9517 (defun gnus-summary-crosspost-article (&optional n)
9518   "Crosspost the current article to some other group."
9519   (interactive "P")
9520   (gnus-summary-move-article n nil nil 'crosspost))
9521
9522 (defcustom gnus-summary-respool-default-method nil
9523   "Default method type for respooling an article.
9524 If nil, use to the current newsgroup method."
9525   :type 'symbol
9526   :group 'gnus-summary-mail)
9527
9528 (defcustom gnus-summary-display-while-building nil
9529   "If non-nil, show and update the summary buffer as it's being built.
9530 If the value is t, update the buffer after every line is inserted.  If
9531 the value is an integer (N), update the display every N lines."
9532   :version "22.1"
9533   :group 'gnus-thread
9534   :type '(choice (const :tag "off" nil)
9535                  number
9536                  (const :tag "frequently" t)))
9537
9538 (defun gnus-summary-respool-article (&optional n method)
9539   "Respool the current article.
9540 The article will be squeezed through the mail spooling process again,
9541 which means that it will be put in some mail newsgroup or other
9542 depending on `nnmail-split-methods'.
9543 If N is a positive number, respool the N next articles.
9544 If N is a negative number, respool the N previous articles.
9545 If N is nil and any articles have been marked with the process mark,
9546 respool those articles instead.
9547
9548 Respooling can be done both from mail groups and \"real\" newsgroups.
9549 In the former case, the articles in question will be moved from the
9550 current group into whatever groups they are destined to.  In the
9551 latter case, they will be copied into the relevant groups."
9552   (interactive
9553    (list current-prefix-arg
9554          (let* ((methods (gnus-methods-using 'respool))
9555                 (methname
9556                  (symbol-name (or gnus-summary-respool-default-method
9557                                   (car (gnus-find-method-for-group
9558                                         gnus-newsgroup-name)))))
9559                 (method
9560                  (gnus-completing-read-with-default
9561                   methname "Backend to use when respooling"
9562                   methods nil t nil 'gnus-mail-method-history))
9563                 ms)
9564            (cond
9565             ((zerop (length (setq ms (gnus-servers-using-backend
9566                                       (intern method)))))
9567              (list (intern method) ""))
9568             ((= 1 (length ms))
9569              (car ms))
9570             (t
9571              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9572                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9573                            ms-alist))))))))
9574   (unless method
9575     (error "No method given for respooling"))
9576   (if (assoc (symbol-name
9577               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9578              (gnus-methods-using 'respool))
9579       (gnus-summary-move-article n nil method)
9580     (gnus-summary-copy-article n nil method)))
9581
9582 (defun gnus-summary-import-article (file &optional edit)
9583   "Import an arbitrary file into a mail newsgroup."
9584   (interactive "fImport file: \nP")
9585   (let ((group gnus-newsgroup-name)
9586         (now (current-time))
9587         atts lines group-art)
9588     (unless (gnus-check-backend-function 'request-accept-article group)
9589       (error "%s does not support article importing" group))
9590     (or (file-readable-p file)
9591         (not (file-regular-p file))
9592         (error "Can't read %s" file))
9593     (save-excursion
9594       (set-buffer (gnus-get-buffer-create " *import file*"))
9595       (erase-buffer)
9596       (nnheader-insert-file-contents file)
9597       (goto-char (point-min))
9598       (if (nnheader-article-p)
9599           (save-restriction
9600             (goto-char (point-min))
9601             (search-forward "\n\n" nil t)
9602             (narrow-to-region (point-min) (1- (point)))
9603             (goto-char (point-min))
9604             (unless (re-search-forward "^date:" nil t)
9605               (goto-char (point-max))
9606               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9607        ;; This doesn't look like an article, so we fudge some headers.
9608         (setq atts (file-attributes file)
9609               lines (count-lines (point-min) (point-max)))
9610         (insert "From: " (read-string "From: ") "\n"
9611                 "Subject: " (read-string "Subject: ") "\n"
9612                 "Date: " (message-make-date (nth 5 atts)) "\n"
9613                 "Message-ID: " (message-make-message-id) "\n"
9614                 "Lines: " (int-to-string lines) "\n"
9615                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9616       (setq group-art (gnus-request-accept-article group nil t))
9617       (kill-buffer (current-buffer)))
9618     (setq gnus-newsgroup-active (gnus-activate-group group))
9619     (forward-line 1)
9620     (gnus-summary-goto-article (cdr group-art) nil t)
9621     (when edit
9622       (gnus-summary-edit-article))))
9623
9624 (defun gnus-summary-create-article ()
9625   "Create an article in a mail newsgroup."
9626   (interactive)
9627   (let ((group gnus-newsgroup-name)
9628         (now (current-time))
9629         group-art)
9630     (unless (gnus-check-backend-function 'request-accept-article group)
9631       (error "%s does not support article importing" group))
9632     (save-excursion
9633       (set-buffer (gnus-get-buffer-create " *import file*"))
9634       (erase-buffer)
9635       (goto-char (point-min))
9636       ;; This doesn't look like an article, so we fudge some headers.
9637       (insert "From: " (read-string "From: ") "\n"
9638               "Subject: " (read-string "Subject: ") "\n"
9639               "Date: " (message-make-date now) "\n"
9640               "Message-ID: " (message-make-message-id) "\n")
9641       (setq group-art (gnus-request-accept-article group nil t))
9642       (kill-buffer (current-buffer)))
9643     (setq gnus-newsgroup-active (gnus-activate-group group))
9644     (forward-line 1)
9645     (gnus-summary-goto-article (cdr group-art) nil t)
9646     (gnus-summary-edit-article)))
9647
9648 (defun gnus-summary-article-posted-p ()
9649   "Say whether the current (mail) article is available from news as well.
9650 This will be the case if the article has both been mailed and posted."
9651   (interactive)
9652   (let ((id (mail-header-references (gnus-summary-article-header)))
9653         (gnus-override-method (car (gnus-refer-article-methods))))
9654     (if (gnus-request-head id "")
9655         (gnus-message 2 "The current message was found on %s"
9656                       gnus-override-method)
9657       (gnus-message 2 "The current message couldn't be found on %s"
9658                     gnus-override-method)
9659       nil)))
9660
9661 (defun gnus-summary-expire-articles (&optional now)
9662   "Expire all articles that are marked as expirable in the current group."
9663   (interactive)
9664   (when (and (not gnus-group-is-exiting-without-update-p)
9665              (gnus-check-backend-function
9666               'request-expire-articles gnus-newsgroup-name))
9667     ;; This backend supports expiry.
9668     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9669            (expirable (if total
9670                           (progn
9671                             ;; We need to update the info for
9672                             ;; this group for `gnus-list-of-read-articles'
9673                             ;; to give us the right answer.
9674                             (gnus-run-hooks 'gnus-exit-group-hook)
9675                             (gnus-summary-update-info)
9676                             (gnus-list-of-read-articles gnus-newsgroup-name))
9677                         (setq gnus-newsgroup-expirable
9678                               (sort gnus-newsgroup-expirable '<))))
9679            (expiry-wait (if now 'immediate
9680                           (gnus-group-find-parameter
9681                            gnus-newsgroup-name 'expiry-wait)))
9682            (nnmail-expiry-target
9683             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9684                 nnmail-expiry-target))
9685            es)
9686       (when expirable
9687         ;; There are expirable articles in this group, so we run them
9688         ;; through the expiry process.
9689         (gnus-message 6 "Expiring articles...")
9690         (unless (gnus-check-group gnus-newsgroup-name)
9691           (error "Can't open server for %s" gnus-newsgroup-name))
9692         ;; The list of articles that weren't expired is returned.
9693         (save-excursion
9694           (if expiry-wait
9695               (let ((nnmail-expiry-wait-function nil)
9696                     (nnmail-expiry-wait expiry-wait))
9697                 (setq es (gnus-request-expire-articles
9698                           expirable gnus-newsgroup-name)))
9699             (setq es (gnus-request-expire-articles
9700                       expirable gnus-newsgroup-name)))
9701           (unless total
9702             (setq gnus-newsgroup-expirable es))
9703           ;; We go through the old list of expirable, and mark all
9704           ;; really expired articles as nonexistent.
9705           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9706             (let ((gnus-use-cache nil))
9707               (dolist (article expirable)
9708                 (when (and (not (memq article es))
9709                            (gnus-data-find article))
9710                   (gnus-summary-mark-article article gnus-canceled-mark)
9711                   (run-hook-with-args 'gnus-summary-article-expire-hook
9712                                       'delete
9713                                       (gnus-data-header
9714                                        (assoc article (gnus-data-list nil)))
9715                                       gnus-newsgroup-name
9716                                       nil
9717                                       nil))))))
9718         (gnus-message 6 "Expiring articles...done")))))
9719
9720 (defun gnus-summary-expire-articles-now ()
9721   "Expunge all expirable articles in the current group.
9722 This means that *all* articles that are marked as expirable will be
9723 deleted forever, right now."
9724   (interactive)
9725   (or gnus-expert-user
9726       (gnus-yes-or-no-p
9727        "Are you really, really sure you want to delete all expirable messages? ")
9728       (error "Phew!"))
9729   (gnus-summary-expire-articles t))
9730
9731 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9732 (defun gnus-summary-delete-article (&optional n)
9733   "Delete the N next (mail) articles.
9734 This command actually deletes articles.  This is not a marking
9735 command.  The article will disappear forever from your life, never to
9736 return.
9737
9738 If N is negative, delete backwards.
9739 If N is nil and articles have been marked with the process mark,
9740 delete these instead.
9741
9742 If `gnus-novice-user' is non-nil you will be asked for
9743 confirmation before the articles are deleted."
9744   (interactive "P")
9745   (unless (gnus-check-backend-function 'request-expire-articles
9746                                        gnus-newsgroup-name)
9747     (error "The current newsgroup does not support article deletion"))
9748   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9749     (error "Couldn't open server"))
9750   ;; Compute the list of articles to delete.
9751   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9752         (nnmail-expiry-target 'delete)
9753         not-deleted)
9754     (if (and gnus-novice-user
9755              (not (gnus-yes-or-no-p
9756                    (format "Do you really want to delete %s forever? "
9757                            (if (> (length articles) 1)
9758                                (format "these %s articles" (length articles))
9759                              "this article")))))
9760         ()
9761       ;; Delete the articles.
9762       (setq not-deleted (gnus-request-expire-articles
9763                          articles gnus-newsgroup-name 'force))
9764       (while articles
9765         (gnus-summary-remove-process-mark (car articles))
9766         ;; The backend might not have been able to delete the article
9767         ;; after all.
9768         (unless (memq (car articles) not-deleted)
9769           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9770         (let* ((article (car articles))
9771                (ghead  (gnus-data-header
9772                                     (assoc article (gnus-data-list nil)))))
9773           (run-hook-with-args 'gnus-summary-article-delete-hook
9774                               'delete ghead gnus-newsgroup-name nil
9775                               nil))
9776         (setq articles (cdr articles)))
9777       (when not-deleted
9778         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9779     (gnus-summary-position-point)
9780     (gnus-set-mode-line 'summary)
9781     not-deleted))
9782
9783 (defun gnus-summary-edit-article (&optional arg)
9784   "Edit the current article.
9785 This will have permanent effect only in mail groups.
9786 If ARG is nil, edit the decoded articles.
9787 If ARG is 1, edit the raw articles.
9788 If ARG is 2, edit the raw articles even in read-only groups.
9789 If ARG is 3, edit the articles with the current handles.
9790 Otherwise, allow editing of articles even in read-only
9791 groups."
9792   (interactive "P")
9793   (let (force raw current-handles)
9794     (cond
9795      ((null arg))
9796      ((eq arg 1)
9797       (setq raw t))
9798      ((eq arg 2)
9799       (setq raw t
9800             force t))
9801      ((eq arg 3)
9802       (setq current-handles
9803             (and (gnus-buffer-live-p gnus-article-buffer)
9804                  (with-current-buffer gnus-article-buffer
9805                    (prog1
9806                        gnus-article-mime-handles
9807                      (setq gnus-article-mime-handles nil))))))
9808      (t
9809       (setq force t)))
9810     (when (and raw (not force)
9811                (member gnus-newsgroup-name '("nndraft:delayed"
9812                                              "nndraft:drafts"
9813                                              "nndraft:queue")))
9814       (error "Can't edit the raw article in group %s"
9815              gnus-newsgroup-name))
9816     (save-excursion
9817       (set-buffer gnus-summary-buffer)
9818       (let ((mail-parse-charset gnus-newsgroup-charset)
9819             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9820         (gnus-set-global-variables)
9821         (when (and (not force)
9822                    (gnus-group-read-only-p))
9823           (error "The current newsgroup does not support article editing"))
9824         (gnus-summary-show-article t)
9825         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9826           (with-current-buffer gnus-article-buffer
9827             (mm-enable-multibyte)))
9828         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9829             (setq raw t))
9830         (gnus-article-edit-article
9831          (if raw 'ignore
9832            `(lambda ()
9833               (let ((mbl mml-buffer-list))
9834                 (setq mml-buffer-list nil)
9835                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9836                   (mime-to-mml ,'current-handles))
9837                 (let ((mbl1 mml-buffer-list))
9838                   (setq mml-buffer-list mbl)
9839                   (set (make-local-variable 'mml-buffer-list) mbl1))
9840                 (gnus-make-local-hook 'kill-buffer-hook)
9841                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9842          `(lambda (no-highlight)
9843             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9844                   (message-options message-options)
9845                   (message-options-set-recipient)
9846                   (mail-parse-ignored-charsets
9847                    ',gnus-newsgroup-ignored-charsets))
9848               ,(if (not raw) '(progn
9849                                 (mml-to-mime)
9850                                 (mml-destroy-buffers)
9851                                 (remove-hook 'kill-buffer-hook
9852                                              'mml-destroy-buffers t)
9853                                 (kill-local-variable 'mml-buffer-list)))
9854               (gnus-summary-edit-article-done
9855                ,(or (mail-header-references gnus-current-headers) "")
9856                ,(gnus-group-read-only-p)
9857                ,gnus-summary-buffer no-highlight))))))))
9858
9859 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9860
9861 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9862                                                  no-highlight)
9863   "Make edits to the current article permanent."
9864   (interactive)
9865   (save-excursion
9866    ;; The buffer restriction contains the entire article if it exists.
9867     (when (article-goto-body)
9868       (let ((lines (count-lines (point) (point-max)))
9869             (length (- (point-max) (point)))
9870             (case-fold-search t)
9871             (body (copy-marker (point))))
9872         (goto-char (point-min))
9873         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9874           (delete-region (match-beginning 1) (match-end 1))
9875           (insert (number-to-string length)))
9876         (goto-char (point-min))
9877         (when (re-search-forward
9878                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9879           (delete-region (match-beginning 1) (match-end 1))
9880           (insert (number-to-string length)))
9881         (goto-char (point-min))
9882         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9883           (delete-region (match-beginning 1) (match-end 1))
9884           (insert (number-to-string lines))))))
9885   ;; Replace the article.
9886   (let ((buf (current-buffer)))
9887     (with-temp-buffer
9888       (insert-buffer-substring buf)
9889
9890       (if (and (not read-only)
9891                (not (gnus-request-replace-article
9892                      (cdr gnus-article-current) (car gnus-article-current)
9893                      (current-buffer) t)))
9894           (error "Couldn't replace article")
9895         ;; Update the summary buffer.
9896         (if (and references
9897                  (equal (message-tokenize-header references " ")
9898                         (message-tokenize-header
9899                          (or (message-fetch-field "references") "") " ")))
9900             ;; We only have to update this line.
9901             (save-excursion
9902               (save-restriction
9903                 (message-narrow-to-head)
9904                 (let ((head (buffer-substring-no-properties
9905                              (point-min) (point-max)))
9906                       header)
9907                   (with-temp-buffer
9908                     (insert (format "211 %d Article retrieved.\n"
9909                                     (cdr gnus-article-current)))
9910                     (insert head)
9911                     (insert ".\n")
9912                     (let ((nntp-server-buffer (current-buffer)))
9913                       (setq header (car (gnus-get-newsgroup-headers
9914                                          nil t))))
9915                     (save-excursion
9916                       (set-buffer gnus-summary-buffer)
9917                       (gnus-data-set-header
9918                        (gnus-data-find (cdr gnus-article-current))
9919                        header)
9920                       (gnus-summary-update-article-line
9921                        (cdr gnus-article-current) header)
9922                       (if (gnus-summary-goto-subject
9923                            (cdr gnus-article-current) nil t)
9924                           (gnus-summary-update-secondary-mark
9925                            (cdr gnus-article-current))))))))
9926           ;; Update threads.
9927           (set-buffer (or buffer gnus-summary-buffer))
9928           (gnus-summary-update-article (cdr gnus-article-current))
9929           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9930               (gnus-summary-update-secondary-mark
9931                (cdr gnus-article-current))))
9932         ;; Prettify the article buffer again.
9933         (unless no-highlight
9934           (save-excursion
9935             (set-buffer gnus-article-buffer)
9936             ;;;!!! Fix this -- article should be rehighlighted.
9937             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9938             (set-buffer gnus-original-article-buffer)
9939             (gnus-request-article
9940              (cdr gnus-article-current)
9941              (car gnus-article-current) (current-buffer))))
9942         ;; Prettify the summary buffer line.
9943         (when (gnus-visual-p 'summary-highlight 'highlight)
9944           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9945
9946 (defun gnus-summary-edit-wash (key)
9947   "Perform editing command KEY in the article buffer."
9948   (interactive
9949    (list
9950     (progn
9951       (message "%s" (concat (this-command-keys) "- "))
9952       (read-char))))
9953   (message "")
9954   (gnus-summary-edit-article)
9955   (execute-kbd-macro (concat (this-command-keys) key))
9956   (gnus-article-edit-done))
9957
9958 ;;; Respooling
9959
9960 (defun gnus-summary-respool-query (&optional silent trace)
9961   "Query where the respool algorithm would put this article."
9962   (interactive)
9963   (let (gnus-mark-article-hook)
9964     (gnus-summary-select-article)
9965     (save-excursion
9966       (set-buffer gnus-original-article-buffer)
9967       (let ((groups (nnmail-article-group 'identity trace)))
9968         (unless silent
9969           (if groups
9970               (message "This message would go to %s"
9971                        (mapconcat 'car groups ", "))
9972             (message "This message would go to no groups"))
9973           groups)))))
9974
9975 (defun gnus-summary-respool-trace ()
9976   "Trace where the respool algorithm would put this article.
9977 Display a buffer showing all fancy splitting patterns which matched."
9978   (interactive)
9979   (gnus-summary-respool-query nil t))
9980
9981 ;; Summary marking commands.
9982
9983 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9984   "Mark articles which has the same subject as read, and then select the next.
9985 If UNMARK is positive, remove any kind of mark.
9986 If UNMARK is negative, tick articles."
9987   (interactive "P")
9988   (when unmark
9989     (setq unmark (prefix-numeric-value unmark)))
9990   (let ((count
9991          (gnus-summary-mark-same-subject
9992           (gnus-summary-article-subject) unmark)))
9993     ;; Select next unread article.  If auto-select-same mode, should
9994     ;; select the first unread article.
9995     (gnus-summary-next-article t (and gnus-auto-select-same
9996                                       (gnus-summary-article-subject)))
9997     (gnus-message 7 "%d article%s marked as %s"
9998                   count (if (= count 1) " is" "s are")
9999                   (if unmark "unread" "read"))))
10000
10001 (defun gnus-summary-kill-same-subject (&optional unmark)
10002   "Mark articles which has the same subject as read.
10003 If UNMARK is positive, remove any kind of mark.
10004 If UNMARK is negative, tick articles."
10005   (interactive "P")
10006   (when unmark
10007     (setq unmark (prefix-numeric-value unmark)))
10008   (let ((count
10009          (gnus-summary-mark-same-subject
10010           (gnus-summary-article-subject) unmark)))
10011     ;; If marked as read, go to next unread subject.
10012     (when (null unmark)
10013       ;; Go to next unread subject.
10014       (gnus-summary-next-subject 1 t))
10015     (gnus-message 7 "%d articles are marked as %s"
10016                   count (if unmark "unread" "read"))))
10017
10018 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10019   "Mark articles with same SUBJECT as read, and return marked number.
10020 If optional argument UNMARK is positive, remove any kinds of marks.
10021 If optional argument UNMARK is negative, mark articles as unread instead."
10022   (let ((count 1))
10023     (save-excursion
10024       (cond
10025        ((null unmark)                   ; Mark as read.
10026         (while (and
10027                 (progn
10028                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10029                   (gnus-summary-show-thread) t)
10030                 (gnus-summary-find-subject subject))
10031           (setq count (1+ count))))
10032        ((> unmark 0)                    ; Tick.
10033         (while (and
10034                 (progn
10035                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10036                   (gnus-summary-show-thread) t)
10037                 (gnus-summary-find-subject subject))
10038           (setq count (1+ count))))
10039        (t                               ; Mark as unread.
10040         (while (and
10041                 (progn
10042                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10043                   (gnus-summary-show-thread) t)
10044                 (gnus-summary-find-subject subject))
10045           (setq count (1+ count)))))
10046       (gnus-set-mode-line 'summary)
10047       ;; Return the number of marked articles.
10048       count)))
10049
10050 (defun gnus-summary-mark-as-processable (n &optional unmark)
10051   "Set the process mark on the next N articles.
10052 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10053 the process mark instead.  The difference between N and the actual
10054 number of articles marked is returned."
10055   (interactive "P")
10056   (if (and (null n) (gnus-region-active-p))
10057       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10058     (setq n (prefix-numeric-value n))
10059     (let ((backward (< n 0))
10060           (n (abs n)))
10061       (while (and
10062               (> n 0)
10063               (if unmark
10064                   (gnus-summary-remove-process-mark
10065                    (gnus-summary-article-number))
10066                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10067               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10068         (setq n (1- n)))
10069       (when (/= 0 n)
10070         (gnus-message 7 "No more articles"))
10071       (gnus-summary-recenter)
10072       (gnus-summary-position-point)
10073       n)))
10074
10075 (defun gnus-summary-unmark-as-processable (n)
10076   "Remove the process mark from the next N articles.
10077 If N is negative, unmark backward instead.  The difference between N and
10078 the actual number of articles unmarked is returned."
10079   (interactive "P")
10080   (gnus-summary-mark-as-processable n t))
10081
10082 (defun gnus-summary-unmark-all-processable ()
10083   "Remove the process mark from all articles."
10084   (interactive)
10085   (save-excursion
10086     (while gnus-newsgroup-processable
10087       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10088   (gnus-summary-position-point))
10089
10090 (defun gnus-summary-add-mark (article type)
10091   "Mark ARTICLE with a mark of TYPE."
10092   (let ((vtype (car (assq type gnus-article-mark-lists)))
10093         var)
10094     (if (not vtype)
10095         (error "No such mark type: %s" type)
10096       (setq var (intern (format "gnus-newsgroup-%s" type)))
10097       (set var (cons article (symbol-value var)))
10098       (if (memq type '(processable cached replied forwarded recent saved))
10099           (gnus-summary-update-secondary-mark article)
10100         ;;; !!! This is bogus.  We should find out what primary
10101         ;;; !!! mark we want to set.
10102         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10103
10104 (defun gnus-summary-mark-as-expirable (n)
10105   "Mark N articles forward as expirable.
10106 If N is negative, mark backward instead.  The difference between N and
10107 the actual number of articles marked is returned."
10108   (interactive "p")
10109   (gnus-summary-mark-forward n gnus-expirable-mark))
10110
10111 (defun gnus-summary-mark-as-spam (n)
10112   "Mark N articles forward as spam.
10113 If N is negative, mark backward instead.  The difference between N and
10114 the actual number of articles marked is returned."
10115   (interactive "p")
10116   (gnus-summary-mark-forward n gnus-spam-mark))
10117
10118 (defun gnus-summary-mark-article-as-replied (article)
10119   "Mark ARTICLE as replied to and update the summary line.
10120 ARTICLE can also be a list of articles."
10121   (interactive (list (gnus-summary-article-number)))
10122   (let ((articles (if (listp article) article (list article))))
10123     (dolist (article articles)
10124       (unless (numberp article)
10125         (error "%s is not a number" article))
10126       (push article gnus-newsgroup-replied)
10127       (let ((buffer-read-only nil))
10128         (when (gnus-summary-goto-subject article nil t)
10129           (gnus-summary-update-secondary-mark article))))))
10130
10131 (defun gnus-summary-mark-article-as-forwarded (article)
10132   "Mark ARTICLE as forwarded and update the summary line.
10133 ARTICLE can also be a list of articles."
10134   (let ((articles (if (listp article) article (list article))))
10135     (dolist (article articles)
10136       (push article gnus-newsgroup-forwarded)
10137       (let ((buffer-read-only nil))
10138         (when (gnus-summary-goto-subject article nil t)
10139           (gnus-summary-update-secondary-mark article))))))
10140
10141 (defun gnus-summary-set-bookmark (article)
10142   "Set a bookmark in current article."
10143   (interactive (list (gnus-summary-article-number)))
10144   (when (or (not (get-buffer gnus-article-buffer))
10145             (not gnus-current-article)
10146             (not gnus-article-current)
10147             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10148     (error "No current article selected"))
10149   ;; Remove old bookmark, if one exists.
10150   (gnus-pull article gnus-newsgroup-bookmarks)
10151   ;; Set the new bookmark, which is on the form
10152   ;; (article-number . line-number-in-body).
10153   (push
10154    (cons article
10155          (with-current-buffer gnus-article-buffer
10156            (count-lines
10157             (min (point)
10158                  (save-excursion
10159                    (article-goto-body)
10160                    (point)))
10161             (point))))
10162    gnus-newsgroup-bookmarks)
10163   (gnus-message 6 "A bookmark has been added to the current article."))
10164
10165 (defun gnus-summary-remove-bookmark (article)
10166   "Remove the bookmark from the current article."
10167   (interactive (list (gnus-summary-article-number)))
10168   ;; Remove old bookmark, if one exists.
10169   (if (not (assq article gnus-newsgroup-bookmarks))
10170       (gnus-message 6 "No bookmark in current article.")
10171     (gnus-pull article gnus-newsgroup-bookmarks)
10172     (gnus-message 6 "Removed bookmark.")))
10173
10174 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10175 (defun gnus-summary-mark-as-dormant (n)
10176   "Mark N articles forward as dormant.
10177 If N is negative, mark backward instead.  The difference between N and
10178 the actual number of articles marked is returned."
10179   (interactive "p")
10180   (gnus-summary-mark-forward n gnus-dormant-mark))
10181
10182 (defun gnus-summary-set-process-mark (article)
10183   "Set the process mark on ARTICLE and update the summary line."
10184   (setq gnus-newsgroup-processable
10185         (cons article
10186               (delq article gnus-newsgroup-processable)))
10187   (when (gnus-summary-goto-subject article)
10188     (gnus-summary-show-thread)
10189     (gnus-summary-goto-subject article)
10190     (gnus-summary-update-secondary-mark article)))
10191
10192 (defun gnus-summary-remove-process-mark (&rest articles)
10193   "Remove the process mark from ARTICLES and update the summary line."
10194   (dolist (article articles)
10195     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10196     (when (gnus-summary-goto-subject article)
10197       (gnus-summary-show-thread)
10198       (gnus-summary-goto-subject article)
10199       (gnus-summary-update-secondary-mark article)))
10200   t)
10201
10202 (defun gnus-summary-set-saved-mark (article)
10203   "Set the process mark on ARTICLE and update the summary line."
10204   (push article gnus-newsgroup-saved)
10205   (when (gnus-summary-goto-subject article)
10206     (gnus-summary-update-secondary-mark article)))
10207
10208 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10209   "Mark N articles as read forwards.
10210 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10211 The difference between N and the actual number of articles marked is
10212 returned.
10213 If NO-EXPIRE, auto-expiry will be inhibited."
10214   (interactive "p")
10215   (gnus-summary-show-thread)
10216   (let ((backward (< n 0))
10217         (gnus-summary-goto-unread
10218          (and gnus-summary-goto-unread
10219               (not (eq gnus-summary-goto-unread 'never))
10220               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10221                                     gnus-ticked-mark gnus-dormant-mark)))))
10222         (n (abs n))
10223         (mark (or mark gnus-del-mark)))
10224     (while (and (> n 0)
10225                 (gnus-summary-mark-article nil mark no-expire)
10226                 (zerop (gnus-summary-next-subject
10227                         (if backward -1 1)
10228                         (and gnus-summary-goto-unread
10229                              (not (eq gnus-summary-goto-unread 'never)))
10230                         t)))
10231       (setq n (1- n)))
10232     (when (/= 0 n)
10233       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10234     (gnus-summary-recenter)
10235     (gnus-summary-position-point)
10236     (gnus-set-mode-line 'summary)
10237     n))
10238
10239 (defun gnus-summary-mark-article-as-read (mark)
10240   "Mark the current article quickly as read with MARK."
10241   (let ((article (gnus-summary-article-number)))
10242     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10243     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10244     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10245     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10246     (push (cons article mark) gnus-newsgroup-reads)
10247     ;; Possibly remove from cache, if that is used.
10248     (when gnus-use-cache
10249       (gnus-cache-enter-remove-article article))
10250     ;; Allow the backend to change the mark.
10251     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10252     ;; Check for auto-expiry.
10253     (when (and gnus-newsgroup-auto-expire
10254                (memq mark gnus-auto-expirable-marks))
10255       (setq mark gnus-expirable-mark)
10256       ;; Let the backend know about the mark change.
10257       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10258       (push article gnus-newsgroup-expirable))
10259     ;; Set the mark in the buffer.
10260     (gnus-summary-update-mark mark 'unread)
10261     t))
10262
10263 (defun gnus-summary-mark-article-as-unread (mark)
10264   "Mark the current article quickly as unread with MARK."
10265   (let* ((article (gnus-summary-article-number))
10266          (old-mark (gnus-summary-article-mark article)))
10267     ;; Allow the backend to change the mark.
10268     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10269     (if (eq mark old-mark)
10270         t
10271       (if (<= article 0)
10272           (progn
10273             (gnus-error 1 "Can't mark negative article numbers")
10274             nil)
10275         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10276         (setq gnus-newsgroup-spam-marked
10277               (delq article gnus-newsgroup-spam-marked))
10278         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10279         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10280         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10281         (cond ((= mark gnus-ticked-mark)
10282                (setq gnus-newsgroup-marked
10283                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10284                                               article)))
10285               ((= mark gnus-spam-mark)
10286                (setq gnus-newsgroup-spam-marked
10287                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10288                                               article)))
10289               ((= mark gnus-dormant-mark)
10290                (setq gnus-newsgroup-dormant
10291                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10292                                               article)))
10293               (t
10294                (setq gnus-newsgroup-unreads
10295                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10296                                               article))))
10297         (gnus-pull article gnus-newsgroup-reads)
10298
10299         ;; See whether the article is to be put in the cache.
10300         (and gnus-use-cache
10301              (vectorp (gnus-summary-article-header article))
10302              (save-excursion
10303                (gnus-cache-possibly-enter-article
10304                 gnus-newsgroup-name article
10305                 (= mark gnus-ticked-mark)
10306                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10307
10308         ;; Fix the mark.
10309         (gnus-summary-update-mark mark 'unread)
10310         t))))
10311
10312 (defun gnus-summary-mark-article (&optional article mark no-expire)
10313   "Mark ARTICLE with MARK.  MARK can be any character.
10314 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10315 `??' (dormant) and `?E' (expirable).
10316 If MARK is nil, then the default character `?r' is used.
10317 If ARTICLE is nil, then the article on the current line will be
10318 marked.
10319 If NO-EXPIRE, auto-expiry will be inhibited."
10320   ;; The mark might be a string.
10321   (when (stringp mark)
10322     (setq mark (aref mark 0)))
10323   ;; If no mark is given, then we check auto-expiring.
10324   (when (null mark)
10325     (setq mark gnus-del-mark))
10326   (when (and (not no-expire)
10327              gnus-newsgroup-auto-expire
10328              (memq mark gnus-auto-expirable-marks))
10329     (setq mark gnus-expirable-mark))
10330   (let ((article (or article (gnus-summary-article-number)))
10331         (old-mark (gnus-summary-article-mark article)))
10332     ;; Allow the backend to change the mark.
10333     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10334     (if (eq mark old-mark)
10335         t
10336       (unless article
10337         (error "No article on current line"))
10338       (if (not (if (or (= mark gnus-unread-mark)
10339                        (= mark gnus-ticked-mark)
10340                        (= mark gnus-spam-mark)
10341                        (= mark gnus-dormant-mark))
10342                    (gnus-mark-article-as-unread article mark)
10343                  (gnus-mark-article-as-read article mark)))
10344           t
10345         ;; See whether the article is to be put in the cache.
10346         (and gnus-use-cache
10347              (not (= mark gnus-canceled-mark))
10348              (vectorp (gnus-summary-article-header article))
10349              (save-excursion
10350                (gnus-cache-possibly-enter-article
10351                 gnus-newsgroup-name article
10352                 (= mark gnus-ticked-mark)
10353                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10354
10355         (when (gnus-summary-goto-subject article nil t)
10356           (let ((buffer-read-only nil))
10357             (gnus-summary-show-thread)
10358             ;; Fix the mark.
10359             (gnus-summary-update-mark mark 'unread)
10360             t))))))
10361
10362 (defun gnus-summary-update-secondary-mark (article)
10363   "Update the secondary (read, process, cache) mark."
10364   (gnus-summary-update-mark
10365    (cond ((memq article gnus-newsgroup-processable)
10366           gnus-process-mark)
10367          ((memq article gnus-newsgroup-cached)
10368           gnus-cached-mark)
10369          ((memq article gnus-newsgroup-replied)
10370           gnus-replied-mark)
10371          ((memq article gnus-newsgroup-forwarded)
10372           gnus-forwarded-mark)
10373          ((memq article gnus-newsgroup-saved)
10374           gnus-saved-mark)
10375          ((memq article gnus-newsgroup-recent)
10376           gnus-recent-mark)
10377          ((memq article gnus-newsgroup-unseen)
10378           gnus-unseen-mark)
10379          (t gnus-no-mark))
10380    'replied)
10381   (when (gnus-visual-p 'summary-highlight 'highlight)
10382     (gnus-run-hooks 'gnus-summary-update-hook))
10383   t)
10384
10385 (defun gnus-summary-update-download-mark (article)
10386   "Update the download mark."
10387   (gnus-summary-update-mark
10388    (cond ((memq article gnus-newsgroup-undownloaded)
10389           gnus-undownloaded-mark)
10390          (gnus-newsgroup-agentized
10391           gnus-downloaded-mark)
10392          (t
10393           gnus-no-mark))
10394    'download)
10395   (gnus-summary-update-line t)
10396   t)
10397
10398 (defun gnus-summary-update-mark (mark type)
10399   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10400         (buffer-read-only nil))
10401     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10402     (when forward
10403       (when (looking-at "\r")
10404         (incf forward))
10405       (when (<= (+ forward (point)) (point-max))
10406         ;; Go to the right position on the line.
10407         (goto-char (+ forward (point)))
10408         ;; Replace the old mark with the new mark.
10409         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10410         ;; Optionally update the marks by some user rule.
10411         (when (eq type 'unread)
10412           (gnus-data-set-mark
10413            (gnus-data-find (gnus-summary-article-number)) mark)
10414           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10415
10416 (defun gnus-mark-article-as-read (article &optional mark)
10417   "Enter ARTICLE in the pertinent lists and remove it from others."
10418   ;; Make the article expirable.
10419   (let ((mark (or mark gnus-del-mark)))
10420     (setq gnus-newsgroup-expirable
10421           (if (= mark gnus-expirable-mark)
10422               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10423             (delq article gnus-newsgroup-expirable)))
10424     ;; Remove from unread and marked lists.
10425     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10426     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10427     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10428     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10429     (push (cons article mark) gnus-newsgroup-reads)
10430     ;; Possibly remove from cache, if that is used.
10431     (when gnus-use-cache
10432       (gnus-cache-enter-remove-article article))
10433     t))
10434
10435 (defun gnus-mark-article-as-unread (article &optional mark)
10436   "Enter ARTICLE in the pertinent lists and remove it from others."
10437   (let ((mark (or mark gnus-ticked-mark)))
10438     (if (<= article 0)
10439         (progn
10440           (gnus-error 1 "Can't mark negative article numbers")
10441           nil)
10442       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10443             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10444             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10445             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10446             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10447
10448       ;; Unsuppress duplicates?
10449       (when gnus-suppress-duplicates
10450         (gnus-dup-unsuppress-article article))
10451
10452       (cond ((= mark gnus-ticked-mark)
10453              (setq gnus-newsgroup-marked
10454                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10455             ((= mark gnus-spam-mark)
10456              (setq gnus-newsgroup-spam-marked
10457                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10458                                             article)))
10459             ((= mark gnus-dormant-mark)
10460              (setq gnus-newsgroup-dormant
10461                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10462             (t
10463              (setq gnus-newsgroup-unreads
10464                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10465       (gnus-pull article gnus-newsgroup-reads)
10466       t)))
10467
10468 (defalias 'gnus-summary-mark-as-unread-forward
10469   'gnus-summary-tick-article-forward)
10470 (make-obsolete 'gnus-summary-mark-as-unread-forward
10471                'gnus-summary-tick-article-forward)
10472 (defun gnus-summary-tick-article-forward (n)
10473   "Tick N articles forwards.
10474 If N is negative, tick backwards instead.
10475 The difference between N and the number of articles ticked is returned."
10476   (interactive "p")
10477   (gnus-summary-mark-forward n gnus-ticked-mark))
10478
10479 (defalias 'gnus-summary-mark-as-unread-backward
10480   'gnus-summary-tick-article-backward)
10481 (make-obsolete 'gnus-summary-mark-as-unread-backward
10482                'gnus-summary-tick-article-backward)
10483 (defun gnus-summary-tick-article-backward (n)
10484   "Tick N articles backwards.
10485 The difference between N and the number of articles ticked is returned."
10486   (interactive "p")
10487   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10488
10489 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10490 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10491 (defun gnus-summary-tick-article (&optional article clear-mark)
10492   "Mark current article as unread.
10493 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10494 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10495   (interactive)
10496   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10497                                        gnus-ticked-mark)))
10498
10499 (defun gnus-summary-mark-as-read-forward (n)
10500   "Mark N articles as read forwards.
10501 If N is negative, mark backwards instead.
10502 The difference between N and the actual number of articles marked is
10503 returned."
10504   (interactive "p")
10505   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10506
10507 (defun gnus-summary-mark-as-read-backward (n)
10508   "Mark the N articles as read backwards.
10509 The difference between N and the actual number of articles marked is
10510 returned."
10511   (interactive "p")
10512   (gnus-summary-mark-forward
10513    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10514
10515 (defun gnus-summary-mark-as-read (&optional article mark)
10516   "Mark current article as read.
10517 ARTICLE specifies the article to be marked as read.
10518 MARK specifies a string to be inserted at the beginning of the line."
10519   (gnus-summary-mark-article article mark))
10520
10521 (defun gnus-summary-clear-mark-forward (n)
10522   "Clear marks from N articles forward.
10523 If N is negative, clear backward instead.
10524 The difference between N and the number of marks cleared is returned."
10525   (interactive "p")
10526   (gnus-summary-mark-forward n gnus-unread-mark))
10527
10528 (defun gnus-summary-clear-mark-backward (n)
10529   "Clear marks from N articles backward.
10530 The difference between N and the number of marks cleared is returned."
10531   (interactive "p")
10532   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10533
10534 (defun gnus-summary-mark-unread-as-read ()
10535   "Intended to be used by `gnus-summary-mark-article-hook'."
10536   (when (memq gnus-current-article gnus-newsgroup-unreads)
10537     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10538
10539 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10540   "Intended to be used by `gnus-summary-mark-article-hook'."
10541   (let ((mark (gnus-summary-article-mark)))
10542     (when (or (gnus-unread-mark-p mark)
10543               (gnus-read-mark-p mark))
10544       (gnus-summary-mark-article gnus-current-article
10545                                  (or new-mark gnus-read-mark)))))
10546
10547 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10548   "Intended to be used by `gnus-summary-mark-article-hook'."
10549   (let ((mark (gnus-summary-article-mark)))
10550     (when (or (gnus-unread-mark-p mark)
10551               (gnus-read-mark-p mark))
10552       (gnus-summary-mark-article (gnus-summary-article-number)
10553                                  (or new-mark gnus-read-mark)))))
10554
10555 (defun gnus-summary-mark-unread-as-ticked ()
10556   "Intended to be used by `gnus-summary-mark-article-hook'."
10557   (when (memq gnus-current-article gnus-newsgroup-unreads)
10558     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10559
10560 (defun gnus-summary-mark-region-as-read (point mark all)
10561   "Mark all unread articles between point and mark as read.
10562 If given a prefix, mark all articles between point and mark as read,
10563 even ticked and dormant ones."
10564   (interactive "r\nP")
10565   (save-excursion
10566     (let (article)
10567       (goto-char point)
10568       (beginning-of-line)
10569       (while (and
10570               (< (point) mark)
10571               (progn
10572                 (when (or all
10573                           (memq (setq article (gnus-summary-article-number))
10574                                 gnus-newsgroup-unreads))
10575                   (gnus-summary-mark-article article gnus-del-mark))
10576                 t)
10577               (gnus-summary-find-next))))))
10578
10579 (defun gnus-summary-mark-below (score mark)
10580   "Mark articles with score less than SCORE with MARK."
10581   (interactive "P\ncMark: ")
10582   (setq score (if score
10583                   (prefix-numeric-value score)
10584                 (or gnus-summary-default-score 0)))
10585   (save-excursion
10586     (set-buffer gnus-summary-buffer)
10587     (goto-char (point-min))
10588     (while
10589         (progn
10590           (and (< (gnus-summary-article-score) score)
10591                (gnus-summary-mark-article nil mark))
10592           (gnus-summary-find-next)))))
10593
10594 (defun gnus-summary-kill-below (&optional score)
10595   "Mark articles with score below SCORE as read."
10596   (interactive "P")
10597   (gnus-summary-mark-below score gnus-killed-mark))
10598
10599 (defun gnus-summary-clear-above (&optional score)
10600   "Clear all marks from articles with score above SCORE."
10601   (interactive "P")
10602   (gnus-summary-mark-above score gnus-unread-mark))
10603
10604 (defun gnus-summary-tick-above (&optional score)
10605   "Tick all articles with score above SCORE."
10606   (interactive "P")
10607   (gnus-summary-mark-above score gnus-ticked-mark))
10608
10609 (defun gnus-summary-mark-above (score mark)
10610   "Mark articles with score over SCORE with MARK."
10611   (interactive "P\ncMark: ")
10612   (setq score (if score
10613                   (prefix-numeric-value score)
10614                 (or gnus-summary-default-score 0)))
10615   (save-excursion
10616     (set-buffer gnus-summary-buffer)
10617     (goto-char (point-min))
10618     (while (and (progn
10619                   (when (> (gnus-summary-article-score) score)
10620                     (gnus-summary-mark-article nil mark))
10621                   t)
10622                 (gnus-summary-find-next)))))
10623
10624 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10625 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10626 (defun gnus-summary-limit-include-expunged (&optional no-error)
10627   "Display all the hidden articles that were expunged for low scores."
10628   (interactive)
10629   (let ((buffer-read-only nil))
10630     (let ((scored gnus-newsgroup-scored)
10631           headers h)
10632       (while scored
10633         (unless (gnus-summary-article-header (caar scored))
10634           (and (setq h (gnus-number-to-header (caar scored)))
10635                (< (cdar scored) gnus-summary-expunge-below)
10636                (push h headers)))
10637         (setq scored (cdr scored)))
10638       (if (not headers)
10639           (when (not no-error)
10640             (error "No expunged articles hidden"))
10641         (goto-char (point-min))
10642         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10643         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10644         (mapcar (lambda (x) (push (mail-header-number x)
10645                                   gnus-newsgroup-limit))
10646                 headers)
10647         (gnus-summary-prepare-unthreaded (nreverse headers))
10648         (goto-char (point-min))
10649         (gnus-summary-position-point)
10650         t))))
10651
10652 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10653   "Mark all unread articles in this newsgroup as read.
10654 If prefix argument ALL is non-nil, ticked and dormant articles will
10655 also be marked as read.
10656 If QUIETLY is non-nil, no questions will be asked.
10657
10658 If TO-HERE is non-nil, it should be a point in the buffer.  All
10659 articles before (after, if REVERSE is set) this point will be marked
10660 as read.
10661
10662 Note that this function will only catch up the unread article
10663 in the current summary buffer limitation.
10664
10665 The number of articles marked as read is returned."
10666   (interactive "P")
10667   (prog1
10668       (save-excursion
10669         (when (or quietly
10670                   (not gnus-interactive-catchup) ;Without confirmation?
10671                   gnus-expert-user
10672                   (gnus-y-or-n-p
10673                    (if all
10674                        "Mark absolutely all articles as read? "
10675                      "Mark all unread articles as read? ")))
10676           (if (and not-mark
10677                    (not gnus-newsgroup-adaptive)
10678                    (not gnus-newsgroup-auto-expire)
10679                    (not gnus-suppress-duplicates)
10680                    (or (not gnus-use-cache)
10681                        (eq gnus-use-cache 'passive)))
10682               (progn
10683                 (when all
10684                   (setq gnus-newsgroup-marked nil
10685                         gnus-newsgroup-spam-marked nil
10686                         gnus-newsgroup-dormant nil))
10687                 (setq gnus-newsgroup-unreads
10688                       (gnus-sorted-nunion
10689                        (gnus-intersection gnus-newsgroup-unreads
10690                                           gnus-newsgroup-downloadable)
10691                        gnus-newsgroup-unfetched)))
10692             ;; We actually mark all articles as canceled, which we
10693             ;; have to do when using auto-expiry or adaptive scoring.
10694             (gnus-summary-show-all-threads)
10695             (if (and to-here reverse)
10696                 (progn
10697                   (goto-char to-here)
10698                   (gnus-summary-mark-current-read-and-unread-as-read
10699                    gnus-catchup-mark)
10700                   (while (gnus-summary-find-next (not all))
10701                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10702               (when (gnus-summary-first-subject (not all))
10703                 (while (and
10704                         (if to-here (< (point) to-here) t)
10705                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10706                         (gnus-summary-find-next (not all))))))
10707             (gnus-set-mode-line 'summary))
10708           t))
10709     (gnus-summary-position-point)))
10710
10711 (defun gnus-summary-catchup-to-here (&optional all)
10712   "Mark all unticked articles before the current one as read.
10713 If ALL is non-nil, also mark ticked and dormant articles as read."
10714   (interactive "P")
10715   (save-excursion
10716     (gnus-save-hidden-threads
10717       (let ((beg (point)))
10718         ;; We check that there are unread articles.
10719         (when (or all (gnus-summary-find-prev))
10720           (gnus-summary-catchup all t beg)))))
10721   (gnus-summary-position-point))
10722
10723 (defun gnus-summary-catchup-from-here (&optional all)
10724   "Mark all unticked articles after (and including) the current one as read.
10725 If ALL is non-nil, also mark ticked and dormant articles as read."
10726   (interactive "P")
10727   (save-excursion
10728     (gnus-save-hidden-threads
10729       (let ((beg (point)))
10730         ;; We check that there are unread articles.
10731         (when (or all (gnus-summary-find-next))
10732           (gnus-summary-catchup all t beg nil t)))))
10733   (gnus-summary-position-point))
10734
10735 (defun gnus-summary-catchup-all (&optional quietly)
10736   "Mark all articles in this newsgroup as read.
10737 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10738 instead, which marks only unread articles as read."
10739   (interactive "P")
10740   (gnus-summary-catchup t quietly))
10741
10742 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10743   "Mark all unread articles in this group as read, then exit.
10744 If prefix argument ALL is non-nil, all articles are marked as read.
10745 If QUIETLY is non-nil, no questions will be asked."
10746   (interactive "P")
10747   (when (gnus-summary-catchup all quietly nil 'fast)
10748     ;; Select next newsgroup or exit.
10749     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10750              (eq gnus-auto-select-next 'quietly))
10751         (gnus-summary-next-group nil)
10752       (gnus-summary-exit))))
10753
10754 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10755   "Mark all articles in this newsgroup as read, and then exit.
10756 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10757 instead, which marks only unread articles as read."
10758   (interactive "P")
10759   (gnus-summary-catchup-and-exit t quietly))
10760
10761 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10762   "Mark all articles in this group as read and select the next group.
10763 If given a prefix, mark all articles, unread as well as ticked, as
10764 read."
10765   (interactive "P")
10766   (save-excursion
10767     (gnus-summary-catchup all))
10768   (gnus-summary-next-group))
10769
10770 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10771   "Mark all articles in this group as read and select the previous group.
10772 If given a prefix, mark all articles, unread as well as ticked, as
10773 read."
10774   (interactive "P")
10775   (save-excursion
10776     (gnus-summary-catchup all))
10777   (gnus-summary-next-group nil nil t))
10778
10779 ;;;
10780 ;;; with article
10781 ;;;
10782
10783 (defmacro gnus-with-article (article &rest forms)
10784   "Select ARTICLE and perform FORMS in the original article buffer.
10785 Then replace the article with the result."
10786   `(progn
10787      ;; We don't want the article to be marked as read.
10788      (let (gnus-mark-article-hook)
10789        (gnus-summary-select-article t t nil ,article))
10790      (set-buffer gnus-original-article-buffer)
10791      ,@forms
10792      (if (not (gnus-check-backend-function
10793                'request-replace-article (car gnus-article-current)))
10794          (gnus-message 5 "Read-only group; not replacing")
10795        (unless (gnus-request-replace-article
10796                 ,article (car gnus-article-current)
10797                 (current-buffer) t)
10798          (error "Couldn't replace article")))
10799      ;; The cache and backlog have to be flushed somewhat.
10800      (when gnus-keep-backlog
10801        (gnus-backlog-remove-article
10802         (car gnus-article-current) (cdr gnus-article-current)))
10803      (when gnus-use-cache
10804        (gnus-cache-update-article
10805         (car gnus-article-current) (cdr gnus-article-current)))))
10806
10807 (put 'gnus-with-article 'lisp-indent-function 1)
10808 (put 'gnus-with-article 'edebug-form-spec '(form body))
10809
10810 ;; Thread-based commands.
10811
10812 (defun gnus-summary-articles-in-thread (&optional article)
10813   "Return a list of all articles in the current thread.
10814 If ARTICLE is non-nil, return all articles in the thread that starts
10815 with that article."
10816   (let* ((article (or article (gnus-summary-article-number)))
10817          (data (gnus-data-find-list article))
10818          (top-level (gnus-data-level (car data)))
10819          (top-subject
10820           (cond ((null gnus-thread-operation-ignore-subject)
10821                  (gnus-simplify-subject-re
10822                   (mail-header-subject (gnus-data-header (car data)))))
10823                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10824                  (gnus-simplify-subject-fuzzy
10825                   (mail-header-subject (gnus-data-header (car data)))))
10826                 (t nil)))
10827          (end-point (save-excursion
10828                       (if (gnus-summary-go-to-next-thread)
10829                           (point) (point-max))))
10830          articles)
10831     (while (and data
10832                 (< (gnus-data-pos (car data)) end-point))
10833       (when (or (not top-subject)
10834                 (string= top-subject
10835                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10836                              (gnus-simplify-subject-fuzzy
10837                               (mail-header-subject
10838                                (gnus-data-header (car data))))
10839                            (gnus-simplify-subject-re
10840                             (mail-header-subject
10841                              (gnus-data-header (car data)))))))
10842         (push (gnus-data-number (car data)) articles))
10843       (unless (and (setq data (cdr data))
10844                    (> (gnus-data-level (car data)) top-level))
10845         (setq data nil)))
10846     ;; Return the list of articles.
10847     (nreverse articles)))
10848
10849 (defun gnus-summary-rethread-current ()
10850   "Rethread the thread the current article is part of."
10851   (interactive)
10852   (let* ((gnus-show-threads t)
10853          (article (gnus-summary-article-number))
10854          (id (mail-header-id (gnus-summary-article-header)))
10855          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10856     (unless id
10857       (error "No article on the current line"))
10858     (gnus-rebuild-thread id)
10859     (gnus-summary-goto-subject article)))
10860
10861 (defun gnus-summary-reparent-thread ()
10862   "Make the current article child of the marked (or previous) article.
10863
10864 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10865 is non-nil or the Subject: of both articles are the same."
10866   (interactive)
10867   (unless (not (gnus-group-read-only-p))
10868     (error "The current newsgroup does not support article editing"))
10869   (unless (<= (length gnus-newsgroup-processable) 1)
10870     (error "No more than one article may be marked"))
10871   (let ((child (gnus-summary-article-number))
10872         ;; First grab the marked article, otherwise one line up.
10873         (parent (if (not (null gnus-newsgroup-processable))
10874                     (car gnus-newsgroup-processable)
10875                   (save-excursion
10876                     (if (eq (forward-line -1) 0)
10877                         (gnus-summary-article-number)
10878                       (error "Beginning of summary buffer"))))))
10879     (gnus-summary-reparent-children parent (list child))))
10880
10881 (defun gnus-summary-reparent-children (parent children)
10882   "Make PARENT the parent of CHILDREN.
10883 When called interactively, PARENT is is current article and
10884 CHILDREN are the process-marked articles."
10885   (interactive
10886    (list (gnus-summary-article-number)
10887          (gnus-summary-work-articles nil)))
10888   (dolist (child children)
10889     (save-window-excursion
10890       (let ((gnus-article-buffer " *reparent*"))
10891         (unless (not (eq parent child))
10892           (error "An article may not be self-referential"))
10893         (let ((message-id (mail-header-id
10894                            (gnus-summary-article-header parent))))
10895           (unless (and message-id (not (equal message-id "")))
10896             (error "No message-id in desired parent"))
10897           (gnus-with-article child
10898             (save-restriction
10899               (goto-char (point-min))
10900               (message-narrow-to-head)
10901               (if (re-search-forward "^References: " nil t)
10902                   (progn
10903                     (re-search-forward "^[^ \t]" nil t)
10904                     (forward-line -1)
10905                     (end-of-line)
10906                     (insert " " message-id))
10907                 (insert "References: " message-id "\n"))))
10908           (set-buffer gnus-summary-buffer)
10909           (gnus-summary-unmark-all-processable)
10910           (gnus-summary-update-article child)
10911           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10912             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10913           (gnus-summary-rethread-current)
10914           (gnus-message 3 "Article %d is now the child of article %d"
10915                         child parent))))))
10916
10917 (defun gnus-summary-toggle-threads (&optional arg)
10918   "Toggle showing conversation threads.
10919 If ARG is positive number, turn showing conversation threads on."
10920   (interactive "P")
10921   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10922     (setq gnus-show-threads
10923           (if (null arg) (not gnus-show-threads)
10924             (> (prefix-numeric-value arg) 0)))
10925     (gnus-summary-prepare)
10926     (gnus-summary-goto-subject current)
10927     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10928     (gnus-summary-position-point)))
10929
10930 (defun gnus-summary-show-all-threads ()
10931   "Show all threads."
10932   (interactive)
10933   (save-excursion
10934     (let ((buffer-read-only nil))
10935       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10936   (gnus-summary-position-point))
10937
10938 (defun gnus-summary-show-thread ()
10939   "Show thread subtrees.
10940 Returns nil if no thread was there to be shown."
10941   (interactive)
10942   (let ((buffer-read-only nil)
10943         (orig (point))
10944         (end (point-at-eol))
10945         ;; Leave point at bol
10946         (beg (progn (beginning-of-line) (point))))
10947     (prog1
10948         ;; Any hidden lines here?
10949         (search-forward "\r" end t)
10950       (subst-char-in-region beg end ?\^M ?\n t)
10951       (goto-char orig)
10952       (gnus-summary-position-point))))
10953
10954 (defun gnus-summary-maybe-hide-threads ()
10955   "If requested, hide the threads that should be hidden."
10956   (when (and gnus-show-threads
10957              gnus-thread-hide-subtree)
10958     (gnus-summary-hide-all-threads
10959      (if (or (consp gnus-thread-hide-subtree)
10960              (functionp gnus-thread-hide-subtree))
10961          (gnus-make-predicate gnus-thread-hide-subtree)
10962        nil))))
10963
10964 ;;; Hiding predicates.
10965
10966 (defun gnus-article-unread-p (header)
10967   (memq (mail-header-number header) gnus-newsgroup-unreads))
10968
10969 (defun gnus-article-unseen-p (header)
10970   (memq (mail-header-number header) gnus-newsgroup-unseen))
10971
10972 (defun gnus-map-articles (predicate articles)
10973   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10974   (apply 'gnus-or (mapcar predicate
10975                           (mapcar (lambda (number)
10976                                     (gnus-summary-article-header number))
10977                                   articles))))
10978
10979 (defun gnus-summary-hide-all-threads (&optional predicate)
10980   "Hide all thread subtrees.
10981 If PREDICATE is supplied, threads that satisfy this predicate
10982 will not be hidden."
10983   (interactive)
10984   (save-excursion
10985     (goto-char (point-min))
10986     (let ((end nil))
10987       (while (not end)
10988         (when (or (not predicate)
10989                   (gnus-map-articles
10990                    predicate (gnus-summary-article-children)))
10991             (gnus-summary-hide-thread))
10992         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10993   (gnus-summary-position-point))
10994
10995 (defun gnus-summary-hide-thread ()
10996   "Hide thread subtrees.
10997 If PREDICATE is supplied, threads that satisfy this predicate
10998 will not be hidden.
10999 Returns nil if no threads were there to be hidden."
11000   (interactive)
11001   (let ((buffer-read-only nil)
11002         (start (point))
11003         (article (gnus-summary-article-number)))
11004     (goto-char start)
11005     ;; Go forward until either the buffer ends or the subthread
11006     ;; ends.
11007     (when (and (not (eobp))
11008                (or (zerop (gnus-summary-next-thread 1 t))
11009                    (goto-char (point-max))))
11010       (prog1
11011           (if (and (> (point) start)
11012                    (search-backward "\n" start t))
11013               (progn
11014                 (subst-char-in-region start (point) ?\n ?\^M)
11015                 (gnus-summary-goto-subject article))
11016             (goto-char start)
11017             nil)))))
11018
11019 (defun gnus-summary-go-to-next-thread (&optional previous)
11020   "Go to the same level (or less) next thread.
11021 If PREVIOUS is non-nil, go to previous thread instead.
11022 Return the article number moved to, or nil if moving was impossible."
11023   (let ((level (gnus-summary-thread-level))
11024         (way (if previous -1 1))
11025         (beg (point)))
11026     (forward-line way)
11027     (while (and (not (eobp))
11028                 (< level (gnus-summary-thread-level)))
11029       (forward-line way))
11030     (if (eobp)
11031         (progn
11032           (goto-char beg)
11033           nil)
11034       (setq beg (point))
11035       (prog1
11036           (gnus-summary-article-number)
11037         (goto-char beg)))))
11038
11039 (defun gnus-summary-next-thread (n &optional silent)
11040   "Go to the same level next N'th thread.
11041 If N is negative, search backward instead.
11042 Returns the difference between N and the number of skips actually
11043 done.
11044
11045 If SILENT, don't output messages."
11046   (interactive "p")
11047   (let ((backward (< n 0))
11048         (n (abs n)))
11049     (while (and (> n 0)
11050                 (gnus-summary-go-to-next-thread backward))
11051       (decf n))
11052     (unless silent
11053       (gnus-summary-position-point))
11054     (when (and (not silent) (/= 0 n))
11055       (gnus-message 7 "No more threads"))
11056     n))
11057
11058 (defun gnus-summary-prev-thread (n)
11059   "Go to the same level previous N'th thread.
11060 Returns the difference between N and the number of skips actually
11061 done."
11062   (interactive "p")
11063   (gnus-summary-next-thread (- n)))
11064
11065 (defun gnus-summary-go-down-thread ()
11066   "Go down one level in the current thread."
11067   (let ((children (gnus-summary-article-children)))
11068     (when children
11069       (gnus-summary-goto-subject (car children)))))
11070
11071 (defun gnus-summary-go-up-thread ()
11072   "Go up one level in the current thread."
11073   (let ((parent (gnus-summary-article-parent)))
11074     (when parent
11075       (gnus-summary-goto-subject parent))))
11076
11077 (defun gnus-summary-down-thread (n)
11078   "Go down thread N steps.
11079 If N is negative, go up instead.
11080 Returns the difference between N and how many steps down that were
11081 taken."
11082   (interactive "p")
11083   (let ((up (< n 0))
11084         (n (abs n)))
11085     (while (and (> n 0)
11086                 (if up (gnus-summary-go-up-thread)
11087                   (gnus-summary-go-down-thread)))
11088       (setq n (1- n)))
11089     (gnus-summary-position-point)
11090     (when (/= 0 n)
11091       (gnus-message 7 "Can't go further"))
11092     n))
11093
11094 (defun gnus-summary-up-thread (n)
11095   "Go up thread N steps.
11096 If N is negative, go down instead.
11097 Returns the difference between N and how many steps down that were
11098 taken."
11099   (interactive "p")
11100   (gnus-summary-down-thread (- n)))
11101
11102 (defun gnus-summary-top-thread ()
11103   "Go to the top of the thread."
11104   (interactive)
11105   (while (gnus-summary-go-up-thread))
11106   (gnus-summary-article-number))
11107
11108 (defun gnus-summary-kill-thread (&optional unmark)
11109   "Mark articles under current thread as read.
11110 If the prefix argument is positive, remove any kinds of marks.
11111 If the prefix argument is negative, tick articles instead."
11112   (interactive "P")
11113   (when unmark
11114     (setq unmark (prefix-numeric-value unmark)))
11115   (let ((articles (gnus-summary-articles-in-thread)))
11116     (save-excursion
11117       ;; Expand the thread.
11118       (gnus-summary-show-thread)
11119       ;; Mark all the articles.
11120       (while articles
11121         (gnus-summary-goto-subject (car articles))
11122         (cond ((null unmark)
11123                (gnus-summary-mark-article-as-read gnus-killed-mark))
11124               ((> unmark 0)
11125                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11126               (t
11127                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11128         (setq articles (cdr articles))))
11129     ;; Hide killed subtrees.
11130     (and (null unmark)
11131          gnus-thread-hide-killed
11132          (gnus-summary-hide-thread))
11133     ;; If marked as read, go to next unread subject.
11134     (when (null unmark)
11135       ;; Go to next unread subject.
11136       (gnus-summary-next-subject 1 t)))
11137   (gnus-set-mode-line 'summary))
11138
11139 ;; Summary sorting commands
11140
11141 (defun gnus-summary-sort-by-number (&optional reverse)
11142   "Sort the summary buffer by article number.
11143 Argument REVERSE means reverse order."
11144   (interactive "P")
11145   (gnus-summary-sort 'number reverse))
11146
11147 (defun gnus-summary-sort-by-random (&optional reverse)
11148   "Randomize the order in the summary buffer.
11149 Argument REVERSE means to randomize in reverse order."
11150   (interactive "P")
11151   (gnus-summary-sort 'random reverse))
11152
11153 (defun gnus-summary-sort-by-author (&optional reverse)
11154   "Sort the summary buffer by author name alphabetically.
11155 If `case-fold-search' is non-nil, case of letters is ignored.
11156 Argument REVERSE means reverse order."
11157   (interactive "P")
11158   (gnus-summary-sort 'author reverse))
11159
11160 (defun gnus-summary-sort-by-recipient (&optional reverse)
11161   "Sort the summary buffer by recipient name alphabetically.
11162 If `case-fold-search' is non-nil, case of letters is ignored.
11163 Argument REVERSE means reverse order."
11164   (interactive "P")
11165   (gnus-summary-sort 'recipient reverse))
11166
11167 (defun gnus-summary-sort-by-subject (&optional reverse)
11168   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11169 If `case-fold-search' is non-nil, case of letters is ignored.
11170 Argument REVERSE means reverse order."
11171   (interactive "P")
11172   (gnus-summary-sort 'subject reverse))
11173
11174 (defun gnus-summary-sort-by-date (&optional reverse)
11175   "Sort the summary buffer by date.
11176 Argument REVERSE means reverse order."
11177   (interactive "P")
11178   (gnus-summary-sort 'date reverse))
11179
11180 (defun gnus-summary-sort-by-score (&optional reverse)
11181   "Sort the summary buffer by score.
11182 Argument REVERSE means reverse order."
11183   (interactive "P")
11184   (gnus-summary-sort 'score reverse))
11185
11186 (defun gnus-summary-sort-by-lines (&optional reverse)
11187   "Sort the summary buffer by the number of lines.
11188 Argument REVERSE means reverse order."
11189   (interactive "P")
11190   (gnus-summary-sort 'lines reverse))
11191
11192 (defun gnus-summary-sort-by-chars (&optional reverse)
11193   "Sort the summary buffer by article length.
11194 Argument REVERSE means reverse order."
11195   (interactive "P")
11196   (gnus-summary-sort 'chars reverse))
11197
11198 (defun gnus-summary-sort-by-original (&optional reverse)
11199   "Sort the summary buffer using the default sorting method.
11200 Argument REVERSE means reverse order."
11201   (interactive "P")
11202   (let* ((buffer-read-only)
11203          (gnus-summary-prepare-hook nil))
11204     ;; We do the sorting by regenerating the threads.
11205     (gnus-summary-prepare)
11206     ;; Hide subthreads if needed.
11207     (gnus-summary-maybe-hide-threads)))
11208
11209 (defun gnus-summary-sort (predicate reverse)
11210   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11211   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11212          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11213          (gnus-thread-sort-functions
11214           (if (not reverse)
11215               thread
11216             `(lambda (t1 t2)
11217                (,thread t2 t1))))
11218          (gnus-sort-gathered-threads-function
11219           gnus-thread-sort-functions)
11220          (gnus-article-sort-functions
11221           (if (not reverse)
11222               article
11223             `(lambda (t1 t2)
11224                (,article t2 t1))))
11225          (buffer-read-only)
11226          (gnus-summary-prepare-hook nil))
11227     ;; We do the sorting by regenerating the threads.
11228     (gnus-summary-prepare)
11229     ;; Hide subthreads if needed.
11230     (gnus-summary-maybe-hide-threads)))
11231
11232 ;; Summary saving commands.
11233
11234 (defun gnus-summary-save-article (&optional n not-saved)
11235   "Save the current article using the default saver function.
11236 If N is a positive number, save the N next articles.
11237 If N is a negative number, save the N previous articles.
11238 If N is nil and any articles have been marked with the process mark,
11239 save those articles instead.
11240 The variable `gnus-default-article-saver' specifies the saver function."
11241   (interactive "P")
11242   (let* ((articles (gnus-summary-work-articles n))
11243          (save-buffer (save-excursion
11244                         (nnheader-set-temp-buffer " *Gnus Save*")))
11245          (num (length articles))
11246          header file)
11247     (dolist (article articles)
11248       (setq header (gnus-summary-article-header article))
11249       (if (not (vectorp header))
11250           ;; This is a pseudo-article.
11251           (if (assq 'name header)
11252               (gnus-copy-file (cdr (assq 'name header)))
11253             (gnus-message 1 "Article %d is unsaveable" article))
11254         ;; This is a real article.
11255         (save-window-excursion
11256           (let ((gnus-display-mime-function nil)
11257                 (gnus-article-prepare-hook nil))
11258             (gnus-summary-select-article t nil nil article)))
11259         (save-excursion
11260           (set-buffer save-buffer)
11261           (erase-buffer)
11262           (insert-buffer-substring gnus-original-article-buffer))
11263         (setq file (gnus-article-save save-buffer file num))
11264         (gnus-summary-remove-process-mark article)
11265         (unless not-saved
11266           (gnus-summary-set-saved-mark article))))
11267     (gnus-kill-buffer save-buffer)
11268     (gnus-summary-position-point)
11269     (gnus-set-mode-line 'summary)
11270     n))
11271
11272 (defun gnus-summary-pipe-output (&optional arg headers)
11273   "Pipe the current article to a subprocess.
11274 If N is a positive number, pipe the N next articles.
11275 If N is a negative number, pipe the N previous articles.
11276 If N is nil and any articles have been marked with the process mark,
11277 pipe those articles instead.
11278 If HEADERS (the symbolic prefix), include the headers, too."
11279   (interactive (gnus-interactive "P\ny"))
11280   (require 'gnus-art)
11281   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11282         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11283     (gnus-summary-save-article arg t))
11284   (let ((buffer (get-buffer "*Shell Command Output*")))
11285     (when (and buffer
11286                (not (zerop (buffer-size buffer))))
11287       (gnus-configure-windows 'pipe))))
11288
11289 (defun gnus-summary-save-article-mail (&optional arg)
11290   "Append the current article to an mail file.
11291 If N is a positive number, save the N next articles.
11292 If N is a negative number, save the N previous articles.
11293 If N is nil and any articles have been marked with the process mark,
11294 save those articles instead."
11295   (interactive "P")
11296   (require 'gnus-art)
11297   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11298     (gnus-summary-save-article arg)))
11299
11300 (defun gnus-summary-save-article-rmail (&optional arg)
11301   "Append the current article to an rmail file.
11302 If N is a positive number, save the N next articles.
11303 If N is a negative number, save the N previous articles.
11304 If N is nil and any articles have been marked with the process mark,
11305 save those articles instead."
11306   (interactive "P")
11307   (require 'gnus-art)
11308   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11309     (gnus-summary-save-article arg)))
11310
11311 (defun gnus-summary-save-article-file (&optional arg)
11312   "Append the current article to a file.
11313 If N is a positive number, save the N next articles.
11314 If N is a negative number, save the N previous articles.
11315 If N is nil and any articles have been marked with the process mark,
11316 save those articles instead."
11317   (interactive "P")
11318   (require 'gnus-art)
11319   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11320     (gnus-summary-save-article arg)))
11321
11322 (defun gnus-summary-write-article-file (&optional arg)
11323   "Write the current article to a file, deleting the previous file.
11324 If N is a positive number, save the N next articles.
11325 If N is a negative number, save the N previous articles.
11326 If N is nil and any articles have been marked with the process mark,
11327 save those articles instead."
11328   (interactive "P")
11329   (require 'gnus-art)
11330   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11331     (gnus-summary-save-article arg)))
11332
11333 (defun gnus-summary-save-article-body-file (&optional arg)
11334   "Append the current article body to a file.
11335 If N is a positive number, save the N next articles.
11336 If N is a negative number, save the N previous articles.
11337 If N is nil and any articles have been marked with the process mark,
11338 save those articles instead."
11339   (interactive "P")
11340   (require 'gnus-art)
11341   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11342     (gnus-summary-save-article arg)))
11343
11344 (defun gnus-summary-muttprint (&optional arg)
11345   "Print the current article using Muttprint.
11346 If N is a positive number, save the N next articles.
11347 If N is a negative number, save the N previous articles.
11348 If N is nil and any articles have been marked with the process mark,
11349 save those articles instead."
11350   (interactive "P")
11351   (require 'gnus-art)
11352   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11353     (gnus-summary-save-article arg t)))
11354
11355 (defun gnus-summary-pipe-message (program)
11356   "Pipe the current article through PROGRAM."
11357   (interactive "sProgram: ")
11358   (gnus-summary-select-article)
11359   (let ((mail-header-separator ""))
11360     (gnus-eval-in-buffer-window gnus-article-buffer
11361       (save-restriction
11362         (widen)
11363         (let ((start (window-start))
11364               buffer-read-only)
11365           (message-pipe-buffer-body program)
11366           (set-window-start (get-buffer-window (current-buffer)) start))))))
11367
11368 (defun gnus-get-split-value (methods)
11369   "Return a value based on the split METHODS."
11370   (let (split-name method result match)
11371     (when methods
11372       (save-excursion
11373         (set-buffer gnus-original-article-buffer)
11374         (save-restriction
11375           (nnheader-narrow-to-headers)
11376           (while (and methods (not split-name))
11377             (goto-char (point-min))
11378             (setq method (pop methods))
11379             (setq match (car method))
11380             (when (cond
11381                    ((stringp match)
11382                     ;; Regular expression.
11383                     (ignore-errors
11384                       (re-search-forward match nil t)))
11385                    ((functionp match)
11386                     ;; Function.
11387                     (save-restriction
11388                       (widen)
11389                       (setq result (funcall match gnus-newsgroup-name))))
11390                    ((consp match)
11391                     ;; Form.
11392                     (save-restriction
11393                       (widen)
11394                       (setq result (eval match)))))
11395               (setq split-name (cdr method))
11396               (cond ((stringp result)
11397                      (push (expand-file-name
11398                             result gnus-article-save-directory)
11399                            split-name))
11400                     ((consp result)
11401                      (setq split-name (append result split-name)))))))))
11402     (nreverse split-name)))
11403
11404 (defun gnus-valid-move-group-p (group)
11405   (and (boundp group)
11406        (symbol-name group)
11407        (symbol-value group)
11408        (gnus-get-function (gnus-find-method-for-group
11409                            (symbol-name group)) 'request-accept-article t)))
11410
11411 (defun gnus-read-move-group-name (prompt default articles prefix)
11412   "Read a group name."
11413   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11414          (minibuffer-confirm-incomplete nil) ; XEmacs
11415          (prom
11416           (format "%s %s to"
11417                   prompt
11418                   (if (> (length articles) 1)
11419                       (format "these %d articles" (length articles))
11420                     "this article")))
11421          (to-newsgroup
11422           (cond
11423            ((null split-name)
11424             (gnus-completing-read-with-default
11425              default prom
11426              gnus-active-hashtb
11427              'gnus-valid-move-group-p
11428              nil prefix
11429              'gnus-group-history))
11430            ((= 1 (length split-name))
11431             (gnus-completing-read-with-default
11432              (car split-name) prom
11433              gnus-active-hashtb
11434              'gnus-valid-move-group-p
11435              nil nil
11436              'gnus-group-history))
11437            (t
11438             (gnus-completing-read-with-default
11439              nil prom
11440              (mapcar 'list (nreverse split-name))
11441              nil nil nil
11442              'gnus-group-history))))
11443          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11444     (when to-newsgroup
11445       (if (or (string= to-newsgroup "")
11446               (string= to-newsgroup prefix))
11447           (setq to-newsgroup default))
11448       (unless to-newsgroup
11449         (error "No group name entered"))
11450       (or (gnus-active to-newsgroup)
11451           (gnus-activate-group to-newsgroup nil nil to-method)
11452           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11453                                      to-newsgroup))
11454               (or (and (gnus-request-create-group to-newsgroup to-method)
11455                        (gnus-activate-group
11456                         to-newsgroup nil nil to-method)
11457                        (gnus-subscribe-group to-newsgroup))
11458                   (error "Couldn't create group %s" to-newsgroup)))
11459           (error "No such group: %s" to-newsgroup)))
11460     to-newsgroup))
11461
11462 (defvar gnus-summary-save-parts-counter)
11463
11464 (defun gnus-summary-save-parts (type dir n &optional reverse)
11465   "Save parts matching TYPE to DIR.
11466 If REVERSE, save parts that do not match TYPE."
11467   (interactive
11468    (list (read-string "Save parts of type: "
11469                       (or (car gnus-summary-save-parts-type-history)
11470                           gnus-summary-save-parts-default-mime)
11471                       'gnus-summary-save-parts-type-history)
11472          (setq gnus-summary-save-parts-last-directory
11473                (read-file-name "Save to directory: "
11474                                gnus-summary-save-parts-last-directory
11475                                nil t))
11476          current-prefix-arg))
11477   (gnus-summary-iterate n
11478     (let ((gnus-display-mime-function nil)
11479           (gnus-inhibit-treatment t))
11480       (gnus-summary-select-article))
11481     (save-excursion
11482       (set-buffer gnus-article-buffer)
11483       (let ((handles (or gnus-article-mime-handles
11484                          (mm-dissect-buffer nil gnus-article-loose-mime)
11485                          (and gnus-article-emulate-mime
11486                               (mm-uu-dissect))))
11487             (gnus-summary-save-parts-counter 1))
11488         (when handles
11489           (gnus-summary-save-parts-1 type dir handles reverse)
11490           (unless gnus-article-mime-handles ;; Don't destroy this case.
11491             (mm-destroy-parts handles)))))))
11492
11493 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11494   (if (stringp (car handle))
11495       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11496               (cdr handle))
11497     (when (if reverse
11498               (not (string-match type (mm-handle-media-type handle)))
11499             (string-match type (mm-handle-media-type handle)))
11500       (let ((file (expand-file-name
11501                    (gnus-map-function
11502                     mm-file-name-rewrite-functions
11503                     (file-name-nondirectory
11504                      (or
11505                       (mail-content-type-get
11506                        (mm-handle-disposition handle) 'filename)
11507                       (mail-content-type-get
11508                        (mm-handle-type handle) 'name)
11509                       (format "%s.%d.%d" gnus-newsgroup-name
11510                               (cdr gnus-article-current)
11511                               gnus-summary-save-parts-counter))))
11512                    dir)))
11513         (incf gnus-summary-save-parts-counter)
11514         (unless (file-exists-p file)
11515           (mm-save-part-to-file handle file))))))
11516
11517 ;; Summary extract commands
11518
11519 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11520   (let ((buffer-read-only nil)
11521         (article (gnus-summary-article-number))
11522         after-article b e)
11523     (unless (gnus-summary-goto-subject article)
11524       (error "No such article: %d" article))
11525     (gnus-summary-position-point)
11526     ;; If all commands are to be bunched up on one line, we collect
11527     ;; them here.
11528     (unless gnus-view-pseudos-separately
11529       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11530             files action)
11531         (while ps
11532           (setq action (cdr (assq 'action (car ps))))
11533           (setq files (list (cdr (assq 'name (car ps)))))
11534           (while (and ps (cdr ps)
11535                       (string= (or action "1")
11536                                (or (cdr (assq 'action (cadr ps))) "2")))
11537             (push (cdr (assq 'name (cadr ps))) files)
11538             (setcdr ps (cddr ps)))
11539           (when files
11540             (when (not (string-match "%s" action))
11541               (push " " files))
11542             (push " " files)
11543             (when (assq 'execute (car ps))
11544               (setcdr (assq 'execute (car ps))
11545                       (funcall (if (string-match "%s" action)
11546                                    'format 'concat)
11547                                action
11548                                (mapconcat
11549                                 (lambda (f)
11550                                   (if (equal f " ")
11551                                       f
11552                                     (shell-quote-argument f)))
11553                                 files " ")))))
11554           (setq ps (cdr ps)))))
11555     (if (and gnus-view-pseudos (not not-view))
11556         (while pslist
11557           (when (assq 'execute (car pslist))
11558             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11559                                   (eq gnus-view-pseudos 'not-confirm)))
11560           (setq pslist (cdr pslist)))
11561       (save-excursion
11562         (while pslist
11563           (setq after-article (or (cdr (assq 'article (car pslist)))
11564                                   (gnus-summary-article-number)))
11565           (gnus-summary-goto-subject after-article)
11566           (forward-line 1)
11567           (setq b (point))
11568           (insert "    " (file-name-nondirectory
11569                           (cdr (assq 'name (car pslist))))
11570                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11571           (setq e (point))
11572           (forward-line -1)             ; back to `b'
11573           (gnus-add-text-properties
11574            b (1- e) (list 'gnus-number gnus-reffed-article-number
11575                           gnus-mouse-face-prop gnus-mouse-face))
11576           (gnus-data-enter
11577            after-article gnus-reffed-article-number
11578            gnus-unread-mark b (car pslist) 0 (- e b))
11579           (setq gnus-newsgroup-unreads
11580                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11581                                          gnus-reffed-article-number))
11582           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11583           (setq pslist (cdr pslist)))))))
11584
11585 (defun gnus-pseudos< (p1 p2)
11586   (let ((c1 (cdr (assq 'action p1)))
11587         (c2 (cdr (assq 'action p2))))
11588     (and c1 c2 (string< c1 c2))))
11589
11590 (defun gnus-request-pseudo-article (props)
11591   (cond ((assq 'execute props)
11592          (gnus-execute-command (cdr (assq 'execute props)))))
11593   (let ((gnus-current-article (gnus-summary-article-number)))
11594     (gnus-run-hooks 'gnus-mark-article-hook)))
11595
11596 (defun gnus-execute-command (command &optional automatic)
11597   (save-excursion
11598     (gnus-article-setup-buffer)
11599     (set-buffer gnus-article-buffer)
11600     (setq buffer-read-only nil)
11601     (let ((command (if automatic command
11602                      (read-string "Command: " (cons command 0)))))
11603       (erase-buffer)
11604       (insert "$ " command "\n\n")
11605       (if gnus-view-pseudo-asynchronously
11606           (start-process "gnus-execute" (current-buffer) shell-file-name
11607                          shell-command-switch command)
11608         (call-process shell-file-name nil t nil
11609                       shell-command-switch command)))))
11610
11611 ;; Summary kill commands.
11612
11613 (defun gnus-summary-edit-global-kill (article)
11614   "Edit the \"global\" kill file."
11615   (interactive (list (gnus-summary-article-number)))
11616   (gnus-group-edit-global-kill article))
11617
11618 (defun gnus-summary-edit-local-kill ()
11619   "Edit a local kill file applied to the current newsgroup."
11620   (interactive)
11621   (setq gnus-current-headers (gnus-summary-article-header))
11622   (gnus-group-edit-local-kill
11623    (gnus-summary-article-number) gnus-newsgroup-name))
11624
11625 ;;; Header reading.
11626
11627 (defun gnus-read-header (id &optional header)
11628   "Read the headers of article ID and enter them into the Gnus system."
11629   (let ((group gnus-newsgroup-name)
11630         (gnus-override-method
11631          (or
11632           gnus-override-method
11633           (and (gnus-news-group-p gnus-newsgroup-name)
11634                (car (gnus-refer-article-methods)))))
11635         where)
11636     ;; First we check to see whether the header in question is already
11637     ;; fetched.
11638     (if (stringp id)
11639         ;; This is a Message-ID.
11640         (setq header (or header (gnus-id-to-header id)))
11641       ;; This is an article number.
11642       (setq header (or header (gnus-summary-article-header id))))
11643     (if (and header
11644              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11645         ;; We have found the header.
11646         header
11647       ;; We have to really fetch the header to this article.
11648       (save-excursion
11649         (set-buffer nntp-server-buffer)
11650         (when (setq where (gnus-request-head id group))
11651           (nnheader-fold-continuation-lines)
11652           (goto-char (point-max))
11653           (insert ".\n")
11654           (goto-char (point-min))
11655           (insert "211 ")
11656           (princ (cond
11657                   ((numberp id) id)
11658                   ((cdr where) (cdr where))
11659                   (header (mail-header-number header))
11660                   (t gnus-reffed-article-number))
11661                  (current-buffer))
11662           (insert " Article retrieved.\n"))
11663         (if (or (not where)
11664                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11665             ()                          ; Malformed head.
11666           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11667             (when (and (stringp id)
11668                        (or
11669                         (not (string= (gnus-group-real-name group)
11670                                       (car where)))
11671                         (not (gnus-server-equal gnus-override-method
11672                                                 (gnus-group-method group)))))
11673               ;; If we fetched by Message-ID and the article came from
11674               ;; a different group (or server), we fudge some bogus
11675               ;; article numbers for this article.
11676               (mail-header-set-number header gnus-reffed-article-number))
11677             (save-excursion
11678               (set-buffer gnus-summary-buffer)
11679               (decf gnus-reffed-article-number)
11680               (gnus-remove-header (mail-header-number header))
11681               (push header gnus-newsgroup-headers)
11682               (setq gnus-current-headers header)
11683               (push (mail-header-number header) gnus-newsgroup-limit)))
11684           header)))))
11685
11686 (defun gnus-remove-header (number)
11687   "Remove header NUMBER from `gnus-newsgroup-headers'."
11688   (if (and gnus-newsgroup-headers
11689            (= number (mail-header-number (car gnus-newsgroup-headers))))
11690       (pop gnus-newsgroup-headers)
11691     (let ((headers gnus-newsgroup-headers))
11692       (while (and (cdr headers)
11693                   (not (= number (mail-header-number (cadr headers)))))
11694         (pop headers))
11695       (when (cdr headers)
11696         (setcdr headers (cddr headers))))))
11697
11698 ;;;
11699 ;;; summary highlights
11700 ;;;
11701
11702 (defun gnus-highlight-selected-summary ()
11703   "Highlight selected article in summary buffer."
11704   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11705   (when gnus-summary-selected-face
11706     (save-excursion
11707       (let* ((beg (point-at-bol))
11708              (end (point-at-eol))
11709              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11710              (from (if (get-text-property beg gnus-mouse-face-prop)
11711                        beg
11712                      (or (next-single-property-change
11713                           beg gnus-mouse-face-prop nil end)
11714                          beg)))
11715              (to
11716               (if (= from end)
11717                   (- from 2)
11718                 (or (next-single-property-change
11719                      from gnus-mouse-face-prop nil end)
11720                     end))))
11721         ;; If no mouse-face prop on line we will have to = from = end,
11722         ;; so we highlight the entire line instead.
11723         (when (= (+ to 2) from)
11724           (setq from beg)
11725           (setq to end))
11726         (if gnus-newsgroup-selected-overlay
11727             ;; Move old overlay.
11728             (gnus-move-overlay
11729              gnus-newsgroup-selected-overlay from to (current-buffer))
11730           ;; Create new overlay.
11731           (gnus-overlay-put
11732            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11733            'face gnus-summary-selected-face))))))
11734
11735 (defvar gnus-summary-highlight-line-cached nil)
11736 (defvar gnus-summary-highlight-line-trigger nil)
11737
11738 (defun gnus-summary-highlight-line-0 ()
11739   (if (and (eq gnus-summary-highlight-line-trigger
11740                gnus-summary-highlight)
11741            gnus-summary-highlight-line-cached)
11742       gnus-summary-highlight-line-cached
11743     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11744           gnus-summary-highlight-line-cached
11745           (let* ((cond (list 'cond))
11746                  (c cond)
11747                  (list gnus-summary-highlight))
11748             (while list
11749               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11750                               nil))
11751               (setq c (cdr c)
11752                     list (cdr list)))
11753             (gnus-byte-compile (list 'lambda nil cond))))))
11754
11755 (defun gnus-summary-highlight-line ()
11756   "Highlight current line according to `gnus-summary-highlight'."
11757   (let* ((beg (point-at-bol))
11758          (article (or (gnus-summary-article-number) gnus-current-article))
11759          (score (or (cdr (assq article
11760                                gnus-newsgroup-scored))
11761                     gnus-summary-default-score 0))
11762          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11763          (inhibit-read-only t)
11764          (default gnus-summary-default-score)
11765          (default-high gnus-summary-default-high-score)
11766          (default-low gnus-summary-default-low-score)
11767          (uncached (and gnus-summary-use-undownloaded-faces
11768                         (memq article gnus-newsgroup-undownloaded)
11769                         (not (memq article gnus-newsgroup-cached)))))
11770     (let ((face (funcall (gnus-summary-highlight-line-0))))
11771       (unless (eq face (get-text-property beg 'face))
11772         (gnus-put-text-property-excluding-characters-with-faces
11773          beg (point-at-eol) 'face
11774          (setq face (if (boundp face) (symbol-value face) face)))
11775         (when gnus-summary-highlight-line-function
11776           (funcall gnus-summary-highlight-line-function article face))))))
11777
11778 (defun gnus-update-read-articles (group unread &optional compute)
11779   "Update the list of read articles in GROUP.
11780 UNREAD is a sorted list."
11781   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11782         (info (gnus-get-info group))
11783         (prev 1)
11784         read)
11785     (if (or (not info) (not active))
11786         ;; There is no info on this group if it was, in fact,
11787         ;; killed.  Gnus stores no information on killed groups, so
11788         ;; there's nothing to be done.
11789         ;; One could store the information somewhere temporarily,
11790         ;; perhaps...  Hmmm...
11791         ()
11792       ;; Remove any negative articles numbers.
11793       (while (and unread (< (car unread) 0))
11794         (setq unread (cdr unread)))
11795       ;; Remove any expired article numbers
11796       (while (and unread (< (car unread) (car active)))
11797         (setq unread (cdr unread)))
11798       ;; Compute the ranges of read articles by looking at the list of
11799       ;; unread articles.
11800       (while unread
11801         (when (/= (car unread) prev)
11802           (push (if (= prev (1- (car unread))) prev
11803                   (cons prev (1- (car unread))))
11804                 read))
11805         (setq prev (1+ (car unread)))
11806         (setq unread (cdr unread)))
11807       (when (<= prev (cdr active))
11808         (push (cons prev (cdr active)) read))
11809       (setq read (if (> (length read) 1) (nreverse read) read))
11810       (if compute
11811           read
11812         (save-excursion
11813           (let (setmarkundo)
11814             ;; Propagate the read marks to the backend.
11815             (when (gnus-check-backend-function 'request-set-mark group)
11816               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11817                     (add (gnus-remove-from-range read (gnus-info-read info))))
11818                 (when (or add del)
11819                   (unless (gnus-check-group group)
11820                     (error "Can't open server for %s" group))
11821                   (gnus-request-set-mark
11822                    group (delq nil (list (if add (list add 'add '(read)))
11823                                          (if del (list del 'del '(read))))))
11824                   (setq setmarkundo
11825                         `(gnus-request-set-mark
11826                           ,group
11827                           ',(delq nil (list
11828                                        (if del (list del 'add '(read)))
11829                                        (if add (list add 'del '(read))))))))))
11830             (set-buffer gnus-group-buffer)
11831             (gnus-undo-register
11832               `(progn
11833                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11834                  (gnus-info-set-read ',info ',(gnus-info-read info))
11835                  (gnus-get-unread-articles-in-group ',info
11836                                                     (gnus-active ,group))
11837                  (gnus-group-update-group ,group t)
11838                  ,setmarkundo))))
11839         ;; Enter this list into the group info.
11840         (gnus-info-set-read info read)
11841         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11842         (gnus-get-unread-articles-in-group info (gnus-active group))
11843         t))))
11844
11845 (defun gnus-offer-save-summaries ()
11846   "Offer to save all active summary buffers."
11847   (let (buffers)
11848     ;; Go through all buffers and find all summaries.
11849     (dolist (buffer (buffer-list))
11850       (when (and (setq buffer (buffer-name buffer))
11851                  (string-match "Summary" buffer)
11852                  (with-current-buffer buffer
11853                    ;; We check that this is, indeed, a summary buffer.
11854                    (and (eq major-mode 'gnus-summary-mode)
11855                         ;; Also make sure this isn't bogus.
11856                         gnus-newsgroup-prepared
11857                         ;; Also make sure that this isn't a
11858                         ;; dead summary buffer.
11859                         (not gnus-dead-summary-mode))))
11860         (push buffer buffers)))
11861     ;; Go through all these summary buffers and offer to save them.
11862     (when buffers
11863       (save-excursion
11864         (map-y-or-n-p
11865          "Update summary buffer %s? "
11866          (lambda (buf)
11867            (switch-to-buffer buf)
11868            (gnus-summary-exit))
11869          buffers)))))
11870
11871 (defun gnus-summary-setup-default-charset ()
11872   "Setup newsgroup default charset."
11873   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11874       (setq gnus-newsgroup-charset nil)
11875     (let* ((ignored-charsets
11876             (or gnus-newsgroup-ephemeral-ignored-charsets
11877                 (append
11878                  (and gnus-newsgroup-name
11879                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11880                  gnus-newsgroup-ignored-charsets))))
11881       (setq gnus-newsgroup-charset
11882             (or gnus-newsgroup-ephemeral-charset
11883                 (and gnus-newsgroup-name
11884                      (gnus-parameter-charset gnus-newsgroup-name))
11885                 gnus-default-charset))
11886       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11887            ignored-charsets))))
11888
11889 ;;;
11890 ;;; Mime Commands
11891 ;;;
11892
11893 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11894   "Display the current article buffer fully MIME-buttonized.
11895 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11896 treated as multipart/mixed."
11897   (interactive "P")
11898   (require 'gnus-art)
11899   (let ((gnus-unbuttonized-mime-types nil)
11900         (gnus-mime-display-multipart-as-mixed show-all-parts))
11901     (gnus-summary-show-article)))
11902
11903 (defun gnus-summary-repair-multipart (article)
11904   "Add a Content-Type header to a multipart article without one."
11905   (interactive (list (gnus-summary-article-number)))
11906   (gnus-with-article article
11907     (message-narrow-to-head)
11908     (message-remove-header "Mime-Version")
11909     (goto-char (point-max))
11910     (insert "Mime-Version: 1.0\n")
11911     (widen)
11912     (when (search-forward "\n--" nil t)
11913       (let ((separator (buffer-substring (point) (point-at-eol))))
11914         (message-narrow-to-head)
11915         (message-remove-header "Content-Type")
11916         (goto-char (point-max))
11917         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11918                         separator))
11919         (widen))))
11920   (let (gnus-mark-article-hook)
11921     (gnus-summary-select-article t t nil article)))
11922
11923 (defun gnus-summary-toggle-display-buttonized ()
11924   "Toggle the buttonizing of the article buffer."
11925   (interactive)
11926   (require 'gnus-art)
11927   (if (setq gnus-inhibit-mime-unbuttonizing
11928             (not gnus-inhibit-mime-unbuttonizing))
11929       (let ((gnus-unbuttonized-mime-types nil))
11930         (gnus-summary-show-article))
11931     (gnus-summary-show-article)))
11932
11933 ;;;
11934 ;;; Generic summary marking commands
11935 ;;;
11936
11937 (defvar gnus-summary-marking-alist
11938   '((read gnus-del-mark "d")
11939     (unread gnus-unread-mark "u")
11940     (ticked gnus-ticked-mark "!")
11941     (dormant gnus-dormant-mark "?")
11942     (expirable gnus-expirable-mark "e"))
11943   "An alist of names/marks/keystrokes.")
11944
11945 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11946 (defvar gnus-summary-mark-map)
11947
11948 (defun gnus-summary-make-all-marking-commands ()
11949   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11950   (dolist (elem gnus-summary-marking-alist)
11951     (apply 'gnus-summary-make-marking-command elem)))
11952
11953 (defun gnus-summary-make-marking-command (name mark keystroke)
11954   (let ((map (make-sparse-keymap)))
11955     (define-key gnus-summary-generic-mark-map keystroke map)
11956     (dolist (lway `((next "next" next nil "n")
11957                     (next-unread "next unread" next t "N")
11958                     (prev "previous" prev nil "p")
11959                     (prev-unread "previous unread" prev t "P")
11960                     (nomove "" nil nil ,keystroke)))
11961       (let ((func (gnus-summary-make-marking-command-1
11962                    mark (car lway) lway name)))
11963         (setq func (eval func))
11964         (define-key map (nth 4 lway) func)))))
11965
11966 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11967   `(defun ,(intern
11968             (format "gnus-summary-put-mark-as-%s%s"
11969                     name (if (eq way 'nomove)
11970                              ""
11971                            (concat "-" (symbol-name way)))))
11972      (n)
11973      ,(format
11974        "Mark the current article as %s%s.
11975 If N, the prefix, then repeat N times.
11976 If N is negative, move in reverse order.
11977 The difference between N and the actual number of articles marked is
11978 returned."
11979        name (cadr lway))
11980      (interactive "p")
11981      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11982
11983 (defun gnus-summary-generic-mark (n mark move unread)
11984   "Mark N articles with MARK."
11985   (unless (eq major-mode 'gnus-summary-mode)
11986     (error "This command can only be used in the summary buffer"))
11987   (gnus-summary-show-thread)
11988   (let ((nummove
11989          (cond
11990           ((eq move 'next) 1)
11991           ((eq move 'prev) -1)
11992           (t 0))))
11993     (if (zerop nummove)
11994         (setq n 1)
11995       (when (< n 0)
11996         (setq n (abs n)
11997               nummove (* -1 nummove))))
11998     (while (and (> n 0)
11999                 (gnus-summary-mark-article nil mark)
12000                 (zerop (gnus-summary-next-subject nummove unread t)))
12001       (setq n (1- n)))
12002     (when (/= 0 n)
12003       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12004     (gnus-summary-recenter)
12005     (gnus-summary-position-point)
12006     (gnus-set-mode-line 'summary)
12007     n))
12008
12009 (defun gnus-summary-insert-articles (articles)
12010   (when (setq articles
12011               (gnus-sorted-difference articles
12012                                       (mapcar (lambda (h)
12013                                                 (mail-header-number h))
12014                                               gnus-newsgroup-headers)))
12015     (setq gnus-newsgroup-headers
12016           (gnus-merge 'list
12017                       gnus-newsgroup-headers
12018                       (gnus-fetch-headers articles)
12019                       'gnus-article-sort-by-number))
12020     ;; Suppress duplicates?
12021     (when gnus-suppress-duplicates
12022       (gnus-dup-suppress-articles))
12023
12024     ;; We might want to build some more threads first.
12025     (when (and gnus-fetch-old-headers
12026                (eq gnus-headers-retrieved-by 'nov))
12027       (if (eq gnus-fetch-old-headers 'invisible)
12028           (gnus-build-all-threads)
12029         (gnus-build-old-threads)))
12030     ;; Let the Gnus agent mark articles as read.
12031     (when gnus-agent
12032       (gnus-agent-get-undownloaded-list))
12033     ;; Remove list identifiers from subject
12034     (when gnus-list-identifiers
12035       (gnus-summary-remove-list-identifiers))
12036     ;; First and last article in this newsgroup.
12037     (when gnus-newsgroup-headers
12038       (setq gnus-newsgroup-begin
12039             (mail-header-number (car gnus-newsgroup-headers))
12040             gnus-newsgroup-end
12041             (mail-header-number
12042              (gnus-last-element gnus-newsgroup-headers))))
12043     (when gnus-use-scoring
12044       (gnus-possibly-score-headers))))
12045
12046 (defun gnus-summary-insert-old-articles (&optional all)
12047   "Insert all old articles in this group.
12048 If ALL is non-nil, already read articles become readable.
12049 If ALL is a number, fetch this number of articles."
12050   (interactive "P")
12051   (prog1
12052       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12053             older len)
12054         (setq older
12055               ;; Some nntp servers lie about their active range.  When
12056               ;; this happens, the active range can be in the millions.
12057               ;; Use a compressed range to avoid creating a huge list.
12058               (gnus-range-difference (list gnus-newsgroup-active) old))
12059         (setq len (gnus-range-length older))
12060         (cond
12061          ((null older) nil)
12062          ((numberp all)
12063           (if (< all len)
12064               (let ((older-range (nreverse older)))
12065                 (setq older nil)
12066
12067                 (while (> all 0)
12068                   (let* ((r (pop older-range))
12069                          (min (if (numberp r) r (car r)))
12070                          (max (if (numberp r) r (cdr r))))
12071                     (while (and (<= min max)
12072                                 (> all 0))
12073                       (push max older)
12074                       (setq all (1- all)
12075                             max (1- max))))))
12076             (setq older (gnus-uncompress-range older))))
12077          (all
12078           (setq older (gnus-uncompress-range older)))
12079          (t
12080           (when (and (numberp gnus-large-newsgroup)
12081                    (> len gnus-large-newsgroup))
12082               (let* ((cursor-in-echo-area nil)
12083                      (initial (gnus-parameter-large-newsgroup-initial
12084                                gnus-newsgroup-name))
12085                      (input
12086                       (read-string
12087                        (format
12088                         "How many articles from %s (%s %d): "
12089                         (gnus-limit-string
12090                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12091                         (if initial "max" "default")
12092                         len)
12093                        (if initial
12094                            (cons (number-to-string initial)
12095                                  0)))))
12096                 (unless (string-match "^[ \t]*$" input)
12097                   (setq all (string-to-number input))
12098                   (if (< all len)
12099                       (let ((older-range (nreverse older)))
12100                         (setq older nil)
12101
12102                         (while (> all 0)
12103                           (let* ((r (pop older-range))
12104                                  (min (if (numberp r) r (car r)))
12105                                  (max (if (numberp r) r (cdr r))))
12106                             (while (and (<= min max)
12107                                         (> all 0))
12108                               (push max older)
12109                               (setq all (1- all)
12110                                     max (1- max))))))))))
12111           (setq older (gnus-uncompress-range older))))
12112         (if (not older)
12113             (message "No old news.")
12114           (gnus-summary-insert-articles older)
12115           (gnus-summary-limit (gnus-sorted-nunion old older))))
12116     (gnus-summary-position-point)))
12117
12118 (defun gnus-summary-insert-new-articles ()
12119   "Insert all new articles in this group."
12120   (interactive)
12121   (prog1
12122       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12123             (old-active gnus-newsgroup-active)
12124             (nnmail-fetched-sources (list t))
12125             i new)
12126         (setq gnus-newsgroup-active
12127               (gnus-activate-group gnus-newsgroup-name 'scan))
12128         (setq i (cdr gnus-newsgroup-active))
12129         (while (> i (cdr old-active))
12130           (push i new)
12131           (decf i))
12132         (if (not new)
12133             (message "No gnus is bad news")
12134           (gnus-summary-insert-articles new)
12135           (setq gnus-newsgroup-unreads
12136                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12137           (gnus-summary-limit (gnus-sorted-nunion old new))))
12138     (gnus-summary-position-point)))
12139
12140 (gnus-summary-make-all-marking-commands)
12141
12142 (gnus-ems-redefine)
12143
12144 (provide 'gnus-sum)
12145
12146 (run-hooks 'gnus-sum-load-hook)
12147
12148 ;; Local Variables:
12149 ;; coding: iso-8859-1
12150 ;; End:
12151
12152 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12153 ;;; gnus-sum.el ends here