Revision: emacs@sv.gnu.org/gnus--devo--0--patch-74
[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 200
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "*If non-nil, select the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), `unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :version "22.1"
333   :group 'gnus-group-select
334   :type '(choice (const best)
335                  (const unread)
336                  (const first)
337                  (const unseen)
338                  (const unseen-or-unread)))
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols nil (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :version "22.1"
377   :group 'gnus-summary-maneuvering
378   :type '(choice (const :tag "None" nil)
379                  (const :tag "Undownloaded when unplugged" undownloaded)
380                  (const :tag "Undownloaded" always-undownloaded)
381                  (const :tag "Unfetched" unfetched)))
382
383 (defcustom gnus-summary-check-current nil
384   "*If non-nil, consider the current article when moving.
385 The \"unread\" movement commands will stay on the same line if the
386 current article is unread."
387   :group 'gnus-summary-maneuvering
388   :type 'boolean)
389
390 (defcustom gnus-auto-center-summary 2
391   "*If non-nil, always center the current summary buffer.
392 In particular, if `vertical' do only vertical recentering.  If non-nil
393 and non-`vertical', do both horizontal and vertical recentering."
394   :group 'gnus-summary-maneuvering
395   :type '(choice (const :tag "none" nil)
396                  (const vertical)
397                  (integer :tag "height")
398                  (sexp :menu-tag "both" t)))
399
400 (defvar gnus-auto-center-group t
401   "*If non-nil, always center the group buffer.")
402
403 (defcustom gnus-show-all-headers nil
404   "*If non-nil, don't hide any headers."
405   :group 'gnus-article-hiding
406   :group 'gnus-article-headers
407   :type 'boolean)
408
409 (defcustom gnus-summary-ignore-duplicates nil
410   "*If non-nil, ignore articles with identical Message-ID headers."
411   :group 'gnus-summary
412   :type 'boolean)
413
414 (defcustom gnus-single-article-buffer t
415   "*If non-nil, display all articles in the same buffer.
416 If nil, each group will get its own article buffer."
417   :group 'gnus-article-various
418   :type 'boolean)
419
420 (defcustom gnus-break-pages t
421   "*If non-nil, do page breaking on articles.
422 The page delimiter is specified by the `gnus-page-delimiter'
423 variable."
424   :group 'gnus-article-various
425   :type 'boolean)
426
427 (defcustom gnus-move-split-methods nil
428   "*Variable used to suggest where articles are to be moved to.
429 It uses the same syntax as the `gnus-split-methods' variable.
430 However, whereas `gnus-split-methods' specifies file names as targets,
431 this variable specifies group names."
432   :group 'gnus-summary-mail
433   :type '(repeat (choice (list :value (fun) function)
434                          (cons :value ("" "") regexp (repeat string))
435                          (sexp :value nil))))
436
437 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
438   "Function used to compute default prefix for article move/copy/etc prompts.
439 The function should take one argument, a group name, and return a
440 string with the suggested prefix."
441   :group 'gnus-summary-mail
442   :type 'function)
443
444 ;; FIXME: Although the custom type is `character' for the following variables,
445 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
446
447 (defcustom gnus-unread-mark ?           ;Whitespace
448   "*Mark used for unread articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ticked-mark ?!
453   "*Mark used for ticked articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-dormant-mark ??
458   "*Mark used for dormant articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-del-mark ?r
463   "*Mark used for del'd articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-read-mark ?R
468   "*Mark used for read articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-expirable-mark ?E
473   "*Mark used for expirable articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-killed-mark ?K
478   "*Mark used for killed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-spam-mark ?$
483   "*Mark used for spam articles."
484   :version "22.1"
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :version "22.1"
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :version "22.1"
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-no-mark ?               ;Whitespace
542   "*Mark used for articles that have no other secondary mark."
543   :version "22.1"
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-ancient-mark ?O
548   "*Mark used for ancient articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-sparse-mark ?Q
553   "*Mark used for sparsely reffed articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-canceled-mark ?G
558   "*Mark used for canceled articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-duplicate-mark ?M
563   "*Mark used for duplicate articles."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-undownloaded-mark ?-
568   "*Mark used for articles that weren't downloaded."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloaded-mark ?+
574   "*Mark used for articles that were downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-downloadable-mark ?%
579   "*Mark used for articles that are to be downloaded."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unsendable-mark ?=
584   "*Mark used for articles that won't be sent."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-over-mark ?+
589   "*Score mark used for articles with high scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-score-below-mark ?-
594   "*Score mark used for articles with low scores."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-empty-thread-mark ?     ;Whitespace
599   "*There is no thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-not-empty-thread-mark ?=
604   "*There is a thread under the article."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-view-pseudo-asynchronously nil
609   "*If non-nil, Gnus will view pseudo-articles asynchronously."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-auto-expirable-marks
614   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
615         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
616         gnus-souped-mark gnus-duplicate-mark)
617   "*The list of marks converted into expiration if a group is auto-expirable."
618   :version "21.1"
619   :group 'gnus-summary
620   :type '(repeat character))
621
622 (defcustom gnus-inhibit-user-auto-expire t
623   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
624   :version "21.1"
625   :group 'gnus-summary
626   :type 'boolean)
627
628 (defcustom gnus-view-pseudos nil
629   "*If `automatic', pseudo-articles will be viewed automatically.
630 If `not-confirm', pseudos will be viewed automatically, and the user
631 will not be asked to confirm the command."
632   :group 'gnus-extract-view
633   :type '(choice (const :tag "off" nil)
634                  (const automatic)
635                  (const not-confirm)))
636
637 (defcustom gnus-view-pseudos-separately t
638   "*If non-nil, one pseudo-article will be created for each file to be viewed.
639 If nil, all files that use the same viewing command will be given as a
640 list of parameters to that command."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-insert-pseudo-articles t
645   "*If non-nil, insert pseudo-articles when decoding articles."
646   :group 'gnus-extract-view
647   :type 'boolean)
648
649 (defcustom gnus-summary-dummy-line-format
650   "   %(:                             :%) %S\n"
651   "*The format specification for the dummy roots in the summary buffer.
652 It works along the same lines as a normal formatting string,
653 with some simple extensions.
654
655 %S  The subject
656
657 General format specifiers can also be used.
658 See `(gnus)Formatting Variables'."
659   :link '(custom-manual "(gnus)Formatting Variables")
660   :group 'gnus-threading
661   :type 'string)
662
663 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
664   "*The format specification for the summary mode line.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions:
667
668 %G  Group name
669 %p  Unprefixed group name
670 %A  Current article number
671 %z  Current article score
672 %V  Gnus version
673 %U  Number of unread articles in the group
674 %e  Number of unselected articles in the group
675 %Z  A string with unread/unselected article counts
676 %g  Shortish group name
677 %S  Subject of the current article
678 %u  User-defined spec
679 %s  Current score file name
680 %d  Number of dormant articles
681 %r  Number of articles that have been marked as read in this session
682 %E  Number of articles expunged by the score files"
683   :group 'gnus-summary-format
684   :type 'string)
685
686 (defcustom gnus-list-identifiers nil
687   "Regexp that matches list identifiers to be removed from subject.
688 This can also be a list of regexps."
689   :version "21.1"
690   :group 'gnus-summary-format
691   :group 'gnus-article-hiding
692   :type '(choice (const :tag "none" nil)
693                  (regexp :value ".*")
694                  (repeat :value (".*") regexp)))
695
696 (defcustom gnus-summary-mark-below 0
697   "*Mark all articles with a score below this variable as read.
698 This variable is local to each summary buffer and usually set by the
699 score file."
700   :group 'gnus-score-default
701   :type 'integer)
702
703 (defun gnus-widget-reversible-match (widget value)
704   "Ignoring WIDGET, convert VALUE to internal form.
705 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
706   ;; (debug value)
707   (or (symbolp value)
708       (and (listp value)
709            (eq (length value) 2)
710            (eq (nth 0 value) 'not)
711            (symbolp (nth 1 value)))))
712
713 (defun gnus-widget-reversible-to-internal (widget value)
714   "Ignoring WIDGET, convert VALUE to internal form.
715 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
716 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
717   ;; (debug value)
718   (if (atom value)
719       (list value nil)
720     (list (nth 1 value) t)))
721
722 (defun gnus-widget-reversible-to-external (widget value)
723   "Ignoring WIDGET, convert VALUE to external form.
724 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
725 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
726   ;; (debug value)
727   (if (nth 1 value)
728       (list 'not (nth 0 value))
729     (nth 0 value)))
730
731 (define-widget 'gnus-widget-reversible 'group
732   "A `group' that convert values."
733   :match 'gnus-widget-reversible-match
734   :value-to-internal 'gnus-widget-reversible-to-internal
735   :value-to-external 'gnus-widget-reversible-to-external)
736
737 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
738   "*List of functions used for sorting articles in the summary buffer.
739
740 Each function takes two articles and returns non-nil if the first
741 article should be sorted before the other.  If you use more than one
742 function, the primary sort function should be the last.  You should
743 probably always include `gnus-article-sort-by-number' in the list of
744 sorting functions -- preferably first.  Also note that sorting by date
745 is often much slower than sorting by number, and the sorting order is
746 very similar.  (Sorting by date means sorting by the time the message
747 was sent, sorting by number means sorting by arrival time.)
748
749 Each item can also be a list `(not F)' where F is a function;
750 this reverses the sort order.
751
752 Ready-made functions include `gnus-article-sort-by-number',
753 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
754 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
755 and `gnus-article-sort-by-score'.
756
757 When threading is turned on, the variable `gnus-thread-sort-functions'
758 controls how articles are sorted."
759   :group 'gnus-summary-sort
760   :type '(repeat (gnus-widget-reversible
761                   (choice (function-item gnus-article-sort-by-number)
762                           (function-item gnus-article-sort-by-author)
763                           (function-item gnus-article-sort-by-subject)
764                           (function-item gnus-article-sort-by-date)
765                           (function-item gnus-article-sort-by-score)
766                           (function-item gnus-article-sort-by-random)
767                           (function :tag "other"))
768                   (boolean :tag "Reverse order"))))
769
770
771 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
772   "*List of functions used for sorting threads in the summary buffer.
773 By default, threads are sorted by article number.
774
775 Each function takes two threads and returns non-nil if the first
776 thread should be sorted before the other.  If you use more than one
777 function, the primary sort function should be the last.  You should
778 probably always include `gnus-thread-sort-by-number' in the list of
779 sorting functions -- preferably first.  Also note that sorting by date
780 is often much slower than sorting by number, and the sorting order is
781 very similar.  (Sorting by date means sorting by the time the message
782 was sent, sorting by number means sorting by arrival time.)
783
784 Each list item can also be a list `(not F)' where F is a
785 function; this specifies reversed sort order.
786
787 Ready-made functions include `gnus-thread-sort-by-number',
788 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
789 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
790 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
791 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
792 and `gnus-thread-sort-by-total-score' (see
793 `gnus-thread-score-function').
794
795 When threading is turned off, the variable
796 `gnus-article-sort-functions' controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat
799           (gnus-widget-reversible
800            (choice (function-item gnus-thread-sort-by-number)
801                    (function-item gnus-thread-sort-by-author)
802                    (function-item gnus-thread-sort-by-recipient)
803                    (function-item gnus-thread-sort-by-subject)
804                    (function-item gnus-thread-sort-by-date)
805                    (function-item gnus-thread-sort-by-score)
806                    (function-item gnus-thread-sort-by-most-recent-number)
807                    (function-item gnus-thread-sort-by-most-recent-date)
808                    (function-item gnus-thread-sort-by-random)
809                    (function-item gnus-thread-sort-by-total-score)
810                    (function :tag "other"))
811            (boolean :tag "Reverse order"))))
812
813 (defcustom gnus-thread-score-function '+
814   "*Function used for calculating the total score of a thread.
815
816 The function is called with the scores of the article and each
817 subthread and should then return the score of the thread.
818
819 Some functions you can use are `+', `max', or `min'."
820   :group 'gnus-summary-sort
821   :type 'function)
822
823 (defcustom gnus-summary-expunge-below nil
824   "All articles that have a score less than this variable will be expunged.
825 This variable is local to the summary buffers."
826   :group 'gnus-score-default
827   :type '(choice (const :tag "off" nil)
828                  integer))
829
830 (defcustom gnus-thread-expunge-below nil
831   "All threads that have a total score less than this variable will be expunged.
832 See `gnus-thread-score-function' for en explanation of what a
833 \"thread score\" is.
834
835 This variable is local to the summary buffers."
836   :group 'gnus-threading
837   :group 'gnus-score-default
838   :type '(choice (const :tag "off" nil)
839                  integer))
840
841 (defcustom gnus-summary-mode-hook nil
842   "*A hook for Gnus summary mode.
843 This hook is run before any variables are set in the summary buffer."
844   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
845   :group 'gnus-summary-various
846   :type 'hook)
847
848 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
849 (when (featurep 'xemacs)
850   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
851   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
852   (add-hook 'gnus-summary-mode-hook
853             'gnus-xmas-switch-horizontal-scrollbar-off))
854
855 (defcustom gnus-summary-menu-hook nil
856   "*Hook run after the creation of the summary mode menu."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-summary-exit-hook nil
861   "*A hook called on exit from the summary buffer.
862 It will be called with point in the group buffer."
863   :group 'gnus-summary-exit
864   :type 'hook)
865
866 (defcustom gnus-summary-prepare-hook nil
867   "*A hook called after the summary buffer has been generated.
868 If you want to modify the summary buffer, you can use this hook."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-prepared-hook nil
873   "*A hook called as the last thing after the summary buffer has been generated."
874   :group 'gnus-summary-various
875   :type 'hook)
876
877 (defcustom gnus-summary-generate-hook nil
878   "*A hook run just before generating the summary buffer.
879 This hook is commonly used to customize threading variables and the
880 like."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-select-group-hook nil
885   "*A hook called when a newsgroup is selected.
886
887 If you'd like to simplify subjects like the
888 `gnus-summary-next-same-subject' command does, you can use the
889 following hook:
890
891  (add-hook gnus-select-group-hook
892            (lambda ()
893              (mapcar (lambda (header)
894                        (mail-header-set-subject
895                         header
896                         (gnus-simplify-subject
897                          (mail-header-subject header) 're-only)))
898                      gnus-newsgroup-headers)))"
899   :group 'gnus-group-select
900   :type 'hook)
901
902 (defcustom gnus-select-article-hook nil
903   "*A hook called when an article is selected."
904   :group 'gnus-summary-choose
905   :options '(gnus-agent-fetch-selected-article)
906   :type 'hook)
907
908 (defcustom gnus-visual-mark-article-hook
909   (list 'gnus-highlight-selected-summary)
910   "*Hook run after selecting an article in the summary buffer.
911 It is meant to be used for highlighting the article in some way.  It
912 is not run if `gnus-visual' is nil."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-parse-headers-hook nil
917   "*A hook called before parsing the headers."
918   :group 'gnus-various
919   :type 'hook)
920
921 (defcustom gnus-exit-group-hook nil
922   "*A hook called when exiting summary mode.
923 This hook is not called from the non-updating exit commands like `Q'."
924   :group 'gnus-various
925   :type 'hook)
926
927 (defcustom gnus-summary-update-hook
928   (list 'gnus-summary-highlight-line)
929   "*A hook called when a summary line is changed.
930 The hook will not be called if `gnus-visual' is nil.
931
932 The default function `gnus-summary-highlight-line' will
933 highlight the line according to the `gnus-summary-highlight'
934 variable."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
939   "*A hook called when an article is selected for the first time.
940 The hook is intended to mark an article as read (or unread)
941 automatically when it is selected."
942   :group 'gnus-summary-choose
943   :type 'hook)
944
945 (defcustom gnus-group-no-more-groups-hook nil
946   "*A hook run when returning to group mode having no more (unread) groups."
947   :group 'gnus-group-select
948   :type 'hook)
949
950 (defcustom gnus-ps-print-hook nil
951   "*A hook run before ps-printing something from Gnus."
952   :group 'gnus-summary
953   :type 'hook)
954
955 (defcustom gnus-summary-article-move-hook nil
956   "*A hook called after an article is moved, copied, respooled, or crossposted."
957   :version "22.1"
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-article-delete-hook nil
962   "*A hook called after an article is deleted."
963   :version "22.1"
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :version "22.1"
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded)
995     (uncached
996      . gnus-summary-normal-undownloaded)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread)
1020     ((> score default-high)
1021      . gnus-summary-high-read)
1022     ((< score default-low)
1023      . gnus-summary-low-read)
1024     (t
1025      . gnus-summary-normal-read))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1055   "Variable that says which function should be used to decode a string with encoded words.")
1056
1057 (defcustom gnus-extra-headers '(To Newsgroups)
1058   "*Extra headers to parse."
1059   :version "21.1"
1060   :group 'gnus-summary
1061   :type '(repeat symbol))
1062
1063 (defcustom gnus-ignored-from-addresses
1064   (and user-mail-address  
1065        (not (string= user-mail-address ""))
1066        (regexp-quote user-mail-address))
1067   "*Regexp of From headers that may be suppressed in favor of To headers."
1068   :version "21.1"
1069   :group 'gnus-summary
1070   :type 'regexp)
1071
1072 (defcustom gnus-summary-to-prefix "-> "
1073   "*String prefixed to the To field in the summary line when
1074 using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-summary-newsgroup-prefix "=> "
1080   "*String prefixed to the Newsgroup field in the summary
1081 line when using `gnus-ignored-from-addresses'."
1082   :version "22.1"
1083   :group 'gnus-summary
1084   :type 'string)
1085
1086 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1087   "List of charsets that should be ignored.
1088 When these charsets are used in the \"charset\" parameter, the
1089 default charset will be used instead."
1090   :version "21.1"
1091   :type '(repeat symbol)
1092   :group 'gnus-charset)
1093
1094 (gnus-define-group-parameter
1095  ignored-charsets
1096  :type list
1097  :function-document
1098  "Return the ignored charsets of GROUP."
1099  :variable gnus-group-ignored-charsets-alist
1100  :variable-default
1101  '(("alt\\.chinese\\.text" iso-8859-1))
1102  :variable-document
1103  "Alist of regexps (to match group names) and charsets that should be ignored.
1104 When these charsets are used in the \"charset\" parameter, the
1105 default charset will be used instead."
1106  :variable-group gnus-charset
1107  :variable-type '(repeat (cons (regexp :tag "Group")
1108                                (repeat symbol)))
1109  :parameter-type '(choice :tag "Ignored charsets"
1110                           :value nil
1111                           (repeat (symbol)))
1112  :parameter-document       "\
1113 List of charsets that should be ignored.
1114
1115 When these charsets are used in the \"charset\" parameter, the
1116 default charset will be used instead.")
1117
1118 (defcustom gnus-group-highlight-words-alist nil
1119   "Alist of group regexps and highlight regexps.
1120 This variable uses the same syntax as `gnus-emphasis-alist'."
1121   :version "21.1"
1122   :type '(repeat (cons (regexp :tag "Group")
1123                        (repeat (list (regexp :tag "Highlight regexp")
1124                                      (number :tag "Group for entire word" 0)
1125                                      (number :tag "Group for displayed part" 0)
1126                                      (symbol :tag "Face"
1127                                              gnus-emphasis-highlight-words)))))
1128   :group 'gnus-summary-visual)
1129
1130 (defcustom gnus-summary-show-article-charset-alist
1131   nil
1132   "Alist of number and charset.
1133 The article will be shown with the charset corresponding to the
1134 numbered argument.
1135 For example: ((1 . cn-gb-2312) (2 . big5))."
1136   :version "21.1"
1137   :type '(repeat (cons (number :tag "Argument" 1)
1138                        (symbol :tag "Charset")))
1139   :group 'gnus-charset)
1140
1141 (defcustom gnus-preserve-marks t
1142   "Whether marks are preserved when moving, copying and respooling messages."
1143   :version "21.1"
1144   :type 'boolean
1145   :group 'gnus-summary-marks)
1146
1147 (defcustom gnus-alter-articles-to-read-function nil
1148   "Function to be called to alter the list of articles to be selected."
1149   :type '(choice (const nil) function)
1150   :group 'gnus-summary)
1151
1152 (defcustom gnus-orphan-score nil
1153   "*All orphans get this score added.  Set in the score file."
1154   :group 'gnus-score-default
1155   :type '(choice (const nil)
1156                  integer))
1157
1158 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1159   "*A regexp to match MIME parts when saving multiple parts of a
1160 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1161 This regexp will be used by default when prompting the user for which
1162 type of files to save."
1163   :group 'gnus-summary
1164   :type 'regexp)
1165
1166 (defcustom gnus-read-all-available-headers nil
1167   "Whether Gnus should parse all headers made available to it.
1168 This is mostly relevant for slow back ends where the user may
1169 wish to widen the summary buffer to include all headers
1170 that were fetched.  Say, for nnultimate groups."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type '(choice boolean regexp))
1174
1175 (defcustom gnus-summary-muttprint-program "muttprint"
1176   "Command (and optional arguments) used to run Muttprint."
1177   :version "22.1"
1178   :group 'gnus-summary
1179   :type 'string)
1180
1181 (defcustom gnus-article-loose-mime t
1182   "If non-nil, don't require MIME-Version header.
1183 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1184 supply the MIME-Version header or deliberately strip it from the mail.
1185 If non-nil (the default), Gnus will treat some articles as MIME
1186 even if the MIME-Version header is missing."
1187   :version "22.1"
1188   :type 'boolean
1189   :group 'gnus-article-mime)
1190
1191 (defcustom gnus-article-emulate-mime t
1192   "If non-nil, use MIME emulation for uuencode and the like.
1193 This means that Gnus will search message bodies for text that look
1194 like uuencoded bits, yEncoded bits, and so on, and present that using
1195 the normal Gnus MIME machinery."
1196   :version "22.1"
1197   :type 'boolean
1198   :group 'gnus-article-mime)
1199
1200 ;;; Internal variables
1201
1202 (defvar gnus-summary-display-cache nil)
1203 (defvar gnus-article-mime-handles nil)
1204 (defvar gnus-article-decoded-p nil)
1205 (defvar gnus-article-charset nil)
1206 (defvar gnus-article-ignored-charsets nil)
1207 (defvar gnus-scores-exclude-files nil)
1208 (defvar gnus-page-broken nil)
1209
1210 (defvar gnus-original-article nil)
1211 (defvar gnus-article-internal-prepare-hook nil)
1212 (defvar gnus-newsgroup-process-stack nil)
1213
1214 (defvar gnus-thread-indent-array nil)
1215 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1216 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1217   "Function called to sort the articles within a thread after it has been gathered together.")
1218
1219 (defvar gnus-summary-save-parts-type-history nil)
1220 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1221
1222 ;; Avoid highlighting in kill files.
1223 (defvar gnus-summary-inhibit-highlight nil)
1224 (defvar gnus-newsgroup-selected-overlay nil)
1225 (defvar gnus-inhibit-limiting nil)
1226 (defvar gnus-newsgroup-adaptive-score-file nil)
1227 (defvar gnus-current-score-file nil)
1228 (defvar gnus-current-move-group nil)
1229 (defvar gnus-current-copy-group nil)
1230 (defvar gnus-current-crosspost-group nil)
1231 (defvar gnus-newsgroup-display nil)
1232
1233 (defvar gnus-newsgroup-dependencies nil)
1234 (defvar gnus-newsgroup-adaptive nil)
1235 (defvar gnus-summary-display-article-function nil)
1236 (defvar gnus-summary-highlight-line-function nil
1237   "Function called after highlighting a summary line.")
1238
1239 (defvar gnus-summary-line-format-alist
1240   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1241     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1242     (?s gnus-tmp-subject-or-nil ?s)
1243     (?n gnus-tmp-name ?s)
1244     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1245         ?s)
1246     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1247             gnus-tmp-from) ?s)
1248     (?F gnus-tmp-from ?s)
1249     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1250     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1251     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1252     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1253     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1254     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1255     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1256     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1257     (?L gnus-tmp-lines ?s)
1258     (?O gnus-tmp-downloaded ?c)
1259     (?I gnus-tmp-indentation ?s)
1260     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1261     (?R gnus-tmp-replied ?c)
1262     (?\[ gnus-tmp-opening-bracket ?c)
1263     (?\] gnus-tmp-closing-bracket ?c)
1264     (?\> (make-string gnus-tmp-level ? ) ?s)
1265     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1266     (?i gnus-tmp-score ?d)
1267     (?z gnus-tmp-score-char ?c)
1268     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1269     (?U gnus-tmp-unread ?c)
1270     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1271         ?s)
1272     (?t (gnus-summary-number-of-articles-in-thread
1273          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1274         ?d)
1275     (?e (gnus-summary-number-of-articles-in-thread
1276          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1277         ?c)
1278     (?u gnus-tmp-user-defined ?s)
1279     (?P (gnus-pick-line-number) ?d)
1280     (?B gnus-tmp-thread-tree-header-string ?s)
1281     (user-date (gnus-user-date
1282                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1283   "An alist of format specifications that can appear in summary lines.
1284 These are paired with what variables they correspond with, along with
1285 the type of the variable (string, integer, character, etc).")
1286
1287 (defvar gnus-summary-dummy-line-format-alist
1288   `((?S gnus-tmp-subject ?s)
1289     (?N gnus-tmp-number ?d)
1290     (?u gnus-tmp-user-defined ?s)))
1291
1292 (defvar gnus-summary-mode-line-format-alist
1293   `((?G gnus-tmp-group-name ?s)
1294     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1295     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1296     (?A gnus-tmp-article-number ?d)
1297     (?Z gnus-tmp-unread-and-unselected ?s)
1298     (?V gnus-version ?s)
1299     (?U gnus-tmp-unread-and-unticked ?d)
1300     (?S gnus-tmp-subject ?s)
1301     (?e gnus-tmp-unselected ?d)
1302     (?u gnus-tmp-user-defined ?s)
1303     (?d (length gnus-newsgroup-dormant) ?d)
1304     (?t (length gnus-newsgroup-marked) ?d)
1305     (?h (length gnus-newsgroup-spam-marked) ?d)
1306     (?r (length gnus-newsgroup-reads) ?d)
1307     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1308     (?E gnus-newsgroup-expunged-tally ?d)
1309     (?s (gnus-current-score-file-nondirectory) ?s)))
1310
1311 (defvar gnus-last-search-regexp nil
1312   "Default regexp for article search command.")
1313
1314 (defvar gnus-last-shell-command nil
1315   "Default shell command on article.")
1316
1317 (defvar gnus-newsgroup-agentized nil
1318   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1319 (defvar gnus-newsgroup-begin nil)
1320 (defvar gnus-newsgroup-end nil)
1321 (defvar gnus-newsgroup-last-rmail nil)
1322 (defvar gnus-newsgroup-last-mail nil)
1323 (defvar gnus-newsgroup-last-folder nil)
1324 (defvar gnus-newsgroup-last-file nil)
1325 (defvar gnus-newsgroup-auto-expire nil)
1326 (defvar gnus-newsgroup-active nil)
1327
1328 (defvar gnus-newsgroup-data nil)
1329 (defvar gnus-newsgroup-data-reverse nil)
1330 (defvar gnus-newsgroup-limit nil)
1331 (defvar gnus-newsgroup-limits nil)
1332 (defvar gnus-summary-use-undownloaded-faces nil)
1333
1334 (defvar gnus-newsgroup-unreads nil
1335   "Sorted list of unread articles in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-unselected nil
1338   "Sorted list of unselected unread articles in the current newsgroup.")
1339
1340 (defvar gnus-newsgroup-reads nil
1341   "Alist of read articles and article marks in the current newsgroup.")
1342
1343 (defvar gnus-newsgroup-expunged-tally nil)
1344
1345 (defvar gnus-newsgroup-marked nil
1346   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1347
1348 (defvar gnus-newsgroup-spam-marked nil
1349   "List of ranges of articles that have been marked as spam.")
1350
1351 (defvar gnus-newsgroup-killed nil
1352   "List of ranges of articles that have been through the scoring process.")
1353
1354 (defvar gnus-newsgroup-cached nil
1355   "Sorted list of articles that come from the article cache.")
1356
1357 (defvar gnus-newsgroup-saved nil
1358   "List of articles that have been saved.")
1359
1360 (defvar gnus-newsgroup-kill-headers nil)
1361
1362 (defvar gnus-newsgroup-replied nil
1363   "List of articles that have been replied to in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-forwarded nil
1366   "List of articles that have been forwarded in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-recent nil
1369   "List of articles that have are recent in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-expirable nil
1372   "Sorted list of articles in the current newsgroup that can be expired.")
1373
1374 (defvar gnus-newsgroup-processable nil
1375   "List of articles in the current newsgroup that can be processed.")
1376
1377 (defvar gnus-newsgroup-downloadable nil
1378   "Sorted list of articles in the current newsgroup that can be processed.")
1379
1380 (defvar gnus-newsgroup-unfetched nil
1381   "Sorted list of articles in the current newsgroup whose headers have
1382 not been fetched into the agent.
1383
1384 This list will always be a subset of gnus-newsgroup-undownloaded.")
1385
1386 (defvar gnus-newsgroup-undownloaded nil
1387   "List of articles in the current newsgroup that haven't been downloaded.")
1388
1389 (defvar gnus-newsgroup-unsendable nil
1390   "List of articles in the current newsgroup that won't be sent.")
1391
1392 (defvar gnus-newsgroup-bookmarks nil
1393   "List of articles in the current newsgroup that have bookmarks.")
1394
1395 (defvar gnus-newsgroup-dormant nil
1396   "Sorted list of dormant articles in the current newsgroup.")
1397
1398 (defvar gnus-newsgroup-unseen nil
1399   "List of unseen articles in the current newsgroup.")
1400
1401 (defvar gnus-newsgroup-seen nil
1402   "Range of seen articles in the current newsgroup.")
1403
1404 (defvar gnus-newsgroup-articles nil
1405   "List of articles in the current newsgroup.")
1406
1407 (defvar gnus-newsgroup-scored nil
1408   "List of scored articles in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-headers nil
1411   "List of article headers in the current newsgroup.")
1412
1413 (defvar gnus-newsgroup-threads nil)
1414
1415 (defvar gnus-newsgroup-prepared nil
1416   "Whether the current group has been prepared properly.")
1417
1418 (defvar gnus-newsgroup-ancient nil
1419   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-sparse nil)
1422
1423 (defvar gnus-current-article nil)
1424 (defvar gnus-article-current nil)
1425 (defvar gnus-current-headers nil)
1426 (defvar gnus-have-all-headers nil)
1427 (defvar gnus-last-article nil)
1428 (defvar gnus-newsgroup-history nil)
1429 (defvar gnus-newsgroup-charset nil)
1430 (defvar gnus-newsgroup-ephemeral-charset nil)
1431 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1432
1433 (defvar gnus-article-before-search nil)
1434
1435 (defvar gnus-summary-local-variables
1436   '(gnus-newsgroup-name
1437     gnus-newsgroup-begin gnus-newsgroup-end
1438     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1439     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1440     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1441     gnus-newsgroup-unselected gnus-newsgroup-marked
1442     gnus-newsgroup-spam-marked
1443     gnus-newsgroup-reads gnus-newsgroup-saved
1444     gnus-newsgroup-replied gnus-newsgroup-forwarded
1445     gnus-newsgroup-recent
1446     gnus-newsgroup-expirable
1447     gnus-newsgroup-processable gnus-newsgroup-killed
1448     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1449     gnus-newsgroup-unfetched
1450     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1451     gnus-newsgroup-seen gnus-newsgroup-articles
1452     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1453     gnus-newsgroup-headers gnus-newsgroup-threads
1454     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1455     gnus-current-article gnus-current-headers gnus-have-all-headers
1456     gnus-last-article gnus-article-internal-prepare-hook
1457     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1458     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1459     gnus-thread-expunge-below
1460     gnus-score-alist gnus-current-score-file
1461     (gnus-summary-expunge-below . global)
1462     (gnus-summary-mark-below . global)
1463     (gnus-orphan-score . global)
1464     gnus-newsgroup-active gnus-scores-exclude-files
1465     gnus-newsgroup-history gnus-newsgroup-ancient
1466     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1467     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1468     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1469     (gnus-newsgroup-expunged-tally . 0)
1470     gnus-cache-removable-articles gnus-newsgroup-cached
1471     gnus-newsgroup-data gnus-newsgroup-data-reverse
1472     gnus-newsgroup-limit gnus-newsgroup-limits
1473     gnus-newsgroup-charset gnus-newsgroup-display
1474     gnus-summary-use-undownloaded-faces)
1475   "Variables that are buffer-local to the summary buffers.")
1476
1477 (defvar gnus-newsgroup-variables nil
1478   "A list of variables that have separate values in different newsgroups.
1479 A list of newsgroup (summary buffer) local variables, or cons of
1480 variables and their default expressions to be evalled (when the default
1481 values are not nil), that should be made global while the summary buffer
1482 is active.
1483
1484 Note: The default expressions will be evaluated (using function `eval')
1485 before assignment to the local variable rather than just assigned to it.
1486 If the default expression is the symbol `global', that symbol will not
1487 be evaluated but the global value of the local variable will be used
1488 instead.
1489
1490 These variables can be used to set variables in the group parameters
1491 while still allowing them to affect operations done in other buffers.
1492 For example:
1493
1494 \(setq gnus-newsgroup-variables
1495      '(message-use-followup-to
1496        (gnus-visible-headers .
1497          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1498 ")
1499
1500 ;; Byte-compiler warning.
1501 (eval-when-compile
1502   ;; Bind features so that require will believe that gnus-sum has
1503   ;; already been loaded (avoids infinite recursion)
1504   (let ((features (cons 'gnus-sum features)))
1505     ;; Several of the declarations in gnus-sum are needed to load the
1506     ;; following files. Right now, these definitions have been
1507     ;; compiled but not defined (evaluated).  We could either do a
1508     ;; eval-and-compile about all of the declarations or evaluate the
1509     ;; source file.
1510     (if (boundp 'gnus-newsgroup-variables)
1511         nil
1512       (load "gnus-sum.el" t t t))
1513     (require 'gnus)
1514     (require 'gnus-art)))
1515
1516 ;; MIME stuff.
1517
1518 (defvar gnus-decode-encoded-word-methods
1519   '(mail-decode-encoded-word-string)
1520   "List of methods used to decode encoded words.
1521
1522 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1523 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1524 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1525 whose names match REGEXP.
1526
1527 For example:
1528 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1529  mail-decode-encoded-word-string
1530  (\"chinese\" . rfc1843-decode-string))")
1531
1532 (defvar gnus-decode-encoded-word-methods-cache nil)
1533
1534 (defun gnus-multi-decode-encoded-word-string (string)
1535   "Apply the functions from `gnus-encoded-word-methods' that match."
1536   (unless (and gnus-decode-encoded-word-methods-cache
1537                (eq gnus-newsgroup-name
1538                    (car gnus-decode-encoded-word-methods-cache)))
1539     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1540     (mapcar (lambda (x)
1541               (if (symbolp x)
1542                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1543                 (if (and gnus-newsgroup-name
1544                          (string-match (car x) gnus-newsgroup-name))
1545                     (nconc gnus-decode-encoded-word-methods-cache
1546                            (list (cdr x))))))
1547             gnus-decode-encoded-word-methods))
1548   (let ((xlist gnus-decode-encoded-word-methods-cache))
1549     (pop xlist)
1550     (while xlist
1551       (setq string (funcall (pop xlist) string))))
1552   string)
1553
1554 ;; Subject simplification.
1555
1556 (defun gnus-simplify-whitespace (str)
1557   "Remove excessive whitespace from STR."
1558   ;; Multiple spaces.
1559   (while (string-match "[ \t][ \t]+" str)
1560     (setq str (concat (substring str 0 (match-beginning 0))
1561                         " "
1562                         (substring str (match-end 0)))))
1563   ;; Leading spaces.
1564   (when (string-match "^[ \t]+" str)
1565     (setq str (substring str (match-end 0))))
1566   ;; Trailing spaces.
1567   (when (string-match "[ \t]+$" str)
1568     (setq str (substring str 0 (match-beginning 0))))
1569   str)
1570
1571 (defun gnus-simplify-all-whitespace (str)
1572   "Remove all whitespace from STR."
1573   (while (string-match "[ \t\n]+" str)
1574     (setq str (replace-match "" nil nil str)))
1575   str)
1576
1577 (defsubst gnus-simplify-subject-re (subject)
1578   "Remove \"Re:\" from subject lines."
1579   (if (string-match message-subject-re-regexp subject)
1580       (substring subject (match-end 0))
1581     subject))
1582
1583 (defun gnus-simplify-subject (subject &optional re-only)
1584   "Remove `Re:' and words in parentheses.
1585 If RE-ONLY is non-nil, strip leading `Re:'s only."
1586   (let ((case-fold-search t))           ;Ignore case.
1587     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1588     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1589       (setq subject (substring subject (match-end 0))))
1590     ;; Remove uninteresting prefixes.
1591     (when (and (not re-only)
1592                gnus-simplify-ignored-prefixes
1593                (string-match gnus-simplify-ignored-prefixes subject))
1594       (setq subject (substring subject (match-end 0))))
1595     ;; Remove words in parentheses from end.
1596     (unless re-only
1597       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1598         (setq subject (substring subject 0 (match-beginning 0)))))
1599     ;; Return subject string.
1600     subject))
1601
1602 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1603 ;; all whitespace.
1604 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1605   (goto-char (point-min))
1606   (while (re-search-forward regexp nil t)
1607     (replace-match (or newtext ""))))
1608
1609 (defun gnus-simplify-buffer-fuzzy ()
1610   "Simplify string in the buffer fuzzily.
1611 The string in the accessible portion of the current buffer is simplified.
1612 It is assumed to be a single-line subject.
1613 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1614 matter is removed.  Additional things can be deleted by setting
1615 `gnus-simplify-subject-fuzzy-regexp'."
1616   (let ((case-fold-search t)
1617         (modified-tick))
1618     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1619
1620     (while (not (eq modified-tick (buffer-modified-tick)))
1621       (setq modified-tick (buffer-modified-tick))
1622       (cond
1623        ((listp gnus-simplify-subject-fuzzy-regexp)
1624         (mapcar 'gnus-simplify-buffer-fuzzy-step
1625                 gnus-simplify-subject-fuzzy-regexp))
1626        (gnus-simplify-subject-fuzzy-regexp
1627         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1628       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1629       (gnus-simplify-buffer-fuzzy-step
1630        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1631       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1632
1633     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1634     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1635     (gnus-simplify-buffer-fuzzy-step " $")
1636     (gnus-simplify-buffer-fuzzy-step "^ +")))
1637
1638 (defun gnus-simplify-subject-fuzzy (subject)
1639   "Simplify a subject string fuzzily.
1640 See `gnus-simplify-buffer-fuzzy' for details."
1641   (save-excursion
1642     (gnus-set-work-buffer)
1643     (let ((case-fold-search t))
1644       ;; Remove uninteresting prefixes.
1645       (when (and gnus-simplify-ignored-prefixes
1646                  (string-match gnus-simplify-ignored-prefixes subject))
1647         (setq subject (substring subject (match-end 0))))
1648       (insert subject)
1649       (inline (gnus-simplify-buffer-fuzzy))
1650       (buffer-string))))
1651
1652 (defsubst gnus-simplify-subject-fully (subject)
1653   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1654   (cond
1655    (gnus-simplify-subject-functions
1656     (gnus-map-function gnus-simplify-subject-functions subject))
1657    ((null gnus-summary-gather-subject-limit)
1658     (gnus-simplify-subject-re subject))
1659    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1660     (gnus-simplify-subject-fuzzy subject))
1661    ((numberp gnus-summary-gather-subject-limit)
1662     (gnus-limit-string (gnus-simplify-subject-re subject)
1663                        gnus-summary-gather-subject-limit))
1664    (t
1665     subject)))
1666
1667 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1668   "Check whether two subjects are equal.
1669 If optional argument SIMPLE-FIRST is t, first argument is already
1670 simplified."
1671   (cond
1672    ((null simple-first)
1673     (equal (gnus-simplify-subject-fully s1)
1674            (gnus-simplify-subject-fully s2)))
1675    (t
1676     (equal s1
1677            (gnus-simplify-subject-fully s2)))))
1678
1679 (defun gnus-summary-bubble-group ()
1680   "Increase the score of the current group.
1681 This is a handy function to add to `gnus-summary-exit-hook' to
1682 increase the score of each group you read."
1683   (gnus-group-add-score gnus-newsgroup-name))
1684
1685 \f
1686 ;;;
1687 ;;; Gnus summary mode
1688 ;;;
1689
1690 (put 'gnus-summary-mode 'mode-class 'special)
1691
1692 (defvar gnus-article-commands-menu)
1693
1694 ;; Non-orthogonal keys
1695
1696 (gnus-define-keys gnus-summary-mode-map
1697   " " gnus-summary-next-page
1698   "\177" gnus-summary-prev-page
1699   [delete] gnus-summary-prev-page
1700   [backspace] gnus-summary-prev-page
1701   "\r" gnus-summary-scroll-up
1702   "\M-\r" gnus-summary-scroll-down
1703   "n" gnus-summary-next-unread-article
1704   "p" gnus-summary-prev-unread-article
1705   "N" gnus-summary-next-article
1706   "P" gnus-summary-prev-article
1707   "\M-\C-n" gnus-summary-next-same-subject
1708   "\M-\C-p" gnus-summary-prev-same-subject
1709   "\M-n" gnus-summary-next-unread-subject
1710   "\M-p" gnus-summary-prev-unread-subject
1711   "." gnus-summary-first-unread-article
1712   "," gnus-summary-best-unread-article
1713   "\M-s" gnus-summary-search-article-forward
1714   "\M-r" gnus-summary-search-article-backward
1715   "<" gnus-summary-beginning-of-article
1716   ">" gnus-summary-end-of-article
1717   "j" gnus-summary-goto-article
1718   "^" gnus-summary-refer-parent-article
1719   "\M-^" gnus-summary-refer-article
1720   "u" gnus-summary-tick-article-forward
1721   "!" gnus-summary-tick-article-forward
1722   "U" gnus-summary-tick-article-backward
1723   "d" gnus-summary-mark-as-read-forward
1724   "D" gnus-summary-mark-as-read-backward
1725   "E" gnus-summary-mark-as-expirable
1726   "\M-u" gnus-summary-clear-mark-forward
1727   "\M-U" gnus-summary-clear-mark-backward
1728   "k" gnus-summary-kill-same-subject-and-select
1729   "\C-k" gnus-summary-kill-same-subject
1730   "\M-\C-k" gnus-summary-kill-thread
1731   "\M-\C-l" gnus-summary-lower-thread
1732   "e" gnus-summary-edit-article
1733   "#" gnus-summary-mark-as-processable
1734   "\M-#" gnus-summary-unmark-as-processable
1735   "\M-\C-t" gnus-summary-toggle-threads
1736   "\M-\C-s" gnus-summary-show-thread
1737   "\M-\C-h" gnus-summary-hide-thread
1738   "\M-\C-f" gnus-summary-next-thread
1739   "\M-\C-b" gnus-summary-prev-thread
1740   [(meta down)] gnus-summary-next-thread
1741   [(meta up)] gnus-summary-prev-thread
1742   "\M-\C-u" gnus-summary-up-thread
1743   "\M-\C-d" gnus-summary-down-thread
1744   "&" gnus-summary-execute-command
1745   "c" gnus-summary-catchup-and-exit
1746   "\C-w" gnus-summary-mark-region-as-read
1747   "\C-t" gnus-summary-toggle-truncation
1748   "?" gnus-summary-mark-as-dormant
1749   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1750   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1751   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1752   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1753   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1754   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1755   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1756   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1757   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1758   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1759   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1760   "=" gnus-summary-expand-window
1761   "\C-x\C-s" gnus-summary-reselect-current-group
1762   "\M-g" gnus-summary-rescan-group
1763   "w" gnus-summary-stop-page-breaking
1764   "\C-c\C-r" gnus-summary-caesar-message
1765   "f" gnus-summary-followup
1766   "F" gnus-summary-followup-with-original
1767   "C" gnus-summary-cancel-article
1768   "r" gnus-summary-reply
1769   "R" gnus-summary-reply-with-original
1770   "\C-c\C-f" gnus-summary-mail-forward
1771   "o" gnus-summary-save-article
1772   "\C-o" gnus-summary-save-article-mail
1773   "|" gnus-summary-pipe-output
1774   "\M-k" gnus-summary-edit-local-kill
1775   "\M-K" gnus-summary-edit-global-kill
1776   ;; "V" gnus-version
1777   "\C-c\C-d" gnus-summary-describe-group
1778   "q" gnus-summary-exit
1779   "Q" gnus-summary-exit-no-update
1780   "\C-c\C-i" gnus-info-find-node
1781   gnus-mouse-2 gnus-mouse-pick-article
1782   [follow-link] mouse-face
1783   "m" gnus-summary-mail-other-window
1784   "a" gnus-summary-post-news
1785   "i" gnus-summary-news-other-window
1786   "x" gnus-summary-limit-to-unread
1787   "s" gnus-summary-isearch-article
1788   "t" gnus-summary-toggle-header
1789   "g" gnus-summary-show-article
1790   "l" gnus-summary-goto-last-article
1791   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1792   "\C-d" gnus-summary-enter-digest-group
1793   "\M-\C-d" gnus-summary-read-document
1794   "\M-\C-e" gnus-summary-edit-parameters
1795   "\M-\C-a" gnus-summary-customize-parameters
1796   "\C-c\C-b" gnus-bug
1797   "*" gnus-cache-enter-article
1798   "\M-*" gnus-cache-remove-article
1799   "\M-&" gnus-summary-universal-argument
1800   "\C-l" gnus-recenter
1801   "I" gnus-summary-increase-score
1802   "L" gnus-summary-lower-score
1803   "\M-i" gnus-symbolic-argument
1804   "h" gnus-summary-select-article-buffer
1805
1806   "b" gnus-article-view-part
1807   "\M-t" gnus-summary-toggle-display-buttonized
1808
1809   "V" gnus-summary-score-map
1810   "X" gnus-uu-extract-map
1811   "S" gnus-summary-send-map)
1812
1813 ;; Sort of orthogonal keymap
1814 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1815   "t" gnus-summary-tick-article-forward
1816   "!" gnus-summary-tick-article-forward
1817   "d" gnus-summary-mark-as-read-forward
1818   "r" gnus-summary-mark-as-read-forward
1819   "c" gnus-summary-clear-mark-forward
1820   " " gnus-summary-clear-mark-forward
1821   "e" gnus-summary-mark-as-expirable
1822   "x" gnus-summary-mark-as-expirable
1823   "?" gnus-summary-mark-as-dormant
1824   "b" gnus-summary-set-bookmark
1825   "B" gnus-summary-remove-bookmark
1826   "#" gnus-summary-mark-as-processable
1827   "\M-#" gnus-summary-unmark-as-processable
1828   "S" gnus-summary-limit-include-expunged
1829   "C" gnus-summary-catchup
1830   "H" gnus-summary-catchup-to-here
1831   "h" gnus-summary-catchup-from-here
1832   "\C-c" gnus-summary-catchup-all
1833   "k" gnus-summary-kill-same-subject-and-select
1834   "K" gnus-summary-kill-same-subject
1835   "P" gnus-uu-mark-map)
1836
1837 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1838   "c" gnus-summary-clear-above
1839   "u" gnus-summary-tick-above
1840   "m" gnus-summary-mark-above
1841   "k" gnus-summary-kill-below)
1842
1843 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1844   "/" gnus-summary-limit-to-subject
1845   "n" gnus-summary-limit-to-articles
1846   "w" gnus-summary-pop-limit
1847   "s" gnus-summary-limit-to-subject
1848   "a" gnus-summary-limit-to-author
1849   "u" gnus-summary-limit-to-unread
1850   "m" gnus-summary-limit-to-marks
1851   "M" gnus-summary-limit-exclude-marks
1852   "v" gnus-summary-limit-to-score
1853   "*" gnus-summary-limit-include-cached
1854   "D" gnus-summary-limit-include-dormant
1855   "T" gnus-summary-limit-include-thread
1856   "d" gnus-summary-limit-exclude-dormant
1857   "t" gnus-summary-limit-to-age
1858   "." gnus-summary-limit-to-unseen
1859   "x" gnus-summary-limit-to-extra
1860   "p" gnus-summary-limit-to-display-predicate
1861   "E" gnus-summary-limit-include-expunged
1862   "c" gnus-summary-limit-exclude-childless-dormant
1863   "C" gnus-summary-limit-mark-excluded-as-read
1864   "o" gnus-summary-insert-old-articles
1865   "N" gnus-summary-insert-new-articles
1866   "r" gnus-summary-limit-to-replied
1867   "R" gnus-summary-limit-to-recipient)
1868
1869 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1870   "n" gnus-summary-next-unread-article
1871   "p" gnus-summary-prev-unread-article
1872   "N" gnus-summary-next-article
1873   "P" gnus-summary-prev-article
1874   "\C-n" gnus-summary-next-same-subject
1875   "\C-p" gnus-summary-prev-same-subject
1876   "\M-n" gnus-summary-next-unread-subject
1877   "\M-p" gnus-summary-prev-unread-subject
1878   "f" gnus-summary-first-unread-article
1879   "b" gnus-summary-best-unread-article
1880   "j" gnus-summary-goto-article
1881   "g" gnus-summary-goto-subject
1882   "l" gnus-summary-goto-last-article
1883   "o" gnus-summary-pop-article)
1884
1885 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1886   "k" gnus-summary-kill-thread
1887   "l" gnus-summary-lower-thread
1888   "i" gnus-summary-raise-thread
1889   "T" gnus-summary-toggle-threads
1890   "t" gnus-summary-rethread-current
1891   "^" gnus-summary-reparent-thread
1892   "s" gnus-summary-show-thread
1893   "S" gnus-summary-show-all-threads
1894   "h" gnus-summary-hide-thread
1895   "H" gnus-summary-hide-all-threads
1896   "n" gnus-summary-next-thread
1897   "p" gnus-summary-prev-thread
1898   "u" gnus-summary-up-thread
1899   "o" gnus-summary-top-thread
1900   "d" gnus-summary-down-thread
1901   "#" gnus-uu-mark-thread
1902   "\M-#" gnus-uu-unmark-thread)
1903
1904 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1905   "g" gnus-summary-prepare
1906   "c" gnus-summary-insert-cached-articles
1907   "d" gnus-summary-insert-dormant-articles)
1908
1909 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1910   "c" gnus-summary-catchup-and-exit
1911   "C" gnus-summary-catchup-all-and-exit
1912   "E" gnus-summary-exit-no-update
1913   "Q" gnus-summary-exit
1914   "Z" gnus-summary-exit
1915   "n" gnus-summary-catchup-and-goto-next-group
1916   "p" gnus-summary-catchup-and-goto-prev-group
1917   "R" gnus-summary-reselect-current-group
1918   "G" gnus-summary-rescan-group
1919   "N" gnus-summary-next-group
1920   "s" gnus-summary-save-newsrc
1921   "P" gnus-summary-prev-group)
1922
1923 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1924   " " gnus-summary-next-page
1925   "n" gnus-summary-next-page
1926   "\177" gnus-summary-prev-page
1927   [delete] gnus-summary-prev-page
1928   "p" gnus-summary-prev-page
1929   "\r" gnus-summary-scroll-up
1930   "\M-\r" gnus-summary-scroll-down
1931   "<" gnus-summary-beginning-of-article
1932   ">" gnus-summary-end-of-article
1933   "b" gnus-summary-beginning-of-article
1934   "e" gnus-summary-end-of-article
1935   "^" gnus-summary-refer-parent-article
1936   "r" gnus-summary-refer-parent-article
1937   "D" gnus-summary-enter-digest-group
1938   "R" gnus-summary-refer-references
1939   "T" gnus-summary-refer-thread
1940   "g" gnus-summary-show-article
1941   "s" gnus-summary-isearch-article
1942   "P" gnus-summary-print-article
1943   "M" gnus-mailing-list-insinuate
1944   "t" gnus-article-babel)
1945
1946 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1947   "b" gnus-article-add-buttons
1948   "B" gnus-article-add-buttons-to-head
1949   "o" gnus-article-treat-overstrike
1950   "e" gnus-article-emphasize
1951   "w" gnus-article-fill-cited-article
1952   "Q" gnus-article-fill-long-lines
1953   "C" gnus-article-capitalize-sentences
1954   "c" gnus-article-remove-cr
1955   "q" gnus-article-de-quoted-unreadable
1956   "6" gnus-article-de-base64-unreadable
1957   "Z" gnus-article-decode-HZ
1958   "A" gnus-article-treat-ansi-sequences
1959   "h" gnus-article-wash-html
1960   "u" gnus-article-unsplit-urls
1961   "s" gnus-summary-force-verify-and-decrypt
1962   "f" gnus-article-display-x-face
1963   "l" gnus-summary-stop-page-breaking
1964   "r" gnus-summary-caesar-message
1965   "m" gnus-summary-morse-message
1966   "t" gnus-summary-toggle-header
1967   "g" gnus-treat-smiley
1968   "v" gnus-summary-verbose-headers
1969   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1970   "p" gnus-article-verify-x-pgp-sig
1971   "d" gnus-article-treat-dumbquotes
1972   "i" gnus-summary-idna-message)
1973
1974 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1975   ;; mnemonic: deuglif*Y*
1976   "u" gnus-article-outlook-unwrap-lines
1977   "a" gnus-article-outlook-repair-attribution
1978   "c" gnus-article-outlook-rearrange-citation
1979   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1980
1981 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1982   "a" gnus-article-hide
1983   "h" gnus-article-hide-headers
1984   "b" gnus-article-hide-boring-headers
1985   "s" gnus-article-hide-signature
1986   "c" gnus-article-hide-citation
1987   "C" gnus-article-hide-citation-in-followups
1988   "l" gnus-article-hide-list-identifiers
1989   "B" gnus-article-strip-banner
1990   "P" gnus-article-hide-pem
1991   "\C-c" gnus-article-hide-citation-maybe)
1992
1993 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1994   "a" gnus-article-highlight
1995   "h" gnus-article-highlight-headers
1996   "c" gnus-article-highlight-citation
1997   "s" gnus-article-highlight-signature)
1998
1999 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2000   "f" gnus-article-treat-fold-headers
2001   "u" gnus-article-treat-unfold-headers
2002   "n" gnus-article-treat-fold-newsgroups)
2003
2004 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2005   "x" gnus-article-display-x-face
2006   "d" gnus-article-display-face
2007   "s" gnus-treat-smiley
2008   "D" gnus-article-remove-images
2009   "f" gnus-treat-from-picon
2010   "m" gnus-treat-mail-picon
2011   "n" gnus-treat-newsgroups-picon)
2012
2013 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2014   "w" gnus-article-decode-mime-words
2015   "c" gnus-article-decode-charset
2016   "v" gnus-mime-view-all-parts
2017   "b" gnus-article-view-part)
2018
2019 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2020   "z" gnus-article-date-ut
2021   "u" gnus-article-date-ut
2022   "l" gnus-article-date-local
2023   "p" gnus-article-date-english
2024   "e" gnus-article-date-lapsed
2025   "o" gnus-article-date-original
2026   "i" gnus-article-date-iso8601
2027   "s" gnus-article-date-user)
2028
2029 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2030   "t" gnus-article-remove-trailing-blank-lines
2031   "l" gnus-article-strip-leading-blank-lines
2032   "m" gnus-article-strip-multiple-blank-lines
2033   "a" gnus-article-strip-blank-lines
2034   "A" gnus-article-strip-all-blank-lines
2035   "s" gnus-article-strip-leading-space
2036   "e" gnus-article-strip-trailing-space
2037   "w" gnus-article-remove-leading-whitespace)
2038
2039 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2040   "v" gnus-version
2041   "f" gnus-summary-fetch-faq
2042   "d" gnus-summary-describe-group
2043   "h" gnus-summary-describe-briefly
2044   "i" gnus-info-find-node
2045   "c" gnus-group-fetch-charter
2046   "C" gnus-group-fetch-control)
2047
2048 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2049   "e" gnus-summary-expire-articles
2050   "\M-\C-e" gnus-summary-expire-articles-now
2051   "\177" gnus-summary-delete-article
2052   [delete] gnus-summary-delete-article
2053   [backspace] gnus-summary-delete-article
2054   "m" gnus-summary-move-article
2055   "r" gnus-summary-respool-article
2056   "w" gnus-summary-edit-article
2057   "c" gnus-summary-copy-article
2058   "B" gnus-summary-crosspost-article
2059   "q" gnus-summary-respool-query
2060   "t" gnus-summary-respool-trace
2061   "i" gnus-summary-import-article
2062   "I" gnus-summary-create-article
2063   "p" gnus-summary-article-posted-p)
2064
2065 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2066   "o" gnus-summary-save-article
2067   "m" gnus-summary-save-article-mail
2068   "F" gnus-summary-write-article-file
2069   "r" gnus-summary-save-article-rmail
2070   "f" gnus-summary-save-article-file
2071   "b" gnus-summary-save-article-body-file
2072   "h" gnus-summary-save-article-folder
2073   "v" gnus-summary-save-article-vm
2074   "p" gnus-summary-pipe-output
2075   "P" gnus-summary-muttprint
2076   "s" gnus-soup-add-article)
2077
2078 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2079   "b" gnus-summary-display-buttonized
2080   "m" gnus-summary-repair-multipart
2081   "v" gnus-article-view-part
2082   "o" gnus-article-save-part
2083   "O" gnus-article-save-part-and-strip
2084   "r" gnus-article-replace-part
2085   "d" gnus-article-delete-part
2086   "j" gnus-article-jump-to-part
2087   "c" gnus-article-copy-part
2088   "C" gnus-article-view-part-as-charset
2089   "e" gnus-article-view-part-externally
2090   "E" gnus-article-encrypt-body
2091   "i" gnus-article-inline-part
2092   "|" gnus-article-pipe-part)
2093
2094 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2095   "p" gnus-summary-mark-as-processable
2096   "u" gnus-summary-unmark-as-processable
2097   "U" gnus-summary-unmark-all-processable
2098   "v" gnus-uu-mark-over
2099   "s" gnus-uu-mark-series
2100   "r" gnus-uu-mark-region
2101   "g" gnus-uu-unmark-region
2102   "R" gnus-uu-mark-by-regexp
2103   "G" gnus-uu-unmark-by-regexp
2104   "t" gnus-uu-mark-thread
2105   "T" gnus-uu-unmark-thread
2106   "a" gnus-uu-mark-all
2107   "b" gnus-uu-mark-buffer
2108   "S" gnus-uu-mark-sparse
2109   "k" gnus-summary-kill-process-mark
2110   "y" gnus-summary-yank-process-mark
2111   "w" gnus-summary-save-process-mark
2112   "i" gnus-uu-invert-processable)
2113
2114 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2115   ;;"x" gnus-uu-extract-any
2116   "m" gnus-summary-save-parts
2117   "u" gnus-uu-decode-uu
2118   "U" gnus-uu-decode-uu-and-save
2119   "s" gnus-uu-decode-unshar
2120   "S" gnus-uu-decode-unshar-and-save
2121   "o" gnus-uu-decode-save
2122   "O" gnus-uu-decode-save
2123   "b" gnus-uu-decode-binhex
2124   "B" gnus-uu-decode-binhex
2125   "p" gnus-uu-decode-postscript
2126   "P" gnus-uu-decode-postscript-and-save)
2127
2128 (gnus-define-keys
2129     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2130   "u" gnus-uu-decode-uu-view
2131   "U" gnus-uu-decode-uu-and-save-view
2132   "s" gnus-uu-decode-unshar-view
2133   "S" gnus-uu-decode-unshar-and-save-view
2134   "o" gnus-uu-decode-save-view
2135   "O" gnus-uu-decode-save-view
2136   "b" gnus-uu-decode-binhex-view
2137   "B" gnus-uu-decode-binhex-view
2138   "p" gnus-uu-decode-postscript-view
2139   "P" gnus-uu-decode-postscript-and-save-view)
2140
2141 (defvar gnus-article-post-menu nil)
2142
2143 (defconst gnus-summary-menu-maxlen 20)
2144
2145 (defun gnus-summary-menu-split (menu)
2146   ;; If we have lots of elements, divide them into groups of 20
2147   ;; and make a pane (or submenu) for each one.
2148   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2149       (let ((menu menu) sublists next
2150             (i 1))
2151         (while menu
2152           ;; Pull off the next gnus-summary-menu-maxlen elements
2153           ;; and make them the next element of sublist.
2154           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2155           (if next
2156               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2157                       nil))
2158           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2159                                              (aref (car (last menu)) 0)) menu)
2160                                sublists))
2161           (setq i (1+ i))
2162           (setq menu next))
2163         (nreverse sublists))
2164     ;; Few elements--put them all in one pane.
2165     menu))
2166
2167 (defun gnus-summary-make-menu-bar ()
2168   (gnus-turn-off-edit-menu 'summary)
2169
2170   (unless (boundp 'gnus-summary-misc-menu)
2171
2172     (easy-menu-define
2173       gnus-summary-kill-menu gnus-summary-mode-map ""
2174       (cons
2175        "Score"
2176        (nconc
2177         (list
2178          ["Customize" gnus-score-customize t])
2179         (gnus-make-score-map 'increase)
2180         (gnus-make-score-map 'lower)
2181         '(("Mark"
2182            ["Kill below" gnus-summary-kill-below t]
2183            ["Mark above" gnus-summary-mark-above t]
2184            ["Tick above" gnus-summary-tick-above t]
2185            ["Clear above" gnus-summary-clear-above t])
2186           ["Current score" gnus-summary-current-score t]
2187           ["Set score" gnus-summary-set-score t]
2188           ["Switch current score file..." gnus-score-change-score-file t]
2189           ["Set mark below..." gnus-score-set-mark-below t]
2190           ["Set expunge below..." gnus-score-set-expunge-below t]
2191           ["Edit current score file" gnus-score-edit-current-scores t]
2192           ["Edit score file" gnus-score-edit-file t]
2193           ["Trace score" gnus-score-find-trace t]
2194           ["Find words" gnus-score-find-favourite-words t]
2195           ["Rescore buffer" gnus-summary-rescore t]
2196           ["Increase score..." gnus-summary-increase-score t]
2197           ["Lower score..." gnus-summary-lower-score t]))))
2198
2199     ;; Define both the Article menu in the summary buffer and the
2200     ;; equivalent Commands menu in the article buffer here for
2201     ;; consistency.
2202     (let ((innards
2203            `(("Hide"
2204               ["All" gnus-article-hide t]
2205               ["Headers" gnus-article-hide-headers t]
2206               ["Signature" gnus-article-hide-signature t]
2207               ["Citation" gnus-article-hide-citation t]
2208               ["List identifiers" gnus-article-hide-list-identifiers t]
2209               ["Banner" gnus-article-strip-banner t]
2210               ["Boring headers" gnus-article-hide-boring-headers t])
2211              ("Highlight"
2212               ["All" gnus-article-highlight t]
2213               ["Headers" gnus-article-highlight-headers t]
2214               ["Signature" gnus-article-highlight-signature t]
2215               ["Citation" gnus-article-highlight-citation t])
2216              ("MIME"
2217               ["Words" gnus-article-decode-mime-words t]
2218               ["Charset" gnus-article-decode-charset t]
2219               ["QP" gnus-article-de-quoted-unreadable t]
2220               ["Base64" gnus-article-de-base64-unreadable t]
2221               ["View MIME buttons" gnus-summary-display-buttonized t]
2222               ["View all" gnus-mime-view-all-parts t]
2223               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2224               ["Encrypt body" gnus-article-encrypt-body
2225                :active (not (gnus-group-read-only-p))
2226                ,@(if (featurep 'xemacs) nil
2227                    '(:help "Encrypt the message body on disk"))]
2228               ["Extract all parts..." gnus-summary-save-parts t]
2229               ("Multipart"
2230                ["Repair multipart" gnus-summary-repair-multipart t]
2231                ["Pipe part..." gnus-article-pipe-part t]
2232                ["Inline part" gnus-article-inline-part t]
2233                ["Encrypt body" gnus-article-encrypt-body
2234                 :active (not (gnus-group-read-only-p))
2235                ,@(if (featurep 'xemacs) nil
2236                    '(:help "Encrypt the message body on disk"))]
2237                ["View part externally" gnus-article-view-part-externally t]
2238                ["View part with charset..." gnus-article-view-part-as-charset t]
2239                ["Copy part" gnus-article-copy-part t]
2240                ["Save part..." gnus-article-save-part t]
2241                ["View part" gnus-article-view-part t]))
2242              ("Date"
2243               ["Local" gnus-article-date-local t]
2244               ["ISO8601" gnus-article-date-iso8601 t]
2245               ["UT" gnus-article-date-ut t]
2246               ["Original" gnus-article-date-original t]
2247               ["Lapsed" gnus-article-date-lapsed t]
2248               ["User-defined" gnus-article-date-user t])
2249              ("Display"
2250               ["Remove images" gnus-article-remove-images t]
2251               ["Toggle smiley" gnus-treat-smiley t]
2252               ["Show X-Face" gnus-article-display-x-face t]
2253               ["Show picons in From" gnus-treat-from-picon t]
2254               ["Show picons in mail headers" gnus-treat-mail-picon t]
2255               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2256               ("View as different encoding"
2257                ,@(gnus-summary-menu-split
2258                   (mapcar
2259                    (lambda (cs)
2260                      ;; Since easymenu under Emacs doesn't allow
2261                      ;; lambda forms for menu commands, we should
2262                      ;; provide intern'ed function symbols.
2263                      (let ((command (intern (format "\
2264 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2265                        (fset command
2266                              `(lambda ()
2267                                 (interactive)
2268                                 (let ((gnus-summary-show-article-charset-alist
2269                                        '((1 . ,cs))))
2270                                   (gnus-summary-show-article 1))))
2271                        `[,(symbol-name cs) ,command t]))
2272                    (sort (if (fboundp 'coding-system-list)
2273                              (coding-system-list)
2274                            (mapcar 'car mm-mime-mule-charset-alist))
2275                          'string<)))))
2276              ("Washing"
2277               ("Remove Blanks"
2278                ["Leading" gnus-article-strip-leading-blank-lines t]
2279                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2280                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2281                ["All of the above" gnus-article-strip-blank-lines t]
2282                ["All" gnus-article-strip-all-blank-lines t]
2283                ["Leading space" gnus-article-strip-leading-space t]
2284                ["Trailing space" gnus-article-strip-trailing-space t]
2285                ["Leading space in headers"
2286                 gnus-article-remove-leading-whitespace t])
2287               ["Overstrike" gnus-article-treat-overstrike t]
2288               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2289               ["Emphasis" gnus-article-emphasize t]
2290               ["Word wrap" gnus-article-fill-cited-article t]
2291               ["Fill long lines" gnus-article-fill-long-lines t]
2292               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2293               ["Remove CR" gnus-article-remove-cr t]
2294               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2295               ["Base64" gnus-article-de-base64-unreadable t]
2296               ["Rot 13" gnus-summary-caesar-message
2297                ,@(if (featurep 'xemacs) '(t)
2298                    '(:help "\"Caesar rotate\" article by 13"))]
2299               ["De-IDNA" gnus-summary-idna-message t]
2300               ["Morse decode" gnus-summary-morse-message t]
2301               ["Unix pipe..." gnus-summary-pipe-message t]
2302               ["Add buttons" gnus-article-add-buttons t]
2303               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2304               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2305               ["Verbose header" gnus-summary-verbose-headers t]
2306               ["Toggle header" gnus-summary-toggle-header t]
2307               ["Unfold headers" gnus-article-treat-unfold-headers t]
2308               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2309               ["Html" gnus-article-wash-html t]
2310               ["Unsplit URLs" gnus-article-unsplit-urls t]
2311               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2312               ["Decode HZ" gnus-article-decode-HZ t]
2313               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2314               ("(Outlook) Deuglify"
2315                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2316                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2317                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2318                ["Full (Outlook) deuglify"
2319                 gnus-article-outlook-deuglify-article t])
2320               )
2321              ("Output"
2322               ["Save in default format..." gnus-summary-save-article
2323                ,@(if (featurep 'xemacs) '(t)
2324                    '(:help "Save article using default method"))]
2325               ["Save in file..." gnus-summary-save-article-file
2326                ,@(if (featurep 'xemacs) '(t)
2327                    '(:help "Save article in file"))]
2328               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2329               ["Save in MH folder..." gnus-summary-save-article-folder t]
2330               ["Save in VM folder..." gnus-summary-save-article-vm t]
2331               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2332               ["Save body in file..." gnus-summary-save-article-body-file t]
2333               ["Pipe through a filter..." gnus-summary-pipe-output t]
2334               ["Add to SOUP packet" gnus-soup-add-article t]
2335               ["Print with Muttprint..." gnus-summary-muttprint t]
2336               ["Print" gnus-summary-print-article
2337                ,@(if (featurep 'xemacs) '(t)
2338                    '(:help "Generate and print a PostScript image"))])
2339              ("Copy, move,... (Backend)"
2340               ,@(if (featurep 'xemacs) nil
2341                   '(:help "Copying, moving, expiring articles..."))
2342               ["Respool article..." gnus-summary-respool-article t]
2343               ["Move article..." gnus-summary-move-article
2344                (gnus-check-backend-function
2345                 'request-move-article gnus-newsgroup-name)]
2346               ["Copy article..." gnus-summary-copy-article t]
2347               ["Crosspost article..." gnus-summary-crosspost-article
2348                (gnus-check-backend-function
2349                 'request-replace-article gnus-newsgroup-name)]
2350               ["Import file..." gnus-summary-import-article
2351                (gnus-check-backend-function
2352                 'request-accept-article gnus-newsgroup-name)]
2353               ["Create article..." gnus-summary-create-article
2354                (gnus-check-backend-function
2355                 'request-accept-article gnus-newsgroup-name)]
2356               ["Check if posted" gnus-summary-article-posted-p t]
2357               ["Edit article" gnus-summary-edit-article
2358                (not (gnus-group-read-only-p))]
2359               ["Delete article" gnus-summary-delete-article
2360                (gnus-check-backend-function
2361                 'request-expire-articles gnus-newsgroup-name)]
2362               ["Query respool" gnus-summary-respool-query t]
2363               ["Trace respool" gnus-summary-respool-trace t]
2364               ["Delete expirable articles" gnus-summary-expire-articles-now
2365                (gnus-check-backend-function
2366                 'request-expire-articles gnus-newsgroup-name)])
2367              ("Extract"
2368               ["Uudecode" gnus-uu-decode-uu
2369                ,@(if (featurep 'xemacs) '(t)
2370                    '(:help "Decode uuencoded article(s)"))]
2371               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2372               ["Unshar" gnus-uu-decode-unshar t]
2373               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2374               ["Save" gnus-uu-decode-save t]
2375               ["Binhex" gnus-uu-decode-binhex t]
2376               ["Postscript" gnus-uu-decode-postscript t]
2377               ["All MIME parts" gnus-summary-save-parts t])
2378              ("Cache"
2379               ["Enter article" gnus-cache-enter-article t]
2380               ["Remove article" gnus-cache-remove-article t])
2381              ["Translate" gnus-article-babel t]
2382              ["Select article buffer" gnus-summary-select-article-buffer t]
2383              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2384              ["Isearch article..." gnus-summary-isearch-article t]
2385              ["Beginning of the article" gnus-summary-beginning-of-article t]
2386              ["End of the article" gnus-summary-end-of-article t]
2387              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2388              ["Fetch referenced articles" gnus-summary-refer-references t]
2389              ["Fetch current thread" gnus-summary-refer-thread t]
2390              ["Fetch article with id..." gnus-summary-refer-article t]
2391              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2392              ["Redisplay" gnus-summary-show-article t]
2393              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2394       (easy-menu-define
2395         gnus-summary-article-menu gnus-summary-mode-map ""
2396         (cons "Article" innards))
2397
2398       (if (not (keymapp gnus-summary-article-menu))
2399           (easy-menu-define
2400             gnus-article-commands-menu gnus-article-mode-map ""
2401             (cons "Commands" innards))
2402         ;; in Emacs, don't share menu.
2403         (setq gnus-article-commands-menu
2404               (copy-keymap gnus-summary-article-menu))
2405         (define-key gnus-article-mode-map [menu-bar commands]
2406           (cons "Commands" gnus-article-commands-menu))))
2407
2408     (easy-menu-define
2409       gnus-summary-thread-menu gnus-summary-mode-map ""
2410       '("Threads"
2411         ["Find all messages in thread" gnus-summary-refer-thread t]
2412         ["Toggle threading" gnus-summary-toggle-threads t]
2413         ["Hide threads" gnus-summary-hide-all-threads t]
2414         ["Show threads" gnus-summary-show-all-threads t]
2415         ["Hide thread" gnus-summary-hide-thread t]
2416         ["Show thread" gnus-summary-show-thread t]
2417         ["Go to next thread" gnus-summary-next-thread t]
2418         ["Go to previous thread" gnus-summary-prev-thread t]
2419         ["Go down thread" gnus-summary-down-thread t]
2420         ["Go up thread" gnus-summary-up-thread t]
2421         ["Top of thread" gnus-summary-top-thread t]
2422         ["Mark thread as read" gnus-summary-kill-thread t]
2423         ["Lower thread score" gnus-summary-lower-thread t]
2424         ["Raise thread score" gnus-summary-raise-thread t]
2425         ["Rethread current" gnus-summary-rethread-current t]))
2426
2427     (easy-menu-define
2428       gnus-summary-post-menu gnus-summary-mode-map ""
2429       `("Post"
2430         ["Send a message (mail or news)" gnus-summary-post-news
2431          ,@(if (featurep 'xemacs) '(t)
2432              '(:help "Compose a new message (mail or news)"))]
2433         ["Followup" gnus-summary-followup
2434          ,@(if (featurep 'xemacs) '(t)
2435              '(:help "Post followup to this article"))]
2436         ["Followup and yank" gnus-summary-followup-with-original
2437          ,@(if (featurep 'xemacs) '(t)
2438              '(:help "Post followup to this article, quoting its contents"))]
2439         ["Supersede article" gnus-summary-supersede-article t]
2440         ["Cancel article" gnus-summary-cancel-article
2441          ,@(if (featurep 'xemacs) '(t)
2442              '(:help "Cancel an article you posted"))]
2443         ["Reply" gnus-summary-reply t]
2444         ["Reply and yank" gnus-summary-reply-with-original t]
2445         ["Wide reply" gnus-summary-wide-reply t]
2446         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2447          ,@(if (featurep 'xemacs) '(t)
2448              '(:help "Mail a reply, quoting this article"))]
2449         ["Very wide reply" gnus-summary-very-wide-reply t]
2450         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2451          ,@(if (featurep 'xemacs) '(t)
2452              '(:help "Mail a very wide reply, quoting this article"))]
2453         ["Mail forward" gnus-summary-mail-forward t]
2454         ["Post forward" gnus-summary-post-forward t]
2455         ["Digest and mail" gnus-uu-digest-mail-forward t]
2456         ["Digest and post" gnus-uu-digest-post-forward t]
2457         ["Resend message" gnus-summary-resend-message t]
2458         ["Resend message edit" gnus-summary-resend-message-edit t]
2459         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2460         ["Send a mail" gnus-summary-mail-other-window t]
2461         ["Create a local message" gnus-summary-news-other-window t]
2462         ["Uuencode and post" gnus-uu-post-news
2463          ,@(if (featurep 'xemacs) '(t)
2464              '(:help "Post a uuencoded article"))]
2465         ["Followup via news" gnus-summary-followup-to-mail t]
2466         ["Followup via news and yank"
2467          gnus-summary-followup-to-mail-with-original t]
2468         ;;("Draft"
2469         ;;["Send" gnus-summary-send-draft t]
2470         ;;["Send bounced" gnus-resend-bounced-mail t])
2471         ))
2472
2473     (cond
2474      ((not (keymapp gnus-summary-post-menu))
2475       (setq gnus-article-post-menu gnus-summary-post-menu))
2476      ((not gnus-article-post-menu)
2477       ;; Don't share post menu.
2478       (setq gnus-article-post-menu
2479             (copy-keymap gnus-summary-post-menu))))
2480     (define-key gnus-article-mode-map [menu-bar post]
2481       (cons "Post" gnus-article-post-menu))
2482
2483     (easy-menu-define
2484       gnus-summary-misc-menu gnus-summary-mode-map ""
2485       `("Gnus"
2486         ("Mark Read"
2487          ["Mark as read" gnus-summary-mark-as-read-forward t]
2488          ["Mark same subject and select"
2489           gnus-summary-kill-same-subject-and-select t]
2490          ["Mark same subject" gnus-summary-kill-same-subject t]
2491          ["Catchup" gnus-summary-catchup
2492           ,@(if (featurep 'xemacs) '(t)
2493               '(:help "Mark unread articles in this group as read"))]
2494          ["Catchup all" gnus-summary-catchup-all t]
2495          ["Catchup to here" gnus-summary-catchup-to-here t]
2496          ["Catchup from here" gnus-summary-catchup-from-here t]
2497          ["Catchup region" gnus-summary-mark-region-as-read
2498           (gnus-mark-active-p)]
2499          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2500         ("Mark Various"
2501          ["Tick" gnus-summary-tick-article-forward t]
2502          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2503          ["Remove marks" gnus-summary-clear-mark-forward t]
2504          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2505          ["Set bookmark" gnus-summary-set-bookmark t]
2506          ["Remove bookmark" gnus-summary-remove-bookmark t])
2507         ("Limit to"
2508          ["Marks..." gnus-summary-limit-to-marks t]
2509          ["Subject..." gnus-summary-limit-to-subject t]
2510          ["Author..." gnus-summary-limit-to-author t]
2511          ["Recipient..." gnus-summary-limit-to-recipient t]
2512          ["Age..." gnus-summary-limit-to-age t]
2513          ["Extra..." gnus-summary-limit-to-extra t]
2514          ["Score..." gnus-summary-limit-to-score t]
2515          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2516          ["Unread" gnus-summary-limit-to-unread t]
2517          ["Unseen" gnus-summary-limit-to-unseen t]
2518          ["Replied" gnus-summary-limit-to-replied t]
2519          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2520          ["Next articles" gnus-summary-limit-to-articles t]
2521          ["Pop limit" gnus-summary-pop-limit t]
2522          ["Show dormant" gnus-summary-limit-include-dormant t]
2523          ["Hide childless dormant"
2524           gnus-summary-limit-exclude-childless-dormant t]
2525          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2526          ["Hide marked" gnus-summary-limit-exclude-marks t]
2527          ["Show expunged" gnus-summary-limit-include-expunged t])
2528         ("Process Mark"
2529          ["Set mark" gnus-summary-mark-as-processable t]
2530          ["Remove mark" gnus-summary-unmark-as-processable t]
2531          ["Remove all marks" gnus-summary-unmark-all-processable t]
2532          ["Invert marks" gnus-uu-invert-processable t]
2533          ["Mark above" gnus-uu-mark-over t]
2534          ["Mark series" gnus-uu-mark-series t]
2535          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2536          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2537          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2538          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2539          ["Mark all" gnus-uu-mark-all t]
2540          ["Mark buffer" gnus-uu-mark-buffer t]
2541          ["Mark sparse" gnus-uu-mark-sparse t]
2542          ["Mark thread" gnus-uu-mark-thread t]
2543          ["Unmark thread" gnus-uu-unmark-thread t]
2544          ("Process Mark Sets"
2545           ["Kill" gnus-summary-kill-process-mark t]
2546           ["Yank" gnus-summary-yank-process-mark
2547            gnus-newsgroup-process-stack]
2548           ["Save" gnus-summary-save-process-mark t]
2549           ["Run command on marked..." gnus-summary-universal-argument t]))
2550         ("Scroll article"
2551          ["Page forward" gnus-summary-next-page
2552           ,@(if (featurep 'xemacs) '(t)
2553               '(:help "Show next page of article"))]
2554          ["Page backward" gnus-summary-prev-page
2555           ,@(if (featurep 'xemacs) '(t)
2556               '(:help "Show previous page of article"))]
2557          ["Line forward" gnus-summary-scroll-up t])
2558         ("Move"
2559          ["Next unread article" gnus-summary-next-unread-article t]
2560          ["Previous unread article" gnus-summary-prev-unread-article t]
2561          ["Next article" gnus-summary-next-article t]
2562          ["Previous article" gnus-summary-prev-article t]
2563          ["Next unread subject" gnus-summary-next-unread-subject t]
2564          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2565          ["Next article same subject" gnus-summary-next-same-subject t]
2566          ["Previous article same subject" gnus-summary-prev-same-subject t]
2567          ["First unread article" gnus-summary-first-unread-article t]
2568          ["Best unread article" gnus-summary-best-unread-article t]
2569          ["Go to subject number..." gnus-summary-goto-subject t]
2570          ["Go to article number..." gnus-summary-goto-article t]
2571          ["Go to the last article" gnus-summary-goto-last-article t]
2572          ["Pop article off history" gnus-summary-pop-article t])
2573         ("Sort"
2574          ["Sort by number" gnus-summary-sort-by-number t]
2575          ["Sort by author" gnus-summary-sort-by-author t]
2576          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2577          ["Sort by subject" gnus-summary-sort-by-subject t]
2578          ["Sort by date" gnus-summary-sort-by-date t]
2579          ["Sort by score" gnus-summary-sort-by-score t]
2580          ["Sort by lines" gnus-summary-sort-by-lines t]
2581          ["Sort by characters" gnus-summary-sort-by-chars t]
2582          ["Randomize" gnus-summary-sort-by-random t]
2583          ["Original sort" gnus-summary-sort-by-original t])
2584         ("Help"
2585          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2586          ["Describe group" gnus-summary-describe-group t]
2587          ["Fetch charter" gnus-group-fetch-charter
2588           ,@(if (featurep 'xemacs) nil
2589               '(:help "Display the charter of the current group"))]
2590          ["Fetch control message" gnus-group-fetch-control
2591           ,@(if (featurep 'xemacs) nil
2592               '(:help "Display the archived control message for the current group"))]
2593          ["Read manual" gnus-info-find-node t])
2594         ("Modes"
2595          ["Pick and read" gnus-pick-mode t]
2596          ["Binary" gnus-binary-mode t])
2597         ("Regeneration"
2598          ["Regenerate" gnus-summary-prepare t]
2599          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2600          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2601          ["Toggle threading" gnus-summary-toggle-threads t])
2602         ["See old articles" gnus-summary-insert-old-articles t]
2603         ["See new articles" gnus-summary-insert-new-articles t]
2604         ["Filter articles..." gnus-summary-execute-command t]
2605         ["Run command on articles..." gnus-summary-universal-argument t]
2606         ["Search articles forward..." gnus-summary-search-article-forward t]
2607         ["Search articles backward..." gnus-summary-search-article-backward t]
2608         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2609         ["Expand window" gnus-summary-expand-window t]
2610         ["Expire expirable articles" gnus-summary-expire-articles
2611          (gnus-check-backend-function
2612           'request-expire-articles gnus-newsgroup-name)]
2613         ["Edit local kill file" gnus-summary-edit-local-kill t]
2614         ["Edit main kill file" gnus-summary-edit-global-kill t]
2615         ["Edit group parameters" gnus-summary-edit-parameters t]
2616         ["Customize group parameters" gnus-summary-customize-parameters t]
2617         ["Send a bug report" gnus-bug t]
2618         ("Exit"
2619          ["Catchup and exit" gnus-summary-catchup-and-exit
2620           ,@(if (featurep 'xemacs) '(t)
2621               '(:help "Mark unread articles in this group as read, then exit"))]
2622          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2623          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2624          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2625          ["Exit group" gnus-summary-exit
2626           ,@(if (featurep 'xemacs) '(t)
2627               '(:help "Exit current group, return to group selection mode"))]
2628          ["Exit group without updating" gnus-summary-exit-no-update t]
2629          ["Exit and goto next group" gnus-summary-next-group t]
2630          ["Exit and goto prev group" gnus-summary-prev-group t]
2631          ["Reselect group" gnus-summary-reselect-current-group t]
2632          ["Rescan group" gnus-summary-rescan-group t]
2633          ["Update dribble" gnus-summary-save-newsrc t])))
2634
2635     (gnus-run-hooks 'gnus-summary-menu-hook)))
2636
2637 (defvar gnus-summary-tool-bar-map nil)
2638
2639 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2640 ;; affect _new_ message buffers.  We might add a function that walks thru all
2641 ;; summary-mode buffers and force the update.
2642 (defun gnus-summary-tool-bar-update (&optional symbol value)
2643   "Update summary mode toolbar.
2644 Setter function for custom variables."
2645   (setq-default gnus-summary-tool-bar-map nil)
2646   (when symbol
2647     ;; When used as ":set" function:
2648     (set-default symbol value))
2649   (when (gnus-buffer-live-p gnus-summary-buffer)
2650     (with-current-buffer gnus-summary-buffer
2651       (gnus-summary-make-tool-bar))))
2652
2653 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2654                                      'gnus-summary-tool-bar-gnome
2655                                    'gnus-summary-tool-bar-retro)
2656   "Specifies the Gnus summary tool bar.
2657
2658 It can be either a list or a symbol refering to a list.  See
2659 `gmm-tool-bar-from-list' for the format of the list.  The
2660 default key map is `gnus-summary-mode-map'.
2661
2662 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2663 `gnus-summary-tool-bar-retro'."
2664   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2665                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2666                  (repeat :tag "User defined list" gmm-tool-bar-item)
2667                  (symbol))
2668   :version "23.0" ;; No Gnus
2669   :initialize 'custom-initialize-default
2670   :set 'gnus-summary-tool-bar-update
2671   :group 'gnus-summary)
2672
2673 (defcustom gnus-summary-tool-bar-gnome
2674   '((gnus-summary-post-news "mail/compose" nil)
2675     (gnus-summary-insert-new-articles "mail/inbox" nil
2676                                       :visible (or (not gnus-agent)
2677                                                    gnus-plugged))
2678     (gnus-summary-reply-with-original "mail/reply")
2679     (gnus-summary-reply "mail/reply" nil :visible nil)
2680     (gnus-summary-followup-with-original "mail/reply-all")
2681     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2682     (gnus-summary-mail-forward "mail/forward")
2683     (gnus-summary-save-article "mail/save")
2684     (gnus-summary-search-article-forward "search" nil :visible nil)
2685     (gnus-summary-print-article "print")
2686     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2687     ;; Some new commands that may need more suitable icons:
2688     (gnus-summary-save-newsrc "save" nil :visible nil)
2689     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2690     (gnus-summary-prev-article "left-arrow")
2691     (gnus-summary-next-article "right-arrow")
2692     (gnus-summary-next-page "next-page")
2693     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2694     ;;
2695     ;; Maybe some sort-by-... could be added:
2696     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2697     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2698     (gnus-summary-mark-as-expirable
2699      "delete" nil
2700      :visible (gnus-check-backend-function 'request-expire-articles
2701                                            gnus-newsgroup-name))
2702     (gnus-summary-mark-as-spam
2703      "mail/spam" t
2704      :visible (and (fboundp 'spam-group-ham-contents-p)
2705                    (spam-group-ham-contents-p gnus-newsgroup-name))
2706      :help "Mark as spam")
2707     (gnus-summary-mark-as-read-forward
2708      "mail/not-spam" nil
2709      :visible (and (fboundp 'spam-group-spam-contents-p)
2710                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2711     ;;
2712     (gnus-summary-exit "exit")
2713     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2714     (gnus-info-find-node "help"))
2715   "List of functions for the summary tool bar (GNOME style).
2716
2717 See `gmm-tool-bar-from-list' for the format of the list."
2718   :type '(repeat gmm-tool-bar-item)
2719   :version "23.0" ;; No Gnus
2720   :initialize 'custom-initialize-default
2721   :set 'gnus-summary-tool-bar-update
2722   :group 'gnus-summary)
2723
2724 (defcustom gnus-summary-tool-bar-retro
2725   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2726     (gnus-summary-next-unread-article "gnus/next-ur")
2727     (gnus-summary-post-news "gnus/post")
2728     (gnus-summary-followup-with-original "gnus/fuwo")
2729     (gnus-summary-followup "gnus/followup")
2730     (gnus-summary-reply-with-original "gnus/reply-wo")
2731     (gnus-summary-reply "gnus/reply")
2732     (gnus-summary-caesar-message "gnus/rot13")
2733     (gnus-uu-decode-uu "gnus/uu-decode")
2734     (gnus-summary-save-article-file "gnus/save-aif")
2735     (gnus-summary-save-article "gnus/save-art")
2736     (gnus-uu-post-news "gnus/uu-post")
2737     (gnus-summary-catchup "gnus/catchup")
2738     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2739     (gnus-summary-exit "gnus/exit-summ")
2740     ;; Some new command that may need more suitable icons:
2741     (gnus-summary-print-article "gnus/print" nil :visible nil)
2742     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2743     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2744     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2745     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2746     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2747     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2748     ;;
2749     (gnus-info-find-node "gnus/help" nil :visible nil))
2750   "List of functions for the summary tool bar (retro look).
2751
2752 See `gmm-tool-bar-from-list' for the format of the list."
2753   :type '(repeat gmm-tool-bar-item)
2754   :version "23.0" ;; No Gnus
2755   :initialize 'custom-initialize-default
2756   :set 'gnus-summary-tool-bar-update
2757   :group 'gnus-summary)
2758
2759 (defcustom gnus-summary-tool-bar-zap-list t
2760   "List of icon items from the global tool bar.
2761 These items are not displayed in the Gnus summary mode tool bar.
2762
2763 See `gmm-tool-bar-from-list' for the format of the list."
2764   :type 'gmm-tool-bar-zap-list
2765   :version "23.0" ;; No Gnus
2766   :initialize 'custom-initialize-default
2767   :set 'gnus-summary-tool-bar-update
2768   :group 'gnus-summary)
2769
2770 (defun gnus-summary-make-tool-bar (&optional force)
2771   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2772 When FORCE, rebuild the tool bar."
2773   (when (and (not (featurep 'xemacs))
2774              (boundp 'tool-bar-mode)
2775              tool-bar-mode
2776              (or (not gnus-summary-tool-bar-map) force))
2777     (let* ((load-path
2778             (gmm-image-load-path "gnus" "mail/save.xpm" 'load-path))
2779            (image-load-path
2780             (gmm-image-load-path "gnus" "mail/save.xpm" 'image-load-path))
2781            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2782                                         gnus-summary-tool-bar-zap-list
2783                                         'gnus-summary-mode-map)))
2784       (when map
2785         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2786         ;; uses it's value.
2787         (setq gnus-summary-tool-bar-map map))))
2788   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2789
2790 (defun gnus-score-set-default (var value)
2791   "A version of set that updates the GNU Emacs menu-bar."
2792   (set var value)
2793   ;; It is the message that forces the active status to be updated.
2794   (message ""))
2795
2796 (defun gnus-make-score-map (type)
2797   "Make a summary score map of type TYPE."
2798   (if t
2799       nil
2800     (let ((headers '(("author" "from" string)
2801                      ("subject" "subject" string)
2802                      ("article body" "body" string)
2803                      ("article head" "head" string)
2804                      ("xref" "xref" string)
2805                      ("extra header" "extra" string)
2806                      ("lines" "lines" number)
2807                      ("followups to author" "followup" string)))
2808           (types '((number ("less than" <)
2809                            ("greater than" >)
2810                            ("equal" =))
2811                    (string ("substring" s)
2812                            ("exact string" e)
2813                            ("fuzzy string" f)
2814                            ("regexp" r))))
2815           (perms '(("temporary" (current-time-string))
2816                    ("permanent" nil)
2817                    ("immediate" now)))
2818           header)
2819       (list
2820        (apply
2821         'nconc
2822         (list
2823          (if (eq type 'lower)
2824              "Lower score"
2825            "Increase score"))
2826         (let (outh)
2827           (while headers
2828             (setq header (car headers))
2829             (setq outh
2830                   (cons
2831                    (apply
2832                     'nconc
2833                     (list (car header))
2834                     (let ((ts (cdr (assoc (nth 2 header) types)))
2835                           outt)
2836                       (while ts
2837                         (setq outt
2838                               (cons
2839                                (apply
2840                                 'nconc
2841                                 (list (caar ts))
2842                                 (let ((ps perms)
2843                                       outp)
2844                                   (while ps
2845                                     (setq outp
2846                                           (cons
2847                                            (vector
2848                                             (caar ps)
2849                                             (list
2850                                              'gnus-summary-score-entry
2851                                              (nth 1 header)
2852                                              (if (or (string= (nth 1 header)
2853                                                               "head")
2854                                                      (string= (nth 1 header)
2855                                                               "body"))
2856                                                  ""
2857                                                (list 'gnus-summary-header
2858                                                      (nth 1 header)))
2859                                              (list 'quote (nth 1 (car ts)))
2860                                              (list 'gnus-score-delta-default
2861                                                    nil)
2862                                              (nth 1 (car ps))
2863                                              t)
2864                                             t)
2865                                            outp))
2866                                     (setq ps (cdr ps)))
2867                                   (list (nreverse outp))))
2868                                outt))
2869                         (setq ts (cdr ts)))
2870                       (list (nreverse outt))))
2871                    outh))
2872             (setq headers (cdr headers)))
2873           (list (nreverse outh))))))))
2874
2875 \f
2876
2877 (defun gnus-summary-mode (&optional group)
2878   "Major mode for reading articles.
2879
2880 All normal editing commands are switched off.
2881 \\<gnus-summary-mode-map>
2882 Each line in this buffer represents one article.  To read an
2883 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2884 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2885 respectively.
2886
2887 You can also post articles and send mail from this buffer.  To
2888 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2889 of an article, type `\\[gnus-summary-reply]'.
2890
2891 There are approx. one gazillion commands you can execute in this
2892 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2893
2894 The following commands are available:
2895
2896 \\{gnus-summary-mode-map}"
2897   (interactive)
2898   (kill-all-local-variables)
2899   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2900     (gnus-summary-make-local-variables))
2901   (gnus-summary-make-local-variables)
2902   (setq gnus-newsgroup-name group)
2903   (when (gnus-visual-p 'summary-menu 'menu)
2904     (gnus-summary-make-menu-bar)
2905     (gnus-summary-make-tool-bar))
2906   (gnus-make-thread-indent-array)
2907   (gnus-simplify-mode-line)
2908   (setq major-mode 'gnus-summary-mode)
2909   (setq mode-name "Summary")
2910   (make-local-variable 'minor-mode-alist)
2911   (use-local-map gnus-summary-mode-map)
2912   (buffer-disable-undo)
2913   (setq buffer-read-only t              ;Disable modification
2914         show-trailing-whitespace nil)
2915   (setq truncate-lines t)
2916   (setq selective-display t)
2917   (setq selective-display-ellipses t)   ;Display `...'
2918   (gnus-summary-set-display-table)
2919   (gnus-set-default-directory)
2920   (make-local-variable 'gnus-summary-line-format)
2921   (make-local-variable 'gnus-summary-line-format-spec)
2922   (make-local-variable 'gnus-summary-dummy-line-format)
2923   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2924   (make-local-variable 'gnus-summary-mark-positions)
2925   (gnus-make-local-hook 'pre-command-hook)
2926   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2927   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2928   (turn-on-gnus-mailing-list-mode)
2929   (mm-enable-multibyte)
2930   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2931   (gnus-update-summary-mark-positions))
2932
2933 (defun gnus-summary-make-local-variables ()
2934   "Make all the local summary buffer variables."
2935   (let (global)
2936     (dolist (local gnus-summary-local-variables)
2937       (if (consp local)
2938           (progn
2939             (if (eq (cdr local) 'global)
2940                 ;; Copy the global value of the variable.
2941                 (setq global (symbol-value (car local)))
2942               ;; Use the value from the list.
2943               (setq global (eval (cdr local))))
2944             (set (make-local-variable (car local)) global))
2945         ;; Simple nil-valued local variable.
2946         (set (make-local-variable local) nil)))))
2947
2948 (defun gnus-summary-clear-local-variables ()
2949   (let ((locals gnus-summary-local-variables))
2950     (while locals
2951       (if (consp (car locals))
2952           (and (symbolp (caar locals))
2953                (set (caar locals) nil))
2954         (and (symbolp (car locals))
2955              (set (car locals) nil)))
2956       (setq locals (cdr locals)))))
2957
2958 ;; Summary data functions.
2959
2960 (defmacro gnus-data-number (data)
2961   `(car ,data))
2962
2963 (defmacro gnus-data-set-number (data number)
2964   `(setcar ,data ,number))
2965
2966 (defmacro gnus-data-mark (data)
2967   `(nth 1 ,data))
2968
2969 (defmacro gnus-data-set-mark (data mark)
2970   `(setcar (nthcdr 1 ,data) ,mark))
2971
2972 (defmacro gnus-data-pos (data)
2973   `(nth 2 ,data))
2974
2975 (defmacro gnus-data-set-pos (data pos)
2976   `(setcar (nthcdr 2 ,data) ,pos))
2977
2978 (defmacro gnus-data-header (data)
2979   `(nth 3 ,data))
2980
2981 (defmacro gnus-data-set-header (data header)
2982   `(setf (nth 3 ,data) ,header))
2983
2984 (defmacro gnus-data-level (data)
2985   `(nth 4 ,data))
2986
2987 (defmacro gnus-data-unread-p (data)
2988   `(= (nth 1 ,data) gnus-unread-mark))
2989
2990 (defmacro gnus-data-read-p (data)
2991   `(/= (nth 1 ,data) gnus-unread-mark))
2992
2993 (defmacro gnus-data-pseudo-p (data)
2994   `(consp (nth 3 ,data)))
2995
2996 (defmacro gnus-data-find (number)
2997   `(assq ,number gnus-newsgroup-data))
2998
2999 (defmacro gnus-data-find-list (number &optional data)
3000   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3001      (memq (assq ,number bdata)
3002            bdata)))
3003
3004 (defmacro gnus-data-make (number mark pos header level)
3005   `(list ,number ,mark ,pos ,header ,level))
3006
3007 (defun gnus-data-enter (after-article number mark pos header level offset)
3008   (let ((data (gnus-data-find-list after-article)))
3009     (unless data
3010       (error "No such article: %d" after-article))
3011     (setcdr data (cons (gnus-data-make number mark pos header level)
3012                        (cdr data)))
3013     (setq gnus-newsgroup-data-reverse nil)
3014     (gnus-data-update-list (cddr data) offset)))
3015
3016 (defun gnus-data-enter-list (after-article list &optional offset)
3017   (when list
3018     (let ((data (and after-article (gnus-data-find-list after-article)))
3019           (ilist list))
3020       (if (not (or data
3021                    after-article))
3022           (let ((odata gnus-newsgroup-data))
3023             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3024             (when offset
3025               (gnus-data-update-list odata offset)))
3026         ;; Find the last element in the list to be spliced into the main
3027         ;; list.
3028         (setq list (last list))
3029         (if (not data)
3030             (progn
3031               (setcdr list gnus-newsgroup-data)
3032               (setq gnus-newsgroup-data ilist)
3033               (when offset
3034                 (gnus-data-update-list (cdr list) offset)))
3035           (setcdr list (cdr data))
3036           (setcdr data ilist)
3037           (when offset
3038             (gnus-data-update-list (cdr list) offset))))
3039       (setq gnus-newsgroup-data-reverse nil))))
3040
3041 (defun gnus-data-remove (article &optional offset)
3042   (let ((data gnus-newsgroup-data))
3043     (if (= (gnus-data-number (car data)) article)
3044         (progn
3045           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3046                 gnus-newsgroup-data-reverse nil)
3047           (when offset
3048             (gnus-data-update-list gnus-newsgroup-data offset)))
3049       (while (cdr data)
3050         (when (= (gnus-data-number (cadr data)) article)
3051           (setcdr data (cddr data))
3052           (when offset
3053             (gnus-data-update-list (cdr data) offset))
3054           (setq data nil
3055                 gnus-newsgroup-data-reverse nil))
3056         (setq data (cdr data))))))
3057
3058 (defmacro gnus-data-list (backward)
3059   `(if ,backward
3060        (or gnus-newsgroup-data-reverse
3061            (setq gnus-newsgroup-data-reverse
3062                  (reverse gnus-newsgroup-data)))
3063      gnus-newsgroup-data))
3064
3065 (defun gnus-data-update-list (data offset)
3066   "Add OFFSET to the POS of all data entries in DATA."
3067   (setq gnus-newsgroup-data-reverse nil)
3068   (while data
3069     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3070     (setq data (cdr data))))
3071
3072 (defun gnus-summary-article-pseudo-p (article)
3073   "Say whether this article is a pseudo article or not."
3074   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3075
3076 (defmacro gnus-summary-article-sparse-p (article)
3077   "Say whether this article is a sparse article or not."
3078   `(memq ,article gnus-newsgroup-sparse))
3079
3080 (defmacro gnus-summary-article-ancient-p (article)
3081   "Say whether this article is a sparse article or not."
3082   `(memq ,article gnus-newsgroup-ancient))
3083
3084 (defun gnus-article-parent-p (number)
3085   "Say whether this article is a parent or not."
3086   (let ((data (gnus-data-find-list number)))
3087     (and (cdr data)              ; There has to be an article after...
3088          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3089             (gnus-data-level (nth 1 data))))))
3090
3091 (defun gnus-article-children (number)
3092   "Return a list of all children to NUMBER."
3093   (let* ((data (gnus-data-find-list number))
3094          (level (gnus-data-level (car data)))
3095          children)
3096     (setq data (cdr data))
3097     (while (and data
3098                 (= (gnus-data-level (car data)) (1+ level)))
3099       (push (gnus-data-number (car data)) children)
3100       (setq data (cdr data)))
3101     children))
3102
3103 (defmacro gnus-summary-skip-intangible ()
3104   "If the current article is intangible, then jump to a different article."
3105   '(let ((to (get-text-property (point) 'gnus-intangible)))
3106      (and to (gnus-summary-goto-subject to))))
3107
3108 (defmacro gnus-summary-article-intangible-p ()
3109   "Say whether this article is intangible or not."
3110   '(get-text-property (point) 'gnus-intangible))
3111
3112 (defun gnus-article-read-p (article)
3113   "Say whether ARTICLE is read or not."
3114   (not (or (memq article gnus-newsgroup-marked)
3115            (memq article gnus-newsgroup-spam-marked)
3116            (memq article gnus-newsgroup-unreads)
3117            (memq article gnus-newsgroup-unselected)
3118            (memq article gnus-newsgroup-dormant))))
3119
3120 ;; Some summary mode macros.
3121
3122 (defmacro gnus-summary-article-number ()
3123   "The article number of the article on the current line.
3124 If there isn't an article number here, then we return the current
3125 article number."
3126   '(progn
3127      (gnus-summary-skip-intangible)
3128      (or (get-text-property (point) 'gnus-number)
3129          (gnus-summary-last-subject))))
3130
3131 (defmacro gnus-summary-article-header (&optional number)
3132   "Return the header of article NUMBER."
3133   `(gnus-data-header (gnus-data-find
3134                       ,(or number '(gnus-summary-article-number)))))
3135
3136 (defmacro gnus-summary-thread-level (&optional number)
3137   "Return the level of thread that starts with article NUMBER."
3138   `(if (and (eq gnus-summary-make-false-root 'dummy)
3139             (get-text-property (point) 'gnus-intangible))
3140        0
3141      (gnus-data-level (gnus-data-find
3142                        ,(or number '(gnus-summary-article-number))))))
3143
3144 (defmacro gnus-summary-article-mark (&optional number)
3145   "Return the mark of article NUMBER."
3146   `(gnus-data-mark (gnus-data-find
3147                     ,(or number '(gnus-summary-article-number)))))
3148
3149 (defmacro gnus-summary-article-pos (&optional number)
3150   "Return the position of the line of article NUMBER."
3151   `(gnus-data-pos (gnus-data-find
3152                    ,(or number '(gnus-summary-article-number)))))
3153
3154 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3155 (defmacro gnus-summary-article-subject (&optional number)
3156   "Return current subject string or nil if nothing."
3157   `(let ((headers
3158           ,(if number
3159                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3160              '(gnus-data-header (assq (gnus-summary-article-number)
3161                                       gnus-newsgroup-data)))))
3162      (and headers
3163           (vectorp headers)
3164           (mail-header-subject headers))))
3165
3166 (defmacro gnus-summary-article-score (&optional number)
3167   "Return current article score."
3168   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3169                   gnus-newsgroup-scored))
3170        gnus-summary-default-score 0))
3171
3172 (defun gnus-summary-article-children (&optional number)
3173   "Return a list of article numbers that are children of article NUMBER."
3174   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3175          (level (gnus-data-level (car data)))
3176          l children)
3177     (while (and (setq data (cdr data))
3178                 (> (setq l (gnus-data-level (car data))) level))
3179       (and (= (1+ level) l)
3180            (push (gnus-data-number (car data))
3181                  children)))
3182     (nreverse children)))
3183
3184 (defun gnus-summary-article-parent (&optional number)
3185   "Return the article number of the parent of article NUMBER."
3186   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3187                                     (gnus-data-list t)))
3188          (level (gnus-data-level (car data))))
3189     (if (zerop level)
3190         ()                              ; This is a root.
3191       ;; We search until we find an article with a level less than
3192       ;; this one.  That function has to be the parent.
3193       (while (and (setq data (cdr data))
3194                   (not (< (gnus-data-level (car data)) level))))
3195       (and data (gnus-data-number (car data))))))
3196
3197 (defun gnus-unread-mark-p (mark)
3198   "Say whether MARK is the unread mark."
3199   (= mark gnus-unread-mark))
3200
3201 (defun gnus-read-mark-p (mark)
3202   "Say whether MARK is one of the marks that mark as read.
3203 This is all marks except unread, ticked, dormant, and expirable."
3204   (not (or (= mark gnus-unread-mark)
3205            (= mark gnus-ticked-mark)
3206            (= mark gnus-spam-mark)
3207            (= mark gnus-dormant-mark)
3208            (= mark gnus-expirable-mark))))
3209
3210 (defmacro gnus-article-mark (number)
3211   "Return the MARK of article NUMBER.
3212 This macro should only be used when computing the mark the \"first\"
3213 time; i.e., when generating the summary lines.  After that,
3214 `gnus-summary-article-mark' should be used to examine the
3215 marks of articles."
3216   `(cond
3217     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3218     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3219     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3220     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3221     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3222     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3223     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3224     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3225            gnus-ancient-mark))))
3226
3227 ;; Saving hidden threads.
3228
3229 (defmacro gnus-save-hidden-threads (&rest forms)
3230   "Save hidden threads, eval FORMS, and restore the hidden threads."
3231   (let ((config (make-symbol "config")))
3232     `(let ((,config (gnus-hidden-threads-configuration)))
3233        (unwind-protect
3234            (save-excursion
3235              ,@forms)
3236          (gnus-restore-hidden-threads-configuration ,config)))))
3237 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3238 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3239
3240 (defun gnus-data-compute-positions ()
3241   "Compute the positions of all articles."
3242   (setq gnus-newsgroup-data-reverse nil)
3243   (let ((data gnus-newsgroup-data))
3244     (save-excursion
3245       (gnus-save-hidden-threads
3246         (gnus-summary-show-all-threads)
3247         (goto-char (point-min))
3248         (while data
3249           (while (get-text-property (point) 'gnus-intangible)
3250             (forward-line 1))
3251           (gnus-data-set-pos (car data) (+ (point) 3))
3252           (setq data (cdr data))
3253           (forward-line 1))))))
3254
3255 (defun gnus-hidden-threads-configuration ()
3256   "Return the current hidden threads configuration."
3257   (save-excursion
3258     (let (config)
3259       (goto-char (point-min))
3260       (while (search-forward "\r" nil t)
3261         (push (1- (point)) config))
3262       config)))
3263
3264 (defun gnus-restore-hidden-threads-configuration (config)
3265   "Restore hidden threads configuration from CONFIG."
3266   (save-excursion
3267     (let (point buffer-read-only)
3268       (while (setq point (pop config))
3269         (when (and (< point (point-max))
3270                    (goto-char point)
3271                    (eq (char-after) ?\n))
3272           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3273
3274 ;; Various summary mode internalish functions.
3275
3276 (defun gnus-mouse-pick-article (e)
3277   (interactive "e")
3278   (mouse-set-point e)
3279   (gnus-summary-next-page nil t))
3280
3281 (defun gnus-summary-set-display-table ()
3282   "Change the display table.
3283 Odd characters have a tendency to mess
3284 up nicely formatted displays - we make all possible glyphs
3285 display only a single character."
3286
3287   ;; We start from the standard display table, if any.
3288   (let ((table (or (copy-sequence standard-display-table)
3289                    (make-display-table)))
3290         (i 32))
3291     ;; Nix out all the control chars...
3292     (while (>= (setq i (1- i)) 0)
3293       (aset table i [??]))
3294    ;; ... but not newline and cr, of course.  (cr is necessary for the
3295     ;; selective display).
3296     (aset table ?\n nil)
3297     (aset table ?\r nil)
3298     ;; We keep TAB as well.
3299     (aset table ?\t nil)
3300     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3301     ;; Emacs 23 (unicode), that are not set already.
3302     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3303                  160
3304                256)))
3305       (while (>= (setq i (1- i)) 127)
3306         ;; Only modify if the entry is nil.
3307         (unless (aref table i)
3308           (aset table i [??]))))
3309     (setq buffer-display-table table)))
3310
3311 (defun gnus-summary-set-article-display-arrow (pos)
3312   "Update the overlay arrow to point to line at position POS."
3313   (when (and gnus-summary-display-arrow
3314              (boundp 'overlay-arrow-position)
3315              (boundp 'overlay-arrow-string))
3316     (save-excursion
3317       (goto-char pos)
3318       (beginning-of-line)
3319       (unless overlay-arrow-position
3320         (setq overlay-arrow-position (make-marker)))
3321       (setq overlay-arrow-string "=>"
3322             overlay-arrow-position (set-marker overlay-arrow-position
3323                                                (point)
3324                                                (current-buffer))))))
3325
3326 (defun gnus-summary-setup-buffer (group)
3327   "Initialize summary buffer."
3328   (let ((buffer (gnus-summary-buffer-name group))
3329         (dead-name (concat "*Dead Summary "
3330                            (gnus-group-decoded-name group) "*")))
3331     ;; If a dead summary buffer exists, we kill it.
3332     (when (gnus-buffer-live-p dead-name)
3333       (gnus-kill-buffer dead-name))
3334     (if (get-buffer buffer)
3335         (progn
3336           (set-buffer buffer)
3337           (setq gnus-summary-buffer (current-buffer))
3338           (not gnus-newsgroup-prepared))
3339       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3340       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3341       (gnus-summary-mode group)
3342       (when gnus-carpal
3343         (gnus-carpal-setup-buffer 'summary))
3344       (unless gnus-single-article-buffer
3345         (make-local-variable 'gnus-article-buffer)
3346         (make-local-variable 'gnus-article-current)
3347         (make-local-variable 'gnus-original-article-buffer))
3348       (setq gnus-newsgroup-name group)
3349       ;; Set any local variables in the group parameters.
3350       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3351       t)))
3352
3353 (defun gnus-set-global-variables ()
3354   "Set the global equivalents of the buffer-local variables.
3355 They are set to the latest values they had.  These reflect the summary
3356 buffer that was in action when the last article was fetched."
3357   (when (eq major-mode 'gnus-summary-mode)
3358     (setq gnus-summary-buffer (current-buffer))
3359     (let ((name gnus-newsgroup-name)
3360           (marked gnus-newsgroup-marked)
3361           (spam gnus-newsgroup-spam-marked)
3362           (unread gnus-newsgroup-unreads)
3363           (headers gnus-current-headers)
3364           (data gnus-newsgroup-data)
3365           (summary gnus-summary-buffer)
3366           (article-buffer gnus-article-buffer)
3367           (original gnus-original-article-buffer)
3368           (gac gnus-article-current)
3369           (reffed gnus-reffed-article-number)
3370           (score-file gnus-current-score-file)
3371           (default-charset gnus-newsgroup-charset)
3372           vlist)
3373       (let ((locals gnus-newsgroup-variables))
3374         (while locals
3375           (if (consp (car locals))
3376               (push (eval (caar locals)) vlist)
3377             (push (eval (car locals)) vlist))
3378           (setq locals (cdr locals)))
3379         (setq vlist (nreverse vlist)))
3380       (with-current-buffer gnus-group-buffer
3381         (setq gnus-newsgroup-name name
3382               gnus-newsgroup-marked marked
3383               gnus-newsgroup-spam-marked spam
3384               gnus-newsgroup-unreads unread
3385               gnus-current-headers headers
3386               gnus-newsgroup-data data
3387               gnus-article-current gac
3388               gnus-summary-buffer summary
3389               gnus-article-buffer article-buffer
3390               gnus-original-article-buffer original
3391               gnus-reffed-article-number reffed
3392               gnus-current-score-file score-file
3393               gnus-newsgroup-charset default-charset)
3394         (let ((locals gnus-newsgroup-variables))
3395           (while locals
3396             (if (consp (car locals))
3397                 (set (caar locals) (pop vlist))
3398               (set (car locals) (pop vlist)))
3399             (setq locals (cdr locals))))
3400         ;; The article buffer also has local variables.
3401         (when (gnus-buffer-live-p gnus-article-buffer)
3402           (set-buffer gnus-article-buffer)
3403           (setq gnus-summary-buffer summary))))))
3404
3405 (defun gnus-summary-article-unread-p (article)
3406   "Say whether ARTICLE is unread or not."
3407   (memq article gnus-newsgroup-unreads))
3408
3409 (defun gnus-summary-first-article-p (&optional article)
3410   "Return whether ARTICLE is the first article in the buffer."
3411   (if (not (setq article (or article (gnus-summary-article-number))))
3412       nil
3413     (eq article (caar gnus-newsgroup-data))))
3414
3415 (defun gnus-summary-last-article-p (&optional article)
3416   "Return whether ARTICLE is the last article in the buffer."
3417   (if (not (setq article (or article (gnus-summary-article-number))))
3418       ;; All non-existent numbers are the last article.  :-)
3419       t
3420     (not (cdr (gnus-data-find-list article)))))
3421
3422 (defun gnus-make-thread-indent-array ()
3423   (let ((n 200))
3424     (unless (and gnus-thread-indent-array
3425                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3426       (setq gnus-thread-indent-array (make-vector 201 "")
3427             gnus-thread-indent-array-level gnus-thread-indent-level)
3428       (while (>= n 0)
3429         (aset gnus-thread-indent-array n
3430               (make-string (* n gnus-thread-indent-level) ? ))
3431         (setq n (1- n))))))
3432
3433 (defun gnus-update-summary-mark-positions ()
3434   "Compute where the summary marks are to go."
3435   (save-excursion
3436     (when (gnus-buffer-exists-p gnus-summary-buffer)
3437       (set-buffer gnus-summary-buffer))
3438     (let ((spec gnus-summary-line-format-spec)
3439           pos)
3440       (save-excursion
3441         (gnus-set-work-buffer)
3442         (let ((gnus-tmp-unread ?Z)
3443               (gnus-replied-mark ?Z)
3444               (gnus-score-below-mark ?Z)
3445               (gnus-score-over-mark ?Z)
3446               (gnus-undownloaded-mark ?Z)
3447               (gnus-summary-line-format-spec spec)
3448               (gnus-newsgroup-downloadable '(0))
3449               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3450               case-fold-search ignores)
3451           ;; Here, all marks are bound to Z.
3452           (gnus-summary-insert-line header
3453                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3454           (goto-char (point-min))
3455           ;; Memorize the positions of the same characters as dummy marks.
3456           (while (re-search-forward "[A-D]" nil t)
3457             (push (point) ignores))
3458           (erase-buffer)
3459           ;; We use A-D as dummy marks in order to know column positions
3460           ;; where marks should be inserted.
3461           (setq gnus-tmp-unread ?A
3462                 gnus-replied-mark ?B
3463                 gnus-score-below-mark ?C
3464                 gnus-score-over-mark ?C
3465                 gnus-undownloaded-mark ?D)
3466           (gnus-summary-insert-line header
3467                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3468           ;; Ignore characters which aren't dummy marks.
3469           (dolist (p ignores)
3470             (delete-region (goto-char (1- p)) p)
3471             (insert ?Z))
3472           (goto-char (point-min))
3473           (setq pos (list (cons 'unread
3474                                 (and (search-forward "A" nil t)
3475                                      (- (point) (point-min) 1)))))
3476           (goto-char (point-min))
3477           (push (cons 'replied (and (search-forward "B" nil t)
3478                                     (- (point) (point-min) 1)))
3479                 pos)
3480           (goto-char (point-min))
3481           (push (cons 'score (and (search-forward "C" nil t)
3482                                   (- (point) (point-min) 1)))
3483                 pos)
3484           (goto-char (point-min))
3485           (push (cons 'download (and (search-forward "D" nil t)
3486                                      (- (point) (point-min) 1)))
3487                 pos)))
3488       (setq gnus-summary-mark-positions pos))))
3489
3490 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3491   "Insert a dummy root in the summary buffer."
3492   (beginning-of-line)
3493   (gnus-add-text-properties
3494    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3495    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3496
3497 (defun gnus-summary-extract-address-component (from)
3498   (or (car (funcall gnus-extract-address-components from))
3499       from))
3500
3501 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3502   (let ((mail-parse-charset gnus-newsgroup-charset)
3503         ; Is it really necessary to do this next part for each summary line?
3504         ; Luckily, doesn't seem to slow things down much.
3505         (mail-parse-ignored-charsets
3506          (with-current-buffer gnus-summary-buffer
3507            gnus-newsgroup-ignored-charsets)))
3508     (or
3509      (and gnus-ignored-from-addresses
3510           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3511           (let ((extra-headers (mail-header-extra header))
3512                 to
3513                 newsgroups)
3514             (cond
3515              ((setq to (cdr (assq 'To extra-headers)))
3516               (concat gnus-summary-to-prefix
3517                       (inline
3518                         (gnus-summary-extract-address-component
3519                          (funcall gnus-decode-encoded-word-function to)))))
3520              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3521               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3522      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3523
3524 (defun gnus-summary-insert-line (gnus-tmp-header
3525                                  gnus-tmp-level gnus-tmp-current
3526                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3527                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3528                                  &optional gnus-tmp-dummy gnus-tmp-score
3529                                  gnus-tmp-process)
3530   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3531          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3532          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3533          (gnus-tmp-score-char
3534           (if (or (null gnus-summary-default-score)
3535                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3536                       gnus-summary-zcore-fuzz))
3537               ?                         ;Whitespace
3538             (if (< gnus-tmp-score gnus-summary-default-score)
3539                 gnus-score-below-mark gnus-score-over-mark)))
3540          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3541          (gnus-tmp-replied
3542           (cond (gnus-tmp-process gnus-process-mark)
3543                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3544                  gnus-cached-mark)
3545                 (gnus-tmp-replied gnus-replied-mark)
3546                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3547                  gnus-forwarded-mark)
3548                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3549                  gnus-saved-mark)
3550                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3551                  gnus-recent-mark)
3552                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3553                  gnus-unseen-mark)
3554                 (t gnus-no-mark)))
3555          (gnus-tmp-downloaded
3556           (cond (undownloaded
3557                  gnus-undownloaded-mark)
3558                 (gnus-newsgroup-agentized
3559                  gnus-downloaded-mark)
3560                 (t
3561                  gnus-no-mark)))
3562          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3563          (gnus-tmp-name
3564           (cond
3565            ((string-match "<[^>]+> *$" gnus-tmp-from)
3566             (let ((beg (match-beginning 0)))
3567               (or (and (string-match "^\".+\"" gnus-tmp-from)
3568                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3569                   (substring gnus-tmp-from 0 beg))))
3570            ((string-match "(.+)" gnus-tmp-from)
3571             (substring gnus-tmp-from
3572                        (1+ (match-beginning 0)) (1- (match-end 0))))
3573            (t gnus-tmp-from)))
3574          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3575          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3576          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3577          (buffer-read-only nil))
3578     (when (string= gnus-tmp-name "")
3579       (setq gnus-tmp-name gnus-tmp-from))
3580     (unless (numberp gnus-tmp-lines)
3581       (setq gnus-tmp-lines -1))
3582     (if (= gnus-tmp-lines -1)
3583         (setq gnus-tmp-lines "?")
3584       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3585       (gnus-put-text-property
3586      (point)
3587      (progn (eval gnus-summary-line-format-spec) (point))
3588        'gnus-number gnus-tmp-number)
3589     (when (gnus-visual-p 'summary-highlight 'highlight)
3590       (forward-line -1)
3591       (gnus-run-hooks 'gnus-summary-update-hook)
3592       (forward-line 1))))
3593
3594 (defun gnus-summary-update-line (&optional dont-update)
3595   "Update summary line after change."
3596   (when (and gnus-summary-default-score
3597              (not gnus-summary-inhibit-highlight))
3598     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3599            (article (gnus-summary-article-number))
3600            (score (gnus-summary-article-score article)))
3601       (unless dont-update
3602         (if (and gnus-summary-mark-below
3603                  (< (gnus-summary-article-score)
3604                     gnus-summary-mark-below))
3605             ;; This article has a low score, so we mark it as read.
3606             (when (memq article gnus-newsgroup-unreads)
3607               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3608           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3609             ;; This article was previously marked as read on account
3610             ;; of a low score, but now it has risen, so we mark it as
3611             ;; unread.
3612             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3613         (gnus-summary-update-mark
3614          (if (or (null gnus-summary-default-score)
3615                  (<= (abs (- score gnus-summary-default-score))
3616                      gnus-summary-zcore-fuzz))
3617              ?                          ;Whitespace
3618            (if (< score gnus-summary-default-score)
3619                gnus-score-below-mark gnus-score-over-mark))
3620          'score))
3621       ;; Do visual highlighting.
3622       (when (gnus-visual-p 'summary-highlight 'highlight)
3623         (gnus-run-hooks 'gnus-summary-update-hook)))))
3624
3625 (defvar gnus-tmp-new-adopts nil)
3626
3627 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3628   "Return the number of articles in THREAD.
3629 This may be 0 in some cases -- if none of the articles in
3630 the thread are to be displayed."
3631   (let* ((number
3632          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3633           (cond
3634            ((not (listp thread))
3635             1)
3636            ((and (consp thread) (cdr thread))
3637             (apply
3638              '+ 1 (mapcar
3639                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3640            ((null thread)
3641             1)
3642            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3643             1)
3644            (t 0))))
3645     (when (and level (zerop level) gnus-tmp-new-adopts)
3646       (incf number
3647             (apply '+ (mapcar
3648                        'gnus-summary-number-of-articles-in-thread
3649                        gnus-tmp-new-adopts))))
3650     (if char
3651         (if (> number 1) gnus-not-empty-thread-mark
3652           gnus-empty-thread-mark)
3653       number)))
3654
3655 (defsubst gnus-summary-line-message-size (head)
3656   "Return pretty-printed version of message size.
3657 This function is intended to be used in
3658 `gnus-summary-line-format-alist'."
3659   (let ((c (or (mail-header-chars head) -1)))
3660     (cond ((< c 0) "n/a")               ; chars not available
3661           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3662           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3663           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3664           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3665
3666
3667 (defun gnus-summary-set-local-parameters (group)
3668   "Go through the local params of GROUP and set all variable specs in that list."
3669   (let ((vars '(quit-config)))          ; Ignore quit-config.
3670     (dolist (elem (gnus-group-find-parameter group))
3671       (and (consp elem)                 ; Has to be a cons.
3672            (consp (cdr elem))           ; The cdr has to be a list.
3673            (symbolp (car elem))         ; Has to be a symbol in there.
3674            (not (memq (car elem) vars))
3675            (ignore-errors               ; So we set it.
3676              (push (car elem) vars)
3677              (make-local-variable (car elem))
3678              (set (car elem) (eval (nth 1 elem))))))))
3679
3680 (defun gnus-summary-read-group (group &optional show-all no-article
3681                                       kill-buffer no-display backward
3682                                       select-articles)
3683   "Start reading news in newsgroup GROUP.
3684 If SHOW-ALL is non-nil, already read articles are also listed.
3685 If NO-ARTICLE is non-nil, no article is selected initially.
3686 If NO-DISPLAY, don't generate a summary buffer."
3687   (let (result)
3688     (while (and group
3689                 (null (setq result
3690                             (let ((gnus-auto-select-next nil))
3691                               (or (gnus-summary-read-group-1
3692                                    group show-all no-article
3693                                    kill-buffer no-display
3694                                    select-articles)
3695                                   (setq show-all nil
3696                                         select-articles nil)))))
3697                 (eq gnus-auto-select-next 'quietly))
3698       (set-buffer gnus-group-buffer)
3699       ;; The entry function called above goes to the next
3700       ;; group automatically, so we go two groups back
3701       ;; if we are searching for the previous group.
3702       (when backward
3703         (gnus-group-prev-unread-group 2))
3704       (if (not (equal group (gnus-group-group-name)))
3705           (setq group (gnus-group-group-name))
3706         (setq group nil)))
3707     result))
3708
3709 (defun gnus-summary-read-group-1 (group show-all no-article
3710                                         kill-buffer no-display
3711                                         &optional select-articles)
3712   ;; Killed foreign groups can't be entered.
3713   ;;  (when (and (not (gnus-group-native-p group))
3714   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3715   ;;    (error "Dead non-native groups can't be entered"))
3716   (gnus-message 5 "Retrieving newsgroup: %s..."
3717                 (gnus-group-decoded-name group))
3718   (let* ((new-group (gnus-summary-setup-buffer group))
3719          (quit-config (gnus-group-quit-config group))
3720          (did-select (and new-group (gnus-select-newsgroup
3721                                      group show-all select-articles))))
3722     (cond
3723      ;; This summary buffer exists already, so we just select it.
3724      ((not new-group)
3725       (gnus-set-global-variables)
3726       (when kill-buffer
3727         (gnus-kill-or-deaden-summary kill-buffer))
3728       (gnus-configure-windows 'summary 'force)
3729       (gnus-set-mode-line 'summary)
3730       (gnus-summary-position-point)
3731       (message "")
3732       t)
3733      ;; We couldn't select this group.
3734      ((null did-select)
3735       (when (and (eq major-mode 'gnus-summary-mode)
3736                  (not (equal (current-buffer) kill-buffer)))
3737         (kill-buffer (current-buffer))
3738         (if (not quit-config)
3739             (progn
3740               ;; Update the info -- marks might need to be removed,
3741               ;; for instance.
3742               (gnus-summary-update-info)
3743               (set-buffer gnus-group-buffer)
3744               (gnus-group-jump-to-group group)
3745               (gnus-group-next-unread-group 1))
3746           (gnus-handle-ephemeral-exit quit-config)))
3747       (let ((grpinfo (gnus-get-info group)))
3748         (if (null (gnus-info-read grpinfo))
3749             (gnus-message 3 "Group %s contains no messages"
3750                           (gnus-group-decoded-name group))
3751           (gnus-message 3 "Can't select group")))
3752       nil)
3753      ;; The user did a `C-g' while prompting for number of articles,
3754      ;; so we exit this group.
3755      ((eq did-select 'quit)
3756       (and (eq major-mode 'gnus-summary-mode)
3757            (not (equal (current-buffer) kill-buffer))
3758            (kill-buffer (current-buffer)))
3759       (when kill-buffer
3760         (gnus-kill-or-deaden-summary kill-buffer))
3761       (if (not quit-config)
3762           (progn
3763             (set-buffer gnus-group-buffer)
3764             (gnus-group-jump-to-group group)
3765             (gnus-group-next-unread-group 1)
3766             (gnus-configure-windows 'group 'force))
3767         (gnus-handle-ephemeral-exit quit-config))
3768       ;; Finally signal the quit.
3769       (signal 'quit nil))
3770      ;; The group was successfully selected.
3771      (t
3772       (gnus-set-global-variables)
3773       ;; Save the active value in effect when the group was entered.
3774       (setq gnus-newsgroup-active
3775             (gnus-copy-sequence
3776              (gnus-active gnus-newsgroup-name)))
3777       ;; You can change the summary buffer in some way with this hook.
3778       (gnus-run-hooks 'gnus-select-group-hook)
3779       (when (memq 'summary (gnus-update-format-specifications
3780                             nil 'summary 'summary-mode 'summary-dummy))
3781         ;; The format specification for the summary line was updated,
3782         ;; so we need to update the mark positions as well.
3783         (gnus-update-summary-mark-positions))
3784       ;; Do score processing.
3785       (when gnus-use-scoring
3786         (gnus-possibly-score-headers))
3787       ;; Check whether to fill in the gaps in the threads.
3788       (when gnus-build-sparse-threads
3789         (gnus-build-sparse-threads))
3790       ;; Find the initial limit.
3791       (if show-all
3792           (let ((gnus-newsgroup-dormant nil))
3793             (gnus-summary-initial-limit show-all))
3794         (gnus-summary-initial-limit show-all))
3795       ;; Generate the summary buffer.
3796       (unless no-display
3797         (gnus-summary-prepare))
3798       (when gnus-use-trees
3799         (gnus-tree-open group)
3800         (setq gnus-summary-highlight-line-function
3801               'gnus-tree-highlight-article))
3802       ;; If the summary buffer is empty, but there are some low-scored
3803       ;; articles or some excluded dormants, we include these in the
3804       ;; buffer.
3805       (when (and (zerop (buffer-size))
3806                  (not no-display))
3807         (cond (gnus-newsgroup-dormant
3808                (gnus-summary-limit-include-dormant))
3809               ((and gnus-newsgroup-scored show-all)
3810                (gnus-summary-limit-include-expunged t))))
3811       ;; Function `gnus-apply-kill-file' must be called in this hook.
3812       (gnus-run-hooks 'gnus-apply-kill-hook)
3813       (if (and (zerop (buffer-size))
3814                (not no-display))
3815           (progn
3816             ;; This newsgroup is empty.
3817             (gnus-summary-catchup-and-exit nil t)
3818             (gnus-message 6 "No unread news")
3819             (when kill-buffer
3820               (gnus-kill-or-deaden-summary kill-buffer))
3821             ;; Return nil from this function.
3822             nil)
3823         ;; Hide conversation thread subtrees.  We cannot do this in
3824         ;; gnus-summary-prepare-hook since kill processing may not
3825         ;; work with hidden articles.
3826         (gnus-summary-maybe-hide-threads)
3827         (when kill-buffer
3828           (gnus-kill-or-deaden-summary kill-buffer))
3829         (gnus-summary-auto-select-subject)
3830         ;; Show first unread article if requested.
3831         (if (and (not no-article)
3832                  (not no-display)
3833                  gnus-newsgroup-unreads
3834                  gnus-auto-select-first)
3835             (progn
3836               (gnus-configure-windows 'summary)
3837               (let ((art (gnus-summary-article-number)))
3838                 (unless (and (not gnus-plugged)
3839                              (or (memq art gnus-newsgroup-undownloaded)
3840                                  (memq art gnus-newsgroup-downloadable)))
3841                   (gnus-summary-goto-article art))))
3842           ;; Don't select any articles.
3843           (gnus-summary-position-point)
3844           (gnus-configure-windows 'summary 'force)
3845           (gnus-set-mode-line 'summary))
3846         (when (and gnus-auto-center-group
3847                    (get-buffer-window gnus-group-buffer t))
3848           ;; Gotta use windows, because recenter does weird stuff if
3849           ;; the current buffer ain't the displayed window.
3850           (let ((owin (selected-window)))
3851             (select-window (get-buffer-window gnus-group-buffer t))
3852             (when (gnus-group-goto-group group)
3853               (recenter))
3854             (select-window owin)))
3855         ;; Mark this buffer as "prepared".
3856         (setq gnus-newsgroup-prepared t)
3857         (gnus-run-hooks 'gnus-summary-prepared-hook)
3858         (unless (gnus-ephemeral-group-p group)
3859           (gnus-group-update-group group))
3860         t)))))
3861
3862 (defun gnus-summary-auto-select-subject ()
3863   "Select the subject line on initial group entry."
3864   (goto-char (point-min))
3865   (cond
3866    ((eq gnus-auto-select-subject 'best)
3867     (gnus-summary-best-unread-subject))
3868    ((eq gnus-auto-select-subject 'unread)
3869     (gnus-summary-first-unread-subject))
3870    ((eq gnus-auto-select-subject 'unseen)
3871     (gnus-summary-first-unseen-subject))
3872    ((eq gnus-auto-select-subject 'unseen-or-unread)
3873     (gnus-summary-first-unseen-or-unread-subject))
3874    ((eq gnus-auto-select-subject 'first)
3875     ;; Do nothing.
3876     )
3877    ((functionp gnus-auto-select-subject)
3878     (funcall gnus-auto-select-subject))))
3879
3880 (defun gnus-summary-prepare ()
3881   "Generate the summary buffer."
3882   (interactive)
3883   (let ((buffer-read-only nil))
3884     (erase-buffer)
3885     (setq gnus-newsgroup-data nil
3886           gnus-newsgroup-data-reverse nil)
3887     (gnus-run-hooks 'gnus-summary-generate-hook)
3888     ;; Generate the buffer, either with threads or without.
3889     (when gnus-newsgroup-headers
3890       (gnus-summary-prepare-threads
3891        (if gnus-show-threads
3892            (gnus-sort-gathered-threads
3893             (funcall gnus-summary-thread-gathering-function
3894                      (gnus-sort-threads
3895                       (gnus-cut-threads (gnus-make-threads)))))
3896          ;; Unthreaded display.
3897          (gnus-sort-articles gnus-newsgroup-headers))))
3898     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3899     ;; Call hooks for modifying summary buffer.
3900     (goto-char (point-min))
3901     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3902
3903 (defsubst gnus-general-simplify-subject (subject)
3904   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3905   (setq subject
3906         (cond
3907          ;; Truncate the subject.
3908          (gnus-simplify-subject-functions
3909           (gnus-map-function gnus-simplify-subject-functions subject))
3910          ((numberp gnus-summary-gather-subject-limit)
3911           (setq subject (gnus-simplify-subject-re subject))
3912           (if (> (length subject) gnus-summary-gather-subject-limit)
3913               (substring subject 0 gnus-summary-gather-subject-limit)
3914             subject))
3915          ;; Fuzzily simplify it.
3916          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3917           (gnus-simplify-subject-fuzzy subject))
3918          ;; Just remove the leading "Re:".
3919          (t
3920           (gnus-simplify-subject-re subject))))
3921
3922   (if (and gnus-summary-gather-exclude-subject
3923            (string-match gnus-summary-gather-exclude-subject subject))
3924       nil                         ; This article shouldn't be gathered
3925     subject))
3926
3927 (defun gnus-summary-simplify-subject-query ()
3928   "Query where the respool algorithm would put this article."
3929   (interactive)
3930   (gnus-summary-select-article)
3931   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3932
3933 (defun gnus-gather-threads-by-subject (threads)
3934   "Gather threads by looking at Subject headers."
3935   (if (not gnus-summary-make-false-root)
3936       threads
3937     (let ((hashtb (gnus-make-hashtable 1024))
3938           (prev threads)
3939           (result threads)
3940           subject hthread whole-subject)
3941       (while threads
3942         (setq subject (gnus-general-simplify-subject
3943                        (setq whole-subject (mail-header-subject
3944                                             (caar threads)))))
3945         (when subject
3946           (if (setq hthread (gnus-gethash subject hashtb))
3947               (progn
3948                 ;; We enter a dummy root into the thread, if we
3949                 ;; haven't done that already.
3950                 (unless (stringp (caar hthread))
3951                   (setcar hthread (list whole-subject (car hthread))))
3952                 ;; We add this new gathered thread to this gathered
3953                 ;; thread.
3954                 (setcdr (car hthread)
3955                         (nconc (cdar hthread) (list (car threads))))
3956                 ;; Remove it from the list of threads.
3957                 (setcdr prev (cdr threads))
3958                 (setq threads prev))
3959             ;; Enter this thread into the hash table.
3960             (gnus-sethash subject
3961                           (if gnus-summary-make-false-root-always
3962                               (progn
3963                                 ;; If you want a dummy root above all
3964                                 ;; threads...
3965                                 (setcar threads (list whole-subject
3966                                                       (car threads)))
3967                                 threads)
3968                             threads)
3969                           hashtb)))
3970         (setq prev threads)
3971         (setq threads (cdr threads)))
3972       result)))
3973
3974 (defun gnus-gather-threads-by-references (threads)
3975   "Gather threads by looking at References headers."
3976   (let ((idhashtb (gnus-make-hashtable 1024))
3977         (thhashtb (gnus-make-hashtable 1024))
3978         (prev threads)
3979         (result threads)
3980         ids references id gthread gid entered ref)
3981     (while threads
3982       (when (setq references (mail-header-references (caar threads)))
3983         (setq id (mail-header-id (caar threads))
3984               ids (inline (gnus-split-references references))
3985               entered nil)
3986         (while (setq ref (pop ids))
3987           (setq ids (delete ref ids))
3988           (if (not (setq gid (gnus-gethash ref idhashtb)))
3989               (progn
3990                 (gnus-sethash ref id idhashtb)
3991                 (gnus-sethash id threads thhashtb))
3992             (setq gthread (gnus-gethash gid thhashtb))
3993             (unless entered
3994               ;; We enter a dummy root into the thread, if we
3995               ;; haven't done that already.
3996               (unless (stringp (caar gthread))
3997                 (setcar gthread (list (mail-header-subject (caar gthread))
3998                                       (car gthread))))
3999               ;; We add this new gathered thread to this gathered
4000               ;; thread.
4001               (setcdr (car gthread)
4002                       (nconc (cdar gthread) (list (car threads)))))
4003             ;; Add it into the thread hash table.
4004             (gnus-sethash id gthread thhashtb)
4005             (setq entered t)
4006             ;; Remove it from the list of threads.
4007             (setcdr prev (cdr threads))
4008             (setq threads prev))))
4009       (setq prev threads)
4010       (setq threads (cdr threads)))
4011     result))
4012
4013 (defun gnus-sort-gathered-threads (threads)
4014   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4015   (let ((result threads))
4016     (while threads
4017       (when (stringp (caar threads))
4018         (setcdr (car threads)
4019                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4020       (setq threads (cdr threads)))
4021     result))
4022
4023 (defun gnus-thread-loop-p (root thread)
4024   "Say whether ROOT is in THREAD."
4025   (let ((stack (list thread))
4026         (infloop 0)
4027         th)
4028     (while (setq thread (pop stack))
4029       (setq th (cdr thread))
4030       (while (and th
4031                   (not (eq (caar th) root)))
4032         (pop th))
4033       (if th
4034           ;; We have found a loop.
4035           (let (ref-dep)
4036             (setcdr thread (delq (car th) (cdr thread)))
4037             (if (boundp (setq ref-dep (intern "none"
4038                                               gnus-newsgroup-dependencies)))
4039                 (setcdr (symbol-value ref-dep)
4040                         (nconc (cdr (symbol-value ref-dep))
4041                                (list (car th))))
4042               (set ref-dep (list nil (car th))))
4043             (setq infloop 1
4044                   stack nil))
4045         ;; Push all the subthreads onto the stack.
4046         (push (cdr thread) stack)))
4047     infloop))
4048
4049 (defun gnus-make-threads ()
4050   "Go through the dependency hashtb and find the roots.  Return all threads."
4051   (let (threads)
4052     (while (catch 'infloop
4053              (mapatoms
4054               (lambda (refs)
4055                 ;; Deal with self-referencing References loops.
4056                 (when (and (car (symbol-value refs))
4057                            (not (zerop
4058                                  (apply
4059                                   '+
4060                                   (mapcar
4061                                    (lambda (thread)
4062                                      (gnus-thread-loop-p
4063                                       (car (symbol-value refs)) thread))
4064                                    (cdr (symbol-value refs)))))))
4065                   (setq threads nil)
4066                   (throw 'infloop t))
4067                 (unless (car (symbol-value refs))
4068                   ;; These threads do not refer back to any other
4069                   ;; articles, so they're roots.
4070                   (setq threads (append (cdr (symbol-value refs)) threads))))
4071               gnus-newsgroup-dependencies)))
4072     threads))
4073
4074 ;; Build the thread tree.
4075 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4076   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4077
4078 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4079 if it was already present.
4080
4081 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4082 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4083 Message-IDs will be renamed to a unique Message-ID before being
4084 entered.
4085
4086 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4087   (let* ((id (mail-header-id header))
4088          (id-dep (and id (intern id dependencies)))
4089          parent-id ref ref-dep ref-header replaced)
4090     ;; Enter this `header' in the `dependencies' table.
4091     (cond
4092      ((not id-dep)
4093       (setq header nil))
4094      ;; The first two cases do the normal part: enter a new `header'
4095      ;; in the `dependencies' table.
4096      ((not (boundp id-dep))
4097       (set id-dep (list header)))
4098      ((null (car (symbol-value id-dep)))
4099       (setcar (symbol-value id-dep) header))
4100
4101      ;; From here the `header' was already present in the
4102      ;; `dependencies' table.
4103      (force-new
4104       ;; Overrides an existing entry;
4105       ;; just set the header part of the entry.
4106       (setcar (symbol-value id-dep) header)
4107       (setq replaced t))
4108
4109      ;; Renames the existing `header' to a unique Message-ID.
4110      ((not gnus-summary-ignore-duplicates)
4111       ;; An article with this Message-ID has already been seen.
4112       ;; We rename the Message-ID.
4113       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4114            (list header))
4115       (mail-header-set-id header id))
4116
4117      ;; The last case ignores an existing entry, except it adds any
4118      ;; additional Xrefs (in case the two articles came from different
4119      ;; servers.
4120      ;; Also sets `header' to `nil' meaning that the `dependencies'
4121      ;; table was *not* modified.
4122      (t
4123       (mail-header-set-xref
4124        (car (symbol-value id-dep))
4125        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4126                    "")
4127                (or (mail-header-xref header) "")))
4128       (setq header nil)))
4129
4130     (when (and header (not replaced))
4131       ;; First check that we are not creating a References loop.
4132       (setq parent-id (gnus-parent-id (mail-header-references header)))
4133       (setq ref parent-id)
4134       (while (and ref
4135                   (setq ref-dep (intern-soft ref dependencies))
4136                   (boundp ref-dep)
4137                   (setq ref-header (car (symbol-value ref-dep))))
4138         (if (string= id ref)
4139             ;; Yuk!  This is a reference loop.  Make the article be a
4140             ;; root article.
4141             (progn
4142               (mail-header-set-references (car (symbol-value id-dep)) "none")
4143               (setq ref nil)
4144               (setq parent-id nil))
4145           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4146       (setq ref-dep (intern (or parent-id "none") dependencies))
4147       (if (boundp ref-dep)
4148           (setcdr (symbol-value ref-dep)
4149                   (nconc (cdr (symbol-value ref-dep))
4150                          (list (symbol-value id-dep))))
4151         (set ref-dep (list nil (symbol-value id-dep)))))
4152     header))
4153
4154 (defun gnus-extract-message-id-from-in-reply-to (string)
4155   (if (string-match "<[^>]+>" string)
4156       (substring string (match-beginning 0) (match-end 0))
4157     nil))
4158
4159 (defun gnus-build-sparse-threads ()
4160   (let ((headers gnus-newsgroup-headers)
4161         (mail-parse-charset gnus-newsgroup-charset)
4162         (gnus-summary-ignore-duplicates t)
4163         header references generation relations
4164         subject child end new-child date)
4165     ;; First we create an alist of generations/relations, where
4166     ;; generations is how much we trust the relation, and the relation
4167     ;; is parent/child.
4168     (gnus-message 7 "Making sparse threads...")
4169     (save-excursion
4170       (nnheader-set-temp-buffer " *gnus sparse threads*")
4171       (while (setq header (pop headers))
4172         (when (and (setq references (mail-header-references header))
4173                    (not (string= references "")))
4174           (insert references)
4175           (setq child (mail-header-id header)
4176                 subject (mail-header-subject header)
4177                 date (mail-header-date header)
4178                 generation 0)
4179           (while (search-backward ">" nil t)
4180             (setq end (1+ (point)))
4181             (when (search-backward "<" nil t)
4182               (setq new-child (buffer-substring (point) end))
4183               (push (list (incf generation)
4184                           child (setq child new-child)
4185                           subject date)
4186                     relations)))
4187           (when child
4188             (push (list (1+ generation) child nil subject) relations))
4189           (erase-buffer)))
4190       (kill-buffer (current-buffer)))
4191     ;; Sort over trustworthiness.
4192     (mapcar
4193      (lambda (relation)
4194        (when (gnus-dependencies-add-header
4195               (make-full-mail-header
4196                gnus-reffed-article-number
4197                (nth 3 relation) "" (or (nth 4 relation) "")
4198                (nth 1 relation)
4199                (or (nth 2 relation) "") 0 0 "")
4200               gnus-newsgroup-dependencies nil)
4201          (push gnus-reffed-article-number gnus-newsgroup-limit)
4202          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4203          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4204                gnus-newsgroup-reads)
4205          (decf gnus-reffed-article-number)))
4206      (sort relations 'car-less-than-car))
4207     (gnus-message 7 "Making sparse threads...done")))
4208
4209 (defun gnus-build-old-threads ()
4210   ;; Look at all the articles that refer back to old articles, and
4211   ;; fetch the headers for the articles that aren't there.  This will
4212   ;; build complete threads - if the roots haven't been expired by the
4213   ;; server, that is.
4214   (let ((mail-parse-charset gnus-newsgroup-charset)
4215         id heads)
4216     (mapatoms
4217      (lambda (refs)
4218        (when (not (car (symbol-value refs)))
4219          (setq heads (cdr (symbol-value refs)))
4220          (while heads
4221            (if (memq (mail-header-number (caar heads))
4222                      gnus-newsgroup-dormant)
4223                (setq heads (cdr heads))
4224              (setq id (symbol-name refs))
4225              (while (and (setq id (gnus-build-get-header id))
4226                          (not (car (gnus-id-to-thread id)))))
4227              (setq heads nil)))))
4228      gnus-newsgroup-dependencies)))
4229
4230 (defsubst gnus-remove-odd-characters (string)
4231   "Translate STRING into something that doesn't contain weird characters."
4232   (mm-subst-char-in-string
4233    ?\r ?\-
4234    (mm-subst-char-in-string ?\n ?\- string t) t))
4235
4236 ;; This function has to be called with point after the article number
4237 ;; on the beginning of the line.
4238 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4239   (let ((eol (point-at-eol))
4240         (buffer (current-buffer))
4241         header references in-reply-to)
4242
4243     ;; overview: [num subject from date id refs chars lines misc]
4244     (unwind-protect
4245         (let (x)
4246           (narrow-to-region (point) eol)
4247           (unless (eobp)
4248             (forward-char))
4249
4250           (setq header
4251                 (make-full-mail-header
4252                  number                 ; number
4253                  (condition-case ()     ; subject
4254                      (gnus-remove-odd-characters
4255                       (funcall gnus-decode-encoded-word-function
4256                                (setq x (nnheader-nov-field))))
4257                    (error x))
4258                  (condition-case ()     ; from
4259                      (gnus-remove-odd-characters
4260                       (funcall gnus-decode-encoded-word-function
4261                                (setq x (nnheader-nov-field))))
4262                    (error x))
4263                  (nnheader-nov-field)   ; date
4264                  (nnheader-nov-read-message-id number)  ; id
4265                  (setq references (nnheader-nov-field)) ; refs
4266                  (nnheader-nov-read-integer) ; chars
4267                  (nnheader-nov-read-integer) ; lines
4268                  (unless (eobp)
4269                    (if (looking-at "Xref: ")
4270                        (goto-char (match-end 0)))
4271                    (nnheader-nov-field)) ; Xref
4272                  (nnheader-nov-parse-extra)))) ; extra
4273
4274       (widen))
4275
4276     (when (and (string= references "")
4277                (setq in-reply-to (mail-header-extra header))
4278                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4279       (mail-header-set-references
4280        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4281
4282     (when gnus-alter-header-function
4283       (funcall gnus-alter-header-function header))
4284     (gnus-dependencies-add-header header dependencies force-new)))
4285
4286 (defun gnus-build-get-header (id)
4287   "Look through the buffer of NOV lines and find the header to ID.
4288 Enter this line into the dependencies hash table, and return
4289 the id of the parent article (if any)."
4290   (let ((deps gnus-newsgroup-dependencies)
4291         found header)
4292     (prog1
4293         (save-excursion
4294           (set-buffer nntp-server-buffer)
4295           (let ((case-fold-search nil))
4296             (goto-char (point-min))
4297             (while (and (not found)
4298                         (search-forward id nil t))
4299               (beginning-of-line)
4300               (setq found (looking-at
4301                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4302                                    (regexp-quote id))))
4303               (or found (beginning-of-line 2)))
4304             (when found
4305               (beginning-of-line)
4306               (and
4307                (setq header (gnus-nov-parse-line
4308                              (read (current-buffer)) deps))
4309                (gnus-parent-id (mail-header-references header))))))
4310       (when header
4311         (let ((number (mail-header-number header)))
4312           (push number gnus-newsgroup-limit)
4313           (push header gnus-newsgroup-headers)
4314           (if (memq number gnus-newsgroup-unselected)
4315               (progn
4316                 (setq gnus-newsgroup-unreads
4317                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4318                                                number))
4319                 (setq gnus-newsgroup-unselected
4320                       (delq number gnus-newsgroup-unselected)))
4321             (push number gnus-newsgroup-ancient)))))))
4322
4323 (defun gnus-build-all-threads ()
4324   "Read all the headers."
4325   (let ((gnus-summary-ignore-duplicates t)
4326         (mail-parse-charset gnus-newsgroup-charset)
4327         (dependencies gnus-newsgroup-dependencies)
4328         header article)
4329     (save-excursion
4330       (set-buffer nntp-server-buffer)
4331       (let ((case-fold-search nil))
4332         (goto-char (point-min))
4333         (while (not (eobp))
4334           (ignore-errors
4335             (setq article (read (current-buffer))
4336                   header (gnus-nov-parse-line article dependencies)))
4337           (when header
4338             (with-current-buffer gnus-summary-buffer
4339               (push header gnus-newsgroup-headers)
4340               (if (memq (setq article (mail-header-number header))
4341                         gnus-newsgroup-unselected)
4342                   (progn
4343                     (setq gnus-newsgroup-unreads
4344                           (gnus-add-to-sorted-list
4345                            gnus-newsgroup-unreads article))
4346                     (setq gnus-newsgroup-unselected
4347                           (delq article gnus-newsgroup-unselected)))
4348                 (push article gnus-newsgroup-ancient)))
4349             (forward-line 1)))))))
4350
4351 (defun gnus-summary-update-article-line (article header)
4352   "Update the line for ARTICLE using HEADER."
4353   (let* ((id (mail-header-id header))
4354          (thread (gnus-id-to-thread id)))
4355     (unless thread
4356       (error "Article in no thread"))
4357     ;; Update the thread.
4358     (setcar thread header)
4359     (gnus-summary-goto-subject article)
4360     (let* ((datal (gnus-data-find-list article))
4361            (data (car datal))
4362            (buffer-read-only nil)
4363            (level (gnus-summary-thread-level)))
4364       (gnus-delete-line)
4365       (let ((inserted (- (point)
4366                          (progn
4367                            (gnus-summary-insert-line
4368                             header level nil
4369                             (memq article gnus-newsgroup-undownloaded)
4370                             (gnus-article-mark article)
4371                             (memq article gnus-newsgroup-replied)
4372                             (memq article gnus-newsgroup-expirable)
4373                             ;; Only insert the Subject string when it's different
4374                             ;; from the previous Subject string.
4375                             (if (and
4376                                  gnus-show-threads
4377                                  (gnus-subject-equal
4378                                   (condition-case ()
4379                                       (mail-header-subject
4380                                        (gnus-data-header
4381                                         (cadr
4382                                          (gnus-data-find-list
4383                                           article
4384                                           (gnus-data-list t)))))
4385                                     ;; Error on the side of excessive subjects.
4386                                     (error ""))
4387                                   (mail-header-subject header)))
4388                                 ""
4389                               (mail-header-subject header))
4390                             nil (cdr (assq article gnus-newsgroup-scored))
4391                             (memq article gnus-newsgroup-processable))
4392                            (point)))))
4393         (when (cdr datal)
4394           (gnus-data-update-list
4395            (cdr datal)
4396            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4397
4398 (defun gnus-summary-update-article (article &optional iheader)
4399   "Update ARTICLE in the summary buffer."
4400   (set-buffer gnus-summary-buffer)
4401   (let* ((header (gnus-summary-article-header article))
4402          (id (mail-header-id header))
4403          (data (gnus-data-find article))
4404          (thread (gnus-id-to-thread id))
4405          (references (mail-header-references header))
4406          (parent
4407           (gnus-id-to-thread
4408            (or (gnus-parent-id
4409                 (when (and references
4410                            (not (equal "" references)))
4411                   references))
4412                "none")))
4413          (buffer-read-only nil)
4414          (old (car thread)))
4415     (when thread
4416       (unless iheader
4417         (setcar thread nil)
4418         (when parent
4419           (delq thread parent)))
4420       (if (gnus-summary-insert-subject id header)
4421           ;; Set the (possibly) new article number in the data structure.
4422           (gnus-data-set-number data (gnus-id-to-article id))
4423         (setcar thread old)
4424         nil))))
4425
4426 (defun gnus-rebuild-thread (id &optional line)
4427   "Rebuild the thread containing ID.
4428 If LINE, insert the rebuilt thread starting on line LINE."
4429   (let ((buffer-read-only nil)
4430         old-pos current thread data)
4431     (if (not gnus-show-threads)
4432         (setq thread (list (car (gnus-id-to-thread id))))
4433       ;; Get the thread this article is part of.
4434       (setq thread (gnus-remove-thread id)))
4435     (setq old-pos (point-at-bol))
4436     (setq current (save-excursion
4437                     (and (re-search-backward "[\r\n]" nil t)
4438                          (gnus-summary-article-number))))
4439     ;; If this is a gathered thread, we have to go some re-gathering.
4440     (when (stringp (car thread))
4441       (let ((subject (car thread))
4442             roots thr)
4443         (setq thread (cdr thread))
4444         (while thread
4445           (unless (memq (setq thr (gnus-id-to-thread
4446                                    (gnus-root-id
4447                                     (mail-header-id (caar thread)))))
4448                         roots)
4449             (push thr roots))
4450           (setq thread (cdr thread)))
4451         ;; We now have all (unique) roots.
4452         (if (= (length roots) 1)
4453             ;; All the loose roots are now one solid root.
4454             (setq thread (car roots))
4455           (setq thread (cons subject (gnus-sort-threads roots))))))
4456     (let (threads)
4457       ;; We then insert this thread into the summary buffer.
4458       (when line
4459         (goto-char (point-min))
4460         (forward-line (1- line)))
4461       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4462         (if gnus-show-threads
4463             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4464           (gnus-summary-prepare-unthreaded thread))
4465         (setq data (nreverse gnus-newsgroup-data))
4466         (setq threads gnus-newsgroup-threads))
4467       ;; We splice the new data into the data structure.
4468       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4469       ;;!!! then we want to insert at the beginning of the buffer.
4470       ;;!!! That happens to be true with Gnus now, but that may
4471       ;;!!! change in the future.  Perhaps.
4472       (gnus-data-enter-list
4473        (if line nil current) data (- (point) old-pos))
4474       (setq gnus-newsgroup-threads
4475             (nconc threads gnus-newsgroup-threads))
4476       (gnus-data-compute-positions))))
4477
4478 (defun gnus-number-to-header (number)
4479   "Return the header for article NUMBER."
4480   (let ((headers gnus-newsgroup-headers))
4481     (while (and headers
4482                 (not (= number (mail-header-number (car headers)))))
4483       (pop headers))
4484     (when headers
4485       (car headers))))
4486
4487 (defun gnus-parent-headers (in-headers &optional generation)
4488   "Return the headers of the GENERATIONeth parent of HEADERS."
4489   (unless generation
4490     (setq generation 1))
4491   (let ((parent t)
4492         (headers in-headers)
4493         references)
4494     (while (and parent
4495                 (not (zerop generation))
4496                 (setq references (mail-header-references headers)))
4497       (setq headers (if (and references
4498                              (setq parent (gnus-parent-id references)))
4499                         (car (gnus-id-to-thread parent))
4500                       nil))
4501       (decf generation))
4502     (and (not (eq headers in-headers))
4503          headers)))
4504
4505 (defun gnus-id-to-thread (id)
4506   "Return the (sub-)thread where ID appears."
4507   (gnus-gethash id gnus-newsgroup-dependencies))
4508
4509 (defun gnus-id-to-article (id)
4510   "Return the article number of ID."
4511   (let ((thread (gnus-id-to-thread id)))
4512     (when (and thread
4513                (car thread))
4514       (mail-header-number (car thread)))))
4515
4516 (defun gnus-id-to-header (id)
4517   "Return the article headers of ID."
4518   (car (gnus-id-to-thread id)))
4519
4520 (defun gnus-article-displayed-root-p (article)
4521   "Say whether ARTICLE is a root(ish) article."
4522   (let ((level (gnus-summary-thread-level article))
4523         (refs (mail-header-references  (gnus-summary-article-header article)))
4524         particle)
4525     (cond
4526      ((null level) nil)
4527      ((zerop level) t)
4528      ((null refs) t)
4529      ((null (gnus-parent-id refs)) t)
4530      ((and (= 1 level)
4531            (null (setq particle (gnus-id-to-article
4532                                  (gnus-parent-id refs))))
4533            (null (gnus-summary-thread-level particle)))))))
4534
4535 (defun gnus-root-id (id)
4536   "Return the id of the root of the thread where ID appears."
4537   (let (last-id prev)
4538     (while (and id (setq prev (car (gnus-id-to-thread id))))
4539       (setq last-id id
4540             id (gnus-parent-id (mail-header-references prev))))
4541     last-id))
4542
4543 (defun gnus-articles-in-thread (thread)
4544   "Return the list of articles in THREAD."
4545   (cons (mail-header-number (car thread))
4546         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4547
4548 (defun gnus-remove-thread (id &optional dont-remove)
4549   "Remove the thread that has ID in it."
4550   (let (headers thread last-id)
4551     ;; First go up in this thread until we find the root.
4552     (setq last-id (gnus-root-id id)
4553           headers (message-flatten-list (gnus-id-to-thread last-id)))
4554     ;; We have now found the real root of this thread.  It might have
4555     ;; been gathered into some loose thread, so we have to search
4556     ;; through the threads to find the thread we wanted.
4557     (let ((threads gnus-newsgroup-threads)
4558           sub)
4559       (while threads
4560         (setq sub (car threads))
4561         (if (stringp (car sub))
4562             ;; This is a gathered thread, so we look at the roots
4563             ;; below it to find whether this article is in this
4564             ;; gathered root.
4565             (progn
4566               (setq sub (cdr sub))
4567               (while sub
4568                 (when (member (caar sub) headers)
4569                   (setq thread (car threads)
4570                         threads nil
4571                         sub nil))
4572                 (setq sub (cdr sub))))
4573           ;; It's an ordinary thread, so we check it.
4574           (when (eq (car sub) (car headers))
4575             (setq thread sub
4576                   threads nil)))
4577         (setq threads (cdr threads)))
4578       ;; If this article is in no thread, then it's a root.
4579       (if thread
4580           (unless dont-remove
4581             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4582         (setq thread (gnus-id-to-thread last-id)))
4583       (when thread
4584         (prog1
4585             thread                      ; We return this thread.
4586           (unless dont-remove
4587             (if (stringp (car thread))
4588                 (progn
4589                   ;; If we use dummy roots, then we have to remove the
4590                   ;; dummy root as well.
4591                   (when (eq gnus-summary-make-false-root 'dummy)
4592                     ;; We go to the dummy root by going to
4593                     ;; the first sub-"thread", and then one line up.
4594                     (gnus-summary-goto-article
4595                      (mail-header-number (caadr thread)))
4596                     (forward-line -1)
4597                     (gnus-delete-line)
4598                     (gnus-data-compute-positions))
4599                   (setq thread (cdr thread))
4600                   (while thread
4601                     (gnus-remove-thread-1 (car thread))
4602                     (setq thread (cdr thread))))
4603               (gnus-remove-thread-1 thread))))))))
4604
4605 (defun gnus-remove-thread-1 (thread)
4606   "Remove the thread THREAD recursively."
4607   (let ((number (mail-header-number (pop thread)))
4608         d)
4609     (setq thread (reverse thread))
4610     (while thread
4611       (gnus-remove-thread-1 (pop thread)))
4612     (when (setq d (gnus-data-find number))
4613       (goto-char (gnus-data-pos d))
4614       (gnus-summary-show-thread)
4615       (gnus-data-remove
4616        number
4617        (- (point-at-bol)
4618           (prog1
4619               (1+ (point-at-eol))
4620             (gnus-delete-line)))))))
4621
4622 (defun gnus-sort-threads-1 (threads func)
4623   (sort (mapcar (lambda (thread)
4624                   (cons (car thread)
4625                         (and (cdr thread)
4626                              (gnus-sort-threads-1 (cdr thread) func))))
4627                 threads) func))
4628
4629 (defun gnus-sort-threads (threads)
4630   "Sort THREADS."
4631   (if (not gnus-thread-sort-functions)
4632       threads
4633     (gnus-message 8 "Sorting threads...")
4634     (let ((max-lisp-eval-depth 5000))
4635       (prog1 (gnus-sort-threads-1
4636          threads
4637          (gnus-make-sort-function gnus-thread-sort-functions))
4638         (gnus-message 8 "Sorting threads...done")))))
4639
4640 (defun gnus-sort-articles (articles)
4641   "Sort ARTICLES."
4642   (when gnus-article-sort-functions
4643     (gnus-message 7 "Sorting articles...")
4644     (prog1
4645         (setq gnus-newsgroup-headers
4646               (sort articles (gnus-make-sort-function
4647                               gnus-article-sort-functions)))
4648       (gnus-message 7 "Sorting articles...done"))))
4649
4650 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4651 (defmacro gnus-thread-header (thread)
4652   "Return header of first article in THREAD.
4653 Note that THREAD must never, ever be anything else than a variable -
4654 using some other form will lead to serious barfage."
4655   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4656   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4657   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4658         (vector thread) 2))
4659
4660 (defsubst gnus-article-sort-by-number (h1 h2)
4661   "Sort articles by article number."
4662   (< (mail-header-number h1)
4663      (mail-header-number h2)))
4664
4665 (defun gnus-thread-sort-by-number (h1 h2)
4666   "Sort threads by root article number."
4667   (gnus-article-sort-by-number
4668    (gnus-thread-header h1) (gnus-thread-header h2)))
4669
4670 (defsubst gnus-article-sort-by-random (h1 h2)
4671   "Sort articles by article number."
4672   (zerop (random 2)))
4673
4674 (defun gnus-thread-sort-by-random (h1 h2)
4675   "Sort threads by root article number."
4676   (gnus-article-sort-by-random
4677    (gnus-thread-header h1) (gnus-thread-header h2)))
4678
4679 (defsubst gnus-article-sort-by-lines (h1 h2)
4680   "Sort articles by article Lines header."
4681   (< (mail-header-lines h1)
4682      (mail-header-lines h2)))
4683
4684 (defun gnus-thread-sort-by-lines (h1 h2)
4685   "Sort threads by root article Lines header."
4686   (gnus-article-sort-by-lines
4687    (gnus-thread-header h1) (gnus-thread-header h2)))
4688
4689 (defsubst gnus-article-sort-by-chars (h1 h2)
4690   "Sort articles by octet length."
4691   (< (mail-header-chars h1)
4692      (mail-header-chars h2)))
4693
4694 (defun gnus-thread-sort-by-chars (h1 h2)
4695   "Sort threads by root article octet length."
4696   (gnus-article-sort-by-chars
4697    (gnus-thread-header h1) (gnus-thread-header h2)))
4698
4699 (defsubst gnus-article-sort-by-author (h1 h2)
4700   "Sort articles by root author."
4701   (string-lessp
4702    (let ((extract (funcall
4703                    gnus-extract-address-components
4704                    (mail-header-from h1))))
4705      (or (car extract) (cadr extract) ""))
4706    (let ((extract (funcall
4707                    gnus-extract-address-components
4708                    (mail-header-from h2))))
4709      (or (car extract) (cadr extract) ""))))
4710
4711 (defun gnus-thread-sort-by-author (h1 h2)
4712   "Sort threads by root author."
4713   (gnus-article-sort-by-author
4714    (gnus-thread-header h1)  (gnus-thread-header h2)))
4715
4716 (defsubst gnus-article-sort-by-recipient (h1 h2)
4717   "Sort articles by recipient."
4718   (string-lessp
4719    (let ((extract (funcall
4720                    gnus-extract-address-components
4721                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4722      (or (car extract) (cadr extract)))
4723    (let ((extract (funcall
4724                    gnus-extract-address-components
4725                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4726      (or (car extract) (cadr extract)))))
4727
4728 (defun gnus-thread-sort-by-recipient (h1 h2)
4729   "Sort threads by root recipient."
4730   (gnus-article-sort-by-recipient
4731    (gnus-thread-header h1) (gnus-thread-header h2)))
4732
4733 (defsubst gnus-article-sort-by-subject (h1 h2)
4734   "Sort articles by root subject."
4735   (string-lessp
4736    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4737    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4738
4739 (defun gnus-thread-sort-by-subject (h1 h2)
4740   "Sort threads by root subject."
4741   (gnus-article-sort-by-subject
4742    (gnus-thread-header h1) (gnus-thread-header h2)))
4743
4744 (defsubst gnus-article-sort-by-date (h1 h2)
4745   "Sort articles by root article date."
4746   (time-less-p
4747    (gnus-date-get-time (mail-header-date h1))
4748    (gnus-date-get-time (mail-header-date h2))))
4749
4750 (defun gnus-thread-sort-by-date (h1 h2)
4751   "Sort threads by root article date."
4752   (gnus-article-sort-by-date
4753    (gnus-thread-header h1) (gnus-thread-header h2)))
4754
4755 (defsubst gnus-article-sort-by-score (h1 h2)
4756   "Sort articles by root article score.
4757 Unscored articles will be counted as having a score of zero."
4758   (> (or (cdr (assq (mail-header-number h1)
4759                     gnus-newsgroup-scored))
4760          gnus-summary-default-score 0)
4761      (or (cdr (assq (mail-header-number h2)
4762                     gnus-newsgroup-scored))
4763          gnus-summary-default-score 0)))
4764
4765 (defun gnus-thread-sort-by-score (h1 h2)
4766   "Sort threads by root article score."
4767   (gnus-article-sort-by-score
4768    (gnus-thread-header h1) (gnus-thread-header h2)))
4769
4770 (defun gnus-thread-sort-by-total-score (h1 h2)
4771   "Sort threads by the sum of all scores in the thread.
4772 Unscored articles will be counted as having a score of zero."
4773   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4774
4775 (defun gnus-thread-total-score (thread)
4776   ;; This function find the total score of THREAD.
4777   (cond
4778    ((null thread)
4779     0)
4780    ((consp thread)
4781     (if (stringp (car thread))
4782         (apply gnus-thread-score-function 0
4783                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4784       (gnus-thread-total-score-1 thread)))
4785    (t
4786     (gnus-thread-total-score-1 (list thread)))))
4787
4788 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4789   "Sort threads such that the thread with the most recently arrived article comes first."
4790   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4791
4792 (defun gnus-thread-highest-number (thread)
4793   "Return the highest article number in THREAD."
4794   (apply 'max (mapcar (lambda (header)
4795                         (mail-header-number header))
4796                       (message-flatten-list thread))))
4797
4798 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4799   "Sort threads such that the thread with the most recently dated article comes first."
4800   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4801
4802 (defun gnus-thread-latest-date (thread)
4803   "Return the highest article date in THREAD."
4804   (let ((previous-time 0))
4805     (apply 'max
4806            (mapcar
4807             (lambda (header)
4808               (setq previous-time
4809                     (condition-case ()
4810                         (time-to-seconds (mail-header-parse-date
4811                                           (mail-header-date header)))
4812                       (error previous-time))))
4813             (sort
4814              (message-flatten-list thread)
4815              (lambda (h1 h2)
4816                (< (mail-header-number h1)
4817                   (mail-header-number h2))))))))
4818
4819 (defun gnus-thread-total-score-1 (root)
4820   ;; This function find the total score of the thread below ROOT.
4821   (setq root (car root))
4822   (apply gnus-thread-score-function
4823          (or (append
4824               (mapcar 'gnus-thread-total-score
4825                       (cdr (gnus-id-to-thread (mail-header-id root))))
4826               (when (> (mail-header-number root) 0)
4827                 (list (or (cdr (assq (mail-header-number root)
4828                                      gnus-newsgroup-scored))
4829                           gnus-summary-default-score 0))))
4830              (list gnus-summary-default-score)
4831              '(0))))
4832
4833 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4834 (defvar gnus-tmp-prev-subject nil)
4835 (defvar gnus-tmp-false-parent nil)
4836 (defvar gnus-tmp-root-expunged nil)
4837 (defvar gnus-tmp-dummy-line nil)
4838
4839 (eval-when-compile (defvar gnus-tmp-header))
4840 (defun gnus-extra-header (type &optional header)
4841   "Return the extra header of TYPE."
4842   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4843       ""))
4844
4845 (defvar gnus-tmp-thread-tree-header-string "")
4846
4847 (defcustom gnus-sum-thread-tree-root "> "
4848   "With %B spec, used for the root of a thread.
4849 If nil, use subject instead."
4850   :version "22.1"
4851   :type '(radio (const :format "%v  " nil) string)
4852   :group 'gnus-thread)
4853
4854 (defcustom gnus-sum-thread-tree-false-root "> "
4855   "With %B spec, used for a false root of a thread.
4856 If nil, use subject instead."
4857   :version "22.1"
4858   :type '(radio (const :format "%v  " nil) string)
4859   :group 'gnus-thread)
4860
4861 (defcustom gnus-sum-thread-tree-single-indent ""
4862   "With %B spec, used for a thread with just one message.
4863 If nil, use subject instead."
4864   :version "22.1"
4865   :type '(radio (const :format "%v  " nil) string)
4866   :group 'gnus-thread)
4867
4868 (defcustom gnus-sum-thread-tree-vertical "| "
4869   "With %B spec, used for drawing a vertical line."
4870   :version "22.1"
4871   :type 'string
4872   :group 'gnus-thread)
4873
4874 (defcustom gnus-sum-thread-tree-indent "  "
4875   "With %B spec, used for indenting."
4876   :version "22.1"
4877   :type 'string
4878   :group 'gnus-thread)
4879
4880 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4881   "With %B spec, used for a leaf with brothers."
4882   :version "22.1"
4883   :type 'string
4884   :group 'gnus-thread)
4885
4886 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4887   "With %B spec, used for a leaf without brothers."
4888   :version "22.1"
4889   :type 'string
4890   :group 'gnus-thread)
4891
4892 (defun gnus-summary-prepare-threads (threads)
4893   "Prepare summary buffer from THREADS and indentation LEVEL.
4894 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4895 or a straight list of headers."
4896   (gnus-message 7 "Generating summary...")
4897
4898   (setq gnus-newsgroup-threads threads)
4899   (beginning-of-line)
4900
4901   (let ((gnus-tmp-level 0)
4902         (default-score (or gnus-summary-default-score 0))
4903         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4904         (building-line-count gnus-summary-display-while-building)
4905         (building-count (integerp gnus-summary-display-while-building))
4906         thread number subject stack state gnus-tmp-gathered beg-match
4907         new-roots gnus-tmp-new-adopts thread-end simp-subject
4908         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4909         gnus-tmp-replied gnus-tmp-subject-or-nil
4910         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4911         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4912         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4913         tree-stack)
4914
4915     (setq gnus-tmp-prev-subject nil
4916           gnus-tmp-thread-tree-header-string "")
4917
4918     (if (vectorp (car threads))
4919         ;; If this is a straight (sic) list of headers, then a
4920         ;; threaded summary display isn't required, so we just create
4921         ;; an unthreaded one.
4922         (gnus-summary-prepare-unthreaded threads)
4923
4924       ;; Do the threaded display.
4925
4926       (if gnus-summary-display-while-building
4927           (switch-to-buffer (buffer-name)))
4928       (while (or threads stack gnus-tmp-new-adopts new-roots)
4929
4930         (if (and (= gnus-tmp-level 0)
4931                  (or (not stack)
4932                      (= (caar stack) 0))
4933                  (not gnus-tmp-false-parent)
4934                  (or gnus-tmp-new-adopts new-roots))
4935             (if gnus-tmp-new-adopts
4936                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4937                       thread (list (car gnus-tmp-new-adopts))
4938                       gnus-tmp-header (caar thread)
4939                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4940               (when new-roots
4941                 (setq thread (list (car new-roots))
4942                       gnus-tmp-header (caar thread)
4943                       new-roots (cdr new-roots))))
4944
4945           (if threads
4946               ;; If there are some threads, we do them before the
4947               ;; threads on the stack.
4948               (setq thread threads
4949                     gnus-tmp-header (caar thread))
4950             ;; There were no current threads, so we pop something off
4951             ;; the stack.
4952             (setq state (car stack)
4953                   gnus-tmp-level (car state)
4954                   tree-stack (cadr state)
4955                   thread (caddr state)
4956                   stack (cdr stack)
4957                   gnus-tmp-header (caar thread))))
4958
4959         (setq gnus-tmp-false-parent nil)
4960         (setq gnus-tmp-root-expunged nil)
4961         (setq thread-end nil)
4962
4963         (if (stringp gnus-tmp-header)
4964             ;; The header is a dummy root.
4965             (cond
4966              ((eq gnus-summary-make-false-root 'adopt)
4967               ;; We let the first article adopt the rest.
4968               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4969                                                (cddar thread)))
4970               (setq gnus-tmp-gathered
4971                     (nconc (mapcar
4972                             (lambda (h) (mail-header-number (car h)))
4973                             (cddar thread))
4974                            gnus-tmp-gathered))
4975               (setq thread (cons (list (caar thread)
4976                                        (cadar thread))
4977                                  (cdr thread)))
4978               (setq gnus-tmp-level -1
4979                     gnus-tmp-false-parent t))
4980              ((eq gnus-summary-make-false-root 'empty)
4981               ;; We print adopted articles with empty subject fields.
4982               (setq gnus-tmp-gathered
4983                     (nconc (mapcar
4984                             (lambda (h) (mail-header-number (car h)))
4985                             (cddar thread))
4986                            gnus-tmp-gathered))
4987               (setq gnus-tmp-level -1))
4988              ((eq gnus-summary-make-false-root 'dummy)
4989               ;; We remember that we probably want to output a dummy
4990               ;; root.
4991               (setq gnus-tmp-dummy-line gnus-tmp-header)
4992               (setq gnus-tmp-prev-subject gnus-tmp-header))
4993              (t
4994               ;; We do not make a root for the gathered
4995               ;; sub-threads at all.
4996               (setq gnus-tmp-level -1)))
4997
4998           (setq number (mail-header-number gnus-tmp-header)
4999                 subject (mail-header-subject gnus-tmp-header)
5000                 simp-subject (gnus-simplify-subject-fully subject))
5001
5002           (cond
5003            ;; If the thread has changed subject, we might want to make
5004            ;; this subthread into a root.
5005            ((and (null gnus-thread-ignore-subject)
5006                  (not (zerop gnus-tmp-level))
5007                  gnus-tmp-prev-subject
5008                  (not (string= gnus-tmp-prev-subject simp-subject)))
5009             (setq new-roots (nconc new-roots (list (car thread)))
5010                   thread-end t
5011                   gnus-tmp-header nil))
5012            ;; If the article lies outside the current limit,
5013            ;; then we do not display it.
5014            ((not (memq number gnus-newsgroup-limit))
5015             (setq gnus-tmp-gathered
5016                   (nconc (mapcar
5017                           (lambda (h) (mail-header-number (car h)))
5018                           (cdar thread))
5019                          gnus-tmp-gathered))
5020             (setq gnus-tmp-new-adopts (if (cdar thread)
5021                                           (append gnus-tmp-new-adopts
5022                                                   (cdar thread))
5023                                         gnus-tmp-new-adopts)
5024                   thread-end t
5025                   gnus-tmp-header nil)
5026             (when (zerop gnus-tmp-level)
5027               (setq gnus-tmp-root-expunged t)))
5028            ;; Perhaps this article is to be marked as read?
5029            ((and gnus-summary-mark-below
5030                  (< (or (cdr (assq number gnus-newsgroup-scored))
5031                         default-score)
5032                     gnus-summary-mark-below)
5033                  ;; Don't touch sparse articles.
5034                  (not (gnus-summary-article-sparse-p number))
5035                  (not (gnus-summary-article-ancient-p number)))
5036             (setq gnus-newsgroup-unreads
5037                   (delq number gnus-newsgroup-unreads))
5038             (if gnus-newsgroup-auto-expire
5039                 (setq gnus-newsgroup-expirable
5040                       (gnus-add-to-sorted-list
5041                        gnus-newsgroup-expirable number))
5042               (push (cons number gnus-low-score-mark)
5043                     gnus-newsgroup-reads))))
5044
5045           (when gnus-tmp-header
5046             ;; We may have an old dummy line to output before this
5047             ;; article.
5048             (when (and gnus-tmp-dummy-line
5049                        (gnus-subject-equal
5050                         gnus-tmp-dummy-line
5051                         (mail-header-subject gnus-tmp-header)))
5052               (gnus-summary-insert-dummy-line
5053                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5054               (setq gnus-tmp-dummy-line nil))
5055
5056             ;; Compute the mark.
5057             (setq gnus-tmp-unread (gnus-article-mark number))
5058
5059             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5060                                   gnus-tmp-header gnus-tmp-level)
5061                   gnus-newsgroup-data)
5062
5063             ;; Actually insert the line.
5064             (setq
5065              gnus-tmp-subject-or-nil
5066              (cond
5067               ((and gnus-thread-ignore-subject
5068                     gnus-tmp-prev-subject
5069                     (not (string= gnus-tmp-prev-subject simp-subject)))
5070                subject)
5071               ((zerop gnus-tmp-level)
5072                (if (and (eq gnus-summary-make-false-root 'empty)
5073                         (memq number gnus-tmp-gathered)
5074                         gnus-tmp-prev-subject
5075                         (string= gnus-tmp-prev-subject simp-subject))
5076                    gnus-summary-same-subject
5077                  subject))
5078               (t gnus-summary-same-subject)))
5079             (if (and (eq gnus-summary-make-false-root 'adopt)
5080                      (= gnus-tmp-level 1)
5081                      (memq number gnus-tmp-gathered))
5082                 (setq gnus-tmp-opening-bracket ?\<
5083                       gnus-tmp-closing-bracket ?\>)
5084               (setq gnus-tmp-opening-bracket ?\[
5085                     gnus-tmp-closing-bracket ?\]))
5086             (setq
5087              gnus-tmp-indentation
5088              (aref gnus-thread-indent-array gnus-tmp-level)
5089              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5090              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5091                                 gnus-summary-default-score 0)
5092              gnus-tmp-score-char
5093              (if (or (null gnus-summary-default-score)
5094                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5095                          gnus-summary-zcore-fuzz))
5096                  ?                      ;Whitespace
5097                (if (< gnus-tmp-score gnus-summary-default-score)
5098                    gnus-score-below-mark gnus-score-over-mark))
5099              gnus-tmp-replied
5100              (cond ((memq number gnus-newsgroup-processable)
5101                     gnus-process-mark)
5102                    ((memq number gnus-newsgroup-cached)
5103                     gnus-cached-mark)
5104                    ((memq number gnus-newsgroup-replied)
5105                     gnus-replied-mark)
5106                    ((memq number gnus-newsgroup-forwarded)
5107                     gnus-forwarded-mark)
5108                    ((memq number gnus-newsgroup-saved)
5109                     gnus-saved-mark)
5110                    ((memq number gnus-newsgroup-recent)
5111                     gnus-recent-mark)
5112                    ((memq number gnus-newsgroup-unseen)
5113                     gnus-unseen-mark)
5114                    (t gnus-no-mark))
5115              gnus-tmp-downloaded
5116              (cond ((memq number gnus-newsgroup-undownloaded)
5117                     gnus-undownloaded-mark)
5118                    (gnus-newsgroup-agentized
5119                     gnus-downloaded-mark)
5120                    (t
5121                     gnus-no-mark))
5122              gnus-tmp-from (mail-header-from gnus-tmp-header)
5123              gnus-tmp-name
5124              (cond
5125               ((string-match "<[^>]+> *$" gnus-tmp-from)
5126                (setq beg-match (match-beginning 0))
5127                (or (and (string-match "^\".+\"" gnus-tmp-from)
5128                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5129                    (substring gnus-tmp-from 0 beg-match)))
5130               ((string-match "(.+)" gnus-tmp-from)
5131                (substring gnus-tmp-from
5132                           (1+ (match-beginning 0)) (1- (match-end 0))))
5133               (t gnus-tmp-from))
5134
5135              ;; Do the %B string
5136              gnus-tmp-thread-tree-header-string
5137              (cond
5138               ((not gnus-show-threads) "")
5139               ((zerop gnus-tmp-level)
5140                (cond ((cdar thread)
5141                       (or gnus-sum-thread-tree-root subject))
5142                      (gnus-tmp-new-adopts
5143                       (or gnus-sum-thread-tree-false-root subject))
5144                      (t
5145                       (or gnus-sum-thread-tree-single-indent subject))))
5146               (t
5147                (concat (apply 'concat
5148                               (mapcar (lambda (item)
5149                                         (if (= item 1)
5150                                             gnus-sum-thread-tree-vertical
5151                                           gnus-sum-thread-tree-indent))
5152                                       (cdr (reverse tree-stack))))
5153                        (if (nth 1 thread)
5154                            gnus-sum-thread-tree-leaf-with-other
5155                          gnus-sum-thread-tree-single-leaf)))))
5156             (when (string= gnus-tmp-name "")
5157               (setq gnus-tmp-name gnus-tmp-from))
5158             (unless (numberp gnus-tmp-lines)
5159               (setq gnus-tmp-lines -1))
5160             (if (= gnus-tmp-lines -1)
5161                 (setq gnus-tmp-lines "?")
5162               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5163               (gnus-put-text-property
5164              (point)
5165              (progn (eval gnus-summary-line-format-spec) (point))
5166                'gnus-number number)
5167             (when gnus-visual-p
5168               (forward-line -1)
5169               (gnus-run-hooks 'gnus-summary-update-hook)
5170               (forward-line 1))
5171
5172             (setq gnus-tmp-prev-subject simp-subject)))
5173
5174         (when (nth 1 thread)
5175           (push (list (max 0 gnus-tmp-level)
5176                       (copy-sequence tree-stack)
5177                       (nthcdr 1 thread))
5178                 stack))
5179         (push (if (nth 1 thread) 1 0) tree-stack)
5180         (incf gnus-tmp-level)
5181         (setq threads (if thread-end nil (cdar thread)))
5182         (if gnus-summary-display-while-building
5183             (if building-count
5184                 (progn
5185                   ;; use a set frequency
5186                   (setq building-line-count (1- building-line-count))
5187                   (when (= building-line-count 0)
5188                     (sit-for 0)
5189                     (setq building-line-count
5190                           gnus-summary-display-while-building)))
5191               ;; always
5192               (sit-for 0)))
5193         (unless threads
5194           (setq gnus-tmp-level 0)))))
5195   (gnus-message 7 "Generating summary...done"))
5196
5197 (defun gnus-summary-prepare-unthreaded (headers)
5198   "Generate an unthreaded summary buffer based on HEADERS."
5199   (let (header number mark)
5200
5201     (beginning-of-line)
5202
5203     (while headers
5204       ;; We may have to root out some bad articles...
5205       (when (memq (setq number (mail-header-number
5206                                 (setq header (pop headers))))
5207                   gnus-newsgroup-limit)
5208         ;; Mark article as read when it has a low score.
5209         (when (and gnus-summary-mark-below
5210                    (< (or (cdr (assq number gnus-newsgroup-scored))
5211                           gnus-summary-default-score 0)
5212                       gnus-summary-mark-below)
5213                    (not (gnus-summary-article-ancient-p number)))
5214           (setq gnus-newsgroup-unreads
5215                 (delq number gnus-newsgroup-unreads))
5216           (if gnus-newsgroup-auto-expire
5217               (push number gnus-newsgroup-expirable)
5218             (push (cons number gnus-low-score-mark)
5219                   gnus-newsgroup-reads)))
5220
5221         (setq mark (gnus-article-mark number))
5222         (push (gnus-data-make number mark (1+ (point)) header 0)
5223               gnus-newsgroup-data)
5224         (gnus-summary-insert-line
5225          header 0 number
5226          (memq number gnus-newsgroup-undownloaded)
5227          mark (memq number gnus-newsgroup-replied)
5228          (memq number gnus-newsgroup-expirable)
5229          (mail-header-subject header) nil
5230          (cdr (assq number gnus-newsgroup-scored))
5231          (memq number gnus-newsgroup-processable))))))
5232
5233 (defun gnus-summary-remove-list-identifiers ()
5234   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5235   (let ((regexp (if (consp gnus-list-identifiers)
5236                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5237                   gnus-list-identifiers))
5238         changed subject)
5239     (when regexp
5240       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5241       (dolist (header gnus-newsgroup-headers)
5242         (setq subject (mail-header-subject header)
5243               changed nil)
5244         (while (string-match regexp subject)
5245           (setq subject
5246                 (concat (substring subject 0 (match-beginning 1))
5247                         (substring subject (match-end 0)))
5248                 changed t))
5249         (when changed
5250           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5251             (setq subject
5252                   (concat (substring subject 0 (match-beginning 1))
5253                           (substring subject (match-end 1)))))
5254           (mail-header-set-subject header subject))))))
5255
5256 (defun gnus-fetch-headers (articles)
5257   "Fetch headers of ARTICLES."
5258   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5259     (gnus-message 5 "Fetching headers for %s..." name)
5260     (prog1
5261         (if (eq 'nov
5262                 (setq gnus-headers-retrieved-by
5263                       (gnus-retrieve-headers
5264                        articles gnus-newsgroup-name
5265                        ;; We might want to fetch old headers, but
5266                        ;; not if there is only 1 article.
5267                        (and (or (and
5268                                  (not (eq gnus-fetch-old-headers 'some))
5269                                  (not (numberp gnus-fetch-old-headers)))
5270                                 (> (length articles) 1))
5271                             gnus-fetch-old-headers))))
5272             (gnus-get-newsgroup-headers-xover
5273              articles nil nil gnus-newsgroup-name t)
5274           (gnus-get-newsgroup-headers))
5275       (gnus-message 5 "Fetching headers for %s...done" name))))
5276
5277 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5278   "Select newsgroup GROUP.
5279 If READ-ALL is non-nil, all articles in the group are selected.
5280 If SELECT-ARTICLES, only select those articles from GROUP."
5281   (let* ((entry (gnus-group-entry group))
5282          ;;!!! Dirty hack; should be removed.
5283          (gnus-summary-ignore-duplicates
5284           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5285               t
5286             gnus-summary-ignore-duplicates))
5287          (info (nth 2 entry))
5288          articles fetched-articles cached)
5289
5290     (unless (gnus-check-server
5291              (set (make-local-variable 'gnus-current-select-method)
5292                   (gnus-find-method-for-group group)))
5293       (error "Couldn't open server"))
5294
5295     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5296         (gnus-activate-group group)     ; Or we can activate it...
5297         (progn                          ; Or we bug out.
5298           (when (equal major-mode 'gnus-summary-mode)
5299             (gnus-kill-buffer (current-buffer)))
5300           (error "Couldn't activate group %s: %s"
5301                  group (gnus-status-message group))))
5302
5303     (unless (gnus-request-group group t)
5304       (when (equal major-mode 'gnus-summary-mode)
5305         (gnus-kill-buffer (current-buffer)))
5306       (error "Couldn't request group %s: %s"
5307              group (gnus-status-message group)))
5308
5309     (when gnus-agent
5310       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5311
5312       (setq gnus-summary-use-undownloaded-faces
5313             (gnus-agent-find-parameter
5314              group
5315              'agent-enable-undownloaded-faces)))
5316
5317     (setq gnus-newsgroup-name group
5318           gnus-newsgroup-unselected nil
5319           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5320
5321     (let ((display (gnus-group-find-parameter group 'display)))
5322       (setq gnus-newsgroup-display
5323             (cond
5324              ((not (zerop (or (car-safe read-all) 0)))
5325               ;; The user entered the group with C-u SPC/RET, let's show
5326               ;; all articles.
5327               'gnus-not-ignore)
5328              ((eq display 'all)
5329               'gnus-not-ignore)
5330              ((arrayp display)
5331               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5332              ((numberp display)
5333               ;; The following is probably the "correct" solution, but
5334               ;; it makes Gnus fetch all headers and then limit the
5335               ;; articles (which is slow), so instead we hack the
5336               ;; select-articles parameter instead. -- Simon Josefsson
5337               ;; <jas@kth.se>
5338               ;;
5339               ;; (gnus-byte-compile
5340               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5341               ;;                         display)))))
5342               (setq select-articles
5343                     (gnus-uncompress-range
5344                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5345                              (if (> tmp 0)
5346                                  tmp
5347                                1))
5348                            (cdr (gnus-active group)))))
5349               nil)
5350              (t
5351               nil))))
5352
5353     (gnus-summary-setup-default-charset)
5354
5355     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5356     (when (gnus-virtual-group-p group)
5357       (setq cached gnus-newsgroup-cached))
5358
5359     (setq gnus-newsgroup-unreads
5360           (gnus-sorted-ndifference
5361            (gnus-sorted-ndifference gnus-newsgroup-unreads
5362                                     gnus-newsgroup-marked)
5363            gnus-newsgroup-dormant))
5364
5365     (setq gnus-newsgroup-processable nil)
5366
5367     (gnus-update-read-articles group gnus-newsgroup-unreads)
5368
5369     ;; Adjust and set lists of article marks.
5370     (when info
5371       (gnus-adjust-marked-articles info))
5372     (if (setq articles select-articles)
5373         (setq gnus-newsgroup-unselected
5374               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5375       (setq articles (gnus-articles-to-read group read-all)))
5376
5377     (cond
5378      ((null articles)
5379       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5380       'quit)
5381      ((eq articles 0) nil)
5382      (t
5383       ;; Init the dependencies hash table.
5384       (setq gnus-newsgroup-dependencies
5385             (gnus-make-hashtable (length articles)))
5386       (gnus-set-global-variables)
5387       ;; Retrieve the headers and read them in.
5388
5389       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5390
5391       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5392       (when cached
5393         (setq gnus-newsgroup-cached cached))
5394
5395       ;; Suppress duplicates?
5396       (when gnus-suppress-duplicates
5397         (gnus-dup-suppress-articles))
5398
5399       ;; Set the initial limit.
5400       (setq gnus-newsgroup-limit (copy-sequence articles))
5401       ;; Remove canceled articles from the list of unread articles.
5402       (setq fetched-articles
5403             (mapcar (lambda (headers) (mail-header-number headers))
5404                     gnus-newsgroup-headers))
5405       (setq gnus-newsgroup-articles fetched-articles)
5406       (setq gnus-newsgroup-unreads
5407             (gnus-sorted-nintersection
5408              gnus-newsgroup-unreads fetched-articles))
5409       (gnus-compute-unseen-list)
5410
5411       ;; Removed marked articles that do not exist.
5412       (gnus-update-missing-marks
5413        (gnus-sorted-difference articles fetched-articles))
5414       ;; We might want to build some more threads first.
5415       (when (and gnus-fetch-old-headers
5416                  (eq gnus-headers-retrieved-by 'nov))
5417         (if (eq gnus-fetch-old-headers 'invisible)
5418             (gnus-build-all-threads)
5419           (gnus-build-old-threads)))
5420       ;; Let the Gnus agent mark articles as read.
5421       (when gnus-agent
5422         (gnus-agent-get-undownloaded-list))
5423       ;; Remove list identifiers from subject
5424       (when gnus-list-identifiers
5425         (gnus-summary-remove-list-identifiers))
5426       ;; Check whether auto-expire is to be done in this group.
5427       (setq gnus-newsgroup-auto-expire
5428             (gnus-group-auto-expirable-p group))
5429       ;; Set up the article buffer now, if necessary.
5430       (unless gnus-single-article-buffer
5431         (gnus-article-setup-buffer))
5432       ;; First and last article in this newsgroup.
5433       (when gnus-newsgroup-headers
5434         (setq gnus-newsgroup-begin
5435               (mail-header-number (car gnus-newsgroup-headers))
5436               gnus-newsgroup-end
5437               (mail-header-number
5438                (gnus-last-element gnus-newsgroup-headers))))
5439       ;; GROUP is successfully selected.
5440       (or gnus-newsgroup-headers t)))))
5441
5442 (defun gnus-compute-unseen-list ()
5443   ;; The `seen' marks are treated specially.
5444   (if (not gnus-newsgroup-seen)
5445       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5446     (setq gnus-newsgroup-unseen
5447           (gnus-inverse-list-range-intersection
5448            gnus-newsgroup-articles gnus-newsgroup-seen))))
5449
5450 (defun gnus-summary-display-make-predicate (display)
5451   (require 'gnus-agent)
5452   (when (= (length display) 1)
5453     (setq display (car display)))
5454   (unless gnus-summary-display-cache
5455     (dolist (elem (append '((unread . unread)
5456                             (read . read)
5457                             (unseen . unseen))
5458                           gnus-article-mark-lists))
5459       (push (cons (cdr elem)
5460                   (gnus-byte-compile
5461                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5462             gnus-summary-display-cache)))
5463   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5464         (gnus-category-predicate-cache gnus-summary-display-cache))
5465     (gnus-get-predicate display)))
5466
5467 ;; Uses the dynamically bound `number' variable.
5468 (eval-when-compile
5469   (defvar number))
5470 (defun gnus-article-marked-p (type &optional article)
5471   (let ((article (or article number)))
5472     (cond
5473      ((eq type 'tick)
5474       (memq article gnus-newsgroup-marked))
5475      ((eq type 'spam)
5476       (memq article gnus-newsgroup-spam-marked))
5477      ((eq type 'unsend)
5478       (memq article gnus-newsgroup-unsendable))
5479      ((eq type 'undownload)
5480       (memq article gnus-newsgroup-undownloaded))
5481      ((eq type 'download)
5482       (memq article gnus-newsgroup-downloadable))
5483      ((eq type 'unread)
5484       (memq article gnus-newsgroup-unreads))
5485      ((eq type 'read)
5486       (memq article gnus-newsgroup-reads))
5487      ((eq type 'dormant)
5488       (memq article gnus-newsgroup-dormant) )
5489      ((eq type 'expire)
5490       (memq article gnus-newsgroup-expirable))
5491      ((eq type 'reply)
5492       (memq article gnus-newsgroup-replied))
5493      ((eq type 'killed)
5494       (memq article gnus-newsgroup-killed))
5495      ((eq type 'bookmark)
5496       (assq article gnus-newsgroup-bookmarks))
5497      ((eq type 'score)
5498       (assq article gnus-newsgroup-scored))
5499      ((eq type 'save)
5500       (memq article gnus-newsgroup-saved))
5501      ((eq type 'cache)
5502       (memq article gnus-newsgroup-cached))
5503      ((eq type 'forward)
5504       (memq article gnus-newsgroup-forwarded))
5505      ((eq type 'seen)
5506       (not (memq article gnus-newsgroup-unseen)))
5507      ((eq type 'recent)
5508       (memq article gnus-newsgroup-recent))
5509      (t t))))
5510
5511 (defun gnus-articles-to-read (group &optional read-all)
5512   "Find out what articles the user wants to read."
5513   (let* ((articles
5514           ;; Select all articles if `read-all' is non-nil, or if there
5515           ;; are no unread articles.
5516           (if (or read-all
5517                   (and (zerop (length gnus-newsgroup-marked))
5518                        (zerop (length gnus-newsgroup-unreads)))
5519                   ;; Fetch all if the predicate is non-nil.
5520                   gnus-newsgroup-display)
5521               ;; We want to select the headers for all the articles in
5522               ;; the group, so we select either all the active
5523               ;; articles in the group, or (if that's nil), the
5524               ;; articles in the cache.
5525               (or
5526                (gnus-uncompress-range (gnus-active group))
5527                (gnus-cache-articles-in-group group))
5528             ;; Select only the "normal" subset of articles.
5529             (gnus-sorted-nunion
5530              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5531              gnus-newsgroup-unreads)))
5532          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5533          (scored (length scored-list))
5534          (number (length articles))
5535          (marked (+ (length gnus-newsgroup-marked)
5536                     (length gnus-newsgroup-dormant)))
5537          (select
5538           (cond
5539            ((numberp read-all)
5540             read-all)
5541            ((numberp gnus-newsgroup-display)
5542             gnus-newsgroup-display)
5543            (t
5544             (condition-case ()
5545                 (cond
5546                  ((and (or (<= scored marked) (= scored number))
5547                        (numberp gnus-large-newsgroup)
5548                        (> number gnus-large-newsgroup))
5549                   (let* ((cursor-in-echo-area nil)
5550                          (initial (gnus-parameter-large-newsgroup-initial
5551                                    gnus-newsgroup-name))
5552                          (input
5553                           (read-string
5554                            (format
5555                             "How many articles from %s (%s %d): "
5556                             (gnus-limit-string
5557                              (gnus-group-decoded-name gnus-newsgroup-name)
5558                              35)
5559                             (if initial "max" "default")
5560                             number)
5561                            (if initial
5562                                (cons (number-to-string initial)
5563                                      0)))))
5564                     (if (string-match "^[ \t]*$" input) number input)))
5565                  ((and (> scored marked) (< scored number)
5566                        (> (- scored number) 20))
5567                   (let ((input
5568                          (read-string
5569                           (format "%s %s (%d scored, %d total): "
5570                                   "How many articles from"
5571                                   (gnus-group-decoded-name group)
5572                                   scored number))))
5573                     (if (string-match "^[ \t]*$" input)
5574                         number input)))
5575                  (t number))
5576               (quit
5577                (message "Quit getting the articles to read")
5578                nil))))))
5579     (setq select (if (stringp select) (string-to-number select) select))
5580     (if (or (null select) (zerop select))
5581         select
5582       (if (and (not (zerop scored)) (<= (abs select) scored))
5583           (progn
5584             (setq articles (sort scored-list '<))
5585             (setq number (length articles)))
5586         (setq articles (copy-sequence articles)))
5587
5588       (when (< (abs select) number)
5589         (if (< select 0)
5590             ;; Select the N oldest articles.
5591             (setcdr (nthcdr (1- (abs select)) articles) nil)
5592           ;; Select the N most recent articles.
5593           (setq articles (nthcdr (- number select) articles))))
5594       (setq gnus-newsgroup-unselected
5595             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5596       (when gnus-alter-articles-to-read-function
5597         (setq articles
5598               (sort
5599                (funcall gnus-alter-articles-to-read-function
5600                         gnus-newsgroup-name articles)
5601                '<)))
5602       articles)))
5603
5604 (defun gnus-killed-articles (killed articles)
5605   (let (out)
5606     (while articles
5607       (when (inline (gnus-member-of-range (car articles) killed))
5608         (push (car articles) out))
5609       (setq articles (cdr articles)))
5610     out))
5611
5612 (defun gnus-uncompress-marks (marks)
5613   "Uncompress the mark ranges in MARKS."
5614   (let ((uncompressed '(score bookmark))
5615         out)
5616     (while marks
5617       (if (memq (caar marks) uncompressed)
5618           (push (car marks) out)
5619         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5620       (setq marks (cdr marks)))
5621     out))
5622
5623 (defun gnus-article-mark-to-type (mark)
5624   "Return the type of MARK."
5625   (or (cadr (assq mark gnus-article-special-mark-lists))
5626       'list))
5627
5628 (defun gnus-article-unpropagatable-p (mark)
5629   "Return whether MARK should be propagated to back end."
5630   (memq mark gnus-article-unpropagated-mark-lists))
5631
5632 (defun gnus-adjust-marked-articles (info)
5633   "Set all article lists and remove all marks that are no longer valid."
5634   (let* ((marked-lists (gnus-info-marks info))
5635          (active (gnus-active (gnus-info-group info)))
5636          (min (car active))
5637          (max (cdr active))
5638          (types gnus-article-mark-lists)
5639          marks var articles article mark mark-type
5640          bgn end)
5641
5642     (dolist (marks marked-lists)
5643       (setq mark (car marks)
5644             mark-type (gnus-article-mark-to-type mark)
5645             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5646
5647       ;; We set the variable according to the type of the marks list,
5648       ;; and then adjust the marks to a subset of the active articles.
5649       (cond
5650        ;; Adjust "simple" lists - compressed yet unsorted
5651        ((eq mark-type 'list)
5652         ;; Simultaneously uncompress and clip to active range
5653         ;; See gnus-uncompress-range for a description of possible marks
5654         (let (l lh)
5655           (if (not (cadr marks))
5656               (set var nil)
5657             (setq articles (if (numberp (cddr marks))
5658                                (list (cdr marks))
5659                              (cdr marks))
5660                   lh (cons nil nil)
5661                   l lh)
5662
5663             (while (setq article (pop articles))
5664               (cond ((consp article)
5665                      (setq bgn (max (car article) min)
5666                            end (min (cdr article) max))
5667                      (while (<= bgn end)
5668                        (setq l (setcdr l (cons bgn nil))
5669                              bgn (1+ bgn))))
5670                     ((and (<= min article)
5671                           (>= max article))
5672                      (setq l (setcdr l (cons article nil))))))
5673             (set var (cdr lh)))))
5674        ;; Adjust assocs.
5675        ((eq mark-type 'tuple)
5676         (set var (setq articles (cdr marks)))
5677         (when (not (listp (cdr (symbol-value var))))
5678           (set var (list (symbol-value var))))
5679         (when (not (listp (cdr articles)))
5680           (setq articles (list articles)))
5681         (while articles
5682           (when (or (not (consp (setq article (pop articles))))
5683                     (< (car article) min)
5684                     (> (car article) max))
5685             (set var (delq article (symbol-value var))))))
5686        ;; Adjust ranges (sloppily).
5687        ((eq mark-type 'range)
5688         (cond
5689          ((eq mark 'seen)
5690           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5691           ;; It should be (seen (NUM1 . NUM2)).
5692           (when (numberp (cddr marks))
5693             (setcdr marks (list (cdr marks))))
5694           (setq articles (cdr marks))
5695           (while (and articles
5696                       (or (and (consp (car articles))
5697                                (> min (cdar articles)))
5698                           (and (numberp (car articles))
5699                                (> min (car articles)))))
5700             (pop articles))
5701           (set var articles))))))))
5702
5703 (defun gnus-update-missing-marks (missing)
5704   "Go through the list of MISSING articles and remove them from the mark lists."
5705   (when missing
5706     (let (var m)
5707       ;; Go through all types.
5708       (dolist (elem gnus-article-mark-lists)
5709         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5710           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5711           (when (symbol-value var)
5712             ;; This list has articles.  So we delete all missing
5713             ;; articles from it.
5714             (setq m missing)
5715             (while m
5716               (set var (delq (pop m) (symbol-value var))))))))))
5717
5718 (defun gnus-update-marks ()
5719   "Enter the various lists of marked articles into the newsgroup info list."
5720   (let ((types gnus-article-mark-lists)
5721         (info (gnus-get-info gnus-newsgroup-name))
5722         type list newmarked symbol delta-marks)
5723     (when info
5724       ;; Add all marks lists to the list of marks lists.
5725       (while (setq type (pop types))
5726         (setq list (symbol-value
5727                     (setq symbol
5728                           (intern (format "gnus-newsgroup-%s" (car type))))))
5729
5730         (when list
5731           ;; Get rid of the entries of the articles that have the
5732           ;; default score.
5733           (when (and (eq (cdr type) 'score)
5734                      gnus-save-score
5735                      list)
5736             (let* ((arts list)
5737                    (prev (cons nil list))
5738                    (all prev))
5739               (while arts
5740                 (if (or (not (consp (car arts)))
5741                         (= (cdar arts) gnus-summary-default-score))
5742                     (setcdr prev (cdr arts))
5743                   (setq prev arts))
5744                 (setq arts (cdr arts)))
5745               (setq list (cdr all)))))
5746
5747         (when (eq (cdr type) 'seen)
5748           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5749
5750         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5751           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5752
5753         (when (and (gnus-check-backend-function
5754                     'request-set-mark gnus-newsgroup-name)
5755                    (not (gnus-article-unpropagatable-p (cdr type))))
5756           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5757                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5758                  (add (gnus-remove-from-range
5759                        (gnus-copy-sequence list) old)))
5760             (when add
5761               (push (list add 'add (list (cdr type))) delta-marks))
5762             (when del
5763               (push (list del 'del (list (cdr type))) delta-marks))))
5764
5765         (when list
5766           (push (cons (cdr type) list) newmarked)))
5767
5768       (when delta-marks
5769         (unless (gnus-check-group gnus-newsgroup-name)
5770           (error "Can't open server for %s" gnus-newsgroup-name))
5771         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5772
5773       ;; Enter these new marks into the info of the group.
5774       (if (nthcdr 3 info)
5775           (setcar (nthcdr 3 info) newmarked)
5776         ;; Add the marks lists to the end of the info.
5777         (when newmarked
5778           (setcdr (nthcdr 2 info) (list newmarked))))
5779
5780       ;; Cut off the end of the info if there's nothing else there.
5781       (let ((i 5))
5782         (while (and (> i 2)
5783                     (not (nth i info)))
5784           (when (nthcdr (decf i) info)
5785             (setcdr (nthcdr i info) nil)))))))
5786
5787 (defun gnus-set-mode-line (where)
5788   "Set the mode line of the article or summary buffers.
5789 If WHERE is `summary', the summary mode line format will be used."
5790   ;; Is this mode line one we keep updated?
5791   (when (and (memq where gnus-updated-mode-lines)
5792              (symbol-value
5793               (intern (format "gnus-%s-mode-line-format-spec" where))))
5794     (let (mode-string)
5795       (save-excursion
5796         ;; We evaluate this in the summary buffer since these
5797         ;; variables are buffer-local to that buffer.
5798         (set-buffer gnus-summary-buffer)
5799        ;; We bind all these variables that are used in the `eval' form
5800         ;; below.
5801         (let* ((mformat (symbol-value
5802                          (intern
5803                           (format "gnus-%s-mode-line-format-spec" where))))
5804                (gnus-tmp-group-name (gnus-group-decoded-name
5805                                      gnus-newsgroup-name))
5806                (gnus-tmp-article-number (or gnus-current-article 0))
5807                (gnus-tmp-unread gnus-newsgroup-unreads)
5808                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5809                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5810                (gnus-tmp-unread-and-unselected
5811                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5812                             (zerop gnus-tmp-unselected))
5813                        "")
5814                       ((zerop gnus-tmp-unselected)
5815                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5816                       (t (format "{%d(+%d) more}"
5817                                  gnus-tmp-unread-and-unticked
5818                                  gnus-tmp-unselected))))
5819                (gnus-tmp-subject
5820                 (if (and gnus-current-headers
5821                          (vectorp gnus-current-headers))
5822                     (gnus-mode-string-quote
5823                      (mail-header-subject gnus-current-headers))
5824                   ""))
5825                bufname-length max-len
5826                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5827           (setq mode-string (eval mformat))
5828           (setq bufname-length (if (string-match "%b" mode-string)
5829                                    (- (length
5830                                        (buffer-name
5831                                         (if (eq where 'summary)
5832                                             nil
5833                                           (get-buffer gnus-article-buffer))))
5834                                       2)
5835                                  0))
5836           (setq max-len (max 4 (if gnus-mode-non-string-length
5837                                    (- (window-width)
5838                                       gnus-mode-non-string-length
5839                                       bufname-length)
5840                                  (length mode-string))))
5841           ;; We might have to chop a bit of the string off...
5842           (when (> (length mode-string) max-len)
5843             (setq mode-string
5844                   (concat (truncate-string-to-width mode-string (- max-len 3))
5845                           "...")))
5846           ;; Pad the mode string a bit.
5847           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5848       ;; Update the mode line.
5849       (setq mode-line-buffer-identification
5850             (gnus-mode-line-buffer-identification (list mode-string)))
5851       (set-buffer-modified-p t))))
5852
5853 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5854   "Go through the HEADERS list and add all Xrefs to a hash table.
5855 The resulting hash table is returned, or nil if no Xrefs were found."
5856   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5857          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5858          (xref-hashtb (gnus-make-hashtable))
5859          start group entry number xrefs header)
5860     (while headers
5861       (setq header (pop headers))
5862       (when (and (setq xrefs (mail-header-xref header))
5863                  (not (memq (setq number (mail-header-number header))
5864                             unreads)))
5865         (setq start 0)
5866         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5867           (setq start (match-end 0))
5868           (setq group (if prefix
5869                           (concat prefix (substring xrefs (match-beginning 1)
5870                                                     (match-end 1)))
5871                         (substring xrefs (match-beginning 1) (match-end 1))))
5872           (setq number
5873                 (string-to-number (substring xrefs (match-beginning 2)
5874                                           (match-end 2))))
5875           (if (setq entry (gnus-gethash group xref-hashtb))
5876               (setcdr entry (cons number (cdr entry)))
5877             (gnus-sethash group (cons number nil) xref-hashtb)))))
5878     (and start xref-hashtb)))
5879
5880 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5881   "Look through all the headers and mark the Xrefs as read."
5882   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5883         name info xref-hashtb idlist method nth4)
5884     (save-excursion
5885       (set-buffer gnus-group-buffer)
5886       (when (setq xref-hashtb
5887                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5888         (mapatoms
5889          (lambda (group)
5890            (unless (string= from-newsgroup (setq name (symbol-name group)))
5891              (setq idlist (symbol-value group))
5892              ;; Dead groups are not updated.
5893              (and (prog1
5894                       (setq info (gnus-get-info name))
5895                     (when (stringp (setq nth4 (gnus-info-method info)))
5896                       (setq nth4 (gnus-server-to-method nth4))))
5897                   ;; Only do the xrefs if the group has the same
5898                   ;; select method as the group we have just read.
5899                   (or (gnus-methods-equal-p
5900                        nth4 (gnus-find-method-for-group from-newsgroup))
5901                       virtual
5902                       (equal nth4 (setq method (gnus-find-method-for-group
5903                                                 from-newsgroup)))
5904                       (and (equal (car nth4) (car method))
5905                            (equal (nth 1 nth4) (nth 1 method))))
5906                   gnus-use-cross-reference
5907                   (or (not (eq gnus-use-cross-reference t))
5908                       virtual
5909                       ;; Only do cross-references on subscribed
5910                       ;; groups, if that is what is wanted.
5911                       (<= (gnus-info-level info) gnus-level-subscribed))
5912                   (gnus-group-make-articles-read name idlist))))
5913          xref-hashtb)))))
5914
5915 (defun gnus-compute-read-articles (group articles)
5916   (let* ((entry (gnus-group-entry group))
5917          (info (nth 2 entry))
5918          (active (gnus-active group))
5919          ninfo)
5920     (when entry
5921       ;; First peel off all invalid article numbers.
5922       (when active
5923         (let ((ids articles)
5924               id first)
5925           (while (setq id (pop ids))
5926             (when (and first (> id (cdr active)))
5927               ;; We'll end up in this situation in one particular
5928               ;; obscure situation.  If you re-scan a group and get
5929               ;; a new article that is cross-posted to a different
5930               ;; group that has not been re-scanned, you might get
5931               ;; crossposted article that has a higher number than
5932               ;; Gnus believes possible.  So we re-activate this
5933               ;; group as well.  This might mean doing the
5934               ;; crossposting thingy will *increase* the number
5935               ;; of articles in some groups.  Tsk, tsk.
5936               (setq active (or (gnus-activate-group group) active)))
5937             (when (or (> id (cdr active))
5938                       (< id (car active)))
5939               (setq articles (delq id articles))))))
5940       ;; If the read list is nil, we init it.
5941       (if (and active
5942                (null (gnus-info-read info))
5943                (> (car active) 1))
5944           (setq ninfo (cons 1 (1- (car active))))
5945         (setq ninfo (gnus-info-read info)))
5946       ;; Then we add the read articles to the range.
5947       (gnus-add-to-range
5948        ninfo (setq articles (sort articles '<))))))
5949
5950 (defun gnus-group-make-articles-read (group articles)
5951   "Update the info of GROUP to say that ARTICLES are read."
5952   (let* ((num 0)
5953          (entry (gnus-group-entry group))
5954          (info (nth 2 entry))
5955          (active (gnus-active group))
5956          range)
5957     (when entry
5958       (setq range (gnus-compute-read-articles group articles))
5959       (with-current-buffer gnus-group-buffer
5960         (gnus-undo-register
5961           `(progn
5962              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5963              (gnus-info-set-read ',info ',(gnus-info-read info))
5964              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5965              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5966              (gnus-group-update-group ,group t))))
5967       ;; Add the read articles to the range.
5968       (gnus-info-set-read info range)
5969       (gnus-request-set-mark group (list (list range 'add '(read))))
5970       ;; Then we have to re-compute how many unread
5971       ;; articles there are in this group.
5972       (when active
5973         (cond
5974          ((not range)
5975           (setq num (- (1+ (cdr active)) (car active))))
5976          ((not (listp (cdr range)))
5977           (setq num (- (cdr active) (- (1+ (cdr range))
5978                                        (car range)))))
5979          (t
5980           (while range
5981             (if (numberp (car range))
5982                 (setq num (1+ num))
5983               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5984             (setq range (cdr range)))
5985           (setq num (- (cdr active) num))))
5986         ;; Update the number of unread articles.
5987         (setcar entry num)
5988         ;; Update the group buffer.
5989         (unless (gnus-ephemeral-group-p group)
5990           (gnus-group-update-group group t))))))
5991
5992 (defvar gnus-newsgroup-none-id 0)
5993
5994 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5995   (let ((cur nntp-server-buffer)
5996         (dependencies
5997          (or dependencies
5998              (with-current-buffer gnus-summary-buffer
5999                gnus-newsgroup-dependencies)))
6000         headers id end ref number
6001         (mail-parse-charset gnus-newsgroup-charset)
6002         (mail-parse-ignored-charsets
6003          (save-excursion (condition-case nil
6004                              (set-buffer gnus-summary-buffer)
6005                            (error))
6006                          gnus-newsgroup-ignored-charsets)))
6007     (save-excursion
6008       (set-buffer nntp-server-buffer)
6009       ;; Translate all TAB characters into SPACE characters.
6010       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6011       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6012       (ietf-drums-unfold-fws)
6013       (gnus-run-hooks 'gnus-parse-headers-hook)
6014       (let ((case-fold-search t)
6015             in-reply-to header p lines chars)
6016         (goto-char (point-min))
6017         ;; Search to the beginning of the next header.  Error messages
6018         ;; do not begin with 2 or 3.
6019         (while (re-search-forward "^[23][0-9]+ " nil t)
6020           (setq id nil
6021                 ref nil)
6022           ;; This implementation of this function, with nine
6023           ;; search-forwards instead of the one re-search-forward and
6024           ;; a case (which basically was the old function) is actually
6025           ;; about twice as fast, even though it looks messier.  You
6026           ;; can't have everything, I guess.  Speed and elegance
6027           ;; doesn't always go hand in hand.
6028           (setq
6029            header
6030            (vector
6031             ;; Number.
6032             (prog1
6033                 (setq number (read cur))
6034               (end-of-line)
6035               (setq p (point))
6036               (narrow-to-region (point)
6037                                 (or (and (search-forward "\n.\n" nil t)
6038                                          (- (point) 2))
6039                                     (point))))
6040             ;; Subject.
6041             (progn
6042               (goto-char p)
6043               (if (search-forward "\nsubject:" nil t)
6044                   (funcall gnus-decode-encoded-word-function
6045                            (nnheader-header-value))
6046                 "(none)"))
6047             ;; From.
6048             (progn
6049               (goto-char p)
6050               (if (search-forward "\nfrom:" nil t)
6051                   (funcall gnus-decode-encoded-word-function
6052                            (nnheader-header-value))
6053                 "(nobody)"))
6054             ;; Date.
6055             (progn
6056               (goto-char p)
6057               (if (search-forward "\ndate:" nil t)
6058                   (nnheader-header-value) ""))
6059             ;; Message-ID.
6060             (progn
6061               (goto-char p)
6062               (setq id (if (re-search-forward
6063                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6064                            ;; We do it this way to make sure the Message-ID
6065                            ;; is (somewhat) syntactically valid.
6066                            (buffer-substring (match-beginning 1)
6067                                              (match-end 1))
6068                          ;; If there was no message-id, we just fake one
6069                          ;; to make subsequent routines simpler.
6070                          (nnheader-generate-fake-message-id number))))
6071             ;; References.
6072             (progn
6073               (goto-char p)
6074               (if (search-forward "\nreferences:" nil t)
6075                   (progn
6076                     (setq end (point))
6077                     (prog1
6078                         (nnheader-header-value)
6079                       (setq ref
6080                             (buffer-substring
6081                              (progn
6082                                (end-of-line)
6083                                (search-backward ">" end t)
6084                                (1+ (point)))
6085                              (progn
6086                                (search-backward "<" end t)
6087                                (point))))))
6088                 ;; Get the references from the in-reply-to header if there
6089                 ;; were no references and the in-reply-to header looks
6090                 ;; promising.
6091                 (if (and (search-forward "\nin-reply-to:" nil t)
6092                          (setq in-reply-to (nnheader-header-value))
6093                          (string-match "<[^>]+>" in-reply-to))
6094                     (let (ref2)
6095                       (setq ref (substring in-reply-to (match-beginning 0)
6096                                            (match-end 0)))
6097                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6098                         (setq ref2 (substring in-reply-to (match-beginning 0)
6099                                               (match-end 0)))
6100                         (when (> (length ref2) (length ref))
6101                           (setq ref ref2)))
6102                       ref)
6103                   (setq ref nil))))
6104             ;; Chars.
6105             (progn
6106               (goto-char p)
6107               (if (search-forward "\nchars: " nil t)
6108                   (if (numberp (setq chars (ignore-errors (read cur))))
6109                       chars -1)
6110                 -1))
6111             ;; Lines.
6112             (progn
6113               (goto-char p)
6114               (if (search-forward "\nlines: " nil t)
6115                   (if (numberp (setq lines (ignore-errors (read cur))))
6116                       lines -1)
6117                 -1))
6118             ;; Xref.
6119             (progn
6120               (goto-char p)
6121               (and (search-forward "\nxref:" nil t)
6122                    (nnheader-header-value)))
6123             ;; Extra.
6124             (when gnus-extra-headers
6125               (let ((extra gnus-extra-headers)
6126                     out)
6127                 (while extra
6128                   (goto-char p)
6129                   (when (search-forward
6130                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6131                     (push (cons (car extra) (nnheader-header-value))
6132                           out))
6133                   (pop extra))
6134                 out))))
6135           (when (equal id ref)
6136             (setq ref nil))
6137
6138           (when gnus-alter-header-function
6139             (funcall gnus-alter-header-function header)
6140             (setq id (mail-header-id header)
6141                   ref (gnus-parent-id (mail-header-references header))))
6142
6143           (when (setq header
6144                       (gnus-dependencies-add-header
6145                        header dependencies force-new))
6146             (push header headers))
6147           (goto-char (point-max))
6148           (widen))
6149         (nreverse headers)))))
6150
6151 ;; Goes through the xover lines and returns a list of vectors
6152 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6153                                                   force-new dependencies
6154                                                   group also-fetch-heads)
6155   "Parse the news overview data in the server buffer.
6156 Return a list of headers that match SEQUENCE (see
6157 `nntp-retrieve-headers')."
6158   ;; Get the Xref when the users reads the articles since most/some
6159   ;; NNTP servers do not include Xrefs when using XOVER.
6160   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6161   (let ((mail-parse-charset gnus-newsgroup-charset)
6162         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6163         (cur nntp-server-buffer)
6164         (dependencies (or dependencies gnus-newsgroup-dependencies))
6165         (allp (cond
6166                ((eq gnus-read-all-available-headers t)
6167                 t)
6168                ((stringp gnus-read-all-available-headers)
6169                 (string-match gnus-read-all-available-headers group))
6170                (t
6171                 nil)))
6172         number headers header)
6173     (save-excursion
6174       (set-buffer nntp-server-buffer)
6175       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6176       ;; Allow the user to mangle the headers before parsing them.
6177       (gnus-run-hooks 'gnus-parse-headers-hook)
6178       (goto-char (point-min))
6179       (gnus-parse-without-error
6180         (while (and (or sequence allp)
6181                     (not (eobp)))
6182           (setq number (read cur))
6183           (when (not allp)
6184             (while (and sequence
6185                         (< (car sequence) number))
6186               (setq sequence (cdr sequence))))
6187           (when (and (or allp
6188                          (and sequence
6189                               (eq number (car sequence))))
6190                      (progn
6191                        (setq sequence (cdr sequence))
6192                        (setq header (inline
6193                                       (gnus-nov-parse-line
6194                                        number dependencies force-new)))))
6195             (push header headers))
6196           (forward-line 1)))
6197       ;; A common bug in inn is that if you have posted an article and
6198       ;; then retrieves the active file, it will answer correctly --
6199       ;; the new article is included.  However, a NOV entry for the
6200       ;; article may not have been generated yet, so this may fail.
6201       ;; We work around this problem by retrieving the last few
6202       ;; headers using HEAD.
6203       (if (or (not also-fetch-heads)
6204               (not sequence))
6205           ;; We (probably) got all the headers.
6206           (nreverse headers)
6207         (let ((gnus-nov-is-evil t))
6208           (nconc
6209            (nreverse headers)
6210            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6211              (gnus-get-newsgroup-headers))))))))
6212
6213 (defun gnus-article-get-xrefs ()
6214   "Fill in the Xref value in `gnus-current-headers', if necessary.
6215 This is meant to be called in `gnus-article-internal-prepare-hook'."
6216   (let ((headers (with-current-buffer gnus-summary-buffer
6217                    gnus-current-headers)))
6218     (or (not gnus-use-cross-reference)
6219         (not headers)
6220         (and (mail-header-xref headers)
6221              (not (string= (mail-header-xref headers) "")))
6222         (let ((case-fold-search t)
6223               xref)
6224           (save-restriction
6225             (nnheader-narrow-to-headers)
6226             (goto-char (point-min))
6227             (when (or (and (not (eobp))
6228                            (eq (downcase (char-after)) ?x)
6229                            (looking-at "Xref:"))
6230                       (search-forward "\nXref:" nil t))
6231               (goto-char (1+ (match-end 0)))
6232               (setq xref (buffer-substring (point) (point-at-eol)))
6233               (mail-header-set-xref headers xref)))))))
6234
6235 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6236   "Find article ID and insert the summary line for that article.
6237 OLD-HEADER can either be a header or a line number to insert
6238 the subject line on."
6239   (let* ((line (and (numberp old-header) old-header))
6240          (old-header (and (vectorp old-header) old-header))
6241          (header (cond ((and old-header use-old-header)
6242                         old-header)
6243                        ((and (numberp id)
6244                              (gnus-number-to-header id))
6245                         (gnus-number-to-header id))
6246                        (t
6247                         (gnus-read-header id))))
6248          (number (and (numberp id) id))
6249          d)
6250     (when header
6251       ;; Rebuild the thread that this article is part of and go to the
6252       ;; article we have fetched.
6253       (when (and (not gnus-show-threads)
6254                  old-header)
6255         (when (and number
6256                    (setq d (gnus-data-find (mail-header-number old-header))))
6257           (goto-char (gnus-data-pos d))
6258           (gnus-data-remove
6259            number
6260            (- (point-at-bol)
6261               (prog1
6262                   (1+ (point-at-eol))
6263                 (gnus-delete-line))))))
6264       ;; Remove list identifiers from subject.
6265       (when gnus-list-identifiers
6266         (let ((gnus-newsgroup-headers (list header)))
6267           (gnus-summary-remove-list-identifiers)))
6268       (when old-header
6269         (mail-header-set-number header (mail-header-number old-header)))
6270       (setq gnus-newsgroup-sparse
6271             (delq (setq number (mail-header-number header))
6272                   gnus-newsgroup-sparse))
6273       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6274       (push number gnus-newsgroup-limit)
6275       (gnus-rebuild-thread (mail-header-id header) line)
6276       (gnus-summary-goto-subject number nil t))
6277     (when (and (numberp number)
6278                (> number 0))
6279       ;; We have to update the boundaries even if we can't fetch the
6280       ;; article if ID is a number -- so that the next `P' or `N'
6281       ;; command will fetch the previous (or next) article even
6282       ;; if the one we tried to fetch this time has been canceled.
6283       (when (> number gnus-newsgroup-end)
6284         (setq gnus-newsgroup-end number))
6285       (when (< number gnus-newsgroup-begin)
6286         (setq gnus-newsgroup-begin number))
6287       (setq gnus-newsgroup-unselected
6288             (delq number gnus-newsgroup-unselected)))
6289     ;; Report back a success?
6290     (and header (mail-header-number header))))
6291
6292 ;;; Process/prefix in the summary buffer
6293
6294 (defun gnus-summary-work-articles (n)
6295   "Return a list of articles to be worked upon.
6296 The prefix argument, the list of process marked articles, and the
6297 current article will be taken into consideration."
6298   (save-excursion
6299     (set-buffer gnus-summary-buffer)
6300     (cond
6301      (n
6302       ;; A numerical prefix has been given.
6303       (setq n (prefix-numeric-value n))
6304       (let ((backward (< n 0))
6305             (n (abs (prefix-numeric-value n)))
6306             articles article)
6307         (save-excursion
6308           (while
6309               (and (> n 0)
6310                    (push (setq article (gnus-summary-article-number))
6311                          articles)
6312                    (if backward
6313                        (gnus-summary-find-prev nil article)
6314                      (gnus-summary-find-next nil article)))
6315             (decf n)))
6316         (nreverse articles)))
6317      ((and (gnus-region-active-p) (mark))
6318       (message "region active")
6319       ;; Work on the region between point and mark.
6320       (let ((max (max (point) (mark)))
6321             articles article)
6322         (save-excursion
6323           (goto-char (min (point) (mark)))
6324           (while
6325               (and
6326                (push (setq article (gnus-summary-article-number)) articles)
6327                (gnus-summary-find-next nil article)
6328                (< (point) max)))
6329           (nreverse articles))))
6330      (gnus-newsgroup-processable
6331       ;; There are process-marked articles present.
6332       ;; Save current state.
6333       (gnus-summary-save-process-mark)
6334       ;; Return the list.
6335       (reverse gnus-newsgroup-processable))
6336      (t
6337       ;; Just return the current article.
6338       (list (gnus-summary-article-number))))))
6339
6340 (defmacro gnus-summary-iterate (arg &rest forms)
6341   "Iterate over the process/prefixed articles and do FORMS.
6342 ARG is the interactive prefix given to the command.  FORMS will be
6343 executed with point over the summary line of the articles."
6344   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6345     `(let ((,articles (gnus-summary-work-articles ,arg)))
6346        (while ,articles
6347          (gnus-summary-goto-subject (car ,articles))
6348          ,@forms
6349          (pop ,articles)))))
6350
6351 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6352 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6353
6354 (defun gnus-summary-save-process-mark ()
6355   "Push the current set of process marked articles on the stack."
6356   (interactive)
6357   (push (copy-sequence gnus-newsgroup-processable)
6358         gnus-newsgroup-process-stack))
6359
6360 (defun gnus-summary-kill-process-mark ()
6361   "Push the current set of process marked articles on the stack and unmark."
6362   (interactive)
6363   (gnus-summary-save-process-mark)
6364   (gnus-summary-unmark-all-processable))
6365
6366 (defun gnus-summary-yank-process-mark ()
6367   "Pop the last process mark state off the stack and restore it."
6368   (interactive)
6369   (unless gnus-newsgroup-process-stack
6370     (error "Empty mark stack"))
6371   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6372
6373 (defun gnus-summary-process-mark-set (set)
6374   "Make SET into the current process marked articles."
6375   (gnus-summary-unmark-all-processable)
6376   (mapc 'gnus-summary-set-process-mark set))
6377
6378 ;;; Searching and stuff
6379
6380 (defun gnus-summary-search-group (&optional backward use-level)
6381   "Search for next unread newsgroup.
6382 If optional argument BACKWARD is non-nil, search backward instead."
6383   (save-excursion
6384     (set-buffer gnus-group-buffer)
6385     (when (gnus-group-search-forward
6386            backward nil (if use-level (gnus-group-group-level) nil))
6387       (gnus-group-group-name))))
6388
6389 (defun gnus-summary-best-group (&optional exclude-group)
6390   "Find the name of the best unread group.
6391 If EXCLUDE-GROUP, do not go to this group."
6392   (with-current-buffer gnus-group-buffer
6393     (save-excursion
6394       (gnus-group-best-unread-group exclude-group))))
6395
6396 (defun gnus-summary-find-next (&optional unread article backward)
6397   (if backward
6398       (gnus-summary-find-prev unread article)
6399     (let* ((dummy (gnus-summary-article-intangible-p))
6400            (article (or article (gnus-summary-article-number)))
6401            (data (gnus-data-find-list article))
6402            result)
6403       (when (and (not dummy)
6404                  (or (not gnus-summary-check-current)
6405                      (not unread)
6406                      (not (gnus-data-unread-p (car data)))))
6407         (setq data (cdr data)))
6408       (when (setq result
6409                   (if unread
6410                       (progn
6411                         (while data
6412                           (unless (memq (gnus-data-number (car data))
6413                                         (cond
6414                                          ((eq gnus-auto-goto-ignores
6415                                               'always-undownloaded)
6416                                           gnus-newsgroup-undownloaded)
6417                                          (gnus-plugged
6418                                           nil)
6419                                          ((eq gnus-auto-goto-ignores
6420                                               'unfetched)
6421                                           gnus-newsgroup-unfetched)
6422                                          ((eq gnus-auto-goto-ignores
6423                                               'undownloaded)
6424                                           gnus-newsgroup-undownloaded)))
6425                             (when (gnus-data-unread-p (car data))
6426                               (setq result (car data)
6427                                     data nil)))
6428                           (setq data (cdr data)))
6429                         result)
6430                     (car data)))
6431         (goto-char (gnus-data-pos result))
6432         (gnus-data-number result)))))
6433
6434 (defun gnus-summary-find-prev (&optional unread article)
6435   (let* ((eobp (eobp))
6436          (article (or article (gnus-summary-article-number)))
6437          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6438          result)
6439     (when (and (not eobp)
6440                (or (not gnus-summary-check-current)
6441                    (not unread)
6442                    (not (gnus-data-unread-p (car data)))))
6443       (setq data (cdr data)))
6444     (when (setq result
6445                 (if unread
6446                     (progn
6447                       (while data
6448                         (unless (memq (gnus-data-number (car data))
6449                                       (cond
6450                                        ((eq gnus-auto-goto-ignores
6451                                             'always-undownloaded)
6452                                         gnus-newsgroup-undownloaded)
6453                                        (gnus-plugged
6454                                         nil)
6455                                        ((eq gnus-auto-goto-ignores
6456                                             'unfetched)
6457                                         gnus-newsgroup-unfetched)
6458                                        ((eq gnus-auto-goto-ignores
6459                                             'undownloaded)
6460                                         gnus-newsgroup-undownloaded)))
6461                           (when (gnus-data-unread-p (car data))
6462                             (setq result (car data)
6463                                   data nil)))
6464                         (setq data (cdr data)))
6465                       result)
6466                   (car data)))
6467       (goto-char (gnus-data-pos result))
6468       (gnus-data-number result))))
6469
6470 (defun gnus-summary-find-subject (subject &optional unread backward article)
6471   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6472          (article (or article (gnus-summary-article-number)))
6473          (articles (gnus-data-list backward))
6474          (arts (gnus-data-find-list article articles))
6475          result)
6476     (when (or (not gnus-summary-check-current)
6477               (not unread)
6478               (not (gnus-data-unread-p (car arts))))
6479       (setq arts (cdr arts)))
6480     (while arts
6481       (and (or (not unread)
6482                (gnus-data-unread-p (car arts)))
6483            (vectorp (gnus-data-header (car arts)))
6484            (gnus-subject-equal
6485             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6486            (setq result (car arts)
6487                  arts nil))
6488       (setq arts (cdr arts)))
6489     (and result
6490          (goto-char (gnus-data-pos result))
6491          (gnus-data-number result))))
6492
6493 (defun gnus-summary-search-forward (&optional unread subject backward)
6494   "Search forward for an article.
6495 If UNREAD, look for unread articles.  If SUBJECT, look for
6496 articles with that subject.  If BACKWARD, search backward instead."
6497   (cond (subject (gnus-summary-find-subject subject unread backward))
6498         (backward (gnus-summary-find-prev unread))
6499         (t (gnus-summary-find-next unread))))
6500
6501 (defun gnus-recenter (&optional n)
6502   "Center point in window and redisplay frame.
6503 Also do horizontal recentering."
6504   (interactive "P")
6505   (when (and gnus-auto-center-summary
6506              (not (eq gnus-auto-center-summary 'vertical)))
6507     (gnus-horizontal-recenter))
6508   (recenter n))
6509
6510 (defun gnus-summary-recenter ()
6511   "Center point in the summary window.
6512 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6513 displayed, no centering will be performed."
6514   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6515   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6516   (interactive)
6517   ;; The user has to want it.
6518   (when gnus-auto-center-summary
6519     (let* ((top (cond ((< (window-height) 4) 0)
6520                       ((< (window-height) 7) 1)
6521                       (t (if (numberp gnus-auto-center-summary)
6522                              gnus-auto-center-summary
6523                            (/ (1- (window-height)) 2)))))
6524            (height (1- (window-height)))
6525            (bottom (save-excursion (goto-char (point-max))
6526                                    (forward-line (- height))
6527                                    (point)))
6528            (window (get-buffer-window (current-buffer))))
6529       (when (get-buffer-window gnus-article-buffer)
6530         ;; Only do recentering when the article buffer is displayed,
6531         ;; Set the window start to either `bottom', which is the biggest
6532         ;; possible valid number, or the second line from the top,
6533         ;; whichever is the least.
6534         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6535           (if (> bottom top-pos)
6536               ;; Keep the second line from the top visible
6537               (set-window-start window top-pos t)
6538             ;; Try to keep the bottom line visible; if it's partially
6539             ;; obscured, either scroll one more line to make it fully
6540             ;; visible, or revert to using TOP-POS.
6541             (save-excursion
6542               (goto-char (point-max))
6543               (forward-line -1)
6544               (let ((last-line-start (point)))
6545                 (goto-char bottom)
6546                 (set-window-start window (point) t)
6547                 (when (not (pos-visible-in-window-p last-line-start window))
6548                   (forward-line 1)
6549                   (set-window-start window (min (point) top-pos) t)))))))
6550       ;; Do horizontal recentering while we're at it.
6551       (when (and (get-buffer-window (current-buffer) t)
6552                  (not (eq gnus-auto-center-summary 'vertical)))
6553         (let ((selected (selected-window)))
6554           (select-window (get-buffer-window (current-buffer) t))
6555           (gnus-summary-position-point)
6556           (gnus-horizontal-recenter)
6557           (select-window selected))))))
6558
6559 (defun gnus-summary-jump-to-group (newsgroup)
6560   "Move point to NEWSGROUP in group mode buffer."
6561   ;; Keep update point of group mode buffer if visible.
6562   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6563       (save-window-excursion
6564         ;; Take care of tree window mode.
6565         (when (get-buffer-window gnus-group-buffer)
6566           (pop-to-buffer gnus-group-buffer))
6567         (gnus-group-jump-to-group newsgroup))
6568     (save-excursion
6569       ;; Take care of tree window mode.
6570       (if (get-buffer-window gnus-group-buffer)
6571           (pop-to-buffer gnus-group-buffer)
6572         (set-buffer gnus-group-buffer))
6573       (gnus-group-jump-to-group newsgroup))))
6574
6575 ;; This function returns a list of article numbers based on the
6576 ;; difference between the ranges of read articles in this group and
6577 ;; the range of active articles.
6578 (defun gnus-list-of-unread-articles (group)
6579   (let* ((read (gnus-info-read (gnus-get-info group)))
6580          (active (or (gnus-active group) (gnus-activate-group group)))
6581          (last (or (cdr active)
6582                    (error "Group %s couldn't be activated " group)))
6583          first nlast unread)
6584     ;; If none are read, then all are unread.
6585     (if (not read)
6586         (setq first (car active))
6587       ;; If the range of read articles is a single range, then the
6588       ;; first unread article is the article after the last read
6589       ;; article.  Sounds logical, doesn't it?
6590       (if (and (not (listp (cdr read)))
6591                (or (< (car read) (car active))
6592                    (progn (setq read (list read))
6593                           nil)))
6594           (setq first (max (car active) (1+ (cdr read))))
6595         ;; `read' is a list of ranges.
6596         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6597                                   (caar read)))
6598                   1)
6599           (setq first (car active)))
6600         (while read
6601           (when first
6602             (while (< first nlast)
6603               (setq unread (cons first unread)
6604                     first (1+ first))))
6605           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6606           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6607           (setq read (cdr read)))))
6608     ;; And add the last unread articles.
6609     (while (<= first last)
6610       (setq unread (cons first unread)
6611             first (1+ first)))
6612     ;; Return the list of unread articles.
6613     (delq 0 (nreverse unread))))
6614
6615 (defun gnus-list-of-read-articles (group)
6616   "Return a list of unread, unticked and non-dormant articles."
6617   (let* ((info (gnus-get-info group))
6618          (marked (gnus-info-marks info))
6619          (active (gnus-active group)))
6620     (and info active
6621          (gnus-list-range-difference
6622           (gnus-list-range-difference
6623            (gnus-sorted-complement
6624             (gnus-uncompress-range active)
6625             (gnus-list-of-unread-articles group))
6626            (cdr (assq 'dormant marked)))
6627           (cdr (assq 'tick marked))))))
6628
6629 ;; This function returns a sequence of article numbers based on the
6630 ;; difference between the ranges of read articles in this group and
6631 ;; the range of active articles.
6632 (defun gnus-sequence-of-unread-articles (group)
6633   (let* ((read (gnus-info-read (gnus-get-info group)))
6634          (active (or (gnus-active group) (gnus-activate-group group)))
6635          (last (cdr active))
6636          first nlast unread)
6637     ;; If none are read, then all are unread.
6638     (if (not read)
6639         (setq first (car active))
6640       ;; If the range of read articles is a single range, then the
6641       ;; first unread article is the article after the last read
6642       ;; article.  Sounds logical, doesn't it?
6643       (if (and (not (listp (cdr read)))
6644                (or (< (car read) (car active))
6645                    (progn (setq read (list read))
6646                           nil)))
6647           (setq first (max (car active) (1+ (cdr read))))
6648         ;; `read' is a list of ranges.
6649         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6650                                   (caar read)))
6651                   1)
6652           (setq first (car active)))
6653         (while read
6654           (when first
6655             (push (cons first nlast) unread))
6656           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6657           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6658           (setq read (cdr read)))))
6659     ;; And add the last unread articles.
6660     (cond ((not (and first last))
6661            nil)
6662           ((< first last)
6663            (push (cons first last) unread))
6664           ((= first last)
6665            (push first unread)))
6666     ;; Return the sequence of unread articles.
6667     (delq 0 (nreverse unread))))
6668
6669 ;; Various summary commands
6670
6671 (defun gnus-summary-select-article-buffer ()
6672   "Reconfigure windows to show article buffer."
6673   (interactive)
6674   (if (not (gnus-buffer-live-p gnus-article-buffer))
6675       (error "There is no article buffer for this summary buffer")
6676     (gnus-configure-windows 'article)
6677     (select-window (get-buffer-window gnus-article-buffer))))
6678
6679 (defun gnus-summary-universal-argument (arg)
6680   "Perform any operation on all articles that are process/prefixed."
6681   (interactive "P")
6682   (let ((articles (gnus-summary-work-articles arg))
6683         func article)
6684     (if (eq
6685          (setq
6686           func
6687           (key-binding
6688            (read-key-sequence
6689             (substitute-command-keys
6690              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6691          'undefined)
6692         (gnus-error 1 "Undefined key")
6693       (save-excursion
6694         (while articles
6695           (gnus-summary-goto-subject (setq article (pop articles)))
6696           (let (gnus-newsgroup-processable)
6697             (command-execute func))
6698           (gnus-summary-remove-process-mark article)))))
6699   (gnus-summary-position-point))
6700
6701 (defun gnus-summary-toggle-truncation (&optional arg)
6702   "Toggle truncation of summary lines.
6703 With ARG, turn line truncation on if ARG is positive."
6704   (interactive "P")
6705   (setq truncate-lines
6706         (if (null arg) (not truncate-lines)
6707           (> (prefix-numeric-value arg) 0)))
6708   (redraw-display))
6709
6710 (defun gnus-summary-find-for-reselect ()
6711   "Return the number of an article to stay on across a reselect.
6712 The current article is considered, then following articles, then previous
6713 articles.  An article is sought which is not cancelled and isn't a temporary
6714 insertion from another group.  If there's no such then return a dummy 0."
6715   (let (found)
6716     (dolist (rev '(nil t))
6717       (unless found      ; don't demand the reverse list if we don't need it
6718         (let ((data (gnus-data-find-list
6719                      (gnus-summary-article-number) (gnus-data-list rev))))
6720           (while (and data (not found))
6721             (if (and (< 0 (gnus-data-number (car data)))
6722                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6723                 (setq found (gnus-data-number (car data))))
6724             (setq data (cdr data))))))
6725     (or found 0)))
6726
6727 (defun gnus-summary-reselect-current-group (&optional all rescan)
6728   "Exit and then reselect the current newsgroup.
6729 The prefix argument ALL means to select all articles."
6730   (interactive "P")
6731   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6732     (error "Ephemeral groups can't be reselected"))
6733   (let ((current-subject (gnus-summary-find-for-reselect))
6734         (group gnus-newsgroup-name))
6735     (setq gnus-newsgroup-begin nil)
6736     (gnus-summary-exit nil 'leave-hidden)
6737     ;; We have to adjust the point of group mode buffer because
6738     ;; point was moved to the next unread newsgroup by exiting.
6739     (gnus-summary-jump-to-group group)
6740     (when rescan
6741       (save-excursion
6742         (gnus-group-get-new-news-this-group 1)))
6743     (gnus-group-read-group all t)
6744     (gnus-summary-goto-subject current-subject nil t)))
6745
6746 (defun gnus-summary-rescan-group (&optional all)
6747   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6748   (interactive "P")
6749   (gnus-summary-reselect-current-group all t))
6750
6751 (defun gnus-summary-update-info (&optional non-destructive)
6752   (save-excursion
6753     (let ((group gnus-newsgroup-name))
6754       (when group
6755         (when gnus-newsgroup-kill-headers
6756           (setq gnus-newsgroup-killed
6757                 (gnus-compress-sequence
6758                  (gnus-sorted-union
6759                   (gnus-list-range-intersection
6760                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6761                   gnus-newsgroup-unreads)
6762                  t)))
6763         (unless (listp (cdr gnus-newsgroup-killed))
6764           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6765         (let ((headers gnus-newsgroup-headers))
6766           ;; Set the new ranges of read articles.
6767           (with-current-buffer gnus-group-buffer
6768             (gnus-undo-force-boundary))
6769           (gnus-update-read-articles
6770            group (gnus-sorted-union
6771                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6772           ;; Set the current article marks.
6773           (let ((gnus-newsgroup-scored
6774                  (if (and (not gnus-save-score)
6775                           (not non-destructive))
6776                      nil
6777                    gnus-newsgroup-scored)))
6778             (save-excursion
6779               (gnus-update-marks)))
6780           ;; Do the cross-ref thing.
6781           (when gnus-use-cross-reference
6782             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6783           ;; Do not switch windows but change the buffer to work.
6784           (set-buffer gnus-group-buffer)
6785           (unless (gnus-ephemeral-group-p group)
6786             (gnus-group-update-group group)))))))
6787
6788 (defun gnus-summary-save-newsrc (&optional force)
6789   "Save the current number of read/marked articles in the dribble buffer.
6790 The dribble buffer will then be saved.
6791 If FORCE (the prefix), also save the .newsrc file(s)."
6792   (interactive "P")
6793   (gnus-summary-update-info t)
6794   (if force
6795       (gnus-save-newsrc-file)
6796     (gnus-dribble-save)))
6797
6798 (defun gnus-summary-exit (&optional temporary leave-hidden)
6799   "Exit reading current newsgroup, and then return to group selection mode.
6800 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6801   (interactive)
6802   (gnus-set-global-variables)
6803   (when (gnus-buffer-live-p gnus-article-buffer)
6804     (save-excursion
6805       (set-buffer gnus-article-buffer)
6806       (mm-destroy-parts gnus-article-mime-handles)
6807       ;; Set it to nil for safety reason.
6808       (setq gnus-article-mime-handle-alist nil)
6809       (setq gnus-article-mime-handles nil)))
6810   (gnus-kill-save-kill-buffer)
6811   (gnus-async-halt-prefetch)
6812   (let* ((group gnus-newsgroup-name)
6813          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6814          (gnus-group-is-exiting-p t)
6815          (mode major-mode)
6816          (group-point nil)
6817          (buf (current-buffer)))
6818     (unless quit-config
6819       ;; Do adaptive scoring, and possibly save score files.
6820       (when gnus-newsgroup-adaptive
6821         (gnus-score-adaptive))
6822       (when gnus-use-scoring
6823         (gnus-score-save)))
6824     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6825     ;; If we have several article buffers, we kill them at exit.
6826     (unless gnus-single-article-buffer
6827       (gnus-kill-buffer gnus-original-article-buffer)
6828       (setq gnus-article-current nil))
6829     (when gnus-use-cache
6830       (gnus-cache-possibly-remove-articles)
6831       (gnus-cache-save-buffers))
6832     (gnus-async-prefetch-remove-group group)
6833     (when gnus-suppress-duplicates
6834       (gnus-dup-enter-articles))
6835     (when gnus-use-trees
6836       (gnus-tree-close group))
6837     (when gnus-use-cache
6838       (gnus-cache-write-active))
6839     ;; Remove entries for this group.
6840     (nnmail-purge-split-history (gnus-group-real-name group))
6841     ;; Make all changes in this group permanent.
6842     (unless quit-config
6843       (gnus-run-hooks 'gnus-exit-group-hook)
6844       (gnus-summary-update-info))
6845     (gnus-close-group group)
6846     ;; Make sure where we were, and go to next newsgroup.
6847     (set-buffer gnus-group-buffer)
6848     (unless quit-config
6849       (gnus-group-jump-to-group group))
6850     (gnus-run-hooks 'gnus-summary-exit-hook)
6851     (unless (or quit-config
6852                 ;; If this group has disappeared from the summary
6853                 ;; buffer, don't skip forwards.
6854                 (not (string= group (gnus-group-group-name))))
6855       (gnus-group-next-unread-group 1))
6856     (setq group-point (point))
6857     (if temporary
6858         nil                             ;Nothing to do.
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       (set-buffer buf)
6865       (if (not gnus-kill-summary-on-exit)
6866           (progn
6867             (gnus-deaden-summary)
6868             (setq mode nil))
6869         ;; We set all buffer-local variables to nil.  It is unclear why
6870         ;; this is needed, but if we don't, buffer-local variables are
6871         ;; not garbage-collected, it seems.  This would the lead to en
6872         ;; ever-growing Emacs.
6873         (gnus-summary-clear-local-variables)
6874         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6875           (gnus-summary-clear-local-variables))
6876         (when (get-buffer gnus-article-buffer)
6877           (bury-buffer gnus-article-buffer))
6878         ;; Return to group mode buffer.
6879         (when (eq mode 'gnus-summary-mode)
6880           (gnus-kill-buffer buf)))
6881       (setq gnus-current-select-method gnus-select-method)
6882       (set-buffer gnus-group-buffer)
6883       (if quit-config
6884           (gnus-handle-ephemeral-exit quit-config)
6885         (goto-char group-point)
6886         ;; If gnus-group-buffer is already displayed, make sure we also move
6887         ;; the cursor in the window that displays it.
6888         (let ((win (get-buffer-window (current-buffer) 0)))
6889           (if win (set-window-point win (point))))
6890         (unless leave-hidden
6891           (gnus-configure-windows 'group 'force)))
6892       ;; Clear the current group name.
6893       (unless quit-config
6894         (setq gnus-newsgroup-name nil)))))
6895
6896 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6897 (defun gnus-summary-exit-no-update (&optional no-questions)
6898   "Quit reading current newsgroup without updating read article info."
6899   (interactive)
6900   (let* ((group gnus-newsgroup-name)
6901          (gnus-group-is-exiting-p t)
6902          (gnus-group-is-exiting-without-update-p t)
6903          (quit-config (gnus-group-quit-config group)))
6904     (when (or no-questions
6905               gnus-expert-user
6906               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6907       (gnus-async-halt-prefetch)
6908       (run-hooks 'gnus-summary-prepare-exit-hook)
6909       (when (gnus-buffer-live-p gnus-article-buffer)
6910         (save-excursion
6911           (set-buffer gnus-article-buffer)
6912           (mm-destroy-parts gnus-article-mime-handles)
6913           ;; Set it to nil for safety reason.
6914           (setq gnus-article-mime-handle-alist nil)
6915           (setq gnus-article-mime-handles nil)))
6916       ;; If we have several article buffers, we kill them at exit.
6917       (unless gnus-single-article-buffer
6918         (gnus-kill-buffer gnus-article-buffer)
6919         (gnus-kill-buffer gnus-original-article-buffer)
6920         (setq gnus-article-current nil))
6921       (if (not gnus-kill-summary-on-exit)
6922           (gnus-deaden-summary)
6923         (gnus-close-group group)
6924         (gnus-summary-clear-local-variables)
6925         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6926           (gnus-summary-clear-local-variables))
6927         (gnus-kill-buffer gnus-summary-buffer))
6928       (unless gnus-single-article-buffer
6929         (setq gnus-article-current nil))
6930       (when gnus-use-trees
6931         (gnus-tree-close group))
6932       (gnus-async-prefetch-remove-group group)
6933       (when (get-buffer gnus-article-buffer)
6934         (bury-buffer gnus-article-buffer))
6935       ;; Return to the group buffer.
6936       (gnus-configure-windows 'group 'force)
6937       ;; Clear the current group name.
6938       (setq gnus-newsgroup-name nil)
6939       (unless (gnus-ephemeral-group-p group)
6940         (gnus-group-update-group group))
6941       (when (equal (gnus-group-group-name) group)
6942         (gnus-group-next-unread-group 1))
6943       (when quit-config
6944         (gnus-handle-ephemeral-exit quit-config)))))
6945
6946 (defun gnus-handle-ephemeral-exit (quit-config)
6947   "Handle movement when leaving an ephemeral group.
6948 The state which existed when entering the ephemeral is reset."
6949   (if (not (buffer-name (car quit-config)))
6950       (gnus-configure-windows 'group 'force)
6951     (set-buffer (car quit-config))
6952     (cond ((eq major-mode 'gnus-summary-mode)
6953            (gnus-set-global-variables))
6954           ((eq major-mode 'gnus-article-mode)
6955            (save-excursion
6956              ;; The `gnus-summary-buffer' variable may point
6957              ;; to the old summary buffer when using a single
6958              ;; article buffer.
6959              (unless (gnus-buffer-live-p gnus-summary-buffer)
6960                (set-buffer gnus-group-buffer))
6961              (set-buffer gnus-summary-buffer)
6962              (gnus-set-global-variables))))
6963     (if (or (eq (cdr quit-config) 'article)
6964             (eq (cdr quit-config) 'pick))
6965         (progn
6966           ;; The current article may be from the ephemeral group
6967           ;; thus it is best that we reload this article
6968           ;;
6969           ;; If we're exiting from a large digest, this can be
6970           ;; extremely slow.  So, it's better not to reload it. -- jh.
6971           ;;(gnus-summary-show-article)
6972           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6973               (gnus-configure-windows 'pick 'force)
6974             (gnus-configure-windows (cdr quit-config) 'force)))
6975       (gnus-configure-windows (cdr quit-config) 'force))
6976     (when (eq major-mode 'gnus-summary-mode)
6977       (gnus-summary-next-subject 1 nil t)
6978       (gnus-summary-recenter)
6979       (gnus-summary-position-point))))
6980
6981 ;;; Dead summaries.
6982
6983 (defvar gnus-dead-summary-mode-map nil)
6984
6985 (unless gnus-dead-summary-mode-map
6986   (setq gnus-dead-summary-mode-map (make-keymap))
6987   (suppress-keymap gnus-dead-summary-mode-map)
6988   (substitute-key-definition
6989    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6990   (dolist (key '("\C-d" "\r" "\177" [delete]))
6991     (define-key gnus-dead-summary-mode-map
6992       key 'gnus-summary-wake-up-the-dead))
6993   (dolist (key '("q" "Q"))
6994     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6995
6996 (defvar gnus-dead-summary-mode nil
6997   "Minor mode for Gnus summary buffers.")
6998
6999 (defun gnus-dead-summary-mode (&optional arg)
7000   "Minor mode for Gnus summary buffers."
7001   (interactive "P")
7002   (when (eq major-mode 'gnus-summary-mode)
7003     (make-local-variable 'gnus-dead-summary-mode)
7004     (setq gnus-dead-summary-mode
7005           (if (null arg) (not gnus-dead-summary-mode)
7006             (> (prefix-numeric-value arg) 0)))
7007     (when gnus-dead-summary-mode
7008       (add-minor-mode
7009        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7010
7011 (defun gnus-deaden-summary ()
7012   "Make the current summary buffer into a dead summary buffer."
7013   ;; Kill any previous dead summary buffer.
7014   (when (and gnus-dead-summary
7015              (buffer-name gnus-dead-summary))
7016     (with-current-buffer gnus-dead-summary
7017       (when gnus-dead-summary-mode
7018         (kill-buffer (current-buffer)))))
7019   ;; Make this the current dead summary.
7020   (setq gnus-dead-summary (current-buffer))
7021   (gnus-dead-summary-mode 1)
7022   (let ((name (buffer-name)))
7023     (when (string-match "Summary" name)
7024       (rename-buffer
7025        (concat (substring name 0 (match-beginning 0)) "Dead "
7026                (substring name (match-beginning 0)))
7027        t)
7028       (bury-buffer))))
7029
7030 (defun gnus-kill-or-deaden-summary (buffer)
7031   "Kill or deaden the summary BUFFER."
7032   (save-excursion
7033     (when (and (buffer-name buffer)
7034                (not gnus-single-article-buffer))
7035       (with-current-buffer buffer
7036         (gnus-kill-buffer gnus-article-buffer)
7037         (gnus-kill-buffer gnus-original-article-buffer)))
7038     (cond
7039      ;; Kill the buffer.
7040      (gnus-kill-summary-on-exit
7041       (when (and gnus-use-trees
7042                  (gnus-buffer-exists-p buffer))
7043         (save-excursion
7044           (set-buffer buffer)
7045           (gnus-tree-close gnus-newsgroup-name)))
7046       (gnus-kill-buffer buffer))
7047      ;; Deaden the buffer.
7048      ((gnus-buffer-exists-p buffer)
7049       (save-excursion
7050         (set-buffer buffer)
7051         (gnus-deaden-summary))))))
7052
7053 (defun gnus-summary-wake-up-the-dead (&rest args)
7054   "Wake up the dead summary buffer."
7055   (interactive)
7056   (gnus-dead-summary-mode -1)
7057   (let ((name (buffer-name)))
7058     (when (string-match "Dead " name)
7059       (rename-buffer
7060        (concat (substring name 0 (match-beginning 0))
7061                (substring name (match-end 0)))
7062        t)))
7063   (gnus-message 3 "This dead summary is now alive again"))
7064
7065 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7066 (defun gnus-summary-fetch-faq (&optional faq-dir)
7067   "Fetch the FAQ for the current group.
7068 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7069 in."
7070   (interactive
7071    (list
7072     (when current-prefix-arg
7073       (completing-read
7074        "FAQ dir: " (and (listp gnus-group-faq-directory)
7075                         (mapcar 'list
7076                                 gnus-group-faq-directory))))))
7077   (let (gnus-faq-buffer)
7078     (when (setq gnus-faq-buffer
7079                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7080       (gnus-configure-windows 'summary-faq))))
7081
7082 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7083 (defun gnus-summary-describe-group (&optional force)
7084   "Describe the current newsgroup."
7085   (interactive "P")
7086   (gnus-group-describe-group force gnus-newsgroup-name))
7087
7088 (defun gnus-summary-describe-briefly ()
7089   "Describe summary mode commands briefly."
7090   (interactive)
7091   (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")))
7092
7093 ;; Walking around group mode buffer from summary mode.
7094
7095 (defun gnus-summary-next-group (&optional no-article target-group backward)
7096   "Exit current newsgroup and then select next unread newsgroup.
7097 If prefix argument NO-ARTICLE is non-nil, no article is selected
7098 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7099 previous group instead."
7100   (interactive "P")
7101   ;; Stop pre-fetching.
7102   (gnus-async-halt-prefetch)
7103   (let ((current-group gnus-newsgroup-name)
7104         (current-buffer (current-buffer))
7105         entered)
7106     ;; First we semi-exit this group to update Xrefs and all variables.
7107     ;; We can't do a real exit, because the window conf must remain
7108     ;; the same in case the user is prompted for info, and we don't
7109     ;; want the window conf to change before that...
7110     (gnus-summary-exit t)
7111     (while (not entered)
7112       ;; Then we find what group we are supposed to enter.
7113       (set-buffer gnus-group-buffer)
7114       (gnus-group-jump-to-group current-group)
7115       (setq target-group
7116             (or target-group
7117                 (if (eq gnus-keep-same-level 'best)
7118                     (gnus-summary-best-group gnus-newsgroup-name)
7119                   (gnus-summary-search-group backward gnus-keep-same-level))))
7120       (if (not target-group)
7121           ;; There are no further groups, so we return to the group
7122           ;; buffer.
7123           (progn
7124             (gnus-message 5 "Returning to the group buffer")
7125             (setq entered t)
7126             (when (gnus-buffer-live-p current-buffer)
7127               (set-buffer current-buffer)
7128               (gnus-summary-exit))
7129             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7130         ;; We try to enter the target group.
7131         (gnus-group-jump-to-group target-group)
7132         (let ((unreads (gnus-group-group-unread)))
7133           (if (and (or (eq t unreads)
7134                        (and unreads (not (zerop unreads))))
7135                    (gnus-summary-read-group
7136                     target-group nil no-article
7137                     (and (buffer-name current-buffer) current-buffer)
7138                     nil backward))
7139               (setq entered t)
7140             (setq current-group target-group
7141                   target-group nil)))))))
7142
7143 (defun gnus-summary-prev-group (&optional no-article)
7144   "Exit current newsgroup and then select previous unread newsgroup.
7145 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7146   (interactive "P")
7147   (gnus-summary-next-group no-article nil t))
7148
7149 ;; Walking around summary lines.
7150
7151 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7152   "Go to the first subject satisfying any non-nil constraint.
7153 If UNREAD is non-nil, the article should be unread.
7154 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7155 If UNSEEN is non-nil, the article should be unseen.
7156 Returns the article selected or nil if there are no matching articles."
7157   (interactive "P")
7158   (cond
7159    ;; Empty summary.
7160    ((null gnus-newsgroup-data)
7161     (gnus-message 3 "No articles in the group")
7162     nil)
7163    ;; Pick the first article.
7164    ((not (or unread undownloaded unseen))
7165     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7166     (gnus-data-number (car gnus-newsgroup-data)))
7167    ;; Find the first unread article.
7168    (t
7169     (let ((data gnus-newsgroup-data))
7170       (while (and data
7171                   (let ((num (gnus-data-number (car data))))
7172                     (or (memq num gnus-newsgroup-unfetched)
7173                         (not (or (and unread
7174                                       (memq num gnus-newsgroup-unreads))
7175                                  (and undownloaded
7176                                       (memq num gnus-newsgroup-undownloaded))
7177                                  (and unseen
7178                                       (memq num gnus-newsgroup-unseen)))))))
7179         (setq data (cdr data)))
7180       (prog1
7181           (if data
7182               (progn
7183                 (goto-char (gnus-data-pos (car data)))
7184                 (gnus-data-number (car data)))
7185             (gnus-message 3 "No more%s articles"
7186                           (let* ((r (when unread " unread"))
7187                                  (d (when undownloaded " undownloaded"))
7188                                  (s (when unseen " unseen"))
7189                                  (l (delq nil (list r d s))))
7190                             (cond ((= 3 (length l))
7191                                    (concat r "," d ", or" s))
7192                                   ((= 2 (length l))
7193                                    (concat (car l) ", or" (cadr l)))
7194                                   ((= 1 (length l))
7195                                    (car l))
7196                                   (t
7197                                    ""))))
7198             nil
7199             )
7200         (gnus-summary-position-point))))))
7201
7202 (defun gnus-summary-next-subject (n &optional unread dont-display)
7203   "Go to next N'th summary line.
7204 If N is negative, go to the previous N'th subject line.
7205 If UNREAD is non-nil, only unread articles are selected.
7206 The difference between N and the actual number of steps taken is
7207 returned."
7208   (interactive "p")
7209   (let ((backward (< n 0))
7210         (n (abs n)))
7211     (while (and (> n 0)
7212                 (if backward
7213                     (gnus-summary-find-prev unread)
7214                   (gnus-summary-find-next unread)))
7215       (unless (zerop (setq n (1- n)))
7216         (gnus-summary-show-thread)))
7217     (when (/= 0 n)
7218       (gnus-message 7 "No more%s articles"
7219                     (if unread " unread" "")))
7220     (unless dont-display
7221       (gnus-summary-recenter)
7222       (gnus-summary-position-point))
7223     n))
7224
7225 (defun gnus-summary-next-unread-subject (n)
7226   "Go to next N'th unread summary line."
7227   (interactive "p")
7228   (gnus-summary-next-subject n t))
7229
7230 (defun gnus-summary-prev-subject (n &optional unread)
7231   "Go to previous N'th summary line.
7232 If optional argument UNREAD is non-nil, only unread article is selected."
7233   (interactive "p")
7234   (gnus-summary-next-subject (- n) unread))
7235
7236 (defun gnus-summary-prev-unread-subject (n)
7237   "Go to previous N'th unread summary line."
7238   (interactive "p")
7239   (gnus-summary-next-subject (- n) t))
7240
7241 (defun gnus-summary-goto-subjects (articles)
7242   "Insert the subject header for ARTICLES in the current buffer."
7243   (save-excursion
7244     (dolist (article articles)
7245       (gnus-summary-goto-subject article t)))
7246   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7247   (gnus-summary-position-point))
7248
7249 (defun gnus-summary-goto-subject (article &optional force silent)
7250   "Go the subject line of ARTICLE.
7251 If FORCE, also allow jumping to articles not currently shown."
7252   (interactive "nArticle number: ")
7253   (unless (numberp article)
7254     (error "Article %s is not a number" article))
7255   (let ((b (point))
7256         (data (gnus-data-find article)))
7257     ;; We read in the article if we have to.
7258     (and (not data)
7259          force
7260          (gnus-summary-insert-subject
7261           article
7262           (if (or (numberp force) (vectorp force)) force)
7263           t)
7264          (setq data (gnus-data-find article)))
7265     (goto-char b)
7266     (if (not data)
7267         (progn
7268           (unless silent
7269             (gnus-message 3 "Can't find article %d" article))
7270           nil)
7271       (let ((pt (gnus-data-pos data)))
7272         (goto-char pt)
7273         (gnus-summary-set-article-display-arrow pt))
7274       (gnus-summary-position-point)
7275       article)))
7276
7277 ;; Walking around summary lines with displaying articles.
7278
7279 (defun gnus-summary-expand-window (&optional arg)
7280   "Make the summary buffer take up the entire Emacs frame.
7281 Given a prefix, will force an `article' buffer configuration."
7282   (interactive "P")
7283   (if arg
7284       (gnus-configure-windows 'article 'force)
7285     (gnus-configure-windows 'summary 'force)))
7286
7287 (defun gnus-summary-display-article (article &optional all-header)
7288   "Display ARTICLE in article buffer."
7289   (when (gnus-buffer-live-p gnus-article-buffer)
7290     (with-current-buffer gnus-article-buffer
7291       (mm-enable-multibyte)))
7292   (gnus-set-global-variables)
7293   (when (gnus-buffer-live-p gnus-article-buffer)
7294     (with-current-buffer gnus-article-buffer
7295       (setq gnus-article-charset gnus-newsgroup-charset)
7296       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7297       (mm-enable-multibyte)))
7298   (if (null article)
7299       nil
7300     (prog1
7301         (if gnus-summary-display-article-function
7302             (funcall gnus-summary-display-article-function article all-header)
7303           (gnus-article-prepare article all-header))
7304       (gnus-run-hooks 'gnus-select-article-hook)
7305       (when (and gnus-current-article
7306                  (not (zerop gnus-current-article)))
7307         (gnus-summary-goto-subject gnus-current-article))
7308       (gnus-summary-recenter)
7309       (when (and gnus-use-trees gnus-show-threads)
7310         (gnus-possibly-generate-tree article)
7311         (gnus-highlight-selected-tree article))
7312       ;; Successfully display article.
7313       (gnus-article-set-window-start
7314        (cdr (assq article gnus-newsgroup-bookmarks))))))
7315
7316 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7317   "Select the current article.
7318 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7319 non-nil, the article will be re-fetched even if it already present in
7320 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7321 be displayed."
7322   ;; Make sure we are in the summary buffer to work around bbdb bug.
7323   (unless (eq major-mode 'gnus-summary-mode)
7324     (set-buffer gnus-summary-buffer))
7325   (let ((article (or article (gnus-summary-article-number)))
7326         (all-headers (not (not all-headers))) ;Must be t or nil.
7327         gnus-summary-display-article-function)
7328     (and (not pseudo)
7329          (gnus-summary-article-pseudo-p article)
7330          (error "This is a pseudo-article"))
7331     (save-excursion
7332       (set-buffer gnus-summary-buffer)
7333       (if (or (and gnus-single-article-buffer
7334                    (or (null gnus-current-article)
7335                        (null gnus-article-current)
7336                        (null (get-buffer gnus-article-buffer))
7337                        (not (eq article (cdr gnus-article-current)))
7338                        (not (equal (car gnus-article-current)
7339                                    gnus-newsgroup-name))))
7340               (and (not gnus-single-article-buffer)
7341                    (or (null gnus-current-article)
7342                        (not (eq gnus-current-article article))))
7343               force)
7344           ;; The requested article is different from the current article.
7345           (progn
7346             (gnus-summary-display-article article all-headers)
7347             (when (gnus-buffer-live-p gnus-article-buffer)
7348               (with-current-buffer gnus-article-buffer
7349                 (if (not gnus-article-decoded-p) ;; a local variable
7350                     (mm-disable-multibyte))))
7351             (gnus-article-set-window-start
7352              (cdr (assq article gnus-newsgroup-bookmarks)))
7353             article)
7354         'old))))
7355
7356 (defun gnus-summary-force-verify-and-decrypt ()
7357   "Display buttons for signed/encrypted parts and verify/decrypt them."
7358   (interactive)
7359   (let ((mm-verify-option 'known)
7360         (mm-decrypt-option 'known)
7361         (gnus-article-emulate-mime t)
7362         (gnus-buttonized-mime-types (append (list "multipart/signed"
7363                                                   "multipart/encrypted")
7364                                             gnus-buttonized-mime-types)))
7365     (gnus-summary-select-article nil 'force)))
7366
7367 (defun gnus-summary-set-current-mark (&optional current-mark)
7368   "Obsolete function."
7369   nil)
7370
7371 (defun gnus-summary-next-article (&optional unread subject backward push)
7372   "Select the next article.
7373 If UNREAD, only unread articles are selected.
7374 If SUBJECT, only articles with SUBJECT are selected.
7375 If BACKWARD, the previous article is selected instead of the next."
7376   (interactive "P")
7377   (cond
7378    ;; Is there such an article?
7379    ((and (gnus-summary-search-forward unread subject backward)
7380          (or (gnus-summary-display-article (gnus-summary-article-number))
7381              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7382     (gnus-summary-position-point))
7383    ;; If not, we try the first unread, if that is wanted.
7384    ((and subject
7385          gnus-auto-select-same
7386          (gnus-summary-first-unread-article))
7387     (gnus-summary-position-point)
7388     (gnus-message 6 "Wrapped"))
7389    ;; Try to get next/previous article not displayed in this group.
7390    ((and gnus-auto-extend-newsgroup
7391          (not unread) (not subject))
7392     (gnus-summary-goto-article
7393      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7394      nil (count-lines (point-min) (point))))
7395    ;; Go to next/previous group.
7396    (t
7397     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7398       (gnus-summary-jump-to-group gnus-newsgroup-name))
7399     (let ((cmd last-command-char)
7400           (point
7401            (with-current-buffer gnus-group-buffer
7402              (point)))
7403           (group
7404            (if (eq gnus-keep-same-level 'best)
7405                (gnus-summary-best-group gnus-newsgroup-name)
7406              (gnus-summary-search-group backward gnus-keep-same-level))))
7407       ;; For some reason, the group window gets selected.  We change
7408       ;; it back.
7409       (select-window (get-buffer-window (current-buffer)))
7410       ;; Select next unread newsgroup automagically.
7411       (cond
7412        ((or (not gnus-auto-select-next)
7413             (not cmd))
7414         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7415        ((or (eq gnus-auto-select-next 'quietly)
7416             (and (eq gnus-auto-select-next 'slightly-quietly)
7417                  push)
7418             (and (eq gnus-auto-select-next 'almost-quietly)
7419                  (gnus-summary-last-article-p)))
7420         ;; Select quietly.
7421         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7422             (gnus-summary-exit)
7423           (gnus-message 7 "No more%s articles (%s)..."
7424                         (if unread " unread" "")
7425                         (if group (concat "selecting " group)
7426                           "exiting"))
7427           (gnus-summary-next-group nil group backward)))
7428        (t
7429         (when (gnus-key-press-event-p last-input-event)
7430           (gnus-summary-walk-group-buffer
7431            gnus-newsgroup-name cmd unread backward point))))))))
7432
7433 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7434   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7435                       (?\C-p (gnus-group-prev-unread-group 1))))
7436         (cursor-in-echo-area t)
7437         keve key group ended prompt)
7438     (save-excursion
7439       (set-buffer gnus-group-buffer)
7440       (goto-char start)
7441       (setq group
7442             (if (eq gnus-keep-same-level 'best)
7443                 (gnus-summary-best-group gnus-newsgroup-name)
7444               (gnus-summary-search-group backward gnus-keep-same-level))))
7445     (while (not ended)
7446       (setq prompt
7447             (format
7448              "No more%s articles%s " (if unread " unread" "")
7449              (if (and group
7450                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7451                  (format " (Type %s for %s [%s])"
7452                          (single-key-description cmd)
7453                          (gnus-group-decoded-name group)
7454                          (gnus-group-unread group))
7455                (format " (Type %s to exit %s)"
7456                        (single-key-description cmd)
7457                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7458       ;; Confirm auto selection.
7459       (setq key (car (setq keve (gnus-read-event-char prompt)))
7460             ended t)
7461       (cond
7462        ((assq key keystrokes)
7463         (let ((obuf (current-buffer)))
7464           (switch-to-buffer gnus-group-buffer)
7465           (when group
7466             (gnus-group-jump-to-group group))
7467           (eval (cadr (assq key keystrokes)))
7468           (setq group (gnus-group-group-name))
7469           (switch-to-buffer obuf))
7470         (setq ended nil))
7471        ((equal key cmd)
7472         (if (or (not group)
7473                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7474             (gnus-summary-exit)
7475           (gnus-summary-next-group nil group backward)))
7476        (t
7477         (push (cdr keve) unread-command-events))))))
7478
7479 (defun gnus-summary-next-unread-article ()
7480   "Select unread article after current one."
7481   (interactive)
7482   (gnus-summary-next-article
7483    (or (not (eq gnus-summary-goto-unread 'never))
7484        (gnus-summary-last-article-p (gnus-summary-article-number)))
7485    (and gnus-auto-select-same
7486         (gnus-summary-article-subject))))
7487
7488 (defun gnus-summary-prev-article (&optional unread subject)
7489   "Select the article after the current one.
7490 If UNREAD is non-nil, only unread articles are selected."
7491   (interactive "P")
7492   (gnus-summary-next-article unread subject t))
7493
7494 (defun gnus-summary-prev-unread-article ()
7495   "Select unread article before current one."
7496   (interactive)
7497   (gnus-summary-prev-article
7498    (or (not (eq gnus-summary-goto-unread 'never))
7499        (gnus-summary-first-article-p (gnus-summary-article-number)))
7500    (and gnus-auto-select-same
7501         (gnus-summary-article-subject))))
7502
7503 (defun gnus-summary-next-page (&optional lines circular stop)
7504   "Show next page of the selected article.
7505 If at the end of the current article, select the next article.
7506 LINES says how many lines should be scrolled up.
7507
7508 If CIRCULAR is non-nil, go to the start of the article instead of
7509 selecting the next article when reaching the end of the current
7510 article.
7511
7512 If STOP is non-nil, just stop when reaching the end of the message.
7513
7514 Also see the variable `gnus-article-skip-boring'."
7515   (interactive "P")
7516   (setq gnus-summary-buffer (current-buffer))
7517   (gnus-set-global-variables)
7518   (let ((article (gnus-summary-article-number))
7519         (article-window (get-buffer-window gnus-article-buffer t))
7520         endp)
7521     ;; If the buffer is empty, we have no article.
7522     (unless article
7523       (error "No article to select"))
7524     (gnus-configure-windows 'article)
7525     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7526         (if (and (eq gnus-summary-goto-unread 'never)
7527                  (not (gnus-summary-last-article-p article)))
7528             (gnus-summary-next-article)
7529           (gnus-summary-next-unread-article))
7530       (if (or (null gnus-current-article)
7531               (null gnus-article-current)
7532               (/= article (cdr gnus-article-current))
7533               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7534           ;; Selected subject is different from current article's.
7535           (gnus-summary-display-article article)
7536         (when article-window
7537           (gnus-eval-in-buffer-window gnus-article-buffer
7538             (setq endp (or (gnus-article-next-page lines)
7539                            (gnus-article-only-boring-p))))
7540           (when endp
7541             (cond (stop
7542                    (gnus-message 3 "End of message"))
7543                   (circular
7544                    (gnus-summary-beginning-of-article))
7545                   (lines
7546                    (gnus-message 3 "End of message"))
7547                   ((null lines)
7548                    (if (and (eq gnus-summary-goto-unread 'never)
7549                             (not (gnus-summary-last-article-p article)))
7550                        (gnus-summary-next-article)
7551                      (gnus-summary-next-unread-article))))))))
7552     (gnus-summary-recenter)
7553     (gnus-summary-position-point)))
7554
7555 (defun gnus-summary-prev-page (&optional lines move)
7556   "Show previous page of selected article.
7557 Argument LINES specifies lines to be scrolled down.
7558 If MOVE, move to the previous unread article if point is at
7559 the beginning of the buffer."
7560   (interactive "P")
7561   (let ((article (gnus-summary-article-number))
7562         (article-window (get-buffer-window gnus-article-buffer t))
7563         endp)
7564     (gnus-configure-windows 'article)
7565     (if (or (null gnus-current-article)
7566             (null gnus-article-current)
7567             (/= article (cdr gnus-article-current))
7568             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7569         ;; Selected subject is different from current article's.
7570         (gnus-summary-display-article article)
7571       (gnus-summary-recenter)
7572       (when article-window
7573         (gnus-eval-in-buffer-window gnus-article-buffer
7574           (setq endp (gnus-article-prev-page lines)))
7575         (when (and move endp)
7576           (cond (lines
7577                  (gnus-message 3 "Beginning of message"))
7578                 ((null lines)
7579                  (if (and (eq gnus-summary-goto-unread 'never)
7580                           (not (gnus-summary-first-article-p article)))
7581                      (gnus-summary-prev-article)
7582                    (gnus-summary-prev-unread-article))))))))
7583   (gnus-summary-position-point))
7584
7585 (defun gnus-summary-prev-page-or-article (&optional lines)
7586   "Show previous page of selected article.
7587 Argument LINES specifies lines to be scrolled down.
7588 If at the beginning of the article, go to the next article."
7589   (interactive "P")
7590   (gnus-summary-prev-page lines t))
7591
7592 (defun gnus-summary-scroll-up (lines)
7593   "Scroll up (or down) one line current article.
7594 Argument LINES specifies lines to be scrolled up (or down if negative)."
7595   (interactive "p")
7596   (gnus-configure-windows 'article)
7597   (gnus-summary-show-thread)
7598   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7599     (gnus-eval-in-buffer-window gnus-article-buffer
7600       (cond ((> lines 0)
7601              (when (gnus-article-next-page lines)
7602                (gnus-message 3 "End of message")))
7603             ((< lines 0)
7604              (gnus-article-prev-page (- lines))))))
7605   (gnus-summary-recenter)
7606   (gnus-summary-position-point))
7607
7608 (defun gnus-summary-scroll-down (lines)
7609   "Scroll down (or up) one line current article.
7610 Argument LINES specifies lines to be scrolled down (or up if negative)."
7611   (interactive "p")
7612   (gnus-summary-scroll-up (- lines)))
7613
7614 (defun gnus-summary-next-same-subject ()
7615   "Select next article which has the same subject as current one."
7616   (interactive)
7617   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7618
7619 (defun gnus-summary-prev-same-subject ()
7620   "Select previous article which has the same subject as current one."
7621   (interactive)
7622   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7623
7624 (defun gnus-summary-next-unread-same-subject ()
7625   "Select next unread article which has the same subject as current one."
7626   (interactive)
7627   (gnus-summary-next-article t (gnus-summary-article-subject)))
7628
7629 (defun gnus-summary-prev-unread-same-subject ()
7630   "Select previous unread article which has the same subject as current one."
7631   (interactive)
7632   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7633
7634 (defun gnus-summary-first-unread-article ()
7635   "Select the first unread article.
7636 Return nil if there are no unread articles."
7637   (interactive)
7638   (prog1
7639       (when (gnus-summary-first-subject t)
7640         (gnus-summary-show-thread)
7641         (gnus-summary-first-subject t)
7642         (gnus-summary-display-article (gnus-summary-article-number)))
7643     (gnus-summary-position-point)))
7644
7645 (defun gnus-summary-first-unread-subject ()
7646   "Place the point on the subject line of the first unread article.
7647 Return nil if there are no unread articles."
7648   (interactive)
7649   (prog1
7650       (when (gnus-summary-first-subject t)
7651         (gnus-summary-show-thread)
7652         (gnus-summary-first-subject t))
7653     (gnus-summary-position-point)))
7654
7655 (defun gnus-summary-first-unseen-subject ()
7656   "Place the point on the subject line of the first unseen article.
7657 Return nil if there are no unseen articles."
7658   (interactive)
7659   (prog1
7660       (when (gnus-summary-first-subject nil nil t)
7661         (gnus-summary-show-thread)
7662         (gnus-summary-first-subject nil nil t))
7663     (gnus-summary-position-point)))
7664
7665 (defun gnus-summary-first-unseen-or-unread-subject ()
7666   "Place the point on the subject line of the first unseen article or,
7667 if all article have been seen, on the subject line of the first unread
7668 article."
7669   (interactive)
7670   (prog1
7671       (unless (when (gnus-summary-first-subject nil nil t)
7672                 (gnus-summary-show-thread)
7673                 (gnus-summary-first-subject nil nil t))
7674         (when (gnus-summary-first-subject t)
7675           (gnus-summary-show-thread)
7676           (gnus-summary-first-subject t)))
7677     (gnus-summary-position-point)))
7678
7679 (defun gnus-summary-first-article ()
7680   "Select the first article.
7681 Return nil if there are no articles."
7682   (interactive)
7683   (prog1
7684       (when (gnus-summary-first-subject)
7685         (gnus-summary-show-thread)
7686         (gnus-summary-first-subject)
7687         (gnus-summary-display-article (gnus-summary-article-number)))
7688     (gnus-summary-position-point)))
7689
7690 (defun gnus-summary-best-unread-article (&optional arg)
7691   "Select the unread article with the highest score.
7692 If given a prefix argument, select the next unread article that has a
7693 score higher than the default score."
7694   (interactive "P")
7695   (let ((article (if arg
7696                      (gnus-summary-better-unread-subject)
7697                    (gnus-summary-best-unread-subject))))
7698     (if article
7699         (gnus-summary-goto-article article)
7700       (error "No unread articles"))))
7701
7702 (defun gnus-summary-best-unread-subject ()
7703   "Select the unread subject with the highest score."
7704   (interactive)
7705   (let ((best -1000000)
7706         (data gnus-newsgroup-data)
7707         article score)
7708     (while data
7709       (and (gnus-data-unread-p (car data))
7710            (> (setq score
7711                     (gnus-summary-article-score (gnus-data-number (car data))))
7712               best)
7713            (setq best score
7714                  article (gnus-data-number (car data))))
7715       (setq data (cdr data)))
7716     (when article
7717       (gnus-summary-goto-subject article))
7718     (gnus-summary-position-point)
7719     article))
7720
7721 (defun gnus-summary-better-unread-subject ()
7722   "Select the first unread subject that has a score over the default score."
7723   (interactive)
7724   (let ((data gnus-newsgroup-data)
7725         article score)
7726     (while (and (setq article (gnus-data-number (car data)))
7727                 (or (gnus-data-read-p (car data))
7728                     (not (> (gnus-summary-article-score article)
7729                             gnus-summary-default-score))))
7730       (setq data (cdr data)))
7731     (when article
7732       (gnus-summary-goto-subject article))
7733     (gnus-summary-position-point)
7734     article))
7735
7736 (defun gnus-summary-last-subject ()
7737   "Go to the last displayed subject line in the group."
7738   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7739     (when article
7740       (gnus-summary-goto-subject article))))
7741
7742 (defun gnus-summary-goto-article (article &optional all-headers force)
7743   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7744 If ALL-HEADERS is non-nil, no header lines are hidden.
7745 If FORCE, go to the article even if it isn't displayed.  If FORCE
7746 is a number, it is the line the article is to be displayed on."
7747   (interactive
7748    (list
7749     (completing-read
7750      "Article number or Message-ID: "
7751      (mapcar (lambda (number) (list (int-to-string number)))
7752              gnus-newsgroup-limit))
7753     current-prefix-arg
7754     t))
7755   (prog1
7756       (if (and (stringp article)
7757                (string-match "@\\|%40" article))
7758           (gnus-summary-refer-article article)
7759         (when (stringp article)
7760           (setq article (string-to-number article)))
7761         (if (gnus-summary-goto-subject article force)
7762             (gnus-summary-display-article article all-headers)
7763           (gnus-message 4 "Couldn't go to article %s" article) nil))
7764     (gnus-summary-position-point)))
7765
7766 (defun gnus-summary-goto-last-article ()
7767   "Go to the previously read article."
7768   (interactive)
7769   (prog1
7770       (when gnus-last-article
7771         (gnus-summary-goto-article gnus-last-article nil t))
7772     (gnus-summary-position-point)))
7773
7774 (defun gnus-summary-pop-article (number)
7775   "Pop one article off the history and go to the previous.
7776 NUMBER articles will be popped off."
7777   (interactive "p")
7778   (let (to)
7779     (setq gnus-newsgroup-history
7780           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7781     (if to
7782         (gnus-summary-goto-article (car to) nil t)
7783       (error "Article history empty")))
7784   (gnus-summary-position-point))
7785
7786 ;; Summary commands and functions for limiting the summary buffer.
7787
7788 (defun gnus-summary-limit-to-articles (n)
7789   "Limit the summary buffer to the next N articles.
7790 If not given a prefix, use the process marked articles instead."
7791   (interactive "P")
7792   (prog1
7793       (let ((articles (gnus-summary-work-articles n)))
7794         (setq gnus-newsgroup-processable nil)
7795         (gnus-summary-limit articles))
7796     (gnus-summary-position-point)))
7797
7798 (defun gnus-summary-pop-limit (&optional total)
7799   "Restore the previous limit.
7800 If given a prefix, remove all limits."
7801   (interactive "P")
7802   (when total
7803     (setq gnus-newsgroup-limits
7804           (list (mapcar (lambda (h) (mail-header-number h))
7805                         gnus-newsgroup-headers))))
7806   (unless gnus-newsgroup-limits
7807     (error "No limit to pop"))
7808   (prog1
7809       (gnus-summary-limit nil 'pop)
7810     (gnus-summary-position-point)))
7811
7812 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7813   "Limit the summary buffer to articles that have subjects that match a regexp.
7814 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7815   (interactive
7816    (list (read-string (if current-prefix-arg
7817                           "Exclude subject (regexp): "
7818                         "Limit to subject (regexp): "))
7819          nil current-prefix-arg))
7820   (unless header
7821     (setq header "subject"))
7822   (when (not (equal "" subject))
7823     (prog1
7824         (let ((articles (gnus-summary-find-matching
7825                          (or header "subject") subject 'all nil nil
7826                          not-matching)))
7827           (unless articles
7828             (error "Found no matches for \"%s\"" subject))
7829           (gnus-summary-limit articles))
7830       (gnus-summary-position-point))))
7831
7832 (defun gnus-summary-limit-to-author (from &optional not-matching)
7833   "Limit the summary buffer to articles that have authors that match a regexp.
7834 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7835   (interactive
7836    (list (read-string (if current-prefix-arg
7837                           "Exclude author (regexp): "
7838                         "Limit to author (regexp): "))
7839          current-prefix-arg))
7840   (gnus-summary-limit-to-subject from "from" not-matching))
7841
7842 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7843   "Limit the summary buffer to articles with the given RECIPIENT.
7844
7845 If NOT-MATCHING, exclude RECIPIENT.
7846
7847 To and Cc headers are checked.  You need to include them in
7848 `nnmail-extra-headers'."
7849   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7850   (interactive
7851    (list (read-string (format "%s recipient (regexp): "
7852                               (if current-prefix-arg "Exclude" "Limit to")))
7853          current-prefix-arg))
7854   (when (not (equal "" recipient))
7855     (prog1 (let* ((to
7856                    (if (memq 'To nnmail-extra-headers)
7857                        (gnus-summary-find-matching
7858                         (cons 'extra 'To) recipient 'all nil nil
7859                         not-matching)
7860                      (gnus-message
7861                       1 "`To' isn't present in `nnmail-extra-headers'")
7862                      (sit-for 1)
7863                      nil))
7864                   (cc
7865                    (if (memq 'Cc nnmail-extra-headers)
7866                        (gnus-summary-find-matching
7867                         (cons 'extra 'Cc) recipient 'all nil nil
7868                         not-matching)
7869                      (gnus-message
7870                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7871                      (sit-for 1)
7872                      nil))
7873                   (articles
7874                    (if not-matching
7875                        ;; We need the numbers that are in both lists:
7876                        (mapcar (lambda (a)
7877                                  (and (memq a to) a))
7878                                cc)
7879                      (nconc to cc))))
7880              (unless articles
7881                (error "Found no matches for \"%s\"" recipient))
7882              (gnus-summary-limit articles))
7883       (gnus-summary-position-point))))
7884
7885 (defun gnus-summary-limit-to-age (age &optional younger-p)
7886   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7887 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7888 articles that are younger than AGE days."
7889   (interactive
7890    (let ((younger current-prefix-arg)
7891          (days-got nil)
7892          days)
7893      (while (not days-got)
7894        (setq days (if younger
7895                       (read-string "Limit to articles younger than (in days, older when negative): ")
7896                     (read-string
7897                      "Limit to articles older than (in days, younger when negative): ")))
7898        (when (> (length days) 0)
7899          (setq days (read days)))
7900        (if (numberp days)
7901            (progn
7902              (setq days-got t)
7903              (when (< days 0)
7904                (setq younger (not younger))
7905                (setq days (* days -1))))
7906          (message "Please enter a number.")
7907          (sleep-for 1)))
7908      (list days younger)))
7909   (prog1
7910       (let ((data gnus-newsgroup-data)
7911             (cutoff (days-to-time age))
7912             articles d date is-younger)
7913         (while (setq d (pop data))
7914           (when (and (vectorp (gnus-data-header d))
7915                      (setq date (mail-header-date (gnus-data-header d))))
7916             (setq is-younger (time-less-p
7917                               (time-since (condition-case ()
7918                                               (date-to-time date)
7919                                             (error '(0 0))))
7920                               cutoff))
7921             (when (if younger-p
7922                       is-younger
7923                     (not is-younger))
7924               (push (gnus-data-number d) articles))))
7925         (gnus-summary-limit (nreverse articles)))
7926     (gnus-summary-position-point)))
7927
7928 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7929   "Limit the summary buffer to articles that match an 'extra' header."
7930   (interactive
7931    (let ((header
7932           (intern
7933            (gnus-completing-read-with-default
7934             (symbol-name (car gnus-extra-headers))
7935             (if current-prefix-arg
7936                 "Exclude extra header"
7937               "Limit extra header")
7938             (mapcar (lambda (x)
7939                       (cons (symbol-name x) x))
7940                     gnus-extra-headers)
7941             nil
7942             t))))
7943      (list header
7944            (read-string (format "%s header %s (regexp): "
7945                                 (if current-prefix-arg "Exclude" "Limit to")
7946                                 header))
7947            current-prefix-arg)))
7948   (when (not (equal "" regexp))
7949     (prog1
7950         (let ((articles (gnus-summary-find-matching
7951                          (cons 'extra header) regexp 'all nil nil
7952                          not-matching)))
7953           (unless articles
7954             (error "Found no matches for \"%s\"" regexp))
7955           (gnus-summary-limit articles))
7956       (gnus-summary-position-point))))
7957
7958 (defun gnus-summary-limit-to-display-predicate ()
7959   "Limit the summary buffer to the predicated in the `display' group parameter."
7960   (interactive)
7961   (unless gnus-newsgroup-display
7962     (error "There is no `display' group parameter"))
7963   (let (articles)
7964     (dolist (number gnus-newsgroup-articles)
7965       (when (funcall gnus-newsgroup-display)
7966         (push number articles)))
7967     (gnus-summary-limit articles))
7968   (gnus-summary-position-point))
7969
7970 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7971 (make-obsolete
7972  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7973
7974 (defun gnus-summary-limit-to-unread (&optional all)
7975   "Limit the summary buffer to articles that are not marked as read.
7976 If ALL is non-nil, limit strictly to unread articles."
7977   (interactive "P")
7978   (if all
7979       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7980     (gnus-summary-limit-to-marks
7981      ;; Concat all the marks that say that an article is read and have
7982      ;; those removed.
7983      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7984            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7985            gnus-low-score-mark gnus-expirable-mark
7986            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7987            gnus-duplicate-mark gnus-souped-mark)
7988      'reverse)))
7989
7990 (defun gnus-summary-limit-to-replied (&optional unreplied)
7991   "Limit the summary buffer to replied articles.
7992 If UNREPLIED (the prefix), limit to unreplied articles."
7993   (interactive "P")
7994   (if unreplied
7995       (gnus-summary-limit
7996        (gnus-set-difference gnus-newsgroup-articles
7997         gnus-newsgroup-replied))
7998     (gnus-summary-limit gnus-newsgroup-replied))
7999   (gnus-summary-position-point))
8000
8001 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8002 (make-obsolete 'gnus-summary-delete-marked-with
8003                'gnus-summary-limit-exclude-marks)
8004
8005 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8006   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8007 If REVERSE, limit the summary buffer to articles that are marked
8008 with MARKS.  MARKS can either be a string of marks or a list of marks.
8009 Returns how many articles were removed."
8010   (interactive "sMarks: ")
8011   (gnus-summary-limit-to-marks marks t))
8012
8013 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8014   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8015 If REVERSE (the prefix), limit the summary buffer to articles that are
8016 not marked with MARKS.  MARKS can either be a string of marks or a
8017 list of marks.
8018 Returns how many articles were removed."
8019   (interactive "sMarks: \nP")
8020   (prog1
8021       (let ((data gnus-newsgroup-data)
8022             (marks (if (listp marks) marks
8023                      (append marks nil))) ; Transform to list.
8024             articles)
8025         (while data
8026           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8027                   (memq (gnus-data-mark (car data)) marks))
8028             (push (gnus-data-number (car data)) articles))
8029           (setq data (cdr data)))
8030         (gnus-summary-limit articles))
8031     (gnus-summary-position-point)))
8032
8033 (defun gnus-summary-limit-to-score (score)
8034   "Limit to articles with score at or above SCORE."
8035   (interactive "NLimit to articles with score of at least: ")
8036   (let ((data gnus-newsgroup-data)
8037         articles)
8038     (while data
8039       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8040                 score)
8041         (push (gnus-data-number (car data)) articles))
8042       (setq data (cdr data)))
8043     (prog1
8044         (gnus-summary-limit articles)
8045       (gnus-summary-position-point))))
8046
8047 (defun gnus-summary-limit-to-unseen ()
8048   "Limit to unseen articles."
8049   (interactive)
8050   (prog1
8051       (gnus-summary-limit gnus-newsgroup-unseen)
8052     (gnus-summary-position-point)))
8053
8054 (defun gnus-summary-limit-include-thread (id)
8055   "Display all the hidden articles that is in the thread with ID in it.
8056 When called interactively, ID is the Message-ID of the current
8057 article."
8058   (interactive (list (mail-header-id (gnus-summary-article-header))))
8059   (let ((articles (gnus-articles-in-thread
8060                    (gnus-id-to-thread (gnus-root-id id)))))
8061     (prog1
8062         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8063       (gnus-summary-limit-include-matching-articles
8064        "subject"
8065        (regexp-quote (gnus-simplify-subject-re
8066                       (mail-header-subject (gnus-id-to-header id)))))
8067       (gnus-summary-position-point))))
8068
8069 (defun gnus-summary-limit-include-matching-articles (header regexp)
8070   "Display all the hidden articles that have HEADERs that match REGEXP."
8071   (interactive (list (read-string "Match on header: ")
8072                      (read-string "Regexp: ")))
8073   (let ((articles (gnus-find-matching-articles header regexp)))
8074     (prog1
8075         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8076       (gnus-summary-position-point))))
8077
8078 (defun gnus-summary-insert-dormant-articles ()
8079   "Insert all the dormant articles for this group into the current buffer."
8080   (interactive)
8081   (let ((gnus-verbose (max 6 gnus-verbose)))
8082     (if (not gnus-newsgroup-dormant)
8083         (gnus-message 3 "No cached articles for this group")
8084       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8085
8086 (defun gnus-summary-limit-include-dormant ()
8087   "Display all the hidden articles that are marked as dormant.
8088 Note that this command only works on a subset of the articles currently
8089 fetched for this group."
8090   (interactive)
8091   (unless gnus-newsgroup-dormant
8092     (error "There are no dormant articles in this group"))
8093   (prog1
8094       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8095     (gnus-summary-position-point)))
8096
8097 (defun gnus-summary-limit-exclude-dormant ()
8098   "Hide all dormant articles."
8099   (interactive)
8100   (prog1
8101       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8102     (gnus-summary-position-point)))
8103
8104 (defun gnus-summary-limit-exclude-childless-dormant ()
8105   "Hide all dormant articles that have no children."
8106   (interactive)
8107   (let ((data (gnus-data-list t))
8108         articles d children)
8109     ;; Find all articles that are either not dormant or have
8110     ;; children.
8111     (while (setq d (pop data))
8112       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8113                 (and (setq children
8114                            (gnus-article-children (gnus-data-number d)))
8115                      (let (found)
8116                        (while children
8117                          (when (memq (car children) articles)
8118                            (setq children nil
8119                                  found t))
8120                          (pop children))
8121                        found)))
8122         (push (gnus-data-number d) articles)))
8123     ;; Do the limiting.
8124     (prog1
8125         (gnus-summary-limit articles)
8126       (gnus-summary-position-point))))
8127
8128 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8129   "Mark all unread excluded articles as read.
8130 If ALL, mark even excluded ticked and dormants as read."
8131   (interactive "P")
8132   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8133   (let ((articles (gnus-sorted-ndifference
8134                    (sort
8135                     (mapcar (lambda (h) (mail-header-number h))
8136                             gnus-newsgroup-headers)
8137                     '<)
8138                    gnus-newsgroup-limit))
8139         article)
8140     (setq gnus-newsgroup-unreads
8141           (gnus-sorted-intersection gnus-newsgroup-unreads
8142                                     gnus-newsgroup-limit))
8143     (if all
8144         (setq gnus-newsgroup-dormant nil
8145               gnus-newsgroup-marked nil
8146               gnus-newsgroup-reads
8147               (nconc
8148                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8149                gnus-newsgroup-reads))
8150       (while (setq article (pop articles))
8151         (unless (or (memq article gnus-newsgroup-dormant)
8152                     (memq article gnus-newsgroup-marked))
8153           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8154
8155 (defun gnus-summary-limit (articles &optional pop)
8156   (if pop
8157       ;; We pop the previous limit off the stack and use that.
8158       (setq articles (car gnus-newsgroup-limits)
8159             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8160     ;; We use the new limit, so we push the old limit on the stack.
8161     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8162   ;; Set the limit.
8163   (setq gnus-newsgroup-limit articles)
8164   (let ((total (length gnus-newsgroup-data))
8165         (data (gnus-data-find-list (gnus-summary-article-number)))
8166         (gnus-summary-mark-below nil)   ; Inhibit this.
8167         found)
8168     ;; This will do all the work of generating the new summary buffer
8169     ;; according to the new limit.
8170     (gnus-summary-prepare)
8171     ;; Hide any threads, possibly.
8172     (gnus-summary-maybe-hide-threads)
8173     ;; Try to return to the article you were at, or one in the
8174     ;; neighborhood.
8175     (when data
8176       ;; We try to find some article after the current one.
8177       (while data
8178         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8179           (setq data nil
8180                 found t))
8181         (setq data (cdr data))))
8182     (unless found
8183       ;; If there is no data, that means that we were after the last
8184       ;; article.  The same goes when we can't find any articles
8185       ;; after the current one.
8186       (goto-char (point-max))
8187       (gnus-summary-find-prev))
8188     (gnus-set-mode-line 'summary)
8189     ;; We return how many articles were removed from the summary
8190     ;; buffer as a result of the new limit.
8191     (- total (length gnus-newsgroup-data))))
8192
8193 (defsubst gnus-invisible-cut-children (threads)
8194   (let ((num 0))
8195     (while threads
8196       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8197         (incf num))
8198       (pop threads))
8199     (< num 2)))
8200
8201 (defsubst gnus-cut-thread (thread)
8202   "Go forwards in the thread until we find an article that we want to display."
8203   (when (or (eq gnus-fetch-old-headers 'some)
8204             (eq gnus-fetch-old-headers 'invisible)
8205             (numberp gnus-fetch-old-headers)
8206             (eq gnus-build-sparse-threads 'some)
8207             (eq gnus-build-sparse-threads 'more))
8208     ;; Deal with old-fetched headers and sparse threads.
8209     (while (and
8210             thread
8211             (or
8212              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8213              (gnus-summary-article-ancient-p
8214               (mail-header-number (car thread))))
8215             (if (or (<= (length (cdr thread)) 1)
8216                     (eq gnus-fetch-old-headers 'invisible))
8217                 (setq gnus-newsgroup-limit
8218                       (delq (mail-header-number (car thread))
8219                             gnus-newsgroup-limit)
8220                       thread (cadr thread))
8221               (when (gnus-invisible-cut-children (cdr thread))
8222                 (let ((th (cdr thread)))
8223                   (while th
8224                     (if (memq (mail-header-number (caar th))
8225                               gnus-newsgroup-limit)
8226                         (setq thread (car th)
8227                               th nil)
8228                       (setq th (cdr th))))))))))
8229   thread)
8230
8231 (defun gnus-cut-threads (threads)
8232   "Cut off all uninteresting articles from the beginning of THREADS."
8233   (when (or (eq gnus-fetch-old-headers 'some)
8234             (eq gnus-fetch-old-headers 'invisible)
8235             (numberp gnus-fetch-old-headers)
8236             (eq gnus-build-sparse-threads 'some)
8237             (eq gnus-build-sparse-threads 'more))
8238     (let ((th threads))
8239       (while th
8240         (setcar th (gnus-cut-thread (car th)))
8241         (setq th (cdr th)))))
8242   ;; Remove nixed out threads.
8243   (delq nil threads))
8244
8245 (defun gnus-summary-initial-limit (&optional show-if-empty)
8246   "Figure out what the initial limit is supposed to be on group entry.
8247 This entails weeding out unwanted dormants, low-scored articles,
8248 fetch-old-headers verbiage, and so on."
8249   ;; Most groups have nothing to remove.
8250   (if (or gnus-inhibit-limiting
8251           (and (null gnus-newsgroup-dormant)
8252                (eq gnus-newsgroup-display 'gnus-not-ignore)
8253                (not (eq gnus-fetch-old-headers 'some))
8254                (not (numberp gnus-fetch-old-headers))
8255                (not (eq gnus-fetch-old-headers 'invisible))
8256                (null gnus-summary-expunge-below)
8257                (not (eq gnus-build-sparse-threads 'some))
8258                (not (eq gnus-build-sparse-threads 'more))
8259                (null gnus-thread-expunge-below)
8260                (not gnus-use-nocem)))
8261       ()                                ; Do nothing.
8262     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8263     (setq gnus-newsgroup-limit nil)
8264     (mapatoms
8265      (lambda (node)
8266        (unless (car (symbol-value node))
8267          ;; These threads have no parents -- they are roots.
8268          (let ((nodes (cdr (symbol-value node)))
8269                thread)
8270            (while nodes
8271              (if (and gnus-thread-expunge-below
8272                       (< (gnus-thread-total-score (car nodes))
8273                          gnus-thread-expunge-below))
8274                  (gnus-expunge-thread (pop nodes))
8275                (setq thread (pop nodes))
8276                (gnus-summary-limit-children thread))))))
8277      gnus-newsgroup-dependencies)
8278     ;; If this limitation resulted in an empty group, we might
8279     ;; pop the previous limit and use it instead.
8280     (when (and (not gnus-newsgroup-limit)
8281                show-if-empty)
8282       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8283     gnus-newsgroup-limit))
8284
8285 (defun gnus-summary-limit-children (thread)
8286   "Return 1 if this subthread is visible and 0 if it is not."
8287   ;; First we get the number of visible children to this thread.  This
8288   ;; is done by recursing down the thread using this function, so this
8289   ;; will really go down to a leaf article first, before slowly
8290   ;; working its way up towards the root.
8291   (when thread
8292     (let* ((max-lisp-eval-depth 5000)
8293            (children
8294            (if (cdr thread)
8295                (apply '+ (mapcar 'gnus-summary-limit-children
8296                                  (cdr thread)))
8297              0))
8298           (number (mail-header-number (car thread)))
8299           score)
8300       (if (and
8301            (not (memq number gnus-newsgroup-marked))
8302            (or
8303             ;; If this article is dormant and has absolutely no visible
8304             ;; children, then this article isn't visible.
8305             (and (memq number gnus-newsgroup-dormant)
8306                  (zerop children))
8307             ;; If this is "fetch-old-headered" and there is no
8308             ;; visible children, then we don't want this article.
8309             (and (or (eq gnus-fetch-old-headers 'some)
8310                      (numberp gnus-fetch-old-headers))
8311                  (gnus-summary-article-ancient-p number)
8312                  (zerop children))
8313             ;; If this is "fetch-old-headered" and `invisible', then
8314             ;; we don't want this article.
8315             (and (eq gnus-fetch-old-headers 'invisible)
8316                  (gnus-summary-article-ancient-p number))
8317             ;; If this is a sparsely inserted article with no children,
8318             ;; we don't want it.
8319             (and (eq gnus-build-sparse-threads 'some)
8320                  (gnus-summary-article-sparse-p number)
8321                  (zerop children))
8322             ;; If we use expunging, and this article is really
8323             ;; low-scored, then we don't want this article.
8324             (when (and gnus-summary-expunge-below
8325                        (< (setq score
8326                                 (or (cdr (assq number gnus-newsgroup-scored))
8327                                     gnus-summary-default-score))
8328                           gnus-summary-expunge-below))
8329               ;; We increase the expunge-tally here, but that has
8330               ;; nothing to do with the limits, really.
8331               (incf gnus-newsgroup-expunged-tally)
8332               ;; We also mark as read here, if that's wanted.
8333               (when (and gnus-summary-mark-below
8334                          (< score gnus-summary-mark-below))
8335                 (setq gnus-newsgroup-unreads
8336                       (delq number gnus-newsgroup-unreads))
8337                 (if gnus-newsgroup-auto-expire
8338                     (push number gnus-newsgroup-expirable)
8339                   (push (cons number gnus-low-score-mark)
8340                         gnus-newsgroup-reads)))
8341               t)
8342             ;; Do the `display' group parameter.
8343             (and gnus-newsgroup-display
8344                  (not (funcall gnus-newsgroup-display)))
8345             ;; Check NoCeM things.
8346             (when (and gnus-use-nocem
8347                        (gnus-nocem-unwanted-article-p
8348                         (mail-header-id (car thread))))
8349               (setq gnus-newsgroup-unreads
8350                     (delq number gnus-newsgroup-unreads))
8351               t)))
8352           ;; Nope, invisible article.
8353           0
8354         ;; Ok, this article is to be visible, so we add it to the limit
8355         ;; and return 1.
8356         (push number gnus-newsgroup-limit)
8357         1))))
8358
8359 (defun gnus-expunge-thread (thread)
8360   "Mark all articles in THREAD as read."
8361   (let* ((number (mail-header-number (car thread))))
8362     (incf gnus-newsgroup-expunged-tally)
8363     ;; We also mark as read here, if that's wanted.
8364     (setq gnus-newsgroup-unreads
8365           (delq number gnus-newsgroup-unreads))
8366     (if gnus-newsgroup-auto-expire
8367         (push number gnus-newsgroup-expirable)
8368       (push (cons number gnus-low-score-mark)
8369             gnus-newsgroup-reads)))
8370   ;; Go recursively through all subthreads.
8371   (mapcar 'gnus-expunge-thread (cdr thread)))
8372
8373 ;; Summary article oriented commands
8374
8375 (defun gnus-summary-refer-parent-article (n)
8376   "Refer parent article N times.
8377 If N is negative, go to ancestor -N instead.
8378 The difference between N and the number of articles fetched is returned."
8379   (interactive "p")
8380   (let ((skip 1)
8381         error header ref)
8382     (when (not (natnump n))
8383       (setq skip (abs n)
8384             n 1))
8385     (while (and (> n 0)
8386                 (not error))
8387       (setq header (gnus-summary-article-header))
8388       (if (and (eq (mail-header-number header)
8389                    (cdr gnus-article-current))
8390                (equal gnus-newsgroup-name
8391                       (car gnus-article-current)))
8392           ;; If we try to find the parent of the currently
8393           ;; displayed article, then we take a look at the actual
8394           ;; References header, since this is slightly more
8395           ;; reliable than the References field we got from the
8396           ;; server.
8397           (save-excursion
8398             (set-buffer gnus-original-article-buffer)
8399             (nnheader-narrow-to-headers)
8400             (unless (setq ref (message-fetch-field "references"))
8401               (when (setq ref (message-fetch-field "in-reply-to"))
8402                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8403             (widen))
8404         (setq ref
8405               ;; It's not the current article, so we take a bet on
8406               ;; the value we got from the server.
8407               (mail-header-references header)))
8408       (if (and ref
8409                (not (equal ref "")))
8410           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8411             (gnus-message 1 "Couldn't find parent"))
8412         (gnus-message 1 "No references in article %d"
8413                       (gnus-summary-article-number))
8414         (setq error t))
8415       (decf n))
8416     (gnus-summary-position-point)
8417     n))
8418
8419 (defun gnus-summary-refer-references ()
8420   "Fetch all articles mentioned in the References header.
8421 Return the number of articles fetched."
8422   (interactive)
8423   (let ((ref (mail-header-references (gnus-summary-article-header)))
8424         (current (gnus-summary-article-number))
8425         (n 0))
8426     (if (or (not ref)
8427             (equal ref ""))
8428         (error "No References in the current article")
8429       ;; For each Message-ID in the References header...
8430       (while (string-match "<[^>]*>" ref)
8431         (incf n)
8432         ;; ... fetch that article.
8433         (gnus-summary-refer-article
8434          (prog1 (match-string 0 ref)
8435            (setq ref (substring ref (match-end 0))))))
8436       (gnus-summary-goto-subject current)
8437       (gnus-summary-position-point)
8438       n)))
8439
8440 (defun gnus-summary-refer-thread (&optional limit)
8441   "Fetch all articles in the current thread.
8442 If LIMIT (the numerical prefix), fetch that many old headers instead
8443 of what's specified by the `gnus-refer-thread-limit' variable."
8444   (interactive "P")
8445   (let ((id (mail-header-id (gnus-summary-article-header)))
8446         (limit (if limit (prefix-numeric-value limit)
8447                  gnus-refer-thread-limit)))
8448     (unless (eq gnus-fetch-old-headers 'invisible)
8449       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8450       ;; Retrieve the headers and read them in.
8451       (if (eq (if (numberp limit)
8452                   (gnus-retrieve-headers
8453                    (list (min
8454                           (+ (mail-header-number
8455                               (gnus-summary-article-header))
8456                              limit)
8457                           gnus-newsgroup-end))
8458                    gnus-newsgroup-name (* limit 2))
8459                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8460                 ;; headers.
8461                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8462                                        gnus-newsgroup-name limit))
8463               'nov)
8464           (gnus-build-all-threads)
8465         (error "Can't fetch thread from back ends that don't support NOV"))
8466       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8467     (gnus-summary-limit-include-thread id)))
8468
8469 (defun gnus-summary-refer-article (message-id)
8470   "Fetch an article specified by MESSAGE-ID."
8471   (interactive "sMessage-ID: ")
8472   (when (and (stringp message-id)
8473              (not (zerop (length message-id))))
8474     (setq message-id (gnus-replace-in-string message-id " " ""))
8475     ;; Construct the correct Message-ID if necessary.
8476     ;; Suggested by tale@pawl.rpi.edu.
8477     (unless (string-match "^<" message-id)
8478       (setq message-id (concat "<" message-id)))
8479     (unless (string-match ">$" message-id)
8480       (setq message-id (concat message-id ">")))
8481     ;; People often post MIDs from URLs, so unhex it:
8482     (unless (string-match "@" message-id)
8483       (setq message-id (gnus-url-unhex-string message-id)))
8484     (let* ((header (gnus-id-to-header message-id))
8485            (sparse (and header
8486                         (gnus-summary-article-sparse-p
8487                          (mail-header-number header))
8488                         (memq (mail-header-number header)
8489                               gnus-newsgroup-limit)))
8490            number)
8491       (cond
8492        ;; If the article is present in the buffer we just go to it.
8493        ((and header
8494              (or (not (gnus-summary-article-sparse-p
8495                        (mail-header-number header)))
8496                  sparse))
8497         (prog1
8498             (gnus-summary-goto-article
8499              (mail-header-number header) nil t)
8500           (when sparse
8501             (gnus-summary-update-article (mail-header-number header)))))
8502        (t
8503         ;; We fetch the article.
8504         (catch 'found
8505           (dolist (gnus-override-method (gnus-refer-article-methods))
8506             (when (and (gnus-check-server gnus-override-method)
8507                        ;; Fetch the header,
8508                        (setq number (gnus-summary-insert-subject message-id)))
8509               ;; and display the article.
8510               (gnus-summary-select-article nil nil nil number)
8511               (throw 'found t)))
8512           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8513
8514 (defun gnus-refer-article-methods ()
8515   "Return a list of referable methods."
8516   (cond
8517    ;; No method, so we default to current and native.
8518    ((null gnus-refer-article-method)
8519     (list gnus-current-select-method gnus-select-method))
8520    ;; Current.
8521    ((eq 'current gnus-refer-article-method)
8522     (list gnus-current-select-method))
8523    ;; List of select methods.
8524    ((not (and (symbolp (car gnus-refer-article-method))
8525               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8526     (let (out)
8527       (dolist (method gnus-refer-article-method)
8528         (push (if (eq 'current method)
8529                   gnus-current-select-method
8530                 method)
8531               out))
8532       (nreverse out)))
8533    ;; One single select method.
8534    (t
8535     (list gnus-refer-article-method))))
8536
8537 (defun gnus-summary-edit-parameters ()
8538   "Edit the group parameters of the current group."
8539   (interactive)
8540   (gnus-group-edit-group gnus-newsgroup-name 'params))
8541
8542 (defun gnus-summary-customize-parameters ()
8543   "Customize the group parameters of the current group."
8544   (interactive)
8545   (gnus-group-customize gnus-newsgroup-name))
8546
8547 (defun gnus-summary-enter-digest-group (&optional force)
8548   "Enter an nndoc group based on the current article.
8549 If FORCE, force a digest interpretation.  If not, try
8550 to guess what the document format is."
8551   (interactive "P")
8552   (let ((conf gnus-current-window-configuration))
8553     (save-window-excursion
8554       (save-excursion
8555         (let (gnus-article-prepare-hook
8556               gnus-display-mime-function
8557               gnus-break-pages)
8558           (gnus-summary-select-article))))
8559     (setq gnus-current-window-configuration conf)
8560     (let* ((name (format "%s-%d"
8561                          (gnus-group-prefixed-name
8562                           gnus-newsgroup-name (list 'nndoc ""))
8563                          (with-current-buffer gnus-summary-buffer
8564                            gnus-current-article)))
8565            (ogroup gnus-newsgroup-name)
8566            (params (append (gnus-info-params (gnus-get-info ogroup))
8567                            (list (cons 'to-group ogroup))
8568                            (list (cons 'parent-group ogroup))
8569                            (list (cons 'save-article-group ogroup))))
8570            (case-fold-search t)
8571            (buf (current-buffer))
8572            dig to-address)
8573       (save-excursion
8574         (set-buffer gnus-original-article-buffer)
8575         ;; Have the digest group inherit the main mail address of
8576         ;; the parent article.
8577         (when (setq to-address (or (gnus-fetch-field "reply-to")
8578                                    (gnus-fetch-field "from")))
8579           (setq params (append
8580                         (list (cons 'to-address
8581                                     (funcall gnus-decode-encoded-word-function
8582                                              to-address))))))
8583         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8584         (insert-buffer-substring gnus-original-article-buffer)
8585         ;; Remove lines that may lead nndoc to misinterpret the
8586         ;; document type.
8587         (narrow-to-region
8588          (goto-char (point-min))
8589          (or (search-forward "\n\n" nil t) (point)))
8590         (goto-char (point-min))
8591         (delete-matching-lines "^Path:\\|^From ")
8592         (widen))
8593       (unwind-protect
8594           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8595                     (gnus-newsgroup-ephemeral-ignored-charsets
8596                      gnus-newsgroup-ignored-charsets))
8597                 (gnus-group-read-ephemeral-group
8598                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8599                               (nndoc-article-type
8600                                ,(if force 'mbox 'guess)))
8601                  t nil nil nil
8602                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8603                                                         "ADAPT")))))
8604               ;; Make all postings to this group go to the parent group.
8605               (nconc (gnus-info-params (gnus-get-info name))
8606                      params)
8607             ;; Couldn't select this doc group.
8608             (switch-to-buffer buf)
8609             (gnus-set-global-variables)
8610             (gnus-configure-windows 'summary)
8611             (gnus-message 3 "Article couldn't be entered?"))
8612         (kill-buffer dig)))))
8613
8614 (defun gnus-summary-read-document (n)
8615   "Open a new group based on the current article(s).
8616 This will allow you to read digests and other similar
8617 documents as newsgroups.
8618 Obeys the standard process/prefix convention."
8619   (interactive "P")
8620   (let* ((ogroup gnus-newsgroup-name)
8621          (params (append (gnus-info-params (gnus-get-info ogroup))
8622                          (list (cons 'to-group ogroup))))
8623          group egroup groups vgroup)
8624     (dolist (article (gnus-summary-work-articles n))
8625       (setq group (format "%s-%d" gnus-newsgroup-name article))
8626       (gnus-summary-remove-process-mark article)
8627       (when (gnus-summary-display-article article)
8628         (save-excursion
8629           (with-temp-buffer
8630             (insert-buffer-substring gnus-original-article-buffer)
8631             ;; Remove some headers that may lead nndoc to make
8632             ;; the wrong guess.
8633             (message-narrow-to-head)
8634             (goto-char (point-min))
8635             (delete-matching-lines "^Path:\\|^From ")
8636             (widen)
8637             (if (setq egroup
8638                       (gnus-group-read-ephemeral-group
8639                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8640                                      (nndoc-article-type guess))
8641                        t nil t))
8642                 (progn
8643             ;; Make all postings to this group go to the parent group.
8644                   (nconc (gnus-info-params (gnus-get-info egroup))
8645                          params)
8646                   (push egroup groups))
8647               ;; Couldn't select this doc group.
8648               (gnus-error 3 "Article couldn't be entered"))))))
8649     ;; Now we have selected all the documents.
8650     (cond
8651      ((not groups)
8652       (error "None of the articles could be interpreted as documents"))
8653      ((gnus-group-read-ephemeral-group
8654        (setq vgroup (format
8655                      "nnvirtual:%s-%s" gnus-newsgroup-name
8656                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8657        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8658        t
8659        (cons (current-buffer) 'summary)))
8660      (t
8661       (error "Couldn't select virtual nndoc group")))))
8662
8663 (defun gnus-summary-isearch-article (&optional regexp-p)
8664   "Do incremental search forward on the current article.
8665 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8666   (interactive "P")
8667   (gnus-summary-select-article)
8668   (gnus-configure-windows 'article)
8669   (gnus-eval-in-buffer-window gnus-article-buffer
8670     (save-restriction
8671       (widen)
8672       (isearch-forward regexp-p))))
8673
8674 (defun gnus-summary-search-article-forward (regexp &optional backward)
8675   "Search for an article containing REGEXP forward.
8676 If BACKWARD, search backward instead."
8677   (interactive
8678    (list (read-string
8679           (format "Search article %s (regexp%s): "
8680                   (if current-prefix-arg "backward" "forward")
8681                   (if gnus-last-search-regexp
8682                       (concat ", default " gnus-last-search-regexp)
8683                     "")))
8684          current-prefix-arg))
8685   (if (string-equal regexp "")
8686       (setq regexp (or gnus-last-search-regexp ""))
8687     (setq gnus-last-search-regexp regexp)
8688     (setq gnus-article-before-search gnus-current-article))
8689   ;; Intentionally set gnus-last-article.
8690   (setq gnus-last-article gnus-article-before-search)
8691   (let ((gnus-last-article gnus-last-article))
8692     (if (gnus-summary-search-article regexp backward)
8693         (gnus-summary-show-thread)
8694       (signal 'search-failed (list regexp)))))
8695
8696 (defun gnus-summary-search-article-backward (regexp)
8697   "Search for an article containing REGEXP backward."
8698   (interactive
8699    (list (read-string
8700           (format "Search article backward (regexp%s): "
8701                   (if gnus-last-search-regexp
8702                       (concat ", default " gnus-last-search-regexp)
8703                     "")))))
8704   (gnus-summary-search-article-forward regexp 'backward))
8705
8706 (defun gnus-summary-search-article (regexp &optional backward)
8707   "Search for an article containing REGEXP.
8708 Optional argument BACKWARD means do search for backward.
8709 `gnus-select-article-hook' is not called during the search."
8710   ;; We have to require this here to make sure that the following
8711   ;; dynamic binding isn't shadowed by autoloading.
8712   (require 'gnus-async)
8713   (require 'gnus-art)
8714   (let ((gnus-select-article-hook nil)  ;Disable hook.
8715         (gnus-article-prepare-hook nil)
8716         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8717         (gnus-use-article-prefetch nil)
8718         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8719         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8720         (gnus-visual nil)
8721         (gnus-keep-backlog nil)
8722         (gnus-break-pages nil)
8723         (gnus-summary-display-arrow nil)
8724         (gnus-updated-mode-lines nil)
8725         (gnus-auto-center-summary nil)
8726         (sum (current-buffer))
8727         (gnus-display-mime-function nil)
8728         (found nil)
8729         point)
8730     (gnus-save-hidden-threads
8731       (gnus-summary-select-article)
8732       (set-buffer gnus-article-buffer)
8733       (goto-char (window-point (get-buffer-window (current-buffer))))
8734       (when backward
8735         (forward-line -1))
8736       (while (not found)
8737         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8738         (if (if backward
8739                 (re-search-backward regexp nil t)
8740               (re-search-forward regexp nil t))
8741             ;; We found the regexp.
8742             (progn
8743               (setq found 'found)
8744               (beginning-of-line)
8745               (set-window-start
8746                (get-buffer-window (current-buffer))
8747                (point))
8748               (forward-line 1)
8749               (set-window-point
8750                (get-buffer-window (current-buffer))
8751                (point))
8752               (set-buffer sum)
8753               (setq point (point)))
8754           ;; We didn't find it, so we go to the next article.
8755           (set-buffer sum)
8756           (setq found 'not)
8757           (while (eq found 'not)
8758             (if (not (if backward (gnus-summary-find-prev)
8759                        (gnus-summary-find-next)))
8760                 ;; No more articles.
8761                 (setq found t)
8762               ;; Select the next article and adjust point.
8763               (unless (gnus-summary-article-sparse-p
8764                        (gnus-summary-article-number))
8765                 (setq found nil)
8766                 (gnus-summary-select-article)
8767                 (set-buffer gnus-article-buffer)
8768                 (widen)
8769                 (goto-char (if backward (point-max) (point-min))))))))
8770       (gnus-message 7 ""))
8771     ;; Return whether we found the regexp.
8772     (when (eq found 'found)
8773       (goto-char point)
8774       (gnus-summary-show-thread)
8775       (gnus-summary-goto-subject gnus-current-article)
8776       (gnus-summary-position-point)
8777       t)))
8778
8779 (defun gnus-find-matching-articles (header regexp)
8780   "Return a list of all articles that match REGEXP on HEADER.
8781 This search includes all articles in the current group that Gnus has
8782 fetched headers for, whether they are displayed or not."
8783   (let ((articles nil)
8784         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8785         (case-fold-search t))
8786     (dolist (header gnus-newsgroup-headers)
8787       (when (string-match regexp (funcall func header))
8788         (push (mail-header-number header) articles)))
8789     (nreverse articles)))
8790
8791 (defun gnus-summary-find-matching (header regexp &optional backward unread
8792                                           not-case-fold not-matching)
8793   "Return a list of all articles that match REGEXP on HEADER.
8794 The search stars on the current article and goes forwards unless
8795 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8796 If UNREAD is non-nil, only unread articles will
8797 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8798 in the comparisons. If NOT-MATCHING, return a list of all articles that
8799 not match REGEXP on HEADER."
8800   (let ((case-fold-search (not not-case-fold))
8801         articles d func)
8802     (if (consp header)
8803         (if (eq (car header) 'extra)
8804             (setq func
8805                   `(lambda (h)
8806                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8807                          "")))
8808           (error "%s is an invalid header" header))
8809       (unless (fboundp (intern (concat "mail-header-" header)))
8810         (error "%s is not a valid header" header))
8811       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8812     (dolist (d (if (eq backward 'all)
8813                    gnus-newsgroup-data
8814                  (gnus-data-find-list
8815                   (gnus-summary-article-number)
8816                   (gnus-data-list backward))))
8817       (when (and (or (not unread)       ; We want all articles...
8818                      (gnus-data-unread-p d)) ; Or just unreads.
8819                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8820                  (if not-matching
8821                      (not (string-match
8822                            regexp
8823                            (funcall func (gnus-data-header d))))
8824                    (string-match regexp
8825                                  (funcall func (gnus-data-header d)))))
8826         (push (gnus-data-number d) articles))) ; Success!
8827     (nreverse articles)))
8828
8829 (defun gnus-summary-execute-command (header regexp command &optional backward)
8830   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8831 If HEADER is an empty string (or nil), the match is done on the entire
8832 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8833   (interactive
8834    (list (let ((completion-ignore-case t))
8835            (completing-read
8836             "Header name: "
8837             (mapcar (lambda (header) (list (format "%s" header)))
8838                     (append
8839                      '("Number" "Subject" "From" "Lines" "Date"
8840                        "Message-ID" "Xref" "References" "Body")
8841                      gnus-extra-headers))
8842             nil 'require-match))
8843          (read-string "Regexp: ")
8844          (read-key-sequence "Command: ")
8845          current-prefix-arg))
8846   (when (equal header "Body")
8847     (setq header ""))
8848   ;; Hidden thread subtrees must be searched as well.
8849   (gnus-summary-show-all-threads)
8850   ;; We don't want to change current point nor window configuration.
8851   (save-excursion
8852     (save-window-excursion
8853       (let (gnus-visual
8854             gnus-treat-strip-trailing-blank-lines
8855             gnus-treat-strip-leading-blank-lines
8856             gnus-treat-strip-multiple-blank-lines
8857             gnus-treat-hide-boring-headers
8858             gnus-treat-fold-newsgroups
8859             gnus-article-prepare-hook)
8860         (gnus-message 6 "Executing %s..." (key-description command))
8861         ;; We'd like to execute COMMAND interactively so as to give arguments.
8862         (gnus-execute header regexp
8863                       `(call-interactively ',(key-binding command))
8864                       backward)
8865         (gnus-message 6 "Executing %s...done" (key-description command))))))
8866
8867 (defun gnus-summary-beginning-of-article ()
8868   "Scroll the article back to the beginning."
8869   (interactive)
8870   (gnus-summary-select-article)
8871   (gnus-configure-windows 'article)
8872   (gnus-eval-in-buffer-window gnus-article-buffer
8873     (widen)
8874     (goto-char (point-min))
8875     (when gnus-break-pages
8876       (gnus-narrow-to-page))))
8877
8878 (defun gnus-summary-end-of-article ()
8879   "Scroll to the end of the article."
8880   (interactive)
8881   (gnus-summary-select-article)
8882   (gnus-configure-windows 'article)
8883   (gnus-eval-in-buffer-window gnus-article-buffer
8884     (widen)
8885     (goto-char (point-max))
8886     (recenter -3)
8887     (when gnus-break-pages
8888       (when (re-search-backward page-delimiter nil t)
8889         (narrow-to-region (match-end 0) (point-max)))
8890       (gnus-narrow-to-page))))
8891
8892 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8893   "Truncate to LEN and quote all \"(\"'s in STRING."
8894   (gnus-replace-in-string (if (and len (> (length string) len))
8895                               (substring string 0 len)
8896                             string)
8897                           "[()]" "\\\\\\&"))
8898
8899 (defun gnus-summary-print-article (&optional filename n)
8900   "Generate and print a PostScript image of the process-marked (mail) articles.
8901
8902 If used interactively, print the current article if none are
8903 process-marked.  With prefix arg, prompt the user for the name of the
8904 file to save in.
8905
8906 When used from Lisp, accept two optional args FILENAME and N.  N means
8907 to print the next N articles.  If N is negative, print the N previous
8908 articles.  If N is nil and articles have been marked with the process
8909 mark, print these instead.
8910
8911 If the optional first argument FILENAME is nil, send the image to the
8912 printer.  If FILENAME is a string, save the PostScript image in a file with
8913 that name.  If FILENAME is a number, prompt the user for the name of the file
8914 to save in."
8915   (interactive (list (ps-print-preprint current-prefix-arg)))
8916   (dolist (article (gnus-summary-work-articles n))
8917     (gnus-summary-select-article nil nil 'pseudo article)
8918     (gnus-eval-in-buffer-window gnus-article-buffer
8919       (gnus-print-buffer))
8920     (gnus-summary-remove-process-mark article))
8921   (ps-despool filename))
8922
8923 (defun gnus-print-buffer ()
8924   (let ((buffer (generate-new-buffer " *print*")))
8925     (unwind-protect
8926         (progn
8927           (copy-to-buffer buffer (point-min) (point-max))
8928           (set-buffer buffer)
8929           (gnus-remove-text-with-property 'gnus-decoration)
8930           (when (gnus-visual-p 'article-highlight 'highlight)
8931             ;; Copy-to-buffer doesn't copy overlay.  So redo
8932             ;; highlight.
8933             (let ((gnus-article-buffer buffer))
8934               (gnus-article-highlight-citation t)
8935               (gnus-article-highlight-signature)
8936               (gnus-article-emphasize)
8937               (gnus-article-delete-invisible-text)))
8938           (let ((ps-left-header
8939                  (list
8940                   (concat "("
8941                           (gnus-summary-print-truncate-and-quote
8942                            (mail-header-subject gnus-current-headers)
8943                            66) ")")
8944                   (concat "("
8945                           (gnus-summary-print-truncate-and-quote
8946                            (mail-header-from gnus-current-headers)
8947                            45) ")")))
8948                 (ps-right-header
8949                  (list
8950                   "/pagenumberstring load"
8951                   (concat "("
8952                           (mail-header-date gnus-current-headers) ")"))))
8953             (gnus-run-hooks 'gnus-ps-print-hook)
8954             (save-excursion
8955               (if window-system
8956                   (ps-spool-buffer-with-faces)
8957                 (ps-spool-buffer)))))
8958       (kill-buffer buffer))))
8959
8960 (defun gnus-summary-show-article (&optional arg)
8961   "Force redisplaying of the current article.
8962 If ARG (the prefix) is a number, show the article with the charset
8963 defined in `gnus-summary-show-article-charset-alist', or the charset
8964 input.
8965 If ARG (the prefix) is non-nil and not a number, show the raw article
8966 without any article massaging functions being run.  Normally, the key
8967 strokes are `C-u g'."
8968   (interactive "P")
8969   (cond
8970    ((numberp arg)
8971     (gnus-summary-show-article t)
8972     (let ((gnus-newsgroup-charset
8973            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8974                (mm-read-coding-system
8975                 "View as charset: " ;; actually it is coding system.
8976                 (with-current-buffer gnus-article-buffer
8977                   (mm-detect-coding-region (point) (point-max))))))
8978           (gnus-newsgroup-ignored-charsets 'gnus-all))
8979       (gnus-summary-select-article nil 'force)
8980       (let ((deps gnus-newsgroup-dependencies)
8981             head header lines)
8982         (save-excursion
8983           (set-buffer gnus-original-article-buffer)
8984           (save-restriction
8985             (message-narrow-to-head)
8986             (setq head (buffer-string))
8987             (goto-char (point-min))
8988             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8989               (goto-char (point-max))
8990               (widen)
8991               (setq lines (1- (count-lines (point) (point-max))))))
8992           (with-temp-buffer
8993             (insert (format "211 %d Article retrieved.\n"
8994                             (cdr gnus-article-current)))
8995             (insert head)
8996             (if lines (insert (format "Lines: %d\n" lines)))
8997             (insert ".\n")
8998             (let ((nntp-server-buffer (current-buffer)))
8999               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9000         (gnus-data-set-header
9001          (gnus-data-find (cdr gnus-article-current))
9002          header)
9003         (gnus-summary-update-article-line
9004          (cdr gnus-article-current) header)
9005         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9006           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9007    ((not arg)
9008     ;; Select the article the normal way.
9009     (gnus-summary-select-article nil 'force))
9010    (t
9011     ;; We have to require this here to make sure that the following
9012     ;; dynamic binding isn't shadowed by autoloading.
9013     (require 'gnus-async)
9014     (require 'gnus-art)
9015     ;; Bind the article treatment functions to nil.
9016     (let ((gnus-have-all-headers t)
9017           gnus-article-prepare-hook
9018           gnus-article-decode-hook
9019           gnus-display-mime-function
9020           gnus-break-pages)
9021       ;; Destroy any MIME parts.
9022       (when (gnus-buffer-live-p gnus-article-buffer)
9023         (save-excursion
9024           (set-buffer gnus-article-buffer)
9025           (mm-destroy-parts gnus-article-mime-handles)
9026           ;; Set it to nil for safety reason.
9027           (setq gnus-article-mime-handle-alist nil)
9028           (setq gnus-article-mime-handles nil)))
9029       (gnus-summary-select-article nil 'force))))
9030   (gnus-summary-goto-subject gnus-current-article)
9031   (gnus-summary-position-point))
9032
9033 (defun gnus-summary-show-raw-article ()
9034   "Show the raw article without any article massaging functions being run."
9035   (interactive)
9036   (gnus-summary-show-article t))
9037
9038 (defun gnus-summary-verbose-headers (&optional arg)
9039   "Toggle permanent full header display.
9040 If ARG is a positive number, turn header display on.
9041 If ARG is a negative number, turn header display off."
9042   (interactive "P")
9043   (setq gnus-show-all-headers
9044         (cond ((or (not (numberp arg))
9045                    (zerop arg))
9046                (not gnus-show-all-headers))
9047               ((natnump arg)
9048                t)))
9049   (gnus-summary-show-article))
9050
9051 (defun gnus-summary-toggle-header (&optional arg)
9052   "Show the headers if they are hidden, or hide them if they are shown.
9053 If ARG is a positive number, show the entire header.
9054 If ARG is a negative number, hide the unwanted header lines."
9055   (interactive "P")
9056   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9057                      (get-buffer-window gnus-article-buffer t))))
9058     (with-current-buffer gnus-article-buffer
9059       (widen)
9060       (article-narrow-to-head)
9061       (let* ((buffer-read-only nil)
9062              (inhibit-point-motion-hooks t)
9063              (hidden (if (numberp arg)
9064                          (>= arg 0)
9065                        (or (not (looking-at "[^ \t\n]+:"))
9066                            (gnus-article-hidden-text-p 'headers))))
9067              s e)
9068         (delete-region (point-min) (point-max))
9069         (with-current-buffer gnus-original-article-buffer
9070           (goto-char (setq s (point-min)))
9071           (setq e (if (search-forward "\n\n" nil t)
9072                       (1- (point))
9073                     (point-max))))
9074         (insert-buffer-substring gnus-original-article-buffer s e)
9075         (run-hooks 'gnus-article-decode-hook)
9076         (if hidden
9077             (let ((gnus-treat-hide-headers nil)
9078                   (gnus-treat-hide-boring-headers nil))
9079               (gnus-delete-wash-type 'headers)
9080               (gnus-treat-article 'head))
9081           (gnus-treat-article 'head))
9082         (widen)
9083         (if window
9084             (set-window-start window (goto-char (point-min))))
9085         (if gnus-break-pages
9086             (gnus-narrow-to-page)
9087           (when (gnus-visual-p 'page-marker)
9088             (let ((buffer-read-only nil))
9089               (gnus-remove-text-with-property 'gnus-prev)
9090               (gnus-remove-text-with-property 'gnus-next))))
9091         (gnus-set-mode-line 'article)))))
9092
9093 (defun gnus-summary-show-all-headers ()
9094   "Make all header lines visible."
9095   (interactive)
9096   (gnus-summary-toggle-header 1))
9097
9098 (defun gnus-summary-caesar-message (&optional arg)
9099   "Caesar rotate the current article by 13.
9100 The numerical prefix specifies how many places to rotate each letter
9101 forward."
9102   (interactive "P")
9103   (gnus-summary-select-article)
9104   (let ((mail-header-separator ""))
9105     (gnus-eval-in-buffer-window gnus-article-buffer
9106       (save-restriction
9107         (widen)
9108         (let ((start (window-start))
9109               buffer-read-only)
9110           (message-caesar-buffer-body arg)
9111           (set-window-start (get-buffer-window (current-buffer)) start)))))
9112   ;; Create buttons and stuff...
9113   (gnus-treat-article nil))
9114
9115 (defun gnus-summary-idna-message (&optional arg)
9116   "Decode IDNA encoded domain names in the current articles.
9117 IDNA encoded domain names looks like `xn--bar'.  If a string
9118 remain unencoded after running this function, it is likely an
9119 invalid IDNA string (`xn--bar' is invalid).
9120
9121 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9122 installed for this command to work."
9123   (interactive "P")
9124   (if (not (and (condition-case nil (require 'idna)
9125                   (file-error))
9126                 (mm-coding-system-p 'utf-8)
9127                 (executable-find (symbol-value 'idna-program))))
9128       (gnus-message
9129        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9130     (gnus-summary-select-article)
9131     (let ((mail-header-separator ""))
9132       (gnus-eval-in-buffer-window gnus-article-buffer
9133         (save-restriction
9134           (widen)
9135           (let ((start (window-start))
9136                 buffer-read-only)
9137             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9138               (replace-match (idna-to-unicode (match-string 1))))
9139             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9140
9141 (autoload 'unmorse-region "morse"
9142   "Convert morse coded text in region to ordinary ASCII text."
9143   t)
9144
9145 (defun gnus-summary-morse-message (&optional arg)
9146   "Morse decode the current article."
9147   (interactive "P")
9148   (gnus-summary-select-article)
9149   (let ((mail-header-separator ""))
9150     (gnus-eval-in-buffer-window gnus-article-buffer
9151       (save-excursion
9152         (save-restriction
9153           (widen)
9154           (let ((pos (window-start))
9155                 buffer-read-only)
9156             (goto-char (point-min))
9157             (when (message-goto-body)
9158               (gnus-narrow-to-body))
9159             (goto-char (point-min))
9160             (while (search-forward "·" (point-max) t)
9161               (replace-match "."))
9162             (unmorse-region (point-min) (point-max))
9163             (widen)
9164             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9165
9166 (defun gnus-summary-stop-page-breaking ()
9167   "Stop page breaking in the current article."
9168   (interactive)
9169   (gnus-summary-select-article)
9170   (gnus-eval-in-buffer-window gnus-article-buffer
9171     (widen)
9172     (when (gnus-visual-p 'page-marker)
9173       (let ((buffer-read-only nil))
9174         (gnus-remove-text-with-property 'gnus-prev)
9175         (gnus-remove-text-with-property 'gnus-next))
9176       (setq gnus-page-broken nil))))
9177
9178 (defun gnus-summary-move-article (&optional n to-newsgroup
9179                                             select-method action)
9180   "Move the current article to a different newsgroup.
9181 If N is a positive number, move the N next articles.
9182 If N is a negative number, move the N previous articles.
9183 If N is nil and any articles have been marked with the process mark,
9184 move those articles instead.
9185 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9186 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9187 re-spool using this method.
9188
9189 When called interactively with TO-NEWSGROUP being nil, the value of
9190 the variable `gnus-move-split-methods' is used for finding a default
9191 for the target newsgroup.
9192
9193 For this function to work, both the current newsgroup and the
9194 newsgroup that you want to move to have to support the `request-move'
9195 and `request-accept' functions.
9196
9197 ACTION can be either `move' (the default), `crosspost' or `copy'."
9198   (interactive "P")
9199   (unless action
9200     (setq action 'move))
9201   ;; Check whether the source group supports the required functions.
9202   (cond ((and (eq action 'move)
9203               (not (gnus-check-backend-function
9204                     'request-move-article gnus-newsgroup-name)))
9205          (error "The current group does not support article moving"))
9206         ((and (eq action 'crosspost)
9207               (not (gnus-check-backend-function
9208                     'request-replace-article gnus-newsgroup-name)))
9209          (error "The current group does not support article editing")))
9210   (let ((articles (gnus-summary-work-articles n))
9211         (prefix (if (gnus-check-backend-function
9212                      'request-move-article gnus-newsgroup-name)
9213                     (funcall gnus-move-group-prefix-function
9214                              gnus-newsgroup-name)
9215                   ""))
9216         (names '((move "Move" "Moving")
9217                  (copy "Copy" "Copying")
9218                  (crosspost "Crosspost" "Crossposting")))
9219         (copy-buf (save-excursion
9220                     (nnheader-set-temp-buffer " *copy article*")))
9221         art-group to-method new-xref article to-groups articles-to-update-marks)
9222     (unless (assq action names)
9223       (error "Unknown action %s" action))
9224     ;; Read the newsgroup name.
9225     (when (and (not to-newsgroup)
9226                (not select-method))
9227       (if (and gnus-move-split-methods
9228                (not
9229                 (and (memq gnus-current-article articles)
9230                      (gnus-buffer-live-p gnus-original-article-buffer))))
9231           ;; When `gnus-move-split-methods' is non-nil, we have to
9232           ;; select an article to give `gnus-read-move-group-name' an
9233           ;; opportunity to suggest an appropriate default.  However,
9234           ;; we needn't render or mark the article.
9235           (let ((gnus-display-mime-function nil)
9236                 (gnus-article-prepare-hook nil)
9237                 (gnus-mark-article-hook nil))
9238             (gnus-summary-select-article nil nil nil (car articles))))
9239       (setq to-newsgroup
9240             (gnus-read-move-group-name
9241              (cadr (assq action names))
9242              (symbol-value (intern (format "gnus-current-%s-group" action)))
9243              articles prefix))
9244       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9245     (setq to-method (or select-method
9246                         (gnus-server-to-method
9247                          (gnus-group-method to-newsgroup))))
9248     ;; Check the method we are to move this article to...
9249     (unless (gnus-check-backend-function
9250              'request-accept-article (car to-method))
9251       (error "%s does not support article copying" (car to-method)))
9252     (unless (gnus-check-server to-method)
9253       (error "Can't open server %s" (car to-method)))
9254     (gnus-message 6 "%s to %s: %s..."
9255                   (caddr (assq action names))
9256                   (or (car select-method) to-newsgroup) articles)
9257     (while articles
9258       (setq article (pop articles))
9259       (setq
9260        art-group
9261        (cond
9262         ;; Move the article.
9263         ((eq action 'move)
9264          ;; Remove this article from future suppression.
9265          (gnus-dup-unsuppress-article article)
9266          (let* ((from-method (gnus-find-method-for-group
9267                               gnus-newsgroup-name))
9268                 (to-method (gnus-find-method-for-group
9269                             to-newsgroup))
9270                 (move-is-internal (gnus-method-equal from-method to-method)))
9271          (gnus-request-move-article
9272           article                       ; Article to move
9273           gnus-newsgroup-name           ; From newsgroup
9274           (nth 1 (gnus-find-method-for-group
9275                   gnus-newsgroup-name)) ; Server
9276           (list 'gnus-request-accept-article
9277                 to-newsgroup (list 'quote select-method)
9278                 (not articles) t)       ; Accept form
9279           (not articles)                ; Only save nov last time
9280           move-is-internal)))           ; is this move internal?
9281         ;; Copy the article.
9282         ((eq action 'copy)
9283          (save-excursion
9284            (set-buffer copy-buf)
9285            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9286              (save-restriction
9287                (nnheader-narrow-to-headers)
9288                (dolist (hdr gnus-copy-article-ignored-headers)
9289                  (message-remove-header hdr t)))
9290              (gnus-request-accept-article
9291               to-newsgroup select-method (not articles) t))))
9292         ;; Crosspost the article.
9293         ((eq action 'crosspost)
9294          (let ((xref (message-tokenize-header
9295                       (mail-header-xref (gnus-summary-article-header article))
9296                       " ")))
9297            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9298                                   ":" (number-to-string article)))
9299            (unless xref
9300              (setq xref (list (system-name))))
9301            (setq new-xref
9302                  (concat
9303                   (mapconcat 'identity
9304                              (delete "Xref:" (delete new-xref xref))
9305                              " ")
9306                   " " new-xref))
9307            (save-excursion
9308              (set-buffer copy-buf)
9309              ;; First put the article in the destination group.
9310              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9311              (when (consp (setq art-group
9312                                 (gnus-request-accept-article
9313                                  to-newsgroup select-method (not articles))))
9314                (setq new-xref (concat new-xref " " (car art-group)
9315                                       ":"
9316                                       (number-to-string (cdr art-group))))
9317                ;; Now we have the new Xrefs header, so we insert
9318                ;; it and replace the new article.
9319                (nnheader-replace-header "Xref" new-xref)
9320                (gnus-request-replace-article
9321                 (cdr art-group) to-newsgroup (current-buffer))
9322                art-group))))))
9323       (cond
9324        ((not art-group)
9325         (gnus-message 1 "Couldn't %s article %s: %s"
9326                       (cadr (assq action names)) article
9327                       (nnheader-get-report (car to-method))))
9328        ((eq art-group 'junk)
9329         (when (eq action 'move)
9330           (gnus-summary-mark-article article gnus-canceled-mark)
9331           (gnus-message 4 "Deleted article %s" article)
9332           ;; run the delete hook
9333           (run-hook-with-args 'gnus-summary-article-delete-hook
9334                               action
9335                               (gnus-data-header
9336                                (assoc article (gnus-data-list nil)))
9337                               gnus-newsgroup-name nil
9338                               select-method)))
9339        (t
9340         (let* ((pto-group (gnus-group-prefixed-name
9341                            (car art-group) to-method))
9342                (info (gnus-get-info pto-group))
9343                (to-group (gnus-info-group info))
9344                to-marks)
9345           ;; Update the group that has been moved to.
9346           (when (and info
9347                      (memq action '(move copy)))
9348             (unless (member to-group to-groups)
9349               (push to-group to-groups))
9350
9351             (unless (memq article gnus-newsgroup-unreads)
9352               (push 'read to-marks)
9353               (gnus-info-set-read
9354                info (gnus-add-to-range (gnus-info-read info)
9355                                        (list (cdr art-group)))))
9356
9357             ;; See whether the article is to be put in the cache.
9358             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9359                              gnus-article-mark-lists
9360                            (delete '(expirable . expire)
9361                                    (copy-sequence gnus-article-mark-lists))))
9362                   (to-article (cdr art-group)))
9363
9364               ;; Enter the article into the cache in the new group,
9365               ;; if that is required.
9366               (when gnus-use-cache
9367                 (gnus-cache-possibly-enter-article
9368                  to-group to-article
9369                  (memq article gnus-newsgroup-marked)
9370                  (memq article gnus-newsgroup-dormant)
9371                  (memq article gnus-newsgroup-unreads)))
9372
9373               (when gnus-preserve-marks
9374                 ;; Copy any marks over to the new group.
9375                 (when (and (equal to-group gnus-newsgroup-name)
9376                            (not (memq article gnus-newsgroup-unreads)))
9377                   ;; Mark this article as read in this group.
9378                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9379                   (setcdr (gnus-active to-group) to-article)
9380                   (setcdr gnus-newsgroup-active to-article))
9381
9382                 (while marks
9383                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9384                     (when (memq article (symbol-value
9385                                          (intern (format "gnus-newsgroup-%s"
9386                                                          (caar marks)))))
9387                       (push (cdar marks) to-marks)
9388                       ;; If the other group is the same as this group,
9389                       ;; then we have to add the mark to the list.
9390                       (when (equal to-group gnus-newsgroup-name)
9391                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9392                              (cons to-article
9393                                    (symbol-value
9394                                     (intern (format "gnus-newsgroup-%s"
9395                                                     (caar marks)))))))
9396                       ;; Copy the marks to other group.
9397                       (gnus-add-marked-articles
9398                        to-group (cdar marks) (list to-article) info)))
9399                   (setq marks (cdr marks)))
9400
9401                 (gnus-request-set-mark
9402                  to-group (list (list (list to-article) 'add to-marks))))
9403
9404               (gnus-dribble-enter
9405                (concat "(gnus-group-set-info '"
9406                        (gnus-prin1-to-string (gnus-get-info to-group))
9407                        ")"))))
9408
9409           ;; Update the Xref header in this article to point to
9410           ;; the new crossposted article we have just created.
9411           (when (eq action 'crosspost)
9412             (save-excursion
9413               (set-buffer copy-buf)
9414               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9415               (nnheader-replace-header "Xref" new-xref)
9416               (gnus-request-replace-article
9417                article gnus-newsgroup-name (current-buffer))))
9418
9419           ;; run the move/copy/crosspost/respool hook
9420           (run-hook-with-args 'gnus-summary-article-move-hook
9421                               action
9422                               (gnus-data-header
9423                                (assoc article (gnus-data-list nil)))
9424                               gnus-newsgroup-name
9425                               to-newsgroup
9426                               select-method))
9427
9428         ;;;!!!Why is this necessary?
9429         (set-buffer gnus-summary-buffer)
9430         
9431         (gnus-summary-goto-subject article)
9432         (when (eq action 'move)
9433           (gnus-summary-mark-article article gnus-canceled-mark))))
9434       (push article articles-to-update-marks))
9435
9436     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9437     ;; Re-activate all groups that have been moved to.
9438     (save-excursion
9439       (set-buffer gnus-group-buffer)
9440       (let ((gnus-group-marked to-groups))
9441         (gnus-group-get-new-news-this-group nil t)))
9442     
9443     (gnus-kill-buffer copy-buf)
9444     (gnus-summary-position-point)
9445     (gnus-set-mode-line 'summary)))
9446
9447 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9448   "Copy the current article to some other group.
9449 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9450 When called interactively, if TO-NEWSGROUP is nil, use the value of
9451 the variable `gnus-move-split-methods' for finding a default target
9452 newsgroup.
9453 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9454 re-spool using this method."
9455   (interactive "P")
9456   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9457
9458 (defun gnus-summary-crosspost-article (&optional n)
9459   "Crosspost the current article to some other group."
9460   (interactive "P")
9461   (gnus-summary-move-article n nil nil 'crosspost))
9462
9463 (defcustom gnus-summary-respool-default-method nil
9464   "Default method type for respooling an article.
9465 If nil, use to the current newsgroup method."
9466   :type 'symbol
9467   :group 'gnus-summary-mail)
9468
9469 (defcustom gnus-summary-display-while-building nil
9470   "If non-nil, show and update the summary buffer as it's being built.
9471 If the value is t, update the buffer after every line is inserted.  If
9472 the value is an integer (N), update the display every N lines."
9473   :version "22.1"
9474   :group 'gnus-thread
9475   :type '(choice (const :tag "off" nil)
9476                  number
9477                  (const :tag "frequently" t)))
9478
9479 (defun gnus-summary-respool-article (&optional n method)
9480   "Respool the current article.
9481 The article will be squeezed through the mail spooling process again,
9482 which means that it will be put in some mail newsgroup or other
9483 depending on `nnmail-split-methods'.
9484 If N is a positive number, respool the N next articles.
9485 If N is a negative number, respool the N previous articles.
9486 If N is nil and any articles have been marked with the process mark,
9487 respool those articles instead.
9488
9489 Respooling can be done both from mail groups and \"real\" newsgroups.
9490 In the former case, the articles in question will be moved from the
9491 current group into whatever groups they are destined to.  In the
9492 latter case, they will be copied into the relevant groups."
9493   (interactive
9494    (list current-prefix-arg
9495          (let* ((methods (gnus-methods-using 'respool))
9496                 (methname
9497                  (symbol-name (or gnus-summary-respool-default-method
9498                                   (car (gnus-find-method-for-group
9499                                         gnus-newsgroup-name)))))
9500                 (method
9501                  (gnus-completing-read-with-default
9502                   methname "Backend to use when respooling"
9503                   methods nil t nil 'gnus-mail-method-history))
9504                 ms)
9505            (cond
9506             ((zerop (length (setq ms (gnus-servers-using-backend
9507                                       (intern method)))))
9508              (list (intern method) ""))
9509             ((= 1 (length ms))
9510              (car ms))
9511             (t
9512              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9513                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9514                            ms-alist))))))))
9515   (unless method
9516     (error "No method given for respooling"))
9517   (if (assoc (symbol-name
9518               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9519              (gnus-methods-using 'respool))
9520       (gnus-summary-move-article n nil method)
9521     (gnus-summary-copy-article n nil method)))
9522
9523 (defun gnus-summary-import-article (file &optional edit)
9524   "Import an arbitrary file into a mail newsgroup."
9525   (interactive "fImport file: \nP")
9526   (let ((group gnus-newsgroup-name)
9527         (now (current-time))
9528         atts lines group-art)
9529     (unless (gnus-check-backend-function 'request-accept-article group)
9530       (error "%s does not support article importing" group))
9531     (or (file-readable-p file)
9532         (not (file-regular-p file))
9533         (error "Can't read %s" file))
9534     (save-excursion
9535       (set-buffer (gnus-get-buffer-create " *import file*"))
9536       (erase-buffer)
9537       (nnheader-insert-file-contents file)
9538       (goto-char (point-min))
9539       (if (nnheader-article-p)
9540           (save-restriction
9541             (goto-char (point-min))
9542             (search-forward "\n\n" nil t)
9543             (narrow-to-region (point-min) (1- (point)))
9544             (goto-char (point-min))
9545             (unless (re-search-forward "^date:" nil t)
9546               (goto-char (point-max))
9547               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9548        ;; This doesn't look like an article, so we fudge some headers.
9549         (setq atts (file-attributes file)
9550               lines (count-lines (point-min) (point-max)))
9551         (insert "From: " (read-string "From: ") "\n"
9552                 "Subject: " (read-string "Subject: ") "\n"
9553                 "Date: " (message-make-date (nth 5 atts)) "\n"
9554                 "Message-ID: " (message-make-message-id) "\n"
9555                 "Lines: " (int-to-string lines) "\n"
9556                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9557       (setq group-art (gnus-request-accept-article group nil t))
9558       (kill-buffer (current-buffer)))
9559     (setq gnus-newsgroup-active (gnus-activate-group group))
9560     (forward-line 1)
9561     (gnus-summary-goto-article (cdr group-art) nil t)
9562     (when edit
9563       (gnus-summary-edit-article))))
9564
9565 (defun gnus-summary-create-article ()
9566   "Create an article in a mail newsgroup."
9567   (interactive)
9568   (let ((group gnus-newsgroup-name)
9569         (now (current-time))
9570         group-art)
9571     (unless (gnus-check-backend-function 'request-accept-article group)
9572       (error "%s does not support article importing" group))
9573     (save-excursion
9574       (set-buffer (gnus-get-buffer-create " *import file*"))
9575       (erase-buffer)
9576       (goto-char (point-min))
9577       ;; This doesn't look like an article, so we fudge some headers.
9578       (insert "From: " (read-string "From: ") "\n"
9579               "Subject: " (read-string "Subject: ") "\n"
9580               "Date: " (message-make-date now) "\n"
9581               "Message-ID: " (message-make-message-id) "\n")
9582       (setq group-art (gnus-request-accept-article group nil t))
9583       (kill-buffer (current-buffer)))
9584     (setq gnus-newsgroup-active (gnus-activate-group group))
9585     (forward-line 1)
9586     (gnus-summary-goto-article (cdr group-art) nil t)
9587     (gnus-summary-edit-article)))
9588
9589 (defun gnus-summary-article-posted-p ()
9590   "Say whether the current (mail) article is available from news as well.
9591 This will be the case if the article has both been mailed and posted."
9592   (interactive)
9593   (let ((id (mail-header-references (gnus-summary-article-header)))
9594         (gnus-override-method (car (gnus-refer-article-methods))))
9595     (if (gnus-request-head id "")
9596         (gnus-message 2 "The current message was found on %s"
9597                       gnus-override-method)
9598       (gnus-message 2 "The current message couldn't be found on %s"
9599                     gnus-override-method)
9600       nil)))
9601
9602 (defun gnus-summary-expire-articles (&optional now)
9603   "Expire all articles that are marked as expirable in the current group."
9604   (interactive)
9605   (when (and (not gnus-group-is-exiting-without-update-p)
9606              (gnus-check-backend-function
9607               'request-expire-articles gnus-newsgroup-name))
9608     ;; This backend supports expiry.
9609     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9610            (expirable (if total
9611                           (progn
9612                             ;; We need to update the info for
9613                             ;; this group for `gnus-list-of-read-articles'
9614                             ;; to give us the right answer.
9615                             (gnus-run-hooks 'gnus-exit-group-hook)
9616                             (gnus-summary-update-info)
9617                             (gnus-list-of-read-articles gnus-newsgroup-name))
9618                         (setq gnus-newsgroup-expirable
9619                               (sort gnus-newsgroup-expirable '<))))
9620            (expiry-wait (if now 'immediate
9621                           (gnus-group-find-parameter
9622                            gnus-newsgroup-name 'expiry-wait)))
9623            (nnmail-expiry-target
9624             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9625                 nnmail-expiry-target))
9626            es)
9627       (when expirable
9628         ;; There are expirable articles in this group, so we run them
9629         ;; through the expiry process.
9630         (gnus-message 6 "Expiring articles...")
9631         (unless (gnus-check-group gnus-newsgroup-name)
9632           (error "Can't open server for %s" gnus-newsgroup-name))
9633         ;; The list of articles that weren't expired is returned.
9634         (save-excursion
9635           (if expiry-wait
9636               (let ((nnmail-expiry-wait-function nil)
9637                     (nnmail-expiry-wait expiry-wait))
9638                 (setq es (gnus-request-expire-articles
9639                           expirable gnus-newsgroup-name)))
9640             (setq es (gnus-request-expire-articles
9641                       expirable gnus-newsgroup-name)))
9642           (unless total
9643             (setq gnus-newsgroup-expirable es))
9644           ;; We go through the old list of expirable, and mark all
9645           ;; really expired articles as nonexistent.
9646           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9647             (let ((gnus-use-cache nil))
9648               (dolist (article expirable)
9649                 (when (and (not (memq article es))
9650                            (gnus-data-find article))
9651                   (gnus-summary-mark-article article gnus-canceled-mark)
9652                   (run-hook-with-args 'gnus-summary-article-expire-hook
9653                                       'delete
9654                                       (gnus-data-header
9655                                        (assoc article (gnus-data-list nil)))
9656                                       gnus-newsgroup-name
9657                                       nil
9658                                       nil))))))
9659         (gnus-message 6 "Expiring articles...done")))))
9660
9661 (defun gnus-summary-expire-articles-now ()
9662   "Expunge all expirable articles in the current group.
9663 This means that *all* articles that are marked as expirable will be
9664 deleted forever, right now."
9665   (interactive)
9666   (or gnus-expert-user
9667       (gnus-yes-or-no-p
9668        "Are you really, really, really sure you want to delete all these messages? ")
9669       (error "Phew!"))
9670   (gnus-summary-expire-articles t))
9671
9672 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9673 (defun gnus-summary-delete-article (&optional n)
9674   "Delete the N next (mail) articles.
9675 This command actually deletes articles.  This is not a marking
9676 command.  The article will disappear forever from your life, never to
9677 return.
9678
9679 If N is negative, delete backwards.
9680 If N is nil and articles have been marked with the process mark,
9681 delete these instead.
9682
9683 If `gnus-novice-user' is non-nil you will be asked for
9684 confirmation before the articles are deleted."
9685   (interactive "P")
9686   (unless (gnus-check-backend-function 'request-expire-articles
9687                                        gnus-newsgroup-name)
9688     (error "The current newsgroup does not support article deletion"))
9689   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9690     (error "Couldn't open server"))
9691   ;; Compute the list of articles to delete.
9692   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9693         (nnmail-expiry-target 'delete)
9694         not-deleted)
9695     (if (and gnus-novice-user
9696              (not (gnus-yes-or-no-p
9697                    (format "Do you really want to delete %s forever? "
9698                            (if (> (length articles) 1)
9699                                (format "these %s articles" (length articles))
9700                              "this article")))))
9701         ()
9702       ;; Delete the articles.
9703       (setq not-deleted (gnus-request-expire-articles
9704                          articles gnus-newsgroup-name 'force))
9705       (while articles
9706         (gnus-summary-remove-process-mark (car articles))
9707         ;; The backend might not have been able to delete the article
9708         ;; after all.
9709         (unless (memq (car articles) not-deleted)
9710           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9711         (let* ((article (car articles))
9712                (ghead  (gnus-data-header
9713                                     (assoc article (gnus-data-list nil)))))
9714           (run-hook-with-args 'gnus-summary-article-delete-hook
9715                               'delete ghead gnus-newsgroup-name nil
9716                               nil))
9717         (setq articles (cdr articles)))
9718       (when not-deleted
9719         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9720     (gnus-summary-position-point)
9721     (gnus-set-mode-line 'summary)
9722     not-deleted))
9723
9724 (defun gnus-summary-edit-article (&optional arg)
9725   "Edit the current article.
9726 This will have permanent effect only in mail groups.
9727 If ARG is nil, edit the decoded articles.
9728 If ARG is 1, edit the raw articles.
9729 If ARG is 2, edit the raw articles even in read-only groups.
9730 If ARG is 3, edit the articles with the current handles.
9731 Otherwise, allow editing of articles even in read-only
9732 groups."
9733   (interactive "P")
9734   (let (force raw current-handles)
9735     (cond
9736      ((null arg))
9737      ((eq arg 1)
9738       (setq raw t))
9739      ((eq arg 2)
9740       (setq raw t
9741             force t))
9742      ((eq arg 3)
9743       (setq current-handles
9744             (and (gnus-buffer-live-p gnus-article-buffer)
9745                  (with-current-buffer gnus-article-buffer
9746                    (prog1
9747                        gnus-article-mime-handles
9748                      (setq gnus-article-mime-handles nil))))))
9749      (t
9750       (setq force t)))
9751     (when (and raw (not force)
9752                (member gnus-newsgroup-name '("nndraft:delayed"
9753                                              "nndraft:drafts"
9754                                              "nndraft:queue")))
9755       (error "Can't edit the raw article in group %s"
9756              gnus-newsgroup-name))
9757     (save-excursion
9758       (set-buffer gnus-summary-buffer)
9759       (let ((mail-parse-charset gnus-newsgroup-charset)
9760             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9761         (gnus-set-global-variables)
9762         (when (and (not force)
9763                    (gnus-group-read-only-p))
9764           (error "The current newsgroup does not support article editing"))
9765         (gnus-summary-show-article t)
9766         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9767           (with-current-buffer gnus-article-buffer
9768             (mm-enable-multibyte)))
9769         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9770             (setq raw t))
9771         (gnus-article-edit-article
9772          (if raw 'ignore
9773            `(lambda ()
9774               (let ((mbl mml-buffer-list))
9775                 (setq mml-buffer-list nil)
9776                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9777                   (mime-to-mml ,'current-handles))
9778                 (let ((mbl1 mml-buffer-list))
9779                   (setq mml-buffer-list mbl)
9780                   (set (make-local-variable 'mml-buffer-list) mbl1))
9781                 (gnus-make-local-hook 'kill-buffer-hook)
9782                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9783          `(lambda (no-highlight)
9784             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9785                   (message-options message-options)
9786                   (message-options-set-recipient)
9787                   (mail-parse-ignored-charsets
9788                    ',gnus-newsgroup-ignored-charsets))
9789               ,(if (not raw) '(progn
9790                                 (mml-to-mime)
9791                                 (mml-destroy-buffers)
9792                                 (remove-hook 'kill-buffer-hook
9793                                              'mml-destroy-buffers t)
9794                                 (kill-local-variable 'mml-buffer-list)))
9795               (gnus-summary-edit-article-done
9796                ,(or (mail-header-references gnus-current-headers) "")
9797                ,(gnus-group-read-only-p)
9798                ,gnus-summary-buffer no-highlight))))))))
9799
9800 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9801
9802 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9803                                                  no-highlight)
9804   "Make edits to the current article permanent."
9805   (interactive)
9806   (save-excursion
9807    ;; The buffer restriction contains the entire article if it exists.
9808     (when (article-goto-body)
9809       (let ((lines (count-lines (point) (point-max)))
9810             (length (- (point-max) (point)))
9811             (case-fold-search t)
9812             (body (copy-marker (point))))
9813         (goto-char (point-min))
9814         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9815           (delete-region (match-beginning 1) (match-end 1))
9816           (insert (number-to-string length)))
9817         (goto-char (point-min))
9818         (when (re-search-forward
9819                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9820           (delete-region (match-beginning 1) (match-end 1))
9821           (insert (number-to-string length)))
9822         (goto-char (point-min))
9823         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9824           (delete-region (match-beginning 1) (match-end 1))
9825           (insert (number-to-string lines))))))
9826   ;; Replace the article.
9827   (let ((buf (current-buffer)))
9828     (with-temp-buffer
9829       (insert-buffer-substring buf)
9830
9831       (if (and (not read-only)
9832                (not (gnus-request-replace-article
9833                      (cdr gnus-article-current) (car gnus-article-current)
9834                      (current-buffer) t)))
9835           (error "Couldn't replace article")
9836         ;; Update the summary buffer.
9837         (if (and references
9838                  (equal (message-tokenize-header references " ")
9839                         (message-tokenize-header
9840                          (or (message-fetch-field "references") "") " ")))
9841             ;; We only have to update this line.
9842             (save-excursion
9843               (save-restriction
9844                 (message-narrow-to-head)
9845                 (let ((head (buffer-substring-no-properties
9846                              (point-min) (point-max)))
9847                       header)
9848                   (with-temp-buffer
9849                     (insert (format "211 %d Article retrieved.\n"
9850                                     (cdr gnus-article-current)))
9851                     (insert head)
9852                     (insert ".\n")
9853                     (let ((nntp-server-buffer (current-buffer)))
9854                       (setq header (car (gnus-get-newsgroup-headers
9855                                          nil t))))
9856                     (save-excursion
9857                       (set-buffer gnus-summary-buffer)
9858                       (gnus-data-set-header
9859                        (gnus-data-find (cdr gnus-article-current))
9860                        header)
9861                       (gnus-summary-update-article-line
9862                        (cdr gnus-article-current) header)
9863                       (if (gnus-summary-goto-subject
9864                            (cdr gnus-article-current) nil t)
9865                           (gnus-summary-update-secondary-mark
9866                            (cdr gnus-article-current))))))))
9867           ;; Update threads.
9868           (set-buffer (or buffer gnus-summary-buffer))
9869           (gnus-summary-update-article (cdr gnus-article-current))
9870           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9871               (gnus-summary-update-secondary-mark
9872                (cdr gnus-article-current))))
9873         ;; Prettify the article buffer again.
9874         (unless no-highlight
9875           (save-excursion
9876             (set-buffer gnus-article-buffer)
9877             ;;;!!! Fix this -- article should be rehighlighted.
9878             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9879             (set-buffer gnus-original-article-buffer)
9880             (gnus-request-article
9881              (cdr gnus-article-current)
9882              (car gnus-article-current) (current-buffer))))
9883         ;; Prettify the summary buffer line.
9884         (when (gnus-visual-p 'summary-highlight 'highlight)
9885           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9886
9887 (defun gnus-summary-edit-wash (key)
9888   "Perform editing command KEY in the article buffer."
9889   (interactive
9890    (list
9891     (progn
9892       (message "%s" (concat (this-command-keys) "- "))
9893       (read-char))))
9894   (message "")
9895   (gnus-summary-edit-article)
9896   (execute-kbd-macro (concat (this-command-keys) key))
9897   (gnus-article-edit-done))
9898
9899 ;;; Respooling
9900
9901 (defun gnus-summary-respool-query (&optional silent trace)
9902   "Query where the respool algorithm would put this article."
9903   (interactive)
9904   (let (gnus-mark-article-hook)
9905     (gnus-summary-select-article)
9906     (save-excursion
9907       (set-buffer gnus-original-article-buffer)
9908       (let ((groups (nnmail-article-group 'identity trace)))
9909         (unless silent
9910           (if groups
9911               (message "This message would go to %s"
9912                        (mapconcat 'car groups ", "))
9913             (message "This message would go to no groups"))
9914           groups)))))
9915
9916 (defun gnus-summary-respool-trace ()
9917   "Trace where the respool algorithm would put this article.
9918 Display a buffer showing all fancy splitting patterns which matched."
9919   (interactive)
9920   (gnus-summary-respool-query nil t))
9921
9922 ;; Summary marking commands.
9923
9924 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9925   "Mark articles which has the same subject as read, and then select the next.
9926 If UNMARK is positive, remove any kind of mark.
9927 If UNMARK is negative, tick articles."
9928   (interactive "P")
9929   (when unmark
9930     (setq unmark (prefix-numeric-value unmark)))
9931   (let ((count
9932          (gnus-summary-mark-same-subject
9933           (gnus-summary-article-subject) unmark)))
9934     ;; Select next unread article.  If auto-select-same mode, should
9935     ;; select the first unread article.
9936     (gnus-summary-next-article t (and gnus-auto-select-same
9937                                       (gnus-summary-article-subject)))
9938     (gnus-message 7 "%d article%s marked as %s"
9939                   count (if (= count 1) " is" "s are")
9940                   (if unmark "unread" "read"))))
9941
9942 (defun gnus-summary-kill-same-subject (&optional unmark)
9943   "Mark articles which has the same subject as read.
9944 If UNMARK is positive, remove any kind of mark.
9945 If UNMARK is negative, tick articles."
9946   (interactive "P")
9947   (when unmark
9948     (setq unmark (prefix-numeric-value unmark)))
9949   (let ((count
9950          (gnus-summary-mark-same-subject
9951           (gnus-summary-article-subject) unmark)))
9952     ;; If marked as read, go to next unread subject.
9953     (when (null unmark)
9954       ;; Go to next unread subject.
9955       (gnus-summary-next-subject 1 t))
9956     (gnus-message 7 "%d articles are marked as %s"
9957                   count (if unmark "unread" "read"))))
9958
9959 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9960   "Mark articles with same SUBJECT as read, and return marked number.
9961 If optional argument UNMARK is positive, remove any kinds of marks.
9962 If optional argument UNMARK is negative, mark articles as unread instead."
9963   (let ((count 1))
9964     (save-excursion
9965       (cond
9966        ((null unmark)                   ; Mark as read.
9967         (while (and
9968                 (progn
9969                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9970                   (gnus-summary-show-thread) t)
9971                 (gnus-summary-find-subject subject))
9972           (setq count (1+ count))))
9973        ((> unmark 0)                    ; Tick.
9974         (while (and
9975                 (progn
9976                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9977                   (gnus-summary-show-thread) t)
9978                 (gnus-summary-find-subject subject))
9979           (setq count (1+ count))))
9980        (t                               ; Mark as unread.
9981         (while (and
9982                 (progn
9983                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9984                   (gnus-summary-show-thread) t)
9985                 (gnus-summary-find-subject subject))
9986           (setq count (1+ count)))))
9987       (gnus-set-mode-line 'summary)
9988       ;; Return the number of marked articles.
9989       count)))
9990
9991 (defun gnus-summary-mark-as-processable (n &optional unmark)
9992   "Set the process mark on the next N articles.
9993 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9994 the process mark instead.  The difference between N and the actual
9995 number of articles marked is returned."
9996   (interactive "P")
9997   (if (and (null n) (gnus-region-active-p))
9998       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9999     (setq n (prefix-numeric-value n))
10000     (let ((backward (< n 0))
10001           (n (abs n)))
10002       (while (and
10003               (> n 0)
10004               (if unmark
10005                   (gnus-summary-remove-process-mark
10006                    (gnus-summary-article-number))
10007                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10008               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10009         (setq n (1- n)))
10010       (when (/= 0 n)
10011         (gnus-message 7 "No more articles"))
10012       (gnus-summary-recenter)
10013       (gnus-summary-position-point)
10014       n)))
10015
10016 (defun gnus-summary-unmark-as-processable (n)
10017   "Remove the process mark from the next N articles.
10018 If N is negative, unmark backward instead.  The difference between N and
10019 the actual number of articles unmarked is returned."
10020   (interactive "P")
10021   (gnus-summary-mark-as-processable n t))
10022
10023 (defun gnus-summary-unmark-all-processable ()
10024   "Remove the process mark from all articles."
10025   (interactive)
10026   (save-excursion
10027     (while gnus-newsgroup-processable
10028       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10029   (gnus-summary-position-point))
10030
10031 (defun gnus-summary-add-mark (article type)
10032   "Mark ARTICLE with a mark of TYPE."
10033   (let ((vtype (car (assq type gnus-article-mark-lists)))
10034         var)
10035     (if (not vtype)
10036         (error "No such mark type: %s" type)
10037       (setq var (intern (format "gnus-newsgroup-%s" type)))
10038       (set var (cons article (symbol-value var)))
10039       (if (memq type '(processable cached replied forwarded recent saved))
10040           (gnus-summary-update-secondary-mark article)
10041         ;;; !!! This is bogus.  We should find out what primary
10042         ;;; !!! mark we want to set.
10043         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10044
10045 (defun gnus-summary-mark-as-expirable (n)
10046   "Mark N articles forward as expirable.
10047 If N is negative, mark backward instead.  The difference between N and
10048 the actual number of articles marked is returned."
10049   (interactive "p")
10050   (gnus-summary-mark-forward n gnus-expirable-mark))
10051
10052 (defun gnus-summary-mark-as-spam (n)
10053   "Mark N articles forward as spam.
10054 If N is negative, mark backward instead.  The difference between N and
10055 the actual number of articles marked is returned."
10056   (interactive "p")
10057   (gnus-summary-mark-forward n gnus-spam-mark))
10058
10059 (defun gnus-summary-mark-article-as-replied (article)
10060   "Mark ARTICLE as replied to and update the summary line.
10061 ARTICLE can also be a list of articles."
10062   (interactive (list (gnus-summary-article-number)))
10063   (let ((articles (if (listp article) article (list article))))
10064     (dolist (article articles)
10065       (unless (numberp article)
10066         (error "%s is not a number" article))
10067       (push article gnus-newsgroup-replied)
10068       (let ((buffer-read-only nil))
10069         (when (gnus-summary-goto-subject article nil t)
10070           (gnus-summary-update-secondary-mark article))))))
10071
10072 (defun gnus-summary-mark-article-as-forwarded (article)
10073   "Mark ARTICLE as forwarded and update the summary line.
10074 ARTICLE can also be a list of articles."
10075   (let ((articles (if (listp article) article (list article))))
10076     (dolist (article articles)
10077       (push article gnus-newsgroup-forwarded)
10078       (let ((buffer-read-only nil))
10079         (when (gnus-summary-goto-subject article nil t)
10080           (gnus-summary-update-secondary-mark article))))))
10081
10082 (defun gnus-summary-set-bookmark (article)
10083   "Set a bookmark in current article."
10084   (interactive (list (gnus-summary-article-number)))
10085   (when (or (not (get-buffer gnus-article-buffer))
10086             (not gnus-current-article)
10087             (not gnus-article-current)
10088             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10089     (error "No current article selected"))
10090   ;; Remove old bookmark, if one exists.
10091   (gnus-pull article gnus-newsgroup-bookmarks)
10092   ;; Set the new bookmark, which is on the form
10093   ;; (article-number . line-number-in-body).
10094   (push
10095    (cons article
10096          (with-current-buffer gnus-article-buffer
10097            (count-lines
10098             (min (point)
10099                  (save-excursion
10100                    (article-goto-body)
10101                    (point)))
10102             (point))))
10103    gnus-newsgroup-bookmarks)
10104   (gnus-message 6 "A bookmark has been added to the current article."))
10105
10106 (defun gnus-summary-remove-bookmark (article)
10107   "Remove the bookmark from the current article."
10108   (interactive (list (gnus-summary-article-number)))
10109   ;; Remove old bookmark, if one exists.
10110   (if (not (assq article gnus-newsgroup-bookmarks))
10111       (gnus-message 6 "No bookmark in current article.")
10112     (gnus-pull article gnus-newsgroup-bookmarks)
10113     (gnus-message 6 "Removed bookmark.")))
10114
10115 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10116 (defun gnus-summary-mark-as-dormant (n)
10117   "Mark N articles forward as dormant.
10118 If N is negative, mark backward instead.  The difference between N and
10119 the actual number of articles marked is returned."
10120   (interactive "p")
10121   (gnus-summary-mark-forward n gnus-dormant-mark))
10122
10123 (defun gnus-summary-set-process-mark (article)
10124   "Set the process mark on ARTICLE and update the summary line."
10125   (setq gnus-newsgroup-processable
10126         (cons article
10127               (delq article gnus-newsgroup-processable)))
10128   (when (gnus-summary-goto-subject article)
10129     (gnus-summary-show-thread)
10130     (gnus-summary-goto-subject article)
10131     (gnus-summary-update-secondary-mark article)))
10132
10133 (defun gnus-summary-remove-process-mark (&rest articles)
10134   "Remove the process mark from ARTICLES and update the summary line."
10135   (dolist (article articles)
10136     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10137     (when (gnus-summary-goto-subject article)
10138       (gnus-summary-show-thread)
10139       (gnus-summary-goto-subject article)
10140       (gnus-summary-update-secondary-mark article)))
10141   t)
10142
10143 (defun gnus-summary-set-saved-mark (article)
10144   "Set the process mark on ARTICLE and update the summary line."
10145   (push article gnus-newsgroup-saved)
10146   (when (gnus-summary-goto-subject article)
10147     (gnus-summary-update-secondary-mark article)))
10148
10149 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10150   "Mark N articles as read forwards.
10151 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10152 The difference between N and the actual number of articles marked is
10153 returned.
10154 If NO-EXPIRE, auto-expiry will be inhibited."
10155   (interactive "p")
10156   (gnus-summary-show-thread)
10157   (let ((backward (< n 0))
10158         (gnus-summary-goto-unread
10159          (and gnus-summary-goto-unread
10160               (not (eq gnus-summary-goto-unread 'never))
10161               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10162                                     gnus-ticked-mark gnus-dormant-mark)))))
10163         (n (abs n))
10164         (mark (or mark gnus-del-mark)))
10165     (while (and (> n 0)
10166                 (gnus-summary-mark-article nil mark no-expire)
10167                 (zerop (gnus-summary-next-subject
10168                         (if backward -1 1)
10169                         (and gnus-summary-goto-unread
10170                              (not (eq gnus-summary-goto-unread 'never)))
10171                         t)))
10172       (setq n (1- n)))
10173     (when (/= 0 n)
10174       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10175     (gnus-summary-recenter)
10176     (gnus-summary-position-point)
10177     (gnus-set-mode-line 'summary)
10178     n))
10179
10180 (defun gnus-summary-mark-article-as-read (mark)
10181   "Mark the current article quickly as read with MARK."
10182   (let ((article (gnus-summary-article-number)))
10183     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10184     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10185     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10186     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10187     (push (cons article mark) gnus-newsgroup-reads)
10188     ;; Possibly remove from cache, if that is used.
10189     (when gnus-use-cache
10190       (gnus-cache-enter-remove-article article))
10191     ;; Allow the backend to change the mark.
10192     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10193     ;; Check for auto-expiry.
10194     (when (and gnus-newsgroup-auto-expire
10195                (memq mark gnus-auto-expirable-marks))
10196       (setq mark gnus-expirable-mark)
10197       ;; Let the backend know about the mark change.
10198       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10199       (push article gnus-newsgroup-expirable))
10200     ;; Set the mark in the buffer.
10201     (gnus-summary-update-mark mark 'unread)
10202     t))
10203
10204 (defun gnus-summary-mark-article-as-unread (mark)
10205   "Mark the current article quickly as unread with MARK."
10206   (let* ((article (gnus-summary-article-number))
10207          (old-mark (gnus-summary-article-mark article)))
10208     ;; Allow the backend to change the mark.
10209     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10210     (if (eq mark old-mark)
10211         t
10212       (if (<= article 0)
10213           (progn
10214             (gnus-error 1 "Can't mark negative article numbers")
10215             nil)
10216         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10217         (setq gnus-newsgroup-spam-marked
10218               (delq article gnus-newsgroup-spam-marked))
10219         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10220         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10221         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10222         (cond ((= mark gnus-ticked-mark)
10223                (setq gnus-newsgroup-marked
10224                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10225                                               article)))
10226               ((= mark gnus-spam-mark)
10227                (setq gnus-newsgroup-spam-marked
10228                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10229                                               article)))
10230               ((= mark gnus-dormant-mark)
10231                (setq gnus-newsgroup-dormant
10232                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10233                                               article)))
10234               (t
10235                (setq gnus-newsgroup-unreads
10236                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10237                                               article))))
10238         (gnus-pull article gnus-newsgroup-reads)
10239
10240         ;; See whether the article is to be put in the cache.
10241         (and gnus-use-cache
10242              (vectorp (gnus-summary-article-header article))
10243              (save-excursion
10244                (gnus-cache-possibly-enter-article
10245                 gnus-newsgroup-name article
10246                 (= mark gnus-ticked-mark)
10247                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10248
10249         ;; Fix the mark.
10250         (gnus-summary-update-mark mark 'unread)
10251         t))))
10252
10253 (defun gnus-summary-mark-article (&optional article mark no-expire)
10254   "Mark ARTICLE with MARK.  MARK can be any character.
10255 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10256 `??' (dormant) and `?E' (expirable).
10257 If MARK is nil, then the default character `?r' is used.
10258 If ARTICLE is nil, then the article on the current line will be
10259 marked.
10260 If NO-EXPIRE, auto-expiry will be inhibited."
10261   ;; The mark might be a string.
10262   (when (stringp mark)
10263     (setq mark (aref mark 0)))
10264   ;; If no mark is given, then we check auto-expiring.
10265   (when (null mark)
10266     (setq mark gnus-del-mark))
10267   (when (and (not no-expire)
10268              gnus-newsgroup-auto-expire
10269              (memq mark gnus-auto-expirable-marks))
10270     (setq mark gnus-expirable-mark))
10271   (let ((article (or article (gnus-summary-article-number)))
10272         (old-mark (gnus-summary-article-mark article)))
10273     ;; Allow the backend to change the mark.
10274     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10275     (if (eq mark old-mark)
10276         t
10277       (unless article
10278         (error "No article on current line"))
10279       (if (not (if (or (= mark gnus-unread-mark)
10280                        (= mark gnus-ticked-mark)
10281                        (= mark gnus-spam-mark)
10282                        (= mark gnus-dormant-mark))
10283                    (gnus-mark-article-as-unread article mark)
10284                  (gnus-mark-article-as-read article mark)))
10285           t
10286         ;; See whether the article is to be put in the cache.
10287         (and gnus-use-cache
10288              (not (= mark gnus-canceled-mark))
10289              (vectorp (gnus-summary-article-header article))
10290              (save-excursion
10291                (gnus-cache-possibly-enter-article
10292                 gnus-newsgroup-name article
10293                 (= mark gnus-ticked-mark)
10294                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10295
10296         (when (gnus-summary-goto-subject article nil t)
10297           (let ((buffer-read-only nil))
10298             (gnus-summary-show-thread)
10299             ;; Fix the mark.
10300             (gnus-summary-update-mark mark 'unread)
10301             t))))))
10302
10303 (defun gnus-summary-update-secondary-mark (article)
10304   "Update the secondary (read, process, cache) mark."
10305   (gnus-summary-update-mark
10306    (cond ((memq article gnus-newsgroup-processable)
10307           gnus-process-mark)
10308          ((memq article gnus-newsgroup-cached)
10309           gnus-cached-mark)
10310          ((memq article gnus-newsgroup-replied)
10311           gnus-replied-mark)
10312          ((memq article gnus-newsgroup-forwarded)
10313           gnus-forwarded-mark)
10314          ((memq article gnus-newsgroup-saved)
10315           gnus-saved-mark)
10316          ((memq article gnus-newsgroup-recent)
10317           gnus-recent-mark)
10318          ((memq article gnus-newsgroup-unseen)
10319           gnus-unseen-mark)
10320          (t gnus-no-mark))
10321    'replied)
10322   (when (gnus-visual-p 'summary-highlight 'highlight)
10323     (gnus-run-hooks 'gnus-summary-update-hook))
10324   t)
10325
10326 (defun gnus-summary-update-download-mark (article)
10327   "Update the download mark."
10328   (gnus-summary-update-mark
10329    (cond ((memq article gnus-newsgroup-undownloaded)
10330           gnus-undownloaded-mark)
10331          (gnus-newsgroup-agentized
10332           gnus-downloaded-mark)
10333          (t
10334           gnus-no-mark))
10335    'download)
10336   (gnus-summary-update-line t)
10337   t)
10338
10339 (defun gnus-summary-update-mark (mark type)
10340   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10341         (buffer-read-only nil))
10342     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10343     (when forward
10344       (when (looking-at "\r")
10345         (incf forward))
10346       (when (<= (+ forward (point)) (point-max))
10347         ;; Go to the right position on the line.
10348         (goto-char (+ forward (point)))
10349         ;; Replace the old mark with the new mark.
10350         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10351         ;; Optionally update the marks by some user rule.
10352         (when (eq type 'unread)
10353           (gnus-data-set-mark
10354            (gnus-data-find (gnus-summary-article-number)) mark)
10355           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10356
10357 (defun gnus-mark-article-as-read (article &optional mark)
10358   "Enter ARTICLE in the pertinent lists and remove it from others."
10359   ;; Make the article expirable.
10360   (let ((mark (or mark gnus-del-mark)))
10361     (setq gnus-newsgroup-expirable
10362           (if (= mark gnus-expirable-mark)
10363               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10364             (delq article gnus-newsgroup-expirable)))
10365     ;; Remove from unread and marked lists.
10366     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10367     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10368     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10369     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10370     (push (cons article mark) gnus-newsgroup-reads)
10371     ;; Possibly remove from cache, if that is used.
10372     (when gnus-use-cache
10373       (gnus-cache-enter-remove-article article))
10374     t))
10375
10376 (defun gnus-mark-article-as-unread (article &optional mark)
10377   "Enter ARTICLE in the pertinent lists and remove it from others."
10378   (let ((mark (or mark gnus-ticked-mark)))
10379     (if (<= article 0)
10380         (progn
10381           (gnus-error 1 "Can't mark negative article numbers")
10382           nil)
10383       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10384             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10385             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10386             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10387             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10388
10389       ;; Unsuppress duplicates?
10390       (when gnus-suppress-duplicates
10391         (gnus-dup-unsuppress-article article))
10392
10393       (cond ((= mark gnus-ticked-mark)
10394              (setq gnus-newsgroup-marked
10395                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10396             ((= mark gnus-spam-mark)
10397              (setq gnus-newsgroup-spam-marked
10398                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10399                                             article)))
10400             ((= mark gnus-dormant-mark)
10401              (setq gnus-newsgroup-dormant
10402                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10403             (t
10404              (setq gnus-newsgroup-unreads
10405                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10406       (gnus-pull article gnus-newsgroup-reads)
10407       t)))
10408
10409 (defalias 'gnus-summary-mark-as-unread-forward
10410   'gnus-summary-tick-article-forward)
10411 (make-obsolete 'gnus-summary-mark-as-unread-forward
10412                'gnus-summary-tick-article-forward)
10413 (defun gnus-summary-tick-article-forward (n)
10414   "Tick N articles forwards.
10415 If N is negative, tick backwards instead.
10416 The difference between N and the number of articles ticked is returned."
10417   (interactive "p")
10418   (gnus-summary-mark-forward n gnus-ticked-mark))
10419
10420 (defalias 'gnus-summary-mark-as-unread-backward
10421   'gnus-summary-tick-article-backward)
10422 (make-obsolete 'gnus-summary-mark-as-unread-backward
10423                'gnus-summary-tick-article-backward)
10424 (defun gnus-summary-tick-article-backward (n)
10425   "Tick N articles backwards.
10426 The difference between N and the number of articles ticked is returned."
10427   (interactive "p")
10428   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10429
10430 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10431 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10432 (defun gnus-summary-tick-article (&optional article clear-mark)
10433   "Mark current article as unread.
10434 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10435 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10436   (interactive)
10437   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10438                                        gnus-ticked-mark)))
10439
10440 (defun gnus-summary-mark-as-read-forward (n)
10441   "Mark N articles as read forwards.
10442 If N is negative, mark backwards instead.
10443 The difference between N and the actual number of articles marked is
10444 returned."
10445   (interactive "p")
10446   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10447
10448 (defun gnus-summary-mark-as-read-backward (n)
10449   "Mark the N articles as read backwards.
10450 The difference between N and the actual number of articles marked is
10451 returned."
10452   (interactive "p")
10453   (gnus-summary-mark-forward
10454    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10455
10456 (defun gnus-summary-mark-as-read (&optional article mark)
10457   "Mark current article as read.
10458 ARTICLE specifies the article to be marked as read.
10459 MARK specifies a string to be inserted at the beginning of the line."
10460   (gnus-summary-mark-article article mark))
10461
10462 (defun gnus-summary-clear-mark-forward (n)
10463   "Clear marks from N articles forward.
10464 If N is negative, clear backward instead.
10465 The difference between N and the number of marks cleared is returned."
10466   (interactive "p")
10467   (gnus-summary-mark-forward n gnus-unread-mark))
10468
10469 (defun gnus-summary-clear-mark-backward (n)
10470   "Clear marks from N articles backward.
10471 The difference between N and the number of marks cleared is returned."
10472   (interactive "p")
10473   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10474
10475 (defun gnus-summary-mark-unread-as-read ()
10476   "Intended to be used by `gnus-summary-mark-article-hook'."
10477   (when (memq gnus-current-article gnus-newsgroup-unreads)
10478     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10479
10480 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10481   "Intended to be used by `gnus-summary-mark-article-hook'."
10482   (let ((mark (gnus-summary-article-mark)))
10483     (when (or (gnus-unread-mark-p mark)
10484               (gnus-read-mark-p mark))
10485       (gnus-summary-mark-article gnus-current-article
10486                                  (or new-mark gnus-read-mark)))))
10487
10488 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10489   "Intended to be used by `gnus-summary-mark-article-hook'."
10490   (let ((mark (gnus-summary-article-mark)))
10491     (when (or (gnus-unread-mark-p mark)
10492               (gnus-read-mark-p mark))
10493       (gnus-summary-mark-article (gnus-summary-article-number)
10494                                  (or new-mark gnus-read-mark)))))
10495
10496 (defun gnus-summary-mark-unread-as-ticked ()
10497   "Intended to be used by `gnus-summary-mark-article-hook'."
10498   (when (memq gnus-current-article gnus-newsgroup-unreads)
10499     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10500
10501 (defun gnus-summary-mark-region-as-read (point mark all)
10502   "Mark all unread articles between point and mark as read.
10503 If given a prefix, mark all articles between point and mark as read,
10504 even ticked and dormant ones."
10505   (interactive "r\nP")
10506   (save-excursion
10507     (let (article)
10508       (goto-char point)
10509       (beginning-of-line)
10510       (while (and
10511               (< (point) mark)
10512               (progn
10513                 (when (or all
10514                           (memq (setq article (gnus-summary-article-number))
10515                                 gnus-newsgroup-unreads))
10516                   (gnus-summary-mark-article article gnus-del-mark))
10517                 t)
10518               (gnus-summary-find-next))))))
10519
10520 (defun gnus-summary-mark-below (score mark)
10521   "Mark articles with score less than SCORE with MARK."
10522   (interactive "P\ncMark: ")
10523   (setq score (if score
10524                   (prefix-numeric-value score)
10525                 (or gnus-summary-default-score 0)))
10526   (save-excursion
10527     (set-buffer gnus-summary-buffer)
10528     (goto-char (point-min))
10529     (while
10530         (progn
10531           (and (< (gnus-summary-article-score) score)
10532                (gnus-summary-mark-article nil mark))
10533           (gnus-summary-find-next)))))
10534
10535 (defun gnus-summary-kill-below (&optional score)
10536   "Mark articles with score below SCORE as read."
10537   (interactive "P")
10538   (gnus-summary-mark-below score gnus-killed-mark))
10539
10540 (defun gnus-summary-clear-above (&optional score)
10541   "Clear all marks from articles with score above SCORE."
10542   (interactive "P")
10543   (gnus-summary-mark-above score gnus-unread-mark))
10544
10545 (defun gnus-summary-tick-above (&optional score)
10546   "Tick all articles with score above SCORE."
10547   (interactive "P")
10548   (gnus-summary-mark-above score gnus-ticked-mark))
10549
10550 (defun gnus-summary-mark-above (score mark)
10551   "Mark articles with score over SCORE with MARK."
10552   (interactive "P\ncMark: ")
10553   (setq score (if score
10554                   (prefix-numeric-value score)
10555                 (or gnus-summary-default-score 0)))
10556   (save-excursion
10557     (set-buffer gnus-summary-buffer)
10558     (goto-char (point-min))
10559     (while (and (progn
10560                   (when (> (gnus-summary-article-score) score)
10561                     (gnus-summary-mark-article nil mark))
10562                   t)
10563                 (gnus-summary-find-next)))))
10564
10565 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10566 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10567 (defun gnus-summary-limit-include-expunged (&optional no-error)
10568   "Display all the hidden articles that were expunged for low scores."
10569   (interactive)
10570   (let ((buffer-read-only nil))
10571     (let ((scored gnus-newsgroup-scored)
10572           headers h)
10573       (while scored
10574         (unless (gnus-summary-article-header (caar scored))
10575           (and (setq h (gnus-number-to-header (caar scored)))
10576                (< (cdar scored) gnus-summary-expunge-below)
10577                (push h headers)))
10578         (setq scored (cdr scored)))
10579       (if (not headers)
10580           (when (not no-error)
10581             (error "No expunged articles hidden"))
10582         (goto-char (point-min))
10583         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10584         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10585         (mapcar (lambda (x) (push (mail-header-number x)
10586                                   gnus-newsgroup-limit))
10587                 headers)
10588         (gnus-summary-prepare-unthreaded (nreverse headers))
10589         (goto-char (point-min))
10590         (gnus-summary-position-point)
10591         t))))
10592
10593 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10594   "Mark all unread articles in this newsgroup as read.
10595 If prefix argument ALL is non-nil, ticked and dormant articles will
10596 also be marked as read.
10597 If QUIETLY is non-nil, no questions will be asked.
10598
10599 If TO-HERE is non-nil, it should be a point in the buffer.  All
10600 articles before (after, if REVERSE is set) this point will be marked
10601 as read.
10602
10603 Note that this function will only catch up the unread article
10604 in the current summary buffer limitation.
10605
10606 The number of articles marked as read is returned."
10607   (interactive "P")
10608   (prog1
10609       (save-excursion
10610         (when (or quietly
10611                   (not gnus-interactive-catchup) ;Without confirmation?
10612                   gnus-expert-user
10613                   (gnus-y-or-n-p
10614                    (if all
10615                        "Mark absolutely all articles as read? "
10616                      "Mark all unread articles as read? ")))
10617           (if (and not-mark
10618                    (not gnus-newsgroup-adaptive)
10619                    (not gnus-newsgroup-auto-expire)
10620                    (not gnus-suppress-duplicates)
10621                    (or (not gnus-use-cache)
10622                        (eq gnus-use-cache 'passive)))
10623               (progn
10624                 (when all
10625                   (setq gnus-newsgroup-marked nil
10626                         gnus-newsgroup-spam-marked nil
10627                         gnus-newsgroup-dormant nil))
10628                 (setq gnus-newsgroup-unreads
10629                       (gnus-sorted-nunion
10630                        (gnus-intersection gnus-newsgroup-unreads
10631                                           gnus-newsgroup-downloadable)
10632                        gnus-newsgroup-unfetched)))
10633             ;; We actually mark all articles as canceled, which we
10634             ;; have to do when using auto-expiry or adaptive scoring.
10635             (gnus-summary-show-all-threads)
10636             (if (and to-here reverse)
10637                 (progn
10638                   (goto-char to-here)
10639                   (gnus-summary-mark-current-read-and-unread-as-read
10640                    gnus-catchup-mark)
10641                   (while (gnus-summary-find-next (not all))
10642                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10643               (when (gnus-summary-first-subject (not all))
10644                 (while (and
10645                         (if to-here (< (point) to-here) t)
10646                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10647                         (gnus-summary-find-next (not all))))))
10648             (gnus-set-mode-line 'summary))
10649           t))
10650     (gnus-summary-position-point)))
10651
10652 (defun gnus-summary-catchup-to-here (&optional all)
10653   "Mark all unticked articles before the current one as read.
10654 If ALL is non-nil, also mark ticked and dormant articles as read."
10655   (interactive "P")
10656   (save-excursion
10657     (gnus-save-hidden-threads
10658       (let ((beg (point)))
10659         ;; We check that there are unread articles.
10660         (when (or all (gnus-summary-find-prev))
10661           (gnus-summary-catchup all t beg)))))
10662   (gnus-summary-position-point))
10663
10664 (defun gnus-summary-catchup-from-here (&optional all)
10665   "Mark all unticked articles after (and including) the current one as read.
10666 If ALL is non-nil, also mark ticked and dormant articles as read."
10667   (interactive "P")
10668   (save-excursion
10669     (gnus-save-hidden-threads
10670       (let ((beg (point)))
10671         ;; We check that there are unread articles.
10672         (when (or all (gnus-summary-find-next))
10673           (gnus-summary-catchup all t beg nil t)))))
10674   (gnus-summary-position-point))
10675
10676 (defun gnus-summary-catchup-all (&optional quietly)
10677   "Mark all articles in this newsgroup as read.
10678 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10679 instead, which marks only unread articles as read."
10680   (interactive "P")
10681   (gnus-summary-catchup t quietly))
10682
10683 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10684   "Mark all unread articles in this group as read, then exit.
10685 If prefix argument ALL is non-nil, all articles are marked as read.
10686 If QUIETLY is non-nil, no questions will be asked."
10687   (interactive "P")
10688   (when (gnus-summary-catchup all quietly nil 'fast)
10689     ;; Select next newsgroup or exit.
10690     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10691              (eq gnus-auto-select-next 'quietly))
10692         (gnus-summary-next-group nil)
10693       (gnus-summary-exit))))
10694
10695 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10696   "Mark all articles in this newsgroup as read, and then exit.
10697 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10698 instead, which marks only unread articles as read."
10699   (interactive "P")
10700   (gnus-summary-catchup-and-exit t quietly))
10701
10702 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10703   "Mark all articles in this group as read and select the next group.
10704 If given a prefix, mark all articles, unread as well as ticked, as
10705 read."
10706   (interactive "P")
10707   (save-excursion
10708     (gnus-summary-catchup all))
10709   (gnus-summary-next-group))
10710
10711 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10712   "Mark all articles in this group as read and select the previous group.
10713 If given a prefix, mark all articles, unread as well as ticked, as
10714 read."
10715   (interactive "P")
10716   (save-excursion
10717     (gnus-summary-catchup all))
10718   (gnus-summary-next-group nil nil t))
10719
10720 ;;;
10721 ;;; with article
10722 ;;;
10723
10724 (defmacro gnus-with-article (article &rest forms)
10725   "Select ARTICLE and perform FORMS in the original article buffer.
10726 Then replace the article with the result."
10727   `(progn
10728      ;; We don't want the article to be marked as read.
10729      (let (gnus-mark-article-hook)
10730        (gnus-summary-select-article t t nil ,article))
10731      (set-buffer gnus-original-article-buffer)
10732      ,@forms
10733      (if (not (gnus-check-backend-function
10734                'request-replace-article (car gnus-article-current)))
10735          (gnus-message 5 "Read-only group; not replacing")
10736        (unless (gnus-request-replace-article
10737                 ,article (car gnus-article-current)
10738                 (current-buffer) t)
10739          (error "Couldn't replace article")))
10740      ;; The cache and backlog have to be flushed somewhat.
10741      (when gnus-keep-backlog
10742        (gnus-backlog-remove-article
10743         (car gnus-article-current) (cdr gnus-article-current)))
10744      (when gnus-use-cache
10745        (gnus-cache-update-article
10746         (car gnus-article-current) (cdr gnus-article-current)))))
10747
10748 (put 'gnus-with-article 'lisp-indent-function 1)
10749 (put 'gnus-with-article 'edebug-form-spec '(form body))
10750
10751 ;; Thread-based commands.
10752
10753 (defun gnus-summary-articles-in-thread (&optional article)
10754   "Return a list of all articles in the current thread.
10755 If ARTICLE is non-nil, return all articles in the thread that starts
10756 with that article."
10757   (let* ((article (or article (gnus-summary-article-number)))
10758          (data (gnus-data-find-list article))
10759          (top-level (gnus-data-level (car data)))
10760          (top-subject
10761           (cond ((null gnus-thread-operation-ignore-subject)
10762                  (gnus-simplify-subject-re
10763                   (mail-header-subject (gnus-data-header (car data)))))
10764                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10765                  (gnus-simplify-subject-fuzzy
10766                   (mail-header-subject (gnus-data-header (car data)))))
10767                 (t nil)))
10768          (end-point (save-excursion
10769                       (if (gnus-summary-go-to-next-thread)
10770                           (point) (point-max))))
10771          articles)
10772     (while (and data
10773                 (< (gnus-data-pos (car data)) end-point))
10774       (when (or (not top-subject)
10775                 (string= top-subject
10776                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10777                              (gnus-simplify-subject-fuzzy
10778                               (mail-header-subject
10779                                (gnus-data-header (car data))))
10780                            (gnus-simplify-subject-re
10781                             (mail-header-subject
10782                              (gnus-data-header (car data)))))))
10783         (push (gnus-data-number (car data)) articles))
10784       (unless (and (setq data (cdr data))
10785                    (> (gnus-data-level (car data)) top-level))
10786         (setq data nil)))
10787     ;; Return the list of articles.
10788     (nreverse articles)))
10789
10790 (defun gnus-summary-rethread-current ()
10791   "Rethread the thread the current article is part of."
10792   (interactive)
10793   (let* ((gnus-show-threads t)
10794          (article (gnus-summary-article-number))
10795          (id (mail-header-id (gnus-summary-article-header)))
10796          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10797     (unless id
10798       (error "No article on the current line"))
10799     (gnus-rebuild-thread id)
10800     (gnus-summary-goto-subject article)))
10801
10802 (defun gnus-summary-reparent-thread ()
10803   "Make the current article child of the marked (or previous) article.
10804
10805 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10806 is non-nil or the Subject: of both articles are the same."
10807   (interactive)
10808   (unless (not (gnus-group-read-only-p))
10809     (error "The current newsgroup does not support article editing"))
10810   (unless (<= (length gnus-newsgroup-processable) 1)
10811     (error "No more than one article may be marked"))
10812   (save-window-excursion
10813     (let ((gnus-article-buffer " *reparent*")
10814           (current-article (gnus-summary-article-number))
10815           ;; First grab the marked article, otherwise one line up.
10816           (parent-article (if (not (null gnus-newsgroup-processable))
10817                               (car gnus-newsgroup-processable)
10818                             (save-excursion
10819                               (if (eq (forward-line -1) 0)
10820                                   (gnus-summary-article-number)
10821                                 (error "Beginning of summary buffer"))))))
10822       (unless (not (eq current-article parent-article))
10823         (error "An article may not be self-referential"))
10824       (let ((message-id (mail-header-id
10825                          (gnus-summary-article-header parent-article))))
10826         (unless (and message-id (not (equal message-id "")))
10827           (error "No message-id in desired parent"))
10828         (gnus-with-article current-article
10829           (save-restriction
10830             (goto-char (point-min))
10831             (message-narrow-to-head)
10832             (if (re-search-forward "^References: " nil t)
10833                 (progn
10834                   (re-search-forward "^[^ \t]" nil t)
10835                   (forward-line -1)
10836                   (end-of-line)
10837                   (insert " " message-id))
10838               (insert "References: " message-id "\n"))))
10839         (set-buffer gnus-summary-buffer)
10840         (gnus-summary-unmark-all-processable)
10841         (gnus-summary-update-article current-article)
10842         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10843             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10844         (gnus-summary-rethread-current)
10845         (gnus-message 3 "Article %d is now the child of article %d"
10846                       current-article parent-article)))))
10847
10848 (defun gnus-summary-toggle-threads (&optional arg)
10849   "Toggle showing conversation threads.
10850 If ARG is positive number, turn showing conversation threads on."
10851   (interactive "P")
10852   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10853     (setq gnus-show-threads
10854           (if (null arg) (not gnus-show-threads)
10855             (> (prefix-numeric-value arg) 0)))
10856     (gnus-summary-prepare)
10857     (gnus-summary-goto-subject current)
10858     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10859     (gnus-summary-position-point)))
10860
10861 (defun gnus-summary-show-all-threads ()
10862   "Show all threads."
10863   (interactive)
10864   (save-excursion
10865     (let ((buffer-read-only nil))
10866       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10867   (gnus-summary-position-point))
10868
10869 (defun gnus-summary-show-thread ()
10870   "Show thread subtrees.
10871 Returns nil if no thread was there to be shown."
10872   (interactive)
10873   (let ((buffer-read-only nil)
10874         (orig (point))
10875         (end (point-at-eol))
10876         ;; Leave point at bol
10877         (beg (progn (beginning-of-line) (point))))
10878     (prog1
10879         ;; Any hidden lines here?
10880         (search-forward "\r" end t)
10881       (subst-char-in-region beg end ?\^M ?\n t)
10882       (goto-char orig)
10883       (gnus-summary-position-point))))
10884
10885 (defun gnus-summary-maybe-hide-threads ()
10886   "If requested, hide the threads that should be hidden."
10887   (when (and gnus-show-threads
10888              gnus-thread-hide-subtree)
10889     (gnus-summary-hide-all-threads
10890      (if (or (consp gnus-thread-hide-subtree)
10891              (functionp gnus-thread-hide-subtree))
10892          (gnus-make-predicate gnus-thread-hide-subtree)
10893        nil))))
10894
10895 ;;; Hiding predicates.
10896
10897 (defun gnus-article-unread-p (header)
10898   (memq (mail-header-number header) gnus-newsgroup-unreads))
10899
10900 (defun gnus-article-unseen-p (header)
10901   (memq (mail-header-number header) gnus-newsgroup-unseen))
10902
10903 (defun gnus-map-articles (predicate articles)
10904   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10905   (apply 'gnus-or (mapcar predicate
10906                           (mapcar 'gnus-summary-article-header articles))))
10907
10908 (defun gnus-summary-hide-all-threads (&optional predicate)
10909   "Hide all thread subtrees.
10910 If PREDICATE is supplied, threads that satisfy this predicate
10911 will not be hidden."
10912   (interactive)
10913   (save-excursion
10914     (goto-char (point-min))
10915     (let ((end nil))
10916       (while (not end)
10917         (when (or (not predicate)
10918                   (gnus-map-articles
10919                    predicate (gnus-summary-article-children)))
10920             (gnus-summary-hide-thread))
10921         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10922   (gnus-summary-position-point))
10923
10924 (defun gnus-summary-hide-thread ()
10925   "Hide thread subtrees.
10926 If PREDICATE is supplied, threads that satisfy this predicate
10927 will not be hidden.
10928 Returns nil if no threads were there to be hidden."
10929   (interactive)
10930   (let ((buffer-read-only nil)
10931         (start (point))
10932         (article (gnus-summary-article-number)))
10933     (goto-char start)
10934     ;; Go forward until either the buffer ends or the subthread
10935     ;; ends.
10936     (when (and (not (eobp))
10937                (or (zerop (gnus-summary-next-thread 1 t))
10938                    (goto-char (point-max))))
10939       (prog1
10940           (if (and (> (point) start)
10941                    (search-backward "\n" start t))
10942               (progn
10943                 (subst-char-in-region start (point) ?\n ?\^M)
10944                 (gnus-summary-goto-subject article))
10945             (goto-char start)
10946             nil)))))
10947
10948 (defun gnus-summary-go-to-next-thread (&optional previous)
10949   "Go to the same level (or less) next thread.
10950 If PREVIOUS is non-nil, go to previous thread instead.
10951 Return the article number moved to, or nil if moving was impossible."
10952   (let ((level (gnus-summary-thread-level))
10953         (way (if previous -1 1))
10954         (beg (point)))
10955     (forward-line way)
10956     (while (and (not (eobp))
10957                 (< level (gnus-summary-thread-level)))
10958       (forward-line way))
10959     (if (eobp)
10960         (progn
10961           (goto-char beg)
10962           nil)
10963       (setq beg (point))
10964       (prog1
10965           (gnus-summary-article-number)
10966         (goto-char beg)))))
10967
10968 (defun gnus-summary-next-thread (n &optional silent)
10969   "Go to the same level next N'th thread.
10970 If N is negative, search backward instead.
10971 Returns the difference between N and the number of skips actually
10972 done.
10973
10974 If SILENT, don't output messages."
10975   (interactive "p")
10976   (let ((backward (< n 0))
10977         (n (abs n)))
10978     (while (and (> n 0)
10979                 (gnus-summary-go-to-next-thread backward))
10980       (decf n))
10981     (unless silent
10982       (gnus-summary-position-point))
10983     (when (and (not silent) (/= 0 n))
10984       (gnus-message 7 "No more threads"))
10985     n))
10986
10987 (defun gnus-summary-prev-thread (n)
10988   "Go to the same level previous N'th thread.
10989 Returns the difference between N and the number of skips actually
10990 done."
10991   (interactive "p")
10992   (gnus-summary-next-thread (- n)))
10993
10994 (defun gnus-summary-go-down-thread ()
10995   "Go down one level in the current thread."
10996   (let ((children (gnus-summary-article-children)))
10997     (when children
10998       (gnus-summary-goto-subject (car children)))))
10999
11000 (defun gnus-summary-go-up-thread ()
11001   "Go up one level in the current thread."
11002   (let ((parent (gnus-summary-article-parent)))
11003     (when parent
11004       (gnus-summary-goto-subject parent))))
11005
11006 (defun gnus-summary-down-thread (n)
11007   "Go down thread N steps.
11008 If N is negative, go up instead.
11009 Returns the difference between N and how many steps down that were
11010 taken."
11011   (interactive "p")
11012   (let ((up (< n 0))
11013         (n (abs n)))
11014     (while (and (> n 0)
11015                 (if up (gnus-summary-go-up-thread)
11016                   (gnus-summary-go-down-thread)))
11017       (setq n (1- n)))
11018     (gnus-summary-position-point)
11019     (when (/= 0 n)
11020       (gnus-message 7 "Can't go further"))
11021     n))
11022
11023 (defun gnus-summary-up-thread (n)
11024   "Go up thread N steps.
11025 If N is negative, go down instead.
11026 Returns the difference between N and how many steps down that were
11027 taken."
11028   (interactive "p")
11029   (gnus-summary-down-thread (- n)))
11030
11031 (defun gnus-summary-top-thread ()
11032   "Go to the top of the thread."
11033   (interactive)
11034   (while (gnus-summary-go-up-thread))
11035   (gnus-summary-article-number))
11036
11037 (defun gnus-summary-kill-thread (&optional unmark)
11038   "Mark articles under current thread as read.
11039 If the prefix argument is positive, remove any kinds of marks.
11040 If the prefix argument is negative, tick articles instead."
11041   (interactive "P")
11042   (when unmark
11043     (setq unmark (prefix-numeric-value unmark)))
11044   (let ((articles (gnus-summary-articles-in-thread)))
11045     (save-excursion
11046       ;; Expand the thread.
11047       (gnus-summary-show-thread)
11048       ;; Mark all the articles.
11049       (while articles
11050         (gnus-summary-goto-subject (car articles))
11051         (cond ((null unmark)
11052                (gnus-summary-mark-article-as-read gnus-killed-mark))
11053               ((> unmark 0)
11054                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11055               (t
11056                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11057         (setq articles (cdr articles))))
11058     ;; Hide killed subtrees.
11059     (and (null unmark)
11060          gnus-thread-hide-killed
11061          (gnus-summary-hide-thread))
11062     ;; If marked as read, go to next unread subject.
11063     (when (null unmark)
11064       ;; Go to next unread subject.
11065       (gnus-summary-next-subject 1 t)))
11066   (gnus-set-mode-line 'summary))
11067
11068 ;; Summary sorting commands
11069
11070 (defun gnus-summary-sort-by-number (&optional reverse)
11071   "Sort the summary buffer by article number.
11072 Argument REVERSE means reverse order."
11073   (interactive "P")
11074   (gnus-summary-sort 'number reverse))
11075
11076 (defun gnus-summary-sort-by-random (&optional reverse)
11077   "Randomize the order in the summary buffer.
11078 Argument REVERSE means to randomize in reverse order."
11079   (interactive "P")
11080   (gnus-summary-sort 'random reverse))
11081
11082 (defun gnus-summary-sort-by-author (&optional reverse)
11083   "Sort the summary buffer by author name alphabetically.
11084 If `case-fold-search' is non-nil, case of letters is ignored.
11085 Argument REVERSE means reverse order."
11086   (interactive "P")
11087   (gnus-summary-sort 'author reverse))
11088
11089 (defun gnus-summary-sort-by-recipient (&optional reverse)
11090   "Sort the summary buffer by recipient name alphabetically.
11091 If `case-fold-search' is non-nil, case of letters is ignored.
11092 Argument REVERSE means reverse order."
11093   (interactive "P")
11094   (gnus-summary-sort 'recipient reverse))
11095
11096 (defun gnus-summary-sort-by-subject (&optional reverse)
11097   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11098 If `case-fold-search' is non-nil, case of letters is ignored.
11099 Argument REVERSE means reverse order."
11100   (interactive "P")
11101   (gnus-summary-sort 'subject reverse))
11102
11103 (defun gnus-summary-sort-by-date (&optional reverse)
11104   "Sort the summary buffer by date.
11105 Argument REVERSE means reverse order."
11106   (interactive "P")
11107   (gnus-summary-sort 'date reverse))
11108
11109 (defun gnus-summary-sort-by-score (&optional reverse)
11110   "Sort the summary buffer by score.
11111 Argument REVERSE means reverse order."
11112   (interactive "P")
11113   (gnus-summary-sort 'score reverse))
11114
11115 (defun gnus-summary-sort-by-lines (&optional reverse)
11116   "Sort the summary buffer by the number of lines.
11117 Argument REVERSE means reverse order."
11118   (interactive "P")
11119   (gnus-summary-sort 'lines reverse))
11120
11121 (defun gnus-summary-sort-by-chars (&optional reverse)
11122   "Sort the summary buffer by article length.
11123 Argument REVERSE means reverse order."
11124   (interactive "P")
11125   (gnus-summary-sort 'chars reverse))
11126
11127 (defun gnus-summary-sort-by-original (&optional reverse)
11128   "Sort the summary buffer using the default sorting method.
11129 Argument REVERSE means reverse order."
11130   (interactive "P")
11131   (let* ((buffer-read-only)
11132          (gnus-summary-prepare-hook nil))
11133     ;; We do the sorting by regenerating the threads.
11134     (gnus-summary-prepare)
11135     ;; Hide subthreads if needed.
11136     (gnus-summary-maybe-hide-threads)))
11137
11138 (defun gnus-summary-sort (predicate reverse)
11139   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11140   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11141          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11142          (gnus-thread-sort-functions
11143           (if (not reverse)
11144               thread
11145             `(lambda (t1 t2)
11146                (,thread t2 t1))))
11147          (gnus-sort-gathered-threads-function
11148           gnus-thread-sort-functions)
11149          (gnus-article-sort-functions
11150           (if (not reverse)
11151               article
11152             `(lambda (t1 t2)
11153                (,article t2 t1))))
11154          (buffer-read-only)
11155          (gnus-summary-prepare-hook nil))
11156     ;; We do the sorting by regenerating the threads.
11157     (gnus-summary-prepare)
11158     ;; Hide subthreads if needed.
11159     (gnus-summary-maybe-hide-threads)))
11160
11161 ;; Summary saving commands.
11162
11163 (defun gnus-summary-save-article (&optional n not-saved)
11164   "Save the current article using the default saver function.
11165 If N is a positive number, save the N next articles.
11166 If N is a negative number, save the N previous articles.
11167 If N is nil and any articles have been marked with the process mark,
11168 save those articles instead.
11169 The variable `gnus-default-article-saver' specifies the saver function."
11170   (interactive "P")
11171   (let* ((articles (gnus-summary-work-articles n))
11172          (save-buffer (save-excursion
11173                         (nnheader-set-temp-buffer " *Gnus Save*")))
11174          (num (length articles))
11175          header file)
11176     (dolist (article articles)
11177       (setq header (gnus-summary-article-header article))
11178       (if (not (vectorp header))
11179           ;; This is a pseudo-article.
11180           (if (assq 'name header)
11181               (gnus-copy-file (cdr (assq 'name header)))
11182             (gnus-message 1 "Article %d is unsaveable" article))
11183         ;; This is a real article.
11184         (save-window-excursion
11185           (let ((gnus-display-mime-function nil)
11186                 (gnus-article-prepare-hook nil))
11187             (gnus-summary-select-article t nil nil article)))
11188         (save-excursion
11189           (set-buffer save-buffer)
11190           (erase-buffer)
11191           (insert-buffer-substring gnus-original-article-buffer))
11192         (setq file (gnus-article-save save-buffer file num))
11193         (gnus-summary-remove-process-mark article)
11194         (unless not-saved
11195           (gnus-summary-set-saved-mark article))))
11196     (gnus-kill-buffer save-buffer)
11197     (gnus-summary-position-point)
11198     (gnus-set-mode-line 'summary)
11199     n))
11200
11201 (defun gnus-summary-pipe-output (&optional arg headers)
11202   "Pipe the current article to a subprocess.
11203 If N is a positive number, pipe the N next articles.
11204 If N is a negative number, pipe the N previous articles.
11205 If N is nil and any articles have been marked with the process mark,
11206 pipe those articles instead.
11207 If HEADERS (the symbolic prefix), include the headers, too."
11208   (interactive (gnus-interactive "P\ny"))
11209   (require 'gnus-art)
11210   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11211         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11212     (gnus-summary-save-article arg t))
11213   (let ((buffer (get-buffer "*Shell Command Output*")))
11214     (when (and buffer
11215                (not (zerop (buffer-size buffer))))
11216       (gnus-configure-windows 'pipe))))
11217
11218 (defun gnus-summary-save-article-mail (&optional arg)
11219   "Append the current article to an mail file.
11220 If N is a positive number, save the N next articles.
11221 If N is a negative number, save the N previous articles.
11222 If N is nil and any articles have been marked with the process mark,
11223 save those articles instead."
11224   (interactive "P")
11225   (require 'gnus-art)
11226   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11227     (gnus-summary-save-article arg)))
11228
11229 (defun gnus-summary-save-article-rmail (&optional arg)
11230   "Append the current article to an rmail file.
11231 If N is a positive number, save the N next articles.
11232 If N is a negative number, save the N previous articles.
11233 If N is nil and any articles have been marked with the process mark,
11234 save those articles instead."
11235   (interactive "P")
11236   (require 'gnus-art)
11237   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11238     (gnus-summary-save-article arg)))
11239
11240 (defun gnus-summary-save-article-file (&optional arg)
11241   "Append the current article to a file.
11242 If N is a positive number, save the N next articles.
11243 If N is a negative number, save the N previous articles.
11244 If N is nil and any articles have been marked with the process mark,
11245 save those articles instead."
11246   (interactive "P")
11247   (require 'gnus-art)
11248   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11249     (gnus-summary-save-article arg)))
11250
11251 (defun gnus-summary-write-article-file (&optional arg)
11252   "Write the current article to a file, deleting the previous file.
11253 If N is a positive number, save the N next articles.
11254 If N is a negative number, save the N previous articles.
11255 If N is nil and any articles have been marked with the process mark,
11256 save those articles instead."
11257   (interactive "P")
11258   (require 'gnus-art)
11259   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11260     (gnus-summary-save-article arg)))
11261
11262 (defun gnus-summary-save-article-body-file (&optional arg)
11263   "Append the current article body to a file.
11264 If N is a positive number, save the N next articles.
11265 If N is a negative number, save the N previous articles.
11266 If N is nil and any articles have been marked with the process mark,
11267 save those articles instead."
11268   (interactive "P")
11269   (require 'gnus-art)
11270   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11271     (gnus-summary-save-article arg)))
11272
11273 (defun gnus-summary-muttprint (&optional arg)
11274   "Print the current article using Muttprint.
11275 If N is a positive number, save the N next articles.
11276 If N is a negative number, save the N previous articles.
11277 If N is nil and any articles have been marked with the process mark,
11278 save those articles instead."
11279   (interactive "P")
11280   (require 'gnus-art)
11281   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11282     (gnus-summary-save-article arg t)))
11283
11284 (defun gnus-summary-pipe-message (program)
11285   "Pipe the current article through PROGRAM."
11286   (interactive "sProgram: ")
11287   (gnus-summary-select-article)
11288   (let ((mail-header-separator ""))
11289     (gnus-eval-in-buffer-window gnus-article-buffer
11290       (save-restriction
11291         (widen)
11292         (let ((start (window-start))
11293               buffer-read-only)
11294           (message-pipe-buffer-body program)
11295           (set-window-start (get-buffer-window (current-buffer)) start))))))
11296
11297 (defun gnus-get-split-value (methods)
11298   "Return a value based on the split METHODS."
11299   (let (split-name method result match)
11300     (when methods
11301       (save-excursion
11302         (set-buffer gnus-original-article-buffer)
11303         (save-restriction
11304           (nnheader-narrow-to-headers)
11305           (while (and methods (not split-name))
11306             (goto-char (point-min))
11307             (setq method (pop methods))
11308             (setq match (car method))
11309             (when (cond
11310                    ((stringp match)
11311                     ;; Regular expression.
11312                     (ignore-errors
11313                       (re-search-forward match nil t)))
11314                    ((functionp match)
11315                     ;; Function.
11316                     (save-restriction
11317                       (widen)
11318                       (setq result (funcall match gnus-newsgroup-name))))
11319                    ((consp match)
11320                     ;; Form.
11321                     (save-restriction
11322                       (widen)
11323                       (setq result (eval match)))))
11324               (setq split-name (cdr method))
11325               (cond ((stringp result)
11326                      (push (expand-file-name
11327                             result gnus-article-save-directory)
11328                            split-name))
11329                     ((consp result)
11330                      (setq split-name (append result split-name)))))))))
11331     (nreverse split-name)))
11332
11333 (defun gnus-valid-move-group-p (group)
11334   (and (boundp group)
11335        (symbol-name group)
11336        (symbol-value group)
11337        (gnus-get-function (gnus-find-method-for-group
11338                            (symbol-name group)) 'request-accept-article t)))
11339
11340 (defun gnus-read-move-group-name (prompt default articles prefix)
11341   "Read a group name."
11342   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11343          (minibuffer-confirm-incomplete nil) ; XEmacs
11344          (prom
11345           (format "%s %s to"
11346                   prompt
11347                   (if (> (length articles) 1)
11348                       (format "these %d articles" (length articles))
11349                     "this article")))
11350          (to-newsgroup
11351           (cond
11352            ((null split-name)
11353             (gnus-completing-read-with-default
11354              default prom
11355              gnus-active-hashtb
11356              'gnus-valid-move-group-p
11357              nil prefix
11358              'gnus-group-history))
11359            ((= 1 (length split-name))
11360             (gnus-completing-read-with-default
11361              (car split-name) prom
11362              gnus-active-hashtb
11363              'gnus-valid-move-group-p
11364              nil nil
11365              'gnus-group-history))
11366            (t
11367             (gnus-completing-read-with-default
11368              nil prom
11369              (mapcar 'list (nreverse split-name))
11370              nil nil nil
11371              'gnus-group-history))))
11372          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11373     (when to-newsgroup
11374       (if (or (string= to-newsgroup "")
11375               (string= to-newsgroup prefix))
11376           (setq to-newsgroup default))
11377       (unless to-newsgroup
11378         (error "No group name entered"))
11379       (or (gnus-active to-newsgroup)
11380           (gnus-activate-group to-newsgroup nil nil to-method)
11381           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11382                                      to-newsgroup))
11383               (or (and (gnus-request-create-group to-newsgroup to-method)
11384                        (gnus-activate-group
11385                         to-newsgroup nil nil to-method)
11386                        (gnus-subscribe-group to-newsgroup))
11387                   (error "Couldn't create group %s" to-newsgroup)))
11388           (error "No such group: %s" to-newsgroup)))
11389     to-newsgroup))
11390
11391 (defun gnus-summary-save-parts (type dir n &optional reverse)
11392   "Save parts matching TYPE to DIR.
11393 If REVERSE, save parts that do not match TYPE."
11394   (interactive
11395    (list (read-string "Save parts of type: "
11396                       (or (car gnus-summary-save-parts-type-history)
11397                           gnus-summary-save-parts-default-mime)
11398                       'gnus-summary-save-parts-type-history)
11399          (setq gnus-summary-save-parts-last-directory
11400                (read-file-name "Save to directory: "
11401                                gnus-summary-save-parts-last-directory
11402                                nil t))
11403          current-prefix-arg))
11404   (gnus-summary-iterate n
11405     (let ((gnus-display-mime-function nil)
11406           (gnus-inhibit-treatment t))
11407       (gnus-summary-select-article))
11408     (save-excursion
11409       (set-buffer gnus-article-buffer)
11410       (let ((handles (or gnus-article-mime-handles
11411                          (mm-dissect-buffer nil gnus-article-loose-mime)
11412                          (and gnus-article-emulate-mime
11413                               (mm-uu-dissect)))))
11414         (when handles
11415           (gnus-summary-save-parts-1 type dir handles reverse)
11416           (unless gnus-article-mime-handles ;; Don't destroy this case.
11417             (mm-destroy-parts handles)))))))
11418
11419 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11420   (if (stringp (car handle))
11421       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11422               (cdr handle))
11423     (when (if reverse
11424               (not (string-match type (mm-handle-media-type handle)))
11425             (string-match type (mm-handle-media-type handle)))
11426       (let ((file (expand-file-name
11427                    (gnus-map-function
11428                     mm-file-name-rewrite-functions
11429                     (file-name-nondirectory
11430                      (or
11431                       (mail-content-type-get
11432                        (mm-handle-disposition handle) 'filename)
11433                       (mail-content-type-get
11434                        (mm-handle-type handle) 'name)
11435                       (concat gnus-newsgroup-name
11436                               "." (number-to-string
11437                                    (cdr gnus-article-current))))))
11438                    dir)))
11439         (unless (file-exists-p file)
11440           (mm-save-part-to-file handle file))))))
11441
11442 ;; Summary extract commands
11443
11444 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11445   (let ((buffer-read-only nil)
11446         (article (gnus-summary-article-number))
11447         after-article b e)
11448     (unless (gnus-summary-goto-subject article)
11449       (error "No such article: %d" article))
11450     (gnus-summary-position-point)
11451     ;; If all commands are to be bunched up on one line, we collect
11452     ;; them here.
11453     (unless gnus-view-pseudos-separately
11454       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11455             files action)
11456         (while ps
11457           (setq action (cdr (assq 'action (car ps))))
11458           (setq files (list (cdr (assq 'name (car ps)))))
11459           (while (and ps (cdr ps)
11460                       (string= (or action "1")
11461                                (or (cdr (assq 'action (cadr ps))) "2")))
11462             (push (cdr (assq 'name (cadr ps))) files)
11463             (setcdr ps (cddr ps)))
11464           (when files
11465             (when (not (string-match "%s" action))
11466               (push " " files))
11467             (push " " files)
11468             (when (assq 'execute (car ps))
11469               (setcdr (assq 'execute (car ps))
11470                       (funcall (if (string-match "%s" action)
11471                                    'format 'concat)
11472                                action
11473                                (mapconcat
11474                                 (lambda (f)
11475                                   (if (equal f " ")
11476                                       f
11477                                     (shell-quote-argument f)))
11478                                 files " ")))))
11479           (setq ps (cdr ps)))))
11480     (if (and gnus-view-pseudos (not not-view))
11481         (while pslist
11482           (when (assq 'execute (car pslist))
11483             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11484                                   (eq gnus-view-pseudos 'not-confirm)))
11485           (setq pslist (cdr pslist)))
11486       (save-excursion
11487         (while pslist
11488           (setq after-article (or (cdr (assq 'article (car pslist)))
11489                                   (gnus-summary-article-number)))
11490           (gnus-summary-goto-subject after-article)
11491           (forward-line 1)
11492           (setq b (point))
11493           (insert "    " (file-name-nondirectory
11494                           (cdr (assq 'name (car pslist))))
11495                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11496           (setq e (point))
11497           (forward-line -1)             ; back to `b'
11498           (gnus-add-text-properties
11499            b (1- e) (list 'gnus-number gnus-reffed-article-number
11500                           gnus-mouse-face-prop gnus-mouse-face))
11501           (gnus-data-enter
11502            after-article gnus-reffed-article-number
11503            gnus-unread-mark b (car pslist) 0 (- e b))
11504           (setq gnus-newsgroup-unreads
11505                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11506                                          gnus-reffed-article-number))
11507           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11508           (setq pslist (cdr pslist)))))))
11509
11510 (defun gnus-pseudos< (p1 p2)
11511   (let ((c1 (cdr (assq 'action p1)))
11512         (c2 (cdr (assq 'action p2))))
11513     (and c1 c2 (string< c1 c2))))
11514
11515 (defun gnus-request-pseudo-article (props)
11516   (cond ((assq 'execute props)
11517          (gnus-execute-command (cdr (assq 'execute props)))))
11518   (let ((gnus-current-article (gnus-summary-article-number)))
11519     (gnus-run-hooks 'gnus-mark-article-hook)))
11520
11521 (defun gnus-execute-command (command &optional automatic)
11522   (save-excursion
11523     (gnus-article-setup-buffer)
11524     (set-buffer gnus-article-buffer)
11525     (setq buffer-read-only nil)
11526     (let ((command (if automatic command
11527                      (read-string "Command: " (cons command 0)))))
11528       (erase-buffer)
11529       (insert "$ " command "\n\n")
11530       (if gnus-view-pseudo-asynchronously
11531           (start-process "gnus-execute" (current-buffer) shell-file-name
11532                          shell-command-switch command)
11533         (call-process shell-file-name nil t nil
11534                       shell-command-switch command)))))
11535
11536 ;; Summary kill commands.
11537
11538 (defun gnus-summary-edit-global-kill (article)
11539   "Edit the \"global\" kill file."
11540   (interactive (list (gnus-summary-article-number)))
11541   (gnus-group-edit-global-kill article))
11542
11543 (defun gnus-summary-edit-local-kill ()
11544   "Edit a local kill file applied to the current newsgroup."
11545   (interactive)
11546   (setq gnus-current-headers (gnus-summary-article-header))
11547   (gnus-group-edit-local-kill
11548    (gnus-summary-article-number) gnus-newsgroup-name))
11549
11550 ;;; Header reading.
11551
11552 (defun gnus-read-header (id &optional header)
11553   "Read the headers of article ID and enter them into the Gnus system."
11554   (let ((group gnus-newsgroup-name)
11555         (gnus-override-method
11556          (or
11557           gnus-override-method
11558           (and (gnus-news-group-p gnus-newsgroup-name)
11559                (car (gnus-refer-article-methods)))))
11560         where)
11561     ;; First we check to see whether the header in question is already
11562     ;; fetched.
11563     (if (stringp id)
11564         ;; This is a Message-ID.
11565         (setq header (or header (gnus-id-to-header id)))
11566       ;; This is an article number.
11567       (setq header (or header (gnus-summary-article-header id))))
11568     (if (and header
11569              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11570         ;; We have found the header.
11571         header
11572       ;; We have to really fetch the header to this article.
11573       (save-excursion
11574         (set-buffer nntp-server-buffer)
11575         (when (setq where (gnus-request-head id group))
11576           (nnheader-fold-continuation-lines)
11577           (goto-char (point-max))
11578           (insert ".\n")
11579           (goto-char (point-min))
11580           (insert "211 ")
11581           (princ (cond
11582                   ((numberp id) id)
11583                   ((cdr where) (cdr where))
11584                   (header (mail-header-number header))
11585                   (t gnus-reffed-article-number))
11586                  (current-buffer))
11587           (insert " Article retrieved.\n"))
11588         (if (or (not where)
11589                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11590             ()                          ; Malformed head.
11591           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11592             (when (and (stringp id)
11593                        (not (string= (gnus-group-real-name group)
11594                                      (car where))))
11595               ;; If we fetched by Message-ID and the article came
11596               ;; from a different group, we fudge some bogus article
11597               ;; numbers for this article.
11598               (mail-header-set-number header gnus-reffed-article-number))
11599             (save-excursion
11600               (set-buffer gnus-summary-buffer)
11601               (decf gnus-reffed-article-number)
11602               (gnus-remove-header (mail-header-number header))
11603               (push header gnus-newsgroup-headers)
11604               (setq gnus-current-headers header)
11605               (push (mail-header-number header) gnus-newsgroup-limit)))
11606           header)))))
11607
11608 (defun gnus-remove-header (number)
11609   "Remove header NUMBER from `gnus-newsgroup-headers'."
11610   (if (and gnus-newsgroup-headers
11611            (= number (mail-header-number (car gnus-newsgroup-headers))))
11612       (pop gnus-newsgroup-headers)
11613     (let ((headers gnus-newsgroup-headers))
11614       (while (and (cdr headers)
11615                   (not (= number (mail-header-number (cadr headers)))))
11616         (pop headers))
11617       (when (cdr headers)
11618         (setcdr headers (cddr headers))))))
11619
11620 ;;;
11621 ;;; summary highlights
11622 ;;;
11623
11624 (defun gnus-highlight-selected-summary ()
11625   "Highlight selected article in summary buffer."
11626   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11627   (when gnus-summary-selected-face
11628     (save-excursion
11629       (let* ((beg (point-at-bol))
11630              (end (point-at-eol))
11631              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11632              (from (if (get-text-property beg gnus-mouse-face-prop)
11633                        beg
11634                      (or (next-single-property-change
11635                           beg gnus-mouse-face-prop nil end)
11636                          beg)))
11637              (to
11638               (if (= from end)
11639                   (- from 2)
11640                 (or (next-single-property-change
11641                      from gnus-mouse-face-prop nil end)
11642                     end))))
11643         ;; If no mouse-face prop on line we will have to = from = end,
11644         ;; so we highlight the entire line instead.
11645         (when (= (+ to 2) from)
11646           (setq from beg)
11647           (setq to end))
11648         (if gnus-newsgroup-selected-overlay
11649             ;; Move old overlay.
11650             (gnus-move-overlay
11651              gnus-newsgroup-selected-overlay from to (current-buffer))
11652           ;; Create new overlay.
11653           (gnus-overlay-put
11654            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11655            'face gnus-summary-selected-face))))))
11656
11657 (defvar gnus-summary-highlight-line-cached nil)
11658 (defvar gnus-summary-highlight-line-trigger nil)
11659
11660 (defun gnus-summary-highlight-line-0 ()
11661   (if (and (eq gnus-summary-highlight-line-trigger
11662                gnus-summary-highlight)
11663            gnus-summary-highlight-line-cached)
11664       gnus-summary-highlight-line-cached
11665     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11666           gnus-summary-highlight-line-cached
11667           (let* ((cond (list 'cond))
11668                  (c cond)
11669                  (list gnus-summary-highlight))
11670             (while list
11671               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11672                               nil))
11673               (setq c (cdr c)
11674                     list (cdr list)))
11675             (gnus-byte-compile (list 'lambda nil cond))))))
11676
11677 (defun gnus-summary-highlight-line ()
11678   "Highlight current line according to `gnus-summary-highlight'."
11679   (let* ((beg (point-at-bol))
11680          (article (or (gnus-summary-article-number) gnus-current-article))
11681          (score (or (cdr (assq article
11682                                gnus-newsgroup-scored))
11683                     gnus-summary-default-score 0))
11684          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11685          (inhibit-read-only t)
11686          (default gnus-summary-default-score)
11687          (default-high gnus-summary-default-high-score)
11688          (default-low gnus-summary-default-low-score)
11689          (uncached (and gnus-summary-use-undownloaded-faces
11690                         (memq article gnus-newsgroup-undownloaded)
11691                         (not (memq article gnus-newsgroup-cached)))))
11692     (let ((face (funcall (gnus-summary-highlight-line-0))))
11693       (unless (eq face (get-text-property beg 'face))
11694         (gnus-put-text-property-excluding-characters-with-faces
11695          beg (point-at-eol) 'face
11696          (setq face (if (boundp face) (symbol-value face) face)))
11697         (when gnus-summary-highlight-line-function
11698           (funcall gnus-summary-highlight-line-function article face))))))
11699
11700 (defun gnus-update-read-articles (group unread &optional compute)
11701   "Update the list of read articles in GROUP.
11702 UNREAD is a sorted list."
11703   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11704         (info (gnus-get-info group))
11705         (prev 1)
11706         read)
11707     (if (or (not info) (not active))
11708         ;; There is no info on this group if it was, in fact,
11709         ;; killed.  Gnus stores no information on killed groups, so
11710         ;; there's nothing to be done.
11711         ;; One could store the information somewhere temporarily,
11712         ;; perhaps...  Hmmm...
11713         ()
11714       ;; Remove any negative articles numbers.
11715       (while (and unread (< (car unread) 0))
11716         (setq unread (cdr unread)))
11717       ;; Remove any expired article numbers
11718       (while (and unread (< (car unread) (car active)))
11719         (setq unread (cdr unread)))
11720       ;; Compute the ranges of read articles by looking at the list of
11721       ;; unread articles.
11722       (while unread
11723         (when (/= (car unread) prev)
11724           (push (if (= prev (1- (car unread))) prev
11725                   (cons prev (1- (car unread))))
11726                 read))
11727         (setq prev (1+ (car unread)))
11728         (setq unread (cdr unread)))
11729       (when (<= prev (cdr active))
11730         (push (cons prev (cdr active)) read))
11731       (setq read (if (> (length read) 1) (nreverse read) read))
11732       (if compute
11733           read
11734         (save-excursion
11735           (let (setmarkundo)
11736             ;; Propagate the read marks to the backend.
11737             (when (gnus-check-backend-function 'request-set-mark group)
11738               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11739                     (add (gnus-remove-from-range read (gnus-info-read info))))
11740                 (when (or add del)
11741                   (unless (gnus-check-group group)
11742                     (error "Can't open server for %s" group))
11743                   (gnus-request-set-mark
11744                    group (delq nil (list (if add (list add 'add '(read)))
11745                                          (if del (list del 'del '(read))))))
11746                   (setq setmarkundo
11747                         `(gnus-request-set-mark
11748                           ,group
11749                           ',(delq nil (list
11750                                        (if del (list del 'add '(read)))
11751                                        (if add (list add 'del '(read))))))))))
11752             (set-buffer gnus-group-buffer)
11753             (gnus-undo-register
11754               `(progn
11755                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11756                  (gnus-info-set-read ',info ',(gnus-info-read info))
11757                  (gnus-get-unread-articles-in-group ',info
11758                                                     (gnus-active ,group))
11759                  (gnus-group-update-group ,group t)
11760                  ,setmarkundo))))
11761         ;; Enter this list into the group info.
11762         (gnus-info-set-read info read)
11763         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11764         (gnus-get-unread-articles-in-group info (gnus-active group))
11765         t))))
11766
11767 (defun gnus-offer-save-summaries ()
11768   "Offer to save all active summary buffers."
11769   (let (buffers)
11770     ;; Go through all buffers and find all summaries.
11771     (dolist (buffer (buffer-list))
11772       (when (and (setq buffer (buffer-name buffer))
11773                  (string-match "Summary" buffer)
11774                  (with-current-buffer buffer
11775                    ;; We check that this is, indeed, a summary buffer.
11776                    (and (eq major-mode 'gnus-summary-mode)
11777                         ;; Also make sure this isn't bogus.
11778                         gnus-newsgroup-prepared
11779                         ;; Also make sure that this isn't a
11780                         ;; dead summary buffer.
11781                         (not gnus-dead-summary-mode))))
11782         (push buffer buffers)))
11783     ;; Go through all these summary buffers and offer to save them.
11784     (when buffers
11785       (save-excursion
11786         (map-y-or-n-p
11787          "Update summary buffer %s? "
11788          (lambda (buf)
11789            (switch-to-buffer buf)
11790            (gnus-summary-exit))
11791          buffers)))))
11792
11793 (defun gnus-summary-setup-default-charset ()
11794   "Setup newsgroup default charset."
11795   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11796       (setq gnus-newsgroup-charset nil)
11797     (let* ((ignored-charsets
11798             (or gnus-newsgroup-ephemeral-ignored-charsets
11799                 (append
11800                  (and gnus-newsgroup-name
11801                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11802                  gnus-newsgroup-ignored-charsets))))
11803       (setq gnus-newsgroup-charset
11804             (or gnus-newsgroup-ephemeral-charset
11805                 (and gnus-newsgroup-name
11806                      (gnus-parameter-charset gnus-newsgroup-name))
11807                 gnus-default-charset))
11808       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11809            ignored-charsets))))
11810
11811 ;;;
11812 ;;; Mime Commands
11813 ;;;
11814
11815 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11816   "Display the current article buffer fully MIME-buttonized.
11817 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11818 treated as multipart/mixed."
11819   (interactive "P")
11820   (require 'gnus-art)
11821   (let ((gnus-unbuttonized-mime-types nil)
11822         (gnus-mime-display-multipart-as-mixed show-all-parts))
11823     (gnus-summary-show-article)))
11824
11825 (defun gnus-summary-repair-multipart (article)
11826   "Add a Content-Type header to a multipart article without one."
11827   (interactive (list (gnus-summary-article-number)))
11828   (gnus-with-article article
11829     (message-narrow-to-head)
11830     (message-remove-header "Mime-Version")
11831     (goto-char (point-max))
11832     (insert "Mime-Version: 1.0\n")
11833     (widen)
11834     (when (search-forward "\n--" nil t)
11835       (let ((separator (buffer-substring (point) (point-at-eol))))
11836         (message-narrow-to-head)
11837         (message-remove-header "Content-Type")
11838         (goto-char (point-max))
11839         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11840                         separator))
11841         (widen))))
11842   (let (gnus-mark-article-hook)
11843     (gnus-summary-select-article t t nil article)))
11844
11845 (defun gnus-summary-toggle-display-buttonized ()
11846   "Toggle the buttonizing of the article buffer."
11847   (interactive)
11848   (require 'gnus-art)
11849   (if (setq gnus-inhibit-mime-unbuttonizing
11850             (not gnus-inhibit-mime-unbuttonizing))
11851       (let ((gnus-unbuttonized-mime-types nil))
11852         (gnus-summary-show-article))
11853     (gnus-summary-show-article)))
11854
11855 ;;;
11856 ;;; Generic summary marking commands
11857 ;;;
11858
11859 (defvar gnus-summary-marking-alist
11860   '((read gnus-del-mark "d")
11861     (unread gnus-unread-mark "u")
11862     (ticked gnus-ticked-mark "!")
11863     (dormant gnus-dormant-mark "?")
11864     (expirable gnus-expirable-mark "e"))
11865   "An alist of names/marks/keystrokes.")
11866
11867 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11868 (defvar gnus-summary-mark-map)
11869
11870 (defun gnus-summary-make-all-marking-commands ()
11871   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11872   (dolist (elem gnus-summary-marking-alist)
11873     (apply 'gnus-summary-make-marking-command elem)))
11874
11875 (defun gnus-summary-make-marking-command (name mark keystroke)
11876   (let ((map (make-sparse-keymap)))
11877     (define-key gnus-summary-generic-mark-map keystroke map)
11878     (dolist (lway `((next "next" next nil "n")
11879                     (next-unread "next unread" next t "N")
11880                     (prev "previous" prev nil "p")
11881                     (prev-unread "previous unread" prev t "P")
11882                     (nomove "" nil nil ,keystroke)))
11883       (let ((func (gnus-summary-make-marking-command-1
11884                    mark (car lway) lway name)))
11885         (setq func (eval func))
11886         (define-key map (nth 4 lway) func)))))
11887
11888 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11889   `(defun ,(intern
11890             (format "gnus-summary-put-mark-as-%s%s"
11891                     name (if (eq way 'nomove)
11892                              ""
11893                            (concat "-" (symbol-name way)))))
11894      (n)
11895      ,(format
11896        "Mark the current article as %s%s.
11897 If N, the prefix, then repeat N times.
11898 If N is negative, move in reverse order.
11899 The difference between N and the actual number of articles marked is
11900 returned."
11901        name (cadr lway))
11902      (interactive "p")
11903      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11904
11905 (defun gnus-summary-generic-mark (n mark move unread)
11906   "Mark N articles with MARK."
11907   (unless (eq major-mode 'gnus-summary-mode)
11908     (error "This command can only be used in the summary buffer"))
11909   (gnus-summary-show-thread)
11910   (let ((nummove
11911          (cond
11912           ((eq move 'next) 1)
11913           ((eq move 'prev) -1)
11914           (t 0))))
11915     (if (zerop nummove)
11916         (setq n 1)
11917       (when (< n 0)
11918         (setq n (abs n)
11919               nummove (* -1 nummove))))
11920     (while (and (> n 0)
11921                 (gnus-summary-mark-article nil mark)
11922                 (zerop (gnus-summary-next-subject nummove unread t)))
11923       (setq n (1- n)))
11924     (when (/= 0 n)
11925       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11926     (gnus-summary-recenter)
11927     (gnus-summary-position-point)
11928     (gnus-set-mode-line 'summary)
11929     n))
11930
11931 (defun gnus-summary-insert-articles (articles)
11932   (when (setq articles
11933               (gnus-sorted-difference articles
11934                                       (mapcar (lambda (h)
11935                                                 (mail-header-number h))
11936                                               gnus-newsgroup-headers)))
11937     (setq gnus-newsgroup-headers
11938           (gnus-merge 'list
11939                       gnus-newsgroup-headers
11940                       (gnus-fetch-headers articles)
11941                       'gnus-article-sort-by-number))
11942     ;; Suppress duplicates?
11943     (when gnus-suppress-duplicates
11944       (gnus-dup-suppress-articles))
11945
11946     ;; We might want to build some more threads first.
11947     (when (and gnus-fetch-old-headers
11948                (eq gnus-headers-retrieved-by 'nov))
11949       (if (eq gnus-fetch-old-headers 'invisible)
11950           (gnus-build-all-threads)
11951         (gnus-build-old-threads)))
11952     ;; Let the Gnus agent mark articles as read.
11953     (when gnus-agent
11954       (gnus-agent-get-undownloaded-list))
11955     ;; Remove list identifiers from subject
11956     (when gnus-list-identifiers
11957       (gnus-summary-remove-list-identifiers))
11958     ;; First and last article in this newsgroup.
11959     (when gnus-newsgroup-headers
11960       (setq gnus-newsgroup-begin
11961             (mail-header-number (car gnus-newsgroup-headers))
11962             gnus-newsgroup-end
11963             (mail-header-number
11964              (gnus-last-element gnus-newsgroup-headers))))
11965     (when gnus-use-scoring
11966       (gnus-possibly-score-headers))))
11967
11968 (defun gnus-summary-insert-old-articles (&optional all)
11969   "Insert all old articles in this group.
11970 If ALL is non-nil, already read articles become readable.
11971 If ALL is a number, fetch this number of articles."
11972   (interactive "P")
11973   (prog1
11974       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11975             older len)
11976         (setq older
11977               ;; Some nntp servers lie about their active range.  When
11978               ;; this happens, the active range can be in the millions.
11979               ;; Use a compressed range to avoid creating a huge list.
11980               (gnus-range-difference (list gnus-newsgroup-active) old))
11981         (setq len (gnus-range-length older))
11982         (cond
11983          ((null older) nil)
11984          ((numberp all)
11985           (if (< all len)
11986               (let ((older-range (nreverse older)))
11987                 (setq older nil)
11988
11989                 (while (> all 0)
11990                   (let* ((r (pop older-range))
11991                          (min (if (numberp r) r (car r)))
11992                          (max (if (numberp r) r (cdr r))))
11993                     (while (and (<= min max)
11994                                 (> all 0))
11995                       (push max older)
11996                       (setq all (1- all)
11997                             max (1- max))))))
11998             (setq older (gnus-uncompress-range older))))
11999          (all
12000           (setq older (gnus-uncompress-range older)))
12001          (t
12002           (when (and (numberp gnus-large-newsgroup)
12003                    (> len gnus-large-newsgroup))
12004               (let* ((cursor-in-echo-area nil)
12005                      (initial (gnus-parameter-large-newsgroup-initial
12006                                gnus-newsgroup-name))
12007                      (input
12008                       (read-string
12009                        (format
12010                         "How many articles from %s (%s %d): "
12011                         (gnus-limit-string
12012                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12013                         (if initial "max" "default")
12014                         len)
12015                        (if initial
12016                            (cons (number-to-string initial)
12017                                  0)))))
12018                 (unless (string-match "^[ \t]*$" input)
12019                   (setq all (string-to-number input))
12020                   (if (< all len)
12021                       (let ((older-range (nreverse older)))
12022                         (setq older nil)
12023
12024                         (while (> all 0)
12025                           (let* ((r (pop older-range))
12026                                  (min (if (numberp r) r (car r)))
12027                                  (max (if (numberp r) r (cdr r))))
12028                             (while (and (<= min max)
12029                                         (> all 0))
12030                               (push max older)
12031                               (setq all (1- all)
12032                                     max (1- max))))))))))
12033           (setq older (gnus-uncompress-range older))))
12034         (if (not older)
12035             (message "No old news.")
12036           (gnus-summary-insert-articles older)
12037           (gnus-summary-limit (gnus-sorted-nunion old older))))
12038     (gnus-summary-position-point)))
12039
12040 (defun gnus-summary-insert-new-articles ()
12041   "Insert all new articles in this group."
12042   (interactive)
12043   (prog1
12044       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12045             (old-active gnus-newsgroup-active)
12046             (nnmail-fetched-sources (list t))
12047             i new)
12048         (setq gnus-newsgroup-active
12049               (gnus-activate-group gnus-newsgroup-name 'scan))
12050         (setq i (cdr gnus-newsgroup-active))
12051         (while (> i (cdr old-active))
12052           (push i new)
12053           (decf i))
12054         (if (not new)
12055             (message "No gnus is bad news")
12056           (gnus-summary-insert-articles new)
12057           (setq gnus-newsgroup-unreads
12058                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12059           (gnus-summary-limit (gnus-sorted-nunion old new))))
12060     (gnus-summary-position-point)))
12061
12062 (gnus-summary-make-all-marking-commands)
12063
12064 (gnus-ems-redefine)
12065
12066 (provide 'gnus-sum)
12067
12068 (run-hooks 'gnus-sum-load-hook)
12069
12070 ;; Local Variables:
12071 ;; coding: iso-8859-1
12072 ;; End:
12073
12074 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12075 ;;; gnus-sum.el ends here