f218fe33bc9abe1168b439b54ce913e3460a6d47
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-mode))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-fetch-old-headers nil
66   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
67 If an unread article in the group refers to an older, already
68 read (or just marked as read) article, the old article will not
69 normally be displayed in the Summary buffer.  If this variable is
70 t, Gnus will attempt to grab the headers to the old articles, and
71 thereby build complete threads.  If it has the value `some', all
72 old headers will be fetched but only enough headers to connect
73 otherwise loose threads will be displayed.  This variable can
74 also be a number.  In that case, no more than that number of old
75 headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work.
79
80 This feature can seriously impact performance it ignores all
81 locally cached header entries."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const :tag "on" t)
85                  (const some)
86                  (const invisible)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 500
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "*If non-nil, select the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), `unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :version "22.1"
333   :group 'gnus-group-select
334   :type '(choice (const best)
335                  (const unread)
336                  (const first)
337                  (const unseen)
338                  (const unseen-or-unread)))
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols nil (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :version "22.1"
377   :group 'gnus-summary-maneuvering
378   :type '(choice (const :tag "None" nil)
379                  (const :tag "Undownloaded when unplugged" undownloaded)
380                  (const :tag "Undownloaded" always-undownloaded)
381                  (const :tag "Unfetched" unfetched)))
382
383 (defcustom gnus-summary-check-current nil
384   "*If non-nil, consider the current article when moving.
385 The \"unread\" movement commands will stay on the same line if the
386 current article is unread."
387   :group 'gnus-summary-maneuvering
388   :type 'boolean)
389
390 (defcustom gnus-auto-center-summary 2
391   "*If non-nil, always center the current summary buffer.
392 In particular, if `vertical' do only vertical recentering.  If non-nil
393 and non-`vertical', do both horizontal and vertical recentering."
394   :group 'gnus-summary-maneuvering
395   :type '(choice (const :tag "none" nil)
396                  (const vertical)
397                  (integer :tag "height")
398                  (sexp :menu-tag "both" t)))
399
400 (defvar gnus-auto-center-group t
401   "*If non-nil, always center the group buffer.")
402
403 (defcustom gnus-show-all-headers nil
404   "*If non-nil, don't hide any headers."
405   :group 'gnus-article-hiding
406   :group 'gnus-article-headers
407   :type 'boolean)
408
409 (defcustom gnus-summary-ignore-duplicates nil
410   "*If non-nil, ignore articles with identical Message-ID headers."
411   :group 'gnus-summary
412   :type 'boolean)
413
414 (defcustom gnus-single-article-buffer t
415   "*If non-nil, display all articles in the same buffer.
416 If nil, each group will get its own article buffer."
417   :group 'gnus-article-various
418   :type 'boolean)
419
420 (defcustom gnus-break-pages t
421   "*If non-nil, do page breaking on articles.
422 The page delimiter is specified by the `gnus-page-delimiter'
423 variable."
424   :group 'gnus-article-various
425   :type 'boolean)
426
427 (defcustom gnus-move-split-methods nil
428   "*Variable used to suggest where articles are to be moved to.
429 It uses the same syntax as the `gnus-split-methods' variable.
430 However, whereas `gnus-split-methods' specifies file names as targets,
431 this variable specifies group names."
432   :group 'gnus-summary-mail
433   :type '(repeat (choice (list :value (fun) function)
434                          (cons :value ("" "") regexp (repeat string))
435                          (sexp :value nil))))
436
437 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
438   "Function used to compute default prefix for article move/copy/etc prompts.
439 The function should take one argument, a group name, and return a
440 string with the suggested prefix."
441   :group 'gnus-summary-mail
442   :type 'function)
443
444 ;; FIXME: Although the custom type is `character' for the following variables,
445 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
446
447 (defcustom gnus-unread-mark ?           ;Whitespace
448   "*Mark used for unread articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ticked-mark ?!
453   "*Mark used for ticked articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-dormant-mark ??
458   "*Mark used for dormant articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-del-mark ?r
463   "*Mark used for del'd articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-read-mark ?R
468   "*Mark used for read articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-expirable-mark ?E
473   "*Mark used for expirable articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-killed-mark ?K
478   "*Mark used for killed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-spam-mark ?$
483   "*Mark used for spam articles."
484   :version "22.1"
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :version "22.1"
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :version "22.1"
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-no-mark ?               ;Whitespace
542   "*Mark used for articles that have no other secondary mark."
543   :version "22.1"
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-ancient-mark ?O
548   "*Mark used for ancient articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-sparse-mark ?Q
553   "*Mark used for sparsely reffed articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-canceled-mark ?G
558   "*Mark used for canceled articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-duplicate-mark ?M
563   "*Mark used for duplicate articles."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-undownloaded-mark ?-
568   "*Mark used for articles that weren't downloaded."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloaded-mark ?+
574   "*Mark used for articles that were downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-downloadable-mark ?%
579   "*Mark used for articles that are to be downloaded."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unsendable-mark ?=
584   "*Mark used for articles that won't be sent."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-over-mark ?+
589   "*Score mark used for articles with high scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-score-below-mark ?-
594   "*Score mark used for articles with low scores."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-empty-thread-mark ?     ;Whitespace
599   "*There is no thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-not-empty-thread-mark ?=
604   "*There is a thread under the article."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-view-pseudo-asynchronously nil
609   "*If non-nil, Gnus will view pseudo-articles asynchronously."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-auto-expirable-marks
614   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
615         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
616         gnus-souped-mark gnus-duplicate-mark)
617   "*The list of marks converted into expiration if a group is auto-expirable."
618   :version "21.1"
619   :group 'gnus-summary
620   :type '(repeat character))
621
622 (defcustom gnus-inhibit-user-auto-expire t
623   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
624   :version "21.1"
625   :group 'gnus-summary
626   :type 'boolean)
627
628 (defcustom gnus-view-pseudos nil
629   "*If `automatic', pseudo-articles will be viewed automatically.
630 If `not-confirm', pseudos will be viewed automatically, and the user
631 will not be asked to confirm the command."
632   :group 'gnus-extract-view
633   :type '(choice (const :tag "off" nil)
634                  (const automatic)
635                  (const not-confirm)))
636
637 (defcustom gnus-view-pseudos-separately t
638   "*If non-nil, one pseudo-article will be created for each file to be viewed.
639 If nil, all files that use the same viewing command will be given as a
640 list of parameters to that command."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-insert-pseudo-articles t
645   "*If non-nil, insert pseudo-articles when decoding articles."
646   :group 'gnus-extract-view
647   :type 'boolean)
648
649 (defcustom gnus-summary-dummy-line-format
650   "   %(:                             :%) %S\n"
651   "*The format specification for the dummy roots in the summary buffer.
652 It works along the same lines as a normal formatting string,
653 with some simple extensions.
654
655 %S  The subject
656
657 General format specifiers can also be used.
658 See `(gnus)Formatting Variables'."
659   :link '(custom-manual "(gnus)Formatting Variables")
660   :group 'gnus-threading
661   :type 'string)
662
663 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
664   "*The format specification for the summary mode line.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions:
667
668 %G  Group name
669 %p  Unprefixed group name
670 %A  Current article number
671 %z  Current article score
672 %V  Gnus version
673 %U  Number of unread articles in the group
674 %e  Number of unselected articles in the group
675 %Z  A string with unread/unselected article counts
676 %g  Shortish group name
677 %S  Subject of the current article
678 %u  User-defined spec
679 %s  Current score file name
680 %d  Number of dormant articles
681 %r  Number of articles that have been marked as read in this session
682 %E  Number of articles expunged by the score files"
683   :group 'gnus-summary-format
684   :type 'string)
685
686 (defcustom gnus-list-identifiers nil
687   "Regexp that matches list identifiers to be removed from subject.
688 This can also be a list of regexps."
689   :version "21.1"
690   :group 'gnus-summary-format
691   :group 'gnus-article-hiding
692   :type '(choice (const :tag "none" nil)
693                  (regexp :value ".*")
694                  (repeat :value (".*") regexp)))
695
696 (defcustom gnus-summary-mark-below 0
697   "*Mark all articles with a score below this variable as read.
698 This variable is local to each summary buffer and usually set by the
699 score file."
700   :group 'gnus-score-default
701   :type 'integer)
702
703 (defun gnus-widget-reversible-match (widget value)
704   "Ignoring WIDGET, convert VALUE to internal form.
705 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
706   ;; (debug value)
707   (or (symbolp value)
708       (and (listp value)
709            (eq (length value) 2)
710            (eq (nth 0 value) 'not)
711            (symbolp (nth 1 value)))))
712
713 (defun gnus-widget-reversible-to-internal (widget value)
714   "Ignoring WIDGET, convert VALUE to internal form.
715 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
716 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
717   ;; (debug value)
718   (if (atom value)
719       (list value nil)
720     (list (nth 1 value) t)))
721
722 (defun gnus-widget-reversible-to-external (widget value)
723   "Ignoring WIDGET, convert VALUE to external form.
724 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
725 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
726   ;; (debug value)
727   (if (nth 1 value)
728       (list 'not (nth 0 value))
729     (nth 0 value)))
730
731 (define-widget 'gnus-widget-reversible 'group
732   "A `group' that convert values."
733   :match 'gnus-widget-reversible-match
734   :value-to-internal 'gnus-widget-reversible-to-internal
735   :value-to-external 'gnus-widget-reversible-to-external)
736
737 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
738   "*List of functions used for sorting articles in the summary buffer.
739
740 Each function takes two articles and returns non-nil if the first
741 article should be sorted before the other.  If you use more than one
742 function, the primary sort function should be the last.  You should
743 probably always include `gnus-article-sort-by-number' in the list of
744 sorting functions -- preferably first.  Also note that sorting by date
745 is often much slower than sorting by number, and the sorting order is
746 very similar.  (Sorting by date means sorting by the time the message
747 was sent, sorting by number means sorting by arrival time.)
748
749 Each item can also be a list `(not F)' where F is a function;
750 this reverses the sort order.
751
752 Ready-made functions include `gnus-article-sort-by-number',
753 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
754 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
755 and `gnus-article-sort-by-score'.
756
757 When threading is turned on, the variable `gnus-thread-sort-functions'
758 controls how articles are sorted."
759   :group 'gnus-summary-sort
760   :type '(repeat (gnus-widget-reversible
761                   (choice (function-item gnus-article-sort-by-number)
762                           (function-item gnus-article-sort-by-author)
763                           (function-item gnus-article-sort-by-subject)
764                           (function-item gnus-article-sort-by-date)
765                           (function-item gnus-article-sort-by-score)
766                           (function-item gnus-article-sort-by-random)
767                           (function :tag "other"))
768                   (boolean :tag "Reverse order"))))
769
770
771 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
772   "*List of functions used for sorting threads in the summary buffer.
773 By default, threads are sorted by article number.
774
775 Each function takes two threads and returns non-nil if the first
776 thread should be sorted before the other.  If you use more than one
777 function, the primary sort function should be the last.  You should
778 probably always include `gnus-thread-sort-by-number' in the list of
779 sorting functions -- preferably first.  Also note that sorting by date
780 is often much slower than sorting by number, and the sorting order is
781 very similar.  (Sorting by date means sorting by the time the message
782 was sent, sorting by number means sorting by arrival time.)
783
784 Each list item can also be a list `(not F)' where F is a
785 function; this specifies reversed sort order.
786
787 Ready-made functions include `gnus-thread-sort-by-number',
788 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
789 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
790 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
791 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
792 and `gnus-thread-sort-by-total-score' (see
793 `gnus-thread-score-function').
794
795 When threading is turned off, the variable
796 `gnus-article-sort-functions' controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat
799           (gnus-widget-reversible
800            (choice (function-item gnus-thread-sort-by-number)
801                    (function-item gnus-thread-sort-by-author)
802                    (function-item gnus-thread-sort-by-recipient)
803                    (function-item gnus-thread-sort-by-subject)
804                    (function-item gnus-thread-sort-by-date)
805                    (function-item gnus-thread-sort-by-score)
806                    (function-item gnus-thread-sort-by-most-recent-number)
807                    (function-item gnus-thread-sort-by-most-recent-date)
808                    (function-item gnus-thread-sort-by-random)
809                    (function-item gnus-thread-sort-by-total-score)
810                    (function :tag "other"))
811            (boolean :tag "Reverse order"))))
812
813 (defcustom gnus-thread-score-function '+
814   "*Function used for calculating the total score of a thread.
815
816 The function is called with the scores of the article and each
817 subthread and should then return the score of the thread.
818
819 Some functions you can use are `+', `max', or `min'."
820   :group 'gnus-summary-sort
821   :type 'function)
822
823 (defcustom gnus-summary-expunge-below nil
824   "All articles that have a score less than this variable will be expunged.
825 This variable is local to the summary buffers."
826   :group 'gnus-score-default
827   :type '(choice (const :tag "off" nil)
828                  integer))
829
830 (defcustom gnus-thread-expunge-below nil
831   "All threads that have a total score less than this variable will be expunged.
832 See `gnus-thread-score-function' for en explanation of what a
833 \"thread score\" is.
834
835 This variable is local to the summary buffers."
836   :group 'gnus-threading
837   :group 'gnus-score-default
838   :type '(choice (const :tag "off" nil)
839                  integer))
840
841 (defcustom gnus-summary-mode-hook nil
842   "*A hook for Gnus summary mode.
843 This hook is run before any variables are set in the summary buffer."
844   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
845   :group 'gnus-summary-various
846   :type 'hook)
847
848 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
849 (when (featurep 'xemacs)
850   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
851   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
852   (add-hook 'gnus-summary-mode-hook
853             'gnus-xmas-switch-horizontal-scrollbar-off))
854
855 (defcustom gnus-summary-menu-hook nil
856   "*Hook run after the creation of the summary mode menu."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-summary-exit-hook nil
861   "*A hook called on exit from the summary buffer.
862 It will be called with point in the group buffer."
863   :group 'gnus-summary-exit
864   :type 'hook)
865
866 (defcustom gnus-summary-prepare-hook nil
867   "*A hook called after the summary buffer has been generated.
868 If you want to modify the summary buffer, you can use this hook."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-prepared-hook nil
873   "*A hook called as the last thing after the summary buffer has been generated."
874   :group 'gnus-summary-various
875   :type 'hook)
876
877 (defcustom gnus-summary-generate-hook nil
878   "*A hook run just before generating the summary buffer.
879 This hook is commonly used to customize threading variables and the
880 like."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-select-group-hook nil
885   "*A hook called when a newsgroup is selected.
886
887 If you'd like to simplify subjects like the
888 `gnus-summary-next-same-subject' command does, you can use the
889 following hook:
890
891  (add-hook gnus-select-group-hook
892            (lambda ()
893              (mapcar (lambda (header)
894                        (mail-header-set-subject
895                         header
896                         (gnus-simplify-subject
897                          (mail-header-subject header) 're-only)))
898                      gnus-newsgroup-headers)))"
899   :group 'gnus-group-select
900   :type 'hook)
901
902 (defcustom gnus-select-article-hook nil
903   "*A hook called when an article is selected."
904   :group 'gnus-summary-choose
905   :options '(gnus-agent-fetch-selected-article)
906   :type 'hook)
907
908 (defcustom gnus-visual-mark-article-hook
909   (list 'gnus-highlight-selected-summary)
910   "*Hook run after selecting an article in the summary buffer.
911 It is meant to be used for highlighting the article in some way.  It
912 is not run if `gnus-visual' is nil."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-parse-headers-hook nil
917   "*A hook called before parsing the headers."
918   :group 'gnus-various
919   :type 'hook)
920
921 (defcustom gnus-exit-group-hook nil
922   "*A hook called when exiting summary mode.
923 This hook is not called from the non-updating exit commands like `Q'."
924   :group 'gnus-various
925   :type 'hook)
926
927 (defcustom gnus-summary-update-hook
928   (list 'gnus-summary-highlight-line)
929   "*A hook called when a summary line is changed.
930 The hook will not be called if `gnus-visual' is nil.
931
932 The default function `gnus-summary-highlight-line' will
933 highlight the line according to the `gnus-summary-highlight'
934 variable."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
939   "*A hook called when an article is selected for the first time.
940 The hook is intended to mark an article as read (or unread)
941 automatically when it is selected."
942   :group 'gnus-summary-choose
943   :type 'hook)
944
945 (defcustom gnus-group-no-more-groups-hook nil
946   "*A hook run when returning to group mode having no more (unread) groups."
947   :group 'gnus-group-select
948   :type 'hook)
949
950 (defcustom gnus-ps-print-hook nil
951   "*A hook run before ps-printing something from Gnus."
952   :group 'gnus-summary
953   :type 'hook)
954
955 (defcustom gnus-summary-article-move-hook nil
956   "*A hook called after an article is moved, copied, respooled, or crossposted."
957   :version "22.1"
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-article-delete-hook nil
962   "*A hook called after an article is deleted."
963   :version "22.1"
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :version "22.1"
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded)
995     (uncached
996      . gnus-summary-normal-undownloaded)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread)
1020     ((> score default-high)
1021      . gnus-summary-high-read)
1022     ((< score default-low)
1023      . gnus-summary-low-read)
1024     (t
1025      . gnus-summary-normal-read))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1055   "Function used to decode a string with encoded words.")
1056
1057 (defvar gnus-decode-encoded-address-function
1058   'mail-decode-encoded-address-string
1059   "Function used to decode addresses with encoded words.")
1060
1061 (defcustom gnus-extra-headers '(To Newsgroups)
1062   "*Extra headers to parse."
1063   :version "21.1"
1064   :group 'gnus-summary
1065   :type '(repeat symbol))
1066
1067 (defcustom gnus-ignored-from-addresses
1068   (and user-mail-address
1069        (not (string= user-mail-address ""))
1070        (regexp-quote user-mail-address))
1071   "*Regexp of From headers that may be suppressed in favor of To headers."
1072   :version "21.1"
1073   :group 'gnus-summary
1074   :type 'regexp)
1075
1076 (defcustom gnus-summary-to-prefix "-> "
1077   "*String prefixed to the To field in the summary line when
1078 using `gnus-ignored-from-addresses'."
1079   :version "22.1"
1080   :group 'gnus-summary
1081   :type 'string)
1082
1083 (defcustom gnus-summary-newsgroup-prefix "=> "
1084   "*String prefixed to the Newsgroup field in the summary
1085 line when using `gnus-ignored-from-addresses'."
1086   :version "22.1"
1087   :group 'gnus-summary
1088   :type 'string)
1089
1090 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1091   "List of charsets that should be ignored.
1092 When these charsets are used in the \"charset\" parameter, the
1093 default charset will be used instead."
1094   :version "21.1"
1095   :type '(repeat symbol)
1096   :group 'gnus-charset)
1097
1098 (gnus-define-group-parameter
1099  ignored-charsets
1100  :type list
1101  :function-document
1102  "Return the ignored charsets of GROUP."
1103  :variable gnus-group-ignored-charsets-alist
1104  :variable-default
1105  '(("alt\\.chinese\\.text" iso-8859-1))
1106  :variable-document
1107  "Alist of regexps (to match group names) and charsets that should be ignored.
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead."
1110  :variable-group gnus-charset
1111  :variable-type '(repeat (cons (regexp :tag "Group")
1112                                (repeat symbol)))
1113  :parameter-type '(choice :tag "Ignored charsets"
1114                           :value nil
1115                           (repeat (symbol)))
1116  :parameter-document       "\
1117 List of charsets that should be ignored.
1118
1119 When these charsets are used in the \"charset\" parameter, the
1120 default charset will be used instead.")
1121
1122 (defcustom gnus-group-highlight-words-alist nil
1123   "Alist of group regexps and highlight regexps.
1124 This variable uses the same syntax as `gnus-emphasis-alist'."
1125   :version "21.1"
1126   :type '(repeat (cons (regexp :tag "Group")
1127                        (repeat (list (regexp :tag "Highlight regexp")
1128                                      (number :tag "Group for entire word" 0)
1129                                      (number :tag "Group for displayed part" 0)
1130                                      (symbol :tag "Face"
1131                                              gnus-emphasis-highlight-words)))))
1132   :group 'gnus-summary-visual)
1133
1134 (defcustom gnus-summary-show-article-charset-alist
1135   nil
1136   "Alist of number and charset.
1137 The article will be shown with the charset corresponding to the
1138 numbered argument.
1139 For example: ((1 . cn-gb-2312) (2 . big5))."
1140   :version "21.1"
1141   :type '(repeat (cons (number :tag "Argument" 1)
1142                        (symbol :tag "Charset")))
1143   :group 'gnus-charset)
1144
1145 (defcustom gnus-preserve-marks t
1146   "Whether marks are preserved when moving, copying and respooling messages."
1147   :version "21.1"
1148   :type 'boolean
1149   :group 'gnus-summary-marks)
1150
1151 (defcustom gnus-alter-articles-to-read-function nil
1152   "Function to be called to alter the list of articles to be selected."
1153   :type '(choice (const nil) function)
1154   :group 'gnus-summary)
1155
1156 (defcustom gnus-orphan-score nil
1157   "*All orphans get this score added.  Set in the score file."
1158   :group 'gnus-score-default
1159   :type '(choice (const nil)
1160                  integer))
1161
1162 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1163   "*A regexp to match MIME parts when saving multiple parts of a
1164 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1165 This regexp will be used by default when prompting the user for which
1166 type of files to save."
1167   :group 'gnus-summary
1168   :type 'regexp)
1169
1170 (defcustom gnus-read-all-available-headers nil
1171   "Whether Gnus should parse all headers made available to it.
1172 This is mostly relevant for slow back ends where the user may
1173 wish to widen the summary buffer to include all headers
1174 that were fetched.  Say, for nnultimate groups."
1175   :version "22.1"
1176   :group 'gnus-summary
1177   :type '(choice boolean regexp))
1178
1179 (defcustom gnus-summary-muttprint-program "muttprint"
1180   "Command (and optional arguments) used to run Muttprint."
1181   :version "22.1"
1182   :group 'gnus-summary
1183   :type 'string)
1184
1185 (defcustom gnus-article-loose-mime t
1186   "If non-nil, don't require MIME-Version header.
1187 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1188 supply the MIME-Version header or deliberately strip it from the mail.
1189 If non-nil (the default), Gnus will treat some articles as MIME
1190 even if the MIME-Version header is missing."
1191   :version "22.1"
1192   :type 'boolean
1193   :group 'gnus-article-mime)
1194
1195 (defcustom gnus-article-emulate-mime t
1196   "If non-nil, use MIME emulation for uuencode and the like.
1197 This means that Gnus will search message bodies for text that look
1198 like uuencoded bits, yEncoded bits, and so on, and present that using
1199 the normal Gnus MIME machinery."
1200   :version "22.1"
1201   :type 'boolean
1202   :group 'gnus-article-mime)
1203
1204 ;;; Internal variables
1205
1206 (defvar gnus-summary-display-cache nil)
1207 (defvar gnus-article-mime-handles nil)
1208 (defvar gnus-article-decoded-p nil)
1209 (defvar gnus-article-charset nil)
1210 (defvar gnus-article-ignored-charsets nil)
1211 (defvar gnus-scores-exclude-files nil)
1212 (defvar gnus-page-broken nil)
1213
1214 (defvar gnus-original-article nil)
1215 (defvar gnus-article-internal-prepare-hook nil)
1216 (defvar gnus-newsgroup-process-stack nil)
1217
1218 (defvar gnus-thread-indent-array nil)
1219 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1220 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1221   "Function called to sort the articles within a thread after it has been gathered together.")
1222
1223 (defvar gnus-summary-save-parts-type-history nil)
1224 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1225
1226 ;; Avoid highlighting in kill files.
1227 (defvar gnus-summary-inhibit-highlight nil)
1228 (defvar gnus-newsgroup-selected-overlay nil)
1229 (defvar gnus-inhibit-limiting nil)
1230 (defvar gnus-newsgroup-adaptive-score-file nil)
1231 (defvar gnus-current-score-file nil)
1232 (defvar gnus-current-move-group nil)
1233 (defvar gnus-current-copy-group nil)
1234 (defvar gnus-current-crosspost-group nil)
1235 (defvar gnus-newsgroup-display nil)
1236
1237 (defvar gnus-newsgroup-dependencies nil)
1238 (defvar gnus-newsgroup-adaptive nil)
1239 (defvar gnus-summary-display-article-function nil)
1240 (defvar gnus-summary-highlight-line-function nil
1241   "Function called after highlighting a summary line.")
1242
1243 (defvar gnus-summary-line-format-alist
1244   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1245     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1246     (?s gnus-tmp-subject-or-nil ?s)
1247     (?n gnus-tmp-name ?s)
1248     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1249         ?s)
1250     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1251             gnus-tmp-from) ?s)
1252     (?F gnus-tmp-from ?s)
1253     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1254     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1255     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1256     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1257     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1258     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1259     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1260     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1261     (?L gnus-tmp-lines ?s)
1262     (?O gnus-tmp-downloaded ?c)
1263     (?I gnus-tmp-indentation ?s)
1264     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1265     (?R gnus-tmp-replied ?c)
1266     (?\[ gnus-tmp-opening-bracket ?c)
1267     (?\] gnus-tmp-closing-bracket ?c)
1268     (?\> (make-string gnus-tmp-level ? ) ?s)
1269     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1270     (?i gnus-tmp-score ?d)
1271     (?z gnus-tmp-score-char ?c)
1272     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1273     (?U gnus-tmp-unread ?c)
1274     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1275         ?s)
1276     (?t (gnus-summary-number-of-articles-in-thread
1277          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1278         ?d)
1279     (?e (gnus-summary-number-of-articles-in-thread
1280          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1281         ?c)
1282     (?u gnus-tmp-user-defined ?s)
1283     (?P (gnus-pick-line-number) ?d)
1284     (?B gnus-tmp-thread-tree-header-string ?s)
1285     (user-date (gnus-user-date
1286                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1287   "An alist of format specifications that can appear in summary lines.
1288 These are paired with what variables they correspond with, along with
1289 the type of the variable (string, integer, character, etc).")
1290
1291 (defvar gnus-summary-dummy-line-format-alist
1292   `((?S gnus-tmp-subject ?s)
1293     (?N gnus-tmp-number ?d)
1294     (?u gnus-tmp-user-defined ?s)))
1295
1296 (defvar gnus-summary-mode-line-format-alist
1297   `((?G gnus-tmp-group-name ?s)
1298     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1299     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1300     (?A gnus-tmp-article-number ?d)
1301     (?Z gnus-tmp-unread-and-unselected ?s)
1302     (?V gnus-version ?s)
1303     (?U gnus-tmp-unread-and-unticked ?d)
1304     (?S gnus-tmp-subject ?s)
1305     (?e gnus-tmp-unselected ?d)
1306     (?u gnus-tmp-user-defined ?s)
1307     (?d (length gnus-newsgroup-dormant) ?d)
1308     (?t (length gnus-newsgroup-marked) ?d)
1309     (?h (length gnus-newsgroup-spam-marked) ?d)
1310     (?r (length gnus-newsgroup-reads) ?d)
1311     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1312     (?E gnus-newsgroup-expunged-tally ?d)
1313     (?s (gnus-current-score-file-nondirectory) ?s)))
1314
1315 (defvar gnus-last-search-regexp nil
1316   "Default regexp for article search command.")
1317
1318 (defvar gnus-last-shell-command nil
1319   "Default shell command on article.")
1320
1321 (defvar gnus-newsgroup-agentized nil
1322   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1323 (defvar gnus-newsgroup-begin nil)
1324 (defvar gnus-newsgroup-end nil)
1325 (defvar gnus-newsgroup-last-rmail nil)
1326 (defvar gnus-newsgroup-last-mail nil)
1327 (defvar gnus-newsgroup-last-folder nil)
1328 (defvar gnus-newsgroup-last-file nil)
1329 (defvar gnus-newsgroup-last-directory nil)
1330 (defvar gnus-newsgroup-auto-expire nil)
1331 (defvar gnus-newsgroup-active nil)
1332
1333 (defvar gnus-newsgroup-data nil)
1334 (defvar gnus-newsgroup-data-reverse nil)
1335 (defvar gnus-newsgroup-limit nil)
1336 (defvar gnus-newsgroup-limits nil)
1337 (defvar gnus-summary-use-undownloaded-faces nil)
1338
1339 (defvar gnus-newsgroup-unreads nil
1340   "Sorted list of unread articles in the current newsgroup.")
1341
1342 (defvar gnus-newsgroup-unselected nil
1343   "Sorted list of unselected unread articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-reads nil
1346   "Alist of read articles and article marks in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-expunged-tally nil)
1349
1350 (defvar gnus-newsgroup-marked nil
1351   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1352
1353 (defvar gnus-newsgroup-spam-marked nil
1354   "List of ranges of articles that have been marked as spam.")
1355
1356 (defvar gnus-newsgroup-killed nil
1357   "List of ranges of articles that have been through the scoring process.")
1358
1359 (defvar gnus-newsgroup-cached nil
1360   "Sorted list of articles that come from the article cache.")
1361
1362 (defvar gnus-newsgroup-saved nil
1363   "List of articles that have been saved.")
1364
1365 (defvar gnus-newsgroup-kill-headers nil)
1366
1367 (defvar gnus-newsgroup-replied nil
1368   "List of articles that have been replied to in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-forwarded nil
1371   "List of articles that have been forwarded in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-recent nil
1374   "List of articles that have are recent in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-expirable nil
1377   "Sorted list of articles in the current newsgroup that can be expired.")
1378
1379 (defvar gnus-newsgroup-processable nil
1380   "List of articles in the current newsgroup that can be processed.")
1381
1382 (defvar gnus-newsgroup-downloadable nil
1383   "Sorted list of articles in the current newsgroup that can be processed.")
1384
1385 (defvar gnus-newsgroup-unfetched nil
1386   "Sorted list of articles in the current newsgroup whose headers have
1387 not been fetched into the agent.
1388
1389 This list will always be a subset of gnus-newsgroup-undownloaded.")
1390
1391 (defvar gnus-newsgroup-undownloaded nil
1392   "List of articles in the current newsgroup that haven't been downloaded.")
1393
1394 (defvar gnus-newsgroup-unsendable nil
1395   "List of articles in the current newsgroup that won't be sent.")
1396
1397 (defvar gnus-newsgroup-bookmarks nil
1398   "List of articles in the current newsgroup that have bookmarks.")
1399
1400 (defvar gnus-newsgroup-dormant nil
1401   "Sorted list of dormant articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-unseen nil
1404   "List of unseen articles in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-seen nil
1407   "Range of seen articles in the current newsgroup.")
1408
1409 (defvar gnus-newsgroup-articles nil
1410   "List of articles in the current newsgroup.")
1411
1412 (defvar gnus-newsgroup-scored nil
1413   "List of scored articles in the current newsgroup.")
1414
1415 (defvar gnus-newsgroup-headers nil
1416   "List of article headers in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-threads nil)
1419
1420 (defvar gnus-newsgroup-prepared nil
1421   "Whether the current group has been prepared properly.")
1422
1423 (defvar gnus-newsgroup-ancient nil
1424   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-sparse nil)
1427
1428 (defvar gnus-current-article nil)
1429 (defvar gnus-article-current nil)
1430 (defvar gnus-current-headers nil)
1431 (defvar gnus-have-all-headers nil)
1432 (defvar gnus-last-article nil)
1433 (defvar gnus-newsgroup-history nil)
1434 (defvar gnus-newsgroup-charset nil)
1435 (defvar gnus-newsgroup-ephemeral-charset nil)
1436 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1437
1438 (defvar gnus-article-before-search nil)
1439
1440 (defvar gnus-summary-local-variables
1441   '(gnus-newsgroup-name
1442     gnus-newsgroup-begin gnus-newsgroup-end
1443     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1444     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1445     gnus-newsgroup-last-directory
1446     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1447     gnus-newsgroup-unselected gnus-newsgroup-marked
1448     gnus-newsgroup-spam-marked
1449     gnus-newsgroup-reads gnus-newsgroup-saved
1450     gnus-newsgroup-replied gnus-newsgroup-forwarded
1451     gnus-newsgroup-recent
1452     gnus-newsgroup-expirable
1453     gnus-newsgroup-processable gnus-newsgroup-killed
1454     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1455     gnus-newsgroup-unfetched
1456     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1457     gnus-newsgroup-seen gnus-newsgroup-articles
1458     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1459     gnus-newsgroup-headers gnus-newsgroup-threads
1460     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1461     gnus-current-article gnus-current-headers gnus-have-all-headers
1462     gnus-last-article gnus-article-internal-prepare-hook
1463     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1464     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1465     gnus-thread-expunge-below
1466     gnus-score-alist gnus-current-score-file
1467     (gnus-summary-expunge-below . global)
1468     (gnus-summary-mark-below . global)
1469     (gnus-orphan-score . global)
1470     gnus-newsgroup-active gnus-scores-exclude-files
1471     gnus-newsgroup-history gnus-newsgroup-ancient
1472     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1473     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1474     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1475     (gnus-newsgroup-expunged-tally . 0)
1476     gnus-cache-removable-articles gnus-newsgroup-cached
1477     gnus-newsgroup-data gnus-newsgroup-data-reverse
1478     gnus-newsgroup-limit gnus-newsgroup-limits
1479     gnus-newsgroup-charset gnus-newsgroup-display
1480     gnus-summary-use-undownloaded-faces)
1481   "Variables that are buffer-local to the summary buffers.")
1482
1483 (defvar gnus-newsgroup-variables nil
1484   "A list of variables that have separate values in different newsgroups.
1485 A list of newsgroup (summary buffer) local variables, or cons of
1486 variables and their default expressions to be evalled (when the default
1487 values are not nil), that should be made global while the summary buffer
1488 is active.
1489
1490 Note: The default expressions will be evaluated (using function `eval')
1491 before assignment to the local variable rather than just assigned to it.
1492 If the default expression is the symbol `global', that symbol will not
1493 be evaluated but the global value of the local variable will be used
1494 instead.
1495
1496 These variables can be used to set variables in the group parameters
1497 while still allowing them to affect operations done in other buffers.
1498 For example:
1499
1500 \(setq gnus-newsgroup-variables
1501      '(message-use-followup-to
1502        (gnus-visible-headers .
1503          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1504 ")
1505
1506 ;; Byte-compiler warning.
1507 (eval-when-compile
1508   ;; Bind features so that require will believe that gnus-sum has
1509   ;; already been loaded (avoids infinite recursion)
1510   (let ((features (cons 'gnus-sum features)))
1511     ;; Several of the declarations in gnus-sum are needed to load the
1512     ;; following files. Right now, these definitions have been
1513     ;; compiled but not defined (evaluated).  We could either do a
1514     ;; eval-and-compile about all of the declarations or evaluate the
1515     ;; source file.
1516     (if (boundp 'gnus-newsgroup-variables)
1517         nil
1518       (load "gnus-sum.el" t t t))
1519     (require 'gnus)
1520     (require 'gnus-art)))
1521
1522 ;; MIME stuff.
1523
1524 (defvar gnus-decode-encoded-word-methods
1525   '(mail-decode-encoded-word-string)
1526   "List of methods used to decode encoded words.
1527
1528 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1529 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1530 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1531 whose names match REGEXP.
1532
1533 For example:
1534 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1535  mail-decode-encoded-word-string
1536  (\"chinese\" . rfc1843-decode-string))")
1537
1538 (defvar gnus-decode-encoded-word-methods-cache nil)
1539
1540 (defun gnus-multi-decode-encoded-word-string (string)
1541   "Apply the functions from `gnus-encoded-word-methods' that match."
1542   (unless (and gnus-decode-encoded-word-methods-cache
1543                (eq gnus-newsgroup-name
1544                    (car gnus-decode-encoded-word-methods-cache)))
1545     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1546     (mapcar (lambda (x)
1547               (if (symbolp x)
1548                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1549                 (if (and gnus-newsgroup-name
1550                          (string-match (car x) gnus-newsgroup-name))
1551                     (nconc gnus-decode-encoded-word-methods-cache
1552                            (list (cdr x))))))
1553             gnus-decode-encoded-word-methods))
1554   (let ((xlist gnus-decode-encoded-word-methods-cache))
1555     (pop xlist)
1556     (while xlist
1557       (setq string (funcall (pop xlist) string))))
1558   string)
1559
1560 ;; Subject simplification.
1561
1562 (defun gnus-simplify-whitespace (str)
1563   "Remove excessive whitespace from STR."
1564   ;; Multiple spaces.
1565   (while (string-match "[ \t][ \t]+" str)
1566     (setq str (concat (substring str 0 (match-beginning 0))
1567                         " "
1568                         (substring str (match-end 0)))))
1569   ;; Leading spaces.
1570   (when (string-match "^[ \t]+" str)
1571     (setq str (substring str (match-end 0))))
1572   ;; Trailing spaces.
1573   (when (string-match "[ \t]+$" str)
1574     (setq str (substring str 0 (match-beginning 0))))
1575   str)
1576
1577 (defun gnus-simplify-all-whitespace (str)
1578   "Remove all whitespace from STR."
1579   (while (string-match "[ \t\n]+" str)
1580     (setq str (replace-match "" nil nil str)))
1581   str)
1582
1583 (defsubst gnus-simplify-subject-re (subject)
1584   "Remove \"Re:\" from subject lines."
1585   (if (string-match message-subject-re-regexp subject)
1586       (substring subject (match-end 0))
1587     subject))
1588
1589 (defun gnus-simplify-subject (subject &optional re-only)
1590   "Remove `Re:' and words in parentheses.
1591 If RE-ONLY is non-nil, strip leading `Re:'s only."
1592   (let ((case-fold-search t))           ;Ignore case.
1593     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1594     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1595       (setq subject (substring subject (match-end 0))))
1596     ;; Remove uninteresting prefixes.
1597     (when (and (not re-only)
1598                gnus-simplify-ignored-prefixes
1599                (string-match gnus-simplify-ignored-prefixes subject))
1600       (setq subject (substring subject (match-end 0))))
1601     ;; Remove words in parentheses from end.
1602     (unless re-only
1603       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1604         (setq subject (substring subject 0 (match-beginning 0)))))
1605     ;; Return subject string.
1606     subject))
1607
1608 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1609 ;; all whitespace.
1610 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1611   (goto-char (point-min))
1612   (while (re-search-forward regexp nil t)
1613     (replace-match (or newtext ""))))
1614
1615 (defun gnus-simplify-buffer-fuzzy ()
1616   "Simplify string in the buffer fuzzily.
1617 The string in the accessible portion of the current buffer is simplified.
1618 It is assumed to be a single-line subject.
1619 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1620 matter is removed.  Additional things can be deleted by setting
1621 `gnus-simplify-subject-fuzzy-regexp'."
1622   (let ((case-fold-search t)
1623         (modified-tick))
1624     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1625
1626     (while (not (eq modified-tick (buffer-modified-tick)))
1627       (setq modified-tick (buffer-modified-tick))
1628       (cond
1629        ((listp gnus-simplify-subject-fuzzy-regexp)
1630         (mapcar 'gnus-simplify-buffer-fuzzy-step
1631                 gnus-simplify-subject-fuzzy-regexp))
1632        (gnus-simplify-subject-fuzzy-regexp
1633         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1634       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1635       (gnus-simplify-buffer-fuzzy-step
1636        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1637       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1638
1639     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1640     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1641     (gnus-simplify-buffer-fuzzy-step " $")
1642     (gnus-simplify-buffer-fuzzy-step "^ +")))
1643
1644 (defun gnus-simplify-subject-fuzzy (subject)
1645   "Simplify a subject string fuzzily.
1646 See `gnus-simplify-buffer-fuzzy' for details."
1647   (save-excursion
1648     (gnus-set-work-buffer)
1649     (let ((case-fold-search t))
1650       ;; Remove uninteresting prefixes.
1651       (when (and gnus-simplify-ignored-prefixes
1652                  (string-match gnus-simplify-ignored-prefixes subject))
1653         (setq subject (substring subject (match-end 0))))
1654       (insert subject)
1655       (inline (gnus-simplify-buffer-fuzzy))
1656       (buffer-string))))
1657
1658 (defsubst gnus-simplify-subject-fully (subject)
1659   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1660   (cond
1661    (gnus-simplify-subject-functions
1662     (gnus-map-function gnus-simplify-subject-functions subject))
1663    ((null gnus-summary-gather-subject-limit)
1664     (gnus-simplify-subject-re subject))
1665    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1666     (gnus-simplify-subject-fuzzy subject))
1667    ((numberp gnus-summary-gather-subject-limit)
1668     (gnus-limit-string (gnus-simplify-subject-re subject)
1669                        gnus-summary-gather-subject-limit))
1670    (t
1671     subject)))
1672
1673 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1674   "Check whether two subjects are equal.
1675 If optional argument SIMPLE-FIRST is t, first argument is already
1676 simplified."
1677   (cond
1678    ((null simple-first)
1679     (equal (gnus-simplify-subject-fully s1)
1680            (gnus-simplify-subject-fully s2)))
1681    (t
1682     (equal s1
1683            (gnus-simplify-subject-fully s2)))))
1684
1685 (defun gnus-summary-bubble-group ()
1686   "Increase the score of the current group.
1687 This is a handy function to add to `gnus-summary-exit-hook' to
1688 increase the score of each group you read."
1689   (gnus-group-add-score gnus-newsgroup-name))
1690
1691 \f
1692 ;;;
1693 ;;; Gnus summary mode
1694 ;;;
1695
1696 (put 'gnus-summary-mode 'mode-class 'special)
1697
1698 (defvar gnus-article-commands-menu)
1699
1700 ;; Non-orthogonal keys
1701
1702 (gnus-define-keys gnus-summary-mode-map
1703   " " gnus-summary-next-page
1704   "\177" gnus-summary-prev-page
1705   [delete] gnus-summary-prev-page
1706   [backspace] gnus-summary-prev-page
1707   "\r" gnus-summary-scroll-up
1708   "\M-\r" gnus-summary-scroll-down
1709   "n" gnus-summary-next-unread-article
1710   "p" gnus-summary-prev-unread-article
1711   "N" gnus-summary-next-article
1712   "P" gnus-summary-prev-article
1713   "\M-\C-n" gnus-summary-next-same-subject
1714   "\M-\C-p" gnus-summary-prev-same-subject
1715   "\M-n" gnus-summary-next-unread-subject
1716   "\M-p" gnus-summary-prev-unread-subject
1717   "." gnus-summary-first-unread-article
1718   "," gnus-summary-best-unread-article
1719   "\M-s" gnus-summary-search-article-forward
1720   "\M-r" gnus-summary-search-article-backward
1721   "\M-S" gnus-summary-repeat-search-article-forward
1722   "\M-R" gnus-summary-repeat-search-article-backward
1723   "<" gnus-summary-beginning-of-article
1724   ">" gnus-summary-end-of-article
1725   "j" gnus-summary-goto-article
1726   "^" gnus-summary-refer-parent-article
1727   "\M-^" gnus-summary-refer-article
1728   "u" gnus-summary-tick-article-forward
1729   "!" gnus-summary-tick-article-forward
1730   "U" gnus-summary-tick-article-backward
1731   "d" gnus-summary-mark-as-read-forward
1732   "D" gnus-summary-mark-as-read-backward
1733   "E" gnus-summary-mark-as-expirable
1734   "\M-u" gnus-summary-clear-mark-forward
1735   "\M-U" gnus-summary-clear-mark-backward
1736   "k" gnus-summary-kill-same-subject-and-select
1737   "\C-k" gnus-summary-kill-same-subject
1738   "\M-\C-k" gnus-summary-kill-thread
1739   "\M-\C-l" gnus-summary-lower-thread
1740   "e" gnus-summary-edit-article
1741   "#" gnus-summary-mark-as-processable
1742   "\M-#" gnus-summary-unmark-as-processable
1743   "\M-\C-t" gnus-summary-toggle-threads
1744   "\M-\C-s" gnus-summary-show-thread
1745   "\M-\C-h" gnus-summary-hide-thread
1746   "\M-\C-f" gnus-summary-next-thread
1747   "\M-\C-b" gnus-summary-prev-thread
1748   [(meta down)] gnus-summary-next-thread
1749   [(meta up)] gnus-summary-prev-thread
1750   "\M-\C-u" gnus-summary-up-thread
1751   "\M-\C-d" gnus-summary-down-thread
1752   "&" gnus-summary-execute-command
1753   "c" gnus-summary-catchup-and-exit
1754   "\C-w" gnus-summary-mark-region-as-read
1755   "\C-t" gnus-summary-toggle-truncation
1756   "?" gnus-summary-mark-as-dormant
1757   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1758   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1759   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1760   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1761   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1762   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1763   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1764   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1765   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1766   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1767   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1768   "=" gnus-summary-expand-window
1769   "\C-x\C-s" gnus-summary-reselect-current-group
1770   "\M-g" gnus-summary-rescan-group
1771   "w" gnus-summary-stop-page-breaking
1772   "\C-c\C-r" gnus-summary-caesar-message
1773   "f" gnus-summary-followup
1774   "F" gnus-summary-followup-with-original
1775   "C" gnus-summary-cancel-article
1776   "r" gnus-summary-reply
1777   "R" gnus-summary-reply-with-original
1778   "\C-c\C-f" gnus-summary-mail-forward
1779   "o" gnus-summary-save-article
1780   "\C-o" gnus-summary-save-article-mail
1781   "|" gnus-summary-pipe-output
1782   "\M-k" gnus-summary-edit-local-kill
1783   "\M-K" gnus-summary-edit-global-kill
1784   ;; "V" gnus-version
1785   "\C-c\C-d" gnus-summary-describe-group
1786   "q" gnus-summary-exit
1787   "Q" gnus-summary-exit-no-update
1788   "\C-c\C-i" gnus-info-find-node
1789   gnus-mouse-2 gnus-mouse-pick-article
1790   [follow-link] mouse-face
1791   "m" gnus-summary-mail-other-window
1792   "a" gnus-summary-post-news
1793   "i" gnus-summary-news-other-window
1794   "x" gnus-summary-limit-to-unread
1795   "s" gnus-summary-isearch-article
1796   "t" gnus-summary-toggle-header
1797   "g" gnus-summary-show-article
1798   "l" gnus-summary-goto-last-article
1799   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1800   "\C-d" gnus-summary-enter-digest-group
1801   "\M-\C-d" gnus-summary-read-document
1802   "\M-\C-e" gnus-summary-edit-parameters
1803   "\M-\C-a" gnus-summary-customize-parameters
1804   "\C-c\C-b" gnus-bug
1805   "*" gnus-cache-enter-article
1806   "\M-*" gnus-cache-remove-article
1807   "\M-&" gnus-summary-universal-argument
1808   "\C-l" gnus-recenter
1809   "I" gnus-summary-increase-score
1810   "L" gnus-summary-lower-score
1811   "\M-i" gnus-symbolic-argument
1812   "h" gnus-summary-select-article-buffer
1813
1814   "b" gnus-article-view-part
1815   "\M-t" gnus-summary-toggle-display-buttonized
1816
1817   "V" gnus-summary-score-map
1818   "X" gnus-uu-extract-map
1819   "S" gnus-summary-send-map)
1820
1821 ;; Sort of orthogonal keymap
1822 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1823   "t" gnus-summary-tick-article-forward
1824   "!" gnus-summary-tick-article-forward
1825   "d" gnus-summary-mark-as-read-forward
1826   "r" gnus-summary-mark-as-read-forward
1827   "c" gnus-summary-clear-mark-forward
1828   " " gnus-summary-clear-mark-forward
1829   "e" gnus-summary-mark-as-expirable
1830   "x" gnus-summary-mark-as-expirable
1831   "?" gnus-summary-mark-as-dormant
1832   "b" gnus-summary-set-bookmark
1833   "B" gnus-summary-remove-bookmark
1834   "#" gnus-summary-mark-as-processable
1835   "\M-#" gnus-summary-unmark-as-processable
1836   "S" gnus-summary-limit-include-expunged
1837   "C" gnus-summary-catchup
1838   "H" gnus-summary-catchup-to-here
1839   "h" gnus-summary-catchup-from-here
1840   "\C-c" gnus-summary-catchup-all
1841   "k" gnus-summary-kill-same-subject-and-select
1842   "K" gnus-summary-kill-same-subject
1843   "P" gnus-uu-mark-map)
1844
1845 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1846   "c" gnus-summary-clear-above
1847   "u" gnus-summary-tick-above
1848   "m" gnus-summary-mark-above
1849   "k" gnus-summary-kill-below)
1850
1851 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1852   "/" gnus-summary-limit-to-subject
1853   "n" gnus-summary-limit-to-articles
1854   "b" gnus-summary-limit-to-bodies
1855   "h" gnus-summary-limit-to-headers
1856   "w" gnus-summary-pop-limit
1857   "s" gnus-summary-limit-to-subject
1858   "a" gnus-summary-limit-to-author
1859   "u" gnus-summary-limit-to-unread
1860   "m" gnus-summary-limit-to-marks
1861   "M" gnus-summary-limit-exclude-marks
1862   "v" gnus-summary-limit-to-score
1863   "*" gnus-summary-limit-include-cached
1864   "D" gnus-summary-limit-include-dormant
1865   "T" gnus-summary-limit-include-thread
1866   "d" gnus-summary-limit-exclude-dormant
1867   "t" gnus-summary-limit-to-age
1868   "." gnus-summary-limit-to-unseen
1869   "x" gnus-summary-limit-to-extra
1870   "p" gnus-summary-limit-to-display-predicate
1871   "E" gnus-summary-limit-include-expunged
1872   "c" gnus-summary-limit-exclude-childless-dormant
1873   "C" gnus-summary-limit-mark-excluded-as-read
1874   "o" gnus-summary-insert-old-articles
1875   "N" gnus-summary-insert-new-articles
1876   "r" gnus-summary-limit-to-replied
1877   "R" gnus-summary-limit-to-recipient)
1878
1879 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1880   "n" gnus-summary-next-unread-article
1881   "p" gnus-summary-prev-unread-article
1882   "N" gnus-summary-next-article
1883   "P" gnus-summary-prev-article
1884   "\C-n" gnus-summary-next-same-subject
1885   "\C-p" gnus-summary-prev-same-subject
1886   "\M-n" gnus-summary-next-unread-subject
1887   "\M-p" gnus-summary-prev-unread-subject
1888   "f" gnus-summary-first-unread-article
1889   "b" gnus-summary-best-unread-article
1890   "j" gnus-summary-goto-article
1891   "g" gnus-summary-goto-subject
1892   "l" gnus-summary-goto-last-article
1893   "o" gnus-summary-pop-article)
1894
1895 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1896   "k" gnus-summary-kill-thread
1897   "l" gnus-summary-lower-thread
1898   "i" gnus-summary-raise-thread
1899   "T" gnus-summary-toggle-threads
1900   "t" gnus-summary-rethread-current
1901   "^" gnus-summary-reparent-thread
1902   "\M-^" gnus-summary-reparent-children
1903   "s" gnus-summary-show-thread
1904   "S" gnus-summary-show-all-threads
1905   "h" gnus-summary-hide-thread
1906   "H" gnus-summary-hide-all-threads
1907   "n" gnus-summary-next-thread
1908   "p" gnus-summary-prev-thread
1909   "u" gnus-summary-up-thread
1910   "o" gnus-summary-top-thread
1911   "d" gnus-summary-down-thread
1912   "#" gnus-uu-mark-thread
1913   "\M-#" gnus-uu-unmark-thread)
1914
1915 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1916   "g" gnus-summary-prepare
1917   "c" gnus-summary-insert-cached-articles
1918   "d" gnus-summary-insert-dormant-articles)
1919
1920 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1921   "c" gnus-summary-catchup-and-exit
1922   "C" gnus-summary-catchup-all-and-exit
1923   "E" gnus-summary-exit-no-update
1924   "Q" gnus-summary-exit
1925   "Z" gnus-summary-exit
1926   "n" gnus-summary-catchup-and-goto-next-group
1927   "p" gnus-summary-catchup-and-goto-prev-group
1928   "R" gnus-summary-reselect-current-group
1929   "G" gnus-summary-rescan-group
1930   "N" gnus-summary-next-group
1931   "s" gnus-summary-save-newsrc
1932   "P" gnus-summary-prev-group)
1933
1934 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1935   " " gnus-summary-next-page
1936   "n" gnus-summary-next-page
1937   "\177" gnus-summary-prev-page
1938   [delete] gnus-summary-prev-page
1939   "p" gnus-summary-prev-page
1940   "\r" gnus-summary-scroll-up
1941   "\M-\r" gnus-summary-scroll-down
1942   "<" gnus-summary-beginning-of-article
1943   ">" gnus-summary-end-of-article
1944   "b" gnus-summary-beginning-of-article
1945   "e" gnus-summary-end-of-article
1946   "^" gnus-summary-refer-parent-article
1947   "r" gnus-summary-refer-parent-article
1948   "D" gnus-summary-enter-digest-group
1949   "R" gnus-summary-refer-references
1950   "T" gnus-summary-refer-thread
1951   "g" gnus-summary-show-article
1952   "s" gnus-summary-isearch-article
1953   "P" gnus-summary-print-article
1954   "M" gnus-mailing-list-insinuate
1955   "t" gnus-article-babel)
1956
1957 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1958   "b" gnus-article-add-buttons
1959   "B" gnus-article-add-buttons-to-head
1960   "o" gnus-article-treat-overstrike
1961   "e" gnus-article-emphasize
1962   "w" gnus-article-fill-cited-article
1963   "Q" gnus-article-fill-long-lines
1964   "L" gnus-article-toggle-truncate-lines
1965   "C" gnus-article-capitalize-sentences
1966   "c" gnus-article-remove-cr
1967   "q" gnus-article-de-quoted-unreadable
1968   "6" gnus-article-de-base64-unreadable
1969   "Z" gnus-article-decode-HZ
1970   "A" gnus-article-treat-ansi-sequences
1971   "h" gnus-article-wash-html
1972   "u" gnus-article-unsplit-urls
1973   "s" gnus-summary-force-verify-and-decrypt
1974   "f" gnus-article-display-x-face
1975   "l" gnus-summary-stop-page-breaking
1976   "r" gnus-summary-caesar-message
1977   "m" gnus-summary-morse-message
1978   "t" gnus-summary-toggle-header
1979   "g" gnus-treat-smiley
1980   "v" gnus-summary-verbose-headers
1981   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1982   "p" gnus-article-verify-x-pgp-sig
1983   "d" gnus-article-treat-dumbquotes
1984   "i" gnus-summary-idna-message)
1985
1986 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1987   ;; mnemonic: deuglif*Y*
1988   "u" gnus-article-outlook-unwrap-lines
1989   "a" gnus-article-outlook-repair-attribution
1990   "c" gnus-article-outlook-rearrange-citation
1991   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1992
1993 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1994   "a" gnus-article-hide
1995   "h" gnus-article-hide-headers
1996   "b" gnus-article-hide-boring-headers
1997   "s" gnus-article-hide-signature
1998   "c" gnus-article-hide-citation
1999   "C" gnus-article-hide-citation-in-followups
2000   "l" gnus-article-hide-list-identifiers
2001   "B" gnus-article-strip-banner
2002   "P" gnus-article-hide-pem
2003   "\C-c" gnus-article-hide-citation-maybe)
2004
2005 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2006   "a" gnus-article-highlight
2007   "h" gnus-article-highlight-headers
2008   "c" gnus-article-highlight-citation
2009   "s" gnus-article-highlight-signature)
2010
2011 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2012   "f" gnus-article-treat-fold-headers
2013   "u" gnus-article-treat-unfold-headers
2014   "n" gnus-article-treat-fold-newsgroups)
2015
2016 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2017   "x" gnus-article-display-x-face
2018   "d" gnus-article-display-face
2019   "s" gnus-treat-smiley
2020   "D" gnus-article-remove-images
2021   "f" gnus-treat-from-picon
2022   "m" gnus-treat-mail-picon
2023   "n" gnus-treat-newsgroups-picon)
2024
2025 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2026   "w" gnus-article-decode-mime-words
2027   "c" gnus-article-decode-charset
2028   "v" gnus-mime-view-all-parts
2029   "b" gnus-article-view-part)
2030
2031 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2032   "z" gnus-article-date-ut
2033   "u" gnus-article-date-ut
2034   "l" gnus-article-date-local
2035   "p" gnus-article-date-english
2036   "e" gnus-article-date-lapsed
2037   "o" gnus-article-date-original
2038   "i" gnus-article-date-iso8601
2039   "s" gnus-article-date-user)
2040
2041 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2042   "t" gnus-article-remove-trailing-blank-lines
2043   "l" gnus-article-strip-leading-blank-lines
2044   "m" gnus-article-strip-multiple-blank-lines
2045   "a" gnus-article-strip-blank-lines
2046   "A" gnus-article-strip-all-blank-lines
2047   "s" gnus-article-strip-leading-space
2048   "e" gnus-article-strip-trailing-space
2049   "w" gnus-article-remove-leading-whitespace)
2050
2051 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2052   "v" gnus-version
2053   "f" gnus-summary-fetch-faq
2054   "d" gnus-summary-describe-group
2055   "h" gnus-summary-describe-briefly
2056   "i" gnus-info-find-node
2057   "c" gnus-group-fetch-charter
2058   "C" gnus-group-fetch-control)
2059
2060 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2061   "e" gnus-summary-expire-articles
2062   "\M-\C-e" gnus-summary-expire-articles-now
2063   "\177" gnus-summary-delete-article
2064   [delete] gnus-summary-delete-article
2065   [backspace] gnus-summary-delete-article
2066   "m" gnus-summary-move-article
2067   "r" gnus-summary-respool-article
2068   "w" gnus-summary-edit-article
2069   "c" gnus-summary-copy-article
2070   "B" gnus-summary-crosspost-article
2071   "q" gnus-summary-respool-query
2072   "t" gnus-summary-respool-trace
2073   "i" gnus-summary-import-article
2074   "I" gnus-summary-create-article
2075   "p" gnus-summary-article-posted-p)
2076
2077 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2078   "o" gnus-summary-save-article
2079   "m" gnus-summary-save-article-mail
2080   "F" gnus-summary-write-article-file
2081   "r" gnus-summary-save-article-rmail
2082   "f" gnus-summary-save-article-file
2083   "b" gnus-summary-save-article-body-file
2084   "B" gnus-summary-write-article-body-file
2085   "h" gnus-summary-save-article-folder
2086   "v" gnus-summary-save-article-vm
2087   "p" gnus-summary-pipe-output
2088   "P" gnus-summary-muttprint
2089   "s" gnus-soup-add-article)
2090
2091 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2092   "b" gnus-summary-display-buttonized
2093   "m" gnus-summary-repair-multipart
2094   "v" gnus-article-view-part
2095   "o" gnus-article-save-part
2096   "O" gnus-article-save-part-and-strip
2097   "r" gnus-article-replace-part
2098   "d" gnus-article-delete-part
2099   "t" gnus-article-view-part-as-type
2100   "j" gnus-article-jump-to-part
2101   "c" gnus-article-copy-part
2102   "C" gnus-article-view-part-as-charset
2103   "e" gnus-article-view-part-externally
2104   "H" gnus-article-browse-html-article
2105   "E" gnus-article-encrypt-body
2106   "i" gnus-article-inline-part
2107   "|" gnus-article-pipe-part)
2108
2109 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2110   "p" gnus-summary-mark-as-processable
2111   "u" gnus-summary-unmark-as-processable
2112   "U" gnus-summary-unmark-all-processable
2113   "v" gnus-uu-mark-over
2114   "s" gnus-uu-mark-series
2115   "r" gnus-uu-mark-region
2116   "g" gnus-uu-unmark-region
2117   "R" gnus-uu-mark-by-regexp
2118   "G" gnus-uu-unmark-by-regexp
2119   "t" gnus-uu-mark-thread
2120   "T" gnus-uu-unmark-thread
2121   "a" gnus-uu-mark-all
2122   "b" gnus-uu-mark-buffer
2123   "S" gnus-uu-mark-sparse
2124   "k" gnus-summary-kill-process-mark
2125   "y" gnus-summary-yank-process-mark
2126   "w" gnus-summary-save-process-mark
2127   "i" gnus-uu-invert-processable)
2128
2129 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2130   ;;"x" gnus-uu-extract-any
2131   "m" gnus-summary-save-parts
2132   "u" gnus-uu-decode-uu
2133   "U" gnus-uu-decode-uu-and-save
2134   "s" gnus-uu-decode-unshar
2135   "S" gnus-uu-decode-unshar-and-save
2136   "o" gnus-uu-decode-save
2137   "O" gnus-uu-decode-save
2138   "b" gnus-uu-decode-binhex
2139   "B" gnus-uu-decode-binhex
2140   "p" gnus-uu-decode-postscript
2141   "P" gnus-uu-decode-postscript-and-save)
2142
2143 (gnus-define-keys
2144     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2145   "u" gnus-uu-decode-uu-view
2146   "U" gnus-uu-decode-uu-and-save-view
2147   "s" gnus-uu-decode-unshar-view
2148   "S" gnus-uu-decode-unshar-and-save-view
2149   "o" gnus-uu-decode-save-view
2150   "O" gnus-uu-decode-save-view
2151   "b" gnus-uu-decode-binhex-view
2152   "B" gnus-uu-decode-binhex-view
2153   "p" gnus-uu-decode-postscript-view
2154   "P" gnus-uu-decode-postscript-and-save-view)
2155
2156 (defvar gnus-article-post-menu nil)
2157
2158 (defconst gnus-summary-menu-maxlen 20)
2159
2160 (defun gnus-summary-menu-split (menu)
2161   ;; If we have lots of elements, divide them into groups of 20
2162   ;; and make a pane (or submenu) for each one.
2163   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2164       (let ((menu menu) sublists next
2165             (i 1))
2166         (while menu
2167           ;; Pull off the next gnus-summary-menu-maxlen elements
2168           ;; and make them the next element of sublist.
2169           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2170           (if next
2171               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2172                       nil))
2173           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2174                                              (aref (car (last menu)) 0)) menu)
2175                                sublists))
2176           (setq i (1+ i))
2177           (setq menu next))
2178         (nreverse sublists))
2179     ;; Few elements--put them all in one pane.
2180     menu))
2181
2182 (defun gnus-summary-make-menu-bar ()
2183   (gnus-turn-off-edit-menu 'summary)
2184
2185   (unless (boundp 'gnus-summary-misc-menu)
2186
2187     (easy-menu-define
2188       gnus-summary-kill-menu gnus-summary-mode-map ""
2189       (cons
2190        "Score"
2191        (nconc
2192         (list
2193          ["Customize" gnus-score-customize t])
2194         (gnus-make-score-map 'increase)
2195         (gnus-make-score-map 'lower)
2196         '(("Mark"
2197            ["Kill below" gnus-summary-kill-below t]
2198            ["Mark above" gnus-summary-mark-above t]
2199            ["Tick above" gnus-summary-tick-above t]
2200            ["Clear above" gnus-summary-clear-above t])
2201           ["Current score" gnus-summary-current-score t]
2202           ["Set score" gnus-summary-set-score t]
2203           ["Switch current score file..." gnus-score-change-score-file t]
2204           ["Set mark below..." gnus-score-set-mark-below t]
2205           ["Set expunge below..." gnus-score-set-expunge-below t]
2206           ["Edit current score file" gnus-score-edit-current-scores t]
2207           ["Edit score file" gnus-score-edit-file t]
2208           ["Trace score" gnus-score-find-trace t]
2209           ["Find words" gnus-score-find-favourite-words t]
2210           ["Rescore buffer" gnus-summary-rescore t]
2211           ["Increase score..." gnus-summary-increase-score t]
2212           ["Lower score..." gnus-summary-lower-score t]))))
2213
2214     ;; Define both the Article menu in the summary buffer and the
2215     ;; equivalent Commands menu in the article buffer here for
2216     ;; consistency.
2217     (let ((innards
2218            `(("Hide"
2219               ["All" gnus-article-hide t]
2220               ["Headers" gnus-article-hide-headers t]
2221               ["Signature" gnus-article-hide-signature t]
2222               ["Citation" gnus-article-hide-citation t]
2223               ["List identifiers" gnus-article-hide-list-identifiers t]
2224               ["Banner" gnus-article-strip-banner t]
2225               ["Boring headers" gnus-article-hide-boring-headers t])
2226              ("Highlight"
2227               ["All" gnus-article-highlight t]
2228               ["Headers" gnus-article-highlight-headers t]
2229               ["Signature" gnus-article-highlight-signature t]
2230               ["Citation" gnus-article-highlight-citation t])
2231              ("MIME"
2232               ["Words" gnus-article-decode-mime-words t]
2233               ["Charset" gnus-article-decode-charset t]
2234               ["QP" gnus-article-de-quoted-unreadable t]
2235               ["Base64" gnus-article-de-base64-unreadable t]
2236               ["View MIME buttons" gnus-summary-display-buttonized t]
2237               ["View all" gnus-mime-view-all-parts t]
2238               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2239               ["Encrypt body" gnus-article-encrypt-body
2240                :active (not (gnus-group-read-only-p))
2241                ,@(if (featurep 'xemacs) nil
2242                    '(:help "Encrypt the message body on disk"))]
2243               ["Extract all parts..." gnus-summary-save-parts t]
2244               ("Multipart"
2245                ["Repair multipart" gnus-summary-repair-multipart t]
2246                ["Pipe part..." gnus-article-pipe-part t]
2247                ["Inline part" gnus-article-inline-part t]
2248                ["View part as type..." gnus-article-view-part-as-type t]
2249                ["Encrypt body" gnus-article-encrypt-body
2250                 :active (not (gnus-group-read-only-p))
2251                ,@(if (featurep 'xemacs) nil
2252                    '(:help "Encrypt the message body on disk"))]
2253                ["View part externally" gnus-article-view-part-externally t]
2254                ["View HTML parts in browser" gnus-article-browse-html-article t]
2255                ["View part with charset..." gnus-article-view-part-as-charset t]
2256                ["Copy part" gnus-article-copy-part t]
2257                ["Save part..." gnus-article-save-part t]
2258                ["View part" gnus-article-view-part t]))
2259              ("Date"
2260               ["Local" gnus-article-date-local t]
2261               ["ISO8601" gnus-article-date-iso8601 t]
2262               ["UT" gnus-article-date-ut t]
2263               ["Original" gnus-article-date-original t]
2264               ["Lapsed" gnus-article-date-lapsed t]
2265               ["User-defined" gnus-article-date-user t])
2266              ("Display"
2267               ["Remove images" gnus-article-remove-images t]
2268               ["Toggle smiley" gnus-treat-smiley t]
2269               ["Show X-Face" gnus-article-display-x-face t]
2270               ["Show picons in From" gnus-treat-from-picon t]
2271               ["Show picons in mail headers" gnus-treat-mail-picon t]
2272               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2273               ("View as different encoding"
2274                ,@(gnus-summary-menu-split
2275                   (mapcar
2276                    (lambda (cs)
2277                      ;; Since easymenu under Emacs doesn't allow
2278                      ;; lambda forms for menu commands, we should
2279                      ;; provide intern'ed function symbols.
2280                      (let ((command (intern (format "\
2281 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2282                        (fset command
2283                              `(lambda ()
2284                                 (interactive)
2285                                 (let ((gnus-summary-show-article-charset-alist
2286                                        '((1 . ,cs))))
2287                                   (gnus-summary-show-article 1))))
2288                        `[,(symbol-name cs) ,command t]))
2289                    (sort (if (fboundp 'coding-system-list)
2290                              (coding-system-list)
2291                            (mapcar 'car mm-mime-mule-charset-alist))
2292                          'string<)))))
2293              ("Washing"
2294               ("Remove Blanks"
2295                ["Leading" gnus-article-strip-leading-blank-lines t]
2296                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2297                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2298                ["All of the above" gnus-article-strip-blank-lines t]
2299                ["All" gnus-article-strip-all-blank-lines t]
2300                ["Leading space" gnus-article-strip-leading-space t]
2301                ["Trailing space" gnus-article-strip-trailing-space t]
2302                ["Leading space in headers"
2303                 gnus-article-remove-leading-whitespace t])
2304               ["Overstrike" gnus-article-treat-overstrike t]
2305               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2306               ["Emphasis" gnus-article-emphasize t]
2307               ["Word wrap" gnus-article-fill-cited-article t]
2308               ["Fill long lines" gnus-article-fill-long-lines t]
2309               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2310               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2311               ["Remove CR" gnus-article-remove-cr t]
2312               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2313               ["Base64" gnus-article-de-base64-unreadable t]
2314               ["Rot 13" gnus-summary-caesar-message
2315                ,@(if (featurep 'xemacs) '(t)
2316                    '(:help "\"Caesar rotate\" article by 13"))]
2317               ["De-IDNA" gnus-summary-idna-message t]
2318               ["Morse decode" gnus-summary-morse-message t]
2319               ["Unix pipe..." gnus-summary-pipe-message t]
2320               ["Add buttons" gnus-article-add-buttons t]
2321               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2322               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2323               ["Verbose header" gnus-summary-verbose-headers t]
2324               ["Toggle header" gnus-summary-toggle-header t]
2325               ["Unfold headers" gnus-article-treat-unfold-headers t]
2326               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2327               ["Html" gnus-article-wash-html t]
2328               ["Unsplit URLs" gnus-article-unsplit-urls t]
2329               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2330               ["Decode HZ" gnus-article-decode-HZ t]
2331               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2332               ("(Outlook) Deuglify"
2333                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2334                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2335                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2336                ["Full (Outlook) deuglify"
2337                 gnus-article-outlook-deuglify-article t])
2338               )
2339              ("Output"
2340               ["Save in default format..." gnus-summary-save-article
2341                ,@(if (featurep 'xemacs) '(t)
2342                    '(:help "Save article using default method"))]
2343               ["Save in file..." gnus-summary-save-article-file
2344                ,@(if (featurep 'xemacs) '(t)
2345                    '(:help "Save article in file"))]
2346               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2347               ["Save in MH folder..." gnus-summary-save-article-folder t]
2348               ["Save in VM folder..." gnus-summary-save-article-vm t]
2349               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2350               ["Save body in file..." gnus-summary-save-article-body-file t]
2351               ["Pipe through a filter..." gnus-summary-pipe-output t]
2352               ["Add to SOUP packet" gnus-soup-add-article t]
2353               ["Print with Muttprint..." gnus-summary-muttprint t]
2354               ["Print" gnus-summary-print-article
2355                ,@(if (featurep 'xemacs) '(t)
2356                    '(:help "Generate and print a PostScript image"))])
2357              ("Copy, move,... (Backend)"
2358               ,@(if (featurep 'xemacs) nil
2359                   '(:help "Copying, moving, expiring articles..."))
2360               ["Respool article..." gnus-summary-respool-article t]
2361               ["Move article..." gnus-summary-move-article
2362                (gnus-check-backend-function
2363                 'request-move-article gnus-newsgroup-name)]
2364               ["Copy article..." gnus-summary-copy-article t]
2365               ["Crosspost article..." gnus-summary-crosspost-article
2366                (gnus-check-backend-function
2367                 'request-replace-article gnus-newsgroup-name)]
2368               ["Import file..." gnus-summary-import-article
2369                (gnus-check-backend-function
2370                 'request-accept-article gnus-newsgroup-name)]
2371               ["Create article..." gnus-summary-create-article
2372                (gnus-check-backend-function
2373                 'request-accept-article gnus-newsgroup-name)]
2374               ["Check if posted" gnus-summary-article-posted-p t]
2375               ["Edit article" gnus-summary-edit-article
2376                (not (gnus-group-read-only-p))]
2377               ["Delete article" gnus-summary-delete-article
2378                (gnus-check-backend-function
2379                 'request-expire-articles gnus-newsgroup-name)]
2380               ["Query respool" gnus-summary-respool-query t]
2381               ["Trace respool" gnus-summary-respool-trace t]
2382               ["Delete expirable articles" gnus-summary-expire-articles-now
2383                (gnus-check-backend-function
2384                 'request-expire-articles gnus-newsgroup-name)])
2385              ("Extract"
2386               ["Uudecode" gnus-uu-decode-uu
2387                ,@(if (featurep 'xemacs) '(t)
2388                    '(:help "Decode uuencoded article(s)"))]
2389               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2390               ["Unshar" gnus-uu-decode-unshar t]
2391               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2392               ["Save" gnus-uu-decode-save t]
2393               ["Binhex" gnus-uu-decode-binhex t]
2394               ["Postscript" gnus-uu-decode-postscript t]
2395               ["All MIME parts" gnus-summary-save-parts t])
2396              ("Cache"
2397               ["Enter article" gnus-cache-enter-article t]
2398               ["Remove article" gnus-cache-remove-article t])
2399              ["Translate" gnus-article-babel t]
2400              ["Select article buffer" gnus-summary-select-article-buffer t]
2401              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2402              ["Isearch article..." gnus-summary-isearch-article t]
2403              ["Beginning of the article" gnus-summary-beginning-of-article t]
2404              ["End of the article" gnus-summary-end-of-article t]
2405              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2406              ["Fetch referenced articles" gnus-summary-refer-references t]
2407              ["Fetch current thread" gnus-summary-refer-thread t]
2408              ["Fetch article with id..." gnus-summary-refer-article t]
2409              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2410              ["Redisplay" gnus-summary-show-article t]
2411              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2412       (easy-menu-define
2413         gnus-summary-article-menu gnus-summary-mode-map ""
2414         (cons "Article" innards))
2415
2416       (if (not (keymapp gnus-summary-article-menu))
2417           (easy-menu-define
2418             gnus-article-commands-menu gnus-article-mode-map ""
2419             (cons "Commands" innards))
2420         ;; in Emacs, don't share menu.
2421         (setq gnus-article-commands-menu
2422               (copy-keymap gnus-summary-article-menu))
2423         (define-key gnus-article-mode-map [menu-bar commands]
2424           (cons "Commands" gnus-article-commands-menu))))
2425
2426     (easy-menu-define
2427       gnus-summary-thread-menu gnus-summary-mode-map ""
2428       '("Threads"
2429         ["Find all messages in thread" gnus-summary-refer-thread t]
2430         ["Toggle threading" gnus-summary-toggle-threads t]
2431         ["Hide threads" gnus-summary-hide-all-threads t]
2432         ["Show threads" gnus-summary-show-all-threads t]
2433         ["Hide thread" gnus-summary-hide-thread t]
2434         ["Show thread" gnus-summary-show-thread t]
2435         ["Go to next thread" gnus-summary-next-thread t]
2436         ["Go to previous thread" gnus-summary-prev-thread t]
2437         ["Go down thread" gnus-summary-down-thread t]
2438         ["Go up thread" gnus-summary-up-thread t]
2439         ["Top of thread" gnus-summary-top-thread t]
2440         ["Mark thread as read" gnus-summary-kill-thread t]
2441         ["Lower thread score" gnus-summary-lower-thread t]
2442         ["Raise thread score" gnus-summary-raise-thread t]
2443         ["Rethread current" gnus-summary-rethread-current t]))
2444
2445     (easy-menu-define
2446       gnus-summary-post-menu gnus-summary-mode-map ""
2447       `("Post"
2448         ["Send a message (mail or news)" gnus-summary-post-news
2449          ,@(if (featurep 'xemacs) '(t)
2450              '(:help "Compose a new message (mail or news)"))]
2451         ["Followup" gnus-summary-followup
2452          ,@(if (featurep 'xemacs) '(t)
2453              '(:help "Post followup to this article"))]
2454         ["Followup and yank" gnus-summary-followup-with-original
2455          ,@(if (featurep 'xemacs) '(t)
2456              '(:help "Post followup to this article, quoting its contents"))]
2457         ["Supersede article" gnus-summary-supersede-article t]
2458         ["Cancel article" gnus-summary-cancel-article
2459          ,@(if (featurep 'xemacs) '(t)
2460              '(:help "Cancel an article you posted"))]
2461         ["Reply" gnus-summary-reply t]
2462         ["Reply and yank" gnus-summary-reply-with-original t]
2463         ["Wide reply" gnus-summary-wide-reply t]
2464         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2465          ,@(if (featurep 'xemacs) '(t)
2466              '(:help "Mail a reply, quoting this article"))]
2467         ["Very wide reply" gnus-summary-very-wide-reply t]
2468         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2469          ,@(if (featurep 'xemacs) '(t)
2470              '(:help "Mail a very wide reply, quoting this article"))]
2471         ["Mail forward" gnus-summary-mail-forward t]
2472         ["Post forward" gnus-summary-post-forward t]
2473         ["Digest and mail" gnus-uu-digest-mail-forward t]
2474         ["Digest and post" gnus-uu-digest-post-forward t]
2475         ["Resend message" gnus-summary-resend-message t]
2476         ["Resend message edit" gnus-summary-resend-message-edit t]
2477         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2478         ["Send a mail" gnus-summary-mail-other-window t]
2479         ["Create a local message" gnus-summary-news-other-window t]
2480         ["Uuencode and post" gnus-uu-post-news
2481          ,@(if (featurep 'xemacs) '(t)
2482              '(:help "Post a uuencoded article"))]
2483         ["Followup via news" gnus-summary-followup-to-mail t]
2484         ["Followup via news and yank"
2485          gnus-summary-followup-to-mail-with-original t]
2486         ;;("Draft"
2487         ;;["Send" gnus-summary-send-draft t]
2488         ;;["Send bounced" gnus-resend-bounced-mail t])
2489         ))
2490
2491     (cond
2492      ((not (keymapp gnus-summary-post-menu))
2493       (setq gnus-article-post-menu gnus-summary-post-menu))
2494      ((not gnus-article-post-menu)
2495       ;; Don't share post menu.
2496       (setq gnus-article-post-menu
2497             (copy-keymap gnus-summary-post-menu))))
2498     (define-key gnus-article-mode-map [menu-bar post]
2499       (cons "Post" gnus-article-post-menu))
2500
2501     (easy-menu-define
2502       gnus-summary-misc-menu gnus-summary-mode-map ""
2503       `("Gnus"
2504         ("Mark Read"
2505          ["Mark as read" gnus-summary-mark-as-read-forward t]
2506          ["Mark same subject and select"
2507           gnus-summary-kill-same-subject-and-select t]
2508          ["Mark same subject" gnus-summary-kill-same-subject t]
2509          ["Catchup" gnus-summary-catchup
2510           ,@(if (featurep 'xemacs) '(t)
2511               '(:help "Mark unread articles in this group as read"))]
2512          ["Catchup all" gnus-summary-catchup-all t]
2513          ["Catchup to here" gnus-summary-catchup-to-here t]
2514          ["Catchup from here" gnus-summary-catchup-from-here t]
2515          ["Catchup region" gnus-summary-mark-region-as-read
2516           (gnus-mark-active-p)]
2517          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2518         ("Mark Various"
2519          ["Tick" gnus-summary-tick-article-forward t]
2520          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2521          ["Remove marks" gnus-summary-clear-mark-forward t]
2522          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2523          ["Set bookmark" gnus-summary-set-bookmark t]
2524          ["Remove bookmark" gnus-summary-remove-bookmark t])
2525         ("Limit to"
2526          ["Marks..." gnus-summary-limit-to-marks t]
2527          ["Subject..." gnus-summary-limit-to-subject t]
2528          ["Author..." gnus-summary-limit-to-author t]
2529          ["Recipient..." gnus-summary-limit-to-recipient t]
2530          ["Age..." gnus-summary-limit-to-age t]
2531          ["Extra..." gnus-summary-limit-to-extra t]
2532          ["Score..." gnus-summary-limit-to-score t]
2533          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2534          ["Unread" gnus-summary-limit-to-unread t]
2535          ["Unseen" gnus-summary-limit-to-unseen t]
2536          ["Replied" gnus-summary-limit-to-replied t]
2537          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2538          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2539          ["Pop limit" gnus-summary-pop-limit t]
2540          ["Show dormant" gnus-summary-limit-include-dormant t]
2541          ["Hide childless dormant"
2542           gnus-summary-limit-exclude-childless-dormant t]
2543          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2544          ["Hide marked" gnus-summary-limit-exclude-marks t]
2545          ["Show expunged" gnus-summary-limit-include-expunged t])
2546         ("Process Mark"
2547          ["Set mark" gnus-summary-mark-as-processable t]
2548          ["Remove mark" gnus-summary-unmark-as-processable t]
2549          ["Remove all marks" gnus-summary-unmark-all-processable t]
2550          ["Invert marks" gnus-uu-invert-processable t]
2551          ["Mark above" gnus-uu-mark-over t]
2552          ["Mark series" gnus-uu-mark-series t]
2553          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2554          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2555          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2556          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2557          ["Mark all" gnus-uu-mark-all t]
2558          ["Mark buffer" gnus-uu-mark-buffer t]
2559          ["Mark sparse" gnus-uu-mark-sparse t]
2560          ["Mark thread" gnus-uu-mark-thread t]
2561          ["Unmark thread" gnus-uu-unmark-thread t]
2562          ("Process Mark Sets"
2563           ["Kill" gnus-summary-kill-process-mark t]
2564           ["Yank" gnus-summary-yank-process-mark
2565            gnus-newsgroup-process-stack]
2566           ["Save" gnus-summary-save-process-mark t]
2567           ["Run command on marked..." gnus-summary-universal-argument t]))
2568         ("Scroll article"
2569          ["Page forward" gnus-summary-next-page
2570           ,@(if (featurep 'xemacs) '(t)
2571               '(:help "Show next page of article"))]
2572          ["Page backward" gnus-summary-prev-page
2573           ,@(if (featurep 'xemacs) '(t)
2574               '(:help "Show previous page of article"))]
2575          ["Line forward" gnus-summary-scroll-up t])
2576         ("Move"
2577          ["Next unread article" gnus-summary-next-unread-article t]
2578          ["Previous unread article" gnus-summary-prev-unread-article t]
2579          ["Next article" gnus-summary-next-article t]
2580          ["Previous article" gnus-summary-prev-article t]
2581          ["Next unread subject" gnus-summary-next-unread-subject t]
2582          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2583          ["Next article same subject" gnus-summary-next-same-subject t]
2584          ["Previous article same subject" gnus-summary-prev-same-subject t]
2585          ["First unread article" gnus-summary-first-unread-article t]
2586          ["Best unread article" gnus-summary-best-unread-article t]
2587          ["Go to subject number..." gnus-summary-goto-subject t]
2588          ["Go to article number..." gnus-summary-goto-article t]
2589          ["Go to the last article" gnus-summary-goto-last-article t]
2590          ["Pop article off history" gnus-summary-pop-article t])
2591         ("Sort"
2592          ["Sort by number" gnus-summary-sort-by-number t]
2593          ["Sort by author" gnus-summary-sort-by-author t]
2594          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2595          ["Sort by subject" gnus-summary-sort-by-subject t]
2596          ["Sort by date" gnus-summary-sort-by-date t]
2597          ["Sort by score" gnus-summary-sort-by-score t]
2598          ["Sort by lines" gnus-summary-sort-by-lines t]
2599          ["Sort by characters" gnus-summary-sort-by-chars t]
2600          ["Randomize" gnus-summary-sort-by-random t]
2601          ["Original sort" gnus-summary-sort-by-original t])
2602         ("Help"
2603          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2604          ["Describe group" gnus-summary-describe-group t]
2605          ["Fetch charter" gnus-group-fetch-charter
2606           ,@(if (featurep 'xemacs) nil
2607               '(:help "Display the charter of the current group"))]
2608          ["Fetch control message" gnus-group-fetch-control
2609           ,@(if (featurep 'xemacs) nil
2610               '(:help "Display the archived control message for the current group"))]
2611          ["Read manual" gnus-info-find-node t])
2612         ("Modes"
2613          ["Pick and read" gnus-pick-mode t]
2614          ["Binary" gnus-binary-mode t])
2615         ("Regeneration"
2616          ["Regenerate" gnus-summary-prepare t]
2617          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2618          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2619          ["Toggle threading" gnus-summary-toggle-threads t])
2620         ["See old articles" gnus-summary-insert-old-articles t]
2621         ["See new articles" gnus-summary-insert-new-articles t]
2622         ["Filter articles..." gnus-summary-execute-command t]
2623         ["Run command on articles..." gnus-summary-universal-argument t]
2624         ["Search articles forward..." gnus-summary-search-article-forward t]
2625         ["Search articles backward..." gnus-summary-search-article-backward t]
2626         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2627         ["Expand window" gnus-summary-expand-window t]
2628         ["Expire expirable articles" gnus-summary-expire-articles
2629          (gnus-check-backend-function
2630           'request-expire-articles gnus-newsgroup-name)]
2631         ["Edit local kill file" gnus-summary-edit-local-kill t]
2632         ["Edit main kill file" gnus-summary-edit-global-kill t]
2633         ["Edit group parameters" gnus-summary-edit-parameters t]
2634         ["Customize group parameters" gnus-summary-customize-parameters t]
2635         ["Send a bug report" gnus-bug t]
2636         ("Exit"
2637          ["Catchup and exit" gnus-summary-catchup-and-exit
2638           ,@(if (featurep 'xemacs) '(t)
2639               '(:help "Mark unread articles in this group as read, then exit"))]
2640          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2641          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2642          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2643          ["Exit group" gnus-summary-exit
2644           ,@(if (featurep 'xemacs) '(t)
2645               '(:help "Exit current group, return to group selection mode"))]
2646          ["Exit group without updating" gnus-summary-exit-no-update t]
2647          ["Exit and goto next group" gnus-summary-next-group t]
2648          ["Exit and goto prev group" gnus-summary-prev-group t]
2649          ["Reselect group" gnus-summary-reselect-current-group t]
2650          ["Rescan group" gnus-summary-rescan-group t]
2651          ["Update dribble" gnus-summary-save-newsrc t])))
2652
2653     (gnus-run-hooks 'gnus-summary-menu-hook)))
2654
2655 (defvar gnus-summary-tool-bar-map nil)
2656
2657 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2658 ;; affect _new_ message buffers.  We might add a function that walks thru all
2659 ;; summary-mode buffers and force the update.
2660 (defun gnus-summary-tool-bar-update (&optional symbol value)
2661   "Update summary mode toolbar.
2662 Setter function for custom variables."
2663   (setq-default gnus-summary-tool-bar-map nil)
2664   (when symbol
2665     ;; When used as ":set" function:
2666     (set-default symbol value))
2667   (when (gnus-buffer-live-p gnus-summary-buffer)
2668     (with-current-buffer gnus-summary-buffer
2669       (gnus-summary-make-tool-bar))))
2670
2671 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2672                                      'gnus-summary-tool-bar-gnome
2673                                    'gnus-summary-tool-bar-retro)
2674   "Specifies the Gnus summary tool bar.
2675
2676 It can be either a list or a symbol refering to a list.  See
2677 `gmm-tool-bar-from-list' for the format of the list.  The
2678 default key map is `gnus-summary-mode-map'.
2679
2680 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2681 `gnus-summary-tool-bar-retro'."
2682   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2683                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2684                  (repeat :tag "User defined list" gmm-tool-bar-item)
2685                  (symbol))
2686   :version "23.0" ;; No Gnus
2687   :initialize 'custom-initialize-default
2688   :set 'gnus-summary-tool-bar-update
2689   :group 'gnus-summary)
2690
2691 (defcustom gnus-summary-tool-bar-gnome
2692   '((gnus-summary-post-news "mail/compose" nil)
2693     (gnus-summary-insert-new-articles "mail/inbox" nil
2694                                       :visible (or (not gnus-agent)
2695                                                    gnus-plugged))
2696     (gnus-summary-reply-with-original "mail/reply")
2697     (gnus-summary-reply "mail/reply" nil :visible nil)
2698     (gnus-summary-followup-with-original "mail/reply-all")
2699     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2700     (gnus-summary-mail-forward "mail/forward")
2701     (gnus-summary-save-article "mail/save")
2702     (gnus-summary-search-article-forward "search" nil :visible nil)
2703     (gnus-summary-print-article "print")
2704     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2705     ;; Some new commands that may need more suitable icons:
2706     (gnus-summary-save-newsrc "save" nil :visible nil)
2707     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2708     (gnus-summary-prev-article "left-arrow")
2709     (gnus-summary-next-article "right-arrow")
2710     (gnus-summary-next-page "next-page")
2711     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2712     ;;
2713     ;; Maybe some sort-by-... could be added:
2714     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2715     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2716     (gnus-summary-mark-as-expirable
2717      "delete" nil
2718      :visible (gnus-check-backend-function 'request-expire-articles
2719                                            gnus-newsgroup-name))
2720     (gnus-summary-mark-as-spam
2721      "mail/spam" t
2722      :visible (and (fboundp 'spam-group-ham-contents-p)
2723                    (spam-group-ham-contents-p gnus-newsgroup-name))
2724      :help "Mark as spam")
2725     (gnus-summary-mark-as-read-forward
2726      "mail/not-spam" nil
2727      :visible (and (fboundp 'spam-group-spam-contents-p)
2728                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2729     ;;
2730     (gnus-summary-exit "exit")
2731     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2732     (gnus-info-find-node "help"))
2733   "List of functions for the summary tool bar (GNOME style).
2734
2735 See `gmm-tool-bar-from-list' for the format of the list."
2736   :type '(repeat gmm-tool-bar-item)
2737   :version "23.0" ;; No Gnus
2738   :initialize 'custom-initialize-default
2739   :set 'gnus-summary-tool-bar-update
2740   :group 'gnus-summary)
2741
2742 (defcustom gnus-summary-tool-bar-retro
2743   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2744     (gnus-summary-next-unread-article "gnus/next-ur")
2745     (gnus-summary-post-news "gnus/post")
2746     (gnus-summary-followup-with-original "gnus/fuwo")
2747     (gnus-summary-followup "gnus/followup")
2748     (gnus-summary-reply-with-original "gnus/reply-wo")
2749     (gnus-summary-reply "gnus/reply")
2750     (gnus-summary-caesar-message "gnus/rot13")
2751     (gnus-uu-decode-uu "gnus/uu-decode")
2752     (gnus-summary-save-article-file "gnus/save-aif")
2753     (gnus-summary-save-article "gnus/save-art")
2754     (gnus-uu-post-news "gnus/uu-post")
2755     (gnus-summary-catchup "gnus/catchup")
2756     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2757     (gnus-summary-exit "gnus/exit-summ")
2758     ;; Some new command that may need more suitable icons:
2759     (gnus-summary-print-article "gnus/print" nil :visible nil)
2760     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2761     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2762     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2763     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2764     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2765     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2766     ;;
2767     (gnus-info-find-node "gnus/help" nil :visible nil))
2768   "List of functions for the summary tool bar (retro look).
2769
2770 See `gmm-tool-bar-from-list' for the format of the list."
2771   :type '(repeat gmm-tool-bar-item)
2772   :version "23.0" ;; No Gnus
2773   :initialize 'custom-initialize-default
2774   :set 'gnus-summary-tool-bar-update
2775   :group 'gnus-summary)
2776
2777 (defcustom gnus-summary-tool-bar-zap-list t
2778   "List of icon items from the global tool bar.
2779 These items are not displayed in the Gnus summary mode tool bar.
2780
2781 See `gmm-tool-bar-from-list' for the format of the list."
2782   :type 'gmm-tool-bar-zap-list
2783   :version "23.0" ;; No Gnus
2784   :initialize 'custom-initialize-default
2785   :set 'gnus-summary-tool-bar-update
2786   :group 'gnus-summary)
2787
2788 (defvar image-load-path)
2789
2790 (defun gnus-summary-make-tool-bar (&optional force)
2791   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2792 When FORCE, rebuild the tool bar."
2793   (when (and (not (featurep 'xemacs))
2794              (boundp 'tool-bar-mode)
2795              tool-bar-mode
2796              (or (not gnus-summary-tool-bar-map) force))
2797     (let* ((load-path
2798             (gmm-image-load-path-for-library "gnus"
2799                                              "mail/save.xpm"
2800                                              nil t))
2801            (image-load-path (cons (car load-path)
2802                                   (when (boundp 'image-load-path)
2803                                     image-load-path)))
2804            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2805                                         gnus-summary-tool-bar-zap-list
2806                                         'gnus-summary-mode-map)))
2807       (when map
2808         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2809         ;; uses it's value.
2810         (setq gnus-summary-tool-bar-map map))))
2811   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2812
2813 (defun gnus-score-set-default (var value)
2814   "A version of set that updates the GNU Emacs menu-bar."
2815   (set var value)
2816   ;; It is the message that forces the active status to be updated.
2817   (message ""))
2818
2819 (defun gnus-make-score-map (type)
2820   "Make a summary score map of type TYPE."
2821   (if t
2822       nil
2823     (let ((headers '(("author" "from" string)
2824                      ("subject" "subject" string)
2825                      ("article body" "body" string)
2826                      ("article head" "head" string)
2827                      ("xref" "xref" string)
2828                      ("extra header" "extra" string)
2829                      ("lines" "lines" number)
2830                      ("followups to author" "followup" string)))
2831           (types '((number ("less than" <)
2832                            ("greater than" >)
2833                            ("equal" =))
2834                    (string ("substring" s)
2835                            ("exact string" e)
2836                            ("fuzzy string" f)
2837                            ("regexp" r))))
2838           (perms '(("temporary" (current-time-string))
2839                    ("permanent" nil)
2840                    ("immediate" now)))
2841           header)
2842       (list
2843        (apply
2844         'nconc
2845         (list
2846          (if (eq type 'lower)
2847              "Lower score"
2848            "Increase score"))
2849         (let (outh)
2850           (while headers
2851             (setq header (car headers))
2852             (setq outh
2853                   (cons
2854                    (apply
2855                     'nconc
2856                     (list (car header))
2857                     (let ((ts (cdr (assoc (nth 2 header) types)))
2858                           outt)
2859                       (while ts
2860                         (setq outt
2861                               (cons
2862                                (apply
2863                                 'nconc
2864                                 (list (caar ts))
2865                                 (let ((ps perms)
2866                                       outp)
2867                                   (while ps
2868                                     (setq outp
2869                                           (cons
2870                                            (vector
2871                                             (caar ps)
2872                                             (list
2873                                              'gnus-summary-score-entry
2874                                              (nth 1 header)
2875                                              (if (or (string= (nth 1 header)
2876                                                               "head")
2877                                                      (string= (nth 1 header)
2878                                                               "body"))
2879                                                  ""
2880                                                (list 'gnus-summary-header
2881                                                      (nth 1 header)))
2882                                              (list 'quote (nth 1 (car ts)))
2883                                              (list 'gnus-score-delta-default
2884                                                    nil)
2885                                              (nth 1 (car ps))
2886                                              t)
2887                                             t)
2888                                            outp))
2889                                     (setq ps (cdr ps)))
2890                                   (list (nreverse outp))))
2891                                outt))
2892                         (setq ts (cdr ts)))
2893                       (list (nreverse outt))))
2894                    outh))
2895             (setq headers (cdr headers)))
2896           (list (nreverse outh))))))))
2897
2898 \f
2899
2900 (defun gnus-summary-mode (&optional group)
2901   "Major mode for reading articles.
2902
2903 All normal editing commands are switched off.
2904 \\<gnus-summary-mode-map>
2905 Each line in this buffer represents one article.  To read an
2906 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2907 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2908 respectively.
2909
2910 You can also post articles and send mail from this buffer.  To
2911 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2912 of an article, type `\\[gnus-summary-reply]'.
2913
2914 There are approx. one gazillion commands you can execute in this
2915 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2916
2917 The following commands are available:
2918
2919 \\{gnus-summary-mode-map}"
2920   (interactive)
2921   (kill-all-local-variables)
2922   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2923     (gnus-summary-make-local-variables))
2924   (gnus-summary-make-local-variables)
2925   (setq gnus-newsgroup-name group)
2926   (when (gnus-visual-p 'summary-menu 'menu)
2927     (gnus-summary-make-menu-bar)
2928     (gnus-summary-make-tool-bar))
2929   (gnus-make-thread-indent-array)
2930   (gnus-simplify-mode-line)
2931   (setq major-mode 'gnus-summary-mode)
2932   (setq mode-name "Summary")
2933   (make-local-variable 'minor-mode-alist)
2934   (use-local-map gnus-summary-mode-map)
2935   (buffer-disable-undo)
2936   (setq buffer-read-only t              ;Disable modification
2937         show-trailing-whitespace nil)
2938   (setq truncate-lines t)
2939   (setq selective-display t)
2940   (setq selective-display-ellipses t)   ;Display `...'
2941   (gnus-summary-set-display-table)
2942   (gnus-set-default-directory)
2943   (make-local-variable 'gnus-summary-line-format)
2944   (make-local-variable 'gnus-summary-line-format-spec)
2945   (make-local-variable 'gnus-summary-dummy-line-format)
2946   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2947   (make-local-variable 'gnus-summary-mark-positions)
2948   (gnus-make-local-hook 'pre-command-hook)
2949   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2950   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2951   (turn-on-gnus-mailing-list-mode)
2952   (mm-enable-multibyte)
2953   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2954   (gnus-update-summary-mark-positions))
2955
2956 (defun gnus-summary-make-local-variables ()
2957   "Make all the local summary buffer variables."
2958   (let (global)
2959     (dolist (local gnus-summary-local-variables)
2960       (if (consp local)
2961           (progn
2962             (if (eq (cdr local) 'global)
2963                 ;; Copy the global value of the variable.
2964                 (setq global (symbol-value (car local)))
2965               ;; Use the value from the list.
2966               (setq global (eval (cdr local))))
2967             (set (make-local-variable (car local)) global))
2968         ;; Simple nil-valued local variable.
2969         (set (make-local-variable local) nil)))))
2970
2971 (defun gnus-summary-clear-local-variables ()
2972   (let ((locals gnus-summary-local-variables))
2973     (while locals
2974       (if (consp (car locals))
2975           (and (symbolp (caar locals))
2976                (set (caar locals) nil))
2977         (and (symbolp (car locals))
2978              (set (car locals) nil)))
2979       (setq locals (cdr locals)))))
2980
2981 ;; Summary data functions.
2982
2983 (defmacro gnus-data-number (data)
2984   `(car ,data))
2985
2986 (defmacro gnus-data-set-number (data number)
2987   `(setcar ,data ,number))
2988
2989 (defmacro gnus-data-mark (data)
2990   `(nth 1 ,data))
2991
2992 (defmacro gnus-data-set-mark (data mark)
2993   `(setcar (nthcdr 1 ,data) ,mark))
2994
2995 (defmacro gnus-data-pos (data)
2996   `(nth 2 ,data))
2997
2998 (defmacro gnus-data-set-pos (data pos)
2999   `(setcar (nthcdr 2 ,data) ,pos))
3000
3001 (defmacro gnus-data-header (data)
3002   `(nth 3 ,data))
3003
3004 (defmacro gnus-data-set-header (data header)
3005   `(setf (nth 3 ,data) ,header))
3006
3007 (defmacro gnus-data-level (data)
3008   `(nth 4 ,data))
3009
3010 (defmacro gnus-data-unread-p (data)
3011   `(= (nth 1 ,data) gnus-unread-mark))
3012
3013 (defmacro gnus-data-read-p (data)
3014   `(/= (nth 1 ,data) gnus-unread-mark))
3015
3016 (defmacro gnus-data-pseudo-p (data)
3017   `(consp (nth 3 ,data)))
3018
3019 (defmacro gnus-data-find (number)
3020   `(assq ,number gnus-newsgroup-data))
3021
3022 (defmacro gnus-data-find-list (number &optional data)
3023   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3024      (memq (assq ,number bdata)
3025            bdata)))
3026
3027 (defmacro gnus-data-make (number mark pos header level)
3028   `(list ,number ,mark ,pos ,header ,level))
3029
3030 (defun gnus-data-enter (after-article number mark pos header level offset)
3031   (let ((data (gnus-data-find-list after-article)))
3032     (unless data
3033       (error "No such article: %d" after-article))
3034     (setcdr data (cons (gnus-data-make number mark pos header level)
3035                        (cdr data)))
3036     (setq gnus-newsgroup-data-reverse nil)
3037     (gnus-data-update-list (cddr data) offset)))
3038
3039 (defun gnus-data-enter-list (after-article list &optional offset)
3040   (when list
3041     (let ((data (and after-article (gnus-data-find-list after-article)))
3042           (ilist list))
3043       (if (not (or data
3044                    after-article))
3045           (let ((odata gnus-newsgroup-data))
3046             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3047             (when offset
3048               (gnus-data-update-list odata offset)))
3049         ;; Find the last element in the list to be spliced into the main
3050         ;; list.
3051         (setq list (last list))
3052         (if (not data)
3053             (progn
3054               (setcdr list gnus-newsgroup-data)
3055               (setq gnus-newsgroup-data ilist)
3056               (when offset
3057                 (gnus-data-update-list (cdr list) offset)))
3058           (setcdr list (cdr data))
3059           (setcdr data ilist)
3060           (when offset
3061             (gnus-data-update-list (cdr list) offset))))
3062       (setq gnus-newsgroup-data-reverse nil))))
3063
3064 (defun gnus-data-remove (article &optional offset)
3065   (let ((data gnus-newsgroup-data))
3066     (if (= (gnus-data-number (car data)) article)
3067         (progn
3068           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3069                 gnus-newsgroup-data-reverse nil)
3070           (when offset
3071             (gnus-data-update-list gnus-newsgroup-data offset)))
3072       (while (cdr data)
3073         (when (= (gnus-data-number (cadr data)) article)
3074           (setcdr data (cddr data))
3075           (when offset
3076             (gnus-data-update-list (cdr data) offset))
3077           (setq data nil
3078                 gnus-newsgroup-data-reverse nil))
3079         (setq data (cdr data))))))
3080
3081 (defmacro gnus-data-list (backward)
3082   `(if ,backward
3083        (or gnus-newsgroup-data-reverse
3084            (setq gnus-newsgroup-data-reverse
3085                  (reverse gnus-newsgroup-data)))
3086      gnus-newsgroup-data))
3087
3088 (defun gnus-data-update-list (data offset)
3089   "Add OFFSET to the POS of all data entries in DATA."
3090   (setq gnus-newsgroup-data-reverse nil)
3091   (while data
3092     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3093     (setq data (cdr data))))
3094
3095 (defun gnus-summary-article-pseudo-p (article)
3096   "Say whether this article is a pseudo article or not."
3097   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3098
3099 (defmacro gnus-summary-article-sparse-p (article)
3100   "Say whether this article is a sparse article or not."
3101   `(memq ,article gnus-newsgroup-sparse))
3102
3103 (defmacro gnus-summary-article-ancient-p (article)
3104   "Say whether this article is a sparse article or not."
3105   `(memq ,article gnus-newsgroup-ancient))
3106
3107 (defun gnus-article-parent-p (number)
3108   "Say whether this article is a parent or not."
3109   (let ((data (gnus-data-find-list number)))
3110     (and (cdr data)              ; There has to be an article after...
3111          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3112             (gnus-data-level (nth 1 data))))))
3113
3114 (defun gnus-article-children (number)
3115   "Return a list of all children to NUMBER."
3116   (let* ((data (gnus-data-find-list number))
3117          (level (gnus-data-level (car data)))
3118          children)
3119     (setq data (cdr data))
3120     (while (and data
3121                 (= (gnus-data-level (car data)) (1+ level)))
3122       (push (gnus-data-number (car data)) children)
3123       (setq data (cdr data)))
3124     children))
3125
3126 (defmacro gnus-summary-skip-intangible ()
3127   "If the current article is intangible, then jump to a different article."
3128   '(let ((to (get-text-property (point) 'gnus-intangible)))
3129      (and to (gnus-summary-goto-subject to))))
3130
3131 (defmacro gnus-summary-article-intangible-p ()
3132   "Say whether this article is intangible or not."
3133   '(get-text-property (point) 'gnus-intangible))
3134
3135 (defun gnus-article-read-p (article)
3136   "Say whether ARTICLE is read or not."
3137   (not (or (memq article gnus-newsgroup-marked)
3138            (memq article gnus-newsgroup-spam-marked)
3139            (memq article gnus-newsgroup-unreads)
3140            (memq article gnus-newsgroup-unselected)
3141            (memq article gnus-newsgroup-dormant))))
3142
3143 ;; Some summary mode macros.
3144
3145 (defmacro gnus-summary-article-number ()
3146   "The article number of the article on the current line.
3147 If there isn't an article number here, then we return the current
3148 article number."
3149   '(progn
3150      (gnus-summary-skip-intangible)
3151      (or (get-text-property (point) 'gnus-number)
3152          (gnus-summary-last-subject))))
3153
3154 (defmacro gnus-summary-article-header (&optional number)
3155   "Return the header of article NUMBER."
3156   `(gnus-data-header (gnus-data-find
3157                       ,(or number '(gnus-summary-article-number)))))
3158
3159 (defmacro gnus-summary-thread-level (&optional number)
3160   "Return the level of thread that starts with article NUMBER."
3161   `(if (and (eq gnus-summary-make-false-root 'dummy)
3162             (get-text-property (point) 'gnus-intangible))
3163        0
3164      (gnus-data-level (gnus-data-find
3165                        ,(or number '(gnus-summary-article-number))))))
3166
3167 (defmacro gnus-summary-article-mark (&optional number)
3168   "Return the mark of article NUMBER."
3169   `(gnus-data-mark (gnus-data-find
3170                     ,(or number '(gnus-summary-article-number)))))
3171
3172 (defmacro gnus-summary-article-pos (&optional number)
3173   "Return the position of the line of article NUMBER."
3174   `(gnus-data-pos (gnus-data-find
3175                    ,(or number '(gnus-summary-article-number)))))
3176
3177 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3178 (defmacro gnus-summary-article-subject (&optional number)
3179   "Return current subject string or nil if nothing."
3180   `(let ((headers
3181           ,(if number
3182                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3183              '(gnus-data-header (assq (gnus-summary-article-number)
3184                                       gnus-newsgroup-data)))))
3185      (and headers
3186           (vectorp headers)
3187           (mail-header-subject headers))))
3188
3189 (defmacro gnus-summary-article-score (&optional number)
3190   "Return current article score."
3191   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3192                   gnus-newsgroup-scored))
3193        gnus-summary-default-score 0))
3194
3195 (defun gnus-summary-article-children (&optional number)
3196   "Return a list of article numbers that are children of article NUMBER."
3197   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3198          (level (gnus-data-level (car data)))
3199          l children)
3200     (while (and (setq data (cdr data))
3201                 (> (setq l (gnus-data-level (car data))) level))
3202       (and (= (1+ level) l)
3203            (push (gnus-data-number (car data))
3204                  children)))
3205     (nreverse children)))
3206
3207 (defun gnus-summary-article-parent (&optional number)
3208   "Return the article number of the parent of article NUMBER."
3209   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3210                                     (gnus-data-list t)))
3211          (level (gnus-data-level (car data))))
3212     (if (zerop level)
3213         ()                              ; This is a root.
3214       ;; We search until we find an article with a level less than
3215       ;; this one.  That function has to be the parent.
3216       (while (and (setq data (cdr data))
3217                   (not (< (gnus-data-level (car data)) level))))
3218       (and data (gnus-data-number (car data))))))
3219
3220 (defun gnus-unread-mark-p (mark)
3221   "Say whether MARK is the unread mark."
3222   (= mark gnus-unread-mark))
3223
3224 (defun gnus-read-mark-p (mark)
3225   "Say whether MARK is one of the marks that mark as read.
3226 This is all marks except unread, ticked, dormant, and expirable."
3227   (not (or (= mark gnus-unread-mark)
3228            (= mark gnus-ticked-mark)
3229            (= mark gnus-spam-mark)
3230            (= mark gnus-dormant-mark)
3231            (= mark gnus-expirable-mark))))
3232
3233 (defmacro gnus-article-mark (number)
3234   "Return the MARK of article NUMBER.
3235 This macro should only be used when computing the mark the \"first\"
3236 time; i.e., when generating the summary lines.  After that,
3237 `gnus-summary-article-mark' should be used to examine the
3238 marks of articles."
3239   `(cond
3240     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3241     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3242     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3243     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3244     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3245     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3246     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3247     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3248            gnus-ancient-mark))))
3249
3250 ;; Saving hidden threads.
3251
3252 (defmacro gnus-save-hidden-threads (&rest forms)
3253   "Save hidden threads, eval FORMS, and restore the hidden threads."
3254   (let ((config (make-symbol "config")))
3255     `(let ((,config (gnus-hidden-threads-configuration)))
3256        (unwind-protect
3257            (save-excursion
3258              ,@forms)
3259          (gnus-restore-hidden-threads-configuration ,config)))))
3260 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3261 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3262
3263 (defun gnus-data-compute-positions ()
3264   "Compute the positions of all articles."
3265   (setq gnus-newsgroup-data-reverse nil)
3266   (let ((data gnus-newsgroup-data))
3267     (save-excursion
3268       (gnus-save-hidden-threads
3269         (gnus-summary-show-all-threads)
3270         (goto-char (point-min))
3271         (while data
3272           (while (get-text-property (point) 'gnus-intangible)
3273             (forward-line 1))
3274           (gnus-data-set-pos (car data) (+ (point) 3))
3275           (setq data (cdr data))
3276           (forward-line 1))))))
3277
3278 (defun gnus-hidden-threads-configuration ()
3279   "Return the current hidden threads configuration."
3280   (save-excursion
3281     (let (config)
3282       (goto-char (point-min))
3283       (while (search-forward "\r" nil t)
3284         (push (1- (point)) config))
3285       config)))
3286
3287 (defun gnus-restore-hidden-threads-configuration (config)
3288   "Restore hidden threads configuration from CONFIG."
3289   (save-excursion
3290     (let (point buffer-read-only)
3291       (while (setq point (pop config))
3292         (when (and (< point (point-max))
3293                    (goto-char point)
3294                    (eq (char-after) ?\n))
3295           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3296
3297 ;; Various summary mode internalish functions.
3298
3299 (defun gnus-mouse-pick-article (e)
3300   (interactive "e")
3301   (mouse-set-point e)
3302   (gnus-summary-next-page nil t))
3303
3304 (defun gnus-summary-set-display-table ()
3305   "Change the display table.
3306 Odd characters have a tendency to mess
3307 up nicely formatted displays - we make all possible glyphs
3308 display only a single character."
3309
3310   ;; We start from the standard display table, if any.
3311   (let ((table (or (copy-sequence standard-display-table)
3312                    (make-display-table)))
3313         (i 32))
3314     ;; Nix out all the control chars...
3315     (while (>= (setq i (1- i)) 0)
3316       (aset table i [??]))
3317    ;; ... but not newline and cr, of course.  (cr is necessary for the
3318     ;; selective display).
3319     (aset table ?\n nil)
3320     (aset table ?\r nil)
3321     ;; We keep TAB as well.
3322     (aset table ?\t nil)
3323     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3324     ;; Emacs 23 (unicode), that are not set already.
3325     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3326                  160
3327                256)))
3328       (while (>= (setq i (1- i)) 127)
3329         ;; Only modify if the entry is nil.
3330         (unless (aref table i)
3331           (aset table i [??]))))
3332     (setq buffer-display-table table)))
3333
3334 (defun gnus-summary-set-article-display-arrow (pos)
3335   "Update the overlay arrow to point to line at position POS."
3336   (when (and gnus-summary-display-arrow
3337              (boundp 'overlay-arrow-position)
3338              (boundp 'overlay-arrow-string))
3339     (save-excursion
3340       (goto-char pos)
3341       (beginning-of-line)
3342       (unless overlay-arrow-position
3343         (setq overlay-arrow-position (make-marker)))
3344       (setq overlay-arrow-string "=>"
3345             overlay-arrow-position (set-marker overlay-arrow-position
3346                                                (point)
3347                                                (current-buffer))))))
3348
3349 (defun gnus-summary-setup-buffer (group)
3350   "Initialize summary buffer."
3351   (let ((buffer (gnus-summary-buffer-name group))
3352         (dead-name (concat "*Dead Summary "
3353                            (gnus-group-decoded-name group) "*")))
3354     ;; If a dead summary buffer exists, we kill it.
3355     (when (gnus-buffer-live-p dead-name)
3356       (gnus-kill-buffer dead-name))
3357     (if (get-buffer buffer)
3358         (progn
3359           (set-buffer buffer)
3360           (setq gnus-summary-buffer (current-buffer))
3361           (not gnus-newsgroup-prepared))
3362       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3363       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3364       (gnus-summary-mode group)
3365       (when gnus-carpal
3366         (gnus-carpal-setup-buffer 'summary))
3367       (when (gnus-group-quit-config group)
3368         (set (make-local-variable 'gnus-single-article-buffer) nil))
3369       (make-local-variable 'gnus-article-buffer)
3370       (make-local-variable 'gnus-article-current)
3371       (make-local-variable 'gnus-original-article-buffer)
3372       (setq gnus-newsgroup-name group)
3373       ;; Set any local variables in the group parameters.
3374       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3375       t)))
3376
3377 (defun gnus-set-global-variables ()
3378   "Set the global equivalents of the buffer-local variables.
3379 They are set to the latest values they had.  These reflect the summary
3380 buffer that was in action when the last article was fetched."
3381   (when (eq major-mode 'gnus-summary-mode)
3382     (setq gnus-summary-buffer (current-buffer))
3383     (let ((name gnus-newsgroup-name)
3384           (marked gnus-newsgroup-marked)
3385           (spam gnus-newsgroup-spam-marked)
3386           (unread gnus-newsgroup-unreads)
3387           (headers gnus-current-headers)
3388           (data gnus-newsgroup-data)
3389           (summary gnus-summary-buffer)
3390           (article-buffer gnus-article-buffer)
3391           (original gnus-original-article-buffer)
3392           (gac gnus-article-current)
3393           (reffed gnus-reffed-article-number)
3394           (score-file gnus-current-score-file)
3395           (default-charset gnus-newsgroup-charset)
3396           vlist)
3397       (let ((locals gnus-newsgroup-variables))
3398         (while locals
3399           (if (consp (car locals))
3400               (push (eval (caar locals)) vlist)
3401             (push (eval (car locals)) vlist))
3402           (setq locals (cdr locals)))
3403         (setq vlist (nreverse vlist)))
3404       (with-current-buffer gnus-group-buffer
3405         (setq gnus-newsgroup-name name
3406               gnus-newsgroup-marked marked
3407               gnus-newsgroup-spam-marked spam
3408               gnus-newsgroup-unreads unread
3409               gnus-current-headers headers
3410               gnus-newsgroup-data data
3411               gnus-article-current gac
3412               gnus-summary-buffer summary
3413               gnus-article-buffer article-buffer
3414               gnus-original-article-buffer original
3415               gnus-reffed-article-number reffed
3416               gnus-current-score-file score-file
3417               gnus-newsgroup-charset default-charset)
3418         (let ((locals gnus-newsgroup-variables))
3419           (while locals
3420             (if (consp (car locals))
3421                 (set (caar locals) (pop vlist))
3422               (set (car locals) (pop vlist)))
3423             (setq locals (cdr locals))))
3424         ;; The article buffer also has local variables.
3425         (when (gnus-buffer-live-p gnus-article-buffer)
3426           (set-buffer gnus-article-buffer)
3427           (setq gnus-summary-buffer summary))))))
3428
3429 (defun gnus-summary-article-unread-p (article)
3430   "Say whether ARTICLE is unread or not."
3431   (memq article gnus-newsgroup-unreads))
3432
3433 (defun gnus-summary-first-article-p (&optional article)
3434   "Return whether ARTICLE is the first article in the buffer."
3435   (if (not (setq article (or article (gnus-summary-article-number))))
3436       nil
3437     (eq article (caar gnus-newsgroup-data))))
3438
3439 (defun gnus-summary-last-article-p (&optional article)
3440   "Return whether ARTICLE is the last article in the buffer."
3441   (if (not (setq article (or article (gnus-summary-article-number))))
3442       ;; All non-existent numbers are the last article.  :-)
3443       t
3444     (not (cdr (gnus-data-find-list article)))))
3445
3446 (defun gnus-make-thread-indent-array ()
3447   (let ((n 200))
3448     (unless (and gnus-thread-indent-array
3449                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3450       (setq gnus-thread-indent-array (make-vector 201 "")
3451             gnus-thread-indent-array-level gnus-thread-indent-level)
3452       (while (>= n 0)
3453         (aset gnus-thread-indent-array n
3454               (make-string (* n gnus-thread-indent-level) ? ))
3455         (setq n (1- n))))))
3456
3457 (defun gnus-update-summary-mark-positions ()
3458   "Compute where the summary marks are to go."
3459   (save-excursion
3460     (when (gnus-buffer-exists-p gnus-summary-buffer)
3461       (set-buffer gnus-summary-buffer))
3462     (let ((spec gnus-summary-line-format-spec)
3463           pos)
3464       (save-excursion
3465         (gnus-set-work-buffer)
3466         (let ((gnus-tmp-unread ?Z)
3467               (gnus-replied-mark ?Z)
3468               (gnus-score-below-mark ?Z)
3469               (gnus-score-over-mark ?Z)
3470               (gnus-undownloaded-mark ?Z)
3471               (gnus-summary-line-format-spec spec)
3472               (gnus-newsgroup-downloadable '(0))
3473               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3474               case-fold-search ignores)
3475           ;; Here, all marks are bound to Z.
3476           (gnus-summary-insert-line header
3477                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3478           (goto-char (point-min))
3479           ;; Memorize the positions of the same characters as dummy marks.
3480           (while (re-search-forward "[A-D]" nil t)
3481             (push (point) ignores))
3482           (erase-buffer)
3483           ;; We use A-D as dummy marks in order to know column positions
3484           ;; where marks should be inserted.
3485           (setq gnus-tmp-unread ?A
3486                 gnus-replied-mark ?B
3487                 gnus-score-below-mark ?C
3488                 gnus-score-over-mark ?C
3489                 gnus-undownloaded-mark ?D)
3490           (gnus-summary-insert-line header
3491                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3492           ;; Ignore characters which aren't dummy marks.
3493           (dolist (p ignores)
3494             (delete-region (goto-char (1- p)) p)
3495             (insert ?Z))
3496           (goto-char (point-min))
3497           (setq pos (list (cons 'unread
3498                                 (and (search-forward "A" nil t)
3499                                      (- (point) (point-min) 1)))))
3500           (goto-char (point-min))
3501           (push (cons 'replied (and (search-forward "B" nil t)
3502                                     (- (point) (point-min) 1)))
3503                 pos)
3504           (goto-char (point-min))
3505           (push (cons 'score (and (search-forward "C" nil t)
3506                                   (- (point) (point-min) 1)))
3507                 pos)
3508           (goto-char (point-min))
3509           (push (cons 'download (and (search-forward "D" nil t)
3510                                      (- (point) (point-min) 1)))
3511                 pos)))
3512       (setq gnus-summary-mark-positions pos))))
3513
3514 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3515   "Insert a dummy root in the summary buffer."
3516   (beginning-of-line)
3517   (gnus-add-text-properties
3518    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3519    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3520
3521 (defun gnus-summary-extract-address-component (from)
3522   (or (car (funcall gnus-extract-address-components from))
3523       from))
3524
3525 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3526   (let ((mail-parse-charset gnus-newsgroup-charset)
3527         ; Is it really necessary to do this next part for each summary line?
3528         ; Luckily, doesn't seem to slow things down much.
3529         (mail-parse-ignored-charsets
3530          (with-current-buffer gnus-summary-buffer
3531            gnus-newsgroup-ignored-charsets)))
3532     (or
3533      (and gnus-ignored-from-addresses
3534           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3535           (let ((extra-headers (mail-header-extra header))
3536                 to
3537                 newsgroups)
3538             (cond
3539              ((setq to (cdr (assq 'To extra-headers)))
3540               (concat gnus-summary-to-prefix
3541                       (inline
3542                         (gnus-summary-extract-address-component
3543                          (funcall gnus-decode-encoded-address-function to)))))
3544              ((setq newsgroups
3545                     (or
3546                      (cdr (assq 'Newsgroups extra-headers))
3547                      (and
3548                       (memq 'Newsgroups gnus-extra-headers)
3549                       (eq (car (gnus-find-method-for-group
3550                                 gnus-newsgroup-name)) 'nntp)
3551                       (gnus-group-real-name gnus-newsgroup-name))))
3552               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3553      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3554
3555 (defun gnus-summary-insert-line (gnus-tmp-header
3556                                  gnus-tmp-level gnus-tmp-current
3557                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3558                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3559                                  &optional gnus-tmp-dummy gnus-tmp-score
3560                                  gnus-tmp-process)
3561   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3562          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3563          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3564          (gnus-tmp-score-char
3565           (if (or (null gnus-summary-default-score)
3566                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3567                       gnus-summary-zcore-fuzz))
3568               ?                         ;Whitespace
3569             (if (< gnus-tmp-score gnus-summary-default-score)
3570                 gnus-score-below-mark gnus-score-over-mark)))
3571          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3572          (gnus-tmp-replied
3573           (cond (gnus-tmp-process gnus-process-mark)
3574                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3575                  gnus-cached-mark)
3576                 (gnus-tmp-replied gnus-replied-mark)
3577                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3578                  gnus-forwarded-mark)
3579                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3580                  gnus-saved-mark)
3581                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3582                  gnus-recent-mark)
3583                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3584                  gnus-unseen-mark)
3585                 (t gnus-no-mark)))
3586          (gnus-tmp-downloaded
3587           (cond (undownloaded
3588                  gnus-undownloaded-mark)
3589                 (gnus-newsgroup-agentized
3590                  gnus-downloaded-mark)
3591                 (t
3592                  gnus-no-mark)))
3593          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3594          (gnus-tmp-name
3595           (cond
3596            ((string-match "<[^>]+> *$" gnus-tmp-from)
3597             (let ((beg (match-beginning 0)))
3598               (or (and (string-match "^\".+\"" gnus-tmp-from)
3599                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3600                   (substring gnus-tmp-from 0 beg))))
3601            ((string-match "(.+)" gnus-tmp-from)
3602             (substring gnus-tmp-from
3603                        (1+ (match-beginning 0)) (1- (match-end 0))))
3604            (t gnus-tmp-from)))
3605          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3606          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3607          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3608          (buffer-read-only nil))
3609     (when (string= gnus-tmp-name "")
3610       (setq gnus-tmp-name gnus-tmp-from))
3611     (unless (numberp gnus-tmp-lines)
3612       (setq gnus-tmp-lines -1))
3613     (if (= gnus-tmp-lines -1)
3614         (setq gnus-tmp-lines "?")
3615       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3616       (gnus-put-text-property
3617      (point)
3618      (progn (eval gnus-summary-line-format-spec) (point))
3619        'gnus-number gnus-tmp-number)
3620     (when (gnus-visual-p 'summary-highlight 'highlight)
3621       (forward-line -1)
3622       (gnus-run-hooks 'gnus-summary-update-hook)
3623       (forward-line 1))))
3624
3625 (defun gnus-summary-update-line (&optional dont-update)
3626   "Update summary line after change."
3627   (when (and gnus-summary-default-score
3628              (not gnus-summary-inhibit-highlight))
3629     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3630            (article (gnus-summary-article-number))
3631            (score (gnus-summary-article-score article)))
3632       (unless dont-update
3633         (if (and gnus-summary-mark-below
3634                  (< (gnus-summary-article-score)
3635                     gnus-summary-mark-below))
3636             ;; This article has a low score, so we mark it as read.
3637             (when (memq article gnus-newsgroup-unreads)
3638               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3639           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3640             ;; This article was previously marked as read on account
3641             ;; of a low score, but now it has risen, so we mark it as
3642             ;; unread.
3643             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3644         (gnus-summary-update-mark
3645          (if (or (null gnus-summary-default-score)
3646                  (<= (abs (- score gnus-summary-default-score))
3647                      gnus-summary-zcore-fuzz))
3648              ?                          ;Whitespace
3649            (if (< score gnus-summary-default-score)
3650                gnus-score-below-mark gnus-score-over-mark))
3651          'score))
3652       ;; Do visual highlighting.
3653       (when (gnus-visual-p 'summary-highlight 'highlight)
3654         (gnus-run-hooks 'gnus-summary-update-hook)))))
3655
3656 (defvar gnus-tmp-new-adopts nil)
3657
3658 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3659   "Return the number of articles in THREAD.
3660 This may be 0 in some cases -- if none of the articles in
3661 the thread are to be displayed."
3662   (let* ((number
3663          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3664           (cond
3665            ((not (listp thread))
3666             1)
3667            ((and (consp thread) (cdr thread))
3668             (apply
3669              '+ 1 (mapcar
3670                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3671            ((null thread)
3672             1)
3673            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3674             1)
3675            (t 0))))
3676     (when (and level (zerop level) gnus-tmp-new-adopts)
3677       (incf number
3678             (apply '+ (mapcar
3679                        'gnus-summary-number-of-articles-in-thread
3680                        gnus-tmp-new-adopts))))
3681     (if char
3682         (if (> number 1) gnus-not-empty-thread-mark
3683           gnus-empty-thread-mark)
3684       number)))
3685
3686 (defsubst gnus-summary-line-message-size (head)
3687   "Return pretty-printed version of message size.
3688 This function is intended to be used in
3689 `gnus-summary-line-format-alist'."
3690   (let ((c (or (mail-header-chars head) -1)))
3691     (cond ((< c 0) "n/a")               ; chars not available
3692           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3693           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3694           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3695           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3696
3697
3698 (defun gnus-summary-set-local-parameters (group)
3699   "Go through the local params of GROUP and set all variable specs in that list."
3700   (let ((vars '(quit-config)))          ; Ignore quit-config.
3701     (dolist (elem (gnus-group-find-parameter group))
3702       (and (consp elem)                 ; Has to be a cons.
3703            (consp (cdr elem))           ; The cdr has to be a list.
3704            (symbolp (car elem))         ; Has to be a symbol in there.
3705            (not (memq (car elem) vars))
3706            (ignore-errors               ; So we set it.
3707              (push (car elem) vars)
3708              (make-local-variable (car elem))
3709              (set (car elem) (eval (nth 1 elem))))))))
3710
3711 (defun gnus-summary-read-group (group &optional show-all no-article
3712                                       kill-buffer no-display backward
3713                                       select-articles)
3714   "Start reading news in newsgroup GROUP.
3715 If SHOW-ALL is non-nil, already read articles are also listed.
3716 If NO-ARTICLE is non-nil, no article is selected initially.
3717 If NO-DISPLAY, don't generate a summary buffer."
3718   (let (result)
3719     (while (and group
3720                 (null (setq result
3721                             (let ((gnus-auto-select-next nil))
3722                               (or (gnus-summary-read-group-1
3723                                    group show-all no-article
3724                                    kill-buffer no-display
3725                                    select-articles)
3726                                   (setq show-all nil
3727                                         select-articles nil)))))
3728                 (eq gnus-auto-select-next 'quietly))
3729       (set-buffer gnus-group-buffer)
3730       ;; The entry function called above goes to the next
3731       ;; group automatically, so we go two groups back
3732       ;; if we are searching for the previous group.
3733       (when backward
3734         (gnus-group-prev-unread-group 2))
3735       (if (not (equal group (gnus-group-group-name)))
3736           (setq group (gnus-group-group-name))
3737         (setq group nil)))
3738     result))
3739
3740 (defun gnus-summary-read-group-1 (group show-all no-article
3741                                         kill-buffer no-display
3742                                         &optional select-articles)
3743   ;; Killed foreign groups can't be entered.
3744   ;;  (when (and (not (gnus-group-native-p group))
3745   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3746   ;;    (error "Dead non-native groups can't be entered"))
3747   (gnus-message 5 "Retrieving newsgroup: %s..."
3748                 (gnus-group-decoded-name group))
3749   (let* ((new-group (gnus-summary-setup-buffer group))
3750          (quit-config (gnus-group-quit-config group))
3751          (did-select (and new-group (gnus-select-newsgroup
3752                                      group show-all select-articles))))
3753     (cond
3754      ;; This summary buffer exists already, so we just select it.
3755      ((not new-group)
3756       (gnus-set-global-variables)
3757       (when kill-buffer
3758         (gnus-kill-or-deaden-summary kill-buffer))
3759       (gnus-configure-windows 'summary 'force)
3760       (gnus-set-mode-line 'summary)
3761       (gnus-summary-position-point)
3762       (message "")
3763       t)
3764      ;; We couldn't select this group.
3765      ((null did-select)
3766       (when (and (eq major-mode 'gnus-summary-mode)
3767                  (not (equal (current-buffer) kill-buffer)))
3768         (kill-buffer (current-buffer))
3769         (if (not quit-config)
3770             (progn
3771               ;; Update the info -- marks might need to be removed,
3772               ;; for instance.
3773               (gnus-summary-update-info)
3774               (set-buffer gnus-group-buffer)
3775               (gnus-group-jump-to-group group)
3776               (gnus-group-next-unread-group 1))
3777           (gnus-handle-ephemeral-exit quit-config)))
3778       (let ((grpinfo (gnus-get-info group)))
3779         (if (null (gnus-info-read grpinfo))
3780             (gnus-message 3 "Group %s contains no messages"
3781                           (gnus-group-decoded-name group))
3782           (gnus-message 3 "Can't select group")))
3783       nil)
3784      ;; The user did a `C-g' while prompting for number of articles,
3785      ;; so we exit this group.
3786      ((eq did-select 'quit)
3787       (and (eq major-mode 'gnus-summary-mode)
3788            (not (equal (current-buffer) kill-buffer))
3789            (kill-buffer (current-buffer)))
3790       (when kill-buffer
3791         (gnus-kill-or-deaden-summary kill-buffer))
3792       (if (not quit-config)
3793           (progn
3794             (set-buffer gnus-group-buffer)
3795             (gnus-group-jump-to-group group)
3796             (gnus-group-next-unread-group 1)
3797             (gnus-configure-windows 'group 'force))
3798         (gnus-handle-ephemeral-exit quit-config))
3799       ;; Finally signal the quit.
3800       (signal 'quit nil))
3801      ;; The group was successfully selected.
3802      (t
3803       (gnus-set-global-variables)
3804       ;; Save the active value in effect when the group was entered.
3805       (setq gnus-newsgroup-active
3806             (gnus-copy-sequence
3807              (gnus-active gnus-newsgroup-name)))
3808       ;; You can change the summary buffer in some way with this hook.
3809       (gnus-run-hooks 'gnus-select-group-hook)
3810       (when (memq 'summary (gnus-update-format-specifications
3811                             nil 'summary 'summary-mode 'summary-dummy))
3812         ;; The format specification for the summary line was updated,
3813         ;; so we need to update the mark positions as well.
3814         (gnus-update-summary-mark-positions))
3815       ;; Do score processing.
3816       (when gnus-use-scoring
3817         (gnus-possibly-score-headers))
3818       ;; Check whether to fill in the gaps in the threads.
3819       (when gnus-build-sparse-threads
3820         (gnus-build-sparse-threads))
3821       ;; Find the initial limit.
3822       (if show-all
3823           (let ((gnus-newsgroup-dormant nil))
3824             (gnus-summary-initial-limit show-all))
3825         (gnus-summary-initial-limit show-all))
3826       ;; Generate the summary buffer.
3827       (unless no-display
3828         (gnus-summary-prepare))
3829       (when gnus-use-trees
3830         (gnus-tree-open group)
3831         (setq gnus-summary-highlight-line-function
3832               'gnus-tree-highlight-article))
3833       ;; If the summary buffer is empty, but there are some low-scored
3834       ;; articles or some excluded dormants, we include these in the
3835       ;; buffer.
3836       (when (and (zerop (buffer-size))
3837                  (not no-display))
3838         (cond (gnus-newsgroup-dormant
3839                (gnus-summary-limit-include-dormant))
3840               ((and gnus-newsgroup-scored show-all)
3841                (gnus-summary-limit-include-expunged t))))
3842       ;; Function `gnus-apply-kill-file' must be called in this hook.
3843       (gnus-run-hooks 'gnus-apply-kill-hook)
3844       (if (and (zerop (buffer-size))
3845                (not no-display))
3846           (progn
3847             ;; This newsgroup is empty.
3848             (gnus-summary-catchup-and-exit nil t)
3849             (gnus-message 6 "No unread news")
3850             (when kill-buffer
3851               (gnus-kill-or-deaden-summary kill-buffer))
3852             ;; Return nil from this function.
3853             nil)
3854         ;; Hide conversation thread subtrees.  We cannot do this in
3855         ;; gnus-summary-prepare-hook since kill processing may not
3856         ;; work with hidden articles.
3857         (gnus-summary-maybe-hide-threads)
3858         (when kill-buffer
3859           (gnus-kill-or-deaden-summary kill-buffer))
3860         (gnus-summary-auto-select-subject)
3861         ;; Show first unread article if requested.
3862         (if (and (not no-article)
3863                  (not no-display)
3864                  gnus-newsgroup-unreads
3865                  gnus-auto-select-first)
3866             (progn
3867               (gnus-configure-windows 'summary)
3868               (let ((art (gnus-summary-article-number)))
3869                 (unless (and (not gnus-plugged)
3870                              (or (memq art gnus-newsgroup-undownloaded)
3871                                  (memq art gnus-newsgroup-downloadable)))
3872                   (gnus-summary-goto-article art))))
3873           ;; Don't select any articles.
3874           (gnus-summary-position-point)
3875           (gnus-configure-windows 'summary 'force)
3876           (gnus-set-mode-line 'summary))
3877         (when (and gnus-auto-center-group
3878                    (get-buffer-window gnus-group-buffer t))
3879           ;; Gotta use windows, because recenter does weird stuff if
3880           ;; the current buffer ain't the displayed window.
3881           (let ((owin (selected-window)))
3882             (select-window (get-buffer-window gnus-group-buffer t))
3883             (when (gnus-group-goto-group group)
3884               (recenter))
3885             (select-window owin)))
3886         ;; Mark this buffer as "prepared".
3887         (setq gnus-newsgroup-prepared t)
3888         (gnus-run-hooks 'gnus-summary-prepared-hook)
3889         (unless (gnus-ephemeral-group-p group)
3890           (gnus-group-update-group group))
3891         t)))))
3892
3893 (defun gnus-summary-auto-select-subject ()
3894   "Select the subject line on initial group entry."
3895   (goto-char (point-min))
3896   (cond
3897    ((eq gnus-auto-select-subject 'best)
3898     (gnus-summary-best-unread-subject))
3899    ((eq gnus-auto-select-subject 'unread)
3900     (gnus-summary-first-unread-subject))
3901    ((eq gnus-auto-select-subject 'unseen)
3902     (gnus-summary-first-unseen-subject))
3903    ((eq gnus-auto-select-subject 'unseen-or-unread)
3904     (gnus-summary-first-unseen-or-unread-subject))
3905    ((eq gnus-auto-select-subject 'first)
3906     ;; Do nothing.
3907     )
3908    ((functionp gnus-auto-select-subject)
3909     (funcall gnus-auto-select-subject))))
3910
3911 (defun gnus-summary-prepare ()
3912   "Generate the summary buffer."
3913   (interactive)
3914   (let ((buffer-read-only nil))
3915     (erase-buffer)
3916     (setq gnus-newsgroup-data nil
3917           gnus-newsgroup-data-reverse nil)
3918     (gnus-run-hooks 'gnus-summary-generate-hook)
3919     ;; Generate the buffer, either with threads or without.
3920     (when gnus-newsgroup-headers
3921       (gnus-summary-prepare-threads
3922        (if gnus-show-threads
3923            (gnus-sort-gathered-threads
3924             (funcall gnus-summary-thread-gathering-function
3925                      (gnus-sort-threads
3926                       (gnus-cut-threads (gnus-make-threads)))))
3927          ;; Unthreaded display.
3928          (gnus-sort-articles gnus-newsgroup-headers))))
3929     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3930     ;; Call hooks for modifying summary buffer.
3931     (goto-char (point-min))
3932     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3933
3934 (defsubst gnus-general-simplify-subject (subject)
3935   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3936   (setq subject
3937         (cond
3938          ;; Truncate the subject.
3939          (gnus-simplify-subject-functions
3940           (gnus-map-function gnus-simplify-subject-functions subject))
3941          ((numberp gnus-summary-gather-subject-limit)
3942           (setq subject (gnus-simplify-subject-re subject))
3943           (if (> (length subject) gnus-summary-gather-subject-limit)
3944               (substring subject 0 gnus-summary-gather-subject-limit)
3945             subject))
3946          ;; Fuzzily simplify it.
3947          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3948           (gnus-simplify-subject-fuzzy subject))
3949          ;; Just remove the leading "Re:".
3950          (t
3951           (gnus-simplify-subject-re subject))))
3952
3953   (if (and gnus-summary-gather-exclude-subject
3954            (string-match gnus-summary-gather-exclude-subject subject))
3955       nil                         ; This article shouldn't be gathered
3956     subject))
3957
3958 (defun gnus-summary-simplify-subject-query ()
3959   "Query where the respool algorithm would put this article."
3960   (interactive)
3961   (gnus-summary-select-article)
3962   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3963
3964 (defun gnus-gather-threads-by-subject (threads)
3965   "Gather threads by looking at Subject headers."
3966   (if (not gnus-summary-make-false-root)
3967       threads
3968     (let ((hashtb (gnus-make-hashtable 1024))
3969           (prev threads)
3970           (result threads)
3971           subject hthread whole-subject)
3972       (while threads
3973         (setq subject (gnus-general-simplify-subject
3974                        (setq whole-subject (mail-header-subject
3975                                             (caar threads)))))
3976         (when subject
3977           (if (setq hthread (gnus-gethash subject hashtb))
3978               (progn
3979                 ;; We enter a dummy root into the thread, if we
3980                 ;; haven't done that already.
3981                 (unless (stringp (caar hthread))
3982                   (setcar hthread (list whole-subject (car hthread))))
3983                 ;; We add this new gathered thread to this gathered
3984                 ;; thread.
3985                 (setcdr (car hthread)
3986                         (nconc (cdar hthread) (list (car threads))))
3987                 ;; Remove it from the list of threads.
3988                 (setcdr prev (cdr threads))
3989                 (setq threads prev))
3990             ;; Enter this thread into the hash table.
3991             (gnus-sethash subject
3992                           (if gnus-summary-make-false-root-always
3993                               (progn
3994                                 ;; If you want a dummy root above all
3995                                 ;; threads...
3996                                 (setcar threads (list whole-subject
3997                                                       (car threads)))
3998                                 threads)
3999                             threads)
4000                           hashtb)))
4001         (setq prev threads)
4002         (setq threads (cdr threads)))
4003       result)))
4004
4005 (defun gnus-gather-threads-by-references (threads)
4006   "Gather threads by looking at References headers."
4007   (let ((idhashtb (gnus-make-hashtable 1024))
4008         (thhashtb (gnus-make-hashtable 1024))
4009         (prev threads)
4010         (result threads)
4011         ids references id gthread gid entered ref)
4012     (while threads
4013       (when (setq references (mail-header-references (caar threads)))
4014         (setq id (mail-header-id (caar threads))
4015               ids (inline (gnus-split-references references))
4016               entered nil)
4017         (while (setq ref (pop ids))
4018           (setq ids (delete ref ids))
4019           (if (not (setq gid (gnus-gethash ref idhashtb)))
4020               (progn
4021                 (gnus-sethash ref id idhashtb)
4022                 (gnus-sethash id threads thhashtb))
4023             (setq gthread (gnus-gethash gid thhashtb))
4024             (unless entered
4025               ;; We enter a dummy root into the thread, if we
4026               ;; haven't done that already.
4027               (unless (stringp (caar gthread))
4028                 (setcar gthread (list (mail-header-subject (caar gthread))
4029                                       (car gthread))))
4030               ;; We add this new gathered thread to this gathered
4031               ;; thread.
4032               (setcdr (car gthread)
4033                       (nconc (cdar gthread) (list (car threads)))))
4034             ;; Add it into the thread hash table.
4035             (gnus-sethash id gthread thhashtb)
4036             (setq entered t)
4037             ;; Remove it from the list of threads.
4038             (setcdr prev (cdr threads))
4039             (setq threads prev))))
4040       (setq prev threads)
4041       (setq threads (cdr threads)))
4042     result))
4043
4044 (defun gnus-sort-gathered-threads (threads)
4045   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4046   (let ((result threads))
4047     (while threads
4048       (when (stringp (caar threads))
4049         (setcdr (car threads)
4050                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4051       (setq threads (cdr threads)))
4052     result))
4053
4054 (defun gnus-thread-loop-p (root thread)
4055   "Say whether ROOT is in THREAD."
4056   (let ((stack (list thread))
4057         (infloop 0)
4058         th)
4059     (while (setq thread (pop stack))
4060       (setq th (cdr thread))
4061       (while (and th
4062                   (not (eq (caar th) root)))
4063         (pop th))
4064       (if th
4065           ;; We have found a loop.
4066           (let (ref-dep)
4067             (setcdr thread (delq (car th) (cdr thread)))
4068             (if (boundp (setq ref-dep (intern "none"
4069                                               gnus-newsgroup-dependencies)))
4070                 (setcdr (symbol-value ref-dep)
4071                         (nconc (cdr (symbol-value ref-dep))
4072                                (list (car th))))
4073               (set ref-dep (list nil (car th))))
4074             (setq infloop 1
4075                   stack nil))
4076         ;; Push all the subthreads onto the stack.
4077         (push (cdr thread) stack)))
4078     infloop))
4079
4080 (defun gnus-make-threads ()
4081   "Go through the dependency hashtb and find the roots.  Return all threads."
4082   (let (threads)
4083     (while (catch 'infloop
4084              (mapatoms
4085               (lambda (refs)
4086                 ;; Deal with self-referencing References loops.
4087                 (when (and (car (symbol-value refs))
4088                            (not (zerop
4089                                  (apply
4090                                   '+
4091                                   (mapcar
4092                                    (lambda (thread)
4093                                      (gnus-thread-loop-p
4094                                       (car (symbol-value refs)) thread))
4095                                    (cdr (symbol-value refs)))))))
4096                   (setq threads nil)
4097                   (throw 'infloop t))
4098                 (unless (car (symbol-value refs))
4099                   ;; These threads do not refer back to any other
4100                   ;; articles, so they're roots.
4101                   (setq threads (append (cdr (symbol-value refs)) threads))))
4102               gnus-newsgroup-dependencies)))
4103     threads))
4104
4105 ;; Build the thread tree.
4106 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4107   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4108
4109 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4110 if it was already present.
4111
4112 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4113 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4114 Message-IDs will be renamed to a unique Message-ID before being
4115 entered.
4116
4117 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4118   (let* ((id (mail-header-id header))
4119          (id-dep (and id (intern id dependencies)))
4120          parent-id ref ref-dep ref-header replaced)
4121     ;; Enter this `header' in the `dependencies' table.
4122     (cond
4123      ((not id-dep)
4124       (setq header nil))
4125      ;; The first two cases do the normal part: enter a new `header'
4126      ;; in the `dependencies' table.
4127      ((not (boundp id-dep))
4128       (set id-dep (list header)))
4129      ((null (car (symbol-value id-dep)))
4130       (setcar (symbol-value id-dep) header))
4131
4132      ;; From here the `header' was already present in the
4133      ;; `dependencies' table.
4134      (force-new
4135       ;; Overrides an existing entry;
4136       ;; just set the header part of the entry.
4137       (setcar (symbol-value id-dep) header)
4138       (setq replaced t))
4139
4140      ;; Renames the existing `header' to a unique Message-ID.
4141      ((not gnus-summary-ignore-duplicates)
4142       ;; An article with this Message-ID has already been seen.
4143       ;; We rename the Message-ID.
4144       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4145            (list header))
4146       (mail-header-set-id header id))
4147
4148      ;; The last case ignores an existing entry, except it adds any
4149      ;; additional Xrefs (in case the two articles came from different
4150      ;; servers.
4151      ;; Also sets `header' to `nil' meaning that the `dependencies'
4152      ;; table was *not* modified.
4153      (t
4154       (mail-header-set-xref
4155        (car (symbol-value id-dep))
4156        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4157                    "")
4158                (or (mail-header-xref header) "")))
4159       (setq header nil)))
4160
4161     (when (and header (not replaced))
4162       ;; First check that we are not creating a References loop.
4163       (setq parent-id (gnus-parent-id (mail-header-references header)))
4164       (setq ref parent-id)
4165       (while (and ref
4166                   (setq ref-dep (intern-soft ref dependencies))
4167                   (boundp ref-dep)
4168                   (setq ref-header (car (symbol-value ref-dep))))
4169         (if (string= id ref)
4170             ;; Yuk!  This is a reference loop.  Make the article be a
4171             ;; root article.
4172             (progn
4173               (mail-header-set-references (car (symbol-value id-dep)) "none")
4174               (setq ref nil)
4175               (setq parent-id nil))
4176           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4177       (setq ref-dep (intern (or parent-id "none") dependencies))
4178       (if (boundp ref-dep)
4179           (setcdr (symbol-value ref-dep)
4180                   (nconc (cdr (symbol-value ref-dep))
4181                          (list (symbol-value id-dep))))
4182         (set ref-dep (list nil (symbol-value id-dep)))))
4183     header))
4184
4185 (defun gnus-extract-message-id-from-in-reply-to (string)
4186   (if (string-match "<[^>]+>" string)
4187       (substring string (match-beginning 0) (match-end 0))
4188     nil))
4189
4190 (defun gnus-build-sparse-threads ()
4191   (let ((headers gnus-newsgroup-headers)
4192         (mail-parse-charset gnus-newsgroup-charset)
4193         (gnus-summary-ignore-duplicates t)
4194         header references generation relations
4195         subject child end new-child date)
4196     ;; First we create an alist of generations/relations, where
4197     ;; generations is how much we trust the relation, and the relation
4198     ;; is parent/child.
4199     (gnus-message 7 "Making sparse threads...")
4200     (save-excursion
4201       (nnheader-set-temp-buffer " *gnus sparse threads*")
4202       (while (setq header (pop headers))
4203         (when (and (setq references (mail-header-references header))
4204                    (not (string= references "")))
4205           (insert references)
4206           (setq child (mail-header-id header)
4207                 subject (mail-header-subject header)
4208                 date (mail-header-date header)
4209                 generation 0)
4210           (while (search-backward ">" nil t)
4211             (setq end (1+ (point)))
4212             (when (search-backward "<" nil t)
4213               (setq new-child (buffer-substring (point) end))
4214               (push (list (incf generation)
4215                           child (setq child new-child)
4216                           subject date)
4217                     relations)))
4218           (when child
4219             (push (list (1+ generation) child nil subject) relations))
4220           (erase-buffer)))
4221       (kill-buffer (current-buffer)))
4222     ;; Sort over trustworthiness.
4223     (mapcar
4224      (lambda (relation)
4225        (when (gnus-dependencies-add-header
4226               (make-full-mail-header
4227                gnus-reffed-article-number
4228                (nth 3 relation) "" (or (nth 4 relation) "")
4229                (nth 1 relation)
4230                (or (nth 2 relation) "") 0 0 "")
4231               gnus-newsgroup-dependencies nil)
4232          (push gnus-reffed-article-number gnus-newsgroup-limit)
4233          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4234          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4235                gnus-newsgroup-reads)
4236          (decf gnus-reffed-article-number)))
4237      (sort relations 'car-less-than-car))
4238     (gnus-message 7 "Making sparse threads...done")))
4239
4240 (defun gnus-build-old-threads ()
4241   ;; Look at all the articles that refer back to old articles, and
4242   ;; fetch the headers for the articles that aren't there.  This will
4243   ;; build complete threads - if the roots haven't been expired by the
4244   ;; server, that is.
4245   (let ((mail-parse-charset gnus-newsgroup-charset)
4246         id heads)
4247     (mapatoms
4248      (lambda (refs)
4249        (when (not (car (symbol-value refs)))
4250          (setq heads (cdr (symbol-value refs)))
4251          (while heads
4252            (if (memq (mail-header-number (caar heads))
4253                      gnus-newsgroup-dormant)
4254                (setq heads (cdr heads))
4255              (setq id (symbol-name refs))
4256              (while (and (setq id (gnus-build-get-header id))
4257                          (not (car (gnus-id-to-thread id)))))
4258              (setq heads nil)))))
4259      gnus-newsgroup-dependencies)))
4260
4261 (defsubst gnus-remove-odd-characters (string)
4262   "Translate STRING into something that doesn't contain weird characters."
4263   (mm-subst-char-in-string
4264    ?\r ?\-
4265    (mm-subst-char-in-string ?\n ?\- string t) t))
4266
4267 ;; This function has to be called with point after the article number
4268 ;; on the beginning of the line.
4269 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4270   (let ((eol (point-at-eol))
4271         (buffer (current-buffer))
4272         header references in-reply-to)
4273
4274     ;; overview: [num subject from date id refs chars lines misc]
4275     (unwind-protect
4276         (let (x)
4277           (narrow-to-region (point) eol)
4278           (unless (eobp)
4279             (forward-char))
4280
4281           (setq header
4282                 (make-full-mail-header
4283                  number                 ; number
4284                  (condition-case ()     ; subject
4285                      (gnus-remove-odd-characters
4286                       (funcall gnus-decode-encoded-word-function
4287                                (setq x (nnheader-nov-field))))
4288                    (error x))
4289                  (condition-case ()     ; from
4290                      (gnus-remove-odd-characters
4291                       (funcall gnus-decode-encoded-address-function
4292                                (setq x (nnheader-nov-field))))
4293                    (error x))
4294                  (nnheader-nov-field)   ; date
4295                  (nnheader-nov-read-message-id number)  ; id
4296                  (setq references (nnheader-nov-field)) ; refs
4297                  (nnheader-nov-read-integer) ; chars
4298                  (nnheader-nov-read-integer) ; lines
4299                  (unless (eobp)
4300                    (if (looking-at "Xref: ")
4301                        (goto-char (match-end 0)))
4302                    (nnheader-nov-field)) ; Xref
4303                  (nnheader-nov-parse-extra)))) ; extra
4304
4305       (widen))
4306
4307     (when (and (string= references "")
4308                (setq in-reply-to (mail-header-extra header))
4309                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4310       (mail-header-set-references
4311        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4312
4313     (when gnus-alter-header-function
4314       (funcall gnus-alter-header-function header))
4315     (gnus-dependencies-add-header header dependencies force-new)))
4316
4317 (defun gnus-build-get-header (id)
4318   "Look through the buffer of NOV lines and find the header to ID.
4319 Enter this line into the dependencies hash table, and return
4320 the id of the parent article (if any)."
4321   (let ((deps gnus-newsgroup-dependencies)
4322         found header)
4323     (prog1
4324         (save-excursion
4325           (set-buffer nntp-server-buffer)
4326           (let ((case-fold-search nil))
4327             (goto-char (point-min))
4328             (while (and (not found)
4329                         (search-forward id nil t))
4330               (beginning-of-line)
4331               (setq found (looking-at
4332                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4333                                    (regexp-quote id))))
4334               (or found (beginning-of-line 2)))
4335             (when found
4336               (beginning-of-line)
4337               (and
4338                (setq header (gnus-nov-parse-line
4339                              (read (current-buffer)) deps))
4340                (gnus-parent-id (mail-header-references header))))))
4341       (when header
4342         (let ((number (mail-header-number header)))
4343           (push number gnus-newsgroup-limit)
4344           (push header gnus-newsgroup-headers)
4345           (if (memq number gnus-newsgroup-unselected)
4346               (progn
4347                 (setq gnus-newsgroup-unreads
4348                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4349                                                number))
4350                 (setq gnus-newsgroup-unselected
4351                       (delq number gnus-newsgroup-unselected)))
4352             (push number gnus-newsgroup-ancient)))))))
4353
4354 (defun gnus-build-all-threads ()
4355   "Read all the headers."
4356   (let ((gnus-summary-ignore-duplicates t)
4357         (mail-parse-charset gnus-newsgroup-charset)
4358         (dependencies gnus-newsgroup-dependencies)
4359         header article)
4360     (save-excursion
4361       (set-buffer nntp-server-buffer)
4362       (let ((case-fold-search nil))
4363         (goto-char (point-min))
4364         (while (not (eobp))
4365           (ignore-errors
4366             (setq article (read (current-buffer))
4367                   header (gnus-nov-parse-line article dependencies)))
4368           (when header
4369             (with-current-buffer gnus-summary-buffer
4370               (push header gnus-newsgroup-headers)
4371               (if (memq (setq article (mail-header-number header))
4372                         gnus-newsgroup-unselected)
4373                   (progn
4374                     (setq gnus-newsgroup-unreads
4375                           (gnus-add-to-sorted-list
4376                            gnus-newsgroup-unreads article))
4377                     (setq gnus-newsgroup-unselected
4378                           (delq article gnus-newsgroup-unselected)))
4379                 (push article gnus-newsgroup-ancient)))
4380             (forward-line 1)))))))
4381
4382 (defun gnus-summary-update-article-line (article header)
4383   "Update the line for ARTICLE using HEADER."
4384   (let* ((id (mail-header-id header))
4385          (thread (gnus-id-to-thread id)))
4386     (unless thread
4387       (error "Article in no thread"))
4388     ;; Update the thread.
4389     (setcar thread header)
4390     (gnus-summary-goto-subject article)
4391     (let* ((datal (gnus-data-find-list article))
4392            (data (car datal))
4393            (buffer-read-only nil)
4394            (level (gnus-summary-thread-level)))
4395       (gnus-delete-line)
4396       (let ((inserted (- (point)
4397                          (progn
4398                            (gnus-summary-insert-line
4399                             header level nil
4400                             (memq article gnus-newsgroup-undownloaded)
4401                             (gnus-article-mark article)
4402                             (memq article gnus-newsgroup-replied)
4403                             (memq article gnus-newsgroup-expirable)
4404                             ;; Only insert the Subject string when it's different
4405                             ;; from the previous Subject string.
4406                             (if (and
4407                                  gnus-show-threads
4408                                  (gnus-subject-equal
4409                                   (condition-case ()
4410                                       (mail-header-subject
4411                                        (gnus-data-header
4412                                         (cadr
4413                                          (gnus-data-find-list
4414                                           article
4415                                           (gnus-data-list t)))))
4416                                     ;; Error on the side of excessive subjects.
4417                                     (error ""))
4418                                   (mail-header-subject header)))
4419                                 ""
4420                               (mail-header-subject header))
4421                             nil (cdr (assq article gnus-newsgroup-scored))
4422                             (memq article gnus-newsgroup-processable))
4423                            (point)))))
4424         (when (cdr datal)
4425           (gnus-data-update-list
4426            (cdr datal)
4427            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4428
4429 (defun gnus-summary-update-article (article &optional iheader)
4430   "Update ARTICLE in the summary buffer."
4431   (set-buffer gnus-summary-buffer)
4432   (let* ((header (gnus-summary-article-header article))
4433          (id (mail-header-id header))
4434          (data (gnus-data-find article))
4435          (thread (gnus-id-to-thread id))
4436          (references (mail-header-references header))
4437          (parent
4438           (gnus-id-to-thread
4439            (or (gnus-parent-id
4440                 (when (and references
4441                            (not (equal "" references)))
4442                   references))
4443                "none")))
4444          (buffer-read-only nil)
4445          (old (car thread)))
4446     (when thread
4447       (unless iheader
4448         (setcar thread nil)
4449         (when parent
4450           (delq thread parent)))
4451       (if (gnus-summary-insert-subject id header)
4452           ;; Set the (possibly) new article number in the data structure.
4453           (gnus-data-set-number data (gnus-id-to-article id))
4454         (setcar thread old)
4455         nil))))
4456
4457 (defun gnus-rebuild-thread (id &optional line)
4458   "Rebuild the thread containing ID.
4459 If LINE, insert the rebuilt thread starting on line LINE."
4460   (let ((buffer-read-only nil)
4461         old-pos current thread data)
4462     (if (not gnus-show-threads)
4463         (setq thread (list (car (gnus-id-to-thread id))))
4464       ;; Get the thread this article is part of.
4465       (setq thread (gnus-remove-thread id)))
4466     (setq old-pos (point-at-bol))
4467     (setq current (save-excursion
4468                     (and (re-search-backward "[\r\n]" nil t)
4469                          (gnus-summary-article-number))))
4470     ;; If this is a gathered thread, we have to go some re-gathering.
4471     (when (stringp (car thread))
4472       (let ((subject (car thread))
4473             roots thr)
4474         (setq thread (cdr thread))
4475         (while thread
4476           (unless (memq (setq thr (gnus-id-to-thread
4477                                    (gnus-root-id
4478                                     (mail-header-id (caar thread)))))
4479                         roots)
4480             (push thr roots))
4481           (setq thread (cdr thread)))
4482         ;; We now have all (unique) roots.
4483         (if (= (length roots) 1)
4484             ;; All the loose roots are now one solid root.
4485             (setq thread (car roots))
4486           (setq thread (cons subject (gnus-sort-threads roots))))))
4487     (let (threads)
4488       ;; We then insert this thread into the summary buffer.
4489       (when line
4490         (goto-char (point-min))
4491         (forward-line (1- line)))
4492       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4493         (if gnus-show-threads
4494             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4495           (gnus-summary-prepare-unthreaded thread))
4496         (setq data (nreverse gnus-newsgroup-data))
4497         (setq threads gnus-newsgroup-threads))
4498       ;; We splice the new data into the data structure.
4499       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4500       ;;!!! then we want to insert at the beginning of the buffer.
4501       ;;!!! That happens to be true with Gnus now, but that may
4502       ;;!!! change in the future.  Perhaps.
4503       (gnus-data-enter-list
4504        (if line nil current) data (- (point) old-pos))
4505       (setq gnus-newsgroup-threads
4506             (nconc threads gnus-newsgroup-threads))
4507       (gnus-data-compute-positions))))
4508
4509 (defun gnus-number-to-header (number)
4510   "Return the header for article NUMBER."
4511   (let ((headers gnus-newsgroup-headers))
4512     (while (and headers
4513                 (not (= number (mail-header-number (car headers)))))
4514       (pop headers))
4515     (when headers
4516       (car headers))))
4517
4518 (defun gnus-parent-headers (in-headers &optional generation)
4519   "Return the headers of the GENERATIONeth parent of HEADERS."
4520   (unless generation
4521     (setq generation 1))
4522   (let ((parent t)
4523         (headers in-headers)
4524         references)
4525     (while (and parent
4526                 (not (zerop generation))
4527                 (setq references (mail-header-references headers)))
4528       (setq headers (if (and references
4529                              (setq parent (gnus-parent-id references)))
4530                         (car (gnus-id-to-thread parent))
4531                       nil))
4532       (decf generation))
4533     (and (not (eq headers in-headers))
4534          headers)))
4535
4536 (defun gnus-id-to-thread (id)
4537   "Return the (sub-)thread where ID appears."
4538   (gnus-gethash id gnus-newsgroup-dependencies))
4539
4540 (defun gnus-id-to-article (id)
4541   "Return the article number of ID."
4542   (let ((thread (gnus-id-to-thread id)))
4543     (when (and thread
4544                (car thread))
4545       (mail-header-number (car thread)))))
4546
4547 (defun gnus-id-to-header (id)
4548   "Return the article headers of ID."
4549   (car (gnus-id-to-thread id)))
4550
4551 (defun gnus-article-displayed-root-p (article)
4552   "Say whether ARTICLE is a root(ish) article."
4553   (let ((level (gnus-summary-thread-level article))
4554         (refs (mail-header-references  (gnus-summary-article-header article)))
4555         particle)
4556     (cond
4557      ((null level) nil)
4558      ((zerop level) t)
4559      ((null refs) t)
4560      ((null (gnus-parent-id refs)) t)
4561      ((and (= 1 level)
4562            (null (setq particle (gnus-id-to-article
4563                                  (gnus-parent-id refs))))
4564            (null (gnus-summary-thread-level particle)))))))
4565
4566 (defun gnus-root-id (id)
4567   "Return the id of the root of the thread where ID appears."
4568   (let (last-id prev)
4569     (while (and id (setq prev (car (gnus-id-to-thread id))))
4570       (setq last-id id
4571             id (gnus-parent-id (mail-header-references prev))))
4572     last-id))
4573
4574 (defun gnus-articles-in-thread (thread)
4575   "Return the list of articles in THREAD."
4576   (cons (mail-header-number (car thread))
4577         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4578
4579 (defun gnus-remove-thread (id &optional dont-remove)
4580   "Remove the thread that has ID in it."
4581   (let (headers thread last-id)
4582     ;; First go up in this thread until we find the root.
4583     (setq last-id (gnus-root-id id)
4584           headers (message-flatten-list (gnus-id-to-thread last-id)))
4585     ;; We have now found the real root of this thread.  It might have
4586     ;; been gathered into some loose thread, so we have to search
4587     ;; through the threads to find the thread we wanted.
4588     (let ((threads gnus-newsgroup-threads)
4589           sub)
4590       (while threads
4591         (setq sub (car threads))
4592         (if (stringp (car sub))
4593             ;; This is a gathered thread, so we look at the roots
4594             ;; below it to find whether this article is in this
4595             ;; gathered root.
4596             (progn
4597               (setq sub (cdr sub))
4598               (while sub
4599                 (when (member (caar sub) headers)
4600                   (setq thread (car threads)
4601                         threads nil
4602                         sub nil))
4603                 (setq sub (cdr sub))))
4604           ;; It's an ordinary thread, so we check it.
4605           (when (eq (car sub) (car headers))
4606             (setq thread sub
4607                   threads nil)))
4608         (setq threads (cdr threads)))
4609       ;; If this article is in no thread, then it's a root.
4610       (if thread
4611           (unless dont-remove
4612             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4613         (setq thread (gnus-id-to-thread last-id)))
4614       (when thread
4615         (prog1
4616             thread                      ; We return this thread.
4617           (unless dont-remove
4618             (if (stringp (car thread))
4619                 (progn
4620                   ;; If we use dummy roots, then we have to remove the
4621                   ;; dummy root as well.
4622                   (when (eq gnus-summary-make-false-root 'dummy)
4623                     ;; We go to the dummy root by going to
4624                     ;; the first sub-"thread", and then one line up.
4625                     (gnus-summary-goto-article
4626                      (mail-header-number (caadr thread)))
4627                     (forward-line -1)
4628                     (gnus-delete-line)
4629                     (gnus-data-compute-positions))
4630                   (setq thread (cdr thread))
4631                   (while thread
4632                     (gnus-remove-thread-1 (car thread))
4633                     (setq thread (cdr thread))))
4634               (gnus-remove-thread-1 thread))))))))
4635
4636 (defun gnus-remove-thread-1 (thread)
4637   "Remove the thread THREAD recursively."
4638   (let ((number (mail-header-number (pop thread)))
4639         d)
4640     (setq thread (reverse thread))
4641     (while thread
4642       (gnus-remove-thread-1 (pop thread)))
4643     (when (setq d (gnus-data-find number))
4644       (goto-char (gnus-data-pos d))
4645       (gnus-summary-show-thread)
4646       (gnus-data-remove
4647        number
4648        (- (point-at-bol)
4649           (prog1
4650               (1+ (point-at-eol))
4651             (gnus-delete-line)))))))
4652
4653 (defun gnus-sort-threads-1 (threads func)
4654   (sort (mapcar (lambda (thread)
4655                   (cons (car thread)
4656                         (and (cdr thread)
4657                              (gnus-sort-threads-1 (cdr thread) func))))
4658                 threads) func))
4659
4660 (defun gnus-sort-threads (threads)
4661   "Sort THREADS."
4662   (if (not gnus-thread-sort-functions)
4663       threads
4664     (gnus-message 8 "Sorting threads...")
4665     (let ((max-lisp-eval-depth 5000))
4666       (prog1 (gnus-sort-threads-1
4667          threads
4668          (gnus-make-sort-function gnus-thread-sort-functions))
4669         (gnus-message 8 "Sorting threads...done")))))
4670
4671 (defun gnus-sort-articles (articles)
4672   "Sort ARTICLES."
4673   (when gnus-article-sort-functions
4674     (gnus-message 7 "Sorting articles...")
4675     (prog1
4676         (setq gnus-newsgroup-headers
4677               (sort articles (gnus-make-sort-function
4678                               gnus-article-sort-functions)))
4679       (gnus-message 7 "Sorting articles...done"))))
4680
4681 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4682 (defmacro gnus-thread-header (thread)
4683   "Return header of first article in THREAD.
4684 Note that THREAD must never, ever be anything else than a variable -
4685 using some other form will lead to serious barfage."
4686   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4687   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4688   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4689         (vector thread) 2))
4690
4691 (defsubst gnus-article-sort-by-number (h1 h2)
4692   "Sort articles by article number."
4693   (< (mail-header-number h1)
4694      (mail-header-number h2)))
4695
4696 (defun gnus-thread-sort-by-number (h1 h2)
4697   "Sort threads by root article number."
4698   (gnus-article-sort-by-number
4699    (gnus-thread-header h1) (gnus-thread-header h2)))
4700
4701 (defsubst gnus-article-sort-by-random (h1 h2)
4702   "Sort articles by article number."
4703   (zerop (random 2)))
4704
4705 (defun gnus-thread-sort-by-random (h1 h2)
4706   "Sort threads by root article number."
4707   (gnus-article-sort-by-random
4708    (gnus-thread-header h1) (gnus-thread-header h2)))
4709
4710 (defsubst gnus-article-sort-by-lines (h1 h2)
4711   "Sort articles by article Lines header."
4712   (< (mail-header-lines h1)
4713      (mail-header-lines h2)))
4714
4715 (defun gnus-thread-sort-by-lines (h1 h2)
4716   "Sort threads by root article Lines header."
4717   (gnus-article-sort-by-lines
4718    (gnus-thread-header h1) (gnus-thread-header h2)))
4719
4720 (defsubst gnus-article-sort-by-chars (h1 h2)
4721   "Sort articles by octet length."
4722   (< (mail-header-chars h1)
4723      (mail-header-chars h2)))
4724
4725 (defun gnus-thread-sort-by-chars (h1 h2)
4726   "Sort threads by root article octet length."
4727   (gnus-article-sort-by-chars
4728    (gnus-thread-header h1) (gnus-thread-header h2)))
4729
4730 (defsubst gnus-article-sort-by-author (h1 h2)
4731   "Sort articles by root author."
4732   (string-lessp
4733    (let ((extract (funcall
4734                    gnus-extract-address-components
4735                    (mail-header-from h1))))
4736      (or (car extract) (cadr extract) ""))
4737    (let ((extract (funcall
4738                    gnus-extract-address-components
4739                    (mail-header-from h2))))
4740      (or (car extract) (cadr extract) ""))))
4741
4742 (defun gnus-thread-sort-by-author (h1 h2)
4743   "Sort threads by root author."
4744   (gnus-article-sort-by-author
4745    (gnus-thread-header h1)  (gnus-thread-header h2)))
4746
4747 (defsubst gnus-article-sort-by-recipient (h1 h2)
4748   "Sort articles by recipient."
4749   (string-lessp
4750    (let ((extract (funcall
4751                    gnus-extract-address-components
4752                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4753      (or (car extract) (cadr extract)))
4754    (let ((extract (funcall
4755                    gnus-extract-address-components
4756                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4757      (or (car extract) (cadr extract)))))
4758
4759 (defun gnus-thread-sort-by-recipient (h1 h2)
4760   "Sort threads by root recipient."
4761   (gnus-article-sort-by-recipient
4762    (gnus-thread-header h1) (gnus-thread-header h2)))
4763
4764 (defsubst gnus-article-sort-by-subject (h1 h2)
4765   "Sort articles by root subject."
4766   (string-lessp
4767    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4768    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4769
4770 (defun gnus-thread-sort-by-subject (h1 h2)
4771   "Sort threads by root subject."
4772   (gnus-article-sort-by-subject
4773    (gnus-thread-header h1) (gnus-thread-header h2)))
4774
4775 (defsubst gnus-article-sort-by-date (h1 h2)
4776   "Sort articles by root article date."
4777   (time-less-p
4778    (gnus-date-get-time (mail-header-date h1))
4779    (gnus-date-get-time (mail-header-date h2))))
4780
4781 (defun gnus-thread-sort-by-date (h1 h2)
4782   "Sort threads by root article date."
4783   (gnus-article-sort-by-date
4784    (gnus-thread-header h1) (gnus-thread-header h2)))
4785
4786 (defsubst gnus-article-sort-by-score (h1 h2)
4787   "Sort articles by root article score.
4788 Unscored articles will be counted as having a score of zero."
4789   (> (or (cdr (assq (mail-header-number h1)
4790                     gnus-newsgroup-scored))
4791          gnus-summary-default-score 0)
4792      (or (cdr (assq (mail-header-number h2)
4793                     gnus-newsgroup-scored))
4794          gnus-summary-default-score 0)))
4795
4796 (defun gnus-thread-sort-by-score (h1 h2)
4797   "Sort threads by root article score."
4798   (gnus-article-sort-by-score
4799    (gnus-thread-header h1) (gnus-thread-header h2)))
4800
4801 (defun gnus-thread-sort-by-total-score (h1 h2)
4802   "Sort threads by the sum of all scores in the thread.
4803 Unscored articles will be counted as having a score of zero."
4804   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4805
4806 (defun gnus-thread-total-score (thread)
4807   ;; This function find the total score of THREAD.
4808   (cond
4809    ((null thread)
4810     0)
4811    ((consp thread)
4812     (if (stringp (car thread))
4813         (apply gnus-thread-score-function 0
4814                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4815       (gnus-thread-total-score-1 thread)))
4816    (t
4817     (gnus-thread-total-score-1 (list thread)))))
4818
4819 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4820   "Sort threads such that the thread with the most recently arrived article comes first."
4821   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4822
4823 (defun gnus-thread-highest-number (thread)
4824   "Return the highest article number in THREAD."
4825   (apply 'max (mapcar (lambda (header)
4826                         (mail-header-number header))
4827                       (message-flatten-list thread))))
4828
4829 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4830   "Sort threads such that the thread with the most recently dated article comes first."
4831   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4832
4833 (defun gnus-thread-latest-date (thread)
4834   "Return the highest article date in THREAD."
4835   (let ((previous-time 0))
4836     (apply 'max
4837            (mapcar
4838             (lambda (header)
4839               (setq previous-time
4840                     (condition-case ()
4841                         (time-to-seconds (mail-header-parse-date
4842                                           (mail-header-date header)))
4843                       (error previous-time))))
4844             (sort
4845              (message-flatten-list thread)
4846              (lambda (h1 h2)
4847                (< (mail-header-number h1)
4848                   (mail-header-number h2))))))))
4849
4850 (defun gnus-thread-total-score-1 (root)
4851   ;; This function find the total score of the thread below ROOT.
4852   (setq root (car root))
4853   (apply gnus-thread-score-function
4854          (or (append
4855               (mapcar 'gnus-thread-total-score
4856                       (cdr (gnus-id-to-thread (mail-header-id root))))
4857               (when (> (mail-header-number root) 0)
4858                 (list (or (cdr (assq (mail-header-number root)
4859                                      gnus-newsgroup-scored))
4860                           gnus-summary-default-score 0))))
4861              (list gnus-summary-default-score)
4862              '(0))))
4863
4864 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4865 (defvar gnus-tmp-prev-subject nil)
4866 (defvar gnus-tmp-false-parent nil)
4867 (defvar gnus-tmp-root-expunged nil)
4868 (defvar gnus-tmp-dummy-line nil)
4869
4870 (eval-when-compile (defvar gnus-tmp-header))
4871 (defun gnus-extra-header (type &optional header)
4872   "Return the extra header of TYPE."
4873   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4874       ""))
4875
4876 (defvar gnus-tmp-thread-tree-header-string "")
4877
4878 (defcustom gnus-sum-thread-tree-root "> "
4879   "With %B spec, used for the root of a thread.
4880 If nil, use subject instead."
4881   :version "22.1"
4882   :type '(radio (const :format "%v  " nil) string)
4883   :group 'gnus-thread)
4884
4885 (defcustom gnus-sum-thread-tree-false-root "> "
4886   "With %B spec, used for a false root of a thread.
4887 If nil, use subject instead."
4888   :version "22.1"
4889   :type '(radio (const :format "%v  " nil) string)
4890   :group 'gnus-thread)
4891
4892 (defcustom gnus-sum-thread-tree-single-indent ""
4893   "With %B spec, used for a thread with just one message.
4894 If nil, use subject instead."
4895   :version "22.1"
4896   :type '(radio (const :format "%v  " nil) string)
4897   :group 'gnus-thread)
4898
4899 (defcustom gnus-sum-thread-tree-vertical "| "
4900   "With %B spec, used for drawing a vertical line."
4901   :version "22.1"
4902   :type 'string
4903   :group 'gnus-thread)
4904
4905 (defcustom gnus-sum-thread-tree-indent "  "
4906   "With %B spec, used for indenting."
4907   :version "22.1"
4908   :type 'string
4909   :group 'gnus-thread)
4910
4911 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4912   "With %B spec, used for a leaf with brothers."
4913   :version "22.1"
4914   :type 'string
4915   :group 'gnus-thread)
4916
4917 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4918   "With %B spec, used for a leaf without brothers."
4919   :version "22.1"
4920   :type 'string
4921   :group 'gnus-thread)
4922
4923 (defun gnus-summary-prepare-threads (threads)
4924   "Prepare summary buffer from THREADS and indentation LEVEL.
4925 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4926 or a straight list of headers."
4927   (gnus-message 7 "Generating summary...")
4928
4929   (setq gnus-newsgroup-threads threads)
4930   (beginning-of-line)
4931
4932   (let ((gnus-tmp-level 0)
4933         (default-score (or gnus-summary-default-score 0))
4934         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4935         (building-line-count gnus-summary-display-while-building)
4936         (building-count (integerp gnus-summary-display-while-building))
4937         thread number subject stack state gnus-tmp-gathered beg-match
4938         new-roots gnus-tmp-new-adopts thread-end simp-subject
4939         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4940         gnus-tmp-replied gnus-tmp-subject-or-nil
4941         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4942         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4943         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4944         tree-stack)
4945
4946     (setq gnus-tmp-prev-subject nil
4947           gnus-tmp-thread-tree-header-string "")
4948
4949     (if (vectorp (car threads))
4950         ;; If this is a straight (sic) list of headers, then a
4951         ;; threaded summary display isn't required, so we just create
4952         ;; an unthreaded one.
4953         (gnus-summary-prepare-unthreaded threads)
4954
4955       ;; Do the threaded display.
4956
4957       (if gnus-summary-display-while-building
4958           (switch-to-buffer (buffer-name)))
4959       (while (or threads stack gnus-tmp-new-adopts new-roots)
4960
4961         (if (and (= gnus-tmp-level 0)
4962                  (or (not stack)
4963                      (= (caar stack) 0))
4964                  (not gnus-tmp-false-parent)
4965                  (or gnus-tmp-new-adopts new-roots))
4966             (if gnus-tmp-new-adopts
4967                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4968                       thread (list (car gnus-tmp-new-adopts))
4969                       gnus-tmp-header (caar thread)
4970                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4971               (when new-roots
4972                 (setq thread (list (car new-roots))
4973                       gnus-tmp-header (caar thread)
4974                       new-roots (cdr new-roots))))
4975
4976           (if threads
4977               ;; If there are some threads, we do them before the
4978               ;; threads on the stack.
4979               (setq thread threads
4980                     gnus-tmp-header (caar thread))
4981             ;; There were no current threads, so we pop something off
4982             ;; the stack.
4983             (setq state (car stack)
4984                   gnus-tmp-level (car state)
4985                   tree-stack (cadr state)
4986                   thread (caddr state)
4987                   stack (cdr stack)
4988                   gnus-tmp-header (caar thread))))
4989
4990         (setq gnus-tmp-false-parent nil)
4991         (setq gnus-tmp-root-expunged nil)
4992         (setq thread-end nil)
4993
4994         (if (stringp gnus-tmp-header)
4995             ;; The header is a dummy root.
4996             (cond
4997              ((eq gnus-summary-make-false-root 'adopt)
4998               ;; We let the first article adopt the rest.
4999               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5000                                                (cddar thread)))
5001               (setq gnus-tmp-gathered
5002                     (nconc (mapcar
5003                             (lambda (h) (mail-header-number (car h)))
5004                             (cddar thread))
5005                            gnus-tmp-gathered))
5006               (setq thread (cons (list (caar thread)
5007                                        (cadar thread))
5008                                  (cdr thread)))
5009               (setq gnus-tmp-level -1
5010                     gnus-tmp-false-parent t))
5011              ((eq gnus-summary-make-false-root 'empty)
5012               ;; We print adopted articles with empty subject fields.
5013               (setq gnus-tmp-gathered
5014                     (nconc (mapcar
5015                             (lambda (h) (mail-header-number (car h)))
5016                             (cddar thread))
5017                            gnus-tmp-gathered))
5018               (setq gnus-tmp-level -1))
5019              ((eq gnus-summary-make-false-root 'dummy)
5020               ;; We remember that we probably want to output a dummy
5021               ;; root.
5022               (setq gnus-tmp-dummy-line gnus-tmp-header)
5023               (setq gnus-tmp-prev-subject gnus-tmp-header))
5024              (t
5025               ;; We do not make a root for the gathered
5026               ;; sub-threads at all.
5027               (setq gnus-tmp-level -1)))
5028
5029           (setq number (mail-header-number gnus-tmp-header)
5030                 subject (mail-header-subject gnus-tmp-header)
5031                 simp-subject (gnus-simplify-subject-fully subject))
5032
5033           (cond
5034            ;; If the thread has changed subject, we might want to make
5035            ;; this subthread into a root.
5036            ((and (null gnus-thread-ignore-subject)
5037                  (not (zerop gnus-tmp-level))
5038                  gnus-tmp-prev-subject
5039                  (not (string= gnus-tmp-prev-subject simp-subject)))
5040             (setq new-roots (nconc new-roots (list (car thread)))
5041                   thread-end t
5042                   gnus-tmp-header nil))
5043            ;; If the article lies outside the current limit,
5044            ;; then we do not display it.
5045            ((not (memq number gnus-newsgroup-limit))
5046             (setq gnus-tmp-gathered
5047                   (nconc (mapcar
5048                           (lambda (h) (mail-header-number (car h)))
5049                           (cdar thread))
5050                          gnus-tmp-gathered))
5051             (setq gnus-tmp-new-adopts (if (cdar thread)
5052                                           (append gnus-tmp-new-adopts
5053                                                   (cdar thread))
5054                                         gnus-tmp-new-adopts)
5055                   thread-end t
5056                   gnus-tmp-header nil)
5057             (when (zerop gnus-tmp-level)
5058               (setq gnus-tmp-root-expunged t)))
5059            ;; Perhaps this article is to be marked as read?
5060            ((and gnus-summary-mark-below
5061                  (< (or (cdr (assq number gnus-newsgroup-scored))
5062                         default-score)
5063                     gnus-summary-mark-below)
5064                  ;; Don't touch sparse articles.
5065                  (not (gnus-summary-article-sparse-p number))
5066                  (not (gnus-summary-article-ancient-p number)))
5067             (setq gnus-newsgroup-unreads
5068                   (delq number gnus-newsgroup-unreads))
5069             (if gnus-newsgroup-auto-expire
5070                 (setq gnus-newsgroup-expirable
5071                       (gnus-add-to-sorted-list
5072                        gnus-newsgroup-expirable number))
5073               (push (cons number gnus-low-score-mark)
5074                     gnus-newsgroup-reads))))
5075
5076           (when gnus-tmp-header
5077             ;; We may have an old dummy line to output before this
5078             ;; article.
5079             (when (and gnus-tmp-dummy-line
5080                        (gnus-subject-equal
5081                         gnus-tmp-dummy-line
5082                         (mail-header-subject gnus-tmp-header)))
5083               (gnus-summary-insert-dummy-line
5084                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5085               (setq gnus-tmp-dummy-line nil))
5086
5087             ;; Compute the mark.
5088             (setq gnus-tmp-unread (gnus-article-mark number))
5089
5090             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5091                                   gnus-tmp-header gnus-tmp-level)
5092                   gnus-newsgroup-data)
5093
5094             ;; Actually insert the line.
5095             (setq
5096              gnus-tmp-subject-or-nil
5097              (cond
5098               ((and gnus-thread-ignore-subject
5099                     gnus-tmp-prev-subject
5100                     (not (string= gnus-tmp-prev-subject simp-subject)))
5101                subject)
5102               ((zerop gnus-tmp-level)
5103                (if (and (eq gnus-summary-make-false-root 'empty)
5104                         (memq number gnus-tmp-gathered)
5105                         gnus-tmp-prev-subject
5106                         (string= gnus-tmp-prev-subject simp-subject))
5107                    gnus-summary-same-subject
5108                  subject))
5109               (t gnus-summary-same-subject)))
5110             (if (and (eq gnus-summary-make-false-root 'adopt)
5111                      (= gnus-tmp-level 1)
5112                      (memq number gnus-tmp-gathered))
5113                 (setq gnus-tmp-opening-bracket ?\<
5114                       gnus-tmp-closing-bracket ?\>)
5115               (setq gnus-tmp-opening-bracket ?\[
5116                     gnus-tmp-closing-bracket ?\]))
5117             (setq
5118              gnus-tmp-indentation
5119              (aref gnus-thread-indent-array gnus-tmp-level)
5120              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5121              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5122                                 gnus-summary-default-score 0)
5123              gnus-tmp-score-char
5124              (if (or (null gnus-summary-default-score)
5125                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5126                          gnus-summary-zcore-fuzz))
5127                  ?                      ;Whitespace
5128                (if (< gnus-tmp-score gnus-summary-default-score)
5129                    gnus-score-below-mark gnus-score-over-mark))
5130              gnus-tmp-replied
5131              (cond ((memq number gnus-newsgroup-processable)
5132                     gnus-process-mark)
5133                    ((memq number gnus-newsgroup-cached)
5134                     gnus-cached-mark)
5135                    ((memq number gnus-newsgroup-replied)
5136                     gnus-replied-mark)
5137                    ((memq number gnus-newsgroup-forwarded)
5138                     gnus-forwarded-mark)
5139                    ((memq number gnus-newsgroup-saved)
5140                     gnus-saved-mark)
5141                    ((memq number gnus-newsgroup-recent)
5142                     gnus-recent-mark)
5143                    ((memq number gnus-newsgroup-unseen)
5144                     gnus-unseen-mark)
5145                    (t gnus-no-mark))
5146              gnus-tmp-downloaded
5147              (cond ((memq number gnus-newsgroup-undownloaded)
5148                     gnus-undownloaded-mark)
5149                    (gnus-newsgroup-agentized
5150                     gnus-downloaded-mark)
5151                    (t
5152                     gnus-no-mark))
5153              gnus-tmp-from (mail-header-from gnus-tmp-header)
5154              gnus-tmp-name
5155              (cond
5156               ((string-match "<[^>]+> *$" gnus-tmp-from)
5157                (setq beg-match (match-beginning 0))
5158                (or (and (string-match "^\".+\"" gnus-tmp-from)
5159                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5160                    (substring gnus-tmp-from 0 beg-match)))
5161               ((string-match "(.+)" gnus-tmp-from)
5162                (substring gnus-tmp-from
5163                           (1+ (match-beginning 0)) (1- (match-end 0))))
5164               (t gnus-tmp-from))
5165
5166              ;; Do the %B string
5167              gnus-tmp-thread-tree-header-string
5168              (cond
5169               ((not gnus-show-threads) "")
5170               ((zerop gnus-tmp-level)
5171                (cond ((cdar thread)
5172                       (or gnus-sum-thread-tree-root subject))
5173                      (gnus-tmp-new-adopts
5174                       (or gnus-sum-thread-tree-false-root subject))
5175                      (t
5176                       (or gnus-sum-thread-tree-single-indent subject))))
5177               (t
5178                (concat (apply 'concat
5179                               (mapcar (lambda (item)
5180                                         (if (= item 1)
5181                                             gnus-sum-thread-tree-vertical
5182                                           gnus-sum-thread-tree-indent))
5183                                       (cdr (reverse tree-stack))))
5184                        (if (nth 1 thread)
5185                            gnus-sum-thread-tree-leaf-with-other
5186                          gnus-sum-thread-tree-single-leaf)))))
5187             (when (string= gnus-tmp-name "")
5188               (setq gnus-tmp-name gnus-tmp-from))
5189             (unless (numberp gnus-tmp-lines)
5190               (setq gnus-tmp-lines -1))
5191             (if (= gnus-tmp-lines -1)
5192                 (setq gnus-tmp-lines "?")
5193               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5194               (gnus-put-text-property
5195              (point)
5196              (progn (eval gnus-summary-line-format-spec) (point))
5197                'gnus-number number)
5198             (when gnus-visual-p
5199               (forward-line -1)
5200               (gnus-run-hooks 'gnus-summary-update-hook)
5201               (forward-line 1))
5202
5203             (setq gnus-tmp-prev-subject simp-subject)))
5204
5205         (when (nth 1 thread)
5206           (push (list (max 0 gnus-tmp-level)
5207                       (copy-sequence tree-stack)
5208                       (nthcdr 1 thread))
5209                 stack))
5210         (push (if (nth 1 thread) 1 0) tree-stack)
5211         (incf gnus-tmp-level)
5212         (setq threads (if thread-end nil (cdar thread)))
5213         (if gnus-summary-display-while-building
5214             (if building-count
5215                 (progn
5216                   ;; use a set frequency
5217                   (setq building-line-count (1- building-line-count))
5218                   (when (= building-line-count 0)
5219                     (sit-for 0)
5220                     (setq building-line-count
5221                           gnus-summary-display-while-building)))
5222               ;; always
5223               (sit-for 0)))
5224         (unless threads
5225           (setq gnus-tmp-level 0)))))
5226   (gnus-message 7 "Generating summary...done"))
5227
5228 (defun gnus-summary-prepare-unthreaded (headers)
5229   "Generate an unthreaded summary buffer based on HEADERS."
5230   (let (header number mark)
5231
5232     (beginning-of-line)
5233
5234     (while headers
5235       ;; We may have to root out some bad articles...
5236       (when (memq (setq number (mail-header-number
5237                                 (setq header (pop headers))))
5238                   gnus-newsgroup-limit)
5239         ;; Mark article as read when it has a low score.
5240         (when (and gnus-summary-mark-below
5241                    (< (or (cdr (assq number gnus-newsgroup-scored))
5242                           gnus-summary-default-score 0)
5243                       gnus-summary-mark-below)
5244                    (not (gnus-summary-article-ancient-p number)))
5245           (setq gnus-newsgroup-unreads
5246                 (delq number gnus-newsgroup-unreads))
5247           (if gnus-newsgroup-auto-expire
5248               (push number gnus-newsgroup-expirable)
5249             (push (cons number gnus-low-score-mark)
5250                   gnus-newsgroup-reads)))
5251
5252         (setq mark (gnus-article-mark number))
5253         (push (gnus-data-make number mark (1+ (point)) header 0)
5254               gnus-newsgroup-data)
5255         (gnus-summary-insert-line
5256          header 0 number
5257          (memq number gnus-newsgroup-undownloaded)
5258          mark (memq number gnus-newsgroup-replied)
5259          (memq number gnus-newsgroup-expirable)
5260          (mail-header-subject header) nil
5261          (cdr (assq number gnus-newsgroup-scored))
5262          (memq number gnus-newsgroup-processable))))))
5263
5264 (defun gnus-summary-remove-list-identifiers ()
5265   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5266   (let ((regexp (if (consp gnus-list-identifiers)
5267                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5268                   gnus-list-identifiers))
5269         changed subject)
5270     (when regexp
5271       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5272       (dolist (header gnus-newsgroup-headers)
5273         (setq subject (mail-header-subject header)
5274               changed nil)
5275         (while (string-match regexp subject)
5276           (setq subject
5277                 (concat (substring subject 0 (match-beginning 1))
5278                         (substring subject (match-end 0)))
5279                 changed t))
5280         (when changed
5281           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5282             (setq subject
5283                   (concat (substring subject 0 (match-beginning 1))
5284                           (substring subject (match-end 1)))))
5285           (mail-header-set-subject header subject))))))
5286
5287 (defun gnus-fetch-headers (articles)
5288   "Fetch headers of ARTICLES."
5289   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5290     (gnus-message 5 "Fetching headers for %s..." name)
5291     (prog1
5292         (if (eq 'nov
5293                 (setq gnus-headers-retrieved-by
5294                       (gnus-retrieve-headers
5295                        articles gnus-newsgroup-name
5296                        ;; We might want to fetch old headers, but
5297                        ;; not if there is only 1 article.
5298                        (and (or (and
5299                                  (not (eq gnus-fetch-old-headers 'some))
5300                                  (not (numberp gnus-fetch-old-headers)))
5301                                 (> (length articles) 1))
5302                             gnus-fetch-old-headers))))
5303             (gnus-get-newsgroup-headers-xover
5304              articles nil nil gnus-newsgroup-name t)
5305           (gnus-get-newsgroup-headers))
5306       (gnus-message 5 "Fetching headers for %s...done" name))))
5307
5308 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5309   "Select newsgroup GROUP.
5310 If READ-ALL is non-nil, all articles in the group are selected.
5311 If SELECT-ARTICLES, only select those articles from GROUP."
5312   (let* ((entry (gnus-group-entry group))
5313          ;;!!! Dirty hack; should be removed.
5314          (gnus-summary-ignore-duplicates
5315           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5316               t
5317             gnus-summary-ignore-duplicates))
5318          (info (nth 2 entry))
5319          articles fetched-articles cached)
5320
5321     (unless (gnus-check-server
5322              (set (make-local-variable 'gnus-current-select-method)
5323                   (gnus-find-method-for-group group)))
5324       (error "Couldn't open server"))
5325
5326     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5327         (gnus-activate-group group)     ; Or we can activate it...
5328         (progn                          ; Or we bug out.
5329           (when (equal major-mode 'gnus-summary-mode)
5330             (gnus-kill-buffer (current-buffer)))
5331           (error "Couldn't activate group %s: %s"
5332                  (gnus-group-decoded-name group) (gnus-status-message group))))
5333
5334     (unless (gnus-request-group group t)
5335       (when (equal major-mode 'gnus-summary-mode)
5336         (gnus-kill-buffer (current-buffer)))
5337       (error "Couldn't request group %s: %s"
5338              (gnus-group-decoded-name group) (gnus-status-message group)))
5339
5340     (when gnus-agent
5341       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5342
5343       (setq gnus-summary-use-undownloaded-faces
5344             (gnus-agent-find-parameter
5345              group
5346              'agent-enable-undownloaded-faces)))
5347
5348     (setq gnus-newsgroup-name group
5349           gnus-newsgroup-unselected nil
5350           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5351
5352     (let ((display (gnus-group-find-parameter group 'display)))
5353       (setq gnus-newsgroup-display
5354             (cond
5355              ((not (zerop (or (car-safe read-all) 0)))
5356               ;; The user entered the group with C-u SPC/RET, let's show
5357               ;; all articles.
5358               'gnus-not-ignore)
5359              ((eq display 'all)
5360               'gnus-not-ignore)
5361              ((arrayp display)
5362               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5363              ((numberp display)
5364               ;; The following is probably the "correct" solution, but
5365               ;; it makes Gnus fetch all headers and then limit the
5366               ;; articles (which is slow), so instead we hack the
5367               ;; select-articles parameter instead. -- Simon Josefsson
5368               ;; <jas@kth.se>
5369               ;;
5370               ;; (gnus-byte-compile
5371               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5372               ;;                         display)))))
5373               (setq select-articles
5374                     (gnus-uncompress-range
5375                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5376                              (if (> tmp 0)
5377                                  tmp
5378                                1))
5379                            (cdr (gnus-active group)))))
5380               nil)
5381              (t
5382               nil))))
5383
5384     (gnus-summary-setup-default-charset)
5385
5386     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5387     (when (gnus-virtual-group-p group)
5388       (setq cached gnus-newsgroup-cached))
5389
5390     (setq gnus-newsgroup-unreads
5391           (gnus-sorted-ndifference
5392            (gnus-sorted-ndifference gnus-newsgroup-unreads
5393                                     gnus-newsgroup-marked)
5394            gnus-newsgroup-dormant))
5395
5396     (setq gnus-newsgroup-processable nil)
5397
5398     (gnus-update-read-articles group gnus-newsgroup-unreads)
5399
5400     ;; Adjust and set lists of article marks.
5401     (when info
5402       (gnus-adjust-marked-articles info))
5403     (if (setq articles select-articles)
5404         (setq gnus-newsgroup-unselected
5405               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5406       (setq articles (gnus-articles-to-read group read-all)))
5407
5408     (cond
5409      ((null articles)
5410       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5411       'quit)
5412      ((eq articles 0) nil)
5413      (t
5414       ;; Init the dependencies hash table.
5415       (setq gnus-newsgroup-dependencies
5416             (gnus-make-hashtable (length articles)))
5417       (gnus-set-global-variables)
5418       ;; Retrieve the headers and read them in.
5419
5420       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5421
5422       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5423       (when cached
5424         (setq gnus-newsgroup-cached cached))
5425
5426       ;; Suppress duplicates?
5427       (when gnus-suppress-duplicates
5428         (gnus-dup-suppress-articles))
5429
5430       ;; Set the initial limit.
5431       (setq gnus-newsgroup-limit (copy-sequence articles))
5432       ;; Remove canceled articles from the list of unread articles.
5433       (setq fetched-articles
5434             (mapcar (lambda (headers) (mail-header-number headers))
5435                     gnus-newsgroup-headers))
5436       (setq gnus-newsgroup-articles fetched-articles)
5437       (setq gnus-newsgroup-unreads
5438             (gnus-sorted-nintersection
5439              gnus-newsgroup-unreads fetched-articles))
5440       (gnus-compute-unseen-list)
5441
5442       ;; Removed marked articles that do not exist.
5443       (gnus-update-missing-marks
5444        (gnus-sorted-difference articles fetched-articles))
5445       ;; We might want to build some more threads first.
5446       (when (and gnus-fetch-old-headers
5447                  (eq gnus-headers-retrieved-by 'nov))
5448         (if (eq gnus-fetch-old-headers 'invisible)
5449             (gnus-build-all-threads)
5450           (gnus-build-old-threads)))
5451       ;; Let the Gnus agent mark articles as read.
5452       (when gnus-agent
5453         (gnus-agent-get-undownloaded-list))
5454       ;; Remove list identifiers from subject
5455       (when gnus-list-identifiers
5456         (gnus-summary-remove-list-identifiers))
5457       ;; Check whether auto-expire is to be done in this group.
5458       (setq gnus-newsgroup-auto-expire
5459             (gnus-group-auto-expirable-p group))
5460       ;; Set up the article buffer now, if necessary.
5461       (unless (and gnus-single-article-buffer
5462                    (equal gnus-article-buffer "*Article*"))
5463         (gnus-article-setup-buffer))
5464       ;; First and last article in this newsgroup.
5465       (when gnus-newsgroup-headers
5466         (setq gnus-newsgroup-begin
5467               (mail-header-number (car gnus-newsgroup-headers))
5468               gnus-newsgroup-end
5469               (mail-header-number
5470                (gnus-last-element gnus-newsgroup-headers))))
5471       ;; GROUP is successfully selected.
5472       (or gnus-newsgroup-headers t)))))
5473
5474 (defun gnus-compute-unseen-list ()
5475   ;; The `seen' marks are treated specially.
5476   (if (not gnus-newsgroup-seen)
5477       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5478     (setq gnus-newsgroup-unseen
5479           (gnus-inverse-list-range-intersection
5480            gnus-newsgroup-articles gnus-newsgroup-seen))))
5481
5482 (defun gnus-summary-display-make-predicate (display)
5483   (require 'gnus-agent)
5484   (when (= (length display) 1)
5485     (setq display (car display)))
5486   (unless gnus-summary-display-cache
5487     (dolist (elem (append '((unread . unread)
5488                             (read . read)
5489                             (unseen . unseen))
5490                           gnus-article-mark-lists))
5491       (push (cons (cdr elem)
5492                   (gnus-byte-compile
5493                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5494             gnus-summary-display-cache)))
5495   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5496         (gnus-category-predicate-cache gnus-summary-display-cache))
5497     (gnus-get-predicate display)))
5498
5499 ;; Uses the dynamically bound `number' variable.
5500 (eval-when-compile
5501   (defvar number))
5502 (defun gnus-article-marked-p (type &optional article)
5503   (let ((article (or article number)))
5504     (cond
5505      ((eq type 'tick)
5506       (memq article gnus-newsgroup-marked))
5507      ((eq type 'spam)
5508       (memq article gnus-newsgroup-spam-marked))
5509      ((eq type 'unsend)
5510       (memq article gnus-newsgroup-unsendable))
5511      ((eq type 'undownload)
5512       (memq article gnus-newsgroup-undownloaded))
5513      ((eq type 'download)
5514       (memq article gnus-newsgroup-downloadable))
5515      ((eq type 'unread)
5516       (memq article gnus-newsgroup-unreads))
5517      ((eq type 'read)
5518       (memq article gnus-newsgroup-reads))
5519      ((eq type 'dormant)
5520       (memq article gnus-newsgroup-dormant) )
5521      ((eq type 'expire)
5522       (memq article gnus-newsgroup-expirable))
5523      ((eq type 'reply)
5524       (memq article gnus-newsgroup-replied))
5525      ((eq type 'killed)
5526       (memq article gnus-newsgroup-killed))
5527      ((eq type 'bookmark)
5528       (assq article gnus-newsgroup-bookmarks))
5529      ((eq type 'score)
5530       (assq article gnus-newsgroup-scored))
5531      ((eq type 'save)
5532       (memq article gnus-newsgroup-saved))
5533      ((eq type 'cache)
5534       (memq article gnus-newsgroup-cached))
5535      ((eq type 'forward)
5536       (memq article gnus-newsgroup-forwarded))
5537      ((eq type 'seen)
5538       (not (memq article gnus-newsgroup-unseen)))
5539      ((eq type 'recent)
5540       (memq article gnus-newsgroup-recent))
5541      (t t))))
5542
5543 (defun gnus-articles-to-read (group &optional read-all)
5544   "Find out what articles the user wants to read."
5545   (let* ((articles
5546           ;; Select all articles if `read-all' is non-nil, or if there
5547           ;; are no unread articles.
5548           (if (or read-all
5549                   (and (zerop (length gnus-newsgroup-marked))
5550                        (zerop (length gnus-newsgroup-unreads)))
5551                   ;; Fetch all if the predicate is non-nil.
5552                   gnus-newsgroup-display)
5553               ;; We want to select the headers for all the articles in
5554               ;; the group, so we select either all the active
5555               ;; articles in the group, or (if that's nil), the
5556               ;; articles in the cache.
5557               (or
5558                (gnus-uncompress-range (gnus-active group))
5559                (gnus-cache-articles-in-group group))
5560             ;; Select only the "normal" subset of articles.
5561             (gnus-sorted-nunion
5562              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5563              gnus-newsgroup-unreads)))
5564          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5565          (scored (length scored-list))
5566          (number (length articles))
5567          (marked (+ (length gnus-newsgroup-marked)
5568                     (length gnus-newsgroup-dormant)))
5569          (select
5570           (cond
5571            ((numberp read-all)
5572             read-all)
5573            ((numberp gnus-newsgroup-display)
5574             gnus-newsgroup-display)
5575            (t
5576             (condition-case ()
5577                 (cond
5578                  ((and (or (<= scored marked) (= scored number))
5579                        (numberp gnus-large-newsgroup)
5580                        (> number gnus-large-newsgroup))
5581                   (let* ((cursor-in-echo-area nil)
5582                          (initial (gnus-parameter-large-newsgroup-initial
5583                                    gnus-newsgroup-name))
5584                          (input
5585                           (read-string
5586                            (format
5587                             "How many articles from %s (%s %d): "
5588                             (gnus-limit-string
5589                              (gnus-group-decoded-name gnus-newsgroup-name)
5590                              35)
5591                             (if initial "max" "default")
5592                             number)
5593                            (if initial
5594                                (cons (number-to-string initial)
5595                                      0)))))
5596                     (if (string-match "^[ \t]*$" input) number input)))
5597                  ((and (> scored marked) (< scored number)
5598                        (> (- scored number) 20))
5599                   (let ((input
5600                          (read-string
5601                           (format "%s %s (%d scored, %d total): "
5602                                   "How many articles from"
5603                                   (gnus-group-decoded-name group)
5604                                   scored number))))
5605                     (if (string-match "^[ \t]*$" input)
5606                         number input)))
5607                  (t number))
5608               (quit
5609                (message "Quit getting the articles to read")
5610                nil))))))
5611     (setq select (if (stringp select) (string-to-number select) select))
5612     (if (or (null select) (zerop select))
5613         select
5614       (if (and (not (zerop scored)) (<= (abs select) scored))
5615           (progn
5616             (setq articles (sort scored-list '<))
5617             (setq number (length articles)))
5618         (setq articles (copy-sequence articles)))
5619
5620       (when (< (abs select) number)
5621         (if (< select 0)
5622             ;; Select the N oldest articles.
5623             (setcdr (nthcdr (1- (abs select)) articles) nil)
5624           ;; Select the N most recent articles.
5625           (setq articles (nthcdr (- number select) articles))))
5626       (setq gnus-newsgroup-unselected
5627             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5628       (when gnus-alter-articles-to-read-function
5629         (setq articles
5630               (sort
5631                (funcall gnus-alter-articles-to-read-function
5632                         gnus-newsgroup-name articles)
5633                '<)))
5634       articles)))
5635
5636 (defun gnus-killed-articles (killed articles)
5637   (let (out)
5638     (while articles
5639       (when (inline (gnus-member-of-range (car articles) killed))
5640         (push (car articles) out))
5641       (setq articles (cdr articles)))
5642     out))
5643
5644 (defun gnus-uncompress-marks (marks)
5645   "Uncompress the mark ranges in MARKS."
5646   (let ((uncompressed '(score bookmark))
5647         out)
5648     (while marks
5649       (if (memq (caar marks) uncompressed)
5650           (push (car marks) out)
5651         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5652       (setq marks (cdr marks)))
5653     out))
5654
5655 (defun gnus-article-mark-to-type (mark)
5656   "Return the type of MARK."
5657   (or (cadr (assq mark gnus-article-special-mark-lists))
5658       'list))
5659
5660 (defun gnus-article-unpropagatable-p (mark)
5661   "Return whether MARK should be propagated to back end."
5662   (memq mark gnus-article-unpropagated-mark-lists))
5663
5664 (defun gnus-adjust-marked-articles (info)
5665   "Set all article lists and remove all marks that are no longer valid."
5666   (let* ((marked-lists (gnus-info-marks info))
5667          (active (gnus-active (gnus-info-group info)))
5668          (min (car active))
5669          (max (cdr active))
5670          (types gnus-article-mark-lists)
5671          marks var articles article mark mark-type
5672          bgn end)
5673
5674     (dolist (marks marked-lists)
5675       (setq mark (car marks)
5676             mark-type (gnus-article-mark-to-type mark)
5677             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5678
5679       ;; We set the variable according to the type of the marks list,
5680       ;; and then adjust the marks to a subset of the active articles.
5681       (cond
5682        ;; Adjust "simple" lists - compressed yet unsorted
5683        ((eq mark-type 'list)
5684         ;; Simultaneously uncompress and clip to active range
5685         ;; See gnus-uncompress-range for a description of possible marks
5686         (let (l lh)
5687           (if (not (cadr marks))
5688               (set var nil)
5689             (setq articles (if (numberp (cddr marks))
5690                                (list (cdr marks))
5691                              (cdr marks))
5692                   lh (cons nil nil)
5693                   l lh)
5694
5695             (while (setq article (pop articles))
5696               (cond ((consp article)
5697                      (setq bgn (max (car article) min)
5698                            end (min (cdr article) max))
5699                      (while (<= bgn end)
5700                        (setq l (setcdr l (cons bgn nil))
5701                              bgn (1+ bgn))))
5702                     ((and (<= min article)
5703                           (>= max article))
5704                      (setq l (setcdr l (cons article nil))))))
5705             (set var (cdr lh)))))
5706        ;; Adjust assocs.
5707        ((eq mark-type 'tuple)
5708         (set var (setq articles (cdr marks)))
5709         (when (not (listp (cdr (symbol-value var))))
5710           (set var (list (symbol-value var))))
5711         (when (not (listp (cdr articles)))
5712           (setq articles (list articles)))
5713         (while articles
5714           (when (or (not (consp (setq article (pop articles))))
5715                     (< (car article) min)
5716                     (> (car article) max))
5717             (set var (delq article (symbol-value var))))))
5718        ;; Adjust ranges (sloppily).
5719        ((eq mark-type 'range)
5720         (cond
5721          ((eq mark 'seen)
5722           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5723           ;; It should be (seen (NUM1 . NUM2)).
5724           (when (numberp (cddr marks))
5725             (setcdr marks (list (cdr marks))))
5726           (setq articles (cdr marks))
5727           (while (and articles
5728                       (or (and (consp (car articles))
5729                                (> min (cdar articles)))
5730                           (and (numberp (car articles))
5731                                (> min (car articles)))))
5732             (pop articles))
5733           (set var articles))))))))
5734
5735 (defun gnus-update-missing-marks (missing)
5736   "Go through the list of MISSING articles and remove them from the mark lists."
5737   (when missing
5738     (let (var m)
5739       ;; Go through all types.
5740       (dolist (elem gnus-article-mark-lists)
5741         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5742           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5743           (when (symbol-value var)
5744             ;; This list has articles.  So we delete all missing
5745             ;; articles from it.
5746             (setq m missing)
5747             (while m
5748               (set var (delq (pop m) (symbol-value var))))))))))
5749
5750 (defun gnus-update-marks ()
5751   "Enter the various lists of marked articles into the newsgroup info list."
5752   (let ((types gnus-article-mark-lists)
5753         (info (gnus-get-info gnus-newsgroup-name))
5754         type list newmarked symbol delta-marks)
5755     (when info
5756       ;; Add all marks lists to the list of marks lists.
5757       (while (setq type (pop types))
5758         (setq list (symbol-value
5759                     (setq symbol
5760                           (intern (format "gnus-newsgroup-%s" (car type))))))
5761
5762         (when list
5763           ;; Get rid of the entries of the articles that have the
5764           ;; default score.
5765           (when (and (eq (cdr type) 'score)
5766                      gnus-save-score
5767                      list)
5768             (let* ((arts list)
5769                    (prev (cons nil list))
5770                    (all prev))
5771               (while arts
5772                 (if (or (not (consp (car arts)))
5773                         (= (cdar arts) gnus-summary-default-score))
5774                     (setcdr prev (cdr arts))
5775                   (setq prev arts))
5776                 (setq arts (cdr arts)))
5777               (setq list (cdr all)))))
5778
5779         (when (eq (cdr type) 'seen)
5780           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5781
5782         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5783           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5784
5785         (when (and (gnus-check-backend-function
5786                     'request-set-mark gnus-newsgroup-name)
5787                    (not (gnus-article-unpropagatable-p (cdr type))))
5788           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5789                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5790                  (add (gnus-remove-from-range
5791                        (gnus-copy-sequence list) old)))
5792             (when add
5793               (push (list add 'add (list (cdr type))) delta-marks))
5794             (when del
5795               (push (list del 'del (list (cdr type))) delta-marks))))
5796
5797         (when list
5798           (push (cons (cdr type) list) newmarked)))
5799
5800       (when delta-marks
5801         (unless (gnus-check-group gnus-newsgroup-name)
5802           (error "Can't open server for %s" gnus-newsgroup-name))
5803         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5804
5805       ;; Enter these new marks into the info of the group.
5806       (if (nthcdr 3 info)
5807           (setcar (nthcdr 3 info) newmarked)
5808         ;; Add the marks lists to the end of the info.
5809         (when newmarked
5810           (setcdr (nthcdr 2 info) (list newmarked))))
5811
5812       ;; Cut off the end of the info if there's nothing else there.
5813       (let ((i 5))
5814         (while (and (> i 2)
5815                     (not (nth i info)))
5816           (when (nthcdr (decf i) info)
5817             (setcdr (nthcdr i info) nil)))))))
5818
5819 (defun gnus-set-mode-line (where)
5820   "Set the mode line of the article or summary buffers.
5821 If WHERE is `summary', the summary mode line format will be used."
5822   ;; Is this mode line one we keep updated?
5823   (when (and (memq where gnus-updated-mode-lines)
5824              (symbol-value
5825               (intern (format "gnus-%s-mode-line-format-spec" where))))
5826     (let (mode-string)
5827       (save-excursion
5828         ;; We evaluate this in the summary buffer since these
5829         ;; variables are buffer-local to that buffer.
5830         (set-buffer gnus-summary-buffer)
5831        ;; We bind all these variables that are used in the `eval' form
5832         ;; below.
5833         (let* ((mformat (symbol-value
5834                          (intern
5835                           (format "gnus-%s-mode-line-format-spec" where))))
5836                (gnus-tmp-group-name (gnus-mode-string-quote
5837                                      (gnus-group-decoded-name
5838                                       gnus-newsgroup-name)))
5839                (gnus-tmp-article-number (or gnus-current-article 0))
5840                (gnus-tmp-unread gnus-newsgroup-unreads)
5841                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5842                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5843                (gnus-tmp-unread-and-unselected
5844                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5845                             (zerop gnus-tmp-unselected))
5846                        "")
5847                       ((zerop gnus-tmp-unselected)
5848                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5849                       (t (format "{%d(+%d) more}"
5850                                  gnus-tmp-unread-and-unticked
5851                                  gnus-tmp-unselected))))
5852                (gnus-tmp-subject
5853                 (if (and gnus-current-headers
5854                          (vectorp gnus-current-headers))
5855                     (gnus-mode-string-quote
5856                      (mail-header-subject gnus-current-headers))
5857                   ""))
5858                bufname-length max-len
5859                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5860           (setq mode-string (eval mformat))
5861           (setq bufname-length (if (string-match "%b" mode-string)
5862                                    (- (length
5863                                        (buffer-name
5864                                         (if (eq where 'summary)
5865                                             nil
5866                                           (get-buffer gnus-article-buffer))))
5867                                       2)
5868                                  0))
5869           (setq max-len (max 4 (if gnus-mode-non-string-length
5870                                    (- (window-width)
5871                                       gnus-mode-non-string-length
5872                                       bufname-length)
5873                                  (length mode-string))))
5874           ;; We might have to chop a bit of the string off...
5875           (when (> (length mode-string) max-len)
5876             (setq mode-string
5877                   (concat (truncate-string-to-width mode-string (- max-len 3))
5878                           "...")))
5879           ;; Pad the mode string a bit.
5880           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5881       ;; Update the mode line.
5882       (setq mode-line-buffer-identification
5883             (gnus-mode-line-buffer-identification (list mode-string)))
5884       (set-buffer-modified-p t))))
5885
5886 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5887   "Go through the HEADERS list and add all Xrefs to a hash table.
5888 The resulting hash table is returned, or nil if no Xrefs were found."
5889   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5890          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5891          (xref-hashtb (gnus-make-hashtable))
5892          start group entry number xrefs header)
5893     (while headers
5894       (setq header (pop headers))
5895       (when (and (setq xrefs (mail-header-xref header))
5896                  (not (memq (setq number (mail-header-number header))
5897                             unreads)))
5898         (setq start 0)
5899         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5900           (setq start (match-end 0))
5901           (setq group (if prefix
5902                           (concat prefix (substring xrefs (match-beginning 1)
5903                                                     (match-end 1)))
5904                         (substring xrefs (match-beginning 1) (match-end 1))))
5905           (setq number
5906                 (string-to-number (substring xrefs (match-beginning 2)
5907                                           (match-end 2))))
5908           (if (setq entry (gnus-gethash group xref-hashtb))
5909               (setcdr entry (cons number (cdr entry)))
5910             (gnus-sethash group (cons number nil) xref-hashtb)))))
5911     (and start xref-hashtb)))
5912
5913 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5914   "Look through all the headers and mark the Xrefs as read."
5915   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5916         name info xref-hashtb idlist method nth4)
5917     (save-excursion
5918       (set-buffer gnus-group-buffer)
5919       (when (setq xref-hashtb
5920                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5921         (mapatoms
5922          (lambda (group)
5923            (unless (string= from-newsgroup (setq name (symbol-name group)))
5924              (setq idlist (symbol-value group))
5925              ;; Dead groups are not updated.
5926              (and (prog1
5927                       (setq info (gnus-get-info name))
5928                     (when (stringp (setq nth4 (gnus-info-method info)))
5929                       (setq nth4 (gnus-server-to-method nth4))))
5930                   ;; Only do the xrefs if the group has the same
5931                   ;; select method as the group we have just read.
5932                   (or (gnus-methods-equal-p
5933                        nth4 (gnus-find-method-for-group from-newsgroup))
5934                       virtual
5935                       (equal nth4 (setq method (gnus-find-method-for-group
5936                                                 from-newsgroup)))
5937                       (and (equal (car nth4) (car method))
5938                            (equal (nth 1 nth4) (nth 1 method))))
5939                   gnus-use-cross-reference
5940                   (or (not (eq gnus-use-cross-reference t))
5941                       virtual
5942                       ;; Only do cross-references on subscribed
5943                       ;; groups, if that is what is wanted.
5944                       (<= (gnus-info-level info) gnus-level-subscribed))
5945                   (gnus-group-make-articles-read name idlist))))
5946          xref-hashtb)))))
5947
5948 (defun gnus-compute-read-articles (group articles)
5949   (let* ((entry (gnus-group-entry group))
5950          (info (nth 2 entry))
5951          (active (gnus-active group))
5952          ninfo)
5953     (when entry
5954       ;; First peel off all invalid article numbers.
5955       (when active
5956         (let ((ids articles)
5957               id first)
5958           (while (setq id (pop ids))
5959             (when (and first (> id (cdr active)))
5960               ;; We'll end up in this situation in one particular
5961               ;; obscure situation.  If you re-scan a group and get
5962               ;; a new article that is cross-posted to a different
5963               ;; group that has not been re-scanned, you might get
5964               ;; crossposted article that has a higher number than
5965               ;; Gnus believes possible.  So we re-activate this
5966               ;; group as well.  This might mean doing the
5967               ;; crossposting thingy will *increase* the number
5968               ;; of articles in some groups.  Tsk, tsk.
5969               (setq active (or (gnus-activate-group group) active)))
5970             (when (or (> id (cdr active))
5971                       (< id (car active)))
5972               (setq articles (delq id articles))))))
5973       ;; If the read list is nil, we init it.
5974       (if (and active
5975                (null (gnus-info-read info))
5976                (> (car active) 1))
5977           (setq ninfo (cons 1 (1- (car active))))
5978         (setq ninfo (gnus-info-read info)))
5979       ;; Then we add the read articles to the range.
5980       (gnus-add-to-range
5981        ninfo (setq articles (sort articles '<))))))
5982
5983 (defun gnus-group-make-articles-read (group articles)
5984   "Update the info of GROUP to say that ARTICLES are read."
5985   (let* ((num 0)
5986          (entry (gnus-group-entry group))
5987          (info (nth 2 entry))
5988          (active (gnus-active group))
5989          range)
5990     (when entry
5991       (setq range (gnus-compute-read-articles group articles))
5992       (with-current-buffer gnus-group-buffer
5993         (gnus-undo-register
5994           `(progn
5995              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5996              (gnus-info-set-read ',info ',(gnus-info-read info))
5997              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5998              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5999              (gnus-group-update-group ,group t))))
6000       ;; Add the read articles to the range.
6001       (gnus-info-set-read info range)
6002       (gnus-request-set-mark group (list (list range 'add '(read))))
6003       ;; Then we have to re-compute how many unread
6004       ;; articles there are in this group.
6005       (when active
6006         (cond
6007          ((not range)
6008           (setq num (- (1+ (cdr active)) (car active))))
6009          ((not (listp (cdr range)))
6010           (setq num (- (cdr active) (- (1+ (cdr range))
6011                                        (car range)))))
6012          (t
6013           (while range
6014             (if (numberp (car range))
6015                 (setq num (1+ num))
6016               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6017             (setq range (cdr range)))
6018           (setq num (- (cdr active) num))))
6019         ;; Update the number of unread articles.
6020         (setcar entry num)
6021         ;; Update the group buffer.
6022         (unless (gnus-ephemeral-group-p group)
6023           (gnus-group-update-group group t))))))
6024
6025 (defvar gnus-newsgroup-none-id 0)
6026
6027 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6028   (let ((cur nntp-server-buffer)
6029         (dependencies
6030          (or dependencies
6031              (with-current-buffer gnus-summary-buffer
6032                gnus-newsgroup-dependencies)))
6033         headers id end ref number
6034         (mail-parse-charset gnus-newsgroup-charset)
6035         (mail-parse-ignored-charsets
6036          (save-excursion (condition-case nil
6037                              (set-buffer gnus-summary-buffer)
6038                            (error))
6039                          gnus-newsgroup-ignored-charsets)))
6040     (save-excursion
6041       (set-buffer nntp-server-buffer)
6042       ;; Translate all TAB characters into SPACE characters.
6043       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6044       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6045       (ietf-drums-unfold-fws)
6046       (gnus-run-hooks 'gnus-parse-headers-hook)
6047       (let ((case-fold-search t)
6048             in-reply-to header p lines chars)
6049         (goto-char (point-min))
6050         ;; Search to the beginning of the next header.  Error messages
6051         ;; do not begin with 2 or 3.
6052         (while (re-search-forward "^[23][0-9]+ " nil t)
6053           (setq id nil
6054                 ref nil)
6055           ;; This implementation of this function, with nine
6056           ;; search-forwards instead of the one re-search-forward and
6057           ;; a case (which basically was the old function) is actually
6058           ;; about twice as fast, even though it looks messier.  You
6059           ;; can't have everything, I guess.  Speed and elegance
6060           ;; doesn't always go hand in hand.
6061           (setq
6062            header
6063            (vector
6064             ;; Number.
6065             (prog1
6066                 (setq number (read cur))
6067               (end-of-line)
6068               (setq p (point))
6069               (narrow-to-region (point)
6070                                 (or (and (search-forward "\n.\n" nil t)
6071                                          (- (point) 2))
6072                                     (point))))
6073             ;; Subject.
6074             (progn
6075               (goto-char p)
6076               (if (search-forward "\nsubject:" nil t)
6077                   (funcall gnus-decode-encoded-word-function
6078                            (nnheader-header-value))
6079                 "(none)"))
6080             ;; From.
6081             (progn
6082               (goto-char p)
6083               (if (search-forward "\nfrom:" nil t)
6084                   (funcall gnus-decode-encoded-address-function
6085                            (nnheader-header-value))
6086                 "(nobody)"))
6087             ;; Date.
6088             (progn
6089               (goto-char p)
6090               (if (search-forward "\ndate:" nil t)
6091                   (nnheader-header-value) ""))
6092             ;; Message-ID.
6093             (progn
6094               (goto-char p)
6095               (setq id (if (re-search-forward
6096                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6097                            ;; We do it this way to make sure the Message-ID
6098                            ;; is (somewhat) syntactically valid.
6099                            (buffer-substring (match-beginning 1)
6100                                              (match-end 1))
6101                          ;; If there was no message-id, we just fake one
6102                          ;; to make subsequent routines simpler.
6103                          (nnheader-generate-fake-message-id number))))
6104             ;; References.
6105             (progn
6106               (goto-char p)
6107               (if (search-forward "\nreferences:" nil t)
6108                   (progn
6109                     (setq end (point))
6110                     (prog1
6111                         (nnheader-header-value)
6112                       (setq ref
6113                             (buffer-substring
6114                              (progn
6115                                (end-of-line)
6116                                (search-backward ">" end t)
6117                                (1+ (point)))
6118                              (progn
6119                                (search-backward "<" end t)
6120                                (point))))))
6121                 ;; Get the references from the in-reply-to header if there
6122                 ;; were no references and the in-reply-to header looks
6123                 ;; promising.
6124                 (if (and (search-forward "\nin-reply-to:" nil t)
6125                          (setq in-reply-to (nnheader-header-value))
6126                          (string-match "<[^>]+>" in-reply-to))
6127                     (let (ref2)
6128                       (setq ref (substring in-reply-to (match-beginning 0)
6129                                            (match-end 0)))
6130                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6131                         (setq ref2 (substring in-reply-to (match-beginning 0)
6132                                               (match-end 0)))
6133                         (when (> (length ref2) (length ref))
6134                           (setq ref ref2)))
6135                       ref)
6136                   (setq ref nil))))
6137             ;; Chars.
6138             (progn
6139               (goto-char p)
6140               (if (search-forward "\nchars: " nil t)
6141                   (if (numberp (setq chars (ignore-errors (read cur))))
6142                       chars -1)
6143                 -1))
6144             ;; Lines.
6145             (progn
6146               (goto-char p)
6147               (if (search-forward "\nlines: " nil t)
6148                   (if (numberp (setq lines (ignore-errors (read cur))))
6149                       lines -1)
6150                 -1))
6151             ;; Xref.
6152             (progn
6153               (goto-char p)
6154               (and (search-forward "\nxref:" nil t)
6155                    (nnheader-header-value)))
6156             ;; Extra.
6157             (when gnus-extra-headers
6158               (let ((extra gnus-extra-headers)
6159                     out)
6160                 (while extra
6161                   (goto-char p)
6162                   (when (search-forward
6163                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6164                     (push (cons (car extra) (nnheader-header-value))
6165                           out))
6166                   (pop extra))
6167                 out))))
6168           (when (equal id ref)
6169             (setq ref nil))
6170
6171           (when gnus-alter-header-function
6172             (funcall gnus-alter-header-function header)
6173             (setq id (mail-header-id header)
6174                   ref (gnus-parent-id (mail-header-references header))))
6175
6176           (when (setq header
6177                       (gnus-dependencies-add-header
6178                        header dependencies force-new))
6179             (push header headers))
6180           (goto-char (point-max))
6181           (widen))
6182         (nreverse headers)))))
6183
6184 ;; Goes through the xover lines and returns a list of vectors
6185 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6186                                                   force-new dependencies
6187                                                   group also-fetch-heads)
6188   "Parse the news overview data in the server buffer.
6189 Return a list of headers that match SEQUENCE (see
6190 `nntp-retrieve-headers')."
6191   ;; Get the Xref when the users reads the articles since most/some
6192   ;; NNTP servers do not include Xrefs when using XOVER.
6193   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6194   (let ((mail-parse-charset gnus-newsgroup-charset)
6195         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6196         (cur nntp-server-buffer)
6197         (dependencies (or dependencies gnus-newsgroup-dependencies))
6198         (allp (cond
6199                ((eq gnus-read-all-available-headers t)
6200                 t)
6201                ((and (stringp gnus-read-all-available-headers)
6202                      group)
6203                 (string-match gnus-read-all-available-headers group))
6204                (t
6205                 nil)))
6206         number headers header)
6207     (save-excursion
6208       (set-buffer nntp-server-buffer)
6209       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6210       ;; Allow the user to mangle the headers before parsing them.
6211       (gnus-run-hooks 'gnus-parse-headers-hook)
6212       (goto-char (point-min))
6213       (gnus-parse-without-error
6214         (while (and (or sequence allp)
6215                     (not (eobp)))
6216           (setq number (read cur))
6217           (when (not allp)
6218             (while (and sequence
6219                         (< (car sequence) number))
6220               (setq sequence (cdr sequence))))
6221           (when (and (or allp
6222                          (and sequence
6223                               (eq number (car sequence))))
6224                      (progn
6225                        (setq sequence (cdr sequence))
6226                        (setq header (inline
6227                                       (gnus-nov-parse-line
6228                                        number dependencies force-new)))))
6229             (push header headers))
6230           (forward-line 1)))
6231       ;; A common bug in inn is that if you have posted an article and
6232       ;; then retrieves the active file, it will answer correctly --
6233       ;; the new article is included.  However, a NOV entry for the
6234       ;; article may not have been generated yet, so this may fail.
6235       ;; We work around this problem by retrieving the last few
6236       ;; headers using HEAD.
6237       (if (or (not also-fetch-heads)
6238               (not sequence))
6239           ;; We (probably) got all the headers.
6240           (nreverse headers)
6241         (let ((gnus-nov-is-evil t))
6242           (nconc
6243            (nreverse headers)
6244            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6245              (gnus-get-newsgroup-headers))))))))
6246
6247 (defun gnus-article-get-xrefs ()
6248   "Fill in the Xref value in `gnus-current-headers', if necessary.
6249 This is meant to be called in `gnus-article-internal-prepare-hook'."
6250   (let ((headers (with-current-buffer gnus-summary-buffer
6251                    gnus-current-headers)))
6252     (or (not gnus-use-cross-reference)
6253         (not headers)
6254         (and (mail-header-xref headers)
6255              (not (string= (mail-header-xref headers) "")))
6256         (let ((case-fold-search t)
6257               xref)
6258           (save-restriction
6259             (nnheader-narrow-to-headers)
6260             (goto-char (point-min))
6261             (when (or (and (not (eobp))
6262                            (eq (downcase (char-after)) ?x)
6263                            (looking-at "Xref:"))
6264                       (search-forward "\nXref:" nil t))
6265               (goto-char (1+ (match-end 0)))
6266               (setq xref (buffer-substring (point) (point-at-eol)))
6267               (mail-header-set-xref headers xref)))))))
6268
6269 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6270   "Find article ID and insert the summary line for that article.
6271 OLD-HEADER can either be a header or a line number to insert
6272 the subject line on."
6273   (let* ((line (and (numberp old-header) old-header))
6274          (old-header (and (vectorp old-header) old-header))
6275          (header (cond ((and old-header use-old-header)
6276                         old-header)
6277                        ((and (numberp id)
6278                              (gnus-number-to-header id))
6279                         (gnus-number-to-header id))
6280                        (t
6281                         (gnus-read-header id))))
6282          (number (and (numberp id) id))
6283          d)
6284     (when header
6285       ;; Rebuild the thread that this article is part of and go to the
6286       ;; article we have fetched.
6287       (when (and (not gnus-show-threads)
6288                  old-header)
6289         (when (and number
6290                    (setq d (gnus-data-find (mail-header-number old-header))))
6291           (goto-char (gnus-data-pos d))
6292           (gnus-data-remove
6293            number
6294            (- (point-at-bol)
6295               (prog1
6296                   (1+ (point-at-eol))
6297                 (gnus-delete-line))))))
6298       ;; Remove list identifiers from subject.
6299       (when gnus-list-identifiers
6300         (let ((gnus-newsgroup-headers (list header)))
6301           (gnus-summary-remove-list-identifiers)))
6302       (when old-header
6303         (mail-header-set-number header (mail-header-number old-header)))
6304       (setq gnus-newsgroup-sparse
6305             (delq (setq number (mail-header-number header))
6306                   gnus-newsgroup-sparse))
6307       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6308       (push number gnus-newsgroup-limit)
6309       (gnus-rebuild-thread (mail-header-id header) line)
6310       (gnus-summary-goto-subject number nil t))
6311     (when (and (numberp number)
6312                (> number 0))
6313       ;; We have to update the boundaries even if we can't fetch the
6314       ;; article if ID is a number -- so that the next `P' or `N'
6315       ;; command will fetch the previous (or next) article even
6316       ;; if the one we tried to fetch this time has been canceled.
6317       (when (> number gnus-newsgroup-end)
6318         (setq gnus-newsgroup-end number))
6319       (when (< number gnus-newsgroup-begin)
6320         (setq gnus-newsgroup-begin number))
6321       (setq gnus-newsgroup-unselected
6322             (delq number gnus-newsgroup-unselected)))
6323     ;; Report back a success?
6324     (and header (mail-header-number header))))
6325
6326 ;;; Process/prefix in the summary buffer
6327
6328 (defun gnus-summary-work-articles (n)
6329   "Return a list of articles to be worked upon.
6330 The prefix argument, the list of process marked articles, and the
6331 current article will be taken into consideration."
6332   (save-excursion
6333     (set-buffer gnus-summary-buffer)
6334     (cond
6335      (n
6336       ;; A numerical prefix has been given.
6337       (setq n (prefix-numeric-value n))
6338       (let ((backward (< n 0))
6339             (n (abs (prefix-numeric-value n)))
6340             articles article)
6341         (save-excursion
6342           (while
6343               (and (> n 0)
6344                    (push (setq article (gnus-summary-article-number))
6345                          articles)
6346                    (if backward
6347                        (gnus-summary-find-prev nil article)
6348                      (gnus-summary-find-next nil article)))
6349             (decf n)))
6350         (nreverse articles)))
6351      ((and (gnus-region-active-p) (mark))
6352       (message "region active")
6353       ;; Work on the region between point and mark.
6354       (let ((max (max (point) (mark)))
6355             articles article)
6356         (save-excursion
6357           (goto-char (min (point) (mark)))
6358           (while
6359               (and
6360                (push (setq article (gnus-summary-article-number)) articles)
6361                (gnus-summary-find-next nil article)
6362                (< (point) max)))
6363           (nreverse articles))))
6364      (gnus-newsgroup-processable
6365       ;; There are process-marked articles present.
6366       ;; Save current state.
6367       (gnus-summary-save-process-mark)
6368       ;; Return the list.
6369       (reverse gnus-newsgroup-processable))
6370      (t
6371       ;; Just return the current article.
6372       (list (gnus-summary-article-number))))))
6373
6374 (defmacro gnus-summary-iterate (arg &rest forms)
6375   "Iterate over the process/prefixed articles and do FORMS.
6376 ARG is the interactive prefix given to the command.  FORMS will be
6377 executed with point over the summary line of the articles."
6378   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6379     `(let ((,articles (gnus-summary-work-articles ,arg)))
6380        (while ,articles
6381          (gnus-summary-goto-subject (car ,articles))
6382          ,@forms
6383          (pop ,articles)))))
6384
6385 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6386 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6387
6388 (defun gnus-summary-save-process-mark ()
6389   "Push the current set of process marked articles on the stack."
6390   (interactive)
6391   (push (copy-sequence gnus-newsgroup-processable)
6392         gnus-newsgroup-process-stack))
6393
6394 (defun gnus-summary-kill-process-mark ()
6395   "Push the current set of process marked articles on the stack and unmark."
6396   (interactive)
6397   (gnus-summary-save-process-mark)
6398   (gnus-summary-unmark-all-processable))
6399
6400 (defun gnus-summary-yank-process-mark ()
6401   "Pop the last process mark state off the stack and restore it."
6402   (interactive)
6403   (unless gnus-newsgroup-process-stack
6404     (error "Empty mark stack"))
6405   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6406
6407 (defun gnus-summary-process-mark-set (set)
6408   "Make SET into the current process marked articles."
6409   (gnus-summary-unmark-all-processable)
6410   (mapc 'gnus-summary-set-process-mark set))
6411
6412 ;;; Searching and stuff
6413
6414 (defun gnus-summary-search-group (&optional backward use-level)
6415   "Search for next unread newsgroup.
6416 If optional argument BACKWARD is non-nil, search backward instead."
6417   (save-excursion
6418     (set-buffer gnus-group-buffer)
6419     (when (gnus-group-search-forward
6420            backward nil (if use-level (gnus-group-group-level) nil))
6421       (gnus-group-group-name))))
6422
6423 (defun gnus-summary-best-group (&optional exclude-group)
6424   "Find the name of the best unread group.
6425 If EXCLUDE-GROUP, do not go to this group."
6426   (with-current-buffer gnus-group-buffer
6427     (save-excursion
6428       (gnus-group-best-unread-group exclude-group))))
6429
6430 (defun gnus-summary-find-next (&optional unread article backward)
6431   (if backward
6432       (gnus-summary-find-prev unread article)
6433     (let* ((dummy (gnus-summary-article-intangible-p))
6434            (article (or article (gnus-summary-article-number)))
6435            (data (gnus-data-find-list article))
6436            result)
6437       (when (and (not dummy)
6438                  (or (not gnus-summary-check-current)
6439                      (not unread)
6440                      (not (gnus-data-unread-p (car data)))))
6441         (setq data (cdr data)))
6442       (when (setq result
6443                   (if unread
6444                       (progn
6445                         (while data
6446                           (unless (memq (gnus-data-number (car data))
6447                                         (cond
6448                                          ((eq gnus-auto-goto-ignores
6449                                               'always-undownloaded)
6450                                           gnus-newsgroup-undownloaded)
6451                                          (gnus-plugged
6452                                           nil)
6453                                          ((eq gnus-auto-goto-ignores
6454                                               'unfetched)
6455                                           gnus-newsgroup-unfetched)
6456                                          ((eq gnus-auto-goto-ignores
6457                                               'undownloaded)
6458                                           gnus-newsgroup-undownloaded)))
6459                             (when (gnus-data-unread-p (car data))
6460                               (setq result (car data)
6461                                     data nil)))
6462                           (setq data (cdr data)))
6463                         result)
6464                     (car data)))
6465         (goto-char (gnus-data-pos result))
6466         (gnus-data-number result)))))
6467
6468 (defun gnus-summary-find-prev (&optional unread article)
6469   (let* ((eobp (eobp))
6470          (article (or article (gnus-summary-article-number)))
6471          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6472          result)
6473     (when (and (not eobp)
6474                (or (not gnus-summary-check-current)
6475                    (not unread)
6476                    (not (gnus-data-unread-p (car data)))))
6477       (setq data (cdr data)))
6478     (when (setq result
6479                 (if unread
6480                     (progn
6481                       (while data
6482                         (unless (memq (gnus-data-number (car data))
6483                                       (cond
6484                                        ((eq gnus-auto-goto-ignores
6485                                             'always-undownloaded)
6486                                         gnus-newsgroup-undownloaded)
6487                                        (gnus-plugged
6488                                         nil)
6489                                        ((eq gnus-auto-goto-ignores
6490                                             'unfetched)
6491                                         gnus-newsgroup-unfetched)
6492                                        ((eq gnus-auto-goto-ignores
6493                                             'undownloaded)
6494                                         gnus-newsgroup-undownloaded)))
6495                           (when (gnus-data-unread-p (car data))
6496                             (setq result (car data)
6497                                   data nil)))
6498                         (setq data (cdr data)))
6499                       result)
6500                   (car data)))
6501       (goto-char (gnus-data-pos result))
6502       (gnus-data-number result))))
6503
6504 (defun gnus-summary-find-subject (subject &optional unread backward article)
6505   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6506          (article (or article (gnus-summary-article-number)))
6507          (articles (gnus-data-list backward))
6508          (arts (gnus-data-find-list article articles))
6509          result)
6510     (when (or (not gnus-summary-check-current)
6511               (not unread)
6512               (not (gnus-data-unread-p (car arts))))
6513       (setq arts (cdr arts)))
6514     (while arts
6515       (and (or (not unread)
6516                (gnus-data-unread-p (car arts)))
6517            (vectorp (gnus-data-header (car arts)))
6518            (gnus-subject-equal
6519             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6520            (setq result (car arts)
6521                  arts nil))
6522       (setq arts (cdr arts)))
6523     (and result
6524          (goto-char (gnus-data-pos result))
6525          (gnus-data-number result))))
6526
6527 (defun gnus-summary-search-forward (&optional unread subject backward)
6528   "Search forward for an article.
6529 If UNREAD, look for unread articles.  If SUBJECT, look for
6530 articles with that subject.  If BACKWARD, search backward instead."
6531   (cond (subject (gnus-summary-find-subject subject unread backward))
6532         (backward (gnus-summary-find-prev unread))
6533         (t (gnus-summary-find-next unread))))
6534
6535 (defun gnus-recenter (&optional n)
6536   "Center point in window and redisplay frame.
6537 Also do horizontal recentering."
6538   (interactive "P")
6539   (when (and gnus-auto-center-summary
6540              (not (eq gnus-auto-center-summary 'vertical)))
6541     (gnus-horizontal-recenter))
6542   (recenter n))
6543
6544 (defun gnus-summary-recenter ()
6545   "Center point in the summary window.
6546 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6547 displayed, no centering will be performed."
6548   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6549   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6550   (interactive)
6551   ;; The user has to want it.
6552   (when gnus-auto-center-summary
6553     (let* ((top (cond ((< (window-height) 4) 0)
6554                       ((< (window-height) 7) 1)
6555                       (t (if (numberp gnus-auto-center-summary)
6556                              gnus-auto-center-summary
6557                            (/ (1- (window-height)) 2)))))
6558            (height (1- (window-height)))
6559            (bottom (save-excursion (goto-char (point-max))
6560                                    (forward-line (- height))
6561                                    (point)))
6562            (window (get-buffer-window (current-buffer))))
6563       (when (get-buffer-window gnus-article-buffer)
6564         ;; Only do recentering when the article buffer is displayed,
6565         ;; Set the window start to either `bottom', which is the biggest
6566         ;; possible valid number, or the second line from the top,
6567         ;; whichever is the least.
6568         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6569           (if (> bottom top-pos)
6570               ;; Keep the second line from the top visible
6571               (set-window-start window top-pos t)
6572             ;; Try to keep the bottom line visible; if it's partially
6573             ;; obscured, either scroll one more line to make it fully
6574             ;; visible, or revert to using TOP-POS.
6575             (save-excursion
6576               (goto-char (point-max))
6577               (forward-line -1)
6578               (let ((last-line-start (point)))
6579                 (goto-char bottom)
6580                 (set-window-start window (point) t)
6581                 (when (not (pos-visible-in-window-p last-line-start window))
6582                   (forward-line 1)
6583                   (set-window-start window (min (point) top-pos) t)))))))
6584       ;; Do horizontal recentering while we're at it.
6585       (when (and (get-buffer-window (current-buffer) t)
6586                  (not (eq gnus-auto-center-summary 'vertical)))
6587         (let ((selected (selected-window)))
6588           (select-window (get-buffer-window (current-buffer) t))
6589           (gnus-summary-position-point)
6590           (gnus-horizontal-recenter)
6591           (select-window selected))))))
6592
6593 (defun gnus-summary-jump-to-group (newsgroup)
6594   "Move point to NEWSGROUP in group mode buffer."
6595   ;; Keep update point of group mode buffer if visible.
6596   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6597       (save-window-excursion
6598         ;; Take care of tree window mode.
6599         (when (get-buffer-window gnus-group-buffer)
6600           (pop-to-buffer gnus-group-buffer))
6601         (gnus-group-jump-to-group newsgroup))
6602     (save-excursion
6603       ;; Take care of tree window mode.
6604       (if (get-buffer-window gnus-group-buffer)
6605           (pop-to-buffer gnus-group-buffer)
6606         (set-buffer gnus-group-buffer))
6607       (gnus-group-jump-to-group newsgroup))))
6608
6609 ;; This function returns a list of article numbers based on the
6610 ;; difference between the ranges of read articles in this group and
6611 ;; the range of active articles.
6612 (defun gnus-list-of-unread-articles (group)
6613   (let* ((read (gnus-info-read (gnus-get-info group)))
6614          (active (or (gnus-active group) (gnus-activate-group group)))
6615          (last (or (cdr active)
6616                    (error "Group %s couldn't be activated " group)))
6617          first nlast unread)
6618     ;; If none are read, then all are unread.
6619     (if (not read)
6620         (setq first (car active))
6621       ;; If the range of read articles is a single range, then the
6622       ;; first unread article is the article after the last read
6623       ;; article.  Sounds logical, doesn't it?
6624       (if (and (not (listp (cdr read)))
6625                (or (< (car read) (car active))
6626                    (progn (setq read (list read))
6627                           nil)))
6628           (setq first (max (car active) (1+ (cdr read))))
6629         ;; `read' is a list of ranges.
6630         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6631                                   (caar read)))
6632                   1)
6633           (setq first (car active)))
6634         (while read
6635           (when first
6636             (while (< first nlast)
6637               (setq unread (cons first unread)
6638                     first (1+ first))))
6639           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6640           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6641           (setq read (cdr read)))))
6642     ;; And add the last unread articles.
6643     (while (<= first last)
6644       (setq unread (cons first unread)
6645             first (1+ first)))
6646     ;; Return the list of unread articles.
6647     (delq 0 (nreverse unread))))
6648
6649 (defun gnus-list-of-read-articles (group)
6650   "Return a list of unread, unticked and non-dormant articles."
6651   (let* ((info (gnus-get-info group))
6652          (marked (gnus-info-marks info))
6653          (active (gnus-active group)))
6654     (and info active
6655          (gnus-list-range-difference
6656           (gnus-list-range-difference
6657            (gnus-sorted-complement
6658             (gnus-uncompress-range active)
6659             (gnus-list-of-unread-articles group))
6660            (cdr (assq 'dormant marked)))
6661           (cdr (assq 'tick marked))))))
6662
6663 ;; This function returns a sequence of article numbers based on the
6664 ;; difference between the ranges of read articles in this group and
6665 ;; the range of active articles.
6666 (defun gnus-sequence-of-unread-articles (group)
6667   (let* ((read (gnus-info-read (gnus-get-info group)))
6668          (active (or (gnus-active group) (gnus-activate-group group)))
6669          (last (cdr active))
6670          first nlast unread)
6671     ;; If none are read, then all are unread.
6672     (if (not read)
6673         (setq first (car active))
6674       ;; If the range of read articles is a single range, then the
6675       ;; first unread article is the article after the last read
6676       ;; article.  Sounds logical, doesn't it?
6677       (if (and (not (listp (cdr read)))
6678                (or (< (car read) (car active))
6679                    (progn (setq read (list read))
6680                           nil)))
6681           (setq first (max (car active) (1+ (cdr read))))
6682         ;; `read' is a list of ranges.
6683         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6684                                   (caar read)))
6685                   1)
6686           (setq first (car active)))
6687         (while read
6688           (when first
6689             (push (cons first nlast) unread))
6690           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6691           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6692           (setq read (cdr read)))))
6693     ;; And add the last unread articles.
6694     (cond ((not (and first last))
6695            nil)
6696           ((< first last)
6697            (push (cons first last) unread))
6698           ((= first last)
6699            (push first unread)))
6700     ;; Return the sequence of unread articles.
6701     (delq 0 (nreverse unread))))
6702
6703 ;; Various summary commands
6704
6705 (defun gnus-summary-select-article-buffer ()
6706   "Reconfigure windows to show article buffer."
6707   (interactive)
6708   (if (not (gnus-buffer-live-p gnus-article-buffer))
6709       (error "There is no article buffer for this summary buffer")
6710     (gnus-configure-windows 'article)
6711     (select-window (get-buffer-window gnus-article-buffer))))
6712
6713 (defun gnus-summary-universal-argument (arg)
6714   "Perform any operation on all articles that are process/prefixed."
6715   (interactive "P")
6716   (let ((articles (gnus-summary-work-articles arg))
6717         func article)
6718     (if (eq
6719          (setq
6720           func
6721           (key-binding
6722            (read-key-sequence
6723             (substitute-command-keys
6724              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6725          'undefined)
6726         (gnus-error 1 "Undefined key")
6727       (save-excursion
6728         (while articles
6729           (gnus-summary-goto-subject (setq article (pop articles)))
6730           (let (gnus-newsgroup-processable)
6731             (command-execute func))
6732           (gnus-summary-remove-process-mark article)))))
6733   (gnus-summary-position-point))
6734
6735 (defun gnus-summary-toggle-truncation (&optional arg)
6736   "Toggle truncation of summary lines.
6737 With ARG, turn line truncation on if ARG is positive."
6738   (interactive "P")
6739   (setq truncate-lines
6740         (if (null arg) (not truncate-lines)
6741           (> (prefix-numeric-value arg) 0)))
6742   (redraw-display))
6743
6744 (defun gnus-summary-find-for-reselect ()
6745   "Return the number of an article to stay on across a reselect.
6746 The current article is considered, then following articles, then previous
6747 articles.  An article is sought which is not cancelled and isn't a temporary
6748 insertion from another group.  If there's no such then return a dummy 0."
6749   (let (found)
6750     (dolist (rev '(nil t))
6751       (unless found      ; don't demand the reverse list if we don't need it
6752         (let ((data (gnus-data-find-list
6753                      (gnus-summary-article-number) (gnus-data-list rev))))
6754           (while (and data (not found))
6755             (if (and (< 0 (gnus-data-number (car data)))
6756                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6757                 (setq found (gnus-data-number (car data))))
6758             (setq data (cdr data))))))
6759     (or found 0)))
6760
6761 (defun gnus-summary-reselect-current-group (&optional all rescan)
6762   "Exit and then reselect the current newsgroup.
6763 The prefix argument ALL means to select all articles."
6764   (interactive "P")
6765   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6766     (error "Ephemeral groups can't be reselected"))
6767   (let ((current-subject (gnus-summary-find-for-reselect))
6768         (group gnus-newsgroup-name))
6769     (setq gnus-newsgroup-begin nil)
6770     (gnus-summary-exit nil 'leave-hidden)
6771     ;; We have to adjust the point of group mode buffer because
6772     ;; point was moved to the next unread newsgroup by exiting.
6773     (gnus-summary-jump-to-group group)
6774     (when rescan
6775       (save-excursion
6776         (gnus-group-get-new-news-this-group 1)))
6777     (gnus-group-read-group all t)
6778     (gnus-summary-goto-subject current-subject nil t)))
6779
6780 (defun gnus-summary-rescan-group (&optional all)
6781   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6782   (interactive "P")
6783   (gnus-summary-reselect-current-group all t))
6784
6785 (defun gnus-summary-update-info (&optional non-destructive)
6786   (save-excursion
6787     (let ((group gnus-newsgroup-name))
6788       (when group
6789         (when gnus-newsgroup-kill-headers
6790           (setq gnus-newsgroup-killed
6791                 (gnus-compress-sequence
6792                  (gnus-sorted-union
6793                   (gnus-list-range-intersection
6794                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6795                   gnus-newsgroup-unreads)
6796                  t)))
6797         (unless (listp (cdr gnus-newsgroup-killed))
6798           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6799         (let ((headers gnus-newsgroup-headers))
6800           ;; Set the new ranges of read articles.
6801           (with-current-buffer gnus-group-buffer
6802             (gnus-undo-force-boundary))
6803           (gnus-update-read-articles
6804            group (gnus-sorted-union
6805                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6806           ;; Set the current article marks.
6807           (let ((gnus-newsgroup-scored
6808                  (if (and (not gnus-save-score)
6809                           (not non-destructive))
6810                      nil
6811                    gnus-newsgroup-scored)))
6812             (save-excursion
6813               (gnus-update-marks)))
6814           ;; Do the cross-ref thing.
6815           (when gnus-use-cross-reference
6816             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6817           ;; Do not switch windows but change the buffer to work.
6818           (set-buffer gnus-group-buffer)
6819           (unless (gnus-ephemeral-group-p group)
6820             (gnus-group-update-group group)))))))
6821
6822 (defun gnus-summary-save-newsrc (&optional force)
6823   "Save the current number of read/marked articles in the dribble buffer.
6824 The dribble buffer will then be saved.
6825 If FORCE (the prefix), also save the .newsrc file(s)."
6826   (interactive "P")
6827   (gnus-summary-update-info t)
6828   (if force
6829       (gnus-save-newsrc-file)
6830     (gnus-dribble-save)))
6831
6832 (defun gnus-summary-exit (&optional temporary leave-hidden)
6833   "Exit reading current newsgroup, and then return to group selection mode.
6834 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6835   (interactive)
6836   (gnus-set-global-variables)
6837   (when (gnus-buffer-live-p gnus-article-buffer)
6838     (save-excursion
6839       (set-buffer gnus-article-buffer)
6840       (mm-destroy-parts gnus-article-mime-handles)
6841       ;; Set it to nil for safety reason.
6842       (setq gnus-article-mime-handle-alist nil)
6843       (setq gnus-article-mime-handles nil)))
6844   (gnus-kill-save-kill-buffer)
6845   (gnus-async-halt-prefetch)
6846   (let* ((group gnus-newsgroup-name)
6847          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6848          (gnus-group-is-exiting-p t)
6849          (mode major-mode)
6850          (group-point nil)
6851          (buf (current-buffer)))
6852     (unless quit-config
6853       ;; Do adaptive scoring, and possibly save score files.
6854       (when gnus-newsgroup-adaptive
6855         (gnus-score-adaptive))
6856       (when gnus-use-scoring
6857         (gnus-score-save)))
6858     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6859     ;; If we have several article buffers, we kill them at exit.
6860     (unless gnus-single-article-buffer
6861       (gnus-kill-buffer gnus-article-buffer)
6862       (gnus-kill-buffer gnus-original-article-buffer)
6863       (setq gnus-article-current nil))
6864     (when gnus-use-cache
6865       (gnus-cache-possibly-remove-articles)
6866       (gnus-cache-save-buffers))
6867     (gnus-async-prefetch-remove-group group)
6868     (when gnus-suppress-duplicates
6869       (gnus-dup-enter-articles))
6870     (when gnus-use-trees
6871       (gnus-tree-close group))
6872     (when gnus-use-cache
6873       (gnus-cache-write-active))
6874     ;; Remove entries for this group.
6875     (nnmail-purge-split-history (gnus-group-real-name group))
6876     ;; Make all changes in this group permanent.
6877     (unless quit-config
6878       (gnus-run-hooks 'gnus-exit-group-hook)
6879       (gnus-summary-update-info))
6880     (gnus-close-group group)
6881     ;; Make sure where we were, and go to next newsgroup.
6882     (set-buffer gnus-group-buffer)
6883     (unless quit-config
6884       (gnus-group-jump-to-group group))
6885     (gnus-run-hooks 'gnus-summary-exit-hook)
6886     (unless (or quit-config
6887                 ;; If this group has disappeared from the summary
6888                 ;; buffer, don't skip forwards.
6889                 (not (string= group (gnus-group-group-name))))
6890       (gnus-group-next-unread-group 1))
6891     (setq group-point (point))
6892     (if temporary
6893         nil                             ;Nothing to do.
6894       ;; If we have several article buffers, we kill them at exit.
6895       (unless gnus-single-article-buffer
6896         (gnus-kill-buffer gnus-article-buffer)
6897         (gnus-kill-buffer gnus-original-article-buffer)
6898         (setq gnus-article-current nil))
6899       (set-buffer buf)
6900       (if (not gnus-kill-summary-on-exit)
6901           (progn
6902             (gnus-deaden-summary)
6903             (setq mode nil))
6904         ;; We set all buffer-local variables to nil.  It is unclear why
6905         ;; this is needed, but if we don't, buffer-local variables are
6906         ;; not garbage-collected, it seems.  This would the lead to en
6907         ;; ever-growing Emacs.
6908         (gnus-summary-clear-local-variables)
6909         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6910           (gnus-summary-clear-local-variables))
6911         (when (get-buffer gnus-article-buffer)
6912           (bury-buffer gnus-article-buffer))
6913         ;; Return to group mode buffer.
6914         (when (eq mode 'gnus-summary-mode)
6915           (gnus-kill-buffer buf)))
6916       (setq gnus-current-select-method gnus-select-method)
6917       (set-buffer gnus-group-buffer)
6918       (if quit-config
6919           (gnus-handle-ephemeral-exit quit-config)
6920         (goto-char group-point)
6921         ;; If gnus-group-buffer is already displayed, make sure we also move
6922         ;; the cursor in the window that displays it.
6923         (let ((win (get-buffer-window (current-buffer) 0)))
6924           (if win (set-window-point win (point))))
6925         (unless leave-hidden
6926           (gnus-configure-windows 'group 'force)))
6927       ;; Clear the current group name.
6928       (unless quit-config
6929         (setq gnus-newsgroup-name nil)))))
6930
6931 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6932 (defun gnus-summary-exit-no-update (&optional no-questions)
6933   "Quit reading current newsgroup without updating read article info."
6934   (interactive)
6935   (let* ((group gnus-newsgroup-name)
6936          (gnus-group-is-exiting-p t)
6937          (gnus-group-is-exiting-without-update-p t)
6938          (quit-config (gnus-group-quit-config group)))
6939     (when (or no-questions
6940               gnus-expert-user
6941               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6942       (gnus-async-halt-prefetch)
6943       (run-hooks 'gnus-summary-prepare-exit-hook)
6944       (when (gnus-buffer-live-p gnus-article-buffer)
6945         (save-excursion
6946           (set-buffer gnus-article-buffer)
6947           (mm-destroy-parts gnus-article-mime-handles)
6948           ;; Set it to nil for safety reason.
6949           (setq gnus-article-mime-handle-alist nil)
6950           (setq gnus-article-mime-handles nil)))
6951       ;; If we have several article buffers, we kill them at exit.
6952       (unless gnus-single-article-buffer
6953         (gnus-kill-buffer gnus-article-buffer)
6954         (gnus-kill-buffer gnus-original-article-buffer)
6955         (setq gnus-article-current nil))
6956       (if (not gnus-kill-summary-on-exit)
6957           (gnus-deaden-summary)
6958         (gnus-close-group group)
6959         (gnus-summary-clear-local-variables)
6960         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6961           (gnus-summary-clear-local-variables))
6962         (gnus-kill-buffer gnus-summary-buffer))
6963       (unless gnus-single-article-buffer
6964         (setq gnus-article-current nil))
6965       (when gnus-use-trees
6966         (gnus-tree-close group))
6967       (gnus-async-prefetch-remove-group group)
6968       (when (get-buffer gnus-article-buffer)
6969         (bury-buffer gnus-article-buffer))
6970       ;; Return to the group buffer.
6971       (gnus-configure-windows 'group 'force)
6972       ;; Clear the current group name.
6973       (setq gnus-newsgroup-name nil)
6974       (unless (gnus-ephemeral-group-p group)
6975         (gnus-group-update-group group))
6976       (when (equal (gnus-group-group-name) group)
6977         (gnus-group-next-unread-group 1))
6978       (when quit-config
6979         (gnus-handle-ephemeral-exit quit-config)))))
6980
6981 (defun gnus-handle-ephemeral-exit (quit-config)
6982   "Handle movement when leaving an ephemeral group.
6983 The state which existed when entering the ephemeral is reset."
6984   (if (not (buffer-name (car quit-config)))
6985       (gnus-configure-windows 'group 'force)
6986     (set-buffer (car quit-config))
6987     (cond ((eq major-mode 'gnus-summary-mode)
6988            (gnus-set-global-variables))
6989           ((eq major-mode 'gnus-article-mode)
6990            (save-excursion
6991              ;; The `gnus-summary-buffer' variable may point
6992              ;; to the old summary buffer when using a single
6993              ;; article buffer.
6994              (unless (gnus-buffer-live-p gnus-summary-buffer)
6995                (set-buffer gnus-group-buffer))
6996              (set-buffer gnus-summary-buffer)
6997              (gnus-set-global-variables))))
6998     (if (or (eq (cdr quit-config) 'article)
6999             (eq (cdr quit-config) 'pick))
7000         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7001             (gnus-configure-windows 'pick 'force)
7002           (gnus-configure-windows (cdr quit-config) 'force))
7003       (gnus-configure-windows (cdr quit-config) 'force))
7004     (when (eq major-mode 'gnus-summary-mode)
7005       (gnus-summary-recenter)
7006       (gnus-summary-position-point))))
7007
7008 ;;; Dead summaries.
7009
7010 (defvar gnus-dead-summary-mode-map nil)
7011
7012 (unless gnus-dead-summary-mode-map
7013   (setq gnus-dead-summary-mode-map (make-keymap))
7014   (suppress-keymap gnus-dead-summary-mode-map)
7015   (substitute-key-definition
7016    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7017   (dolist (key '("\C-d" "\r" "\177" [delete]))
7018     (define-key gnus-dead-summary-mode-map
7019       key 'gnus-summary-wake-up-the-dead))
7020   (dolist (key '("q" "Q"))
7021     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7022
7023 (defvar gnus-dead-summary-mode nil
7024   "Minor mode for Gnus summary buffers.")
7025
7026 (defun gnus-dead-summary-mode (&optional arg)
7027   "Minor mode for Gnus summary buffers."
7028   (interactive "P")
7029   (when (eq major-mode 'gnus-summary-mode)
7030     (make-local-variable 'gnus-dead-summary-mode)
7031     (setq gnus-dead-summary-mode
7032           (if (null arg) (not gnus-dead-summary-mode)
7033             (> (prefix-numeric-value arg) 0)))
7034     (when gnus-dead-summary-mode
7035       (add-minor-mode
7036        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7037
7038 (defun gnus-deaden-summary ()
7039   "Make the current summary buffer into a dead summary buffer."
7040   ;; Kill any previous dead summary buffer.
7041   (when (and gnus-dead-summary
7042              (buffer-name gnus-dead-summary))
7043     (with-current-buffer gnus-dead-summary
7044       (when gnus-dead-summary-mode
7045         (kill-buffer (current-buffer)))))
7046   ;; Make this the current dead summary.
7047   (setq gnus-dead-summary (current-buffer))
7048   (gnus-dead-summary-mode 1)
7049   (let ((name (buffer-name)))
7050     (when (string-match "Summary" name)
7051       (rename-buffer
7052        (concat (substring name 0 (match-beginning 0)) "Dead "
7053                (substring name (match-beginning 0)))
7054        t)
7055       (bury-buffer))))
7056
7057 (defun gnus-kill-or-deaden-summary (buffer)
7058   "Kill or deaden the summary BUFFER."
7059   (save-excursion
7060     (when (and (buffer-name buffer)
7061                (not gnus-single-article-buffer))
7062       (with-current-buffer buffer
7063         (gnus-kill-buffer gnus-article-buffer)
7064         (gnus-kill-buffer gnus-original-article-buffer)))
7065     (cond
7066      ;; Kill the buffer.
7067      (gnus-kill-summary-on-exit
7068       (when (and gnus-use-trees
7069                  (gnus-buffer-exists-p buffer))
7070         (save-excursion
7071           (set-buffer buffer)
7072           (gnus-tree-close gnus-newsgroup-name)))
7073       (gnus-kill-buffer buffer))
7074      ;; Deaden the buffer.
7075      ((gnus-buffer-exists-p buffer)
7076       (save-excursion
7077         (set-buffer buffer)
7078         (gnus-deaden-summary))))))
7079
7080 (defun gnus-summary-wake-up-the-dead (&rest args)
7081   "Wake up the dead summary buffer."
7082   (interactive)
7083   (gnus-dead-summary-mode -1)
7084   (let ((name (buffer-name)))
7085     (when (string-match "Dead " name)
7086       (rename-buffer
7087        (concat (substring name 0 (match-beginning 0))
7088                (substring name (match-end 0)))
7089        t)))
7090   (gnus-message 3 "This dead summary is now alive again"))
7091
7092 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7093 (defun gnus-summary-fetch-faq (&optional faq-dir)
7094   "Fetch the FAQ for the current group.
7095 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7096 in."
7097   (interactive
7098    (list
7099     (when current-prefix-arg
7100       (completing-read
7101        "FAQ dir: " (and (listp gnus-group-faq-directory)
7102                         (mapcar 'list
7103                                 gnus-group-faq-directory))))))
7104   (let (gnus-faq-buffer)
7105     (when (setq gnus-faq-buffer
7106                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7107       (gnus-configure-windows 'summary-faq))))
7108
7109 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7110 (defun gnus-summary-describe-group (&optional force)
7111   "Describe the current newsgroup."
7112   (interactive "P")
7113   (gnus-group-describe-group force gnus-newsgroup-name))
7114
7115 (defun gnus-summary-describe-briefly ()
7116   "Describe summary mode commands briefly."
7117   (interactive)
7118   (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")))
7119
7120 ;; Walking around group mode buffer from summary mode.
7121
7122 (defun gnus-summary-next-group (&optional no-article target-group backward)
7123   "Exit current newsgroup and then select next unread newsgroup.
7124 If prefix argument NO-ARTICLE is non-nil, no article is selected
7125 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7126 previous group instead."
7127   (interactive "P")
7128   ;; Stop pre-fetching.
7129   (gnus-async-halt-prefetch)
7130   (let ((current-group gnus-newsgroup-name)
7131         (current-buffer (current-buffer))
7132         entered)
7133     ;; First we semi-exit this group to update Xrefs and all variables.
7134     ;; We can't do a real exit, because the window conf must remain
7135     ;; the same in case the user is prompted for info, and we don't
7136     ;; want the window conf to change before that...
7137     (gnus-summary-exit t)
7138     (while (not entered)
7139       ;; Then we find what group we are supposed to enter.
7140       (set-buffer gnus-group-buffer)
7141       (gnus-group-jump-to-group current-group)
7142       (setq target-group
7143             (or target-group
7144                 (if (eq gnus-keep-same-level 'best)
7145                     (gnus-summary-best-group gnus-newsgroup-name)
7146                   (gnus-summary-search-group backward gnus-keep-same-level))))
7147       (if (not target-group)
7148           ;; There are no further groups, so we return to the group
7149           ;; buffer.
7150           (progn
7151             (gnus-message 5 "Returning to the group buffer")
7152             (setq entered t)
7153             (when (gnus-buffer-live-p current-buffer)
7154               (set-buffer current-buffer)
7155               (gnus-summary-exit))
7156             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7157         ;; We try to enter the target group.
7158         (gnus-group-jump-to-group target-group)
7159         (let ((unreads (gnus-group-group-unread)))
7160           (if (and (or (eq t unreads)
7161                        (and unreads (not (zerop unreads))))
7162                    (gnus-summary-read-group
7163                     target-group nil no-article
7164                     (and (buffer-name current-buffer) current-buffer)
7165                     nil backward))
7166               (setq entered t)
7167             (setq current-group target-group
7168                   target-group nil)))))))
7169
7170 (defun gnus-summary-prev-group (&optional no-article)
7171   "Exit current newsgroup and then select previous unread newsgroup.
7172 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7173   (interactive "P")
7174   (gnus-summary-next-group no-article nil t))
7175
7176 ;; Walking around summary lines.
7177
7178 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7179   "Go to the first subject satisfying any non-nil constraint.
7180 If UNREAD is non-nil, the article should be unread.
7181 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7182 If UNSEEN is non-nil, the article should be unseen.
7183 Returns the article selected or nil if there are no matching articles."
7184   (interactive "P")
7185   (cond
7186    ;; Empty summary.
7187    ((null gnus-newsgroup-data)
7188     (gnus-message 3 "No articles in the group")
7189     nil)
7190    ;; Pick the first article.
7191    ((not (or unread undownloaded unseen))
7192     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7193     (gnus-data-number (car gnus-newsgroup-data)))
7194    ;; Find the first unread article.
7195    (t
7196     (let ((data gnus-newsgroup-data))
7197       (while (and data
7198                   (let ((num (gnus-data-number (car data))))
7199                     (or (memq num gnus-newsgroup-unfetched)
7200                         (not (or (and unread
7201                                       (memq num gnus-newsgroup-unreads))
7202                                  (and undownloaded
7203                                       (memq num gnus-newsgroup-undownloaded))
7204                                  (and unseen
7205                                       (memq num gnus-newsgroup-unseen)))))))
7206         (setq data (cdr data)))
7207       (prog1
7208           (if data
7209               (progn
7210                 (goto-char (gnus-data-pos (car data)))
7211                 (gnus-data-number (car data)))
7212             (gnus-message 3 "No more%s articles"
7213                           (let* ((r (when unread " unread"))
7214                                  (d (when undownloaded " undownloaded"))
7215                                  (s (when unseen " unseen"))
7216                                  (l (delq nil (list r d s))))
7217                             (cond ((= 3 (length l))
7218                                    (concat r "," d ", or" s))
7219                                   ((= 2 (length l))
7220                                    (concat (car l) ", or" (cadr l)))
7221                                   ((= 1 (length l))
7222                                    (car l))
7223                                   (t
7224                                    ""))))
7225             nil
7226             )
7227         (gnus-summary-position-point))))))
7228
7229 (defun gnus-summary-next-subject (n &optional unread dont-display)
7230   "Go to next N'th summary line.
7231 If N is negative, go to the previous N'th subject line.
7232 If UNREAD is non-nil, only unread articles are selected.
7233 The difference between N and the actual number of steps taken is
7234 returned."
7235   (interactive "p")
7236   (let ((backward (< n 0))
7237         (n (abs n)))
7238     (while (and (> n 0)
7239                 (if backward
7240                     (gnus-summary-find-prev unread)
7241                   (gnus-summary-find-next unread)))
7242       (unless (zerop (setq n (1- n)))
7243         (gnus-summary-show-thread)))
7244     (when (/= 0 n)
7245       (gnus-message 7 "No more%s articles"
7246                     (if unread " unread" "")))
7247     (unless dont-display
7248       (gnus-summary-recenter)
7249       (gnus-summary-position-point))
7250     n))
7251
7252 (defun gnus-summary-next-unread-subject (n)
7253   "Go to next N'th unread summary line."
7254   (interactive "p")
7255   (gnus-summary-next-subject n t))
7256
7257 (defun gnus-summary-prev-subject (n &optional unread)
7258   "Go to previous N'th summary line.
7259 If optional argument UNREAD is non-nil, only unread article is selected."
7260   (interactive "p")
7261   (gnus-summary-next-subject (- n) unread))
7262
7263 (defun gnus-summary-prev-unread-subject (n)
7264   "Go to previous N'th unread summary line."
7265   (interactive "p")
7266   (gnus-summary-next-subject (- n) t))
7267
7268 (defun gnus-summary-goto-subjects (articles)
7269   "Insert the subject header for ARTICLES in the current buffer."
7270   (save-excursion
7271     (dolist (article articles)
7272       (gnus-summary-goto-subject article t)))
7273   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7274   (gnus-summary-position-point))
7275
7276 (defun gnus-summary-goto-subject (article &optional force silent)
7277   "Go the subject line of ARTICLE.
7278 If FORCE, also allow jumping to articles not currently shown."
7279   (interactive "nArticle number: ")
7280   (unless (numberp article)
7281     (error "Article %s is not a number" article))
7282   (let ((b (point))
7283         (data (gnus-data-find article)))
7284     ;; We read in the article if we have to.
7285     (and (not data)
7286          force
7287          (gnus-summary-insert-subject
7288           article
7289           (if (or (numberp force) (vectorp force)) force)
7290           t)
7291          (setq data (gnus-data-find article)))
7292     (goto-char b)
7293     (if (not data)
7294         (progn
7295           (unless silent
7296             (gnus-message 3 "Can't find article %d" article))
7297           nil)
7298       (let ((pt (gnus-data-pos data)))
7299         (goto-char pt)
7300         (gnus-summary-set-article-display-arrow pt))
7301       (gnus-summary-position-point)
7302       article)))
7303
7304 ;; Walking around summary lines with displaying articles.
7305
7306 (defun gnus-summary-expand-window (&optional arg)
7307   "Make the summary buffer take up the entire Emacs frame.
7308 Given a prefix, will force an `article' buffer configuration."
7309   (interactive "P")
7310   (if arg
7311       (gnus-configure-windows 'article 'force)
7312     (gnus-configure-windows 'summary 'force)))
7313
7314 (defun gnus-summary-display-article (article &optional all-header)
7315   "Display ARTICLE in article buffer."
7316   (when (gnus-buffer-live-p gnus-article-buffer)
7317     (with-current-buffer gnus-article-buffer
7318       (mm-enable-multibyte)))
7319   (gnus-set-global-variables)
7320   (when (gnus-buffer-live-p gnus-article-buffer)
7321     (with-current-buffer gnus-article-buffer
7322       (setq gnus-article-charset gnus-newsgroup-charset)
7323       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7324       (mm-enable-multibyte)))
7325   (if (null article)
7326       nil
7327     (prog1
7328         (if gnus-summary-display-article-function
7329             (funcall gnus-summary-display-article-function article all-header)
7330           (gnus-article-prepare article all-header))
7331       (gnus-run-hooks 'gnus-select-article-hook)
7332       (when (and gnus-current-article
7333                  (not (zerop gnus-current-article)))
7334         (gnus-summary-goto-subject gnus-current-article))
7335       (gnus-summary-recenter)
7336       (when (and gnus-use-trees gnus-show-threads)
7337         (gnus-possibly-generate-tree article)
7338         (gnus-highlight-selected-tree article))
7339       ;; Successfully display article.
7340       (gnus-article-set-window-start
7341        (cdr (assq article gnus-newsgroup-bookmarks))))))
7342
7343 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7344   "Select the current article.
7345 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7346 non-nil, the article will be re-fetched even if it already present in
7347 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7348 be displayed."
7349   ;; Make sure we are in the summary buffer to work around bbdb bug.
7350   (unless (eq major-mode 'gnus-summary-mode)
7351     (set-buffer gnus-summary-buffer))
7352   (let ((article (or article (gnus-summary-article-number)))
7353         (all-headers (not (not all-headers))) ;Must be t or nil.
7354         gnus-summary-display-article-function)
7355     (and (not pseudo)
7356          (gnus-summary-article-pseudo-p article)
7357          (error "This is a pseudo-article"))
7358     (save-excursion
7359       (set-buffer gnus-summary-buffer)
7360       (if (or (and gnus-single-article-buffer
7361                    (or (null gnus-current-article)
7362                        (null gnus-article-current)
7363                        (null (get-buffer gnus-article-buffer))
7364                        (not (eq article (cdr gnus-article-current)))
7365                        (not (equal (car gnus-article-current)
7366                                    gnus-newsgroup-name))))
7367               (and (not gnus-single-article-buffer)
7368                    (or (null gnus-current-article)
7369                        (not (eq gnus-current-article article))))
7370               force)
7371           ;; The requested article is different from the current article.
7372           (progn
7373             (gnus-summary-display-article article all-headers)
7374             (when (gnus-buffer-live-p gnus-article-buffer)
7375               (with-current-buffer gnus-article-buffer
7376                 (if (not gnus-article-decoded-p) ;; a local variable
7377                     (mm-disable-multibyte))))
7378             (gnus-article-set-window-start
7379              (cdr (assq article gnus-newsgroup-bookmarks)))
7380             article)
7381         'old))))
7382
7383 (defun gnus-summary-force-verify-and-decrypt ()
7384   "Display buttons for signed/encrypted parts and verify/decrypt them."
7385   (interactive)
7386   (let ((mm-verify-option 'known)
7387         (mm-decrypt-option 'known)
7388         (gnus-article-emulate-mime t)
7389         (gnus-buttonized-mime-types (append (list "multipart/signed"
7390                                                   "multipart/encrypted")
7391                                             gnus-buttonized-mime-types)))
7392     (gnus-summary-select-article nil 'force)))
7393
7394 (defun gnus-summary-set-current-mark (&optional current-mark)
7395   "Obsolete function."
7396   nil)
7397
7398 (defun gnus-summary-next-article (&optional unread subject backward push)
7399   "Select the next article.
7400 If UNREAD, only unread articles are selected.
7401 If SUBJECT, only articles with SUBJECT are selected.
7402 If BACKWARD, the previous article is selected instead of the next."
7403   (interactive "P")
7404   (cond
7405    ;; Is there such an article?
7406    ((and (gnus-summary-search-forward unread subject backward)
7407          (or (gnus-summary-display-article (gnus-summary-article-number))
7408              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7409     (gnus-summary-position-point))
7410    ;; If not, we try the first unread, if that is wanted.
7411    ((and subject
7412          gnus-auto-select-same
7413          (gnus-summary-first-unread-article))
7414     (gnus-summary-position-point)
7415     (gnus-message 6 "Wrapped"))
7416    ;; Try to get next/previous article not displayed in this group.
7417    ((and gnus-auto-extend-newsgroup
7418          (not unread) (not subject))
7419     (gnus-summary-goto-article
7420      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7421      nil (count-lines (point-min) (point))))
7422    ;; Go to next/previous group.
7423    (t
7424     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7425       (gnus-summary-jump-to-group gnus-newsgroup-name))
7426     (let ((cmd last-command-char)
7427           (point
7428            (with-current-buffer gnus-group-buffer
7429              (point)))
7430           (group
7431            (if (eq gnus-keep-same-level 'best)
7432                (gnus-summary-best-group gnus-newsgroup-name)
7433              (gnus-summary-search-group backward gnus-keep-same-level))))
7434       ;; For some reason, the group window gets selected.  We change
7435       ;; it back.
7436       (select-window (get-buffer-window (current-buffer)))
7437       ;; Select next unread newsgroup automagically.
7438       (cond
7439        ((or (not gnus-auto-select-next)
7440             (not cmd))
7441         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7442        ((or (eq gnus-auto-select-next 'quietly)
7443             (and (eq gnus-auto-select-next 'slightly-quietly)
7444                  push)
7445             (and (eq gnus-auto-select-next 'almost-quietly)
7446                  (gnus-summary-last-article-p)))
7447         ;; Select quietly.
7448         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7449             (gnus-summary-exit)
7450           (gnus-message 7 "No more%s articles (%s)..."
7451                         (if unread " unread" "")
7452                         (if group (concat "selecting " group)
7453                           "exiting"))
7454           (gnus-summary-next-group nil group backward)))
7455        (t
7456         (when (gnus-key-press-event-p last-input-event)
7457           (gnus-summary-walk-group-buffer
7458            gnus-newsgroup-name cmd unread backward point))))))))
7459
7460 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7461   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7462                       (?\C-p (gnus-group-prev-unread-group 1))))
7463         (cursor-in-echo-area t)
7464         keve key group ended prompt)
7465     (save-excursion
7466       (set-buffer gnus-group-buffer)
7467       (goto-char start)
7468       (setq group
7469             (if (eq gnus-keep-same-level 'best)
7470                 (gnus-summary-best-group gnus-newsgroup-name)
7471               (gnus-summary-search-group backward gnus-keep-same-level))))
7472     (while (not ended)
7473       (setq prompt
7474             (format
7475              "No more%s articles%s " (if unread " unread" "")
7476              (if (and group
7477                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7478                  (format " (Type %s for %s [%s])"
7479                          (single-key-description cmd)
7480                          (gnus-group-decoded-name group)
7481                          (gnus-group-unread group))
7482                (format " (Type %s to exit %s)"
7483                        (single-key-description cmd)
7484                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7485       ;; Confirm auto selection.
7486       (setq key (car (setq keve (gnus-read-event-char prompt)))
7487             ended t)
7488       (cond
7489        ((assq key keystrokes)
7490         (let ((obuf (current-buffer)))
7491           (switch-to-buffer gnus-group-buffer)
7492           (when group
7493             (gnus-group-jump-to-group group))
7494           (eval (cadr (assq key keystrokes)))
7495           (setq group (gnus-group-group-name))
7496           (switch-to-buffer obuf))
7497         (setq ended nil))
7498        ((equal key cmd)
7499         (if (or (not group)
7500                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7501             (gnus-summary-exit)
7502           (gnus-summary-next-group nil group backward)))
7503        (t
7504         (push (cdr keve) unread-command-events))))))
7505
7506 (defun gnus-summary-next-unread-article ()
7507   "Select unread article after current one."
7508   (interactive)
7509   (gnus-summary-next-article
7510    (or (not (eq gnus-summary-goto-unread 'never))
7511        (gnus-summary-last-article-p (gnus-summary-article-number)))
7512    (and gnus-auto-select-same
7513         (gnus-summary-article-subject))))
7514
7515 (defun gnus-summary-prev-article (&optional unread subject)
7516   "Select the article after the current one.
7517 If UNREAD is non-nil, only unread articles are selected."
7518   (interactive "P")
7519   (gnus-summary-next-article unread subject t))
7520
7521 (defun gnus-summary-prev-unread-article ()
7522   "Select unread article before current one."
7523   (interactive)
7524   (gnus-summary-prev-article
7525    (or (not (eq gnus-summary-goto-unread 'never))
7526        (gnus-summary-first-article-p (gnus-summary-article-number)))
7527    (and gnus-auto-select-same
7528         (gnus-summary-article-subject))))
7529
7530 (defun gnus-summary-next-page (&optional lines circular stop)
7531   "Show next page of the selected article.
7532 If at the end of the current article, select the next article.
7533 LINES says how many lines should be scrolled up.
7534
7535 If CIRCULAR is non-nil, go to the start of the article instead of
7536 selecting the next article when reaching the end of the current
7537 article.
7538
7539 If STOP is non-nil, just stop when reaching the end of the message.
7540
7541 Also see the variable `gnus-article-skip-boring'."
7542   (interactive "P")
7543   (setq gnus-summary-buffer (current-buffer))
7544   (gnus-set-global-variables)
7545   (let ((article (gnus-summary-article-number))
7546         (article-window (get-buffer-window gnus-article-buffer t))
7547         endp)
7548     ;; If the buffer is empty, we have no article.
7549     (unless article
7550       (error "No article to select"))
7551     (gnus-configure-windows 'article)
7552     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7553         (if (and (eq gnus-summary-goto-unread 'never)
7554                  (not (gnus-summary-last-article-p article)))
7555             (gnus-summary-next-article)
7556           (gnus-summary-next-unread-article))
7557       (if (or (null gnus-current-article)
7558               (null gnus-article-current)
7559               (/= article (cdr gnus-article-current))
7560               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7561           ;; Selected subject is different from current article's.
7562           (gnus-summary-display-article article)
7563         (when article-window
7564           (gnus-eval-in-buffer-window gnus-article-buffer
7565             (setq endp (or (gnus-article-next-page lines)
7566                            (gnus-article-only-boring-p))))
7567           (when endp
7568             (cond (stop
7569                    (gnus-message 3 "End of message"))
7570                   (circular
7571                    (gnus-summary-beginning-of-article))
7572                   (lines
7573                    (gnus-message 3 "End of message"))
7574                   ((null lines)
7575                    (if (and (eq gnus-summary-goto-unread 'never)
7576                             (not (gnus-summary-last-article-p article)))
7577                        (gnus-summary-next-article)
7578                      (gnus-summary-next-unread-article))))))))
7579     (gnus-summary-recenter)
7580     (gnus-summary-position-point)))
7581
7582 (defun gnus-summary-prev-page (&optional lines move)
7583   "Show previous page of selected article.
7584 Argument LINES specifies lines to be scrolled down.
7585 If MOVE, move to the previous unread article if point is at
7586 the beginning of the buffer."
7587   (interactive "P")
7588   (let ((article (gnus-summary-article-number))
7589         (article-window (get-buffer-window gnus-article-buffer t))
7590         endp)
7591     (gnus-configure-windows 'article)
7592     (if (or (null gnus-current-article)
7593             (null gnus-article-current)
7594             (/= article (cdr gnus-article-current))
7595             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7596         ;; Selected subject is different from current article's.
7597         (gnus-summary-display-article article)
7598       (gnus-summary-recenter)
7599       (when article-window
7600         (gnus-eval-in-buffer-window gnus-article-buffer
7601           (setq endp (gnus-article-prev-page lines)))
7602         (when (and move endp)
7603           (cond (lines
7604                  (gnus-message 3 "Beginning of message"))
7605                 ((null lines)
7606                  (if (and (eq gnus-summary-goto-unread 'never)
7607                           (not (gnus-summary-first-article-p article)))
7608                      (gnus-summary-prev-article)
7609                    (gnus-summary-prev-unread-article))))))))
7610   (gnus-summary-position-point))
7611
7612 (defun gnus-summary-prev-page-or-article (&optional lines)
7613   "Show previous page of selected article.
7614 Argument LINES specifies lines to be scrolled down.
7615 If at the beginning of the article, go to the next article."
7616   (interactive "P")
7617   (gnus-summary-prev-page lines t))
7618
7619 (defun gnus-summary-scroll-up (lines)
7620   "Scroll up (or down) one line current article.
7621 Argument LINES specifies lines to be scrolled up (or down if negative)."
7622   (interactive "p")
7623   (gnus-configure-windows 'article)
7624   (gnus-summary-show-thread)
7625   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7626     (gnus-eval-in-buffer-window gnus-article-buffer
7627       (cond ((> lines 0)
7628              (when (gnus-article-next-page lines)
7629                (gnus-message 3 "End of message")))
7630             ((< lines 0)
7631              (gnus-article-prev-page (- lines))))))
7632   (gnus-summary-recenter)
7633   (gnus-summary-position-point))
7634
7635 (defun gnus-summary-scroll-down (lines)
7636   "Scroll down (or up) one line current article.
7637 Argument LINES specifies lines to be scrolled down (or up if negative)."
7638   (interactive "p")
7639   (gnus-summary-scroll-up (- lines)))
7640
7641 (defun gnus-summary-next-same-subject ()
7642   "Select next article which has the same subject as current one."
7643   (interactive)
7644   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7645
7646 (defun gnus-summary-prev-same-subject ()
7647   "Select previous article which has the same subject as current one."
7648   (interactive)
7649   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7650
7651 (defun gnus-summary-next-unread-same-subject ()
7652   "Select next unread article which has the same subject as current one."
7653   (interactive)
7654   (gnus-summary-next-article t (gnus-summary-article-subject)))
7655
7656 (defun gnus-summary-prev-unread-same-subject ()
7657   "Select previous unread article which has the same subject as current one."
7658   (interactive)
7659   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7660
7661 (defun gnus-summary-first-unread-article ()
7662   "Select the first unread article.
7663 Return nil if there are no unread articles."
7664   (interactive)
7665   (prog1
7666       (when (gnus-summary-first-subject t)
7667         (gnus-summary-show-thread)
7668         (gnus-summary-first-subject t)
7669         (gnus-summary-display-article (gnus-summary-article-number)))
7670     (gnus-summary-position-point)))
7671
7672 (defun gnus-summary-first-unread-subject ()
7673   "Place the point on the subject line of the first unread article.
7674 Return nil if there are no unread articles."
7675   (interactive)
7676   (prog1
7677       (when (gnus-summary-first-subject t)
7678         (gnus-summary-show-thread)
7679         (gnus-summary-first-subject t))
7680     (gnus-summary-position-point)))
7681
7682 (defun gnus-summary-first-unseen-subject ()
7683   "Place the point on the subject line of the first unseen article.
7684 Return nil if there are no unseen articles."
7685   (interactive)
7686   (prog1
7687       (when (gnus-summary-first-subject nil nil t)
7688         (gnus-summary-show-thread)
7689         (gnus-summary-first-subject nil nil t))
7690     (gnus-summary-position-point)))
7691
7692 (defun gnus-summary-first-unseen-or-unread-subject ()
7693   "Place the point on the subject line of the first unseen article or,
7694 if all article have been seen, on the subject line of the first unread
7695 article."
7696   (interactive)
7697   (prog1
7698       (unless (when (gnus-summary-first-subject nil nil t)
7699                 (gnus-summary-show-thread)
7700                 (gnus-summary-first-subject nil nil t))
7701         (when (gnus-summary-first-subject t)
7702           (gnus-summary-show-thread)
7703           (gnus-summary-first-subject t)))
7704     (gnus-summary-position-point)))
7705
7706 (defun gnus-summary-first-article ()
7707   "Select the first article.
7708 Return nil if there are no articles."
7709   (interactive)
7710   (prog1
7711       (when (gnus-summary-first-subject)
7712         (gnus-summary-show-thread)
7713         (gnus-summary-first-subject)
7714         (gnus-summary-display-article (gnus-summary-article-number)))
7715     (gnus-summary-position-point)))
7716
7717 (defun gnus-summary-best-unread-article (&optional arg)
7718   "Select the unread article with the highest score.
7719 If given a prefix argument, select the next unread article that has a
7720 score higher than the default score."
7721   (interactive "P")
7722   (let ((article (if arg
7723                      (gnus-summary-better-unread-subject)
7724                    (gnus-summary-best-unread-subject))))
7725     (if article
7726         (gnus-summary-goto-article article)
7727       (error "No unread articles"))))
7728
7729 (defun gnus-summary-best-unread-subject ()
7730   "Select the unread subject with the highest score."
7731   (interactive)
7732   (let ((best -1000000)
7733         (data gnus-newsgroup-data)
7734         article score)
7735     (while data
7736       (and (gnus-data-unread-p (car data))
7737            (> (setq score
7738                     (gnus-summary-article-score (gnus-data-number (car data))))
7739               best)
7740            (setq best score
7741                  article (gnus-data-number (car data))))
7742       (setq data (cdr data)))
7743     (when article
7744       (gnus-summary-goto-subject article))
7745     (gnus-summary-position-point)
7746     article))
7747
7748 (defun gnus-summary-better-unread-subject ()
7749   "Select the first unread subject that has a score over the default score."
7750   (interactive)
7751   (let ((data gnus-newsgroup-data)
7752         article score)
7753     (while (and (setq article (gnus-data-number (car data)))
7754                 (or (gnus-data-read-p (car data))
7755                     (not (> (gnus-summary-article-score article)
7756                             gnus-summary-default-score))))
7757       (setq data (cdr data)))
7758     (when article
7759       (gnus-summary-goto-subject article))
7760     (gnus-summary-position-point)
7761     article))
7762
7763 (defun gnus-summary-last-subject ()
7764   "Go to the last displayed subject line in the group."
7765   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7766     (when article
7767       (gnus-summary-goto-subject article))))
7768
7769 (defun gnus-summary-goto-article (article &optional all-headers force)
7770   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7771 If ALL-HEADERS is non-nil, no header lines are hidden.
7772 If FORCE, go to the article even if it isn't displayed.  If FORCE
7773 is a number, it is the line the article is to be displayed on."
7774   (interactive
7775    (list
7776     (completing-read
7777      "Article number or Message-ID: "
7778      (mapcar (lambda (number) (list (int-to-string number)))
7779              gnus-newsgroup-limit))
7780     current-prefix-arg
7781     t))
7782   (prog1
7783       (if (and (stringp article)
7784                (string-match "@\\|%40" article))
7785           (gnus-summary-refer-article article)
7786         (when (stringp article)
7787           (setq article (string-to-number article)))
7788         (if (gnus-summary-goto-subject article force)
7789             (gnus-summary-display-article article all-headers)
7790           (gnus-message 4 "Couldn't go to article %s" article) nil))
7791     (gnus-summary-position-point)))
7792
7793 (defun gnus-summary-goto-last-article ()
7794   "Go to the previously read article."
7795   (interactive)
7796   (prog1
7797       (when gnus-last-article
7798         (gnus-summary-goto-article gnus-last-article nil t))
7799     (gnus-summary-position-point)))
7800
7801 (defun gnus-summary-pop-article (number)
7802   "Pop one article off the history and go to the previous.
7803 NUMBER articles will be popped off."
7804   (interactive "p")
7805   (let (to)
7806     (setq gnus-newsgroup-history
7807           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7808     (if to
7809         (gnus-summary-goto-article (car to) nil t)
7810       (error "Article history empty")))
7811   (gnus-summary-position-point))
7812
7813 ;; Summary commands and functions for limiting the summary buffer.
7814
7815 (defun gnus-summary-limit-to-articles (n)
7816   "Limit the summary buffer to the next N articles.
7817 If not given a prefix, use the process marked articles instead."
7818   (interactive "P")
7819   (prog1
7820       (let ((articles (gnus-summary-work-articles n)))
7821         (setq gnus-newsgroup-processable nil)
7822         (gnus-summary-limit articles))
7823     (gnus-summary-position-point)))
7824
7825 (defun gnus-summary-pop-limit (&optional total)
7826   "Restore the previous limit.
7827 If given a prefix, remove all limits."
7828   (interactive "P")
7829   (when total
7830     (setq gnus-newsgroup-limits
7831           (list (mapcar (lambda (h) (mail-header-number h))
7832                         gnus-newsgroup-headers))))
7833   (unless gnus-newsgroup-limits
7834     (error "No limit to pop"))
7835   (prog1
7836       (gnus-summary-limit nil 'pop)
7837     (gnus-summary-position-point)))
7838
7839 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7840   "Limit the summary buffer to articles that have subjects that match a regexp.
7841 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7842   (interactive
7843    (list (read-string (if current-prefix-arg
7844                           "Exclude subject (regexp): "
7845                         "Limit to subject (regexp): "))
7846          nil current-prefix-arg))
7847   (unless header
7848     (setq header "subject"))
7849   (when (not (equal "" subject))
7850     (prog1
7851         (let ((articles (gnus-summary-find-matching
7852                          (or header "subject") subject 'all nil nil
7853                          not-matching)))
7854           (unless articles
7855             (error "Found no matches for \"%s\"" subject))
7856           (gnus-summary-limit articles))
7857       (gnus-summary-position-point))))
7858
7859 (defun gnus-summary-limit-to-author (from &optional not-matching)
7860   "Limit the summary buffer to articles that have authors that match a regexp.
7861 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7862   (interactive
7863    (list (read-string (if current-prefix-arg
7864                           "Exclude author (regexp): "
7865                         "Limit to author (regexp): "))
7866          current-prefix-arg))
7867   (gnus-summary-limit-to-subject from "from" not-matching))
7868
7869 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7870   "Limit the summary buffer to articles with the given RECIPIENT.
7871
7872 If NOT-MATCHING, exclude RECIPIENT.
7873
7874 To and Cc headers are checked.  You need to include them in
7875 `nnmail-extra-headers'."
7876   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7877   (interactive
7878    (list (read-string (format "%s recipient (regexp): "
7879                               (if current-prefix-arg "Exclude" "Limit to")))
7880          current-prefix-arg))
7881   (when (not (equal "" recipient))
7882     (prog1 (let* ((to
7883                    (if (memq 'To nnmail-extra-headers)
7884                        (gnus-summary-find-matching
7885                         (cons 'extra 'To) recipient 'all nil nil
7886                         not-matching)
7887                      (gnus-message
7888                       1 "`To' isn't present in `nnmail-extra-headers'")
7889                      (sit-for 1)
7890                      nil))
7891                   (cc
7892                    (if (memq 'Cc nnmail-extra-headers)
7893                        (gnus-summary-find-matching
7894                         (cons 'extra 'Cc) recipient 'all nil nil
7895                         not-matching)
7896                      (gnus-message
7897                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7898                      (sit-for 1)
7899                      nil))
7900                   (articles
7901                    (if not-matching
7902                        ;; We need the numbers that are in both lists:
7903                        (mapcar (lambda (a)
7904                                  (and (memq a to) a))
7905                                cc)
7906                      (nconc to cc))))
7907              (unless articles
7908                (error "Found no matches for \"%s\"" recipient))
7909              (gnus-summary-limit articles))
7910       (gnus-summary-position-point))))
7911
7912 (defun gnus-summary-limit-to-age (age &optional younger-p)
7913   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7914 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7915 articles that are younger than AGE days."
7916   (interactive
7917    (let ((younger current-prefix-arg)
7918          (days-got nil)
7919          days)
7920      (while (not days-got)
7921        (setq days (if younger
7922                       (read-string "Limit to articles younger than (in days, older when negative): ")
7923                     (read-string
7924                      "Limit to articles older than (in days, younger when negative): ")))
7925        (when (> (length days) 0)
7926          (setq days (read days)))
7927        (if (numberp days)
7928            (progn
7929              (setq days-got t)
7930              (when (< days 0)
7931                (setq younger (not younger))
7932                (setq days (* days -1))))
7933          (message "Please enter a number.")
7934          (sleep-for 1)))
7935      (list days younger)))
7936   (prog1
7937       (let ((data gnus-newsgroup-data)
7938             (cutoff (days-to-time age))
7939             articles d date is-younger)
7940         (while (setq d (pop data))
7941           (when (and (vectorp (gnus-data-header d))
7942                      (setq date (mail-header-date (gnus-data-header d))))
7943             (setq is-younger (time-less-p
7944                               (time-since (condition-case ()
7945                                               (date-to-time date)
7946                                             (error '(0 0))))
7947                               cutoff))
7948             (when (if younger-p
7949                       is-younger
7950                     (not is-younger))
7951               (push (gnus-data-number d) articles))))
7952         (gnus-summary-limit (nreverse articles)))
7953     (gnus-summary-position-point)))
7954
7955 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7956   "Limit the summary buffer to articles that match an 'extra' header."
7957   (interactive
7958    (let ((header
7959           (intern
7960            (gnus-completing-read-with-default
7961             (symbol-name (car gnus-extra-headers))
7962             (if current-prefix-arg
7963                 "Exclude extra header"
7964               "Limit extra header")
7965             (mapcar (lambda (x)
7966                       (cons (symbol-name x) x))
7967                     gnus-extra-headers)
7968             nil
7969             t))))
7970      (list header
7971            (read-string (format "%s header %s (regexp): "
7972                                 (if current-prefix-arg "Exclude" "Limit to")
7973                                 header))
7974            current-prefix-arg)))
7975   (when (not (equal "" regexp))
7976     (prog1
7977         (let ((articles (gnus-summary-find-matching
7978                          (cons 'extra header) regexp 'all nil nil
7979                          not-matching)))
7980           (unless articles
7981             (error "Found no matches for \"%s\"" regexp))
7982           (gnus-summary-limit articles))
7983       (gnus-summary-position-point))))
7984
7985 (defun gnus-summary-limit-to-display-predicate ()
7986   "Limit the summary buffer to the predicated in the `display' group parameter."
7987   (interactive)
7988   (unless gnus-newsgroup-display
7989     (error "There is no `display' group parameter"))
7990   (let (articles)
7991     (dolist (number gnus-newsgroup-articles)
7992       (when (funcall gnus-newsgroup-display)
7993         (push number articles)))
7994     (gnus-summary-limit articles))
7995   (gnus-summary-position-point))
7996
7997 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7998 (make-obsolete
7999  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8000
8001 (defun gnus-summary-limit-to-unread (&optional all)
8002   "Limit the summary buffer to articles that are not marked as read.
8003 If ALL is non-nil, limit strictly to unread articles."
8004   (interactive "P")
8005   (if all
8006       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8007     (gnus-summary-limit-to-marks
8008      ;; Concat all the marks that say that an article is read and have
8009      ;; those removed.
8010      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8011            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8012            gnus-low-score-mark gnus-expirable-mark
8013            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8014            gnus-duplicate-mark gnus-souped-mark)
8015      'reverse)))
8016
8017 (defun gnus-summary-limit-to-headers (match &optional reverse)
8018   "Limit the summary buffer to articles that have headers that match MATCH.
8019 If REVERSE (the prefix), limit to articles that don't match."
8020   (interactive "sMatch headers (regexp): \nP")
8021   (gnus-summary-limit-to-bodies match reverse t))
8022
8023 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8024   "Limit the summary buffer to articles that have bodies that match MATCH.
8025 If REVERSE (the prefix), limit to articles that don't match."
8026   (interactive "sMatch body (regexp): \nP")
8027   (let ((articles nil)
8028         (gnus-select-article-hook nil)  ;Disable hook.
8029         (gnus-article-prepare-hook nil)
8030         (gnus-use-article-prefetch nil)
8031         (gnus-keep-backlog nil)
8032         (gnus-break-pages nil)
8033         (gnus-summary-display-arrow nil)
8034         (gnus-updated-mode-lines nil)
8035         (gnus-auto-center-summary nil)
8036         (gnus-display-mime-function nil))
8037     (dolist (data gnus-newsgroup-data)
8038       (let (gnus-mark-article-hook)
8039         (gnus-summary-select-article t t nil (gnus-data-number data)))
8040       (save-excursion
8041         (set-buffer gnus-article-buffer)
8042         (article-goto-body)
8043         (let* ((case-fold-search t)
8044                (found (if headersp
8045                           (re-search-backward match nil t)
8046                         (re-search-forward match nil t))))
8047           (when (or (and found
8048                          (not reverse))
8049                     (and (not found)
8050                          reverse))
8051             (push (gnus-data-number data) articles)))))
8052     (if (not articles)
8053         (message "No messages matched")
8054       (gnus-summary-limit articles)))
8055   (gnus-summary-position-point))
8056
8057 (defun gnus-summary-limit-to-replied (&optional unreplied)
8058   "Limit the summary buffer to replied articles.
8059 If UNREPLIED (the prefix), limit to unreplied articles."
8060   (interactive "P")
8061   (if unreplied
8062       (gnus-summary-limit
8063        (gnus-set-difference gnus-newsgroup-articles
8064         gnus-newsgroup-replied))
8065     (gnus-summary-limit gnus-newsgroup-replied))
8066   (gnus-summary-position-point))
8067
8068 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8069 (make-obsolete 'gnus-summary-delete-marked-with
8070                'gnus-summary-limit-exclude-marks)
8071
8072 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8073   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8074 If REVERSE, limit the summary buffer to articles that are marked
8075 with MARKS.  MARKS can either be a string of marks or a list of marks.
8076 Returns how many articles were removed."
8077   (interactive "sMarks: ")
8078   (gnus-summary-limit-to-marks marks t))
8079
8080 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8081   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8082 If REVERSE (the prefix), limit the summary buffer to articles that are
8083 not marked with MARKS.  MARKS can either be a string of marks or a
8084 list of marks.
8085 Returns how many articles were removed."
8086   (interactive "sMarks: \nP")
8087   (prog1
8088       (let ((data gnus-newsgroup-data)
8089             (marks (if (listp marks) marks
8090                      (append marks nil))) ; Transform to list.
8091             articles)
8092         (while data
8093           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8094                   (memq (gnus-data-mark (car data)) marks))
8095             (push (gnus-data-number (car data)) articles))
8096           (setq data (cdr data)))
8097         (gnus-summary-limit articles))
8098     (gnus-summary-position-point)))
8099
8100 (defun gnus-summary-limit-to-score (score)
8101   "Limit to articles with score at or above SCORE."
8102   (interactive "NLimit to articles with score of at least: ")
8103   (let ((data gnus-newsgroup-data)
8104         articles)
8105     (while data
8106       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8107                 score)
8108         (push (gnus-data-number (car data)) articles))
8109       (setq data (cdr data)))
8110     (prog1
8111         (gnus-summary-limit articles)
8112       (gnus-summary-position-point))))
8113
8114 (defun gnus-summary-limit-to-unseen ()
8115   "Limit to unseen articles."
8116   (interactive)
8117   (prog1
8118       (gnus-summary-limit gnus-newsgroup-unseen)
8119     (gnus-summary-position-point)))
8120
8121 (defun gnus-summary-limit-include-thread (id)
8122   "Display all the hidden articles that is in the thread with ID in it.
8123 When called interactively, ID is the Message-ID of the current
8124 article."
8125   (interactive (list (mail-header-id (gnus-summary-article-header))))
8126   (let ((articles (gnus-articles-in-thread
8127                    (gnus-id-to-thread (gnus-root-id id)))))
8128     (prog1
8129         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8130       (gnus-summary-limit-include-matching-articles
8131        "subject"
8132        (regexp-quote (gnus-simplify-subject-re
8133                       (mail-header-subject (gnus-id-to-header id)))))
8134       (gnus-summary-position-point))))
8135
8136 (defun gnus-summary-limit-include-matching-articles (header regexp)
8137   "Display all the hidden articles that have HEADERs that match REGEXP."
8138   (interactive (list (read-string "Match on header: ")
8139                      (read-string "Regexp: ")))
8140   (let ((articles (gnus-find-matching-articles header regexp)))
8141     (prog1
8142         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8143       (gnus-summary-position-point))))
8144
8145 (defun gnus-summary-insert-dormant-articles ()
8146   "Insert all the dormant articles for this group into the current buffer."
8147   (interactive)
8148   (let ((gnus-verbose (max 6 gnus-verbose)))
8149     (if (not gnus-newsgroup-dormant)
8150         (gnus-message 3 "No cached articles for this group")
8151       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8152
8153 (defun gnus-summary-limit-include-dormant ()
8154   "Display all the hidden articles that are marked as dormant.
8155 Note that this command only works on a subset of the articles currently
8156 fetched for this group."
8157   (interactive)
8158   (unless gnus-newsgroup-dormant
8159     (error "There are no dormant articles in this group"))
8160   (prog1
8161       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8162     (gnus-summary-position-point)))
8163
8164 (defun gnus-summary-limit-exclude-dormant ()
8165   "Hide all dormant articles."
8166   (interactive)
8167   (prog1
8168       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8169     (gnus-summary-position-point)))
8170
8171 (defun gnus-summary-limit-exclude-childless-dormant ()
8172   "Hide all dormant articles that have no children."
8173   (interactive)
8174   (let ((data (gnus-data-list t))
8175         articles d children)
8176     ;; Find all articles that are either not dormant or have
8177     ;; children.
8178     (while (setq d (pop data))
8179       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8180                 (and (setq children
8181                            (gnus-article-children (gnus-data-number d)))
8182                      (let (found)
8183                        (while children
8184                          (when (memq (car children) articles)
8185                            (setq children nil
8186                                  found t))
8187                          (pop children))
8188                        found)))
8189         (push (gnus-data-number d) articles)))
8190     ;; Do the limiting.
8191     (prog1
8192         (gnus-summary-limit articles)
8193       (gnus-summary-position-point))))
8194
8195 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8196   "Mark all unread excluded articles as read.
8197 If ALL, mark even excluded ticked and dormants as read."
8198   (interactive "P")
8199   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8200   (let ((articles (gnus-sorted-ndifference
8201                    (sort
8202                     (mapcar (lambda (h) (mail-header-number h))
8203                             gnus-newsgroup-headers)
8204                     '<)
8205                    gnus-newsgroup-limit))
8206         article)
8207     (setq gnus-newsgroup-unreads
8208           (gnus-sorted-intersection gnus-newsgroup-unreads
8209                                     gnus-newsgroup-limit))
8210     (if all
8211         (setq gnus-newsgroup-dormant nil
8212               gnus-newsgroup-marked nil
8213               gnus-newsgroup-reads
8214               (nconc
8215                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8216                gnus-newsgroup-reads))
8217       (while (setq article (pop articles))
8218         (unless (or (memq article gnus-newsgroup-dormant)
8219                     (memq article gnus-newsgroup-marked))
8220           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8221
8222 (defun gnus-summary-limit (articles &optional pop)
8223   (if pop
8224       ;; We pop the previous limit off the stack and use that.
8225       (setq articles (car gnus-newsgroup-limits)
8226             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8227     ;; We use the new limit, so we push the old limit on the stack.
8228     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8229   ;; Set the limit.
8230   (setq gnus-newsgroup-limit articles)
8231   (let ((total (length gnus-newsgroup-data))
8232         (data (gnus-data-find-list (gnus-summary-article-number)))
8233         (gnus-summary-mark-below nil)   ; Inhibit this.
8234         found)
8235     ;; This will do all the work of generating the new summary buffer
8236     ;; according to the new limit.
8237     (gnus-summary-prepare)
8238     ;; Hide any threads, possibly.
8239     (gnus-summary-maybe-hide-threads)
8240     ;; Try to return to the article you were at, or one in the
8241     ;; neighborhood.
8242     (when data
8243       ;; We try to find some article after the current one.
8244       (while data
8245         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8246           (setq data nil
8247                 found t))
8248         (setq data (cdr data))))
8249     (unless found
8250       ;; If there is no data, that means that we were after the last
8251       ;; article.  The same goes when we can't find any articles
8252       ;; after the current one.
8253       (goto-char (point-max))
8254       (gnus-summary-find-prev))
8255     (gnus-set-mode-line 'summary)
8256     ;; We return how many articles were removed from the summary
8257     ;; buffer as a result of the new limit.
8258     (- total (length gnus-newsgroup-data))))
8259
8260 (defsubst gnus-invisible-cut-children (threads)
8261   (let ((num 0))
8262     (while threads
8263       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8264         (incf num))
8265       (pop threads))
8266     (< num 2)))
8267
8268 (defsubst gnus-cut-thread (thread)
8269   "Go forwards in the thread until we find an article that we want to display."
8270   (when (or (eq gnus-fetch-old-headers 'some)
8271             (eq gnus-fetch-old-headers 'invisible)
8272             (numberp gnus-fetch-old-headers)
8273             (eq gnus-build-sparse-threads 'some)
8274             (eq gnus-build-sparse-threads 'more))
8275     ;; Deal with old-fetched headers and sparse threads.
8276     (while (and
8277             thread
8278             (or
8279              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8280              (gnus-summary-article-ancient-p
8281               (mail-header-number (car thread))))
8282             (if (or (<= (length (cdr thread)) 1)
8283                     (eq gnus-fetch-old-headers 'invisible))
8284                 (setq gnus-newsgroup-limit
8285                       (delq (mail-header-number (car thread))
8286                             gnus-newsgroup-limit)
8287                       thread (cadr thread))
8288               (when (gnus-invisible-cut-children (cdr thread))
8289                 (let ((th (cdr thread)))
8290                   (while th
8291                     (if (memq (mail-header-number (caar th))
8292                               gnus-newsgroup-limit)
8293                         (setq thread (car th)
8294                               th nil)
8295                       (setq th (cdr th))))))))))
8296   thread)
8297
8298 (defun gnus-cut-threads (threads)
8299   "Cut off all uninteresting articles from the beginning of THREADS."
8300   (when (or (eq gnus-fetch-old-headers 'some)
8301             (eq gnus-fetch-old-headers 'invisible)
8302             (numberp gnus-fetch-old-headers)
8303             (eq gnus-build-sparse-threads 'some)
8304             (eq gnus-build-sparse-threads 'more))
8305     (let ((th threads))
8306       (while th
8307         (setcar th (gnus-cut-thread (car th)))
8308         (setq th (cdr th)))))
8309   ;; Remove nixed out threads.
8310   (delq nil threads))
8311
8312 (defun gnus-summary-initial-limit (&optional show-if-empty)
8313   "Figure out what the initial limit is supposed to be on group entry.
8314 This entails weeding out unwanted dormants, low-scored articles,
8315 fetch-old-headers verbiage, and so on."
8316   ;; Most groups have nothing to remove.
8317   (if (or gnus-inhibit-limiting
8318           (and (null gnus-newsgroup-dormant)
8319                (eq gnus-newsgroup-display 'gnus-not-ignore)
8320                (not (eq gnus-fetch-old-headers 'some))
8321                (not (numberp gnus-fetch-old-headers))
8322                (not (eq gnus-fetch-old-headers 'invisible))
8323                (null gnus-summary-expunge-below)
8324                (not (eq gnus-build-sparse-threads 'some))
8325                (not (eq gnus-build-sparse-threads 'more))
8326                (null gnus-thread-expunge-below)
8327                (not gnus-use-nocem)))
8328       ()                                ; Do nothing.
8329     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8330     (setq gnus-newsgroup-limit nil)
8331     (mapatoms
8332      (lambda (node)
8333        (unless (car (symbol-value node))
8334          ;; These threads have no parents -- they are roots.
8335          (let ((nodes (cdr (symbol-value node)))
8336                thread)
8337            (while nodes
8338              (if (and gnus-thread-expunge-below
8339                       (< (gnus-thread-total-score (car nodes))
8340                          gnus-thread-expunge-below))
8341                  (gnus-expunge-thread (pop nodes))
8342                (setq thread (pop nodes))
8343                (gnus-summary-limit-children thread))))))
8344      gnus-newsgroup-dependencies)
8345     ;; If this limitation resulted in an empty group, we might
8346     ;; pop the previous limit and use it instead.
8347     (when (and (not gnus-newsgroup-limit)
8348                show-if-empty)
8349       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8350     gnus-newsgroup-limit))
8351
8352 (defun gnus-summary-limit-children (thread)
8353   "Return 1 if this subthread is visible and 0 if it is not."
8354   ;; First we get the number of visible children to this thread.  This
8355   ;; is done by recursing down the thread using this function, so this
8356   ;; will really go down to a leaf article first, before slowly
8357   ;; working its way up towards the root.
8358   (when thread
8359     (let* ((max-lisp-eval-depth 5000)
8360            (children
8361            (if (cdr thread)
8362                (apply '+ (mapcar 'gnus-summary-limit-children
8363                                  (cdr thread)))
8364              0))
8365           (number (mail-header-number (car thread)))
8366           score)
8367       (if (and
8368            (not (memq number gnus-newsgroup-marked))
8369            (or
8370             ;; If this article is dormant and has absolutely no visible
8371             ;; children, then this article isn't visible.
8372             (and (memq number gnus-newsgroup-dormant)
8373                  (zerop children))
8374             ;; If this is "fetch-old-headered" and there is no
8375             ;; visible children, then we don't want this article.
8376             (and (or (eq gnus-fetch-old-headers 'some)
8377                      (numberp gnus-fetch-old-headers))
8378                  (gnus-summary-article-ancient-p number)
8379                  (zerop children))
8380             ;; If this is "fetch-old-headered" and `invisible', then
8381             ;; we don't want this article.
8382             (and (eq gnus-fetch-old-headers 'invisible)
8383                  (gnus-summary-article-ancient-p number))
8384             ;; If this is a sparsely inserted article with no children,
8385             ;; we don't want it.
8386             (and (eq gnus-build-sparse-threads 'some)
8387                  (gnus-summary-article-sparse-p number)
8388                  (zerop children))
8389             ;; If we use expunging, and this article is really
8390             ;; low-scored, then we don't want this article.
8391             (when (and gnus-summary-expunge-below
8392                        (< (setq score
8393                                 (or (cdr (assq number gnus-newsgroup-scored))
8394                                     gnus-summary-default-score))
8395                           gnus-summary-expunge-below))
8396               ;; We increase the expunge-tally here, but that has
8397               ;; nothing to do with the limits, really.
8398               (incf gnus-newsgroup-expunged-tally)
8399               ;; We also mark as read here, if that's wanted.
8400               (when (and gnus-summary-mark-below
8401                          (< score gnus-summary-mark-below))
8402                 (setq gnus-newsgroup-unreads
8403                       (delq number gnus-newsgroup-unreads))
8404                 (if gnus-newsgroup-auto-expire
8405                     (push number gnus-newsgroup-expirable)
8406                   (push (cons number gnus-low-score-mark)
8407                         gnus-newsgroup-reads)))
8408               t)
8409             ;; Do the `display' group parameter.
8410             (and gnus-newsgroup-display
8411                  (not (funcall gnus-newsgroup-display)))
8412             ;; Check NoCeM things.
8413             (when (and gnus-use-nocem
8414                        (gnus-nocem-unwanted-article-p
8415                         (mail-header-id (car thread))))
8416               (setq gnus-newsgroup-unreads
8417                     (delq number gnus-newsgroup-unreads))
8418               t)))
8419           ;; Nope, invisible article.
8420           0
8421         ;; Ok, this article is to be visible, so we add it to the limit
8422         ;; and return 1.
8423         (push number gnus-newsgroup-limit)
8424         1))))
8425
8426 (defun gnus-expunge-thread (thread)
8427   "Mark all articles in THREAD as read."
8428   (let* ((number (mail-header-number (car thread))))
8429     (incf gnus-newsgroup-expunged-tally)
8430     ;; We also mark as read here, if that's wanted.
8431     (setq gnus-newsgroup-unreads
8432           (delq number gnus-newsgroup-unreads))
8433     (if gnus-newsgroup-auto-expire
8434         (push number gnus-newsgroup-expirable)
8435       (push (cons number gnus-low-score-mark)
8436             gnus-newsgroup-reads)))
8437   ;; Go recursively through all subthreads.
8438   (mapcar 'gnus-expunge-thread (cdr thread)))
8439
8440 ;; Summary article oriented commands
8441
8442 (defun gnus-summary-refer-parent-article (n)
8443   "Refer parent article N times.
8444 If N is negative, go to ancestor -N instead.
8445 The difference between N and the number of articles fetched is returned."
8446   (interactive "p")
8447   (let ((skip 1)
8448         error header ref)
8449     (when (not (natnump n))
8450       (setq skip (abs n)
8451             n 1))
8452     (while (and (> n 0)
8453                 (not error))
8454       (setq header (gnus-summary-article-header))
8455       (if (and (eq (mail-header-number header)
8456                    (cdr gnus-article-current))
8457                (equal gnus-newsgroup-name
8458                       (car gnus-article-current)))
8459           ;; If we try to find the parent of the currently
8460           ;; displayed article, then we take a look at the actual
8461           ;; References header, since this is slightly more
8462           ;; reliable than the References field we got from the
8463           ;; server.
8464           (save-excursion
8465             (set-buffer gnus-original-article-buffer)
8466             (nnheader-narrow-to-headers)
8467             (unless (setq ref (message-fetch-field "references"))
8468               (when (setq ref (message-fetch-field "in-reply-to"))
8469                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8470             (widen))
8471         (setq ref
8472               ;; It's not the current article, so we take a bet on
8473               ;; the value we got from the server.
8474               (mail-header-references header)))
8475       (if (and ref
8476                (not (equal ref "")))
8477           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8478             (gnus-message 1 "Couldn't find parent"))
8479         (gnus-message 1 "No references in article %d"
8480                       (gnus-summary-article-number))
8481         (setq error t))
8482       (decf n))
8483     (gnus-summary-position-point)
8484     n))
8485
8486 (defun gnus-summary-refer-references ()
8487   "Fetch all articles mentioned in the References header.
8488 Return the number of articles fetched."
8489   (interactive)
8490   (let ((ref (mail-header-references (gnus-summary-article-header)))
8491         (current (gnus-summary-article-number))
8492         (n 0))
8493     (if (or (not ref)
8494             (equal ref ""))
8495         (error "No References in the current article")
8496       ;; For each Message-ID in the References header...
8497       (while (string-match "<[^>]*>" ref)
8498         (incf n)
8499         ;; ... fetch that article.
8500         (gnus-summary-refer-article
8501          (prog1 (match-string 0 ref)
8502            (setq ref (substring ref (match-end 0))))))
8503       (gnus-summary-goto-subject current)
8504       (gnus-summary-position-point)
8505       n)))
8506
8507 (defun gnus-summary-refer-thread (&optional limit)
8508   "Fetch all articles in the current thread.
8509 If LIMIT (the numerical prefix), fetch that many old headers instead
8510 of what's specified by the `gnus-refer-thread-limit' variable."
8511   (interactive "P")
8512   (let ((id (mail-header-id (gnus-summary-article-header)))
8513         (limit (if limit (prefix-numeric-value limit)
8514                  gnus-refer-thread-limit)))
8515     (unless (eq gnus-fetch-old-headers 'invisible)
8516       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8517       ;; Retrieve the headers and read them in.
8518       (if (eq (if (numberp limit)
8519                   (gnus-retrieve-headers
8520                    (list (min
8521                           (+ (mail-header-number
8522                               (gnus-summary-article-header))
8523                              limit)
8524                           gnus-newsgroup-end))
8525                    gnus-newsgroup-name (* limit 2))
8526                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8527                 ;; headers.
8528                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8529                                        gnus-newsgroup-name limit))
8530               'nov)
8531           (gnus-build-all-threads)
8532         (error "Can't fetch thread from back ends that don't support NOV"))
8533       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8534     (gnus-summary-limit-include-thread id)))
8535
8536 (defun gnus-summary-refer-article (message-id)
8537   "Fetch an article specified by MESSAGE-ID."
8538   (interactive "sMessage-ID: ")
8539   (when (and (stringp message-id)
8540              (not (zerop (length message-id))))
8541     (setq message-id (gnus-replace-in-string message-id " " ""))
8542     ;; Construct the correct Message-ID if necessary.
8543     ;; Suggested by tale@pawl.rpi.edu.
8544     (unless (string-match "^<" message-id)
8545       (setq message-id (concat "<" message-id)))
8546     (unless (string-match ">$" message-id)
8547       (setq message-id (concat message-id ">")))
8548     ;; People often post MIDs from URLs, so unhex it:
8549     (unless (string-match "@" message-id)
8550       (setq message-id (gnus-url-unhex-string message-id)))
8551     (let* ((header (gnus-id-to-header message-id))
8552            (sparse (and header
8553                         (gnus-summary-article-sparse-p
8554                          (mail-header-number header))
8555                         (memq (mail-header-number header)
8556                               gnus-newsgroup-limit)))
8557            number)
8558       (cond
8559        ;; If the article is present in the buffer we just go to it.
8560        ((and header
8561              (or (not (gnus-summary-article-sparse-p
8562                        (mail-header-number header)))
8563                  sparse))
8564         (prog1
8565             (gnus-summary-goto-article
8566              (mail-header-number header) nil t)
8567           (when sparse
8568             (gnus-summary-update-article (mail-header-number header)))))
8569        (t
8570         ;; We fetch the article.
8571         (catch 'found
8572           (dolist (gnus-override-method (gnus-refer-article-methods))
8573             (when (and (gnus-check-server gnus-override-method)
8574                        ;; Fetch the header,
8575                        (setq number (gnus-summary-insert-subject message-id)))
8576               ;; and display the article.
8577               (gnus-summary-select-article nil nil nil number)
8578               (throw 'found t)))
8579           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8580
8581 (defun gnus-refer-article-methods ()
8582   "Return a list of referable methods."
8583   (cond
8584    ;; No method, so we default to current and native.
8585    ((null gnus-refer-article-method)
8586     (list gnus-current-select-method gnus-select-method))
8587    ;; Current.
8588    ((eq 'current gnus-refer-article-method)
8589     (list gnus-current-select-method))
8590    ;; List of select methods.
8591    ((not (and (symbolp (car gnus-refer-article-method))
8592               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8593     (let (out)
8594       (dolist (method gnus-refer-article-method)
8595         (push (if (eq 'current method)
8596                   gnus-current-select-method
8597                 method)
8598               out))
8599       (nreverse out)))
8600    ;; One single select method.
8601    (t
8602     (list gnus-refer-article-method))))
8603
8604 (defun gnus-summary-edit-parameters ()
8605   "Edit the group parameters of the current group."
8606   (interactive)
8607   (gnus-group-edit-group gnus-newsgroup-name 'params))
8608
8609 (defun gnus-summary-customize-parameters ()
8610   "Customize the group parameters of the current group."
8611   (interactive)
8612   (gnus-group-customize gnus-newsgroup-name))
8613
8614 (defun gnus-summary-enter-digest-group (&optional force)
8615   "Enter an nndoc group based on the current article.
8616 If FORCE, force a digest interpretation.  If not, try
8617 to guess what the document format is."
8618   (interactive "P")
8619   (let ((conf gnus-current-window-configuration))
8620     (save-window-excursion
8621       (save-excursion
8622         (let (gnus-article-prepare-hook
8623               gnus-display-mime-function
8624               gnus-break-pages)
8625           (gnus-summary-select-article))))
8626     (setq gnus-current-window-configuration conf)
8627     (let* ((name (format "%s-%d"
8628                          (gnus-group-prefixed-name
8629                           gnus-newsgroup-name (list 'nndoc ""))
8630                          (with-current-buffer gnus-summary-buffer
8631                            gnus-current-article)))
8632            (ogroup gnus-newsgroup-name)
8633            (params (append (gnus-info-params (gnus-get-info ogroup))
8634                            (list (cons 'to-group ogroup))
8635                            (list (cons 'parent-group ogroup))
8636                            (list (cons 'save-article-group ogroup))))
8637            (case-fold-search t)
8638            (buf (current-buffer))
8639            dig to-address)
8640       (save-excursion
8641         (set-buffer gnus-original-article-buffer)
8642         ;; Have the digest group inherit the main mail address of
8643         ;; the parent article.
8644         (when (setq to-address (or (gnus-fetch-field "reply-to")
8645                                    (gnus-fetch-field "from")))
8646           (setq params
8647                 (append
8648                  (list (cons 'to-address
8649                              (funcall gnus-decode-encoded-address-function
8650                                       to-address))))))
8651         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8652         (insert-buffer-substring gnus-original-article-buffer)
8653         ;; Remove lines that may lead nndoc to misinterpret the
8654         ;; document type.
8655         (narrow-to-region
8656          (goto-char (point-min))
8657          (or (search-forward "\n\n" nil t) (point)))
8658         (goto-char (point-min))
8659         (delete-matching-lines "^Path:\\|^From ")
8660         (widen))
8661       (unwind-protect
8662           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8663                     (gnus-newsgroup-ephemeral-ignored-charsets
8664                      gnus-newsgroup-ignored-charsets))
8665                 (gnus-group-read-ephemeral-group
8666                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8667                               (nndoc-article-type
8668                                ,(if force 'mbox 'guess)))
8669                  t nil nil nil
8670                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8671                                                         "ADAPT")))))
8672               ;; Make all postings to this group go to the parent group.
8673               (nconc (gnus-info-params (gnus-get-info name))
8674                      params)
8675             ;; Couldn't select this doc group.
8676             (switch-to-buffer buf)
8677             (gnus-set-global-variables)
8678             (gnus-configure-windows 'summary)
8679             (gnus-message 3 "Article couldn't be entered?"))
8680         (kill-buffer dig)))))
8681
8682 (defun gnus-summary-read-document (n)
8683   "Open a new group based on the current article(s).
8684 This will allow you to read digests and other similar
8685 documents as newsgroups.
8686 Obeys the standard process/prefix convention."
8687   (interactive "P")
8688   (let* ((ogroup gnus-newsgroup-name)
8689          (params (append (gnus-info-params (gnus-get-info ogroup))
8690                          (list (cons 'to-group ogroup))))
8691          group egroup groups vgroup)
8692     (dolist (article (gnus-summary-work-articles n))
8693       (setq group (format "%s-%d" gnus-newsgroup-name article))
8694       (gnus-summary-remove-process-mark article)
8695       (when (gnus-summary-display-article article)
8696         (save-excursion
8697           (with-temp-buffer
8698             (insert-buffer-substring gnus-original-article-buffer)
8699             ;; Remove some headers that may lead nndoc to make
8700             ;; the wrong guess.
8701             (message-narrow-to-head)
8702             (goto-char (point-min))
8703             (delete-matching-lines "^Path:\\|^From ")
8704             (widen)
8705             (if (setq egroup
8706                       (gnus-group-read-ephemeral-group
8707                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8708                                      (nndoc-article-type guess))
8709                        t nil t))
8710                 (progn
8711             ;; Make all postings to this group go to the parent group.
8712                   (nconc (gnus-info-params (gnus-get-info egroup))
8713                          params)
8714                   (push egroup groups))
8715               ;; Couldn't select this doc group.
8716               (gnus-error 3 "Article couldn't be entered"))))))
8717     ;; Now we have selected all the documents.
8718     (cond
8719      ((not groups)
8720       (error "None of the articles could be interpreted as documents"))
8721      ((gnus-group-read-ephemeral-group
8722        (setq vgroup (format
8723                      "nnvirtual:%s-%s" gnus-newsgroup-name
8724                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8725        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8726        t
8727        (cons (current-buffer) 'summary)))
8728      (t
8729       (error "Couldn't select virtual nndoc group")))))
8730
8731 (defun gnus-summary-isearch-article (&optional regexp-p)
8732   "Do incremental search forward on the current article.
8733 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8734   (interactive "P")
8735   (gnus-summary-select-article)
8736   (gnus-configure-windows 'article)
8737   (gnus-eval-in-buffer-window gnus-article-buffer
8738     (save-restriction
8739       (widen)
8740       (isearch-forward regexp-p))))
8741
8742 (defun gnus-summary-repeat-search-article-forward ()
8743   "Repeat the previous search forwards."
8744   (interactive)
8745   (unless gnus-last-search-regexp
8746     (error "No previous search"))
8747   (gnus-summary-search-article-forward gnus-last-search-regexp))
8748
8749 (defun gnus-summary-repeat-search-article-backward ()
8750   "Repeat the previous search backwards."
8751   (interactive)
8752   (unless gnus-last-search-regexp
8753     (error "No previous search"))
8754   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8755
8756 (defun gnus-summary-search-article-forward (regexp &optional backward)
8757   "Search for an article containing REGEXP forward.
8758 If BACKWARD, search backward instead."
8759   (interactive
8760    (list (read-string
8761           (format "Search article %s (regexp%s): "
8762                   (if current-prefix-arg "backward" "forward")
8763                   (if gnus-last-search-regexp
8764                       (concat ", default " gnus-last-search-regexp)
8765                     "")))
8766          current-prefix-arg))
8767   (if (string-equal regexp "")
8768       (setq regexp (or gnus-last-search-regexp ""))
8769     (setq gnus-last-search-regexp regexp)
8770     (setq gnus-article-before-search gnus-current-article))
8771   ;; Intentionally set gnus-last-article.
8772   (setq gnus-last-article gnus-article-before-search)
8773   (let ((gnus-last-article gnus-last-article))
8774     (if (gnus-summary-search-article regexp backward)
8775         (gnus-summary-show-thread)
8776       (signal 'search-failed (list regexp)))))
8777
8778 (defun gnus-summary-search-article-backward (regexp)
8779   "Search for an article containing REGEXP backward."
8780   (interactive
8781    (list (read-string
8782           (format "Search article backward (regexp%s): "
8783                   (if gnus-last-search-regexp
8784                       (concat ", default " gnus-last-search-regexp)
8785                     "")))))
8786   (gnus-summary-search-article-forward regexp 'backward))
8787
8788 (defun gnus-summary-search-article (regexp &optional backward)
8789   "Search for an article containing REGEXP.
8790 Optional argument BACKWARD means do search for backward.
8791 `gnus-select-article-hook' is not called during the search."
8792   ;; We have to require this here to make sure that the following
8793   ;; dynamic binding isn't shadowed by autoloading.
8794   (require 'gnus-async)
8795   (require 'gnus-art)
8796   (let ((gnus-select-article-hook nil)  ;Disable hook.
8797         (gnus-article-prepare-hook nil)
8798         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8799         (gnus-use-article-prefetch nil)
8800         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8801         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8802         (gnus-visual nil)
8803         (gnus-keep-backlog nil)
8804         (gnus-break-pages nil)
8805         (gnus-summary-display-arrow nil)
8806         (gnus-updated-mode-lines nil)
8807         (gnus-auto-center-summary nil)
8808         (sum (current-buffer))
8809         (gnus-display-mime-function nil)
8810         (found nil)
8811         point)
8812     (gnus-save-hidden-threads
8813       (gnus-summary-select-article)
8814       (set-buffer gnus-article-buffer)
8815       (goto-char (window-point (get-buffer-window (current-buffer))))
8816       (when backward
8817         (forward-line -1))
8818       (while (not found)
8819         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8820         (if (if backward
8821                 (re-search-backward regexp nil t)
8822               (re-search-forward regexp nil t))
8823             ;; We found the regexp.
8824             (progn
8825               (setq found 'found)
8826               (beginning-of-line)
8827               (set-window-start
8828                (get-buffer-window (current-buffer))
8829                (point))
8830               (forward-line 1)
8831               (set-window-point
8832                (get-buffer-window (current-buffer))
8833                (point))
8834               (set-buffer sum)
8835               (setq point (point)))
8836           ;; We didn't find it, so we go to the next article.
8837           (set-buffer sum)
8838           (setq found 'not)
8839           (while (eq found 'not)
8840             (if (not (if backward (gnus-summary-find-prev)
8841                        (gnus-summary-find-next)))
8842                 ;; No more articles.
8843                 (setq found t)
8844               ;; Select the next article and adjust point.
8845               (unless (gnus-summary-article-sparse-p
8846                        (gnus-summary-article-number))
8847                 (setq found nil)
8848                 (gnus-summary-select-article)
8849                 (set-buffer gnus-article-buffer)
8850                 (widen)
8851                 (goto-char (if backward (point-max) (point-min))))))))
8852       (gnus-message 7 ""))
8853     ;; Return whether we found the regexp.
8854     (when (eq found 'found)
8855       (goto-char point)
8856       (gnus-summary-show-thread)
8857       (gnus-summary-goto-subject gnus-current-article)
8858       (gnus-summary-position-point)
8859       t)))
8860
8861 (defun gnus-find-matching-articles (header regexp)
8862   "Return a list of all articles that match REGEXP on HEADER.
8863 This search includes all articles in the current group that Gnus has
8864 fetched headers for, whether they are displayed or not."
8865   (let ((articles nil)
8866         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8867         (case-fold-search t))
8868     (dolist (header gnus-newsgroup-headers)
8869       (when (string-match regexp (funcall func header))
8870         (push (mail-header-number header) articles)))
8871     (nreverse articles)))
8872
8873 (defun gnus-summary-find-matching (header regexp &optional backward unread
8874                                           not-case-fold not-matching)
8875   "Return a list of all articles that match REGEXP on HEADER.
8876 The search stars on the current article and goes forwards unless
8877 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8878 If UNREAD is non-nil, only unread articles will
8879 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8880 in the comparisons. If NOT-MATCHING, return a list of all articles that
8881 not match REGEXP on HEADER."
8882   (let ((case-fold-search (not not-case-fold))
8883         articles d func)
8884     (if (consp header)
8885         (if (eq (car header) 'extra)
8886             (setq func
8887                   `(lambda (h)
8888                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8889                          "")))
8890           (error "%s is an invalid header" header))
8891       (unless (fboundp (intern (concat "mail-header-" header)))
8892         (error "%s is not a valid header" header))
8893       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8894     (dolist (d (if (eq backward 'all)
8895                    gnus-newsgroup-data
8896                  (gnus-data-find-list
8897                   (gnus-summary-article-number)
8898                   (gnus-data-list backward))))
8899       (when (and (or (not unread)       ; We want all articles...
8900                      (gnus-data-unread-p d)) ; Or just unreads.
8901                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8902                  (if not-matching
8903                      (not (string-match
8904                            regexp
8905                            (funcall func (gnus-data-header d))))
8906                    (string-match regexp
8907                                  (funcall func (gnus-data-header d)))))
8908         (push (gnus-data-number d) articles))) ; Success!
8909     (nreverse articles)))
8910
8911 (defun gnus-summary-execute-command (header regexp command &optional backward)
8912   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8913 If HEADER is an empty string (or nil), the match is done on the entire
8914 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8915   (interactive
8916    (list (let ((completion-ignore-case t))
8917            (completing-read
8918             "Header name: "
8919             (mapcar (lambda (header) (list (format "%s" header)))
8920                     (append
8921                      '("Number" "Subject" "From" "Lines" "Date"
8922                        "Message-ID" "Xref" "References" "Body")
8923                      gnus-extra-headers))
8924             nil 'require-match))
8925          (read-string "Regexp: ")
8926          (read-key-sequence "Command: ")
8927          current-prefix-arg))
8928   (when (equal header "Body")
8929     (setq header ""))
8930   ;; Hidden thread subtrees must be searched as well.
8931   (gnus-summary-show-all-threads)
8932   ;; We don't want to change current point nor window configuration.
8933   (save-excursion
8934     (save-window-excursion
8935       (let (gnus-visual
8936             gnus-treat-strip-trailing-blank-lines
8937             gnus-treat-strip-leading-blank-lines
8938             gnus-treat-strip-multiple-blank-lines
8939             gnus-treat-hide-boring-headers
8940             gnus-treat-fold-newsgroups
8941             gnus-article-prepare-hook)
8942         (gnus-message 6 "Executing %s..." (key-description command))
8943         ;; We'd like to execute COMMAND interactively so as to give arguments.
8944         (gnus-execute header regexp
8945                       `(call-interactively ',(key-binding command))
8946                       backward)
8947         (gnus-message 6 "Executing %s...done" (key-description command))))))
8948
8949 (defun gnus-summary-beginning-of-article ()
8950   "Scroll the article back to the beginning."
8951   (interactive)
8952   (gnus-summary-select-article)
8953   (gnus-configure-windows 'article)
8954   (gnus-eval-in-buffer-window gnus-article-buffer
8955     (widen)
8956     (goto-char (point-min))
8957     (when gnus-break-pages
8958       (gnus-narrow-to-page))))
8959
8960 (defun gnus-summary-end-of-article ()
8961   "Scroll to the end of the article."
8962   (interactive)
8963   (gnus-summary-select-article)
8964   (gnus-configure-windows 'article)
8965   (gnus-eval-in-buffer-window gnus-article-buffer
8966     (widen)
8967     (goto-char (point-max))
8968     (recenter -3)
8969     (when gnus-break-pages
8970       (when (re-search-backward page-delimiter nil t)
8971         (narrow-to-region (match-end 0) (point-max)))
8972       (gnus-narrow-to-page))))
8973
8974 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8975   "Truncate to LEN and quote all \"(\"'s in STRING."
8976   (gnus-replace-in-string (if (and len (> (length string) len))
8977                               (substring string 0 len)
8978                             string)
8979                           "[()]" "\\\\\\&"))
8980
8981 (defun gnus-summary-print-article (&optional filename n)
8982   "Generate and print a PostScript image of the process-marked (mail) articles.
8983
8984 If used interactively, print the current article if none are
8985 process-marked.  With prefix arg, prompt the user for the name of the
8986 file to save in.
8987
8988 When used from Lisp, accept two optional args FILENAME and N.  N means
8989 to print the next N articles.  If N is negative, print the N previous
8990 articles.  If N is nil and articles have been marked with the process
8991 mark, print these instead.
8992
8993 If the optional first argument FILENAME is nil, send the image to the
8994 printer.  If FILENAME is a string, save the PostScript image in a file with
8995 that name.  If FILENAME is a number, prompt the user for the name of the file
8996 to save in."
8997   (interactive (list (ps-print-preprint current-prefix-arg)))
8998   (dolist (article (gnus-summary-work-articles n))
8999     (gnus-summary-select-article nil nil 'pseudo article)
9000     (gnus-eval-in-buffer-window gnus-article-buffer
9001       (gnus-print-buffer))
9002     (gnus-summary-remove-process-mark article))
9003   (ps-despool filename))
9004
9005 (defun gnus-print-buffer ()
9006   (let ((buffer (generate-new-buffer " *print*")))
9007     (unwind-protect
9008         (progn
9009           (copy-to-buffer buffer (point-min) (point-max))
9010           (set-buffer buffer)
9011           (gnus-remove-text-with-property 'gnus-decoration)
9012           (when (gnus-visual-p 'article-highlight 'highlight)
9013             ;; Copy-to-buffer doesn't copy overlay.  So redo
9014             ;; highlight.
9015             (let ((gnus-article-buffer buffer))
9016               (gnus-article-highlight-citation t)
9017               (gnus-article-highlight-signature)
9018               (gnus-article-emphasize)
9019               (gnus-article-delete-invisible-text)))
9020           (let ((ps-left-header
9021                  (list
9022                   (concat "("
9023                           (gnus-summary-print-truncate-and-quote
9024                            (mail-header-subject gnus-current-headers)
9025                            66) ")")
9026                   (concat "("
9027                           (gnus-summary-print-truncate-and-quote
9028                            (mail-header-from gnus-current-headers)
9029                            45) ")")))
9030                 (ps-right-header
9031                  (list
9032                   "/pagenumberstring load"
9033                   (concat "("
9034                           (mail-header-date gnus-current-headers) ")"))))
9035             (gnus-run-hooks 'gnus-ps-print-hook)
9036             (save-excursion
9037               (if window-system
9038                   (ps-spool-buffer-with-faces)
9039                 (ps-spool-buffer)))))
9040       (kill-buffer buffer))))
9041
9042 (defun gnus-summary-show-article (&optional arg)
9043   "Force redisplaying of the current article.
9044 If ARG (the prefix) is a number, show the article with the charset
9045 defined in `gnus-summary-show-article-charset-alist', or the charset
9046 input.
9047 If ARG (the prefix) is non-nil and not a number, show the raw article
9048 without any article massaging functions being run.  Normally, the key
9049 strokes are `C-u g'."
9050   (interactive "P")
9051   (cond
9052    ((numberp arg)
9053     (gnus-summary-show-article t)
9054     (let ((gnus-newsgroup-charset
9055            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9056                (mm-read-coding-system
9057                 "View as charset: " ;; actually it is coding system.
9058                 (with-current-buffer gnus-article-buffer
9059                   (mm-detect-coding-region (point) (point-max))))))
9060           (gnus-newsgroup-ignored-charsets 'gnus-all))
9061       (gnus-summary-select-article nil 'force)
9062       (let ((deps gnus-newsgroup-dependencies)
9063             head header lines)
9064         (save-excursion
9065           (set-buffer gnus-original-article-buffer)
9066           (save-restriction
9067             (message-narrow-to-head)
9068             (setq head (buffer-string))
9069             (goto-char (point-min))
9070             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9071               (goto-char (point-max))
9072               (widen)
9073               (setq lines (1- (count-lines (point) (point-max))))))
9074           (with-temp-buffer
9075             (insert (format "211 %d Article retrieved.\n"
9076                             (cdr gnus-article-current)))
9077             (insert head)
9078             (if lines (insert (format "Lines: %d\n" lines)))
9079             (insert ".\n")
9080             (let ((nntp-server-buffer (current-buffer)))
9081               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9082         (gnus-data-set-header
9083          (gnus-data-find (cdr gnus-article-current))
9084          header)
9085         (gnus-summary-update-article-line
9086          (cdr gnus-article-current) header)
9087         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9088           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9089    ((not arg)
9090     ;; Select the article the normal way.
9091     (gnus-summary-select-article nil 'force))
9092    (t
9093     ;; We have to require this here to make sure that the following
9094     ;; dynamic binding isn't shadowed by autoloading.
9095     (require 'gnus-async)
9096     (require 'gnus-art)
9097     ;; Bind the article treatment functions to nil.
9098     (let ((gnus-have-all-headers t)
9099           gnus-article-prepare-hook
9100           gnus-article-decode-hook
9101           gnus-display-mime-function
9102           gnus-break-pages)
9103       ;; Destroy any MIME parts.
9104       (when (gnus-buffer-live-p gnus-article-buffer)
9105         (save-excursion
9106           (set-buffer gnus-article-buffer)
9107           (mm-destroy-parts gnus-article-mime-handles)
9108           ;; Set it to nil for safety reason.
9109           (setq gnus-article-mime-handle-alist nil)
9110           (setq gnus-article-mime-handles nil)))
9111       (gnus-summary-select-article nil 'force))))
9112   (gnus-summary-goto-subject gnus-current-article)
9113   (gnus-summary-position-point))
9114
9115 (defun gnus-summary-show-raw-article ()
9116   "Show the raw article without any article massaging functions being run."
9117   (interactive)
9118   (gnus-summary-show-article t))
9119
9120 (defun gnus-summary-verbose-headers (&optional arg)
9121   "Toggle permanent full header display.
9122 If ARG is a positive number, turn header display on.
9123 If ARG is a negative number, turn header display off."
9124   (interactive "P")
9125   (setq gnus-show-all-headers
9126         (cond ((or (not (numberp arg))
9127                    (zerop arg))
9128                (not gnus-show-all-headers))
9129               ((natnump arg)
9130                t)))
9131   (gnus-summary-show-article))
9132
9133 (defun gnus-summary-toggle-header (&optional arg)
9134   "Show the headers if they are hidden, or hide them if they are shown.
9135 If ARG is a positive number, show the entire header.
9136 If ARG is a negative number, hide the unwanted header lines."
9137   (interactive "P")
9138   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9139                      (get-buffer-window gnus-article-buffer t))))
9140     (with-current-buffer gnus-article-buffer
9141       (widen)
9142       (article-narrow-to-head)
9143       (let* ((buffer-read-only nil)
9144              (inhibit-point-motion-hooks t)
9145              (hidden (if (numberp arg)
9146                          (>= arg 0)
9147                        (or (not (looking-at "[^ \t\n]+:"))
9148                            (gnus-article-hidden-text-p 'headers))))
9149              s e)
9150         (delete-region (point-min) (point-max))
9151         (with-current-buffer gnus-original-article-buffer
9152           (goto-char (setq s (point-min)))
9153           (setq e (if (search-forward "\n\n" nil t)
9154                       (1- (point))
9155                     (point-max))))
9156         (insert-buffer-substring gnus-original-article-buffer s e)
9157         (run-hooks 'gnus-article-decode-hook)
9158         (if hidden
9159             (let ((gnus-treat-hide-headers nil)
9160                   (gnus-treat-hide-boring-headers nil))
9161               (gnus-delete-wash-type 'headers)
9162               (gnus-treat-article 'head))
9163           (gnus-treat-article 'head))
9164         (widen)
9165         (if window
9166             (set-window-start window (goto-char (point-min))))
9167         (if gnus-break-pages
9168             (gnus-narrow-to-page)
9169           (when (gnus-visual-p 'page-marker)
9170             (let ((buffer-read-only nil))
9171               (gnus-remove-text-with-property 'gnus-prev)
9172               (gnus-remove-text-with-property 'gnus-next))))
9173         (gnus-set-mode-line 'article)))))
9174
9175 (defun gnus-summary-show-all-headers ()
9176   "Make all header lines visible."
9177   (interactive)
9178   (gnus-summary-toggle-header 1))
9179
9180 (defun gnus-summary-caesar-message (&optional arg)
9181   "Caesar rotate the current article by 13.
9182 With a non-numerical prefix, also rotate headers.  A numerical
9183 prefix specifies how many places to rotate each letter forward."
9184   (interactive "P")
9185   (gnus-summary-select-article)
9186   (let ((mail-header-separator ""))
9187     (gnus-eval-in-buffer-window gnus-article-buffer
9188       (save-restriction
9189         (widen)
9190         (let ((start (window-start))
9191               buffer-read-only)
9192           (if (equal arg '(4))
9193               (message-caesar-buffer-body nil t)
9194             (message-caesar-buffer-body arg))
9195           (set-window-start (get-buffer-window (current-buffer)) start)))))
9196   ;; Create buttons and stuff...
9197   (gnus-treat-article nil))
9198
9199 (defun gnus-summary-idna-message (&optional arg)
9200   "Decode IDNA encoded domain names in the current articles.
9201 IDNA encoded domain names looks like `xn--bar'.  If a string
9202 remain unencoded after running this function, it is likely an
9203 invalid IDNA string (`xn--bar' is invalid).
9204
9205 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9206 installed for this command to work."
9207   (interactive "P")
9208   (if (not (and (condition-case nil (require 'idna)
9209                   (file-error))
9210                 (mm-coding-system-p 'utf-8)
9211                 (executable-find (symbol-value 'idna-program))))
9212       (gnus-message
9213        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9214     (gnus-summary-select-article)
9215     (let ((mail-header-separator ""))
9216       (gnus-eval-in-buffer-window gnus-article-buffer
9217         (save-restriction
9218           (widen)
9219           (let ((start (window-start))
9220                 buffer-read-only)
9221             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9222               (replace-match (idna-to-unicode (match-string 1))))
9223             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9224
9225 (defun gnus-summary-morse-message (&optional arg)
9226   "Morse decode the current article."
9227   (interactive "P")
9228   (gnus-summary-select-article)
9229   (let ((mail-header-separator ""))
9230     (gnus-eval-in-buffer-window gnus-article-buffer
9231       (save-excursion
9232         (save-restriction
9233           (widen)
9234           (let ((pos (window-start))
9235                 buffer-read-only)
9236             (goto-char (point-min))
9237             (when (message-goto-body)
9238               (gnus-narrow-to-body))
9239             (goto-char (point-min))
9240             (while (search-forward "·" (point-max) t)
9241               (replace-match "."))
9242             (unmorse-region (point-min) (point-max))
9243             (widen)
9244             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9245
9246 (defun gnus-summary-stop-page-breaking ()
9247   "Stop page breaking in the current article."
9248   (interactive)
9249   (gnus-summary-select-article)
9250   (gnus-eval-in-buffer-window gnus-article-buffer
9251     (widen)
9252     (when (gnus-visual-p 'page-marker)
9253       (let ((buffer-read-only nil))
9254         (gnus-remove-text-with-property 'gnus-prev)
9255         (gnus-remove-text-with-property 'gnus-next))
9256       (setq gnus-page-broken nil))))
9257
9258 (defun gnus-summary-move-article (&optional n to-newsgroup
9259                                             select-method action)
9260   "Move the current article to a different newsgroup.
9261 If N is a positive number, move the N next articles.
9262 If N is a negative number, move the N previous articles.
9263 If N is nil and any articles have been marked with the process mark,
9264 move those articles instead.
9265 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9266 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9267 re-spool using this method.
9268
9269 When called interactively with TO-NEWSGROUP being nil, the value of
9270 the variable `gnus-move-split-methods' is used for finding a default
9271 for the target newsgroup.
9272
9273 For this function to work, both the current newsgroup and the
9274 newsgroup that you want to move to have to support the `request-move'
9275 and `request-accept' functions.
9276
9277 ACTION can be either `move' (the default), `crosspost' or `copy'."
9278   (interactive "P")
9279   (unless action
9280     (setq action 'move))
9281   ;; Check whether the source group supports the required functions.
9282   (cond ((and (eq action 'move)
9283               (not (gnus-check-backend-function
9284                     'request-move-article gnus-newsgroup-name)))
9285          (error "The current group does not support article moving"))
9286         ((and (eq action 'crosspost)
9287               (not (gnus-check-backend-function
9288                     'request-replace-article gnus-newsgroup-name)))
9289          (error "The current group does not support article editing")))
9290   (let ((articles (gnus-summary-work-articles n))
9291         (prefix (if (gnus-check-backend-function
9292                      'request-move-article gnus-newsgroup-name)
9293                     (funcall gnus-move-group-prefix-function
9294                              gnus-newsgroup-name)
9295                   ""))
9296         (names '((move "Move" "Moving")
9297                  (copy "Copy" "Copying")
9298                  (crosspost "Crosspost" "Crossposting")))
9299         (copy-buf (save-excursion
9300                     (nnheader-set-temp-buffer " *copy article*")))
9301         art-group to-method new-xref article to-groups articles-to-update-marks)
9302     (unless (assq action names)
9303       (error "Unknown action %s" action))
9304     ;; Read the newsgroup name.
9305     (when (and (not to-newsgroup)
9306                (not select-method))
9307       (if (and gnus-move-split-methods
9308                (not
9309                 (and (memq gnus-current-article articles)
9310                      (gnus-buffer-live-p gnus-original-article-buffer))))
9311           ;; When `gnus-move-split-methods' is non-nil, we have to
9312           ;; select an article to give `gnus-read-move-group-name' an
9313           ;; opportunity to suggest an appropriate default.  However,
9314           ;; we needn't render or mark the article.
9315           (let ((gnus-display-mime-function nil)
9316                 (gnus-article-prepare-hook nil)
9317                 (gnus-mark-article-hook nil))
9318             (gnus-summary-select-article nil nil nil (car articles))))
9319       (setq to-newsgroup
9320             (gnus-read-move-group-name
9321              (cadr (assq action names))
9322              (symbol-value (intern (format "gnus-current-%s-group" action)))
9323              articles prefix))
9324       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9325     (setq to-method (or select-method
9326                         (gnus-server-to-method
9327                          (gnus-group-method to-newsgroup))))
9328     ;; Check the method we are to move this article to...
9329     (unless (gnus-check-backend-function
9330              'request-accept-article (car to-method))
9331       (error "%s does not support article copying" (car to-method)))
9332     (unless (gnus-check-server to-method)
9333       (error "Can't open server %s" (car to-method)))
9334     (gnus-message 6 "%s to %s: %s..."
9335                   (caddr (assq action names))
9336                   (or (car select-method) to-newsgroup) articles)
9337     (while articles
9338       (setq article (pop articles))
9339       (setq
9340        art-group
9341        (cond
9342         ;; Move the article.
9343         ((eq action 'move)
9344          ;; Remove this article from future suppression.
9345          (gnus-dup-unsuppress-article article)
9346          (let* ((from-method (gnus-find-method-for-group
9347                               gnus-newsgroup-name))
9348                 (to-method (gnus-find-method-for-group
9349                             to-newsgroup))
9350                 (move-is-internal (gnus-method-equal from-method to-method)))
9351            (gnus-request-move-article
9352             article                     ; Article to move
9353             gnus-newsgroup-name         ; From newsgroup
9354             (nth 1 (gnus-find-method-for-group
9355                     gnus-newsgroup-name)) ; Server
9356             (list 'gnus-request-accept-article
9357                   to-newsgroup (list 'quote select-method)
9358                   (not articles) t)     ; Accept form
9359             (not articles)              ; Only save nov last time
9360             move-is-internal)))         ; is this move internal?
9361         ;; Copy the article.
9362         ((eq action 'copy)
9363          (save-excursion
9364            (set-buffer copy-buf)
9365            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9366              (save-restriction
9367                (nnheader-narrow-to-headers)
9368                (dolist (hdr gnus-copy-article-ignored-headers)
9369                  (message-remove-header hdr t)))
9370              (gnus-request-accept-article
9371               to-newsgroup select-method (not articles) t))))
9372         ;; Crosspost the article.
9373         ((eq action 'crosspost)
9374          (let ((xref (message-tokenize-header
9375                       (mail-header-xref (gnus-summary-article-header article))
9376                       " ")))
9377            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9378                                   ":" (number-to-string article)))
9379            (unless xref
9380              (setq xref (list (system-name))))
9381            (setq new-xref
9382                  (concat
9383                   (mapconcat 'identity
9384                              (delete "Xref:" (delete new-xref xref))
9385                              " ")
9386                   " " new-xref))
9387            (save-excursion
9388              (set-buffer copy-buf)
9389              ;; First put the article in the destination group.
9390              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9391              (when (consp (setq art-group
9392                                 (gnus-request-accept-article
9393                                  to-newsgroup select-method (not articles) t)))
9394                (setq new-xref (concat new-xref " " (car art-group)
9395                                       ":"
9396                                       (number-to-string (cdr art-group))))
9397                ;; Now we have the new Xrefs header, so we insert
9398                ;; it and replace the new article.
9399                (nnheader-replace-header "Xref" new-xref)
9400                (gnus-request-replace-article
9401                 (cdr art-group) to-newsgroup (current-buffer) t)
9402                art-group))))))
9403       (cond
9404        ((not art-group)
9405         (gnus-message 1 "Couldn't %s article %s: %s"
9406                       (cadr (assq action names)) article
9407                       (nnheader-get-report (car to-method))))
9408        ((eq art-group 'junk)
9409         (when (eq action 'move)
9410           (gnus-summary-mark-article article gnus-canceled-mark)
9411           (gnus-message 4 "Deleted article %s" article)
9412           ;; run the delete hook
9413           (run-hook-with-args 'gnus-summary-article-delete-hook
9414                               action
9415                               (gnus-data-header
9416                                (assoc article (gnus-data-list nil)))
9417                               gnus-newsgroup-name nil
9418                               select-method)))
9419        (t
9420         (let* ((pto-group (gnus-group-prefixed-name
9421                            (car art-group) to-method))
9422                (info (gnus-get-info pto-group))
9423                (to-group (gnus-info-group info))
9424                to-marks)
9425           ;; Update the group that has been moved to.
9426           (when (and info
9427                      (memq action '(move copy)))
9428             (unless (member to-group to-groups)
9429               (push to-group to-groups))
9430
9431             (unless (memq article gnus-newsgroup-unreads)
9432               (push 'read to-marks)
9433               (gnus-info-set-read
9434                info (gnus-add-to-range (gnus-info-read info)
9435                                        (list (cdr art-group)))))
9436
9437             ;; See whether the article is to be put in the cache.
9438             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9439                              gnus-article-mark-lists
9440                            (delete '(expirable . expire)
9441                                    (copy-sequence gnus-article-mark-lists))))
9442                   (to-article (cdr art-group)))
9443
9444               ;; Enter the article into the cache in the new group,
9445               ;; if that is required.
9446               (when gnus-use-cache
9447                 (gnus-cache-possibly-enter-article
9448                  to-group to-article
9449                  (memq article gnus-newsgroup-marked)
9450                  (memq article gnus-newsgroup-dormant)
9451                  (memq article gnus-newsgroup-unreads)))
9452
9453               (when gnus-preserve-marks
9454                 ;; Copy any marks over to the new group.
9455                 (when (and (equal to-group gnus-newsgroup-name)
9456                            (not (memq article gnus-newsgroup-unreads)))
9457                   ;; Mark this article as read in this group.
9458                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9459                   (setcdr (gnus-active to-group) to-article)
9460                   (setcdr gnus-newsgroup-active to-article))
9461
9462                 (while marks
9463                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9464                     (when (memq article (symbol-value
9465                                          (intern (format "gnus-newsgroup-%s"
9466                                                          (caar marks)))))
9467                       (push (cdar marks) to-marks)
9468                       ;; If the other group is the same as this group,
9469                       ;; then we have to add the mark to the list.
9470                       (when (equal to-group gnus-newsgroup-name)
9471                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9472                              (cons to-article
9473                                    (symbol-value
9474                                     (intern (format "gnus-newsgroup-%s"
9475                                                     (caar marks)))))))
9476                       ;; Copy the marks to other group.
9477                       (gnus-add-marked-articles
9478                        to-group (cdar marks) (list to-article) info)))
9479                   (setq marks (cdr marks)))
9480
9481                 (gnus-request-set-mark
9482                  to-group (list (list (list to-article) 'add to-marks))))
9483
9484               (gnus-dribble-enter
9485                (concat "(gnus-group-set-info '"
9486                        (gnus-prin1-to-string (gnus-get-info to-group))
9487                        ")"))))
9488
9489           ;; Update the Xref header in this article to point to
9490           ;; the new crossposted article we have just created.
9491           (when (eq action 'crosspost)
9492             (save-excursion
9493               (set-buffer copy-buf)
9494               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9495               (nnheader-replace-header "Xref" new-xref)
9496               (gnus-request-replace-article
9497                article gnus-newsgroup-name (current-buffer) t)))
9498
9499           ;; run the move/copy/crosspost/respool hook
9500           (run-hook-with-args 'gnus-summary-article-move-hook
9501                               action
9502                               (gnus-data-header
9503                                (assoc article (gnus-data-list nil)))
9504                               gnus-newsgroup-name
9505                               to-newsgroup
9506                               select-method))
9507
9508         ;;;!!!Why is this necessary?
9509         (set-buffer gnus-summary-buffer)
9510
9511         (gnus-summary-goto-subject article)
9512         (when (eq action 'move)
9513           (gnus-summary-mark-article article gnus-canceled-mark))))
9514       (push article articles-to-update-marks))
9515
9516     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9517     ;; Re-activate all groups that have been moved to.
9518     (save-excursion
9519       (set-buffer gnus-group-buffer)
9520       (let ((gnus-group-marked to-groups))
9521         (gnus-group-get-new-news-this-group nil t)))
9522
9523     (gnus-kill-buffer copy-buf)
9524     (gnus-summary-position-point)
9525     (gnus-set-mode-line 'summary)))
9526
9527 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9528   "Copy the current article to some other group.
9529 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9530 When called interactively, if TO-NEWSGROUP is nil, use the value of
9531 the variable `gnus-move-split-methods' for finding a default target
9532 newsgroup.
9533 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9534 re-spool using this method."
9535   (interactive "P")
9536   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9537
9538 (defun gnus-summary-crosspost-article (&optional n)
9539   "Crosspost the current article to some other group."
9540   (interactive "P")
9541   (gnus-summary-move-article n nil nil 'crosspost))
9542
9543 (defcustom gnus-summary-respool-default-method nil
9544   "Default method type for respooling an article.
9545 If nil, use to the current newsgroup method."
9546   :type 'symbol
9547   :group 'gnus-summary-mail)
9548
9549 (defcustom gnus-summary-display-while-building nil
9550   "If non-nil, show and update the summary buffer as it's being built.
9551 If the value is t, update the buffer after every line is inserted.  If
9552 the value is an integer (N), update the display every N lines."
9553   :version "22.1"
9554   :group 'gnus-thread
9555   :type '(choice (const :tag "off" nil)
9556                  number
9557                  (const :tag "frequently" t)))
9558
9559 (defun gnus-summary-respool-article (&optional n method)
9560   "Respool the current article.
9561 The article will be squeezed through the mail spooling process again,
9562 which means that it will be put in some mail newsgroup or other
9563 depending on `nnmail-split-methods'.
9564 If N is a positive number, respool the N next articles.
9565 If N is a negative number, respool the N previous articles.
9566 If N is nil and any articles have been marked with the process mark,
9567 respool those articles instead.
9568
9569 Respooling can be done both from mail groups and \"real\" newsgroups.
9570 In the former case, the articles in question will be moved from the
9571 current group into whatever groups they are destined to.  In the
9572 latter case, they will be copied into the relevant groups."
9573   (interactive
9574    (list current-prefix-arg
9575          (let* ((methods (gnus-methods-using 'respool))
9576                 (methname
9577                  (symbol-name (or gnus-summary-respool-default-method
9578                                   (car (gnus-find-method-for-group
9579                                         gnus-newsgroup-name)))))
9580                 (method
9581                  (gnus-completing-read-with-default
9582                   methname "Backend to use when respooling"
9583                   methods nil t nil 'gnus-mail-method-history))
9584                 ms)
9585            (cond
9586             ((zerop (length (setq ms (gnus-servers-using-backend
9587                                       (intern method)))))
9588              (list (intern method) ""))
9589             ((= 1 (length ms))
9590              (car ms))
9591             (t
9592              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9593                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9594                            ms-alist))))))))
9595   (unless method
9596     (error "No method given for respooling"))
9597   (if (assoc (symbol-name
9598               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9599              (gnus-methods-using 'respool))
9600       (gnus-summary-move-article n nil method)
9601     (gnus-summary-copy-article n nil method)))
9602
9603 (defun gnus-summary-import-article (file &optional edit)
9604   "Import an arbitrary file into a mail newsgroup."
9605   (interactive "fImport file: \nP")
9606   (let ((group gnus-newsgroup-name)
9607         (now (current-time))
9608         atts lines group-art)
9609     (unless (gnus-check-backend-function 'request-accept-article group)
9610       (error "%s does not support article importing" group))
9611     (or (file-readable-p file)
9612         (not (file-regular-p file))
9613         (error "Can't read %s" file))
9614     (save-excursion
9615       (set-buffer (gnus-get-buffer-create " *import file*"))
9616       (erase-buffer)
9617       (nnheader-insert-file-contents file)
9618       (goto-char (point-min))
9619       (if (nnheader-article-p)
9620           (save-restriction
9621             (goto-char (point-min))
9622             (search-forward "\n\n" nil t)
9623             (narrow-to-region (point-min) (1- (point)))
9624             (goto-char (point-min))
9625             (unless (re-search-forward "^date:" nil t)
9626               (goto-char (point-max))
9627               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9628        ;; This doesn't look like an article, so we fudge some headers.
9629         (setq atts (file-attributes file)
9630               lines (count-lines (point-min) (point-max)))
9631         (insert "From: " (read-string "From: ") "\n"
9632                 "Subject: " (read-string "Subject: ") "\n"
9633                 "Date: " (message-make-date (nth 5 atts)) "\n"
9634                 "Message-ID: " (message-make-message-id) "\n"
9635                 "Lines: " (int-to-string lines) "\n"
9636                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9637       (setq group-art (gnus-request-accept-article group nil t))
9638       (kill-buffer (current-buffer)))
9639     (setq gnus-newsgroup-active (gnus-activate-group group))
9640     (forward-line 1)
9641     (gnus-summary-goto-article (cdr group-art) nil t)
9642     (when edit
9643       (gnus-summary-edit-article))))
9644
9645 (defun gnus-summary-create-article ()
9646   "Create an article in a mail newsgroup."
9647   (interactive)
9648   (let ((group gnus-newsgroup-name)
9649         (now (current-time))
9650         group-art)
9651     (unless (gnus-check-backend-function 'request-accept-article group)
9652       (error "%s does not support article importing" group))
9653     (save-excursion
9654       (set-buffer (gnus-get-buffer-create " *import file*"))
9655       (erase-buffer)
9656       (goto-char (point-min))
9657       ;; This doesn't look like an article, so we fudge some headers.
9658       (insert "From: " (read-string "From: ") "\n"
9659               "Subject: " (read-string "Subject: ") "\n"
9660               "Date: " (message-make-date now) "\n"
9661               "Message-ID: " (message-make-message-id) "\n")
9662       (setq group-art (gnus-request-accept-article group nil t))
9663       (kill-buffer (current-buffer)))
9664     (setq gnus-newsgroup-active (gnus-activate-group group))
9665     (forward-line 1)
9666     (gnus-summary-goto-article (cdr group-art) nil t)
9667     (gnus-summary-edit-article)))
9668
9669 (defun gnus-summary-article-posted-p ()
9670   "Say whether the current (mail) article is available from news as well.
9671 This will be the case if the article has both been mailed and posted."
9672   (interactive)
9673   (let ((id (mail-header-references (gnus-summary-article-header)))
9674         (gnus-override-method (car (gnus-refer-article-methods))))
9675     (if (gnus-request-head id "")
9676         (gnus-message 2 "The current message was found on %s"
9677                       gnus-override-method)
9678       (gnus-message 2 "The current message couldn't be found on %s"
9679                     gnus-override-method)
9680       nil)))
9681
9682 (defun gnus-summary-expire-articles (&optional now)
9683   "Expire all articles that are marked as expirable in the current group."
9684   (interactive)
9685   (when (and (not gnus-group-is-exiting-without-update-p)
9686              (gnus-check-backend-function
9687               'request-expire-articles gnus-newsgroup-name))
9688     ;; This backend supports expiry.
9689     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9690            (expirable (if total
9691                           (progn
9692                             ;; We need to update the info for
9693                             ;; this group for `gnus-list-of-read-articles'
9694                             ;; to give us the right answer.
9695                             (gnus-run-hooks 'gnus-exit-group-hook)
9696                             (gnus-summary-update-info)
9697                             (gnus-list-of-read-articles gnus-newsgroup-name))
9698                         (setq gnus-newsgroup-expirable
9699                               (sort gnus-newsgroup-expirable '<))))
9700            (expiry-wait (if now 'immediate
9701                           (gnus-group-find-parameter
9702                            gnus-newsgroup-name 'expiry-wait)))
9703            (nnmail-expiry-target
9704             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9705                 nnmail-expiry-target))
9706            es)
9707       (when expirable
9708         ;; There are expirable articles in this group, so we run them
9709         ;; through the expiry process.
9710         (gnus-message 6 "Expiring articles...")
9711         (unless (gnus-check-group gnus-newsgroup-name)
9712           (error "Can't open server for %s" gnus-newsgroup-name))
9713         ;; The list of articles that weren't expired is returned.
9714         (save-excursion
9715           (if expiry-wait
9716               (let ((nnmail-expiry-wait-function nil)
9717                     (nnmail-expiry-wait expiry-wait))
9718                 (setq es (gnus-request-expire-articles
9719                           expirable gnus-newsgroup-name)))
9720             (setq es (gnus-request-expire-articles
9721                       expirable gnus-newsgroup-name)))
9722           (unless total
9723             (setq gnus-newsgroup-expirable es))
9724           ;; We go through the old list of expirable, and mark all
9725           ;; really expired articles as nonexistent.
9726           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9727             (let ((gnus-use-cache nil))
9728               (dolist (article expirable)
9729                 (when (and (not (memq article es))
9730                            (gnus-data-find article))
9731                   (gnus-summary-mark-article article gnus-canceled-mark)
9732                   (run-hook-with-args 'gnus-summary-article-expire-hook
9733                                       'delete
9734                                       (gnus-data-header
9735                                        (assoc article (gnus-data-list nil)))
9736                                       gnus-newsgroup-name
9737                                       nil
9738                                       nil))))))
9739         (gnus-message 6 "Expiring articles...done")))))
9740
9741 (defun gnus-summary-expire-articles-now ()
9742   "Expunge all expirable articles in the current group.
9743 This means that *all* articles that are marked as expirable will be
9744 deleted forever, right now."
9745   (interactive)
9746   (or gnus-expert-user
9747       (gnus-yes-or-no-p
9748        "Are you really, really sure you want to delete all expirable messages? ")
9749       (error "Phew!"))
9750   (gnus-summary-expire-articles t))
9751
9752 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9753 (defun gnus-summary-delete-article (&optional n)
9754   "Delete the N next (mail) articles.
9755 This command actually deletes articles.  This is not a marking
9756 command.  The article will disappear forever from your life, never to
9757 return.
9758
9759 If N is negative, delete backwards.
9760 If N is nil and articles have been marked with the process mark,
9761 delete these instead.
9762
9763 If `gnus-novice-user' is non-nil you will be asked for
9764 confirmation before the articles are deleted."
9765   (interactive "P")
9766   (unless (gnus-check-backend-function 'request-expire-articles
9767                                        gnus-newsgroup-name)
9768     (error "The current newsgroup does not support article deletion"))
9769   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9770     (error "Couldn't open server"))
9771   ;; Compute the list of articles to delete.
9772   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9773         (nnmail-expiry-target 'delete)
9774         not-deleted)
9775     (if (and gnus-novice-user
9776              (not (gnus-yes-or-no-p
9777                    (format "Do you really want to delete %s forever? "
9778                            (if (> (length articles) 1)
9779                                (format "these %s articles" (length articles))
9780                              "this article")))))
9781         ()
9782       ;; Delete the articles.
9783       (setq not-deleted (gnus-request-expire-articles
9784                          articles gnus-newsgroup-name 'force))
9785       (while articles
9786         (gnus-summary-remove-process-mark (car articles))
9787         ;; The backend might not have been able to delete the article
9788         ;; after all.
9789         (unless (memq (car articles) not-deleted)
9790           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9791         (let* ((article (car articles))
9792                (ghead  (gnus-data-header
9793                                     (assoc article (gnus-data-list nil)))))
9794           (run-hook-with-args 'gnus-summary-article-delete-hook
9795                               'delete ghead gnus-newsgroup-name nil
9796                               nil))
9797         (setq articles (cdr articles)))
9798       (when not-deleted
9799         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9800     (gnus-summary-position-point)
9801     (gnus-set-mode-line 'summary)
9802     not-deleted))
9803
9804 (defun gnus-summary-edit-article (&optional arg)
9805   "Edit the current article.
9806 This will have permanent effect only in mail groups.
9807 If ARG is nil, edit the decoded articles.
9808 If ARG is 1, edit the raw articles.
9809 If ARG is 2, edit the raw articles even in read-only groups.
9810 If ARG is 3, edit the articles with the current handles.
9811 Otherwise, allow editing of articles even in read-only
9812 groups."
9813   (interactive "P")
9814   (let (force raw current-handles)
9815     (cond
9816      ((null arg))
9817      ((eq arg 1)
9818       (setq raw t))
9819      ((eq arg 2)
9820       (setq raw t
9821             force t))
9822      ((eq arg 3)
9823       (setq current-handles
9824             (and (gnus-buffer-live-p gnus-article-buffer)
9825                  (with-current-buffer gnus-article-buffer
9826                    (prog1
9827                        gnus-article-mime-handles
9828                      (setq gnus-article-mime-handles nil))))))
9829      (t
9830       (setq force t)))
9831     (when (and raw (not force)
9832                (member gnus-newsgroup-name '("nndraft:delayed"
9833                                              "nndraft:drafts"
9834                                              "nndraft:queue")))
9835       (error "Can't edit the raw article in group %s"
9836              gnus-newsgroup-name))
9837     (save-excursion
9838       (set-buffer gnus-summary-buffer)
9839       (let ((mail-parse-charset gnus-newsgroup-charset)
9840             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9841         (gnus-set-global-variables)
9842         (when (and (not force)
9843                    (gnus-group-read-only-p))
9844           (error "The current newsgroup does not support article editing"))
9845         (gnus-summary-show-article t)
9846         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9847           (with-current-buffer gnus-article-buffer
9848             (mm-enable-multibyte)))
9849         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9850             (setq raw t))
9851         (gnus-article-edit-article
9852          (if raw 'ignore
9853            `(lambda ()
9854               (let ((mbl mml-buffer-list))
9855                 (setq mml-buffer-list nil)
9856                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9857                   (mime-to-mml ,'current-handles))
9858                 (let ((mbl1 mml-buffer-list))
9859                   (setq mml-buffer-list mbl)
9860                   (set (make-local-variable 'mml-buffer-list) mbl1))
9861                 (gnus-make-local-hook 'kill-buffer-hook)
9862                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9863          `(lambda (no-highlight)
9864             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9865                   (message-options message-options)
9866                   (message-options-set-recipient)
9867                   (mail-parse-ignored-charsets
9868                    ',gnus-newsgroup-ignored-charsets))
9869               ,(if (not raw) '(progn
9870                                 (mml-to-mime)
9871                                 (mml-destroy-buffers)
9872                                 (remove-hook 'kill-buffer-hook
9873                                              'mml-destroy-buffers t)
9874                                 (kill-local-variable 'mml-buffer-list)))
9875               (gnus-summary-edit-article-done
9876                ,(or (mail-header-references gnus-current-headers) "")
9877                ,(gnus-group-read-only-p)
9878                ,gnus-summary-buffer no-highlight))))))))
9879
9880 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9881
9882 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9883                                                  no-highlight)
9884   "Make edits to the current article permanent."
9885   (interactive)
9886   (save-excursion
9887    ;; The buffer restriction contains the entire article if it exists.
9888     (when (article-goto-body)
9889       (let ((lines (count-lines (point) (point-max)))
9890             (length (- (point-max) (point)))
9891             (case-fold-search t)
9892             (body (copy-marker (point))))
9893         (goto-char (point-min))
9894         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9895           (delete-region (match-beginning 1) (match-end 1))
9896           (insert (number-to-string length)))
9897         (goto-char (point-min))
9898         (when (re-search-forward
9899                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9900           (delete-region (match-beginning 1) (match-end 1))
9901           (insert (number-to-string length)))
9902         (goto-char (point-min))
9903         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9904           (delete-region (match-beginning 1) (match-end 1))
9905           (insert (number-to-string lines))))))
9906   ;; Replace the article.
9907   (let ((buf (current-buffer)))
9908     (with-temp-buffer
9909       (insert-buffer-substring buf)
9910
9911       (if (and (not read-only)
9912                (not (gnus-request-replace-article
9913                      (cdr gnus-article-current) (car gnus-article-current)
9914                      (current-buffer) t)))
9915           (error "Couldn't replace article")
9916         ;; Update the summary buffer.
9917         (if (and references
9918                  (equal (message-tokenize-header references " ")
9919                         (message-tokenize-header
9920                          (or (message-fetch-field "references") "") " ")))
9921             ;; We only have to update this line.
9922             (save-excursion
9923               (save-restriction
9924                 (message-narrow-to-head)
9925                 (let ((head (buffer-substring-no-properties
9926                              (point-min) (point-max)))
9927                       header)
9928                   (with-temp-buffer
9929                     (insert (format "211 %d Article retrieved.\n"
9930                                     (cdr gnus-article-current)))
9931                     (insert head)
9932                     (insert ".\n")
9933                     (let ((nntp-server-buffer (current-buffer)))
9934                       (setq header (car (gnus-get-newsgroup-headers
9935                                          nil t))))
9936                     (save-excursion
9937                       (set-buffer gnus-summary-buffer)
9938                       (gnus-data-set-header
9939                        (gnus-data-find (cdr gnus-article-current))
9940                        header)
9941                       (gnus-summary-update-article-line
9942                        (cdr gnus-article-current) header)
9943                       (if (gnus-summary-goto-subject
9944                            (cdr gnus-article-current) nil t)
9945                           (gnus-summary-update-secondary-mark
9946                            (cdr gnus-article-current))))))))
9947           ;; Update threads.
9948           (set-buffer (or buffer gnus-summary-buffer))
9949           (gnus-summary-update-article (cdr gnus-article-current))
9950           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9951               (gnus-summary-update-secondary-mark
9952                (cdr gnus-article-current))))
9953         ;; Prettify the article buffer again.
9954         (unless no-highlight
9955           (save-excursion
9956             (set-buffer gnus-article-buffer)
9957             ;;;!!! Fix this -- article should be rehighlighted.
9958             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9959             (set-buffer gnus-original-article-buffer)
9960             (gnus-request-article
9961              (cdr gnus-article-current)
9962              (car gnus-article-current) (current-buffer))))
9963         ;; Prettify the summary buffer line.
9964         (when (gnus-visual-p 'summary-highlight 'highlight)
9965           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9966
9967 (defun gnus-summary-edit-wash (key)
9968   "Perform editing command KEY in the article buffer."
9969   (interactive
9970    (list
9971     (progn
9972       (message "%s" (concat (this-command-keys) "- "))
9973       (read-char))))
9974   (message "")
9975   (gnus-summary-edit-article)
9976   (execute-kbd-macro (concat (this-command-keys) key))
9977   (gnus-article-edit-done))
9978
9979 ;;; Respooling
9980
9981 (defun gnus-summary-respool-query (&optional silent trace)
9982   "Query where the respool algorithm would put this article."
9983   (interactive)
9984   (let (gnus-mark-article-hook)
9985     (gnus-summary-select-article)
9986     (save-excursion
9987       (set-buffer gnus-original-article-buffer)
9988       (let ((groups (nnmail-article-group 'identity trace)))
9989         (unless silent
9990           (if groups
9991               (message "This message would go to %s"
9992                        (mapconcat 'car groups ", "))
9993             (message "This message would go to no groups"))
9994           groups)))))
9995
9996 (defun gnus-summary-respool-trace ()
9997   "Trace where the respool algorithm would put this article.
9998 Display a buffer showing all fancy splitting patterns which matched."
9999   (interactive)
10000   (gnus-summary-respool-query nil t))
10001
10002 ;; Summary marking commands.
10003
10004 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10005   "Mark articles which has the same subject as read, and then select the next.
10006 If UNMARK is positive, remove any kind of mark.
10007 If UNMARK is negative, tick articles."
10008   (interactive "P")
10009   (when unmark
10010     (setq unmark (prefix-numeric-value unmark)))
10011   (let ((count
10012          (gnus-summary-mark-same-subject
10013           (gnus-summary-article-subject) unmark)))
10014     ;; Select next unread article.  If auto-select-same mode, should
10015     ;; select the first unread article.
10016     (gnus-summary-next-article t (and gnus-auto-select-same
10017                                       (gnus-summary-article-subject)))
10018     (gnus-message 7 "%d article%s marked as %s"
10019                   count (if (= count 1) " is" "s are")
10020                   (if unmark "unread" "read"))))
10021
10022 (defun gnus-summary-kill-same-subject (&optional unmark)
10023   "Mark articles which has the same subject as read.
10024 If UNMARK is positive, remove any kind of mark.
10025 If UNMARK is negative, tick articles."
10026   (interactive "P")
10027   (when unmark
10028     (setq unmark (prefix-numeric-value unmark)))
10029   (let ((count
10030          (gnus-summary-mark-same-subject
10031           (gnus-summary-article-subject) unmark)))
10032     ;; If marked as read, go to next unread subject.
10033     (when (null unmark)
10034       ;; Go to next unread subject.
10035       (gnus-summary-next-subject 1 t))
10036     (gnus-message 7 "%d articles are marked as %s"
10037                   count (if unmark "unread" "read"))))
10038
10039 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10040   "Mark articles with same SUBJECT as read, and return marked number.
10041 If optional argument UNMARK is positive, remove any kinds of marks.
10042 If optional argument UNMARK is negative, mark articles as unread instead."
10043   (let ((count 1))
10044     (save-excursion
10045       (cond
10046        ((null unmark)                   ; Mark as read.
10047         (while (and
10048                 (progn
10049                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10050                   (gnus-summary-show-thread) t)
10051                 (gnus-summary-find-subject subject))
10052           (setq count (1+ count))))
10053        ((> unmark 0)                    ; Tick.
10054         (while (and
10055                 (progn
10056                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10057                   (gnus-summary-show-thread) t)
10058                 (gnus-summary-find-subject subject))
10059           (setq count (1+ count))))
10060        (t                               ; Mark as unread.
10061         (while (and
10062                 (progn
10063                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10064                   (gnus-summary-show-thread) t)
10065                 (gnus-summary-find-subject subject))
10066           (setq count (1+ count)))))
10067       (gnus-set-mode-line 'summary)
10068       ;; Return the number of marked articles.
10069       count)))
10070
10071 (defun gnus-summary-mark-as-processable (n &optional unmark)
10072   "Set the process mark on the next N articles.
10073 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10074 the process mark instead.  The difference between N and the actual
10075 number of articles marked is returned."
10076   (interactive "P")
10077   (if (and (null n) (gnus-region-active-p))
10078       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10079     (setq n (prefix-numeric-value n))
10080     (let ((backward (< n 0))
10081           (n (abs n)))
10082       (while (and
10083               (> n 0)
10084               (if unmark
10085                   (gnus-summary-remove-process-mark
10086                    (gnus-summary-article-number))
10087                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10088               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10089         (setq n (1- n)))
10090       (when (/= 0 n)
10091         (gnus-message 7 "No more articles"))
10092       (gnus-summary-recenter)
10093       (gnus-summary-position-point)
10094       n)))
10095
10096 (defun gnus-summary-unmark-as-processable (n)
10097   "Remove the process mark from the next N articles.
10098 If N is negative, unmark backward instead.  The difference between N and
10099 the actual number of articles unmarked is returned."
10100   (interactive "P")
10101   (gnus-summary-mark-as-processable n t))
10102
10103 (defun gnus-summary-unmark-all-processable ()
10104   "Remove the process mark from all articles."
10105   (interactive)
10106   (save-excursion
10107     (while gnus-newsgroup-processable
10108       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10109   (gnus-summary-position-point))
10110
10111 (defun gnus-summary-add-mark (article type)
10112   "Mark ARTICLE with a mark of TYPE."
10113   (let ((vtype (car (assq type gnus-article-mark-lists)))
10114         var)
10115     (if (not vtype)
10116         (error "No such mark type: %s" type)
10117       (setq var (intern (format "gnus-newsgroup-%s" type)))
10118       (set var (cons article (symbol-value var)))
10119       (if (memq type '(processable cached replied forwarded recent saved))
10120           (gnus-summary-update-secondary-mark article)
10121         ;;; !!! This is bogus.  We should find out what primary
10122         ;;; !!! mark we want to set.
10123         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10124
10125 (defun gnus-summary-mark-as-expirable (n)
10126   "Mark N articles forward as expirable.
10127 If N is negative, mark backward instead.  The difference between N and
10128 the actual number of articles marked is returned."
10129   (interactive "p")
10130   (gnus-summary-mark-forward n gnus-expirable-mark))
10131
10132 (defun gnus-summary-mark-as-spam (n)
10133   "Mark N articles forward as spam.
10134 If N is negative, mark backward instead.  The difference between N and
10135 the actual number of articles marked is returned."
10136   (interactive "p")
10137   (gnus-summary-mark-forward n gnus-spam-mark))
10138
10139 (defun gnus-summary-mark-article-as-replied (article)
10140   "Mark ARTICLE as replied to and update the summary line.
10141 ARTICLE can also be a list of articles."
10142   (interactive (list (gnus-summary-article-number)))
10143   (let ((articles (if (listp article) article (list article))))
10144     (dolist (article articles)
10145       (unless (numberp article)
10146         (error "%s is not a number" article))
10147       (push article gnus-newsgroup-replied)
10148       (let ((buffer-read-only nil))
10149         (when (gnus-summary-goto-subject article nil t)
10150           (gnus-summary-update-secondary-mark article))))))
10151
10152 (defun gnus-summary-mark-article-as-forwarded (article)
10153   "Mark ARTICLE as forwarded and update the summary line.
10154 ARTICLE can also be a list of articles."
10155   (let ((articles (if (listp article) article (list article))))
10156     (dolist (article articles)
10157       (push article gnus-newsgroup-forwarded)
10158       (let ((buffer-read-only nil))
10159         (when (gnus-summary-goto-subject article nil t)
10160           (gnus-summary-update-secondary-mark article))))))
10161
10162 (defun gnus-summary-set-bookmark (article)
10163   "Set a bookmark in current article."
10164   (interactive (list (gnus-summary-article-number)))
10165   (when (or (not (get-buffer gnus-article-buffer))
10166             (not gnus-current-article)
10167             (not gnus-article-current)
10168             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10169     (error "No current article selected"))
10170   ;; Remove old bookmark, if one exists.
10171   (gnus-pull article gnus-newsgroup-bookmarks)
10172   ;; Set the new bookmark, which is on the form
10173   ;; (article-number . line-number-in-body).
10174   (push
10175    (cons article
10176          (with-current-buffer gnus-article-buffer
10177            (count-lines
10178             (min (point)
10179                  (save-excursion
10180                    (article-goto-body)
10181                    (point)))
10182             (point))))
10183    gnus-newsgroup-bookmarks)
10184   (gnus-message 6 "A bookmark has been added to the current article."))
10185
10186 (defun gnus-summary-remove-bookmark (article)
10187   "Remove the bookmark from the current article."
10188   (interactive (list (gnus-summary-article-number)))
10189   ;; Remove old bookmark, if one exists.
10190   (if (not (assq article gnus-newsgroup-bookmarks))
10191       (gnus-message 6 "No bookmark in current article.")
10192     (gnus-pull article gnus-newsgroup-bookmarks)
10193     (gnus-message 6 "Removed bookmark.")))
10194
10195 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10196 (defun gnus-summary-mark-as-dormant (n)
10197   "Mark N articles forward as dormant.
10198 If N is negative, mark backward instead.  The difference between N and
10199 the actual number of articles marked is returned."
10200   (interactive "p")
10201   (gnus-summary-mark-forward n gnus-dormant-mark))
10202
10203 (defun gnus-summary-set-process-mark (article)
10204   "Set the process mark on ARTICLE and update the summary line."
10205   (setq gnus-newsgroup-processable
10206         (cons article
10207               (delq article gnus-newsgroup-processable)))
10208   (when (gnus-summary-goto-subject article)
10209     (gnus-summary-show-thread)
10210     (gnus-summary-goto-subject article)
10211     (gnus-summary-update-secondary-mark article)))
10212
10213 (defun gnus-summary-remove-process-mark (&rest articles)
10214   "Remove the process mark from ARTICLES and update the summary line."
10215   (dolist (article articles)
10216     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10217     (when (gnus-summary-goto-subject article)
10218       (gnus-summary-show-thread)
10219       (gnus-summary-goto-subject article)
10220       (gnus-summary-update-secondary-mark article)))
10221   t)
10222
10223 (defun gnus-summary-set-saved-mark (article)
10224   "Set the process mark on ARTICLE and update the summary line."
10225   (push article gnus-newsgroup-saved)
10226   (when (gnus-summary-goto-subject article)
10227     (gnus-summary-update-secondary-mark article)))
10228
10229 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10230   "Mark N articles as read forwards.
10231 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10232 The difference between N and the actual number of articles marked is
10233 returned.
10234 If NO-EXPIRE, auto-expiry will be inhibited."
10235   (interactive "p")
10236   (gnus-summary-show-thread)
10237   (let ((backward (< n 0))
10238         (gnus-summary-goto-unread
10239          (and gnus-summary-goto-unread
10240               (not (eq gnus-summary-goto-unread 'never))
10241               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10242                                     gnus-ticked-mark gnus-dormant-mark)))))
10243         (n (abs n))
10244         (mark (or mark gnus-del-mark)))
10245     (while (and (> n 0)
10246                 (gnus-summary-mark-article nil mark no-expire)
10247                 (zerop (gnus-summary-next-subject
10248                         (if backward -1 1)
10249                         (and gnus-summary-goto-unread
10250                              (not (eq gnus-summary-goto-unread 'never)))
10251                         t)))
10252       (setq n (1- n)))
10253     (when (/= 0 n)
10254       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10255     (gnus-summary-recenter)
10256     (gnus-summary-position-point)
10257     (gnus-set-mode-line 'summary)
10258     n))
10259
10260 (defun gnus-summary-mark-article-as-read (mark)
10261   "Mark the current article quickly as read with MARK."
10262   (let ((article (gnus-summary-article-number)))
10263     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10264     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10265     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10266     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10267     (push (cons article mark) gnus-newsgroup-reads)
10268     ;; Possibly remove from cache, if that is used.
10269     (when gnus-use-cache
10270       (gnus-cache-enter-remove-article article))
10271     ;; Allow the backend to change the mark.
10272     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10273     ;; Check for auto-expiry.
10274     (when (and gnus-newsgroup-auto-expire
10275                (memq mark gnus-auto-expirable-marks))
10276       (setq mark gnus-expirable-mark)
10277       ;; Let the backend know about the mark change.
10278       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10279       (push article gnus-newsgroup-expirable))
10280     ;; Set the mark in the buffer.
10281     (gnus-summary-update-mark mark 'unread)
10282     t))
10283
10284 (defun gnus-summary-mark-article-as-unread (mark)
10285   "Mark the current article quickly as unread with MARK."
10286   (let* ((article (gnus-summary-article-number))
10287          (old-mark (gnus-summary-article-mark article)))
10288     ;; Allow the backend to change the mark.
10289     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10290     (if (eq mark old-mark)
10291         t
10292       (if (<= article 0)
10293           (progn
10294             (gnus-error 1 "Can't mark negative article numbers")
10295             nil)
10296         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10297         (setq gnus-newsgroup-spam-marked
10298               (delq article gnus-newsgroup-spam-marked))
10299         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10300         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10301         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10302         (cond ((= mark gnus-ticked-mark)
10303                (setq gnus-newsgroup-marked
10304                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10305                                               article)))
10306               ((= mark gnus-spam-mark)
10307                (setq gnus-newsgroup-spam-marked
10308                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10309                                               article)))
10310               ((= mark gnus-dormant-mark)
10311                (setq gnus-newsgroup-dormant
10312                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10313                                               article)))
10314               (t
10315                (setq gnus-newsgroup-unreads
10316                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10317                                               article))))
10318         (gnus-pull article gnus-newsgroup-reads)
10319
10320         ;; See whether the article is to be put in the cache.
10321         (and gnus-use-cache
10322              (vectorp (gnus-summary-article-header article))
10323              (save-excursion
10324                (gnus-cache-possibly-enter-article
10325                 gnus-newsgroup-name article
10326                 (= mark gnus-ticked-mark)
10327                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10328
10329         ;; Fix the mark.
10330         (gnus-summary-update-mark mark 'unread)
10331         t))))
10332
10333 (defun gnus-summary-mark-article (&optional article mark no-expire)
10334   "Mark ARTICLE with MARK.  MARK can be any character.
10335 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10336 `??' (dormant) and `?E' (expirable).
10337 If MARK is nil, then the default character `?r' is used.
10338 If ARTICLE is nil, then the article on the current line will be
10339 marked.
10340 If NO-EXPIRE, auto-expiry will be inhibited."
10341   ;; The mark might be a string.
10342   (when (stringp mark)
10343     (setq mark (aref mark 0)))
10344   ;; If no mark is given, then we check auto-expiring.
10345   (when (null mark)
10346     (setq mark gnus-del-mark))
10347   (when (and (not no-expire)
10348              gnus-newsgroup-auto-expire
10349              (memq mark gnus-auto-expirable-marks))
10350     (setq mark gnus-expirable-mark))
10351   (let ((article (or article (gnus-summary-article-number)))
10352         (old-mark (gnus-summary-article-mark article)))
10353     ;; Allow the backend to change the mark.
10354     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10355     (if (eq mark old-mark)
10356         t
10357       (unless article
10358         (error "No article on current line"))
10359       (if (not (if (or (= mark gnus-unread-mark)
10360                        (= mark gnus-ticked-mark)
10361                        (= mark gnus-spam-mark)
10362                        (= mark gnus-dormant-mark))
10363                    (gnus-mark-article-as-unread article mark)
10364                  (gnus-mark-article-as-read article mark)))
10365           t
10366         ;; See whether the article is to be put in the cache.
10367         (and gnus-use-cache
10368              (not (= mark gnus-canceled-mark))
10369              (vectorp (gnus-summary-article-header article))
10370              (save-excursion
10371                (gnus-cache-possibly-enter-article
10372                 gnus-newsgroup-name article
10373                 (= mark gnus-ticked-mark)
10374                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10375
10376         (when (gnus-summary-goto-subject article nil t)
10377           (let ((buffer-read-only nil))
10378             (gnus-summary-show-thread)
10379             ;; Fix the mark.
10380             (gnus-summary-update-mark mark 'unread)
10381             t))))))
10382
10383 (defun gnus-summary-update-secondary-mark (article)
10384   "Update the secondary (read, process, cache) mark."
10385   (gnus-summary-update-mark
10386    (cond ((memq article gnus-newsgroup-processable)
10387           gnus-process-mark)
10388          ((memq article gnus-newsgroup-cached)
10389           gnus-cached-mark)
10390          ((memq article gnus-newsgroup-replied)
10391           gnus-replied-mark)
10392          ((memq article gnus-newsgroup-forwarded)
10393           gnus-forwarded-mark)
10394          ((memq article gnus-newsgroup-saved)
10395           gnus-saved-mark)
10396          ((memq article gnus-newsgroup-recent)
10397           gnus-recent-mark)
10398          ((memq article gnus-newsgroup-unseen)
10399           gnus-unseen-mark)
10400          (t gnus-no-mark))
10401    'replied)
10402   (when (gnus-visual-p 'summary-highlight 'highlight)
10403     (gnus-run-hooks 'gnus-summary-update-hook))
10404   t)
10405
10406 (defun gnus-summary-update-download-mark (article)
10407   "Update the download mark."
10408   (gnus-summary-update-mark
10409    (cond ((memq article gnus-newsgroup-undownloaded)
10410           gnus-undownloaded-mark)
10411          (gnus-newsgroup-agentized
10412           gnus-downloaded-mark)
10413          (t
10414           gnus-no-mark))
10415    'download)
10416   (gnus-summary-update-line t)
10417   t)
10418
10419 (defun gnus-summary-update-mark (mark type)
10420   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10421         (buffer-read-only nil))
10422     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10423     (when forward
10424       (when (looking-at "\r")
10425         (incf forward))
10426       (when (<= (+ forward (point)) (point-max))
10427         ;; Go to the right position on the line.
10428         (goto-char (+ forward (point)))
10429         ;; Replace the old mark with the new mark.
10430         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10431         ;; Optionally update the marks by some user rule.
10432         (when (eq type 'unread)
10433           (gnus-data-set-mark
10434            (gnus-data-find (gnus-summary-article-number)) mark)
10435           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10436
10437 (defun gnus-mark-article-as-read (article &optional mark)
10438   "Enter ARTICLE in the pertinent lists and remove it from others."
10439   ;; Make the article expirable.
10440   (let ((mark (or mark gnus-del-mark)))
10441     (setq gnus-newsgroup-expirable
10442           (if (= mark gnus-expirable-mark)
10443               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10444             (delq article gnus-newsgroup-expirable)))
10445     ;; Remove from unread and marked lists.
10446     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10447     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10448     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10449     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10450     (push (cons article mark) gnus-newsgroup-reads)
10451     ;; Possibly remove from cache, if that is used.
10452     (when gnus-use-cache
10453       (gnus-cache-enter-remove-article article))
10454     t))
10455
10456 (defun gnus-mark-article-as-unread (article &optional mark)
10457   "Enter ARTICLE in the pertinent lists and remove it from others."
10458   (let ((mark (or mark gnus-ticked-mark)))
10459     (if (<= article 0)
10460         (progn
10461           (gnus-error 1 "Can't mark negative article numbers")
10462           nil)
10463       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10464             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10465             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10466             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10467             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10468
10469       ;; Unsuppress duplicates?
10470       (when gnus-suppress-duplicates
10471         (gnus-dup-unsuppress-article article))
10472
10473       (cond ((= mark gnus-ticked-mark)
10474              (setq gnus-newsgroup-marked
10475                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10476             ((= mark gnus-spam-mark)
10477              (setq gnus-newsgroup-spam-marked
10478                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10479                                             article)))
10480             ((= mark gnus-dormant-mark)
10481              (setq gnus-newsgroup-dormant
10482                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10483             (t
10484              (setq gnus-newsgroup-unreads
10485                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10486       (gnus-pull article gnus-newsgroup-reads)
10487       t)))
10488
10489 (defalias 'gnus-summary-mark-as-unread-forward
10490   'gnus-summary-tick-article-forward)
10491 (make-obsolete 'gnus-summary-mark-as-unread-forward
10492                'gnus-summary-tick-article-forward)
10493 (defun gnus-summary-tick-article-forward (n)
10494   "Tick N articles forwards.
10495 If N is negative, tick backwards instead.
10496 The difference between N and the number of articles ticked is returned."
10497   (interactive "p")
10498   (gnus-summary-mark-forward n gnus-ticked-mark))
10499
10500 (defalias 'gnus-summary-mark-as-unread-backward
10501   'gnus-summary-tick-article-backward)
10502 (make-obsolete 'gnus-summary-mark-as-unread-backward
10503                'gnus-summary-tick-article-backward)
10504 (defun gnus-summary-tick-article-backward (n)
10505   "Tick N articles backwards.
10506 The difference between N and the number of articles ticked is returned."
10507   (interactive "p")
10508   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10509
10510 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10511 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10512 (defun gnus-summary-tick-article (&optional article clear-mark)
10513   "Mark current article as unread.
10514 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10515 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10516   (interactive)
10517   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10518                                        gnus-ticked-mark)))
10519
10520 (defun gnus-summary-mark-as-read-forward (n)
10521   "Mark N articles as read forwards.
10522 If N is negative, mark backwards instead.
10523 The difference between N and the actual number of articles marked is
10524 returned."
10525   (interactive "p")
10526   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10527
10528 (defun gnus-summary-mark-as-read-backward (n)
10529   "Mark the N articles as read backwards.
10530 The difference between N and the actual number of articles marked is
10531 returned."
10532   (interactive "p")
10533   (gnus-summary-mark-forward
10534    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10535
10536 (defun gnus-summary-mark-as-read (&optional article mark)
10537   "Mark current article as read.
10538 ARTICLE specifies the article to be marked as read.
10539 MARK specifies a string to be inserted at the beginning of the line."
10540   (gnus-summary-mark-article article mark))
10541
10542 (defun gnus-summary-clear-mark-forward (n)
10543   "Clear marks from N articles forward.
10544 If N is negative, clear backward instead.
10545 The difference between N and the number of marks cleared is returned."
10546   (interactive "p")
10547   (gnus-summary-mark-forward n gnus-unread-mark))
10548
10549 (defun gnus-summary-clear-mark-backward (n)
10550   "Clear marks from N articles backward.
10551 The difference between N and the number of marks cleared is returned."
10552   (interactive "p")
10553   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10554
10555 (defun gnus-summary-mark-unread-as-read ()
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-read-mark)))
10559
10560 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10561   "Intended to be used by `gnus-summary-mark-article-hook'."
10562   (let ((mark (gnus-summary-article-mark)))
10563     (when (or (gnus-unread-mark-p mark)
10564               (gnus-read-mark-p mark))
10565       (gnus-summary-mark-article gnus-current-article
10566                                  (or new-mark gnus-read-mark)))))
10567
10568 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10569   "Intended to be used by `gnus-summary-mark-article-hook'."
10570   (let ((mark (gnus-summary-article-mark)))
10571     (when (or (gnus-unread-mark-p mark)
10572               (gnus-read-mark-p mark))
10573       (gnus-summary-mark-article (gnus-summary-article-number)
10574                                  (or new-mark gnus-read-mark)))))
10575
10576 (defun gnus-summary-mark-unread-as-ticked ()
10577   "Intended to be used by `gnus-summary-mark-article-hook'."
10578   (when (memq gnus-current-article gnus-newsgroup-unreads)
10579     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10580
10581 (defun gnus-summary-mark-region-as-read (point mark all)
10582   "Mark all unread articles between point and mark as read.
10583 If given a prefix, mark all articles between point and mark as read,
10584 even ticked and dormant ones."
10585   (interactive "r\nP")
10586   (save-excursion
10587     (let (article)
10588       (goto-char point)
10589       (beginning-of-line)
10590       (while (and
10591               (< (point) mark)
10592               (progn
10593                 (when (or all
10594                           (memq (setq article (gnus-summary-article-number))
10595                                 gnus-newsgroup-unreads))
10596                   (gnus-summary-mark-article article gnus-del-mark))
10597                 t)
10598               (gnus-summary-find-next))))))
10599
10600 (defun gnus-summary-mark-below (score mark)
10601   "Mark articles with score less than SCORE with MARK."
10602   (interactive "P\ncMark: ")
10603   (setq score (if score
10604                   (prefix-numeric-value score)
10605                 (or gnus-summary-default-score 0)))
10606   (save-excursion
10607     (set-buffer gnus-summary-buffer)
10608     (goto-char (point-min))
10609     (while
10610         (progn
10611           (and (< (gnus-summary-article-score) score)
10612                (gnus-summary-mark-article nil mark))
10613           (gnus-summary-find-next)))))
10614
10615 (defun gnus-summary-kill-below (&optional score)
10616   "Mark articles with score below SCORE as read."
10617   (interactive "P")
10618   (gnus-summary-mark-below score gnus-killed-mark))
10619
10620 (defun gnus-summary-clear-above (&optional score)
10621   "Clear all marks from articles with score above SCORE."
10622   (interactive "P")
10623   (gnus-summary-mark-above score gnus-unread-mark))
10624
10625 (defun gnus-summary-tick-above (&optional score)
10626   "Tick all articles with score above SCORE."
10627   (interactive "P")
10628   (gnus-summary-mark-above score gnus-ticked-mark))
10629
10630 (defun gnus-summary-mark-above (score mark)
10631   "Mark articles with score over SCORE with MARK."
10632   (interactive "P\ncMark: ")
10633   (setq score (if score
10634                   (prefix-numeric-value score)
10635                 (or gnus-summary-default-score 0)))
10636   (save-excursion
10637     (set-buffer gnus-summary-buffer)
10638     (goto-char (point-min))
10639     (while (and (progn
10640                   (when (> (gnus-summary-article-score) score)
10641                     (gnus-summary-mark-article nil mark))
10642                   t)
10643                 (gnus-summary-find-next)))))
10644
10645 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10646 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10647 (defun gnus-summary-limit-include-expunged (&optional no-error)
10648   "Display all the hidden articles that were expunged for low scores."
10649   (interactive)
10650   (let ((buffer-read-only nil))
10651     (let ((scored gnus-newsgroup-scored)
10652           headers h)
10653       (while scored
10654         (unless (gnus-summary-article-header (caar scored))
10655           (and (setq h (gnus-number-to-header (caar scored)))
10656                (< (cdar scored) gnus-summary-expunge-below)
10657                (push h headers)))
10658         (setq scored (cdr scored)))
10659       (if (not headers)
10660           (when (not no-error)
10661             (error "No expunged articles hidden"))
10662         (goto-char (point-min))
10663         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10664         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10665         (mapcar (lambda (x) (push (mail-header-number x)
10666                                   gnus-newsgroup-limit))
10667                 headers)
10668         (gnus-summary-prepare-unthreaded (nreverse headers))
10669         (goto-char (point-min))
10670         (gnus-summary-position-point)
10671         t))))
10672
10673 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10674   "Mark all unread articles in this newsgroup as read.
10675 If prefix argument ALL is non-nil, ticked and dormant articles will
10676 also be marked as read.
10677 If QUIETLY is non-nil, no questions will be asked.
10678
10679 If TO-HERE is non-nil, it should be a point in the buffer.  All
10680 articles before (after, if REVERSE is set) this point will be marked
10681 as read.
10682
10683 Note that this function will only catch up the unread article
10684 in the current summary buffer limitation.
10685
10686 The number of articles marked as read is returned."
10687   (interactive "P")
10688   (prog1
10689       (save-excursion
10690         (when (or quietly
10691                   (not gnus-interactive-catchup) ;Without confirmation?
10692                   gnus-expert-user
10693                   (gnus-y-or-n-p
10694                    (if all
10695                        "Mark absolutely all articles as read? "
10696                      "Mark all unread articles as read? ")))
10697           (if (and not-mark
10698                    (not gnus-newsgroup-adaptive)
10699                    (not gnus-newsgroup-auto-expire)
10700                    (not gnus-suppress-duplicates)
10701                    (or (not gnus-use-cache)
10702                        (eq gnus-use-cache 'passive)))
10703               (progn
10704                 (when all
10705                   (setq gnus-newsgroup-marked nil
10706                         gnus-newsgroup-spam-marked nil
10707                         gnus-newsgroup-dormant nil))
10708                 (setq gnus-newsgroup-unreads
10709                       (gnus-sorted-nunion
10710                        (gnus-sorted-intersection gnus-newsgroup-unreads
10711                                                  gnus-newsgroup-downloadable)
10712                        gnus-newsgroup-unfetched)))
10713             ;; We actually mark all articles as canceled, which we
10714             ;; have to do when using auto-expiry or adaptive scoring.
10715             (gnus-summary-show-all-threads)
10716             (if (and to-here reverse)
10717                 (progn
10718                   (goto-char to-here)
10719                   (gnus-summary-mark-current-read-and-unread-as-read
10720                    gnus-catchup-mark)
10721                   (while (gnus-summary-find-next (not all))
10722                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10723               (when (gnus-summary-first-subject (not all))
10724                 (while (and
10725                         (if to-here (< (point) to-here) t)
10726                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10727                         (gnus-summary-find-next (not all))))))
10728             (gnus-set-mode-line 'summary))
10729           t))
10730     (gnus-summary-position-point)))
10731
10732 (defun gnus-summary-catchup-to-here (&optional all)
10733   "Mark all unticked articles before the current one as read.
10734 If ALL is non-nil, also mark ticked and dormant articles as read."
10735   (interactive "P")
10736   (save-excursion
10737     (gnus-save-hidden-threads
10738       (let ((beg (point)))
10739         ;; We check that there are unread articles.
10740         (when (or all (gnus-summary-find-prev))
10741           (gnus-summary-catchup all t beg)))))
10742   (gnus-summary-position-point))
10743
10744 (defun gnus-summary-catchup-from-here (&optional all)
10745   "Mark all unticked articles after (and including) the current one as read.
10746 If ALL is non-nil, also mark ticked and dormant articles as read."
10747   (interactive "P")
10748   (save-excursion
10749     (gnus-save-hidden-threads
10750       (let ((beg (point)))
10751         ;; We check that there are unread articles.
10752         (when (or all (gnus-summary-find-next))
10753           (gnus-summary-catchup all t beg nil t)))))
10754   (gnus-summary-position-point))
10755
10756 (defun gnus-summary-catchup-all (&optional quietly)
10757   "Mark all articles in this newsgroup as read.
10758 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10759 instead, which marks only unread articles as read."
10760   (interactive "P")
10761   (gnus-summary-catchup t quietly))
10762
10763 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10764   "Mark all unread articles in this group as read, then exit.
10765 If prefix argument ALL is non-nil, all articles are marked as read.
10766 If QUIETLY is non-nil, no questions will be asked."
10767   (interactive "P")
10768   (when (gnus-summary-catchup all quietly nil 'fast)
10769     ;; Select next newsgroup or exit.
10770     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10771              (eq gnus-auto-select-next 'quietly))
10772         (gnus-summary-next-group nil)
10773       (gnus-summary-exit))))
10774
10775 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10776   "Mark all articles in this newsgroup as read, and then exit.
10777 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10778 instead, which marks only unread articles as read."
10779   (interactive "P")
10780   (gnus-summary-catchup-and-exit t quietly))
10781
10782 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10783   "Mark all articles in this group as read and select the next group.
10784 If given a prefix, mark all articles, unread as well as ticked, as
10785 read."
10786   (interactive "P")
10787   (save-excursion
10788     (gnus-summary-catchup all))
10789   (gnus-summary-next-group))
10790
10791 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10792   "Mark all articles in this group as read and select the previous group.
10793 If given a prefix, mark all articles, unread as well as ticked, as
10794 read."
10795   (interactive "P")
10796   (save-excursion
10797     (gnus-summary-catchup all))
10798   (gnus-summary-next-group nil nil t))
10799
10800 ;;;
10801 ;;; with article
10802 ;;;
10803
10804 (defmacro gnus-with-article (article &rest forms)
10805   "Select ARTICLE and perform FORMS in the original article buffer.
10806 Then replace the article with the result."
10807   `(progn
10808      ;; We don't want the article to be marked as read.
10809      (let (gnus-mark-article-hook)
10810        (gnus-summary-select-article t t nil ,article))
10811      (set-buffer gnus-original-article-buffer)
10812      ,@forms
10813      (if (not (gnus-check-backend-function
10814                'request-replace-article (car gnus-article-current)))
10815          (gnus-message 5 "Read-only group; not replacing")
10816        (unless (gnus-request-replace-article
10817                 ,article (car gnus-article-current)
10818                 (current-buffer) t)
10819          (error "Couldn't replace article")))
10820      ;; The cache and backlog have to be flushed somewhat.
10821      (when gnus-keep-backlog
10822        (gnus-backlog-remove-article
10823         (car gnus-article-current) (cdr gnus-article-current)))
10824      (when gnus-use-cache
10825        (gnus-cache-update-article
10826         (car gnus-article-current) (cdr gnus-article-current)))))
10827
10828 (put 'gnus-with-article 'lisp-indent-function 1)
10829 (put 'gnus-with-article 'edebug-form-spec '(form body))
10830
10831 ;; Thread-based commands.
10832
10833 (defun gnus-summary-articles-in-thread (&optional article)
10834   "Return a list of all articles in the current thread.
10835 If ARTICLE is non-nil, return all articles in the thread that starts
10836 with that article."
10837   (let* ((article (or article (gnus-summary-article-number)))
10838          (data (gnus-data-find-list article))
10839          (top-level (gnus-data-level (car data)))
10840          (top-subject
10841           (cond ((null gnus-thread-operation-ignore-subject)
10842                  (gnus-simplify-subject-re
10843                   (mail-header-subject (gnus-data-header (car data)))))
10844                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10845                  (gnus-simplify-subject-fuzzy
10846                   (mail-header-subject (gnus-data-header (car data)))))
10847                 (t nil)))
10848          (end-point (save-excursion
10849                       (if (gnus-summary-go-to-next-thread)
10850                           (point) (point-max))))
10851          articles)
10852     (while (and data
10853                 (< (gnus-data-pos (car data)) end-point))
10854       (when (or (not top-subject)
10855                 (string= top-subject
10856                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10857                              (gnus-simplify-subject-fuzzy
10858                               (mail-header-subject
10859                                (gnus-data-header (car data))))
10860                            (gnus-simplify-subject-re
10861                             (mail-header-subject
10862                              (gnus-data-header (car data)))))))
10863         (push (gnus-data-number (car data)) articles))
10864       (unless (and (setq data (cdr data))
10865                    (> (gnus-data-level (car data)) top-level))
10866         (setq data nil)))
10867     ;; Return the list of articles.
10868     (nreverse articles)))
10869
10870 (defun gnus-summary-rethread-current ()
10871   "Rethread the thread the current article is part of."
10872   (interactive)
10873   (let* ((gnus-show-threads t)
10874          (article (gnus-summary-article-number))
10875          (id (mail-header-id (gnus-summary-article-header)))
10876          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10877     (unless id
10878       (error "No article on the current line"))
10879     (gnus-rebuild-thread id)
10880     (gnus-summary-goto-subject article)))
10881
10882 (defun gnus-summary-reparent-thread ()
10883   "Make the current article child of the marked (or previous) article.
10884
10885 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10886 is non-nil or the Subject: of both articles are the same."
10887   (interactive)
10888   (unless (not (gnus-group-read-only-p))
10889     (error "The current newsgroup does not support article editing"))
10890   (unless (<= (length gnus-newsgroup-processable) 1)
10891     (error "No more than one article may be marked"))
10892   (let ((child (gnus-summary-article-number))
10893         ;; First grab the marked article, otherwise one line up.
10894         (parent (if (not (null gnus-newsgroup-processable))
10895                     (car gnus-newsgroup-processable)
10896                   (save-excursion
10897                     (if (eq (forward-line -1) 0)
10898                         (gnus-summary-article-number)
10899                       (error "Beginning of summary buffer"))))))
10900     (gnus-summary-reparent-children parent (list child))))
10901
10902 (defun gnus-summary-reparent-children (parent children)
10903   "Make PARENT the parent of CHILDREN.
10904 When called interactively, PARENT is is current article and
10905 CHILDREN are the process-marked articles."
10906   (interactive
10907    (list (gnus-summary-article-number)
10908          (gnus-summary-work-articles nil)))
10909   (dolist (child children)
10910     (save-window-excursion
10911       (let ((gnus-article-buffer " *reparent*"))
10912         (unless (not (eq parent child))
10913           (error "An article may not be self-referential"))
10914         (let ((message-id (mail-header-id
10915                            (gnus-summary-article-header parent))))
10916           (unless (and message-id (not (equal message-id "")))
10917             (error "No message-id in desired parent"))
10918           (gnus-with-article child
10919             (save-restriction
10920               (goto-char (point-min))
10921               (message-narrow-to-head)
10922               (if (re-search-forward "^References: " nil t)
10923                   (progn
10924                     (re-search-forward "^[^ \t]" nil t)
10925                     (forward-line -1)
10926                     (end-of-line)
10927                     (insert " " message-id))
10928                 (insert "References: " message-id "\n"))))
10929           (set-buffer gnus-summary-buffer)
10930           (gnus-summary-unmark-all-processable)
10931           (gnus-summary-update-article child)
10932           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10933             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10934           (gnus-summary-rethread-current)
10935           (gnus-message 3 "Article %d is now the child of article %d"
10936                         child parent))))))
10937
10938 (defun gnus-summary-toggle-threads (&optional arg)
10939   "Toggle showing conversation threads.
10940 If ARG is positive number, turn showing conversation threads on."
10941   (interactive "P")
10942   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10943     (setq gnus-show-threads
10944           (if (null arg) (not gnus-show-threads)
10945             (> (prefix-numeric-value arg) 0)))
10946     (gnus-summary-prepare)
10947     (gnus-summary-goto-subject current)
10948     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10949     (gnus-summary-position-point)))
10950
10951 (defun gnus-summary-show-all-threads ()
10952   "Show all threads."
10953   (interactive)
10954   (save-excursion
10955     (let ((buffer-read-only nil))
10956       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10957   (gnus-summary-position-point))
10958
10959 (defun gnus-summary-show-thread ()
10960   "Show thread subtrees.
10961 Returns nil if no thread was there to be shown."
10962   (interactive)
10963   (let ((buffer-read-only nil)
10964         (orig (point))
10965         (end (point-at-eol))
10966         ;; Leave point at bol
10967         (beg (progn (beginning-of-line) (point))))
10968     (prog1
10969         ;; Any hidden lines here?
10970         (search-forward "\r" end t)
10971       (subst-char-in-region beg end ?\^M ?\n t)
10972       (goto-char orig)
10973       (gnus-summary-position-point))))
10974
10975 (defun gnus-summary-maybe-hide-threads ()
10976   "If requested, hide the threads that should be hidden."
10977   (when (and gnus-show-threads
10978              gnus-thread-hide-subtree)
10979     (gnus-summary-hide-all-threads
10980      (if (or (consp gnus-thread-hide-subtree)
10981              (functionp gnus-thread-hide-subtree))
10982          (gnus-make-predicate gnus-thread-hide-subtree)
10983        nil))))
10984
10985 ;;; Hiding predicates.
10986
10987 (defun gnus-article-unread-p (header)
10988   (memq (mail-header-number header) gnus-newsgroup-unreads))
10989
10990 (defun gnus-article-unseen-p (header)
10991   (memq (mail-header-number header) gnus-newsgroup-unseen))
10992
10993 (defun gnus-map-articles (predicate articles)
10994   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10995   (apply 'gnus-or (mapcar predicate
10996                           (mapcar (lambda (number)
10997                                     (gnus-summary-article-header number))
10998                                   articles))))
10999
11000 (defun gnus-summary-hide-all-threads (&optional predicate)
11001   "Hide all thread subtrees.
11002 If PREDICATE is supplied, threads that satisfy this predicate
11003 will not be hidden."
11004   (interactive)
11005   (save-excursion
11006     (goto-char (point-min))
11007     (let ((end nil))
11008       (while (not end)
11009         (when (or (not predicate)
11010                   (gnus-map-articles
11011                    predicate (gnus-summary-article-children)))
11012             (gnus-summary-hide-thread))
11013         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11014   (gnus-summary-position-point))
11015
11016 (defun gnus-summary-hide-thread ()
11017   "Hide thread subtrees.
11018 If PREDICATE is supplied, threads that satisfy this predicate
11019 will not be hidden.
11020 Returns nil if no threads were there to be hidden."
11021   (interactive)
11022   (let ((buffer-read-only nil)
11023         (start (point))
11024         (article (gnus-summary-article-number)))
11025     (goto-char start)
11026     ;; Go forward until either the buffer ends or the subthread
11027     ;; ends.
11028     (when (and (not (eobp))
11029                (or (zerop (gnus-summary-next-thread 1 t))
11030                    (goto-char (point-max))))
11031       (prog1
11032           (if (and (> (point) start)
11033                    (search-backward "\n" start t))
11034               (progn
11035                 (subst-char-in-region start (point) ?\n ?\^M)
11036                 (gnus-summary-goto-subject article))
11037             (goto-char start)
11038             nil)))))
11039
11040 (defun gnus-summary-go-to-next-thread (&optional previous)
11041   "Go to the same level (or less) next thread.
11042 If PREVIOUS is non-nil, go to previous thread instead.
11043 Return the article number moved to, or nil if moving was impossible."
11044   (let ((level (gnus-summary-thread-level))
11045         (way (if previous -1 1))
11046         (beg (point)))
11047     (forward-line way)
11048     (while (and (not (eobp))
11049                 (< level (gnus-summary-thread-level)))
11050       (forward-line way))
11051     (if (eobp)
11052         (progn
11053           (goto-char beg)
11054           nil)
11055       (setq beg (point))
11056       (prog1
11057           (gnus-summary-article-number)
11058         (goto-char beg)))))
11059
11060 (defun gnus-summary-next-thread (n &optional silent)
11061   "Go to the same level next N'th thread.
11062 If N is negative, search backward instead.
11063 Returns the difference between N and the number of skips actually
11064 done.
11065
11066 If SILENT, don't output messages."
11067   (interactive "p")
11068   (let ((backward (< n 0))
11069         (n (abs n)))
11070     (while (and (> n 0)
11071                 (gnus-summary-go-to-next-thread backward))
11072       (decf n))
11073     (unless silent
11074       (gnus-summary-position-point))
11075     (when (and (not silent) (/= 0 n))
11076       (gnus-message 7 "No more threads"))
11077     n))
11078
11079 (defun gnus-summary-prev-thread (n)
11080   "Go to the same level previous N'th thread.
11081 Returns the difference between N and the number of skips actually
11082 done."
11083   (interactive "p")
11084   (gnus-summary-next-thread (- n)))
11085
11086 (defun gnus-summary-go-down-thread ()
11087   "Go down one level in the current thread."
11088   (let ((children (gnus-summary-article-children)))
11089     (when children
11090       (gnus-summary-goto-subject (car children)))))
11091
11092 (defun gnus-summary-go-up-thread ()
11093   "Go up one level in the current thread."
11094   (let ((parent (gnus-summary-article-parent)))
11095     (when parent
11096       (gnus-summary-goto-subject parent))))
11097
11098 (defun gnus-summary-down-thread (n)
11099   "Go down thread N steps.
11100 If N is negative, go up instead.
11101 Returns the difference between N and how many steps down that were
11102 taken."
11103   (interactive "p")
11104   (let ((up (< n 0))
11105         (n (abs n)))
11106     (while (and (> n 0)
11107                 (if up (gnus-summary-go-up-thread)
11108                   (gnus-summary-go-down-thread)))
11109       (setq n (1- n)))
11110     (gnus-summary-position-point)
11111     (when (/= 0 n)
11112       (gnus-message 7 "Can't go further"))
11113     n))
11114
11115 (defun gnus-summary-up-thread (n)
11116   "Go up thread N steps.
11117 If N is negative, go down instead.
11118 Returns the difference between N and how many steps down that were
11119 taken."
11120   (interactive "p")
11121   (gnus-summary-down-thread (- n)))
11122
11123 (defun gnus-summary-top-thread ()
11124   "Go to the top of the thread."
11125   (interactive)
11126   (while (gnus-summary-go-up-thread))
11127   (gnus-summary-article-number))
11128
11129 (defun gnus-summary-kill-thread (&optional unmark)
11130   "Mark articles under current thread as read.
11131 If the prefix argument is positive, remove any kinds of marks.
11132 If the prefix argument is negative, tick articles instead."
11133   (interactive "P")
11134   (when unmark
11135     (setq unmark (prefix-numeric-value unmark)))
11136   (let ((articles (gnus-summary-articles-in-thread)))
11137     (save-excursion
11138       ;; Expand the thread.
11139       (gnus-summary-show-thread)
11140       ;; Mark all the articles.
11141       (while articles
11142         (gnus-summary-goto-subject (car articles))
11143         (cond ((null unmark)
11144                (gnus-summary-mark-article-as-read gnus-killed-mark))
11145               ((> unmark 0)
11146                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11147               (t
11148                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11149         (setq articles (cdr articles))))
11150     ;; Hide killed subtrees.
11151     (and (null unmark)
11152          gnus-thread-hide-killed
11153          (gnus-summary-hide-thread))
11154     ;; If marked as read, go to next unread subject.
11155     (when (null unmark)
11156       ;; Go to next unread subject.
11157       (gnus-summary-next-subject 1 t)))
11158   (gnus-set-mode-line 'summary))
11159
11160 ;; Summary sorting commands
11161
11162 (defun gnus-summary-sort-by-number (&optional reverse)
11163   "Sort the summary buffer by article number.
11164 Argument REVERSE means reverse order."
11165   (interactive "P")
11166   (gnus-summary-sort 'number reverse))
11167
11168 (defun gnus-summary-sort-by-random (&optional reverse)
11169   "Randomize the order in the summary buffer.
11170 Argument REVERSE means to randomize in reverse order."
11171   (interactive "P")
11172   (gnus-summary-sort 'random reverse))
11173
11174 (defun gnus-summary-sort-by-author (&optional reverse)
11175   "Sort the summary buffer by author name alphabetically.
11176 If `case-fold-search' is non-nil, case of letters is ignored.
11177 Argument REVERSE means reverse order."
11178   (interactive "P")
11179   (gnus-summary-sort 'author reverse))
11180
11181 (defun gnus-summary-sort-by-recipient (&optional reverse)
11182   "Sort the summary buffer by recipient name alphabetically.
11183 If `case-fold-search' is non-nil, case of letters is ignored.
11184 Argument REVERSE means reverse order."
11185   (interactive "P")
11186   (gnus-summary-sort 'recipient reverse))
11187
11188 (defun gnus-summary-sort-by-subject (&optional reverse)
11189   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11190 If `case-fold-search' is non-nil, case of letters is ignored.
11191 Argument REVERSE means reverse order."
11192   (interactive "P")
11193   (gnus-summary-sort 'subject reverse))
11194
11195 (defun gnus-summary-sort-by-date (&optional reverse)
11196   "Sort the summary buffer by date.
11197 Argument REVERSE means reverse order."
11198   (interactive "P")
11199   (gnus-summary-sort 'date reverse))
11200
11201 (defun gnus-summary-sort-by-score (&optional reverse)
11202   "Sort the summary buffer by score.
11203 Argument REVERSE means reverse order."
11204   (interactive "P")
11205   (gnus-summary-sort 'score reverse))
11206
11207 (defun gnus-summary-sort-by-lines (&optional reverse)
11208   "Sort the summary buffer by the number of lines.
11209 Argument REVERSE means reverse order."
11210   (interactive "P")
11211   (gnus-summary-sort 'lines reverse))
11212
11213 (defun gnus-summary-sort-by-chars (&optional reverse)
11214   "Sort the summary buffer by article length.
11215 Argument REVERSE means reverse order."
11216   (interactive "P")
11217   (gnus-summary-sort 'chars reverse))
11218
11219 (defun gnus-summary-sort-by-original (&optional reverse)
11220   "Sort the summary buffer using the default sorting method.
11221 Argument REVERSE means reverse order."
11222   (interactive "P")
11223   (let* ((buffer-read-only)
11224          (gnus-summary-prepare-hook nil))
11225     ;; We do the sorting by regenerating the threads.
11226     (gnus-summary-prepare)
11227     ;; Hide subthreads if needed.
11228     (gnus-summary-maybe-hide-threads)))
11229
11230 (defun gnus-summary-sort (predicate reverse)
11231   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11232   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11233          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11234          (gnus-thread-sort-functions
11235           (if (not reverse)
11236               thread
11237             `(lambda (t1 t2)
11238                (,thread t2 t1))))
11239          (gnus-sort-gathered-threads-function
11240           gnus-thread-sort-functions)
11241          (gnus-article-sort-functions
11242           (if (not reverse)
11243               article
11244             `(lambda (t1 t2)
11245                (,article t2 t1))))
11246          (buffer-read-only)
11247          (gnus-summary-prepare-hook nil))
11248     ;; We do the sorting by regenerating the threads.
11249     (gnus-summary-prepare)
11250     ;; Hide subthreads if needed.
11251     (gnus-summary-maybe-hide-threads)))
11252
11253 ;; Summary saving commands.
11254
11255 (defun gnus-summary-save-article (&optional n not-saved)
11256   "Save the current article using the default saver function.
11257 If N is a positive number, save the N next articles.
11258 If N is a negative number, save the N previous articles.
11259 If N is nil and any articles have been marked with the process mark,
11260 save those articles instead.
11261 The variable `gnus-default-article-saver' specifies the saver function.
11262
11263 If the optional second argument NOT-SAVED is non-nil, articles saved
11264 will not be marked as saved."
11265   (interactive "P")
11266   (require 'gnus-art)
11267   (let* ((articles (gnus-summary-work-articles n))
11268          (save-buffer (save-excursion
11269                         (nnheader-set-temp-buffer " *Gnus Save*")))
11270          (num (length articles))
11271          ;; Whether to save decoded articles or raw articles.
11272          (decode (when gnus-article-save-coding-system
11273                    (get gnus-default-article-saver :decode)))
11274          ;; When saving many articles in a single file, use the other
11275          ;; function to save articles other than the first one.
11276          (saver2 (get gnus-default-article-saver :function))
11277          (gnus-prompt-before-saving (if saver2
11278                                         t
11279                                       gnus-prompt-before-saving))
11280          (gnus-default-article-saver gnus-default-article-saver)
11281          header file)
11282     (dolist (article articles)
11283       (setq header (gnus-summary-article-header article))
11284       (if (not (vectorp header))
11285           ;; This is a pseudo-article.
11286           (if (assq 'name header)
11287               (gnus-copy-file (cdr (assq 'name header)))
11288             (gnus-message 1 "Article %d is unsaveable" article))
11289         ;; This is a real article.
11290         (save-window-excursion
11291           (let ((gnus-display-mime-function (when decode
11292                                               gnus-display-mime-function))
11293                 (gnus-article-prepare-hook (when decode
11294                                              gnus-article-prepare-hook)))
11295             (gnus-summary-select-article t nil nil article)
11296             (gnus-summary-goto-subject article)))
11297         (save-excursion
11298           (set-buffer save-buffer)
11299           (erase-buffer)
11300           (insert-buffer-substring (if decode
11301                                        gnus-article-buffer
11302                                      gnus-original-article-buffer)))
11303         (setq file (gnus-article-save save-buffer file num))
11304         (gnus-summary-remove-process-mark article)
11305         (unless not-saved
11306           (gnus-summary-set-saved-mark article)))
11307       (when saver2
11308         (setq gnus-default-article-saver saver2
11309               saver2 nil)))
11310     (gnus-kill-buffer save-buffer)
11311     (gnus-summary-position-point)
11312     (gnus-set-mode-line 'summary)
11313     n))
11314
11315 (defun gnus-summary-pipe-output (&optional arg headers)
11316   "Pipe the current article to a subprocess.
11317 If N is a positive number, pipe the N next articles.
11318 If N is a negative number, pipe the N previous articles.
11319 If N is nil and any articles have been marked with the process mark,
11320 pipe those articles instead.
11321 If HEADERS (the symbolic prefix), include the headers, too."
11322   (interactive (gnus-interactive "P\ny"))
11323   (require 'gnus-art)
11324   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11325         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11326     (gnus-summary-save-article arg t))
11327   (let ((buffer (get-buffer "*Shell Command Output*")))
11328     (when (and buffer
11329                (not (zerop (buffer-size buffer))))
11330       (gnus-configure-windows 'pipe))))
11331
11332 (defun gnus-summary-save-article-mail (&optional arg)
11333   "Append the current article to a Unix mail box file.
11334 If N is a positive number, save the N next articles.
11335 If N is a negative number, save the N previous articles.
11336 If N is nil and any articles have been marked with the process mark,
11337 save those articles instead."
11338   (interactive "P")
11339   (require 'gnus-art)
11340   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11341     (gnus-summary-save-article arg)))
11342
11343 (defun gnus-summary-save-article-rmail (&optional arg)
11344   "Append the current article to an rmail file.
11345 If N is a positive number, save the N next articles.
11346 If N is a negative number, save the N previous articles.
11347 If N is nil and any articles have been marked with the process mark,
11348 save those articles instead."
11349   (interactive "P")
11350   (require 'gnus-art)
11351   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11352     (gnus-summary-save-article arg)))
11353
11354 (defun gnus-summary-save-article-file (&optional arg)
11355   "Append the current article to a file.
11356 If N is a positive number, save the N next articles.
11357 If N is a negative number, save the N previous articles.
11358 If N is nil and any articles have been marked with the process mark,
11359 save those articles instead."
11360   (interactive "P")
11361   (require 'gnus-art)
11362   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11363     (gnus-summary-save-article arg)))
11364
11365 (defun gnus-summary-write-article-file (&optional arg)
11366   "Write the current article to a file, deleting the previous file.
11367 If N is a positive number, save the N next articles.
11368 If N is a negative number, save the N previous articles.
11369 If N is nil and any articles have been marked with the process mark,
11370 save those articles instead."
11371   (interactive "P")
11372   (require 'gnus-art)
11373   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11374     (gnus-summary-save-article arg)))
11375
11376 (defun gnus-summary-save-article-body-file (&optional arg)
11377   "Append the current article body to a file.
11378 If N is a positive number, save the N next articles.
11379 If N is a negative number, save the N previous articles.
11380 If N is nil and any articles have been marked with the process mark,
11381 save those articles instead."
11382   (interactive "P")
11383   (require 'gnus-art)
11384   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11385     (gnus-summary-save-article arg)))
11386
11387 (defun gnus-summary-write-article-body-file (&optional arg)
11388   "Write the current article body to a file, deleting the previous file.
11389 If N is a positive number, save the N next articles.
11390 If N is a negative number, save the N previous articles.
11391 If N is nil and any articles have been marked with the process mark,
11392 save those articles instead."
11393   (interactive "P")
11394   (require 'gnus-art)
11395   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11396     (gnus-summary-save-article arg)))
11397
11398 (defun gnus-summary-muttprint (&optional arg)
11399   "Print the current article using Muttprint.
11400 If N is a positive number, save the N next articles.
11401 If N is a negative number, save the N previous articles.
11402 If N is nil and any articles have been marked with the process mark,
11403 save those articles instead."
11404   (interactive "P")
11405   (require 'gnus-art)
11406   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11407     (gnus-summary-save-article arg t)))
11408
11409 (defun gnus-summary-pipe-message (program)
11410   "Pipe the current article through PROGRAM."
11411   (interactive "sProgram: ")
11412   (gnus-summary-select-article)
11413   (let ((mail-header-separator ""))
11414     (gnus-eval-in-buffer-window gnus-article-buffer
11415       (save-restriction
11416         (widen)
11417         (let ((start (window-start))
11418               buffer-read-only)
11419           (message-pipe-buffer-body program)
11420           (set-window-start (get-buffer-window (current-buffer)) start))))))
11421
11422 (defun gnus-get-split-value (methods)
11423   "Return a value based on the split METHODS."
11424   (let (split-name method result match)
11425     (when methods
11426       (save-excursion
11427         (set-buffer gnus-original-article-buffer)
11428         (save-restriction
11429           (nnheader-narrow-to-headers)
11430           (while (and methods (not split-name))
11431             (goto-char (point-min))
11432             (setq method (pop methods))
11433             (setq match (car method))
11434             (when (cond
11435                    ((stringp match)
11436                     ;; Regular expression.
11437                     (ignore-errors
11438                       (re-search-forward match nil t)))
11439                    ((functionp match)
11440                     ;; Function.
11441                     (save-restriction
11442                       (widen)
11443                       (setq result (funcall match gnus-newsgroup-name))))
11444                    ((consp match)
11445                     ;; Form.
11446                     (save-restriction
11447                       (widen)
11448                       (setq result (eval match)))))
11449               (setq split-name (cdr method))
11450               (cond ((stringp result)
11451                      (push (expand-file-name
11452                             result gnus-article-save-directory)
11453                            split-name))
11454                     ((consp result)
11455                      (setq split-name (append result split-name)))))))))
11456     (nreverse split-name)))
11457
11458 (defun gnus-valid-move-group-p (group)
11459   (and (boundp group)
11460        (symbol-name group)
11461        (symbol-value group)
11462        (gnus-get-function (gnus-find-method-for-group
11463                            (symbol-name group)) 'request-accept-article t)))
11464
11465 (defun gnus-read-move-group-name (prompt default articles prefix)
11466   "Read a group name."
11467   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11468          (minibuffer-confirm-incomplete nil) ; XEmacs
11469          (prom
11470           (format "%s %s to"
11471                   prompt
11472                   (if (> (length articles) 1)
11473                       (format "these %d articles" (length articles))
11474                     "this article")))
11475          (to-newsgroup
11476           (cond
11477            ((null split-name)
11478             (gnus-completing-read-with-default
11479              default prom
11480              gnus-active-hashtb
11481              'gnus-valid-move-group-p
11482              nil prefix
11483              'gnus-group-history))
11484            ((= 1 (length split-name))
11485             (gnus-completing-read-with-default
11486              (car split-name) prom
11487              gnus-active-hashtb
11488              'gnus-valid-move-group-p
11489              nil nil
11490              'gnus-group-history))
11491            (t
11492             (gnus-completing-read-with-default
11493              nil prom
11494              (mapcar 'list (nreverse split-name))
11495              nil nil nil
11496              'gnus-group-history))))
11497          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11498     (when to-newsgroup
11499       (if (or (string= to-newsgroup "")
11500               (string= to-newsgroup prefix))
11501           (setq to-newsgroup default))
11502       (unless to-newsgroup
11503         (error "No group name entered"))
11504       (or (gnus-active to-newsgroup)
11505           (gnus-activate-group to-newsgroup nil nil to-method)
11506           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11507                                      to-newsgroup))
11508               (or (and (gnus-request-create-group to-newsgroup to-method)
11509                        (gnus-activate-group
11510                         to-newsgroup nil nil to-method)
11511                        (gnus-subscribe-group to-newsgroup))
11512                   (error "Couldn't create group %s" to-newsgroup)))
11513           (error "No such group: %s" to-newsgroup)))
11514     to-newsgroup))
11515
11516 (defvar gnus-summary-save-parts-counter)
11517
11518 (defun gnus-summary-save-parts (type dir n &optional reverse)
11519   "Save parts matching TYPE to DIR.
11520 If REVERSE, save parts that do not match TYPE."
11521   (interactive
11522    (list (read-string "Save parts of type: "
11523                       (or (car gnus-summary-save-parts-type-history)
11524                           gnus-summary-save-parts-default-mime)
11525                       'gnus-summary-save-parts-type-history)
11526          (setq gnus-summary-save-parts-last-directory
11527                (read-file-name "Save to directory: "
11528                                gnus-summary-save-parts-last-directory
11529                                nil t))
11530          current-prefix-arg))
11531   (gnus-summary-iterate n
11532     (let ((gnus-display-mime-function nil)
11533           (gnus-inhibit-treatment t))
11534       (gnus-summary-select-article))
11535     (save-excursion
11536       (set-buffer gnus-article-buffer)
11537       (let ((handles (or gnus-article-mime-handles
11538                          (mm-dissect-buffer nil gnus-article-loose-mime)
11539                          (and gnus-article-emulate-mime
11540                               (mm-uu-dissect))))
11541             (gnus-summary-save-parts-counter 1))
11542         (when handles
11543           (gnus-summary-save-parts-1 type dir handles reverse)
11544           (unless gnus-article-mime-handles ;; Don't destroy this case.
11545             (mm-destroy-parts handles)))))))
11546
11547 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11548   (if (stringp (car handle))
11549       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11550               (cdr handle))
11551     (when (if reverse
11552               (not (string-match type (mm-handle-media-type handle)))
11553             (string-match type (mm-handle-media-type handle)))
11554       (let ((file (expand-file-name
11555                    (gnus-map-function
11556                     mm-file-name-rewrite-functions
11557                     (file-name-nondirectory
11558                      (or
11559                       (mail-content-type-get
11560                        (mm-handle-disposition handle) 'filename)
11561                       (mail-content-type-get
11562                        (mm-handle-type handle) 'name)
11563                       (format "%s.%d.%d" gnus-newsgroup-name
11564                               (cdr gnus-article-current)
11565                               gnus-summary-save-parts-counter))))
11566                    dir)))
11567         (incf gnus-summary-save-parts-counter)
11568         (unless (file-exists-p file)
11569           (mm-save-part-to-file handle file))))))
11570
11571 ;; Summary extract commands
11572
11573 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11574   (let ((buffer-read-only nil)
11575         (article (gnus-summary-article-number))
11576         after-article b e)
11577     (unless (gnus-summary-goto-subject article)
11578       (error "No such article: %d" article))
11579     (gnus-summary-position-point)
11580     ;; If all commands are to be bunched up on one line, we collect
11581     ;; them here.
11582     (unless gnus-view-pseudos-separately
11583       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11584             files action)
11585         (while ps
11586           (setq action (cdr (assq 'action (car ps))))
11587           (setq files (list (cdr (assq 'name (car ps)))))
11588           (while (and ps (cdr ps)
11589                       (string= (or action "1")
11590                                (or (cdr (assq 'action (cadr ps))) "2")))
11591             (push (cdr (assq 'name (cadr ps))) files)
11592             (setcdr ps (cddr ps)))
11593           (when files
11594             (when (not (string-match "%s" action))
11595               (push " " files))
11596             (push " " files)
11597             (when (assq 'execute (car ps))
11598               (setcdr (assq 'execute (car ps))
11599                       (funcall (if (string-match "%s" action)
11600                                    'format 'concat)
11601                                action
11602                                (mapconcat
11603                                 (lambda (f)
11604                                   (if (equal f " ")
11605                                       f
11606                                     (shell-quote-argument f)))
11607                                 files " ")))))
11608           (setq ps (cdr ps)))))
11609     (if (and gnus-view-pseudos (not not-view))
11610         (while pslist
11611           (when (assq 'execute (car pslist))
11612             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11613                                   (eq gnus-view-pseudos 'not-confirm)))
11614           (setq pslist (cdr pslist)))
11615       (save-excursion
11616         (while pslist
11617           (setq after-article (or (cdr (assq 'article (car pslist)))
11618                                   (gnus-summary-article-number)))
11619           (gnus-summary-goto-subject after-article)
11620           (forward-line 1)
11621           (setq b (point))
11622           (insert "    " (file-name-nondirectory
11623                           (cdr (assq 'name (car pslist))))
11624                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11625           (setq e (point))
11626           (forward-line -1)             ; back to `b'
11627           (gnus-add-text-properties
11628            b (1- e) (list 'gnus-number gnus-reffed-article-number
11629                           gnus-mouse-face-prop gnus-mouse-face))
11630           (gnus-data-enter
11631            after-article gnus-reffed-article-number
11632            gnus-unread-mark b (car pslist) 0 (- e b))
11633           (setq gnus-newsgroup-unreads
11634                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11635                                          gnus-reffed-article-number))
11636           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11637           (setq pslist (cdr pslist)))))))
11638
11639 (defun gnus-pseudos< (p1 p2)
11640   (let ((c1 (cdr (assq 'action p1)))
11641         (c2 (cdr (assq 'action p2))))
11642     (and c1 c2 (string< c1 c2))))
11643
11644 (defun gnus-request-pseudo-article (props)
11645   (cond ((assq 'execute props)
11646          (gnus-execute-command (cdr (assq 'execute props)))))
11647   (let ((gnus-current-article (gnus-summary-article-number)))
11648     (gnus-run-hooks 'gnus-mark-article-hook)))
11649
11650 (defun gnus-execute-command (command &optional automatic)
11651   (save-excursion
11652     (gnus-article-setup-buffer)
11653     (set-buffer gnus-article-buffer)
11654     (setq buffer-read-only nil)
11655     (let ((command (if automatic command
11656                      (read-string "Command: " (cons command 0)))))
11657       (erase-buffer)
11658       (insert "$ " command "\n\n")
11659       (if gnus-view-pseudo-asynchronously
11660           (start-process "gnus-execute" (current-buffer) shell-file-name
11661                          shell-command-switch command)
11662         (call-process shell-file-name nil t nil
11663                       shell-command-switch command)))))
11664
11665 ;; Summary kill commands.
11666
11667 (defun gnus-summary-edit-global-kill (article)
11668   "Edit the \"global\" kill file."
11669   (interactive (list (gnus-summary-article-number)))
11670   (gnus-group-edit-global-kill article))
11671
11672 (defun gnus-summary-edit-local-kill ()
11673   "Edit a local kill file applied to the current newsgroup."
11674   (interactive)
11675   (setq gnus-current-headers (gnus-summary-article-header))
11676   (gnus-group-edit-local-kill
11677    (gnus-summary-article-number) gnus-newsgroup-name))
11678
11679 ;;; Header reading.
11680
11681 (defun gnus-read-header (id &optional header)
11682   "Read the headers of article ID and enter them into the Gnus system."
11683   (let ((group gnus-newsgroup-name)
11684         (gnus-override-method
11685          (or
11686           gnus-override-method
11687           (and (gnus-news-group-p gnus-newsgroup-name)
11688                (car (gnus-refer-article-methods)))))
11689         where)
11690     ;; First we check to see whether the header in question is already
11691     ;; fetched.
11692     (if (stringp id)
11693         ;; This is a Message-ID.
11694         (setq header (or header (gnus-id-to-header id)))
11695       ;; This is an article number.
11696       (setq header (or header (gnus-summary-article-header id))))
11697     (if (and header
11698              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11699         ;; We have found the header.
11700         header
11701       ;; We have to really fetch the header to this article.
11702       (save-excursion
11703         (set-buffer nntp-server-buffer)
11704         (when (setq where (gnus-request-head id group))
11705           (nnheader-fold-continuation-lines)
11706           (goto-char (point-max))
11707           (insert ".\n")
11708           (goto-char (point-min))
11709           (insert "211 ")
11710           (princ (cond
11711                   ((numberp id) id)
11712                   ((cdr where) (cdr where))
11713                   (header (mail-header-number header))
11714                   (t gnus-reffed-article-number))
11715                  (current-buffer))
11716           (insert " Article retrieved.\n"))
11717         (if (or (not where)
11718                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11719             ()                          ; Malformed head.
11720           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11721             (when (and (stringp id)
11722                        (or
11723                         (not (string= (gnus-group-real-name group)
11724                                       (car where)))
11725                         (not (gnus-server-equal gnus-override-method
11726                                                 (gnus-group-method group)))))
11727               ;; If we fetched by Message-ID and the article came from
11728               ;; a different group (or server), we fudge some bogus
11729               ;; article numbers for this article.
11730               (mail-header-set-number header gnus-reffed-article-number))
11731             (save-excursion
11732               (set-buffer gnus-summary-buffer)
11733               (decf gnus-reffed-article-number)
11734               (gnus-remove-header (mail-header-number header))
11735               (push header gnus-newsgroup-headers)
11736               (setq gnus-current-headers header)
11737               (push (mail-header-number header) gnus-newsgroup-limit)))
11738           header)))))
11739
11740 (defun gnus-remove-header (number)
11741   "Remove header NUMBER from `gnus-newsgroup-headers'."
11742   (if (and gnus-newsgroup-headers
11743            (= number (mail-header-number (car gnus-newsgroup-headers))))
11744       (pop gnus-newsgroup-headers)
11745     (let ((headers gnus-newsgroup-headers))
11746       (while (and (cdr headers)
11747                   (not (= number (mail-header-number (cadr headers)))))
11748         (pop headers))
11749       (when (cdr headers)
11750         (setcdr headers (cddr headers))))))
11751
11752 ;;;
11753 ;;; summary highlights
11754 ;;;
11755
11756 (defun gnus-highlight-selected-summary ()
11757   "Highlight selected article in summary buffer."
11758   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11759   (when gnus-summary-selected-face
11760     (save-excursion
11761       (let* ((beg (point-at-bol))
11762              (end (point-at-eol))
11763              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11764              (from (if (get-text-property beg gnus-mouse-face-prop)
11765                        beg
11766                      (or (next-single-property-change
11767                           beg gnus-mouse-face-prop nil end)
11768                          beg)))
11769              (to
11770               (if (= from end)
11771                   (- from 2)
11772                 (or (next-single-property-change
11773                      from gnus-mouse-face-prop nil end)
11774                     end))))
11775         ;; If no mouse-face prop on line we will have to = from = end,
11776         ;; so we highlight the entire line instead.
11777         (when (= (+ to 2) from)
11778           (setq from beg)
11779           (setq to end))
11780         (if gnus-newsgroup-selected-overlay
11781             ;; Move old overlay.
11782             (gnus-move-overlay
11783              gnus-newsgroup-selected-overlay from to (current-buffer))
11784           ;; Create new overlay.
11785           (gnus-overlay-put
11786            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11787            'face gnus-summary-selected-face))))))
11788
11789 (defvar gnus-summary-highlight-line-cached nil)
11790 (defvar gnus-summary-highlight-line-trigger nil)
11791
11792 (defun gnus-summary-highlight-line-0 ()
11793   (if (and (eq gnus-summary-highlight-line-trigger
11794                gnus-summary-highlight)
11795            gnus-summary-highlight-line-cached)
11796       gnus-summary-highlight-line-cached
11797     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11798           gnus-summary-highlight-line-cached
11799           (let* ((cond (list 'cond))
11800                  (c cond)
11801                  (list gnus-summary-highlight))
11802             (while list
11803               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11804                               nil))
11805               (setq c (cdr c)
11806                     list (cdr list)))
11807             (gnus-byte-compile (list 'lambda nil cond))))))
11808
11809 (defun gnus-summary-highlight-line ()
11810   "Highlight current line according to `gnus-summary-highlight'."
11811   (let* ((beg (point-at-bol))
11812          (article (or (gnus-summary-article-number) gnus-current-article))
11813          (score (or (cdr (assq article
11814                                gnus-newsgroup-scored))
11815                     gnus-summary-default-score 0))
11816          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11817          (inhibit-read-only t)
11818          (default gnus-summary-default-score)
11819          (default-high gnus-summary-default-high-score)
11820          (default-low gnus-summary-default-low-score)
11821          (uncached (and gnus-summary-use-undownloaded-faces
11822                         (memq article gnus-newsgroup-undownloaded)
11823                         (not (memq article gnus-newsgroup-cached)))))
11824     (let ((face (funcall (gnus-summary-highlight-line-0))))
11825       (unless (eq face (get-text-property beg 'face))
11826         (gnus-put-text-property-excluding-characters-with-faces
11827          beg (point-at-eol) 'face
11828          (setq face (if (boundp face) (symbol-value face) face)))
11829         (when gnus-summary-highlight-line-function
11830           (funcall gnus-summary-highlight-line-function article face))))))
11831
11832 (defun gnus-update-read-articles (group unread &optional compute)
11833   "Update the list of read articles in GROUP.
11834 UNREAD is a sorted list."
11835   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11836         (info (gnus-get-info group))
11837         (prev 1)
11838         read)
11839     (if (or (not info) (not active))
11840         ;; There is no info on this group if it was, in fact,
11841         ;; killed.  Gnus stores no information on killed groups, so
11842         ;; there's nothing to be done.
11843         ;; One could store the information somewhere temporarily,
11844         ;; perhaps...  Hmmm...
11845         ()
11846       ;; Remove any negative articles numbers.
11847       (while (and unread (< (car unread) 0))
11848         (setq unread (cdr unread)))
11849       ;; Remove any expired article numbers
11850       (while (and unread (< (car unread) (car active)))
11851         (setq unread (cdr unread)))
11852       ;; Compute the ranges of read articles by looking at the list of
11853       ;; unread articles.
11854       (while unread
11855         (when (/= (car unread) prev)
11856           (push (if (= prev (1- (car unread))) prev
11857                   (cons prev (1- (car unread))))
11858                 read))
11859         (setq prev (1+ (car unread)))
11860         (setq unread (cdr unread)))
11861       (when (<= prev (cdr active))
11862         (push (cons prev (cdr active)) read))
11863       (setq read (if (> (length read) 1) (nreverse read) read))
11864       (if compute
11865           read
11866         (save-excursion
11867           (let (setmarkundo)
11868             ;; Propagate the read marks to the backend.
11869             (when (gnus-check-backend-function 'request-set-mark group)
11870               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11871                     (add (gnus-remove-from-range read (gnus-info-read info))))
11872                 (when (or add del)
11873                   (unless (gnus-check-group group)
11874                     (error "Can't open server for %s" group))
11875                   (gnus-request-set-mark
11876                    group (delq nil (list (if add (list add 'add '(read)))
11877                                          (if del (list del 'del '(read))))))
11878                   (setq setmarkundo
11879                         `(gnus-request-set-mark
11880                           ,group
11881                           ',(delq nil (list
11882                                        (if del (list del 'add '(read)))
11883                                        (if add (list add 'del '(read))))))))))
11884             (set-buffer gnus-group-buffer)
11885             (gnus-undo-register
11886               `(progn
11887                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11888                  (gnus-info-set-read ',info ',(gnus-info-read info))
11889                  (gnus-get-unread-articles-in-group ',info
11890                                                     (gnus-active ,group))
11891                  (gnus-group-update-group ,group t)
11892                  ,setmarkundo))))
11893         ;; Enter this list into the group info.
11894         (gnus-info-set-read info read)
11895         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11896         (gnus-get-unread-articles-in-group info (gnus-active group))
11897         t))))
11898
11899 (defun gnus-offer-save-summaries ()
11900   "Offer to save all active summary buffers."
11901   (let (buffers)
11902     ;; Go through all buffers and find all summaries.
11903     (dolist (buffer (buffer-list))
11904       (when (and (setq buffer (buffer-name buffer))
11905                  (string-match "Summary" buffer)
11906                  (with-current-buffer buffer
11907                    ;; We check that this is, indeed, a summary buffer.
11908                    (and (eq major-mode 'gnus-summary-mode)
11909                         ;; Also make sure this isn't bogus.
11910                         gnus-newsgroup-prepared
11911                         ;; Also make sure that this isn't a
11912                         ;; dead summary buffer.
11913                         (not gnus-dead-summary-mode))))
11914         (push buffer buffers)))
11915     ;; Go through all these summary buffers and offer to save them.
11916     (when buffers
11917       (save-excursion
11918         (map-y-or-n-p
11919          "Update summary buffer %s? "
11920          (lambda (buf)
11921            (switch-to-buffer buf)
11922            (gnus-summary-exit))
11923          buffers)))))
11924
11925 (defun gnus-summary-setup-default-charset ()
11926   "Setup newsgroup default charset."
11927   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11928       (setq gnus-newsgroup-charset nil)
11929     (let* ((ignored-charsets
11930             (or gnus-newsgroup-ephemeral-ignored-charsets
11931                 (append
11932                  (and gnus-newsgroup-name
11933                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11934                  gnus-newsgroup-ignored-charsets))))
11935       (setq gnus-newsgroup-charset
11936             (or gnus-newsgroup-ephemeral-charset
11937                 (and gnus-newsgroup-name
11938                      (gnus-parameter-charset gnus-newsgroup-name))
11939                 gnus-default-charset))
11940       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11941            ignored-charsets))))
11942
11943 ;;;
11944 ;;; Mime Commands
11945 ;;;
11946
11947 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11948   "Display the current article buffer fully MIME-buttonized.
11949 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11950 treated as multipart/mixed."
11951   (interactive "P")
11952   (require 'gnus-art)
11953   (let ((gnus-unbuttonized-mime-types nil)
11954         (gnus-mime-display-multipart-as-mixed show-all-parts))
11955     (gnus-summary-show-article)))
11956
11957 (defun gnus-summary-repair-multipart (article)
11958   "Add a Content-Type header to a multipart article without one."
11959   (interactive (list (gnus-summary-article-number)))
11960   (gnus-with-article article
11961     (message-narrow-to-head)
11962     (message-remove-header "Mime-Version")
11963     (goto-char (point-max))
11964     (insert "Mime-Version: 1.0\n")
11965     (widen)
11966     (when (search-forward "\n--" nil t)
11967       (let ((separator (buffer-substring (point) (point-at-eol))))
11968         (message-narrow-to-head)
11969         (message-remove-header "Content-Type")
11970         (goto-char (point-max))
11971         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11972                         separator))
11973         (widen))))
11974   (let (gnus-mark-article-hook)
11975     (gnus-summary-select-article t t nil article)))
11976
11977 (defun gnus-summary-toggle-display-buttonized ()
11978   "Toggle the buttonizing of the article buffer."
11979   (interactive)
11980   (require 'gnus-art)
11981   (if (setq gnus-inhibit-mime-unbuttonizing
11982             (not gnus-inhibit-mime-unbuttonizing))
11983       (let ((gnus-unbuttonized-mime-types nil))
11984         (gnus-summary-show-article))
11985     (gnus-summary-show-article)))
11986
11987 ;;;
11988 ;;; Generic summary marking commands
11989 ;;;
11990
11991 (defvar gnus-summary-marking-alist
11992   '((read gnus-del-mark "d")
11993     (unread gnus-unread-mark "u")
11994     (ticked gnus-ticked-mark "!")
11995     (dormant gnus-dormant-mark "?")
11996     (expirable gnus-expirable-mark "e"))
11997   "An alist of names/marks/keystrokes.")
11998
11999 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12000 (defvar gnus-summary-mark-map)
12001
12002 (defun gnus-summary-make-all-marking-commands ()
12003   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12004   (dolist (elem gnus-summary-marking-alist)
12005     (apply 'gnus-summary-make-marking-command elem)))
12006
12007 (defun gnus-summary-make-marking-command (name mark keystroke)
12008   (let ((map (make-sparse-keymap)))
12009     (define-key gnus-summary-generic-mark-map keystroke map)
12010     (dolist (lway `((next "next" next nil "n")
12011                     (next-unread "next unread" next t "N")
12012                     (prev "previous" prev nil "p")
12013                     (prev-unread "previous unread" prev t "P")
12014                     (nomove "" nil nil ,keystroke)))
12015       (let ((func (gnus-summary-make-marking-command-1
12016                    mark (car lway) lway name)))
12017         (setq func (eval func))
12018         (define-key map (nth 4 lway) func)))))
12019
12020 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12021   `(defun ,(intern
12022             (format "gnus-summary-put-mark-as-%s%s"
12023                     name (if (eq way 'nomove)
12024                              ""
12025                            (concat "-" (symbol-name way)))))
12026      (n)
12027      ,(format
12028        "Mark the current article as %s%s.
12029 If N, the prefix, then repeat N times.
12030 If N is negative, move in reverse order.
12031 The difference between N and the actual number of articles marked is
12032 returned."
12033        name (cadr lway))
12034      (interactive "p")
12035      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12036
12037 (defun gnus-summary-generic-mark (n mark move unread)
12038   "Mark N articles with MARK."
12039   (unless (eq major-mode 'gnus-summary-mode)
12040     (error "This command can only be used in the summary buffer"))
12041   (gnus-summary-show-thread)
12042   (let ((nummove
12043          (cond
12044           ((eq move 'next) 1)
12045           ((eq move 'prev) -1)
12046           (t 0))))
12047     (if (zerop nummove)
12048         (setq n 1)
12049       (when (< n 0)
12050         (setq n (abs n)
12051               nummove (* -1 nummove))))
12052     (while (and (> n 0)
12053                 (gnus-summary-mark-article nil mark)
12054                 (zerop (gnus-summary-next-subject nummove unread t)))
12055       (setq n (1- n)))
12056     (when (/= 0 n)
12057       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12058     (gnus-summary-recenter)
12059     (gnus-summary-position-point)
12060     (gnus-set-mode-line 'summary)
12061     n))
12062
12063 (defun gnus-summary-insert-articles (articles)
12064   (when (setq articles
12065               (gnus-sorted-difference articles
12066                                       (mapcar (lambda (h)
12067                                                 (mail-header-number h))
12068                                               gnus-newsgroup-headers)))
12069     (setq gnus-newsgroup-headers
12070           (gnus-merge 'list
12071                       gnus-newsgroup-headers
12072                       (gnus-fetch-headers articles)
12073                       'gnus-article-sort-by-number))
12074     ;; Suppress duplicates?
12075     (when gnus-suppress-duplicates
12076       (gnus-dup-suppress-articles))
12077
12078     ;; We might want to build some more threads first.
12079     (when (and gnus-fetch-old-headers
12080                (eq gnus-headers-retrieved-by 'nov))
12081       (if (eq gnus-fetch-old-headers 'invisible)
12082           (gnus-build-all-threads)
12083         (gnus-build-old-threads)))
12084     ;; Let the Gnus agent mark articles as read.
12085     (when gnus-agent
12086       (gnus-agent-get-undownloaded-list))
12087     ;; Remove list identifiers from subject
12088     (when gnus-list-identifiers
12089       (gnus-summary-remove-list-identifiers))
12090     ;; First and last article in this newsgroup.
12091     (when gnus-newsgroup-headers
12092       (setq gnus-newsgroup-begin
12093             (mail-header-number (car gnus-newsgroup-headers))
12094             gnus-newsgroup-end
12095             (mail-header-number
12096              (gnus-last-element gnus-newsgroup-headers))))
12097     (when gnus-use-scoring
12098       (gnus-possibly-score-headers))))
12099
12100 (defun gnus-summary-insert-old-articles (&optional all)
12101   "Insert all old articles in this group.
12102 If ALL is non-nil, already read articles become readable.
12103 If ALL is a number, fetch this number of articles."
12104   (interactive "P")
12105   (prog1
12106       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12107             older len)
12108         (setq older
12109               ;; Some nntp servers lie about their active range.  When
12110               ;; this happens, the active range can be in the millions.
12111               ;; Use a compressed range to avoid creating a huge list.
12112               (gnus-range-difference (list gnus-newsgroup-active) old))
12113         (setq len (gnus-range-length older))
12114         (cond
12115          ((null older) nil)
12116          ((numberp all)
12117           (if (< all len)
12118               (let ((older-range (nreverse older)))
12119                 (setq older nil)
12120
12121                 (while (> all 0)
12122                   (let* ((r (pop older-range))
12123                          (min (if (numberp r) r (car r)))
12124                          (max (if (numberp r) r (cdr r))))
12125                     (while (and (<= min max)
12126                                 (> all 0))
12127                       (push max older)
12128                       (setq all (1- all)
12129                             max (1- max))))))
12130             (setq older (gnus-uncompress-range older))))
12131          (all
12132           (setq older (gnus-uncompress-range older)))
12133          (t
12134           (when (and (numberp gnus-large-newsgroup)
12135                    (> len gnus-large-newsgroup))
12136               (let* ((cursor-in-echo-area nil)
12137                      (initial (gnus-parameter-large-newsgroup-initial
12138                                gnus-newsgroup-name))
12139                      (input
12140                       (read-string
12141                        (format
12142                         "How many articles from %s (%s %d): "
12143                         (gnus-limit-string
12144                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12145                         (if initial "max" "default")
12146                         len)
12147                        (if initial
12148                            (cons (number-to-string initial)
12149                                  0)))))
12150                 (unless (string-match "^[ \t]*$" input)
12151                   (setq all (string-to-number input))
12152                   (if (< all len)
12153                       (let ((older-range (nreverse older)))
12154                         (setq older nil)
12155
12156                         (while (> all 0)
12157                           (let* ((r (pop older-range))
12158                                  (min (if (numberp r) r (car r)))
12159                                  (max (if (numberp r) r (cdr r))))
12160                             (while (and (<= min max)
12161                                         (> all 0))
12162                               (push max older)
12163                               (setq all (1- all)
12164                                     max (1- max))))))))))
12165           (setq older (gnus-uncompress-range older))))
12166         (if (not older)
12167             (message "No old news.")
12168           (gnus-summary-insert-articles older)
12169           (gnus-summary-limit (gnus-sorted-nunion old older))))
12170     (gnus-summary-position-point)))
12171
12172 (defun gnus-summary-insert-new-articles ()
12173   "Insert all new articles in this group."
12174   (interactive)
12175   (prog1
12176       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12177             (old-active gnus-newsgroup-active)
12178             (nnmail-fetched-sources (list t))
12179             i new)
12180         (setq gnus-newsgroup-active
12181               (gnus-activate-group gnus-newsgroup-name 'scan))
12182         (setq i (cdr gnus-newsgroup-active))
12183         (while (> i (cdr old-active))
12184           (push i new)
12185           (decf i))
12186         (if (not new)
12187             (message "No gnus is bad news")
12188           (gnus-summary-insert-articles new)
12189           (setq gnus-newsgroup-unreads
12190                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12191           (gnus-summary-limit (gnus-sorted-nunion old new))))
12192     (gnus-summary-position-point)))
12193
12194 (gnus-summary-make-all-marking-commands)
12195
12196 (gnus-ems-redefine)
12197
12198 (provide 'gnus-sum)
12199
12200 (run-hooks 'gnus-sum-load-hook)
12201
12202 ;; Local Variables:
12203 ;; coding: iso-8859-1
12204 ;; End:
12205
12206 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12207 ;;; gnus-sum.el ends here