* mm-util.el (mm-enrich-utf-8-by-mule-ucs): Don't edit
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-mode))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-fetch-old-headers nil
66   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
67 If an unread article in the group refers to an older, already
68 read (or just marked as read) article, the old article will not
69 normally be displayed in the Summary buffer.  If this variable is
70 t, Gnus will attempt to grab the headers to the old articles, and
71 thereby build complete threads.  If it has the value `some', all
72 old headers will be fetched but only enough headers to connect
73 otherwise loose threads will be displayed.  This variable can
74 also be a number.  In that case, no more than that number of old
75 headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work.
79
80 This feature can seriously impact performance it ignores all
81 locally cached header entries."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const :tag "on" t)
85                  (const some)
86                  (const invisible)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 500
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "*If non-nil, select the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), `unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :version "22.1"
333   :group 'gnus-group-select
334   :type '(choice (const best)
335                  (const unread)
336                  (const first)
337                  (const unseen)
338                  (const unseen-or-unread)))
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols nil (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :version "22.1"
377   :group 'gnus-summary-maneuvering
378   :type '(choice (const :tag "None" nil)
379                  (const :tag "Undownloaded when unplugged" undownloaded)
380                  (const :tag "Undownloaded" always-undownloaded)
381                  (const :tag "Unfetched" unfetched)))
382
383 (defcustom gnus-summary-check-current nil
384   "*If non-nil, consider the current article when moving.
385 The \"unread\" movement commands will stay on the same line if the
386 current article is unread."
387   :group 'gnus-summary-maneuvering
388   :type 'boolean)
389
390 (defcustom gnus-auto-center-summary 2
391   "*If non-nil, always center the current summary buffer.
392 In particular, if `vertical' do only vertical recentering.  If non-nil
393 and non-`vertical', do both horizontal and vertical recentering."
394   :group 'gnus-summary-maneuvering
395   :type '(choice (const :tag "none" nil)
396                  (const vertical)
397                  (integer :tag "height")
398                  (sexp :menu-tag "both" t)))
399
400 (defvar gnus-auto-center-group t
401   "*If non-nil, always center the group buffer.")
402
403 (defcustom gnus-show-all-headers nil
404   "*If non-nil, don't hide any headers."
405   :group 'gnus-article-hiding
406   :group 'gnus-article-headers
407   :type 'boolean)
408
409 (defcustom gnus-summary-ignore-duplicates nil
410   "*If non-nil, ignore articles with identical Message-ID headers."
411   :group 'gnus-summary
412   :type 'boolean)
413
414 (defcustom gnus-single-article-buffer t
415   "*If non-nil, display all articles in the same buffer.
416 If nil, each group will get its own article buffer."
417   :group 'gnus-article-various
418   :type 'boolean)
419
420 (defcustom gnus-break-pages t
421   "*If non-nil, do page breaking on articles.
422 The page delimiter is specified by the `gnus-page-delimiter'
423 variable."
424   :group 'gnus-article-various
425   :type 'boolean)
426
427 (defcustom gnus-move-split-methods nil
428   "*Variable used to suggest where articles are to be moved to.
429 It uses the same syntax as the `gnus-split-methods' variable.
430 However, whereas `gnus-split-methods' specifies file names as targets,
431 this variable specifies group names."
432   :group 'gnus-summary-mail
433   :type '(repeat (choice (list :value (fun) function)
434                          (cons :value ("" "") regexp (repeat string))
435                          (sexp :value nil))))
436
437 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
438   "Function used to compute default prefix for article move/copy/etc prompts.
439 The function should take one argument, a group name, and return a
440 string with the suggested prefix."
441   :group 'gnus-summary-mail
442   :type 'function)
443
444 ;; FIXME: Although the custom type is `character' for the following variables,
445 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
446
447 (defcustom gnus-unread-mark ?           ;Whitespace
448   "*Mark used for unread articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ticked-mark ?!
453   "*Mark used for ticked articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-dormant-mark ??
458   "*Mark used for dormant articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-del-mark ?r
463   "*Mark used for del'd articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-read-mark ?R
468   "*Mark used for read articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-expirable-mark ?E
473   "*Mark used for expirable articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-killed-mark ?K
478   "*Mark used for killed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-spam-mark ?$
483   "*Mark used for spam articles."
484   :version "22.1"
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :version "22.1"
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :version "22.1"
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-no-mark ?               ;Whitespace
542   "*Mark used for articles that have no other secondary mark."
543   :version "22.1"
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-ancient-mark ?O
548   "*Mark used for ancient articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-sparse-mark ?Q
553   "*Mark used for sparsely reffed articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-canceled-mark ?G
558   "*Mark used for canceled articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-duplicate-mark ?M
563   "*Mark used for duplicate articles."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-undownloaded-mark ?-
568   "*Mark used for articles that weren't downloaded."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloaded-mark ?+
574   "*Mark used for articles that were downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-downloadable-mark ?%
579   "*Mark used for articles that are to be downloaded."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unsendable-mark ?=
584   "*Mark used for articles that won't be sent."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-over-mark ?+
589   "*Score mark used for articles with high scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-score-below-mark ?-
594   "*Score mark used for articles with low scores."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-empty-thread-mark ?     ;Whitespace
599   "*There is no thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-not-empty-thread-mark ?=
604   "*There is a thread under the article."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-view-pseudo-asynchronously nil
609   "*If non-nil, Gnus will view pseudo-articles asynchronously."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-auto-expirable-marks
614   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
615         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
616         gnus-souped-mark gnus-duplicate-mark)
617   "*The list of marks converted into expiration if a group is auto-expirable."
618   :version "21.1"
619   :group 'gnus-summary
620   :type '(repeat character))
621
622 (defcustom gnus-inhibit-user-auto-expire t
623   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
624   :version "21.1"
625   :group 'gnus-summary
626   :type 'boolean)
627
628 (defcustom gnus-view-pseudos nil
629   "*If `automatic', pseudo-articles will be viewed automatically.
630 If `not-confirm', pseudos will be viewed automatically, and the user
631 will not be asked to confirm the command."
632   :group 'gnus-extract-view
633   :type '(choice (const :tag "off" nil)
634                  (const automatic)
635                  (const not-confirm)))
636
637 (defcustom gnus-view-pseudos-separately t
638   "*If non-nil, one pseudo-article will be created for each file to be viewed.
639 If nil, all files that use the same viewing command will be given as a
640 list of parameters to that command."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-insert-pseudo-articles t
645   "*If non-nil, insert pseudo-articles when decoding articles."
646   :group 'gnus-extract-view
647   :type 'boolean)
648
649 (defcustom gnus-summary-dummy-line-format
650   "   %(:                             :%) %S\n"
651   "*The format specification for the dummy roots in the summary buffer.
652 It works along the same lines as a normal formatting string,
653 with some simple extensions.
654
655 %S  The subject
656
657 General format specifiers can also be used.
658 See `(gnus)Formatting Variables'."
659   :link '(custom-manual "(gnus)Formatting Variables")
660   :group 'gnus-threading
661   :type 'string)
662
663 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
664   "*The format specification for the summary mode line.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions:
667
668 %G  Group name
669 %p  Unprefixed group name
670 %A  Current article number
671 %z  Current article score
672 %V  Gnus version
673 %U  Number of unread articles in the group
674 %e  Number of unselected articles in the group
675 %Z  A string with unread/unselected article counts
676 %g  Shortish group name
677 %S  Subject of the current article
678 %u  User-defined spec
679 %s  Current score file name
680 %d  Number of dormant articles
681 %r  Number of articles that have been marked as read in this session
682 %E  Number of articles expunged by the score files"
683   :group 'gnus-summary-format
684   :type 'string)
685
686 (defcustom gnus-list-identifiers nil
687   "Regexp that matches list identifiers to be removed from subject.
688 This can also be a list of regexps."
689   :version "21.1"
690   :group 'gnus-summary-format
691   :group 'gnus-article-hiding
692   :type '(choice (const :tag "none" nil)
693                  (regexp :value ".*")
694                  (repeat :value (".*") regexp)))
695
696 (defcustom gnus-summary-mark-below 0
697   "*Mark all articles with a score below this variable as read.
698 This variable is local to each summary buffer and usually set by the
699 score file."
700   :group 'gnus-score-default
701   :type 'integer)
702
703 (defun gnus-widget-reversible-match (widget value)
704   "Ignoring WIDGET, convert VALUE to internal form.
705 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
706   ;; (debug value)
707   (or (symbolp value)
708       (and (listp value)
709            (eq (length value) 2)
710            (eq (nth 0 value) 'not)
711            (symbolp (nth 1 value)))))
712
713 (defun gnus-widget-reversible-to-internal (widget value)
714   "Ignoring WIDGET, convert VALUE to internal form.
715 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
716 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
717   ;; (debug value)
718   (if (atom value)
719       (list value nil)
720     (list (nth 1 value) t)))
721
722 (defun gnus-widget-reversible-to-external (widget value)
723   "Ignoring WIDGET, convert VALUE to external form.
724 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
725 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
726   ;; (debug value)
727   (if (nth 1 value)
728       (list 'not (nth 0 value))
729     (nth 0 value)))
730
731 (define-widget 'gnus-widget-reversible 'group
732   "A `group' that convert values."
733   :match 'gnus-widget-reversible-match
734   :value-to-internal 'gnus-widget-reversible-to-internal
735   :value-to-external 'gnus-widget-reversible-to-external)
736
737 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
738   "*List of functions used for sorting articles in the summary buffer.
739
740 Each function takes two articles and returns non-nil if the first
741 article should be sorted before the other.  If you use more than one
742 function, the primary sort function should be the last.  You should
743 probably always include `gnus-article-sort-by-number' in the list of
744 sorting functions -- preferably first.  Also note that sorting by date
745 is often much slower than sorting by number, and the sorting order is
746 very similar.  (Sorting by date means sorting by the time the message
747 was sent, sorting by number means sorting by arrival time.)
748
749 Each item can also be a list `(not F)' where F is a function;
750 this reverses the sort order.
751
752 Ready-made functions include `gnus-article-sort-by-number',
753 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
754 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
755 and `gnus-article-sort-by-score'.
756
757 When threading is turned on, the variable `gnus-thread-sort-functions'
758 controls how articles are sorted."
759   :group 'gnus-summary-sort
760   :type '(repeat (gnus-widget-reversible
761                   (choice (function-item gnus-article-sort-by-number)
762                           (function-item gnus-article-sort-by-author)
763                           (function-item gnus-article-sort-by-subject)
764                           (function-item gnus-article-sort-by-date)
765                           (function-item gnus-article-sort-by-score)
766                           (function-item gnus-article-sort-by-random)
767                           (function :tag "other"))
768                   (boolean :tag "Reverse order"))))
769
770
771 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
772   "*List of functions used for sorting threads in the summary buffer.
773 By default, threads are sorted by article number.
774
775 Each function takes two threads and returns non-nil if the first
776 thread should be sorted before the other.  If you use more than one
777 function, the primary sort function should be the last.  You should
778 probably always include `gnus-thread-sort-by-number' in the list of
779 sorting functions -- preferably first.  Also note that sorting by date
780 is often much slower than sorting by number, and the sorting order is
781 very similar.  (Sorting by date means sorting by the time the message
782 was sent, sorting by number means sorting by arrival time.)
783
784 Each list item can also be a list `(not F)' where F is a
785 function; this specifies reversed sort order.
786
787 Ready-made functions include `gnus-thread-sort-by-number',
788 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
789 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
790 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
791 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
792 and `gnus-thread-sort-by-total-score' (see
793 `gnus-thread-score-function').
794
795 When threading is turned off, the variable
796 `gnus-article-sort-functions' controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat
799           (gnus-widget-reversible
800            (choice (function-item gnus-thread-sort-by-number)
801                    (function-item gnus-thread-sort-by-author)
802                    (function-item gnus-thread-sort-by-recipient)
803                    (function-item gnus-thread-sort-by-subject)
804                    (function-item gnus-thread-sort-by-date)
805                    (function-item gnus-thread-sort-by-score)
806                    (function-item gnus-thread-sort-by-most-recent-number)
807                    (function-item gnus-thread-sort-by-most-recent-date)
808                    (function-item gnus-thread-sort-by-random)
809                    (function-item gnus-thread-sort-by-total-score)
810                    (function :tag "other"))
811            (boolean :tag "Reverse order"))))
812
813 (defcustom gnus-thread-score-function '+
814   "*Function used for calculating the total score of a thread.
815
816 The function is called with the scores of the article and each
817 subthread and should then return the score of the thread.
818
819 Some functions you can use are `+', `max', or `min'."
820   :group 'gnus-summary-sort
821   :type 'function)
822
823 (defcustom gnus-summary-expunge-below nil
824   "All articles that have a score less than this variable will be expunged.
825 This variable is local to the summary buffers."
826   :group 'gnus-score-default
827   :type '(choice (const :tag "off" nil)
828                  integer))
829
830 (defcustom gnus-thread-expunge-below nil
831   "All threads that have a total score less than this variable will be expunged.
832 See `gnus-thread-score-function' for en explanation of what a
833 \"thread score\" is.
834
835 This variable is local to the summary buffers."
836   :group 'gnus-threading
837   :group 'gnus-score-default
838   :type '(choice (const :tag "off" nil)
839                  integer))
840
841 (defcustom gnus-summary-mode-hook nil
842   "*A hook for Gnus summary mode.
843 This hook is run before any variables are set in the summary buffer."
844   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
845   :group 'gnus-summary-various
846   :type 'hook)
847
848 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
849 (when (featurep 'xemacs)
850   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
851   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
852   (add-hook 'gnus-summary-mode-hook
853             'gnus-xmas-switch-horizontal-scrollbar-off))
854
855 (defcustom gnus-summary-menu-hook nil
856   "*Hook run after the creation of the summary mode menu."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-summary-exit-hook nil
861   "*A hook called on exit from the summary buffer.
862 It will be called with point in the group buffer."
863   :group 'gnus-summary-exit
864   :type 'hook)
865
866 (defcustom gnus-summary-prepare-hook nil
867   "*A hook called after the summary buffer has been generated.
868 If you want to modify the summary buffer, you can use this hook."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-prepared-hook nil
873   "*A hook called as the last thing after the summary buffer has been generated."
874   :group 'gnus-summary-various
875   :type 'hook)
876
877 (defcustom gnus-summary-generate-hook nil
878   "*A hook run just before generating the summary buffer.
879 This hook is commonly used to customize threading variables and the
880 like."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-select-group-hook nil
885   "*A hook called when a newsgroup is selected.
886
887 If you'd like to simplify subjects like the
888 `gnus-summary-next-same-subject' command does, you can use the
889 following hook:
890
891  (add-hook gnus-select-group-hook
892            (lambda ()
893              (mapcar (lambda (header)
894                        (mail-header-set-subject
895                         header
896                         (gnus-simplify-subject
897                          (mail-header-subject header) 're-only)))
898                      gnus-newsgroup-headers)))"
899   :group 'gnus-group-select
900   :type 'hook)
901
902 (defcustom gnus-select-article-hook nil
903   "*A hook called when an article is selected."
904   :group 'gnus-summary-choose
905   :options '(gnus-agent-fetch-selected-article)
906   :type 'hook)
907
908 (defcustom gnus-visual-mark-article-hook
909   (list 'gnus-highlight-selected-summary)
910   "*Hook run after selecting an article in the summary buffer.
911 It is meant to be used for highlighting the article in some way.  It
912 is not run if `gnus-visual' is nil."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-parse-headers-hook nil
917   "*A hook called before parsing the headers."
918   :group 'gnus-various
919   :type 'hook)
920
921 (defcustom gnus-exit-group-hook nil
922   "*A hook called when exiting summary mode.
923 This hook is not called from the non-updating exit commands like `Q'."
924   :group 'gnus-various
925   :type 'hook)
926
927 (defcustom gnus-summary-update-hook
928   (list 'gnus-summary-highlight-line)
929   "*A hook called when a summary line is changed.
930 The hook will not be called if `gnus-visual' is nil.
931
932 The default function `gnus-summary-highlight-line' will
933 highlight the line according to the `gnus-summary-highlight'
934 variable."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
939   "*A hook called when an article is selected for the first time.
940 The hook is intended to mark an article as read (or unread)
941 automatically when it is selected."
942   :group 'gnus-summary-choose
943   :type 'hook)
944
945 (defcustom gnus-group-no-more-groups-hook nil
946   "*A hook run when returning to group mode having no more (unread) groups."
947   :group 'gnus-group-select
948   :type 'hook)
949
950 (defcustom gnus-ps-print-hook nil
951   "*A hook run before ps-printing something from Gnus."
952   :group 'gnus-summary
953   :type 'hook)
954
955 (defcustom gnus-summary-article-move-hook nil
956   "*A hook called after an article is moved, copied, respooled, or crossposted."
957   :version "22.1"
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-article-delete-hook nil
962   "*A hook called after an article is deleted."
963   :version "22.1"
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :version "22.1"
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded)
995     (uncached
996      . gnus-summary-normal-undownloaded)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread)
1020     ((> score default-high)
1021      . gnus-summary-high-read)
1022     ((< score default-low)
1023      . gnus-summary-low-read)
1024     (t
1025      . gnus-summary-normal-read))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1055   "Variable that says which function should be used to decode a string with encoded words.")
1056
1057 (defcustom gnus-extra-headers '(To Newsgroups)
1058   "*Extra headers to parse."
1059   :version "21.1"
1060   :group 'gnus-summary
1061   :type '(repeat symbol))
1062
1063 (defcustom gnus-ignored-from-addresses
1064   (and user-mail-address  
1065        (not (string= user-mail-address ""))
1066        (regexp-quote user-mail-address))
1067   "*Regexp of From headers that may be suppressed in favor of To headers."
1068   :version "21.1"
1069   :group 'gnus-summary
1070   :type 'regexp)
1071
1072 (defcustom gnus-summary-to-prefix "-> "
1073   "*String prefixed to the To field in the summary line when
1074 using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-summary-newsgroup-prefix "=> "
1080   "*String prefixed to the Newsgroup field in the summary
1081 line when using `gnus-ignored-from-addresses'."
1082   :version "22.1"
1083   :group 'gnus-summary
1084   :type 'string)
1085
1086 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1087   "List of charsets that should be ignored.
1088 When these charsets are used in the \"charset\" parameter, the
1089 default charset will be used instead."
1090   :version "21.1"
1091   :type '(repeat symbol)
1092   :group 'gnus-charset)
1093
1094 (gnus-define-group-parameter
1095  ignored-charsets
1096  :type list
1097  :function-document
1098  "Return the ignored charsets of GROUP."
1099  :variable gnus-group-ignored-charsets-alist
1100  :variable-default
1101  '(("alt\\.chinese\\.text" iso-8859-1))
1102  :variable-document
1103  "Alist of regexps (to match group names) and charsets that should be ignored.
1104 When these charsets are used in the \"charset\" parameter, the
1105 default charset will be used instead."
1106  :variable-group gnus-charset
1107  :variable-type '(repeat (cons (regexp :tag "Group")
1108                                (repeat symbol)))
1109  :parameter-type '(choice :tag "Ignored charsets"
1110                           :value nil
1111                           (repeat (symbol)))
1112  :parameter-document       "\
1113 List of charsets that should be ignored.
1114
1115 When these charsets are used in the \"charset\" parameter, the
1116 default charset will be used instead.")
1117
1118 (defcustom gnus-group-highlight-words-alist nil
1119   "Alist of group regexps and highlight regexps.
1120 This variable uses the same syntax as `gnus-emphasis-alist'."
1121   :version "21.1"
1122   :type '(repeat (cons (regexp :tag "Group")
1123                        (repeat (list (regexp :tag "Highlight regexp")
1124                                      (number :tag "Group for entire word" 0)
1125                                      (number :tag "Group for displayed part" 0)
1126                                      (symbol :tag "Face"
1127                                              gnus-emphasis-highlight-words)))))
1128   :group 'gnus-summary-visual)
1129
1130 (defcustom gnus-summary-show-article-charset-alist
1131   nil
1132   "Alist of number and charset.
1133 The article will be shown with the charset corresponding to the
1134 numbered argument.
1135 For example: ((1 . cn-gb-2312) (2 . big5))."
1136   :version "21.1"
1137   :type '(repeat (cons (number :tag "Argument" 1)
1138                        (symbol :tag "Charset")))
1139   :group 'gnus-charset)
1140
1141 (defcustom gnus-preserve-marks t
1142   "Whether marks are preserved when moving, copying and respooling messages."
1143   :version "21.1"
1144   :type 'boolean
1145   :group 'gnus-summary-marks)
1146
1147 (defcustom gnus-alter-articles-to-read-function nil
1148   "Function to be called to alter the list of articles to be selected."
1149   :type '(choice (const nil) function)
1150   :group 'gnus-summary)
1151
1152 (defcustom gnus-orphan-score nil
1153   "*All orphans get this score added.  Set in the score file."
1154   :group 'gnus-score-default
1155   :type '(choice (const nil)
1156                  integer))
1157
1158 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1159   "*A regexp to match MIME parts when saving multiple parts of a
1160 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1161 This regexp will be used by default when prompting the user for which
1162 type of files to save."
1163   :group 'gnus-summary
1164   :type 'regexp)
1165
1166 (defcustom gnus-read-all-available-headers nil
1167   "Whether Gnus should parse all headers made available to it.
1168 This is mostly relevant for slow back ends where the user may
1169 wish to widen the summary buffer to include all headers
1170 that were fetched.  Say, for nnultimate groups."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type '(choice boolean regexp))
1174
1175 (defcustom gnus-summary-muttprint-program "muttprint"
1176   "Command (and optional arguments) used to run Muttprint."
1177   :version "22.1"
1178   :group 'gnus-summary
1179   :type 'string)
1180
1181 (defcustom gnus-article-loose-mime t
1182   "If non-nil, don't require MIME-Version header.
1183 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1184 supply the MIME-Version header or deliberately strip it from the mail.
1185 If non-nil (the default), Gnus will treat some articles as MIME
1186 even if the MIME-Version header is missing."
1187   :version "22.1"
1188   :type 'boolean
1189   :group 'gnus-article-mime)
1190
1191 (defcustom gnus-article-emulate-mime t
1192   "If non-nil, use MIME emulation for uuencode and the like.
1193 This means that Gnus will search message bodies for text that look
1194 like uuencoded bits, yEncoded bits, and so on, and present that using
1195 the normal Gnus MIME machinery."
1196   :version "22.1"
1197   :type 'boolean
1198   :group 'gnus-article-mime)
1199
1200 ;;; Internal variables
1201
1202 (defvar gnus-summary-display-cache nil)
1203 (defvar gnus-article-mime-handles nil)
1204 (defvar gnus-article-decoded-p nil)
1205 (defvar gnus-article-charset nil)
1206 (defvar gnus-article-ignored-charsets nil)
1207 (defvar gnus-scores-exclude-files nil)
1208 (defvar gnus-page-broken nil)
1209
1210 (defvar gnus-original-article nil)
1211 (defvar gnus-article-internal-prepare-hook nil)
1212 (defvar gnus-newsgroup-process-stack nil)
1213
1214 (defvar gnus-thread-indent-array nil)
1215 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1216 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1217   "Function called to sort the articles within a thread after it has been gathered together.")
1218
1219 (defvar gnus-summary-save-parts-type-history nil)
1220 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1221
1222 ;; Avoid highlighting in kill files.
1223 (defvar gnus-summary-inhibit-highlight nil)
1224 (defvar gnus-newsgroup-selected-overlay nil)
1225 (defvar gnus-inhibit-limiting nil)
1226 (defvar gnus-newsgroup-adaptive-score-file nil)
1227 (defvar gnus-current-score-file nil)
1228 (defvar gnus-current-move-group nil)
1229 (defvar gnus-current-copy-group nil)
1230 (defvar gnus-current-crosspost-group nil)
1231 (defvar gnus-newsgroup-display nil)
1232
1233 (defvar gnus-newsgroup-dependencies nil)
1234 (defvar gnus-newsgroup-adaptive nil)
1235 (defvar gnus-summary-display-article-function nil)
1236 (defvar gnus-summary-highlight-line-function nil
1237   "Function called after highlighting a summary line.")
1238
1239 (defvar gnus-summary-line-format-alist
1240   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1241     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1242     (?s gnus-tmp-subject-or-nil ?s)
1243     (?n gnus-tmp-name ?s)
1244     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1245         ?s)
1246     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1247             gnus-tmp-from) ?s)
1248     (?F gnus-tmp-from ?s)
1249     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1250     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1251     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1252     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1253     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1254     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1255     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1256     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1257     (?L gnus-tmp-lines ?s)
1258     (?O gnus-tmp-downloaded ?c)
1259     (?I gnus-tmp-indentation ?s)
1260     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1261     (?R gnus-tmp-replied ?c)
1262     (?\[ gnus-tmp-opening-bracket ?c)
1263     (?\] gnus-tmp-closing-bracket ?c)
1264     (?\> (make-string gnus-tmp-level ? ) ?s)
1265     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1266     (?i gnus-tmp-score ?d)
1267     (?z gnus-tmp-score-char ?c)
1268     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1269     (?U gnus-tmp-unread ?c)
1270     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1271         ?s)
1272     (?t (gnus-summary-number-of-articles-in-thread
1273          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1274         ?d)
1275     (?e (gnus-summary-number-of-articles-in-thread
1276          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1277         ?c)
1278     (?u gnus-tmp-user-defined ?s)
1279     (?P (gnus-pick-line-number) ?d)
1280     (?B gnus-tmp-thread-tree-header-string ?s)
1281     (user-date (gnus-user-date
1282                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1283   "An alist of format specifications that can appear in summary lines.
1284 These are paired with what variables they correspond with, along with
1285 the type of the variable (string, integer, character, etc).")
1286
1287 (defvar gnus-summary-dummy-line-format-alist
1288   `((?S gnus-tmp-subject ?s)
1289     (?N gnus-tmp-number ?d)
1290     (?u gnus-tmp-user-defined ?s)))
1291
1292 (defvar gnus-summary-mode-line-format-alist
1293   `((?G gnus-tmp-group-name ?s)
1294     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1295     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1296     (?A gnus-tmp-article-number ?d)
1297     (?Z gnus-tmp-unread-and-unselected ?s)
1298     (?V gnus-version ?s)
1299     (?U gnus-tmp-unread-and-unticked ?d)
1300     (?S gnus-tmp-subject ?s)
1301     (?e gnus-tmp-unselected ?d)
1302     (?u gnus-tmp-user-defined ?s)
1303     (?d (length gnus-newsgroup-dormant) ?d)
1304     (?t (length gnus-newsgroup-marked) ?d)
1305     (?h (length gnus-newsgroup-spam-marked) ?d)
1306     (?r (length gnus-newsgroup-reads) ?d)
1307     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1308     (?E gnus-newsgroup-expunged-tally ?d)
1309     (?s (gnus-current-score-file-nondirectory) ?s)))
1310
1311 (defvar gnus-last-search-regexp nil
1312   "Default regexp for article search command.")
1313
1314 (defvar gnus-last-shell-command nil
1315   "Default shell command on article.")
1316
1317 (defvar gnus-newsgroup-agentized nil
1318   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1319 (defvar gnus-newsgroup-begin nil)
1320 (defvar gnus-newsgroup-end nil)
1321 (defvar gnus-newsgroup-last-rmail nil)
1322 (defvar gnus-newsgroup-last-mail nil)
1323 (defvar gnus-newsgroup-last-folder nil)
1324 (defvar gnus-newsgroup-last-file nil)
1325 (defvar gnus-newsgroup-last-directory nil)
1326 (defvar gnus-newsgroup-auto-expire nil)
1327 (defvar gnus-newsgroup-active nil)
1328
1329 (defvar gnus-newsgroup-data nil)
1330 (defvar gnus-newsgroup-data-reverse nil)
1331 (defvar gnus-newsgroup-limit nil)
1332 (defvar gnus-newsgroup-limits nil)
1333 (defvar gnus-summary-use-undownloaded-faces nil)
1334
1335 (defvar gnus-newsgroup-unreads nil
1336   "Sorted list of unread articles in the current newsgroup.")
1337
1338 (defvar gnus-newsgroup-unselected nil
1339   "Sorted list of unselected unread articles in the current newsgroup.")
1340
1341 (defvar gnus-newsgroup-reads nil
1342   "Alist of read articles and article marks in the current newsgroup.")
1343
1344 (defvar gnus-newsgroup-expunged-tally nil)
1345
1346 (defvar gnus-newsgroup-marked nil
1347   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1348
1349 (defvar gnus-newsgroup-spam-marked nil
1350   "List of ranges of articles that have been marked as spam.")
1351
1352 (defvar gnus-newsgroup-killed nil
1353   "List of ranges of articles that have been through the scoring process.")
1354
1355 (defvar gnus-newsgroup-cached nil
1356   "Sorted list of articles that come from the article cache.")
1357
1358 (defvar gnus-newsgroup-saved nil
1359   "List of articles that have been saved.")
1360
1361 (defvar gnus-newsgroup-kill-headers nil)
1362
1363 (defvar gnus-newsgroup-replied nil
1364   "List of articles that have been replied to in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-forwarded nil
1367   "List of articles that have been forwarded in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-recent nil
1370   "List of articles that have are recent in the current newsgroup.")
1371
1372 (defvar gnus-newsgroup-expirable nil
1373   "Sorted list of articles in the current newsgroup that can be expired.")
1374
1375 (defvar gnus-newsgroup-processable nil
1376   "List of articles in the current newsgroup that can be processed.")
1377
1378 (defvar gnus-newsgroup-downloadable nil
1379   "Sorted list of articles in the current newsgroup that can be processed.")
1380
1381 (defvar gnus-newsgroup-unfetched nil
1382   "Sorted list of articles in the current newsgroup whose headers have
1383 not been fetched into the agent.
1384
1385 This list will always be a subset of gnus-newsgroup-undownloaded.")
1386
1387 (defvar gnus-newsgroup-undownloaded nil
1388   "List of articles in the current newsgroup that haven't been downloaded.")
1389
1390 (defvar gnus-newsgroup-unsendable nil
1391   "List of articles in the current newsgroup that won't be sent.")
1392
1393 (defvar gnus-newsgroup-bookmarks nil
1394   "List of articles in the current newsgroup that have bookmarks.")
1395
1396 (defvar gnus-newsgroup-dormant nil
1397   "Sorted list of dormant articles in the current newsgroup.")
1398
1399 (defvar gnus-newsgroup-unseen nil
1400   "List of unseen articles in the current newsgroup.")
1401
1402 (defvar gnus-newsgroup-seen nil
1403   "Range of seen articles in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-articles nil
1406   "List of articles in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-scored nil
1409   "List of scored articles in the current newsgroup.")
1410
1411 (defvar gnus-newsgroup-headers nil
1412   "List of article headers in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-threads nil)
1415
1416 (defvar gnus-newsgroup-prepared nil
1417   "Whether the current group has been prepared properly.")
1418
1419 (defvar gnus-newsgroup-ancient nil
1420   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1421
1422 (defvar gnus-newsgroup-sparse nil)
1423
1424 (defvar gnus-current-article nil)
1425 (defvar gnus-article-current nil)
1426 (defvar gnus-current-headers nil)
1427 (defvar gnus-have-all-headers nil)
1428 (defvar gnus-last-article nil)
1429 (defvar gnus-newsgroup-history nil)
1430 (defvar gnus-newsgroup-charset nil)
1431 (defvar gnus-newsgroup-ephemeral-charset nil)
1432 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1433
1434 (defvar gnus-article-before-search nil)
1435
1436 (defvar gnus-summary-local-variables
1437   '(gnus-newsgroup-name
1438     gnus-newsgroup-begin gnus-newsgroup-end
1439     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1440     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1441     gnus-newsgroup-last-directory
1442     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1443     gnus-newsgroup-unselected gnus-newsgroup-marked
1444     gnus-newsgroup-spam-marked
1445     gnus-newsgroup-reads gnus-newsgroup-saved
1446     gnus-newsgroup-replied gnus-newsgroup-forwarded
1447     gnus-newsgroup-recent
1448     gnus-newsgroup-expirable
1449     gnus-newsgroup-processable gnus-newsgroup-killed
1450     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1451     gnus-newsgroup-unfetched
1452     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1453     gnus-newsgroup-seen gnus-newsgroup-articles
1454     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1455     gnus-newsgroup-headers gnus-newsgroup-threads
1456     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1457     gnus-current-article gnus-current-headers gnus-have-all-headers
1458     gnus-last-article gnus-article-internal-prepare-hook
1459     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1460     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1461     gnus-thread-expunge-below
1462     gnus-score-alist gnus-current-score-file
1463     (gnus-summary-expunge-below . global)
1464     (gnus-summary-mark-below . global)
1465     (gnus-orphan-score . global)
1466     gnus-newsgroup-active gnus-scores-exclude-files
1467     gnus-newsgroup-history gnus-newsgroup-ancient
1468     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1469     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1470     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1471     (gnus-newsgroup-expunged-tally . 0)
1472     gnus-cache-removable-articles gnus-newsgroup-cached
1473     gnus-newsgroup-data gnus-newsgroup-data-reverse
1474     gnus-newsgroup-limit gnus-newsgroup-limits
1475     gnus-newsgroup-charset gnus-newsgroup-display
1476     gnus-summary-use-undownloaded-faces)
1477   "Variables that are buffer-local to the summary buffers.")
1478
1479 (defvar gnus-newsgroup-variables nil
1480   "A list of variables that have separate values in different newsgroups.
1481 A list of newsgroup (summary buffer) local variables, or cons of
1482 variables and their default expressions to be evalled (when the default
1483 values are not nil), that should be made global while the summary buffer
1484 is active.
1485
1486 Note: The default expressions will be evaluated (using function `eval')
1487 before assignment to the local variable rather than just assigned to it.
1488 If the default expression is the symbol `global', that symbol will not
1489 be evaluated but the global value of the local variable will be used
1490 instead.
1491
1492 These variables can be used to set variables in the group parameters
1493 while still allowing them to affect operations done in other buffers.
1494 For example:
1495
1496 \(setq gnus-newsgroup-variables
1497      '(message-use-followup-to
1498        (gnus-visible-headers .
1499          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1500 ")
1501
1502 ;; Byte-compiler warning.
1503 (eval-when-compile
1504   ;; Bind features so that require will believe that gnus-sum has
1505   ;; already been loaded (avoids infinite recursion)
1506   (let ((features (cons 'gnus-sum features)))
1507     ;; Several of the declarations in gnus-sum are needed to load the
1508     ;; following files. Right now, these definitions have been
1509     ;; compiled but not defined (evaluated).  We could either do a
1510     ;; eval-and-compile about all of the declarations or evaluate the
1511     ;; source file.
1512     (if (boundp 'gnus-newsgroup-variables)
1513         nil
1514       (load "gnus-sum.el" t t t))
1515     (require 'gnus)
1516     (require 'gnus-art)))
1517
1518 ;; MIME stuff.
1519
1520 (defvar gnus-decode-encoded-word-methods
1521   '(mail-decode-encoded-word-string)
1522   "List of methods used to decode encoded words.
1523
1524 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1525 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1526 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1527 whose names match REGEXP.
1528
1529 For example:
1530 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1531  mail-decode-encoded-word-string
1532  (\"chinese\" . rfc1843-decode-string))")
1533
1534 (defvar gnus-decode-encoded-word-methods-cache nil)
1535
1536 (defun gnus-multi-decode-encoded-word-string (string)
1537   "Apply the functions from `gnus-encoded-word-methods' that match."
1538   (unless (and gnus-decode-encoded-word-methods-cache
1539                (eq gnus-newsgroup-name
1540                    (car gnus-decode-encoded-word-methods-cache)))
1541     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1542     (mapcar (lambda (x)
1543               (if (symbolp x)
1544                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1545                 (if (and gnus-newsgroup-name
1546                          (string-match (car x) gnus-newsgroup-name))
1547                     (nconc gnus-decode-encoded-word-methods-cache
1548                            (list (cdr x))))))
1549             gnus-decode-encoded-word-methods))
1550   (let ((xlist gnus-decode-encoded-word-methods-cache))
1551     (pop xlist)
1552     (while xlist
1553       (setq string (funcall (pop xlist) string))))
1554   string)
1555
1556 ;; Subject simplification.
1557
1558 (defun gnus-simplify-whitespace (str)
1559   "Remove excessive whitespace from STR."
1560   ;; Multiple spaces.
1561   (while (string-match "[ \t][ \t]+" str)
1562     (setq str (concat (substring str 0 (match-beginning 0))
1563                         " "
1564                         (substring str (match-end 0)))))
1565   ;; Leading spaces.
1566   (when (string-match "^[ \t]+" str)
1567     (setq str (substring str (match-end 0))))
1568   ;; Trailing spaces.
1569   (when (string-match "[ \t]+$" str)
1570     (setq str (substring str 0 (match-beginning 0))))
1571   str)
1572
1573 (defun gnus-simplify-all-whitespace (str)
1574   "Remove all whitespace from STR."
1575   (while (string-match "[ \t\n]+" str)
1576     (setq str (replace-match "" nil nil str)))
1577   str)
1578
1579 (defsubst gnus-simplify-subject-re (subject)
1580   "Remove \"Re:\" from subject lines."
1581   (if (string-match message-subject-re-regexp subject)
1582       (substring subject (match-end 0))
1583     subject))
1584
1585 (defun gnus-simplify-subject (subject &optional re-only)
1586   "Remove `Re:' and words in parentheses.
1587 If RE-ONLY is non-nil, strip leading `Re:'s only."
1588   (let ((case-fold-search t))           ;Ignore case.
1589     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1590     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1591       (setq subject (substring subject (match-end 0))))
1592     ;; Remove uninteresting prefixes.
1593     (when (and (not re-only)
1594                gnus-simplify-ignored-prefixes
1595                (string-match gnus-simplify-ignored-prefixes subject))
1596       (setq subject (substring subject (match-end 0))))
1597     ;; Remove words in parentheses from end.
1598     (unless re-only
1599       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1600         (setq subject (substring subject 0 (match-beginning 0)))))
1601     ;; Return subject string.
1602     subject))
1603
1604 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1605 ;; all whitespace.
1606 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1607   (goto-char (point-min))
1608   (while (re-search-forward regexp nil t)
1609     (replace-match (or newtext ""))))
1610
1611 (defun gnus-simplify-buffer-fuzzy ()
1612   "Simplify string in the buffer fuzzily.
1613 The string in the accessible portion of the current buffer is simplified.
1614 It is assumed to be a single-line subject.
1615 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1616 matter is removed.  Additional things can be deleted by setting
1617 `gnus-simplify-subject-fuzzy-regexp'."
1618   (let ((case-fold-search t)
1619         (modified-tick))
1620     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1621
1622     (while (not (eq modified-tick (buffer-modified-tick)))
1623       (setq modified-tick (buffer-modified-tick))
1624       (cond
1625        ((listp gnus-simplify-subject-fuzzy-regexp)
1626         (mapcar 'gnus-simplify-buffer-fuzzy-step
1627                 gnus-simplify-subject-fuzzy-regexp))
1628        (gnus-simplify-subject-fuzzy-regexp
1629         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1630       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1631       (gnus-simplify-buffer-fuzzy-step
1632        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1633       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1634
1635     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1636     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1637     (gnus-simplify-buffer-fuzzy-step " $")
1638     (gnus-simplify-buffer-fuzzy-step "^ +")))
1639
1640 (defun gnus-simplify-subject-fuzzy (subject)
1641   "Simplify a subject string fuzzily.
1642 See `gnus-simplify-buffer-fuzzy' for details."
1643   (save-excursion
1644     (gnus-set-work-buffer)
1645     (let ((case-fold-search t))
1646       ;; Remove uninteresting prefixes.
1647       (when (and gnus-simplify-ignored-prefixes
1648                  (string-match gnus-simplify-ignored-prefixes subject))
1649         (setq subject (substring subject (match-end 0))))
1650       (insert subject)
1651       (inline (gnus-simplify-buffer-fuzzy))
1652       (buffer-string))))
1653
1654 (defsubst gnus-simplify-subject-fully (subject)
1655   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1656   (cond
1657    (gnus-simplify-subject-functions
1658     (gnus-map-function gnus-simplify-subject-functions subject))
1659    ((null gnus-summary-gather-subject-limit)
1660     (gnus-simplify-subject-re subject))
1661    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1662     (gnus-simplify-subject-fuzzy subject))
1663    ((numberp gnus-summary-gather-subject-limit)
1664     (gnus-limit-string (gnus-simplify-subject-re subject)
1665                        gnus-summary-gather-subject-limit))
1666    (t
1667     subject)))
1668
1669 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1670   "Check whether two subjects are equal.
1671 If optional argument SIMPLE-FIRST is t, first argument is already
1672 simplified."
1673   (cond
1674    ((null simple-first)
1675     (equal (gnus-simplify-subject-fully s1)
1676            (gnus-simplify-subject-fully s2)))
1677    (t
1678     (equal s1
1679            (gnus-simplify-subject-fully s2)))))
1680
1681 (defun gnus-summary-bubble-group ()
1682   "Increase the score of the current group.
1683 This is a handy function to add to `gnus-summary-exit-hook' to
1684 increase the score of each group you read."
1685   (gnus-group-add-score gnus-newsgroup-name))
1686
1687 \f
1688 ;;;
1689 ;;; Gnus summary mode
1690 ;;;
1691
1692 (put 'gnus-summary-mode 'mode-class 'special)
1693
1694 (defvar gnus-article-commands-menu)
1695
1696 ;; Non-orthogonal keys
1697
1698 (gnus-define-keys gnus-summary-mode-map
1699   " " gnus-summary-next-page
1700   "\177" gnus-summary-prev-page
1701   [delete] gnus-summary-prev-page
1702   [backspace] gnus-summary-prev-page
1703   "\r" gnus-summary-scroll-up
1704   "\M-\r" gnus-summary-scroll-down
1705   "n" gnus-summary-next-unread-article
1706   "p" gnus-summary-prev-unread-article
1707   "N" gnus-summary-next-article
1708   "P" gnus-summary-prev-article
1709   "\M-\C-n" gnus-summary-next-same-subject
1710   "\M-\C-p" gnus-summary-prev-same-subject
1711   "\M-n" gnus-summary-next-unread-subject
1712   "\M-p" gnus-summary-prev-unread-subject
1713   "." gnus-summary-first-unread-article
1714   "," gnus-summary-best-unread-article
1715   "\M-s" gnus-summary-search-article-forward
1716   "\M-r" gnus-summary-search-article-backward
1717   "\M-S" gnus-summary-repeat-search-article-forward
1718   "\M-R" gnus-summary-repeat-search-article-backward
1719   "<" gnus-summary-beginning-of-article
1720   ">" gnus-summary-end-of-article
1721   "j" gnus-summary-goto-article
1722   "^" gnus-summary-refer-parent-article
1723   "\M-^" gnus-summary-refer-article
1724   "u" gnus-summary-tick-article-forward
1725   "!" gnus-summary-tick-article-forward
1726   "U" gnus-summary-tick-article-backward
1727   "d" gnus-summary-mark-as-read-forward
1728   "D" gnus-summary-mark-as-read-backward
1729   "E" gnus-summary-mark-as-expirable
1730   "\M-u" gnus-summary-clear-mark-forward
1731   "\M-U" gnus-summary-clear-mark-backward
1732   "k" gnus-summary-kill-same-subject-and-select
1733   "\C-k" gnus-summary-kill-same-subject
1734   "\M-\C-k" gnus-summary-kill-thread
1735   "\M-\C-l" gnus-summary-lower-thread
1736   "e" gnus-summary-edit-article
1737   "#" gnus-summary-mark-as-processable
1738   "\M-#" gnus-summary-unmark-as-processable
1739   "\M-\C-t" gnus-summary-toggle-threads
1740   "\M-\C-s" gnus-summary-show-thread
1741   "\M-\C-h" gnus-summary-hide-thread
1742   "\M-\C-f" gnus-summary-next-thread
1743   "\M-\C-b" gnus-summary-prev-thread
1744   [(meta down)] gnus-summary-next-thread
1745   [(meta up)] gnus-summary-prev-thread
1746   "\M-\C-u" gnus-summary-up-thread
1747   "\M-\C-d" gnus-summary-down-thread
1748   "&" gnus-summary-execute-command
1749   "c" gnus-summary-catchup-and-exit
1750   "\C-w" gnus-summary-mark-region-as-read
1751   "\C-t" gnus-summary-toggle-truncation
1752   "?" gnus-summary-mark-as-dormant
1753   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1754   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1755   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1756   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1757   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1758   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1759   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1760   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1761   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1762   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1763   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1764   "=" gnus-summary-expand-window
1765   "\C-x\C-s" gnus-summary-reselect-current-group
1766   "\M-g" gnus-summary-rescan-group
1767   "w" gnus-summary-stop-page-breaking
1768   "\C-c\C-r" gnus-summary-caesar-message
1769   "f" gnus-summary-followup
1770   "F" gnus-summary-followup-with-original
1771   "C" gnus-summary-cancel-article
1772   "r" gnus-summary-reply
1773   "R" gnus-summary-reply-with-original
1774   "\C-c\C-f" gnus-summary-mail-forward
1775   "o" gnus-summary-save-article
1776   "\C-o" gnus-summary-save-article-mail
1777   "|" gnus-summary-pipe-output
1778   "\M-k" gnus-summary-edit-local-kill
1779   "\M-K" gnus-summary-edit-global-kill
1780   ;; "V" gnus-version
1781   "\C-c\C-d" gnus-summary-describe-group
1782   "q" gnus-summary-exit
1783   "Q" gnus-summary-exit-no-update
1784   "\C-c\C-i" gnus-info-find-node
1785   gnus-mouse-2 gnus-mouse-pick-article
1786   [follow-link] mouse-face
1787   "m" gnus-summary-mail-other-window
1788   "a" gnus-summary-post-news
1789   "i" gnus-summary-news-other-window
1790   "x" gnus-summary-limit-to-unread
1791   "s" gnus-summary-isearch-article
1792   "t" gnus-summary-toggle-header
1793   "g" gnus-summary-show-article
1794   "l" gnus-summary-goto-last-article
1795   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1796   "\C-d" gnus-summary-enter-digest-group
1797   "\M-\C-d" gnus-summary-read-document
1798   "\M-\C-e" gnus-summary-edit-parameters
1799   "\M-\C-a" gnus-summary-customize-parameters
1800   "\C-c\C-b" gnus-bug
1801   "*" gnus-cache-enter-article
1802   "\M-*" gnus-cache-remove-article
1803   "\M-&" gnus-summary-universal-argument
1804   "\C-l" gnus-recenter
1805   "I" gnus-summary-increase-score
1806   "L" gnus-summary-lower-score
1807   "\M-i" gnus-symbolic-argument
1808   "h" gnus-summary-select-article-buffer
1809
1810   "b" gnus-article-view-part
1811   "\M-t" gnus-summary-toggle-display-buttonized
1812
1813   "V" gnus-summary-score-map
1814   "X" gnus-uu-extract-map
1815   "S" gnus-summary-send-map)
1816
1817 ;; Sort of orthogonal keymap
1818 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1819   "t" gnus-summary-tick-article-forward
1820   "!" gnus-summary-tick-article-forward
1821   "d" gnus-summary-mark-as-read-forward
1822   "r" gnus-summary-mark-as-read-forward
1823   "c" gnus-summary-clear-mark-forward
1824   " " gnus-summary-clear-mark-forward
1825   "e" gnus-summary-mark-as-expirable
1826   "x" gnus-summary-mark-as-expirable
1827   "?" gnus-summary-mark-as-dormant
1828   "b" gnus-summary-set-bookmark
1829   "B" gnus-summary-remove-bookmark
1830   "#" gnus-summary-mark-as-processable
1831   "\M-#" gnus-summary-unmark-as-processable
1832   "S" gnus-summary-limit-include-expunged
1833   "C" gnus-summary-catchup
1834   "H" gnus-summary-catchup-to-here
1835   "h" gnus-summary-catchup-from-here
1836   "\C-c" gnus-summary-catchup-all
1837   "k" gnus-summary-kill-same-subject-and-select
1838   "K" gnus-summary-kill-same-subject
1839   "P" gnus-uu-mark-map)
1840
1841 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1842   "c" gnus-summary-clear-above
1843   "u" gnus-summary-tick-above
1844   "m" gnus-summary-mark-above
1845   "k" gnus-summary-kill-below)
1846
1847 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1848   "/" gnus-summary-limit-to-subject
1849   "n" gnus-summary-limit-to-articles
1850   "b" gnus-summary-limit-to-bodies
1851   "w" gnus-summary-pop-limit
1852   "s" gnus-summary-limit-to-subject
1853   "a" gnus-summary-limit-to-author
1854   "u" gnus-summary-limit-to-unread
1855   "m" gnus-summary-limit-to-marks
1856   "M" gnus-summary-limit-exclude-marks
1857   "v" gnus-summary-limit-to-score
1858   "*" gnus-summary-limit-include-cached
1859   "D" gnus-summary-limit-include-dormant
1860   "T" gnus-summary-limit-include-thread
1861   "d" gnus-summary-limit-exclude-dormant
1862   "t" gnus-summary-limit-to-age
1863   "." gnus-summary-limit-to-unseen
1864   "x" gnus-summary-limit-to-extra
1865   "p" gnus-summary-limit-to-display-predicate
1866   "E" gnus-summary-limit-include-expunged
1867   "c" gnus-summary-limit-exclude-childless-dormant
1868   "C" gnus-summary-limit-mark-excluded-as-read
1869   "o" gnus-summary-insert-old-articles
1870   "N" gnus-summary-insert-new-articles
1871   "r" gnus-summary-limit-to-replied
1872   "R" gnus-summary-limit-to-recipient)
1873
1874 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1875   "n" gnus-summary-next-unread-article
1876   "p" gnus-summary-prev-unread-article
1877   "N" gnus-summary-next-article
1878   "P" gnus-summary-prev-article
1879   "\C-n" gnus-summary-next-same-subject
1880   "\C-p" gnus-summary-prev-same-subject
1881   "\M-n" gnus-summary-next-unread-subject
1882   "\M-p" gnus-summary-prev-unread-subject
1883   "f" gnus-summary-first-unread-article
1884   "b" gnus-summary-best-unread-article
1885   "j" gnus-summary-goto-article
1886   "g" gnus-summary-goto-subject
1887   "l" gnus-summary-goto-last-article
1888   "o" gnus-summary-pop-article)
1889
1890 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1891   "k" gnus-summary-kill-thread
1892   "l" gnus-summary-lower-thread
1893   "i" gnus-summary-raise-thread
1894   "T" gnus-summary-toggle-threads
1895   "t" gnus-summary-rethread-current
1896   "^" gnus-summary-reparent-thread
1897   "\M-^" gnus-summary-reparent-children
1898   "s" gnus-summary-show-thread
1899   "S" gnus-summary-show-all-threads
1900   "h" gnus-summary-hide-thread
1901   "H" gnus-summary-hide-all-threads
1902   "n" gnus-summary-next-thread
1903   "p" gnus-summary-prev-thread
1904   "u" gnus-summary-up-thread
1905   "o" gnus-summary-top-thread
1906   "d" gnus-summary-down-thread
1907   "#" gnus-uu-mark-thread
1908   "\M-#" gnus-uu-unmark-thread)
1909
1910 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1911   "g" gnus-summary-prepare
1912   "c" gnus-summary-insert-cached-articles
1913   "d" gnus-summary-insert-dormant-articles)
1914
1915 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1916   "c" gnus-summary-catchup-and-exit
1917   "C" gnus-summary-catchup-all-and-exit
1918   "E" gnus-summary-exit-no-update
1919   "Q" gnus-summary-exit
1920   "Z" gnus-summary-exit
1921   "n" gnus-summary-catchup-and-goto-next-group
1922   "p" gnus-summary-catchup-and-goto-prev-group
1923   "R" gnus-summary-reselect-current-group
1924   "G" gnus-summary-rescan-group
1925   "N" gnus-summary-next-group
1926   "s" gnus-summary-save-newsrc
1927   "P" gnus-summary-prev-group)
1928
1929 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1930   " " gnus-summary-next-page
1931   "n" gnus-summary-next-page
1932   "\177" gnus-summary-prev-page
1933   [delete] gnus-summary-prev-page
1934   "p" gnus-summary-prev-page
1935   "\r" gnus-summary-scroll-up
1936   "\M-\r" gnus-summary-scroll-down
1937   "<" gnus-summary-beginning-of-article
1938   ">" gnus-summary-end-of-article
1939   "b" gnus-summary-beginning-of-article
1940   "e" gnus-summary-end-of-article
1941   "^" gnus-summary-refer-parent-article
1942   "r" gnus-summary-refer-parent-article
1943   "D" gnus-summary-enter-digest-group
1944   "R" gnus-summary-refer-references
1945   "T" gnus-summary-refer-thread
1946   "g" gnus-summary-show-article
1947   "s" gnus-summary-isearch-article
1948   "P" gnus-summary-print-article
1949   "M" gnus-mailing-list-insinuate
1950   "t" gnus-article-babel)
1951
1952 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1953   "b" gnus-article-add-buttons
1954   "B" gnus-article-add-buttons-to-head
1955   "o" gnus-article-treat-overstrike
1956   "e" gnus-article-emphasize
1957   "w" gnus-article-fill-cited-article
1958   "Q" gnus-article-fill-long-lines
1959   "C" gnus-article-capitalize-sentences
1960   "c" gnus-article-remove-cr
1961   "q" gnus-article-de-quoted-unreadable
1962   "6" gnus-article-de-base64-unreadable
1963   "Z" gnus-article-decode-HZ
1964   "A" gnus-article-treat-ansi-sequences
1965   "h" gnus-article-wash-html
1966   "u" gnus-article-unsplit-urls
1967   "s" gnus-summary-force-verify-and-decrypt
1968   "f" gnus-article-display-x-face
1969   "l" gnus-summary-stop-page-breaking
1970   "r" gnus-summary-caesar-message
1971   "m" gnus-summary-morse-message
1972   "t" gnus-summary-toggle-header
1973   "g" gnus-treat-smiley
1974   "v" gnus-summary-verbose-headers
1975   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1976   "p" gnus-article-verify-x-pgp-sig
1977   "d" gnus-article-treat-dumbquotes
1978   "i" gnus-summary-idna-message)
1979
1980 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1981   ;; mnemonic: deuglif*Y*
1982   "u" gnus-article-outlook-unwrap-lines
1983   "a" gnus-article-outlook-repair-attribution
1984   "c" gnus-article-outlook-rearrange-citation
1985   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1986
1987 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1988   "a" gnus-article-hide
1989   "h" gnus-article-hide-headers
1990   "b" gnus-article-hide-boring-headers
1991   "s" gnus-article-hide-signature
1992   "c" gnus-article-hide-citation
1993   "C" gnus-article-hide-citation-in-followups
1994   "l" gnus-article-hide-list-identifiers
1995   "B" gnus-article-strip-banner
1996   "P" gnus-article-hide-pem
1997   "\C-c" gnus-article-hide-citation-maybe)
1998
1999 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2000   "a" gnus-article-highlight
2001   "h" gnus-article-highlight-headers
2002   "c" gnus-article-highlight-citation
2003   "s" gnus-article-highlight-signature)
2004
2005 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2006   "f" gnus-article-treat-fold-headers
2007   "u" gnus-article-treat-unfold-headers
2008   "n" gnus-article-treat-fold-newsgroups)
2009
2010 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2011   "x" gnus-article-display-x-face
2012   "d" gnus-article-display-face
2013   "s" gnus-treat-smiley
2014   "D" gnus-article-remove-images
2015   "f" gnus-treat-from-picon
2016   "m" gnus-treat-mail-picon
2017   "n" gnus-treat-newsgroups-picon)
2018
2019 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2020   "w" gnus-article-decode-mime-words
2021   "c" gnus-article-decode-charset
2022   "v" gnus-mime-view-all-parts
2023   "b" gnus-article-view-part)
2024
2025 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2026   "z" gnus-article-date-ut
2027   "u" gnus-article-date-ut
2028   "l" gnus-article-date-local
2029   "p" gnus-article-date-english
2030   "e" gnus-article-date-lapsed
2031   "o" gnus-article-date-original
2032   "i" gnus-article-date-iso8601
2033   "s" gnus-article-date-user)
2034
2035 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2036   "t" gnus-article-remove-trailing-blank-lines
2037   "l" gnus-article-strip-leading-blank-lines
2038   "m" gnus-article-strip-multiple-blank-lines
2039   "a" gnus-article-strip-blank-lines
2040   "A" gnus-article-strip-all-blank-lines
2041   "s" gnus-article-strip-leading-space
2042   "e" gnus-article-strip-trailing-space
2043   "w" gnus-article-remove-leading-whitespace)
2044
2045 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2046   "v" gnus-version
2047   "f" gnus-summary-fetch-faq
2048   "d" gnus-summary-describe-group
2049   "h" gnus-summary-describe-briefly
2050   "i" gnus-info-find-node
2051   "c" gnus-group-fetch-charter
2052   "C" gnus-group-fetch-control)
2053
2054 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2055   "e" gnus-summary-expire-articles
2056   "\M-\C-e" gnus-summary-expire-articles-now
2057   "\177" gnus-summary-delete-article
2058   [delete] gnus-summary-delete-article
2059   [backspace] gnus-summary-delete-article
2060   "m" gnus-summary-move-article
2061   "r" gnus-summary-respool-article
2062   "w" gnus-summary-edit-article
2063   "c" gnus-summary-copy-article
2064   "B" gnus-summary-crosspost-article
2065   "q" gnus-summary-respool-query
2066   "t" gnus-summary-respool-trace
2067   "i" gnus-summary-import-article
2068   "I" gnus-summary-create-article
2069   "p" gnus-summary-article-posted-p)
2070
2071 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2072   "o" gnus-summary-save-article
2073   "m" gnus-summary-save-article-mail
2074   "F" gnus-summary-write-article-file
2075   "r" gnus-summary-save-article-rmail
2076   "f" gnus-summary-save-article-file
2077   "b" gnus-summary-save-article-body-file
2078   "B" gnus-summary-write-article-body-file
2079   "h" gnus-summary-save-article-folder
2080   "v" gnus-summary-save-article-vm
2081   "p" gnus-summary-pipe-output
2082   "P" gnus-summary-muttprint
2083   "s" gnus-soup-add-article)
2084
2085 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2086   "b" gnus-summary-display-buttonized
2087   "m" gnus-summary-repair-multipart
2088   "v" gnus-article-view-part
2089   "o" gnus-article-save-part
2090   "O" gnus-article-save-part-and-strip
2091   "r" gnus-article-replace-part
2092   "d" gnus-article-delete-part
2093   "t" gnus-article-view-part-as-type
2094   "j" gnus-article-jump-to-part
2095   "c" gnus-article-copy-part
2096   "C" gnus-article-view-part-as-charset
2097   "e" gnus-article-view-part-externally
2098   "H" gnus-article-browse-html-article
2099   "E" gnus-article-encrypt-body
2100   "i" gnus-article-inline-part
2101   "|" gnus-article-pipe-part)
2102
2103 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2104   "p" gnus-summary-mark-as-processable
2105   "u" gnus-summary-unmark-as-processable
2106   "U" gnus-summary-unmark-all-processable
2107   "v" gnus-uu-mark-over
2108   "s" gnus-uu-mark-series
2109   "r" gnus-uu-mark-region
2110   "g" gnus-uu-unmark-region
2111   "R" gnus-uu-mark-by-regexp
2112   "G" gnus-uu-unmark-by-regexp
2113   "t" gnus-uu-mark-thread
2114   "T" gnus-uu-unmark-thread
2115   "a" gnus-uu-mark-all
2116   "b" gnus-uu-mark-buffer
2117   "S" gnus-uu-mark-sparse
2118   "k" gnus-summary-kill-process-mark
2119   "y" gnus-summary-yank-process-mark
2120   "w" gnus-summary-save-process-mark
2121   "i" gnus-uu-invert-processable)
2122
2123 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2124   ;;"x" gnus-uu-extract-any
2125   "m" gnus-summary-save-parts
2126   "u" gnus-uu-decode-uu
2127   "U" gnus-uu-decode-uu-and-save
2128   "s" gnus-uu-decode-unshar
2129   "S" gnus-uu-decode-unshar-and-save
2130   "o" gnus-uu-decode-save
2131   "O" gnus-uu-decode-save
2132   "b" gnus-uu-decode-binhex
2133   "B" gnus-uu-decode-binhex
2134   "p" gnus-uu-decode-postscript
2135   "P" gnus-uu-decode-postscript-and-save)
2136
2137 (gnus-define-keys
2138     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2139   "u" gnus-uu-decode-uu-view
2140   "U" gnus-uu-decode-uu-and-save-view
2141   "s" gnus-uu-decode-unshar-view
2142   "S" gnus-uu-decode-unshar-and-save-view
2143   "o" gnus-uu-decode-save-view
2144   "O" gnus-uu-decode-save-view
2145   "b" gnus-uu-decode-binhex-view
2146   "B" gnus-uu-decode-binhex-view
2147   "p" gnus-uu-decode-postscript-view
2148   "P" gnus-uu-decode-postscript-and-save-view)
2149
2150 (defvar gnus-article-post-menu nil)
2151
2152 (defconst gnus-summary-menu-maxlen 20)
2153
2154 (defun gnus-summary-menu-split (menu)
2155   ;; If we have lots of elements, divide them into groups of 20
2156   ;; and make a pane (or submenu) for each one.
2157   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2158       (let ((menu menu) sublists next
2159             (i 1))
2160         (while menu
2161           ;; Pull off the next gnus-summary-menu-maxlen elements
2162           ;; and make them the next element of sublist.
2163           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2164           (if next
2165               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2166                       nil))
2167           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2168                                              (aref (car (last menu)) 0)) menu)
2169                                sublists))
2170           (setq i (1+ i))
2171           (setq menu next))
2172         (nreverse sublists))
2173     ;; Few elements--put them all in one pane.
2174     menu))
2175
2176 (defun gnus-summary-make-menu-bar ()
2177   (gnus-turn-off-edit-menu 'summary)
2178
2179   (unless (boundp 'gnus-summary-misc-menu)
2180
2181     (easy-menu-define
2182       gnus-summary-kill-menu gnus-summary-mode-map ""
2183       (cons
2184        "Score"
2185        (nconc
2186         (list
2187          ["Customize" gnus-score-customize t])
2188         (gnus-make-score-map 'increase)
2189         (gnus-make-score-map 'lower)
2190         '(("Mark"
2191            ["Kill below" gnus-summary-kill-below t]
2192            ["Mark above" gnus-summary-mark-above t]
2193            ["Tick above" gnus-summary-tick-above t]
2194            ["Clear above" gnus-summary-clear-above t])
2195           ["Current score" gnus-summary-current-score t]
2196           ["Set score" gnus-summary-set-score t]
2197           ["Switch current score file..." gnus-score-change-score-file t]
2198           ["Set mark below..." gnus-score-set-mark-below t]
2199           ["Set expunge below..." gnus-score-set-expunge-below t]
2200           ["Edit current score file" gnus-score-edit-current-scores t]
2201           ["Edit score file" gnus-score-edit-file t]
2202           ["Trace score" gnus-score-find-trace t]
2203           ["Find words" gnus-score-find-favourite-words t]
2204           ["Rescore buffer" gnus-summary-rescore t]
2205           ["Increase score..." gnus-summary-increase-score t]
2206           ["Lower score..." gnus-summary-lower-score t]))))
2207
2208     ;; Define both the Article menu in the summary buffer and the
2209     ;; equivalent Commands menu in the article buffer here for
2210     ;; consistency.
2211     (let ((innards
2212            `(("Hide"
2213               ["All" gnus-article-hide t]
2214               ["Headers" gnus-article-hide-headers t]
2215               ["Signature" gnus-article-hide-signature t]
2216               ["Citation" gnus-article-hide-citation t]
2217               ["List identifiers" gnus-article-hide-list-identifiers t]
2218               ["Banner" gnus-article-strip-banner t]
2219               ["Boring headers" gnus-article-hide-boring-headers t])
2220              ("Highlight"
2221               ["All" gnus-article-highlight t]
2222               ["Headers" gnus-article-highlight-headers t]
2223               ["Signature" gnus-article-highlight-signature t]
2224               ["Citation" gnus-article-highlight-citation t])
2225              ("MIME"
2226               ["Words" gnus-article-decode-mime-words t]
2227               ["Charset" gnus-article-decode-charset t]
2228               ["QP" gnus-article-de-quoted-unreadable t]
2229               ["Base64" gnus-article-de-base64-unreadable t]
2230               ["View MIME buttons" gnus-summary-display-buttonized t]
2231               ["View all" gnus-mime-view-all-parts t]
2232               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt 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               ["Extract all parts..." gnus-summary-save-parts t]
2238               ("Multipart"
2239                ["Repair multipart" gnus-summary-repair-multipart t]
2240                ["Pipe part..." gnus-article-pipe-part t]
2241                ["Inline part" gnus-article-inline-part t]
2242                ["View part as type..." gnus-article-view-part-as-type t]
2243                ["Encrypt body" gnus-article-encrypt-body
2244                 :active (not (gnus-group-read-only-p))
2245                ,@(if (featurep 'xemacs) nil
2246                    '(:help "Encrypt the message body on disk"))]
2247                ["View part externally" gnus-article-view-part-externally t]
2248                ["View HTML parts in browser" gnus-article-browse-html-article t]
2249                ["View part with charset..." gnus-article-view-part-as-charset t]
2250                ["Copy part" gnus-article-copy-part t]
2251                ["Save part..." gnus-article-save-part t]
2252                ["View part" gnus-article-view-part t]))
2253              ("Date"
2254               ["Local" gnus-article-date-local t]
2255               ["ISO8601" gnus-article-date-iso8601 t]
2256               ["UT" gnus-article-date-ut t]
2257               ["Original" gnus-article-date-original t]
2258               ["Lapsed" gnus-article-date-lapsed t]
2259               ["User-defined" gnus-article-date-user t])
2260              ("Display"
2261               ["Remove images" gnus-article-remove-images t]
2262               ["Toggle smiley" gnus-treat-smiley t]
2263               ["Show X-Face" gnus-article-display-x-face t]
2264               ["Show picons in From" gnus-treat-from-picon t]
2265               ["Show picons in mail headers" gnus-treat-mail-picon t]
2266               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2267               ("View as different encoding"
2268                ,@(gnus-summary-menu-split
2269                   (mapcar
2270                    (lambda (cs)
2271                      ;; Since easymenu under Emacs doesn't allow
2272                      ;; lambda forms for menu commands, we should
2273                      ;; provide intern'ed function symbols.
2274                      (let ((command (intern (format "\
2275 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2276                        (fset command
2277                              `(lambda ()
2278                                 (interactive)
2279                                 (let ((gnus-summary-show-article-charset-alist
2280                                        '((1 . ,cs))))
2281                                   (gnus-summary-show-article 1))))
2282                        `[,(symbol-name cs) ,command t]))
2283                    (sort (if (fboundp 'coding-system-list)
2284                              (coding-system-list)
2285                            (mapcar 'car mm-mime-mule-charset-alist))
2286                          'string<)))))
2287              ("Washing"
2288               ("Remove Blanks"
2289                ["Leading" gnus-article-strip-leading-blank-lines t]
2290                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2291                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2292                ["All of the above" gnus-article-strip-blank-lines t]
2293                ["All" gnus-article-strip-all-blank-lines t]
2294                ["Leading space" gnus-article-strip-leading-space t]
2295                ["Trailing space" gnus-article-strip-trailing-space t]
2296                ["Leading space in headers"
2297                 gnus-article-remove-leading-whitespace t])
2298               ["Overstrike" gnus-article-treat-overstrike t]
2299               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2300               ["Emphasis" gnus-article-emphasize t]
2301               ["Word wrap" gnus-article-fill-cited-article t]
2302               ["Fill long lines" gnus-article-fill-long-lines t]
2303               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2304               ["Remove CR" gnus-article-remove-cr t]
2305               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2306               ["Base64" gnus-article-de-base64-unreadable t]
2307               ["Rot 13" gnus-summary-caesar-message
2308                ,@(if (featurep 'xemacs) '(t)
2309                    '(:help "\"Caesar rotate\" article by 13"))]
2310               ["De-IDNA" gnus-summary-idna-message t]
2311               ["Morse decode" gnus-summary-morse-message t]
2312               ["Unix pipe..." gnus-summary-pipe-message t]
2313               ["Add buttons" gnus-article-add-buttons t]
2314               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2315               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2316               ["Verbose header" gnus-summary-verbose-headers t]
2317               ["Toggle header" gnus-summary-toggle-header t]
2318               ["Unfold headers" gnus-article-treat-unfold-headers t]
2319               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2320               ["Html" gnus-article-wash-html t]
2321               ["Unsplit URLs" gnus-article-unsplit-urls t]
2322               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2323               ["Decode HZ" gnus-article-decode-HZ t]
2324               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2325               ("(Outlook) Deuglify"
2326                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2327                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2328                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2329                ["Full (Outlook) deuglify"
2330                 gnus-article-outlook-deuglify-article t])
2331               )
2332              ("Output"
2333               ["Save in default format..." gnus-summary-save-article
2334                ,@(if (featurep 'xemacs) '(t)
2335                    '(:help "Save article using default method"))]
2336               ["Save in file..." gnus-summary-save-article-file
2337                ,@(if (featurep 'xemacs) '(t)
2338                    '(:help "Save article in file"))]
2339               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2340               ["Save in MH folder..." gnus-summary-save-article-folder t]
2341               ["Save in VM folder..." gnus-summary-save-article-vm t]
2342               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2343               ["Save body in file..." gnus-summary-save-article-body-file t]
2344               ["Pipe through a filter..." gnus-summary-pipe-output t]
2345               ["Add to SOUP packet" gnus-soup-add-article t]
2346               ["Print with Muttprint..." gnus-summary-muttprint t]
2347               ["Print" gnus-summary-print-article
2348                ,@(if (featurep 'xemacs) '(t)
2349                    '(:help "Generate and print a PostScript image"))])
2350              ("Copy, move,... (Backend)"
2351               ,@(if (featurep 'xemacs) nil
2352                   '(:help "Copying, moving, expiring articles..."))
2353               ["Respool article..." gnus-summary-respool-article t]
2354               ["Move article..." gnus-summary-move-article
2355                (gnus-check-backend-function
2356                 'request-move-article gnus-newsgroup-name)]
2357               ["Copy article..." gnus-summary-copy-article t]
2358               ["Crosspost article..." gnus-summary-crosspost-article
2359                (gnus-check-backend-function
2360                 'request-replace-article gnus-newsgroup-name)]
2361               ["Import file..." gnus-summary-import-article
2362                (gnus-check-backend-function
2363                 'request-accept-article gnus-newsgroup-name)]
2364               ["Create article..." gnus-summary-create-article
2365                (gnus-check-backend-function
2366                 'request-accept-article gnus-newsgroup-name)]
2367               ["Check if posted" gnus-summary-article-posted-p t]
2368               ["Edit article" gnus-summary-edit-article
2369                (not (gnus-group-read-only-p))]
2370               ["Delete article" gnus-summary-delete-article
2371                (gnus-check-backend-function
2372                 'request-expire-articles gnus-newsgroup-name)]
2373               ["Query respool" gnus-summary-respool-query t]
2374               ["Trace respool" gnus-summary-respool-trace t]
2375               ["Delete expirable articles" gnus-summary-expire-articles-now
2376                (gnus-check-backend-function
2377                 'request-expire-articles gnus-newsgroup-name)])
2378              ("Extract"
2379               ["Uudecode" gnus-uu-decode-uu
2380                ,@(if (featurep 'xemacs) '(t)
2381                    '(:help "Decode uuencoded article(s)"))]
2382               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2383               ["Unshar" gnus-uu-decode-unshar t]
2384               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2385               ["Save" gnus-uu-decode-save t]
2386               ["Binhex" gnus-uu-decode-binhex t]
2387               ["Postscript" gnus-uu-decode-postscript t]
2388               ["All MIME parts" gnus-summary-save-parts t])
2389              ("Cache"
2390               ["Enter article" gnus-cache-enter-article t]
2391               ["Remove article" gnus-cache-remove-article t])
2392              ["Translate" gnus-article-babel t]
2393              ["Select article buffer" gnus-summary-select-article-buffer t]
2394              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2395              ["Isearch article..." gnus-summary-isearch-article t]
2396              ["Beginning of the article" gnus-summary-beginning-of-article t]
2397              ["End of the article" gnus-summary-end-of-article t]
2398              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2399              ["Fetch referenced articles" gnus-summary-refer-references t]
2400              ["Fetch current thread" gnus-summary-refer-thread t]
2401              ["Fetch article with id..." gnus-summary-refer-article t]
2402              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2403              ["Redisplay" gnus-summary-show-article t]
2404              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2405       (easy-menu-define
2406         gnus-summary-article-menu gnus-summary-mode-map ""
2407         (cons "Article" innards))
2408
2409       (if (not (keymapp gnus-summary-article-menu))
2410           (easy-menu-define
2411             gnus-article-commands-menu gnus-article-mode-map ""
2412             (cons "Commands" innards))
2413         ;; in Emacs, don't share menu.
2414         (setq gnus-article-commands-menu
2415               (copy-keymap gnus-summary-article-menu))
2416         (define-key gnus-article-mode-map [menu-bar commands]
2417           (cons "Commands" gnus-article-commands-menu))))
2418
2419     (easy-menu-define
2420       gnus-summary-thread-menu gnus-summary-mode-map ""
2421       '("Threads"
2422         ["Find all messages in thread" gnus-summary-refer-thread t]
2423         ["Toggle threading" gnus-summary-toggle-threads t]
2424         ["Hide threads" gnus-summary-hide-all-threads t]
2425         ["Show threads" gnus-summary-show-all-threads t]
2426         ["Hide thread" gnus-summary-hide-thread t]
2427         ["Show thread" gnus-summary-show-thread t]
2428         ["Go to next thread" gnus-summary-next-thread t]
2429         ["Go to previous thread" gnus-summary-prev-thread t]
2430         ["Go down thread" gnus-summary-down-thread t]
2431         ["Go up thread" gnus-summary-up-thread t]
2432         ["Top of thread" gnus-summary-top-thread t]
2433         ["Mark thread as read" gnus-summary-kill-thread t]
2434         ["Lower thread score" gnus-summary-lower-thread t]
2435         ["Raise thread score" gnus-summary-raise-thread t]
2436         ["Rethread current" gnus-summary-rethread-current t]))
2437
2438     (easy-menu-define
2439       gnus-summary-post-menu gnus-summary-mode-map ""
2440       `("Post"
2441         ["Send a message (mail or news)" gnus-summary-post-news
2442          ,@(if (featurep 'xemacs) '(t)
2443              '(:help "Compose a new message (mail or news)"))]
2444         ["Followup" gnus-summary-followup
2445          ,@(if (featurep 'xemacs) '(t)
2446              '(:help "Post followup to this article"))]
2447         ["Followup and yank" gnus-summary-followup-with-original
2448          ,@(if (featurep 'xemacs) '(t)
2449              '(:help "Post followup to this article, quoting its contents"))]
2450         ["Supersede article" gnus-summary-supersede-article t]
2451         ["Cancel article" gnus-summary-cancel-article
2452          ,@(if (featurep 'xemacs) '(t)
2453              '(:help "Cancel an article you posted"))]
2454         ["Reply" gnus-summary-reply t]
2455         ["Reply and yank" gnus-summary-reply-with-original t]
2456         ["Wide reply" gnus-summary-wide-reply t]
2457         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2458          ,@(if (featurep 'xemacs) '(t)
2459              '(:help "Mail a reply, quoting this article"))]
2460         ["Very wide reply" gnus-summary-very-wide-reply t]
2461         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2462          ,@(if (featurep 'xemacs) '(t)
2463              '(:help "Mail a very wide reply, quoting this article"))]
2464         ["Mail forward" gnus-summary-mail-forward t]
2465         ["Post forward" gnus-summary-post-forward t]
2466         ["Digest and mail" gnus-uu-digest-mail-forward t]
2467         ["Digest and post" gnus-uu-digest-post-forward t]
2468         ["Resend message" gnus-summary-resend-message t]
2469         ["Resend message edit" gnus-summary-resend-message-edit t]
2470         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2471         ["Send a mail" gnus-summary-mail-other-window t]
2472         ["Create a local message" gnus-summary-news-other-window t]
2473         ["Uuencode and post" gnus-uu-post-news
2474          ,@(if (featurep 'xemacs) '(t)
2475              '(:help "Post a uuencoded article"))]
2476         ["Followup via news" gnus-summary-followup-to-mail t]
2477         ["Followup via news and yank"
2478          gnus-summary-followup-to-mail-with-original t]
2479         ;;("Draft"
2480         ;;["Send" gnus-summary-send-draft t]
2481         ;;["Send bounced" gnus-resend-bounced-mail t])
2482         ))
2483
2484     (cond
2485      ((not (keymapp gnus-summary-post-menu))
2486       (setq gnus-article-post-menu gnus-summary-post-menu))
2487      ((not gnus-article-post-menu)
2488       ;; Don't share post menu.
2489       (setq gnus-article-post-menu
2490             (copy-keymap gnus-summary-post-menu))))
2491     (define-key gnus-article-mode-map [menu-bar post]
2492       (cons "Post" gnus-article-post-menu))
2493
2494     (easy-menu-define
2495       gnus-summary-misc-menu gnus-summary-mode-map ""
2496       `("Gnus"
2497         ("Mark Read"
2498          ["Mark as read" gnus-summary-mark-as-read-forward t]
2499          ["Mark same subject and select"
2500           gnus-summary-kill-same-subject-and-select t]
2501          ["Mark same subject" gnus-summary-kill-same-subject t]
2502          ["Catchup" gnus-summary-catchup
2503           ,@(if (featurep 'xemacs) '(t)
2504               '(:help "Mark unread articles in this group as read"))]
2505          ["Catchup all" gnus-summary-catchup-all t]
2506          ["Catchup to here" gnus-summary-catchup-to-here t]
2507          ["Catchup from here" gnus-summary-catchup-from-here t]
2508          ["Catchup region" gnus-summary-mark-region-as-read
2509           (gnus-mark-active-p)]
2510          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2511         ("Mark Various"
2512          ["Tick" gnus-summary-tick-article-forward t]
2513          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2514          ["Remove marks" gnus-summary-clear-mark-forward t]
2515          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2516          ["Set bookmark" gnus-summary-set-bookmark t]
2517          ["Remove bookmark" gnus-summary-remove-bookmark t])
2518         ("Limit to"
2519          ["Marks..." gnus-summary-limit-to-marks t]
2520          ["Subject..." gnus-summary-limit-to-subject t]
2521          ["Author..." gnus-summary-limit-to-author t]
2522          ["Recipient..." gnus-summary-limit-to-recipient t]
2523          ["Age..." gnus-summary-limit-to-age t]
2524          ["Extra..." gnus-summary-limit-to-extra t]
2525          ["Score..." gnus-summary-limit-to-score t]
2526          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2527          ["Unread" gnus-summary-limit-to-unread t]
2528          ["Unseen" gnus-summary-limit-to-unseen t]
2529          ["Replied" gnus-summary-limit-to-replied t]
2530          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2531          ["Next articles" gnus-summary-limit-to-articles t]
2532          ["Pop limit" gnus-summary-pop-limit t]
2533          ["Show dormant" gnus-summary-limit-include-dormant t]
2534          ["Hide childless dormant"
2535           gnus-summary-limit-exclude-childless-dormant t]
2536          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2537          ["Hide marked" gnus-summary-limit-exclude-marks t]
2538          ["Show expunged" gnus-summary-limit-include-expunged t])
2539         ("Process Mark"
2540          ["Set mark" gnus-summary-mark-as-processable t]
2541          ["Remove mark" gnus-summary-unmark-as-processable t]
2542          ["Remove all marks" gnus-summary-unmark-all-processable t]
2543          ["Invert marks" gnus-uu-invert-processable t]
2544          ["Mark above" gnus-uu-mark-over t]
2545          ["Mark series" gnus-uu-mark-series t]
2546          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2547          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2548          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2549          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2550          ["Mark all" gnus-uu-mark-all t]
2551          ["Mark buffer" gnus-uu-mark-buffer t]
2552          ["Mark sparse" gnus-uu-mark-sparse t]
2553          ["Mark thread" gnus-uu-mark-thread t]
2554          ["Unmark thread" gnus-uu-unmark-thread t]
2555          ("Process Mark Sets"
2556           ["Kill" gnus-summary-kill-process-mark t]
2557           ["Yank" gnus-summary-yank-process-mark
2558            gnus-newsgroup-process-stack]
2559           ["Save" gnus-summary-save-process-mark t]
2560           ["Run command on marked..." gnus-summary-universal-argument t]))
2561         ("Scroll article"
2562          ["Page forward" gnus-summary-next-page
2563           ,@(if (featurep 'xemacs) '(t)
2564               '(:help "Show next page of article"))]
2565          ["Page backward" gnus-summary-prev-page
2566           ,@(if (featurep 'xemacs) '(t)
2567               '(:help "Show previous page of article"))]
2568          ["Line forward" gnus-summary-scroll-up t])
2569         ("Move"
2570          ["Next unread article" gnus-summary-next-unread-article t]
2571          ["Previous unread article" gnus-summary-prev-unread-article t]
2572          ["Next article" gnus-summary-next-article t]
2573          ["Previous article" gnus-summary-prev-article t]
2574          ["Next unread subject" gnus-summary-next-unread-subject t]
2575          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2576          ["Next article same subject" gnus-summary-next-same-subject t]
2577          ["Previous article same subject" gnus-summary-prev-same-subject t]
2578          ["First unread article" gnus-summary-first-unread-article t]
2579          ["Best unread article" gnus-summary-best-unread-article t]
2580          ["Go to subject number..." gnus-summary-goto-subject t]
2581          ["Go to article number..." gnus-summary-goto-article t]
2582          ["Go to the last article" gnus-summary-goto-last-article t]
2583          ["Pop article off history" gnus-summary-pop-article t])
2584         ("Sort"
2585          ["Sort by number" gnus-summary-sort-by-number t]
2586          ["Sort by author" gnus-summary-sort-by-author t]
2587          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2588          ["Sort by subject" gnus-summary-sort-by-subject t]
2589          ["Sort by date" gnus-summary-sort-by-date t]
2590          ["Sort by score" gnus-summary-sort-by-score t]
2591          ["Sort by lines" gnus-summary-sort-by-lines t]
2592          ["Sort by characters" gnus-summary-sort-by-chars t]
2593          ["Randomize" gnus-summary-sort-by-random t]
2594          ["Original sort" gnus-summary-sort-by-original t])
2595         ("Help"
2596          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2597          ["Describe group" gnus-summary-describe-group t]
2598          ["Fetch charter" gnus-group-fetch-charter
2599           ,@(if (featurep 'xemacs) nil
2600               '(:help "Display the charter of the current group"))]
2601          ["Fetch control message" gnus-group-fetch-control
2602           ,@(if (featurep 'xemacs) nil
2603               '(:help "Display the archived control message for the current group"))]
2604          ["Read manual" gnus-info-find-node t])
2605         ("Modes"
2606          ["Pick and read" gnus-pick-mode t]
2607          ["Binary" gnus-binary-mode t])
2608         ("Regeneration"
2609          ["Regenerate" gnus-summary-prepare t]
2610          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2611          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2612          ["Toggle threading" gnus-summary-toggle-threads t])
2613         ["See old articles" gnus-summary-insert-old-articles t]
2614         ["See new articles" gnus-summary-insert-new-articles t]
2615         ["Filter articles..." gnus-summary-execute-command t]
2616         ["Run command on articles..." gnus-summary-universal-argument t]
2617         ["Search articles forward..." gnus-summary-search-article-forward t]
2618         ["Search articles backward..." gnus-summary-search-article-backward t]
2619         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2620         ["Expand window" gnus-summary-expand-window t]
2621         ["Expire expirable articles" gnus-summary-expire-articles
2622          (gnus-check-backend-function
2623           'request-expire-articles gnus-newsgroup-name)]
2624         ["Edit local kill file" gnus-summary-edit-local-kill t]
2625         ["Edit main kill file" gnus-summary-edit-global-kill t]
2626         ["Edit group parameters" gnus-summary-edit-parameters t]
2627         ["Customize group parameters" gnus-summary-customize-parameters t]
2628         ["Send a bug report" gnus-bug t]
2629         ("Exit"
2630          ["Catchup and exit" gnus-summary-catchup-and-exit
2631           ,@(if (featurep 'xemacs) '(t)
2632               '(:help "Mark unread articles in this group as read, then exit"))]
2633          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2634          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2635          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2636          ["Exit group" gnus-summary-exit
2637           ,@(if (featurep 'xemacs) '(t)
2638               '(:help "Exit current group, return to group selection mode"))]
2639          ["Exit group without updating" gnus-summary-exit-no-update t]
2640          ["Exit and goto next group" gnus-summary-next-group t]
2641          ["Exit and goto prev group" gnus-summary-prev-group t]
2642          ["Reselect group" gnus-summary-reselect-current-group t]
2643          ["Rescan group" gnus-summary-rescan-group t]
2644          ["Update dribble" gnus-summary-save-newsrc t])))
2645
2646     (gnus-run-hooks 'gnus-summary-menu-hook)))
2647
2648 (defvar gnus-summary-tool-bar-map nil)
2649
2650 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2651 ;; affect _new_ message buffers.  We might add a function that walks thru all
2652 ;; summary-mode buffers and force the update.
2653 (defun gnus-summary-tool-bar-update (&optional symbol value)
2654   "Update summary mode toolbar.
2655 Setter function for custom variables."
2656   (setq-default gnus-summary-tool-bar-map nil)
2657   (when symbol
2658     ;; When used as ":set" function:
2659     (set-default symbol value))
2660   (when (gnus-buffer-live-p gnus-summary-buffer)
2661     (with-current-buffer gnus-summary-buffer
2662       (gnus-summary-make-tool-bar))))
2663
2664 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2665                                      'gnus-summary-tool-bar-gnome
2666                                    'gnus-summary-tool-bar-retro)
2667   "Specifies the Gnus summary tool bar.
2668
2669 It can be either a list or a symbol refering to a list.  See
2670 `gmm-tool-bar-from-list' for the format of the list.  The
2671 default key map is `gnus-summary-mode-map'.
2672
2673 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2674 `gnus-summary-tool-bar-retro'."
2675   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2676                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2677                  (repeat :tag "User defined list" gmm-tool-bar-item)
2678                  (symbol))
2679   :version "23.0" ;; No Gnus
2680   :initialize 'custom-initialize-default
2681   :set 'gnus-summary-tool-bar-update
2682   :group 'gnus-summary)
2683
2684 (defcustom gnus-summary-tool-bar-gnome
2685   '((gnus-summary-post-news "mail/compose" nil)
2686     (gnus-summary-insert-new-articles "mail/inbox" nil
2687                                       :visible (or (not gnus-agent)
2688                                                    gnus-plugged))
2689     (gnus-summary-reply-with-original "mail/reply")
2690     (gnus-summary-reply "mail/reply" nil :visible nil)
2691     (gnus-summary-followup-with-original "mail/reply-all")
2692     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2693     (gnus-summary-mail-forward "mail/forward")
2694     (gnus-summary-save-article "mail/save")
2695     (gnus-summary-search-article-forward "search" nil :visible nil)
2696     (gnus-summary-print-article "print")
2697     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2698     ;; Some new commands that may need more suitable icons:
2699     (gnus-summary-save-newsrc "save" nil :visible nil)
2700     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2701     (gnus-summary-prev-article "left-arrow")
2702     (gnus-summary-next-article "right-arrow")
2703     (gnus-summary-next-page "next-page")
2704     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2705     ;;
2706     ;; Maybe some sort-by-... could be added:
2707     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2708     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2709     (gnus-summary-mark-as-expirable
2710      "delete" nil
2711      :visible (gnus-check-backend-function 'request-expire-articles
2712                                            gnus-newsgroup-name))
2713     (gnus-summary-mark-as-spam
2714      "mail/spam" t
2715      :visible (and (fboundp 'spam-group-ham-contents-p)
2716                    (spam-group-ham-contents-p gnus-newsgroup-name))
2717      :help "Mark as spam")
2718     (gnus-summary-mark-as-read-forward
2719      "mail/not-spam" nil
2720      :visible (and (fboundp 'spam-group-spam-contents-p)
2721                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2722     ;;
2723     (gnus-summary-exit "exit")
2724     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2725     (gnus-info-find-node "help"))
2726   "List of functions for the summary tool bar (GNOME style).
2727
2728 See `gmm-tool-bar-from-list' for the format of the list."
2729   :type '(repeat gmm-tool-bar-item)
2730   :version "23.0" ;; No Gnus
2731   :initialize 'custom-initialize-default
2732   :set 'gnus-summary-tool-bar-update
2733   :group 'gnus-summary)
2734
2735 (defcustom gnus-summary-tool-bar-retro
2736   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2737     (gnus-summary-next-unread-article "gnus/next-ur")
2738     (gnus-summary-post-news "gnus/post")
2739     (gnus-summary-followup-with-original "gnus/fuwo")
2740     (gnus-summary-followup "gnus/followup")
2741     (gnus-summary-reply-with-original "gnus/reply-wo")
2742     (gnus-summary-reply "gnus/reply")
2743     (gnus-summary-caesar-message "gnus/rot13")
2744     (gnus-uu-decode-uu "gnus/uu-decode")
2745     (gnus-summary-save-article-file "gnus/save-aif")
2746     (gnus-summary-save-article "gnus/save-art")
2747     (gnus-uu-post-news "gnus/uu-post")
2748     (gnus-summary-catchup "gnus/catchup")
2749     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2750     (gnus-summary-exit "gnus/exit-summ")
2751     ;; Some new command that may need more suitable icons:
2752     (gnus-summary-print-article "gnus/print" nil :visible nil)
2753     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2754     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2755     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2756     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2757     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2758     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2759     ;;
2760     (gnus-info-find-node "gnus/help" nil :visible nil))
2761   "List of functions for the summary tool bar (retro look).
2762
2763 See `gmm-tool-bar-from-list' for the format of the list."
2764   :type '(repeat gmm-tool-bar-item)
2765   :version "23.0" ;; No Gnus
2766   :initialize 'custom-initialize-default
2767   :set 'gnus-summary-tool-bar-update
2768   :group 'gnus-summary)
2769
2770 (defcustom gnus-summary-tool-bar-zap-list t
2771   "List of icon items from the global tool bar.
2772 These items are not displayed in the Gnus summary mode tool bar.
2773
2774 See `gmm-tool-bar-from-list' for the format of the list."
2775   :type 'gmm-tool-bar-zap-list
2776   :version "23.0" ;; No Gnus
2777   :initialize 'custom-initialize-default
2778   :set 'gnus-summary-tool-bar-update
2779   :group 'gnus-summary)
2780
2781 (defvar image-load-path)
2782
2783 (defun gnus-summary-make-tool-bar (&optional force)
2784   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2785 When FORCE, rebuild the tool bar."
2786   (when (and (not (featurep 'xemacs))
2787              (boundp 'tool-bar-mode)
2788              tool-bar-mode
2789              (or (not gnus-summary-tool-bar-map) force))
2790     (let* ((load-path
2791             (gmm-image-load-path-for-library "gnus"
2792                                              "mail/save.xpm"
2793                                              nil t))
2794            (image-load-path (cons (car load-path)
2795                                   (when (boundp 'image-load-path)
2796                                     image-load-path)))
2797            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2798                                         gnus-summary-tool-bar-zap-list
2799                                         'gnus-summary-mode-map)))
2800       (when map
2801         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2802         ;; uses it's value.
2803         (setq gnus-summary-tool-bar-map map))))
2804   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2805
2806 (defun gnus-score-set-default (var value)
2807   "A version of set that updates the GNU Emacs menu-bar."
2808   (set var value)
2809   ;; It is the message that forces the active status to be updated.
2810   (message ""))
2811
2812 (defun gnus-make-score-map (type)
2813   "Make a summary score map of type TYPE."
2814   (if t
2815       nil
2816     (let ((headers '(("author" "from" string)
2817                      ("subject" "subject" string)
2818                      ("article body" "body" string)
2819                      ("article head" "head" string)
2820                      ("xref" "xref" string)
2821                      ("extra header" "extra" string)
2822                      ("lines" "lines" number)
2823                      ("followups to author" "followup" string)))
2824           (types '((number ("less than" <)
2825                            ("greater than" >)
2826                            ("equal" =))
2827                    (string ("substring" s)
2828                            ("exact string" e)
2829                            ("fuzzy string" f)
2830                            ("regexp" r))))
2831           (perms '(("temporary" (current-time-string))
2832                    ("permanent" nil)
2833                    ("immediate" now)))
2834           header)
2835       (list
2836        (apply
2837         'nconc
2838         (list
2839          (if (eq type 'lower)
2840              "Lower score"
2841            "Increase score"))
2842         (let (outh)
2843           (while headers
2844             (setq header (car headers))
2845             (setq outh
2846                   (cons
2847                    (apply
2848                     'nconc
2849                     (list (car header))
2850                     (let ((ts (cdr (assoc (nth 2 header) types)))
2851                           outt)
2852                       (while ts
2853                         (setq outt
2854                               (cons
2855                                (apply
2856                                 'nconc
2857                                 (list (caar ts))
2858                                 (let ((ps perms)
2859                                       outp)
2860                                   (while ps
2861                                     (setq outp
2862                                           (cons
2863                                            (vector
2864                                             (caar ps)
2865                                             (list
2866                                              'gnus-summary-score-entry
2867                                              (nth 1 header)
2868                                              (if (or (string= (nth 1 header)
2869                                                               "head")
2870                                                      (string= (nth 1 header)
2871                                                               "body"))
2872                                                  ""
2873                                                (list 'gnus-summary-header
2874                                                      (nth 1 header)))
2875                                              (list 'quote (nth 1 (car ts)))
2876                                              (list 'gnus-score-delta-default
2877                                                    nil)
2878                                              (nth 1 (car ps))
2879                                              t)
2880                                             t)
2881                                            outp))
2882                                     (setq ps (cdr ps)))
2883                                   (list (nreverse outp))))
2884                                outt))
2885                         (setq ts (cdr ts)))
2886                       (list (nreverse outt))))
2887                    outh))
2888             (setq headers (cdr headers)))
2889           (list (nreverse outh))))))))
2890
2891 \f
2892
2893 (defun gnus-summary-mode (&optional group)
2894   "Major mode for reading articles.
2895
2896 All normal editing commands are switched off.
2897 \\<gnus-summary-mode-map>
2898 Each line in this buffer represents one article.  To read an
2899 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2900 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2901 respectively.
2902
2903 You can also post articles and send mail from this buffer.  To
2904 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2905 of an article, type `\\[gnus-summary-reply]'.
2906
2907 There are approx. one gazillion commands you can execute in this
2908 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2909
2910 The following commands are available:
2911
2912 \\{gnus-summary-mode-map}"
2913   (interactive)
2914   (kill-all-local-variables)
2915   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2916     (gnus-summary-make-local-variables))
2917   (gnus-summary-make-local-variables)
2918   (setq gnus-newsgroup-name group)
2919   (when (gnus-visual-p 'summary-menu 'menu)
2920     (gnus-summary-make-menu-bar)
2921     (gnus-summary-make-tool-bar))
2922   (gnus-make-thread-indent-array)
2923   (gnus-simplify-mode-line)
2924   (setq major-mode 'gnus-summary-mode)
2925   (setq mode-name "Summary")
2926   (make-local-variable 'minor-mode-alist)
2927   (use-local-map gnus-summary-mode-map)
2928   (buffer-disable-undo)
2929   (setq buffer-read-only t              ;Disable modification
2930         show-trailing-whitespace nil)
2931   (setq truncate-lines t)
2932   (setq selective-display t)
2933   (setq selective-display-ellipses t)   ;Display `...'
2934   (gnus-summary-set-display-table)
2935   (gnus-set-default-directory)
2936   (make-local-variable 'gnus-summary-line-format)
2937   (make-local-variable 'gnus-summary-line-format-spec)
2938   (make-local-variable 'gnus-summary-dummy-line-format)
2939   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2940   (make-local-variable 'gnus-summary-mark-positions)
2941   (gnus-make-local-hook 'pre-command-hook)
2942   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2943   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2944   (turn-on-gnus-mailing-list-mode)
2945   (mm-enable-multibyte)
2946   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2947   (gnus-update-summary-mark-positions))
2948
2949 (defun gnus-summary-make-local-variables ()
2950   "Make all the local summary buffer variables."
2951   (let (global)
2952     (dolist (local gnus-summary-local-variables)
2953       (if (consp local)
2954           (progn
2955             (if (eq (cdr local) 'global)
2956                 ;; Copy the global value of the variable.
2957                 (setq global (symbol-value (car local)))
2958               ;; Use the value from the list.
2959               (setq global (eval (cdr local))))
2960             (set (make-local-variable (car local)) global))
2961         ;; Simple nil-valued local variable.
2962         (set (make-local-variable local) nil)))))
2963
2964 (defun gnus-summary-clear-local-variables ()
2965   (let ((locals gnus-summary-local-variables))
2966     (while locals
2967       (if (consp (car locals))
2968           (and (symbolp (caar locals))
2969                (set (caar locals) nil))
2970         (and (symbolp (car locals))
2971              (set (car locals) nil)))
2972       (setq locals (cdr locals)))))
2973
2974 ;; Summary data functions.
2975
2976 (defmacro gnus-data-number (data)
2977   `(car ,data))
2978
2979 (defmacro gnus-data-set-number (data number)
2980   `(setcar ,data ,number))
2981
2982 (defmacro gnus-data-mark (data)
2983   `(nth 1 ,data))
2984
2985 (defmacro gnus-data-set-mark (data mark)
2986   `(setcar (nthcdr 1 ,data) ,mark))
2987
2988 (defmacro gnus-data-pos (data)
2989   `(nth 2 ,data))
2990
2991 (defmacro gnus-data-set-pos (data pos)
2992   `(setcar (nthcdr 2 ,data) ,pos))
2993
2994 (defmacro gnus-data-header (data)
2995   `(nth 3 ,data))
2996
2997 (defmacro gnus-data-set-header (data header)
2998   `(setf (nth 3 ,data) ,header))
2999
3000 (defmacro gnus-data-level (data)
3001   `(nth 4 ,data))
3002
3003 (defmacro gnus-data-unread-p (data)
3004   `(= (nth 1 ,data) gnus-unread-mark))
3005
3006 (defmacro gnus-data-read-p (data)
3007   `(/= (nth 1 ,data) gnus-unread-mark))
3008
3009 (defmacro gnus-data-pseudo-p (data)
3010   `(consp (nth 3 ,data)))
3011
3012 (defmacro gnus-data-find (number)
3013   `(assq ,number gnus-newsgroup-data))
3014
3015 (defmacro gnus-data-find-list (number &optional data)
3016   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3017      (memq (assq ,number bdata)
3018            bdata)))
3019
3020 (defmacro gnus-data-make (number mark pos header level)
3021   `(list ,number ,mark ,pos ,header ,level))
3022
3023 (defun gnus-data-enter (after-article number mark pos header level offset)
3024   (let ((data (gnus-data-find-list after-article)))
3025     (unless data
3026       (error "No such article: %d" after-article))
3027     (setcdr data (cons (gnus-data-make number mark pos header level)
3028                        (cdr data)))
3029     (setq gnus-newsgroup-data-reverse nil)
3030     (gnus-data-update-list (cddr data) offset)))
3031
3032 (defun gnus-data-enter-list (after-article list &optional offset)
3033   (when list
3034     (let ((data (and after-article (gnus-data-find-list after-article)))
3035           (ilist list))
3036       (if (not (or data
3037                    after-article))
3038           (let ((odata gnus-newsgroup-data))
3039             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3040             (when offset
3041               (gnus-data-update-list odata offset)))
3042         ;; Find the last element in the list to be spliced into the main
3043         ;; list.
3044         (setq list (last list))
3045         (if (not data)
3046             (progn
3047               (setcdr list gnus-newsgroup-data)
3048               (setq gnus-newsgroup-data ilist)
3049               (when offset
3050                 (gnus-data-update-list (cdr list) offset)))
3051           (setcdr list (cdr data))
3052           (setcdr data ilist)
3053           (when offset
3054             (gnus-data-update-list (cdr list) offset))))
3055       (setq gnus-newsgroup-data-reverse nil))))
3056
3057 (defun gnus-data-remove (article &optional offset)
3058   (let ((data gnus-newsgroup-data))
3059     (if (= (gnus-data-number (car data)) article)
3060         (progn
3061           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3062                 gnus-newsgroup-data-reverse nil)
3063           (when offset
3064             (gnus-data-update-list gnus-newsgroup-data offset)))
3065       (while (cdr data)
3066         (when (= (gnus-data-number (cadr data)) article)
3067           (setcdr data (cddr data))
3068           (when offset
3069             (gnus-data-update-list (cdr data) offset))
3070           (setq data nil
3071                 gnus-newsgroup-data-reverse nil))
3072         (setq data (cdr data))))))
3073
3074 (defmacro gnus-data-list (backward)
3075   `(if ,backward
3076        (or gnus-newsgroup-data-reverse
3077            (setq gnus-newsgroup-data-reverse
3078                  (reverse gnus-newsgroup-data)))
3079      gnus-newsgroup-data))
3080
3081 (defun gnus-data-update-list (data offset)
3082   "Add OFFSET to the POS of all data entries in DATA."
3083   (setq gnus-newsgroup-data-reverse nil)
3084   (while data
3085     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3086     (setq data (cdr data))))
3087
3088 (defun gnus-summary-article-pseudo-p (article)
3089   "Say whether this article is a pseudo article or not."
3090   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3091
3092 (defmacro gnus-summary-article-sparse-p (article)
3093   "Say whether this article is a sparse article or not."
3094   `(memq ,article gnus-newsgroup-sparse))
3095
3096 (defmacro gnus-summary-article-ancient-p (article)
3097   "Say whether this article is a sparse article or not."
3098   `(memq ,article gnus-newsgroup-ancient))
3099
3100 (defun gnus-article-parent-p (number)
3101   "Say whether this article is a parent or not."
3102   (let ((data (gnus-data-find-list number)))
3103     (and (cdr data)              ; There has to be an article after...
3104          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3105             (gnus-data-level (nth 1 data))))))
3106
3107 (defun gnus-article-children (number)
3108   "Return a list of all children to NUMBER."
3109   (let* ((data (gnus-data-find-list number))
3110          (level (gnus-data-level (car data)))
3111          children)
3112     (setq data (cdr data))
3113     (while (and data
3114                 (= (gnus-data-level (car data)) (1+ level)))
3115       (push (gnus-data-number (car data)) children)
3116       (setq data (cdr data)))
3117     children))
3118
3119 (defmacro gnus-summary-skip-intangible ()
3120   "If the current article is intangible, then jump to a different article."
3121   '(let ((to (get-text-property (point) 'gnus-intangible)))
3122      (and to (gnus-summary-goto-subject to))))
3123
3124 (defmacro gnus-summary-article-intangible-p ()
3125   "Say whether this article is intangible or not."
3126   '(get-text-property (point) 'gnus-intangible))
3127
3128 (defun gnus-article-read-p (article)
3129   "Say whether ARTICLE is read or not."
3130   (not (or (memq article gnus-newsgroup-marked)
3131            (memq article gnus-newsgroup-spam-marked)
3132            (memq article gnus-newsgroup-unreads)
3133            (memq article gnus-newsgroup-unselected)
3134            (memq article gnus-newsgroup-dormant))))
3135
3136 ;; Some summary mode macros.
3137
3138 (defmacro gnus-summary-article-number ()
3139   "The article number of the article on the current line.
3140 If there isn't an article number here, then we return the current
3141 article number."
3142   '(progn
3143      (gnus-summary-skip-intangible)
3144      (or (get-text-property (point) 'gnus-number)
3145          (gnus-summary-last-subject))))
3146
3147 (defmacro gnus-summary-article-header (&optional number)
3148   "Return the header of article NUMBER."
3149   `(gnus-data-header (gnus-data-find
3150                       ,(or number '(gnus-summary-article-number)))))
3151
3152 (defmacro gnus-summary-thread-level (&optional number)
3153   "Return the level of thread that starts with article NUMBER."
3154   `(if (and (eq gnus-summary-make-false-root 'dummy)
3155             (get-text-property (point) 'gnus-intangible))
3156        0
3157      (gnus-data-level (gnus-data-find
3158                        ,(or number '(gnus-summary-article-number))))))
3159
3160 (defmacro gnus-summary-article-mark (&optional number)
3161   "Return the mark of article NUMBER."
3162   `(gnus-data-mark (gnus-data-find
3163                     ,(or number '(gnus-summary-article-number)))))
3164
3165 (defmacro gnus-summary-article-pos (&optional number)
3166   "Return the position of the line of article NUMBER."
3167   `(gnus-data-pos (gnus-data-find
3168                    ,(or number '(gnus-summary-article-number)))))
3169
3170 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3171 (defmacro gnus-summary-article-subject (&optional number)
3172   "Return current subject string or nil if nothing."
3173   `(let ((headers
3174           ,(if number
3175                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3176              '(gnus-data-header (assq (gnus-summary-article-number)
3177                                       gnus-newsgroup-data)))))
3178      (and headers
3179           (vectorp headers)
3180           (mail-header-subject headers))))
3181
3182 (defmacro gnus-summary-article-score (&optional number)
3183   "Return current article score."
3184   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3185                   gnus-newsgroup-scored))
3186        gnus-summary-default-score 0))
3187
3188 (defun gnus-summary-article-children (&optional number)
3189   "Return a list of article numbers that are children of article NUMBER."
3190   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3191          (level (gnus-data-level (car data)))
3192          l children)
3193     (while (and (setq data (cdr data))
3194                 (> (setq l (gnus-data-level (car data))) level))
3195       (and (= (1+ level) l)
3196            (push (gnus-data-number (car data))
3197                  children)))
3198     (nreverse children)))
3199
3200 (defun gnus-summary-article-parent (&optional number)
3201   "Return the article number of the parent of article NUMBER."
3202   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3203                                     (gnus-data-list t)))
3204          (level (gnus-data-level (car data))))
3205     (if (zerop level)
3206         ()                              ; This is a root.
3207       ;; We search until we find an article with a level less than
3208       ;; this one.  That function has to be the parent.
3209       (while (and (setq data (cdr data))
3210                   (not (< (gnus-data-level (car data)) level))))
3211       (and data (gnus-data-number (car data))))))
3212
3213 (defun gnus-unread-mark-p (mark)
3214   "Say whether MARK is the unread mark."
3215   (= mark gnus-unread-mark))
3216
3217 (defun gnus-read-mark-p (mark)
3218   "Say whether MARK is one of the marks that mark as read.
3219 This is all marks except unread, ticked, dormant, and expirable."
3220   (not (or (= mark gnus-unread-mark)
3221            (= mark gnus-ticked-mark)
3222            (= mark gnus-spam-mark)
3223            (= mark gnus-dormant-mark)
3224            (= mark gnus-expirable-mark))))
3225
3226 (defmacro gnus-article-mark (number)
3227   "Return the MARK of article NUMBER.
3228 This macro should only be used when computing the mark the \"first\"
3229 time; i.e., when generating the summary lines.  After that,
3230 `gnus-summary-article-mark' should be used to examine the
3231 marks of articles."
3232   `(cond
3233     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3234     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3235     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3236     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3237     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3238     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3239     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3240     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3241            gnus-ancient-mark))))
3242
3243 ;; Saving hidden threads.
3244
3245 (defmacro gnus-save-hidden-threads (&rest forms)
3246   "Save hidden threads, eval FORMS, and restore the hidden threads."
3247   (let ((config (make-symbol "config")))
3248     `(let ((,config (gnus-hidden-threads-configuration)))
3249        (unwind-protect
3250            (save-excursion
3251              ,@forms)
3252          (gnus-restore-hidden-threads-configuration ,config)))))
3253 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3254 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3255
3256 (defun gnus-data-compute-positions ()
3257   "Compute the positions of all articles."
3258   (setq gnus-newsgroup-data-reverse nil)
3259   (let ((data gnus-newsgroup-data))
3260     (save-excursion
3261       (gnus-save-hidden-threads
3262         (gnus-summary-show-all-threads)
3263         (goto-char (point-min))
3264         (while data
3265           (while (get-text-property (point) 'gnus-intangible)
3266             (forward-line 1))
3267           (gnus-data-set-pos (car data) (+ (point) 3))
3268           (setq data (cdr data))
3269           (forward-line 1))))))
3270
3271 (defun gnus-hidden-threads-configuration ()
3272   "Return the current hidden threads configuration."
3273   (save-excursion
3274     (let (config)
3275       (goto-char (point-min))
3276       (while (search-forward "\r" nil t)
3277         (push (1- (point)) config))
3278       config)))
3279
3280 (defun gnus-restore-hidden-threads-configuration (config)
3281   "Restore hidden threads configuration from CONFIG."
3282   (save-excursion
3283     (let (point buffer-read-only)
3284       (while (setq point (pop config))
3285         (when (and (< point (point-max))
3286                    (goto-char point)
3287                    (eq (char-after) ?\n))
3288           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3289
3290 ;; Various summary mode internalish functions.
3291
3292 (defun gnus-mouse-pick-article (e)
3293   (interactive "e")
3294   (mouse-set-point e)
3295   (gnus-summary-next-page nil t))
3296
3297 (defun gnus-summary-set-display-table ()
3298   "Change the display table.
3299 Odd characters have a tendency to mess
3300 up nicely formatted displays - we make all possible glyphs
3301 display only a single character."
3302
3303   ;; We start from the standard display table, if any.
3304   (let ((table (or (copy-sequence standard-display-table)
3305                    (make-display-table)))
3306         (i 32))
3307     ;; Nix out all the control chars...
3308     (while (>= (setq i (1- i)) 0)
3309       (aset table i [??]))
3310    ;; ... but not newline and cr, of course.  (cr is necessary for the
3311     ;; selective display).
3312     (aset table ?\n nil)
3313     (aset table ?\r nil)
3314     ;; We keep TAB as well.
3315     (aset table ?\t nil)
3316     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3317     ;; Emacs 23 (unicode), that are not set already.
3318     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3319                  160
3320                256)))
3321       (while (>= (setq i (1- i)) 127)
3322         ;; Only modify if the entry is nil.
3323         (unless (aref table i)
3324           (aset table i [??]))))
3325     (setq buffer-display-table table)))
3326
3327 (defun gnus-summary-set-article-display-arrow (pos)
3328   "Update the overlay arrow to point to line at position POS."
3329   (when (and gnus-summary-display-arrow
3330              (boundp 'overlay-arrow-position)
3331              (boundp 'overlay-arrow-string))
3332     (save-excursion
3333       (goto-char pos)
3334       (beginning-of-line)
3335       (unless overlay-arrow-position
3336         (setq overlay-arrow-position (make-marker)))
3337       (setq overlay-arrow-string "=>"
3338             overlay-arrow-position (set-marker overlay-arrow-position
3339                                                (point)
3340                                                (current-buffer))))))
3341
3342 (defun gnus-summary-setup-buffer (group)
3343   "Initialize summary buffer."
3344   (let ((buffer (gnus-summary-buffer-name group))
3345         (dead-name (concat "*Dead Summary "
3346                            (gnus-group-decoded-name group) "*")))
3347     ;; If a dead summary buffer exists, we kill it.
3348     (when (gnus-buffer-live-p dead-name)
3349       (gnus-kill-buffer dead-name))
3350     (if (get-buffer buffer)
3351         (progn
3352           (set-buffer buffer)
3353           (setq gnus-summary-buffer (current-buffer))
3354           (not gnus-newsgroup-prepared))
3355       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3356       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3357       (gnus-summary-mode group)
3358       (when gnus-carpal
3359         (gnus-carpal-setup-buffer 'summary))
3360       (when (gnus-group-quit-config group)
3361         (set (make-local-variable 'gnus-single-article-buffer) nil))
3362       (make-local-variable 'gnus-article-buffer)
3363       (make-local-variable 'gnus-article-current)
3364       (make-local-variable 'gnus-original-article-buffer)
3365       (setq gnus-newsgroup-name group)
3366       ;; Set any local variables in the group parameters.
3367       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3368       t)))
3369
3370 (defun gnus-set-global-variables ()
3371   "Set the global equivalents of the buffer-local variables.
3372 They are set to the latest values they had.  These reflect the summary
3373 buffer that was in action when the last article was fetched."
3374   (when (eq major-mode 'gnus-summary-mode)
3375     (setq gnus-summary-buffer (current-buffer))
3376     (let ((name gnus-newsgroup-name)
3377           (marked gnus-newsgroup-marked)
3378           (spam gnus-newsgroup-spam-marked)
3379           (unread gnus-newsgroup-unreads)
3380           (headers gnus-current-headers)
3381           (data gnus-newsgroup-data)
3382           (summary gnus-summary-buffer)
3383           (article-buffer gnus-article-buffer)
3384           (original gnus-original-article-buffer)
3385           (gac gnus-article-current)
3386           (reffed gnus-reffed-article-number)
3387           (score-file gnus-current-score-file)
3388           (default-charset gnus-newsgroup-charset)
3389           vlist)
3390       (let ((locals gnus-newsgroup-variables))
3391         (while locals
3392           (if (consp (car locals))
3393               (push (eval (caar locals)) vlist)
3394             (push (eval (car locals)) vlist))
3395           (setq locals (cdr locals)))
3396         (setq vlist (nreverse vlist)))
3397       (with-current-buffer gnus-group-buffer
3398         (setq gnus-newsgroup-name name
3399               gnus-newsgroup-marked marked
3400               gnus-newsgroup-spam-marked spam
3401               gnus-newsgroup-unreads unread
3402               gnus-current-headers headers
3403               gnus-newsgroup-data data
3404               gnus-article-current gac
3405               gnus-summary-buffer summary
3406               gnus-article-buffer article-buffer
3407               gnus-original-article-buffer original
3408               gnus-reffed-article-number reffed
3409               gnus-current-score-file score-file
3410               gnus-newsgroup-charset default-charset)
3411         (let ((locals gnus-newsgroup-variables))
3412           (while locals
3413             (if (consp (car locals))
3414                 (set (caar locals) (pop vlist))
3415               (set (car locals) (pop vlist)))
3416             (setq locals (cdr locals))))
3417         ;; The article buffer also has local variables.
3418         (when (gnus-buffer-live-p gnus-article-buffer)
3419           (set-buffer gnus-article-buffer)
3420           (setq gnus-summary-buffer summary))))))
3421
3422 (defun gnus-summary-article-unread-p (article)
3423   "Say whether ARTICLE is unread or not."
3424   (memq article gnus-newsgroup-unreads))
3425
3426 (defun gnus-summary-first-article-p (&optional article)
3427   "Return whether ARTICLE is the first article in the buffer."
3428   (if (not (setq article (or article (gnus-summary-article-number))))
3429       nil
3430     (eq article (caar gnus-newsgroup-data))))
3431
3432 (defun gnus-summary-last-article-p (&optional article)
3433   "Return whether ARTICLE is the last article in the buffer."
3434   (if (not (setq article (or article (gnus-summary-article-number))))
3435       ;; All non-existent numbers are the last article.  :-)
3436       t
3437     (not (cdr (gnus-data-find-list article)))))
3438
3439 (defun gnus-make-thread-indent-array ()
3440   (let ((n 200))
3441     (unless (and gnus-thread-indent-array
3442                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3443       (setq gnus-thread-indent-array (make-vector 201 "")
3444             gnus-thread-indent-array-level gnus-thread-indent-level)
3445       (while (>= n 0)
3446         (aset gnus-thread-indent-array n
3447               (make-string (* n gnus-thread-indent-level) ? ))
3448         (setq n (1- n))))))
3449
3450 (defun gnus-update-summary-mark-positions ()
3451   "Compute where the summary marks are to go."
3452   (save-excursion
3453     (when (gnus-buffer-exists-p gnus-summary-buffer)
3454       (set-buffer gnus-summary-buffer))
3455     (let ((spec gnus-summary-line-format-spec)
3456           pos)
3457       (save-excursion
3458         (gnus-set-work-buffer)
3459         (let ((gnus-tmp-unread ?Z)
3460               (gnus-replied-mark ?Z)
3461               (gnus-score-below-mark ?Z)
3462               (gnus-score-over-mark ?Z)
3463               (gnus-undownloaded-mark ?Z)
3464               (gnus-summary-line-format-spec spec)
3465               (gnus-newsgroup-downloadable '(0))
3466               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3467               case-fold-search ignores)
3468           ;; Here, all marks are bound to Z.
3469           (gnus-summary-insert-line header
3470                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3471           (goto-char (point-min))
3472           ;; Memorize the positions of the same characters as dummy marks.
3473           (while (re-search-forward "[A-D]" nil t)
3474             (push (point) ignores))
3475           (erase-buffer)
3476           ;; We use A-D as dummy marks in order to know column positions
3477           ;; where marks should be inserted.
3478           (setq gnus-tmp-unread ?A
3479                 gnus-replied-mark ?B
3480                 gnus-score-below-mark ?C
3481                 gnus-score-over-mark ?C
3482                 gnus-undownloaded-mark ?D)
3483           (gnus-summary-insert-line header
3484                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3485           ;; Ignore characters which aren't dummy marks.
3486           (dolist (p ignores)
3487             (delete-region (goto-char (1- p)) p)
3488             (insert ?Z))
3489           (goto-char (point-min))
3490           (setq pos (list (cons 'unread
3491                                 (and (search-forward "A" nil t)
3492                                      (- (point) (point-min) 1)))))
3493           (goto-char (point-min))
3494           (push (cons 'replied (and (search-forward "B" nil t)
3495                                     (- (point) (point-min) 1)))
3496                 pos)
3497           (goto-char (point-min))
3498           (push (cons 'score (and (search-forward "C" nil t)
3499                                   (- (point) (point-min) 1)))
3500                 pos)
3501           (goto-char (point-min))
3502           (push (cons 'download (and (search-forward "D" nil t)
3503                                      (- (point) (point-min) 1)))
3504                 pos)))
3505       (setq gnus-summary-mark-positions pos))))
3506
3507 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3508   "Insert a dummy root in the summary buffer."
3509   (beginning-of-line)
3510   (gnus-add-text-properties
3511    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3512    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3513
3514 (defun gnus-summary-extract-address-component (from)
3515   (or (car (funcall gnus-extract-address-components from))
3516       from))
3517
3518 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3519   (let ((mail-parse-charset gnus-newsgroup-charset)
3520         ; Is it really necessary to do this next part for each summary line?
3521         ; Luckily, doesn't seem to slow things down much.
3522         (mail-parse-ignored-charsets
3523          (with-current-buffer gnus-summary-buffer
3524            gnus-newsgroup-ignored-charsets)))
3525     (or
3526      (and gnus-ignored-from-addresses
3527           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3528           (let ((extra-headers (mail-header-extra header))
3529                 to
3530                 newsgroups)
3531             (cond
3532              ((setq to (cdr (assq 'To extra-headers)))
3533               (concat gnus-summary-to-prefix
3534                       (inline
3535                         (gnus-summary-extract-address-component
3536                          (funcall gnus-decode-encoded-word-function to)))))
3537              ((setq newsgroups
3538                     (or
3539                      (cdr (assq 'Newsgroups extra-headers))
3540                      (and
3541                       (memq 'Newsgroups gnus-extra-headers)
3542                       (eq (car (gnus-find-method-for-group
3543                                 gnus-newsgroup-name)) 'nntp)
3544                       (gnus-group-real-name gnus-newsgroup-name))))
3545               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3546      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3547
3548 (defun gnus-summary-insert-line (gnus-tmp-header
3549                                  gnus-tmp-level gnus-tmp-current
3550                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3551                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3552                                  &optional gnus-tmp-dummy gnus-tmp-score
3553                                  gnus-tmp-process)
3554   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3555          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3556          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3557          (gnus-tmp-score-char
3558           (if (or (null gnus-summary-default-score)
3559                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3560                       gnus-summary-zcore-fuzz))
3561               ?                         ;Whitespace
3562             (if (< gnus-tmp-score gnus-summary-default-score)
3563                 gnus-score-below-mark gnus-score-over-mark)))
3564          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3565          (gnus-tmp-replied
3566           (cond (gnus-tmp-process gnus-process-mark)
3567                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3568                  gnus-cached-mark)
3569                 (gnus-tmp-replied gnus-replied-mark)
3570                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3571                  gnus-forwarded-mark)
3572                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3573                  gnus-saved-mark)
3574                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3575                  gnus-recent-mark)
3576                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3577                  gnus-unseen-mark)
3578                 (t gnus-no-mark)))
3579          (gnus-tmp-downloaded
3580           (cond (undownloaded
3581                  gnus-undownloaded-mark)
3582                 (gnus-newsgroup-agentized
3583                  gnus-downloaded-mark)
3584                 (t
3585                  gnus-no-mark)))
3586          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3587          (gnus-tmp-name
3588           (cond
3589            ((string-match "<[^>]+> *$" gnus-tmp-from)
3590             (let ((beg (match-beginning 0)))
3591               (or (and (string-match "^\".+\"" gnus-tmp-from)
3592                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3593                   (substring gnus-tmp-from 0 beg))))
3594            ((string-match "(.+)" gnus-tmp-from)
3595             (substring gnus-tmp-from
3596                        (1+ (match-beginning 0)) (1- (match-end 0))))
3597            (t gnus-tmp-from)))
3598          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3599          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3600          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3601          (buffer-read-only nil))
3602     (when (string= gnus-tmp-name "")
3603       (setq gnus-tmp-name gnus-tmp-from))
3604     (unless (numberp gnus-tmp-lines)
3605       (setq gnus-tmp-lines -1))
3606     (if (= gnus-tmp-lines -1)
3607         (setq gnus-tmp-lines "?")
3608       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3609       (gnus-put-text-property
3610      (point)
3611      (progn (eval gnus-summary-line-format-spec) (point))
3612        'gnus-number gnus-tmp-number)
3613     (when (gnus-visual-p 'summary-highlight 'highlight)
3614       (forward-line -1)
3615       (gnus-run-hooks 'gnus-summary-update-hook)
3616       (forward-line 1))))
3617
3618 (defun gnus-summary-update-line (&optional dont-update)
3619   "Update summary line after change."
3620   (when (and gnus-summary-default-score
3621              (not gnus-summary-inhibit-highlight))
3622     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3623            (article (gnus-summary-article-number))
3624            (score (gnus-summary-article-score article)))
3625       (unless dont-update
3626         (if (and gnus-summary-mark-below
3627                  (< (gnus-summary-article-score)
3628                     gnus-summary-mark-below))
3629             ;; This article has a low score, so we mark it as read.
3630             (when (memq article gnus-newsgroup-unreads)
3631               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3632           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3633             ;; This article was previously marked as read on account
3634             ;; of a low score, but now it has risen, so we mark it as
3635             ;; unread.
3636             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3637         (gnus-summary-update-mark
3638          (if (or (null gnus-summary-default-score)
3639                  (<= (abs (- score gnus-summary-default-score))
3640                      gnus-summary-zcore-fuzz))
3641              ?                          ;Whitespace
3642            (if (< score gnus-summary-default-score)
3643                gnus-score-below-mark gnus-score-over-mark))
3644          'score))
3645       ;; Do visual highlighting.
3646       (when (gnus-visual-p 'summary-highlight 'highlight)
3647         (gnus-run-hooks 'gnus-summary-update-hook)))))
3648
3649 (defvar gnus-tmp-new-adopts nil)
3650
3651 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3652   "Return the number of articles in THREAD.
3653 This may be 0 in some cases -- if none of the articles in
3654 the thread are to be displayed."
3655   (let* ((number
3656          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3657           (cond
3658            ((not (listp thread))
3659             1)
3660            ((and (consp thread) (cdr thread))
3661             (apply
3662              '+ 1 (mapcar
3663                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3664            ((null thread)
3665             1)
3666            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3667             1)
3668            (t 0))))
3669     (when (and level (zerop level) gnus-tmp-new-adopts)
3670       (incf number
3671             (apply '+ (mapcar
3672                        'gnus-summary-number-of-articles-in-thread
3673                        gnus-tmp-new-adopts))))
3674     (if char
3675         (if (> number 1) gnus-not-empty-thread-mark
3676           gnus-empty-thread-mark)
3677       number)))
3678
3679 (defsubst gnus-summary-line-message-size (head)
3680   "Return pretty-printed version of message size.
3681 This function is intended to be used in
3682 `gnus-summary-line-format-alist'."
3683   (let ((c (or (mail-header-chars head) -1)))
3684     (cond ((< c 0) "n/a")               ; chars not available
3685           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3686           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3687           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3688           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3689
3690
3691 (defun gnus-summary-set-local-parameters (group)
3692   "Go through the local params of GROUP and set all variable specs in that list."
3693   (let ((vars '(quit-config)))          ; Ignore quit-config.
3694     (dolist (elem (gnus-group-find-parameter group))
3695       (and (consp elem)                 ; Has to be a cons.
3696            (consp (cdr elem))           ; The cdr has to be a list.
3697            (symbolp (car elem))         ; Has to be a symbol in there.
3698            (not (memq (car elem) vars))
3699            (ignore-errors               ; So we set it.
3700              (push (car elem) vars)
3701              (make-local-variable (car elem))
3702              (set (car elem) (eval (nth 1 elem))))))))
3703
3704 (defun gnus-summary-read-group (group &optional show-all no-article
3705                                       kill-buffer no-display backward
3706                                       select-articles)
3707   "Start reading news in newsgroup GROUP.
3708 If SHOW-ALL is non-nil, already read articles are also listed.
3709 If NO-ARTICLE is non-nil, no article is selected initially.
3710 If NO-DISPLAY, don't generate a summary buffer."
3711   (let (result)
3712     (while (and group
3713                 (null (setq result
3714                             (let ((gnus-auto-select-next nil))
3715                               (or (gnus-summary-read-group-1
3716                                    group show-all no-article
3717                                    kill-buffer no-display
3718                                    select-articles)
3719                                   (setq show-all nil
3720                                         select-articles nil)))))
3721                 (eq gnus-auto-select-next 'quietly))
3722       (set-buffer gnus-group-buffer)
3723       ;; The entry function called above goes to the next
3724       ;; group automatically, so we go two groups back
3725       ;; if we are searching for the previous group.
3726       (when backward
3727         (gnus-group-prev-unread-group 2))
3728       (if (not (equal group (gnus-group-group-name)))
3729           (setq group (gnus-group-group-name))
3730         (setq group nil)))
3731     result))
3732
3733 (defun gnus-summary-read-group-1 (group show-all no-article
3734                                         kill-buffer no-display
3735                                         &optional select-articles)
3736   ;; Killed foreign groups can't be entered.
3737   ;;  (when (and (not (gnus-group-native-p group))
3738   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3739   ;;    (error "Dead non-native groups can't be entered"))
3740   (gnus-message 5 "Retrieving newsgroup: %s..."
3741                 (gnus-group-decoded-name group))
3742   (let* ((new-group (gnus-summary-setup-buffer group))
3743          (quit-config (gnus-group-quit-config group))
3744          (did-select (and new-group (gnus-select-newsgroup
3745                                      group show-all select-articles))))
3746     (cond
3747      ;; This summary buffer exists already, so we just select it.
3748      ((not new-group)
3749       (gnus-set-global-variables)
3750       (when kill-buffer
3751         (gnus-kill-or-deaden-summary kill-buffer))
3752       (gnus-configure-windows 'summary 'force)
3753       (gnus-set-mode-line 'summary)
3754       (gnus-summary-position-point)
3755       (message "")
3756       t)
3757      ;; We couldn't select this group.
3758      ((null did-select)
3759       (when (and (eq major-mode 'gnus-summary-mode)
3760                  (not (equal (current-buffer) kill-buffer)))
3761         (kill-buffer (current-buffer))
3762         (if (not quit-config)
3763             (progn
3764               ;; Update the info -- marks might need to be removed,
3765               ;; for instance.
3766               (gnus-summary-update-info)
3767               (set-buffer gnus-group-buffer)
3768               (gnus-group-jump-to-group group)
3769               (gnus-group-next-unread-group 1))
3770           (gnus-handle-ephemeral-exit quit-config)))
3771       (let ((grpinfo (gnus-get-info group)))
3772         (if (null (gnus-info-read grpinfo))
3773             (gnus-message 3 "Group %s contains no messages"
3774                           (gnus-group-decoded-name group))
3775           (gnus-message 3 "Can't select group")))
3776       nil)
3777      ;; The user did a `C-g' while prompting for number of articles,
3778      ;; so we exit this group.
3779      ((eq did-select 'quit)
3780       (and (eq major-mode 'gnus-summary-mode)
3781            (not (equal (current-buffer) kill-buffer))
3782            (kill-buffer (current-buffer)))
3783       (when kill-buffer
3784         (gnus-kill-or-deaden-summary kill-buffer))
3785       (if (not quit-config)
3786           (progn
3787             (set-buffer gnus-group-buffer)
3788             (gnus-group-jump-to-group group)
3789             (gnus-group-next-unread-group 1)
3790             (gnus-configure-windows 'group 'force))
3791         (gnus-handle-ephemeral-exit quit-config))
3792       ;; Finally signal the quit.
3793       (signal 'quit nil))
3794      ;; The group was successfully selected.
3795      (t
3796       (gnus-set-global-variables)
3797       ;; Save the active value in effect when the group was entered.
3798       (setq gnus-newsgroup-active
3799             (gnus-copy-sequence
3800              (gnus-active gnus-newsgroup-name)))
3801       ;; You can change the summary buffer in some way with this hook.
3802       (gnus-run-hooks 'gnus-select-group-hook)
3803       (when (memq 'summary (gnus-update-format-specifications
3804                             nil 'summary 'summary-mode 'summary-dummy))
3805         ;; The format specification for the summary line was updated,
3806         ;; so we need to update the mark positions as well.
3807         (gnus-update-summary-mark-positions))
3808       ;; Do score processing.
3809       (when gnus-use-scoring
3810         (gnus-possibly-score-headers))
3811       ;; Check whether to fill in the gaps in the threads.
3812       (when gnus-build-sparse-threads
3813         (gnus-build-sparse-threads))
3814       ;; Find the initial limit.
3815       (if show-all
3816           (let ((gnus-newsgroup-dormant nil))
3817             (gnus-summary-initial-limit show-all))
3818         (gnus-summary-initial-limit show-all))
3819       ;; Generate the summary buffer.
3820       (unless no-display
3821         (gnus-summary-prepare))
3822       (when gnus-use-trees
3823         (gnus-tree-open group)
3824         (setq gnus-summary-highlight-line-function
3825               'gnus-tree-highlight-article))
3826       ;; If the summary buffer is empty, but there are some low-scored
3827       ;; articles or some excluded dormants, we include these in the
3828       ;; buffer.
3829       (when (and (zerop (buffer-size))
3830                  (not no-display))
3831         (cond (gnus-newsgroup-dormant
3832                (gnus-summary-limit-include-dormant))
3833               ((and gnus-newsgroup-scored show-all)
3834                (gnus-summary-limit-include-expunged t))))
3835       ;; Function `gnus-apply-kill-file' must be called in this hook.
3836       (gnus-run-hooks 'gnus-apply-kill-hook)
3837       (if (and (zerop (buffer-size))
3838                (not no-display))
3839           (progn
3840             ;; This newsgroup is empty.
3841             (gnus-summary-catchup-and-exit nil t)
3842             (gnus-message 6 "No unread news")
3843             (when kill-buffer
3844               (gnus-kill-or-deaden-summary kill-buffer))
3845             ;; Return nil from this function.
3846             nil)
3847         ;; Hide conversation thread subtrees.  We cannot do this in
3848         ;; gnus-summary-prepare-hook since kill processing may not
3849         ;; work with hidden articles.
3850         (gnus-summary-maybe-hide-threads)
3851         (when kill-buffer
3852           (gnus-kill-or-deaden-summary kill-buffer))
3853         (gnus-summary-auto-select-subject)
3854         ;; Show first unread article if requested.
3855         (if (and (not no-article)
3856                  (not no-display)
3857                  gnus-newsgroup-unreads
3858                  gnus-auto-select-first)
3859             (progn
3860               (gnus-configure-windows 'summary)
3861               (let ((art (gnus-summary-article-number)))
3862                 (unless (and (not gnus-plugged)
3863                              (or (memq art gnus-newsgroup-undownloaded)
3864                                  (memq art gnus-newsgroup-downloadable)))
3865                   (gnus-summary-goto-article art))))
3866           ;; Don't select any articles.
3867           (gnus-summary-position-point)
3868           (gnus-configure-windows 'summary 'force)
3869           (gnus-set-mode-line 'summary))
3870         (when (and gnus-auto-center-group
3871                    (get-buffer-window gnus-group-buffer t))
3872           ;; Gotta use windows, because recenter does weird stuff if
3873           ;; the current buffer ain't the displayed window.
3874           (let ((owin (selected-window)))
3875             (select-window (get-buffer-window gnus-group-buffer t))
3876             (when (gnus-group-goto-group group)
3877               (recenter))
3878             (select-window owin)))
3879         ;; Mark this buffer as "prepared".
3880         (setq gnus-newsgroup-prepared t)
3881         (gnus-run-hooks 'gnus-summary-prepared-hook)
3882         (unless (gnus-ephemeral-group-p group)
3883           (gnus-group-update-group group))
3884         t)))))
3885
3886 (defun gnus-summary-auto-select-subject ()
3887   "Select the subject line on initial group entry."
3888   (goto-char (point-min))
3889   (cond
3890    ((eq gnus-auto-select-subject 'best)
3891     (gnus-summary-best-unread-subject))
3892    ((eq gnus-auto-select-subject 'unread)
3893     (gnus-summary-first-unread-subject))
3894    ((eq gnus-auto-select-subject 'unseen)
3895     (gnus-summary-first-unseen-subject))
3896    ((eq gnus-auto-select-subject 'unseen-or-unread)
3897     (gnus-summary-first-unseen-or-unread-subject))
3898    ((eq gnus-auto-select-subject 'first)
3899     ;; Do nothing.
3900     )
3901    ((functionp gnus-auto-select-subject)
3902     (funcall gnus-auto-select-subject))))
3903
3904 (defun gnus-summary-prepare ()
3905   "Generate the summary buffer."
3906   (interactive)
3907   (let ((buffer-read-only nil))
3908     (erase-buffer)
3909     (setq gnus-newsgroup-data nil
3910           gnus-newsgroup-data-reverse nil)
3911     (gnus-run-hooks 'gnus-summary-generate-hook)
3912     ;; Generate the buffer, either with threads or without.
3913     (when gnus-newsgroup-headers
3914       (gnus-summary-prepare-threads
3915        (if gnus-show-threads
3916            (gnus-sort-gathered-threads
3917             (funcall gnus-summary-thread-gathering-function
3918                      (gnus-sort-threads
3919                       (gnus-cut-threads (gnus-make-threads)))))
3920          ;; Unthreaded display.
3921          (gnus-sort-articles gnus-newsgroup-headers))))
3922     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3923     ;; Call hooks for modifying summary buffer.
3924     (goto-char (point-min))
3925     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3926
3927 (defsubst gnus-general-simplify-subject (subject)
3928   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3929   (setq subject
3930         (cond
3931          ;; Truncate the subject.
3932          (gnus-simplify-subject-functions
3933           (gnus-map-function gnus-simplify-subject-functions subject))
3934          ((numberp gnus-summary-gather-subject-limit)
3935           (setq subject (gnus-simplify-subject-re subject))
3936           (if (> (length subject) gnus-summary-gather-subject-limit)
3937               (substring subject 0 gnus-summary-gather-subject-limit)
3938             subject))
3939          ;; Fuzzily simplify it.
3940          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3941           (gnus-simplify-subject-fuzzy subject))
3942          ;; Just remove the leading "Re:".
3943          (t
3944           (gnus-simplify-subject-re subject))))
3945
3946   (if (and gnus-summary-gather-exclude-subject
3947            (string-match gnus-summary-gather-exclude-subject subject))
3948       nil                         ; This article shouldn't be gathered
3949     subject))
3950
3951 (defun gnus-summary-simplify-subject-query ()
3952   "Query where the respool algorithm would put this article."
3953   (interactive)
3954   (gnus-summary-select-article)
3955   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3956
3957 (defun gnus-gather-threads-by-subject (threads)
3958   "Gather threads by looking at Subject headers."
3959   (if (not gnus-summary-make-false-root)
3960       threads
3961     (let ((hashtb (gnus-make-hashtable 1024))
3962           (prev threads)
3963           (result threads)
3964           subject hthread whole-subject)
3965       (while threads
3966         (setq subject (gnus-general-simplify-subject
3967                        (setq whole-subject (mail-header-subject
3968                                             (caar threads)))))
3969         (when subject
3970           (if (setq hthread (gnus-gethash subject hashtb))
3971               (progn
3972                 ;; We enter a dummy root into the thread, if we
3973                 ;; haven't done that already.
3974                 (unless (stringp (caar hthread))
3975                   (setcar hthread (list whole-subject (car hthread))))
3976                 ;; We add this new gathered thread to this gathered
3977                 ;; thread.
3978                 (setcdr (car hthread)
3979                         (nconc (cdar hthread) (list (car threads))))
3980                 ;; Remove it from the list of threads.
3981                 (setcdr prev (cdr threads))
3982                 (setq threads prev))
3983             ;; Enter this thread into the hash table.
3984             (gnus-sethash subject
3985                           (if gnus-summary-make-false-root-always
3986                               (progn
3987                                 ;; If you want a dummy root above all
3988                                 ;; threads...
3989                                 (setcar threads (list whole-subject
3990                                                       (car threads)))
3991                                 threads)
3992                             threads)
3993                           hashtb)))
3994         (setq prev threads)
3995         (setq threads (cdr threads)))
3996       result)))
3997
3998 (defun gnus-gather-threads-by-references (threads)
3999   "Gather threads by looking at References headers."
4000   (let ((idhashtb (gnus-make-hashtable 1024))
4001         (thhashtb (gnus-make-hashtable 1024))
4002         (prev threads)
4003         (result threads)
4004         ids references id gthread gid entered ref)
4005     (while threads
4006       (when (setq references (mail-header-references (caar threads)))
4007         (setq id (mail-header-id (caar threads))
4008               ids (inline (gnus-split-references references))
4009               entered nil)
4010         (while (setq ref (pop ids))
4011           (setq ids (delete ref ids))
4012           (if (not (setq gid (gnus-gethash ref idhashtb)))
4013               (progn
4014                 (gnus-sethash ref id idhashtb)
4015                 (gnus-sethash id threads thhashtb))
4016             (setq gthread (gnus-gethash gid thhashtb))
4017             (unless entered
4018               ;; We enter a dummy root into the thread, if we
4019               ;; haven't done that already.
4020               (unless (stringp (caar gthread))
4021                 (setcar gthread (list (mail-header-subject (caar gthread))
4022                                       (car gthread))))
4023               ;; We add this new gathered thread to this gathered
4024               ;; thread.
4025               (setcdr (car gthread)
4026                       (nconc (cdar gthread) (list (car threads)))))
4027             ;; Add it into the thread hash table.
4028             (gnus-sethash id gthread thhashtb)
4029             (setq entered t)
4030             ;; Remove it from the list of threads.
4031             (setcdr prev (cdr threads))
4032             (setq threads prev))))
4033       (setq prev threads)
4034       (setq threads (cdr threads)))
4035     result))
4036
4037 (defun gnus-sort-gathered-threads (threads)
4038   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4039   (let ((result threads))
4040     (while threads
4041       (when (stringp (caar threads))
4042         (setcdr (car threads)
4043                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4044       (setq threads (cdr threads)))
4045     result))
4046
4047 (defun gnus-thread-loop-p (root thread)
4048   "Say whether ROOT is in THREAD."
4049   (let ((stack (list thread))
4050         (infloop 0)
4051         th)
4052     (while (setq thread (pop stack))
4053       (setq th (cdr thread))
4054       (while (and th
4055                   (not (eq (caar th) root)))
4056         (pop th))
4057       (if th
4058           ;; We have found a loop.
4059           (let (ref-dep)
4060             (setcdr thread (delq (car th) (cdr thread)))
4061             (if (boundp (setq ref-dep (intern "none"
4062                                               gnus-newsgroup-dependencies)))
4063                 (setcdr (symbol-value ref-dep)
4064                         (nconc (cdr (symbol-value ref-dep))
4065                                (list (car th))))
4066               (set ref-dep (list nil (car th))))
4067             (setq infloop 1
4068                   stack nil))
4069         ;; Push all the subthreads onto the stack.
4070         (push (cdr thread) stack)))
4071     infloop))
4072
4073 (defun gnus-make-threads ()
4074   "Go through the dependency hashtb and find the roots.  Return all threads."
4075   (let (threads)
4076     (while (catch 'infloop
4077              (mapatoms
4078               (lambda (refs)
4079                 ;; Deal with self-referencing References loops.
4080                 (when (and (car (symbol-value refs))
4081                            (not (zerop
4082                                  (apply
4083                                   '+
4084                                   (mapcar
4085                                    (lambda (thread)
4086                                      (gnus-thread-loop-p
4087                                       (car (symbol-value refs)) thread))
4088                                    (cdr (symbol-value refs)))))))
4089                   (setq threads nil)
4090                   (throw 'infloop t))
4091                 (unless (car (symbol-value refs))
4092                   ;; These threads do not refer back to any other
4093                   ;; articles, so they're roots.
4094                   (setq threads (append (cdr (symbol-value refs)) threads))))
4095               gnus-newsgroup-dependencies)))
4096     threads))
4097
4098 ;; Build the thread tree.
4099 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4100   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4101
4102 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4103 if it was already present.
4104
4105 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4106 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4107 Message-IDs will be renamed to a unique Message-ID before being
4108 entered.
4109
4110 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4111   (let* ((id (mail-header-id header))
4112          (id-dep (and id (intern id dependencies)))
4113          parent-id ref ref-dep ref-header replaced)
4114     ;; Enter this `header' in the `dependencies' table.
4115     (cond
4116      ((not id-dep)
4117       (setq header nil))
4118      ;; The first two cases do the normal part: enter a new `header'
4119      ;; in the `dependencies' table.
4120      ((not (boundp id-dep))
4121       (set id-dep (list header)))
4122      ((null (car (symbol-value id-dep)))
4123       (setcar (symbol-value id-dep) header))
4124
4125      ;; From here the `header' was already present in the
4126      ;; `dependencies' table.
4127      (force-new
4128       ;; Overrides an existing entry;
4129       ;; just set the header part of the entry.
4130       (setcar (symbol-value id-dep) header)
4131       (setq replaced t))
4132
4133      ;; Renames the existing `header' to a unique Message-ID.
4134      ((not gnus-summary-ignore-duplicates)
4135       ;; An article with this Message-ID has already been seen.
4136       ;; We rename the Message-ID.
4137       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4138            (list header))
4139       (mail-header-set-id header id))
4140
4141      ;; The last case ignores an existing entry, except it adds any
4142      ;; additional Xrefs (in case the two articles came from different
4143      ;; servers.
4144      ;; Also sets `header' to `nil' meaning that the `dependencies'
4145      ;; table was *not* modified.
4146      (t
4147       (mail-header-set-xref
4148        (car (symbol-value id-dep))
4149        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4150                    "")
4151                (or (mail-header-xref header) "")))
4152       (setq header nil)))
4153
4154     (when (and header (not replaced))
4155       ;; First check that we are not creating a References loop.
4156       (setq parent-id (gnus-parent-id (mail-header-references header)))
4157       (setq ref parent-id)
4158       (while (and ref
4159                   (setq ref-dep (intern-soft ref dependencies))
4160                   (boundp ref-dep)
4161                   (setq ref-header (car (symbol-value ref-dep))))
4162         (if (string= id ref)
4163             ;; Yuk!  This is a reference loop.  Make the article be a
4164             ;; root article.
4165             (progn
4166               (mail-header-set-references (car (symbol-value id-dep)) "none")
4167               (setq ref nil)
4168               (setq parent-id nil))
4169           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4170       (setq ref-dep (intern (or parent-id "none") dependencies))
4171       (if (boundp ref-dep)
4172           (setcdr (symbol-value ref-dep)
4173                   (nconc (cdr (symbol-value ref-dep))
4174                          (list (symbol-value id-dep))))
4175         (set ref-dep (list nil (symbol-value id-dep)))))
4176     header))
4177
4178 (defun gnus-extract-message-id-from-in-reply-to (string)
4179   (if (string-match "<[^>]+>" string)
4180       (substring string (match-beginning 0) (match-end 0))
4181     nil))
4182
4183 (defun gnus-build-sparse-threads ()
4184   (let ((headers gnus-newsgroup-headers)
4185         (mail-parse-charset gnus-newsgroup-charset)
4186         (gnus-summary-ignore-duplicates t)
4187         header references generation relations
4188         subject child end new-child date)
4189     ;; First we create an alist of generations/relations, where
4190     ;; generations is how much we trust the relation, and the relation
4191     ;; is parent/child.
4192     (gnus-message 7 "Making sparse threads...")
4193     (save-excursion
4194       (nnheader-set-temp-buffer " *gnus sparse threads*")
4195       (while (setq header (pop headers))
4196         (when (and (setq references (mail-header-references header))
4197                    (not (string= references "")))
4198           (insert references)
4199           (setq child (mail-header-id header)
4200                 subject (mail-header-subject header)
4201                 date (mail-header-date header)
4202                 generation 0)
4203           (while (search-backward ">" nil t)
4204             (setq end (1+ (point)))
4205             (when (search-backward "<" nil t)
4206               (setq new-child (buffer-substring (point) end))
4207               (push (list (incf generation)
4208                           child (setq child new-child)
4209                           subject date)
4210                     relations)))
4211           (when child
4212             (push (list (1+ generation) child nil subject) relations))
4213           (erase-buffer)))
4214       (kill-buffer (current-buffer)))
4215     ;; Sort over trustworthiness.
4216     (mapcar
4217      (lambda (relation)
4218        (when (gnus-dependencies-add-header
4219               (make-full-mail-header
4220                gnus-reffed-article-number
4221                (nth 3 relation) "" (or (nth 4 relation) "")
4222                (nth 1 relation)
4223                (or (nth 2 relation) "") 0 0 "")
4224               gnus-newsgroup-dependencies nil)
4225          (push gnus-reffed-article-number gnus-newsgroup-limit)
4226          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4227          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4228                gnus-newsgroup-reads)
4229          (decf gnus-reffed-article-number)))
4230      (sort relations 'car-less-than-car))
4231     (gnus-message 7 "Making sparse threads...done")))
4232
4233 (defun gnus-build-old-threads ()
4234   ;; Look at all the articles that refer back to old articles, and
4235   ;; fetch the headers for the articles that aren't there.  This will
4236   ;; build complete threads - if the roots haven't been expired by the
4237   ;; server, that is.
4238   (let ((mail-parse-charset gnus-newsgroup-charset)
4239         id heads)
4240     (mapatoms
4241      (lambda (refs)
4242        (when (not (car (symbol-value refs)))
4243          (setq heads (cdr (symbol-value refs)))
4244          (while heads
4245            (if (memq (mail-header-number (caar heads))
4246                      gnus-newsgroup-dormant)
4247                (setq heads (cdr heads))
4248              (setq id (symbol-name refs))
4249              (while (and (setq id (gnus-build-get-header id))
4250                          (not (car (gnus-id-to-thread id)))))
4251              (setq heads nil)))))
4252      gnus-newsgroup-dependencies)))
4253
4254 (defsubst gnus-remove-odd-characters (string)
4255   "Translate STRING into something that doesn't contain weird characters."
4256   (mm-subst-char-in-string
4257    ?\r ?\-
4258    (mm-subst-char-in-string ?\n ?\- string t) t))
4259
4260 ;; This function has to be called with point after the article number
4261 ;; on the beginning of the line.
4262 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4263   (let ((eol (point-at-eol))
4264         (buffer (current-buffer))
4265         header references in-reply-to)
4266
4267     ;; overview: [num subject from date id refs chars lines misc]
4268     (unwind-protect
4269         (let (x)
4270           (narrow-to-region (point) eol)
4271           (unless (eobp)
4272             (forward-char))
4273
4274           (setq header
4275                 (make-full-mail-header
4276                  number                 ; number
4277                  (condition-case ()     ; subject
4278                      (gnus-remove-odd-characters
4279                       (funcall gnus-decode-encoded-word-function
4280                                (setq x (nnheader-nov-field))))
4281                    (error x))
4282                  (condition-case ()     ; from
4283                      (gnus-remove-odd-characters
4284                       (funcall gnus-decode-encoded-word-function
4285                                (setq x (nnheader-nov-field))))
4286                    (error x))
4287                  (nnheader-nov-field)   ; date
4288                  (nnheader-nov-read-message-id number)  ; id
4289                  (setq references (nnheader-nov-field)) ; refs
4290                  (nnheader-nov-read-integer) ; chars
4291                  (nnheader-nov-read-integer) ; lines
4292                  (unless (eobp)
4293                    (if (looking-at "Xref: ")
4294                        (goto-char (match-end 0)))
4295                    (nnheader-nov-field)) ; Xref
4296                  (nnheader-nov-parse-extra)))) ; extra
4297
4298       (widen))
4299
4300     (when (and (string= references "")
4301                (setq in-reply-to (mail-header-extra header))
4302                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4303       (mail-header-set-references
4304        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4305
4306     (when gnus-alter-header-function
4307       (funcall gnus-alter-header-function header))
4308     (gnus-dependencies-add-header header dependencies force-new)))
4309
4310 (defun gnus-build-get-header (id)
4311   "Look through the buffer of NOV lines and find the header to ID.
4312 Enter this line into the dependencies hash table, and return
4313 the id of the parent article (if any)."
4314   (let ((deps gnus-newsgroup-dependencies)
4315         found header)
4316     (prog1
4317         (save-excursion
4318           (set-buffer nntp-server-buffer)
4319           (let ((case-fold-search nil))
4320             (goto-char (point-min))
4321             (while (and (not found)
4322                         (search-forward id nil t))
4323               (beginning-of-line)
4324               (setq found (looking-at
4325                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4326                                    (regexp-quote id))))
4327               (or found (beginning-of-line 2)))
4328             (when found
4329               (beginning-of-line)
4330               (and
4331                (setq header (gnus-nov-parse-line
4332                              (read (current-buffer)) deps))
4333                (gnus-parent-id (mail-header-references header))))))
4334       (when header
4335         (let ((number (mail-header-number header)))
4336           (push number gnus-newsgroup-limit)
4337           (push header gnus-newsgroup-headers)
4338           (if (memq number gnus-newsgroup-unselected)
4339               (progn
4340                 (setq gnus-newsgroup-unreads
4341                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4342                                                number))
4343                 (setq gnus-newsgroup-unselected
4344                       (delq number gnus-newsgroup-unselected)))
4345             (push number gnus-newsgroup-ancient)))))))
4346
4347 (defun gnus-build-all-threads ()
4348   "Read all the headers."
4349   (let ((gnus-summary-ignore-duplicates t)
4350         (mail-parse-charset gnus-newsgroup-charset)
4351         (dependencies gnus-newsgroup-dependencies)
4352         header article)
4353     (save-excursion
4354       (set-buffer nntp-server-buffer)
4355       (let ((case-fold-search nil))
4356         (goto-char (point-min))
4357         (while (not (eobp))
4358           (ignore-errors
4359             (setq article (read (current-buffer))
4360                   header (gnus-nov-parse-line article dependencies)))
4361           (when header
4362             (with-current-buffer gnus-summary-buffer
4363               (push header gnus-newsgroup-headers)
4364               (if (memq (setq article (mail-header-number header))
4365                         gnus-newsgroup-unselected)
4366                   (progn
4367                     (setq gnus-newsgroup-unreads
4368                           (gnus-add-to-sorted-list
4369                            gnus-newsgroup-unreads article))
4370                     (setq gnus-newsgroup-unselected
4371                           (delq article gnus-newsgroup-unselected)))
4372                 (push article gnus-newsgroup-ancient)))
4373             (forward-line 1)))))))
4374
4375 (defun gnus-summary-update-article-line (article header)
4376   "Update the line for ARTICLE using HEADER."
4377   (let* ((id (mail-header-id header))
4378          (thread (gnus-id-to-thread id)))
4379     (unless thread
4380       (error "Article in no thread"))
4381     ;; Update the thread.
4382     (setcar thread header)
4383     (gnus-summary-goto-subject article)
4384     (let* ((datal (gnus-data-find-list article))
4385            (data (car datal))
4386            (buffer-read-only nil)
4387            (level (gnus-summary-thread-level)))
4388       (gnus-delete-line)
4389       (let ((inserted (- (point)
4390                          (progn
4391                            (gnus-summary-insert-line
4392                             header level nil
4393                             (memq article gnus-newsgroup-undownloaded)
4394                             (gnus-article-mark article)
4395                             (memq article gnus-newsgroup-replied)
4396                             (memq article gnus-newsgroup-expirable)
4397                             ;; Only insert the Subject string when it's different
4398                             ;; from the previous Subject string.
4399                             (if (and
4400                                  gnus-show-threads
4401                                  (gnus-subject-equal
4402                                   (condition-case ()
4403                                       (mail-header-subject
4404                                        (gnus-data-header
4405                                         (cadr
4406                                          (gnus-data-find-list
4407                                           article
4408                                           (gnus-data-list t)))))
4409                                     ;; Error on the side of excessive subjects.
4410                                     (error ""))
4411                                   (mail-header-subject header)))
4412                                 ""
4413                               (mail-header-subject header))
4414                             nil (cdr (assq article gnus-newsgroup-scored))
4415                             (memq article gnus-newsgroup-processable))
4416                            (point)))))
4417         (when (cdr datal)
4418           (gnus-data-update-list
4419            (cdr datal)
4420            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4421
4422 (defun gnus-summary-update-article (article &optional iheader)
4423   "Update ARTICLE in the summary buffer."
4424   (set-buffer gnus-summary-buffer)
4425   (let* ((header (gnus-summary-article-header article))
4426          (id (mail-header-id header))
4427          (data (gnus-data-find article))
4428          (thread (gnus-id-to-thread id))
4429          (references (mail-header-references header))
4430          (parent
4431           (gnus-id-to-thread
4432            (or (gnus-parent-id
4433                 (when (and references
4434                            (not (equal "" references)))
4435                   references))
4436                "none")))
4437          (buffer-read-only nil)
4438          (old (car thread)))
4439     (when thread
4440       (unless iheader
4441         (setcar thread nil)
4442         (when parent
4443           (delq thread parent)))
4444       (if (gnus-summary-insert-subject id header)
4445           ;; Set the (possibly) new article number in the data structure.
4446           (gnus-data-set-number data (gnus-id-to-article id))
4447         (setcar thread old)
4448         nil))))
4449
4450 (defun gnus-rebuild-thread (id &optional line)
4451   "Rebuild the thread containing ID.
4452 If LINE, insert the rebuilt thread starting on line LINE."
4453   (let ((buffer-read-only nil)
4454         old-pos current thread data)
4455     (if (not gnus-show-threads)
4456         (setq thread (list (car (gnus-id-to-thread id))))
4457       ;; Get the thread this article is part of.
4458       (setq thread (gnus-remove-thread id)))
4459     (setq old-pos (point-at-bol))
4460     (setq current (save-excursion
4461                     (and (re-search-backward "[\r\n]" nil t)
4462                          (gnus-summary-article-number))))
4463     ;; If this is a gathered thread, we have to go some re-gathering.
4464     (when (stringp (car thread))
4465       (let ((subject (car thread))
4466             roots thr)
4467         (setq thread (cdr thread))
4468         (while thread
4469           (unless (memq (setq thr (gnus-id-to-thread
4470                                    (gnus-root-id
4471                                     (mail-header-id (caar thread)))))
4472                         roots)
4473             (push thr roots))
4474           (setq thread (cdr thread)))
4475         ;; We now have all (unique) roots.
4476         (if (= (length roots) 1)
4477             ;; All the loose roots are now one solid root.
4478             (setq thread (car roots))
4479           (setq thread (cons subject (gnus-sort-threads roots))))))
4480     (let (threads)
4481       ;; We then insert this thread into the summary buffer.
4482       (when line
4483         (goto-char (point-min))
4484         (forward-line (1- line)))
4485       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4486         (if gnus-show-threads
4487             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4488           (gnus-summary-prepare-unthreaded thread))
4489         (setq data (nreverse gnus-newsgroup-data))
4490         (setq threads gnus-newsgroup-threads))
4491       ;; We splice the new data into the data structure.
4492       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4493       ;;!!! then we want to insert at the beginning of the buffer.
4494       ;;!!! That happens to be true with Gnus now, but that may
4495       ;;!!! change in the future.  Perhaps.
4496       (gnus-data-enter-list
4497        (if line nil current) data (- (point) old-pos))
4498       (setq gnus-newsgroup-threads
4499             (nconc threads gnus-newsgroup-threads))
4500       (gnus-data-compute-positions))))
4501
4502 (defun gnus-number-to-header (number)
4503   "Return the header for article NUMBER."
4504   (let ((headers gnus-newsgroup-headers))
4505     (while (and headers
4506                 (not (= number (mail-header-number (car headers)))))
4507       (pop headers))
4508     (when headers
4509       (car headers))))
4510
4511 (defun gnus-parent-headers (in-headers &optional generation)
4512   "Return the headers of the GENERATIONeth parent of HEADERS."
4513   (unless generation
4514     (setq generation 1))
4515   (let ((parent t)
4516         (headers in-headers)
4517         references)
4518     (while (and parent
4519                 (not (zerop generation))
4520                 (setq references (mail-header-references headers)))
4521       (setq headers (if (and references
4522                              (setq parent (gnus-parent-id references)))
4523                         (car (gnus-id-to-thread parent))
4524                       nil))
4525       (decf generation))
4526     (and (not (eq headers in-headers))
4527          headers)))
4528
4529 (defun gnus-id-to-thread (id)
4530   "Return the (sub-)thread where ID appears."
4531   (gnus-gethash id gnus-newsgroup-dependencies))
4532
4533 (defun gnus-id-to-article (id)
4534   "Return the article number of ID."
4535   (let ((thread (gnus-id-to-thread id)))
4536     (when (and thread
4537                (car thread))
4538       (mail-header-number (car thread)))))
4539
4540 (defun gnus-id-to-header (id)
4541   "Return the article headers of ID."
4542   (car (gnus-id-to-thread id)))
4543
4544 (defun gnus-article-displayed-root-p (article)
4545   "Say whether ARTICLE is a root(ish) article."
4546   (let ((level (gnus-summary-thread-level article))
4547         (refs (mail-header-references  (gnus-summary-article-header article)))
4548         particle)
4549     (cond
4550      ((null level) nil)
4551      ((zerop level) t)
4552      ((null refs) t)
4553      ((null (gnus-parent-id refs)) t)
4554      ((and (= 1 level)
4555            (null (setq particle (gnus-id-to-article
4556                                  (gnus-parent-id refs))))
4557            (null (gnus-summary-thread-level particle)))))))
4558
4559 (defun gnus-root-id (id)
4560   "Return the id of the root of the thread where ID appears."
4561   (let (last-id prev)
4562     (while (and id (setq prev (car (gnus-id-to-thread id))))
4563       (setq last-id id
4564             id (gnus-parent-id (mail-header-references prev))))
4565     last-id))
4566
4567 (defun gnus-articles-in-thread (thread)
4568   "Return the list of articles in THREAD."
4569   (cons (mail-header-number (car thread))
4570         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4571
4572 (defun gnus-remove-thread (id &optional dont-remove)
4573   "Remove the thread that has ID in it."
4574   (let (headers thread last-id)
4575     ;; First go up in this thread until we find the root.
4576     (setq last-id (gnus-root-id id)
4577           headers (message-flatten-list (gnus-id-to-thread last-id)))
4578     ;; We have now found the real root of this thread.  It might have
4579     ;; been gathered into some loose thread, so we have to search
4580     ;; through the threads to find the thread we wanted.
4581     (let ((threads gnus-newsgroup-threads)
4582           sub)
4583       (while threads
4584         (setq sub (car threads))
4585         (if (stringp (car sub))
4586             ;; This is a gathered thread, so we look at the roots
4587             ;; below it to find whether this article is in this
4588             ;; gathered root.
4589             (progn
4590               (setq sub (cdr sub))
4591               (while sub
4592                 (when (member (caar sub) headers)
4593                   (setq thread (car threads)
4594                         threads nil
4595                         sub nil))
4596                 (setq sub (cdr sub))))
4597           ;; It's an ordinary thread, so we check it.
4598           (when (eq (car sub) (car headers))
4599             (setq thread sub
4600                   threads nil)))
4601         (setq threads (cdr threads)))
4602       ;; If this article is in no thread, then it's a root.
4603       (if thread
4604           (unless dont-remove
4605             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4606         (setq thread (gnus-id-to-thread last-id)))
4607       (when thread
4608         (prog1
4609             thread                      ; We return this thread.
4610           (unless dont-remove
4611             (if (stringp (car thread))
4612                 (progn
4613                   ;; If we use dummy roots, then we have to remove the
4614                   ;; dummy root as well.
4615                   (when (eq gnus-summary-make-false-root 'dummy)
4616                     ;; We go to the dummy root by going to
4617                     ;; the first sub-"thread", and then one line up.
4618                     (gnus-summary-goto-article
4619                      (mail-header-number (caadr thread)))
4620                     (forward-line -1)
4621                     (gnus-delete-line)
4622                     (gnus-data-compute-positions))
4623                   (setq thread (cdr thread))
4624                   (while thread
4625                     (gnus-remove-thread-1 (car thread))
4626                     (setq thread (cdr thread))))
4627               (gnus-remove-thread-1 thread))))))))
4628
4629 (defun gnus-remove-thread-1 (thread)
4630   "Remove the thread THREAD recursively."
4631   (let ((number (mail-header-number (pop thread)))
4632         d)
4633     (setq thread (reverse thread))
4634     (while thread
4635       (gnus-remove-thread-1 (pop thread)))
4636     (when (setq d (gnus-data-find number))
4637       (goto-char (gnus-data-pos d))
4638       (gnus-summary-show-thread)
4639       (gnus-data-remove
4640        number
4641        (- (point-at-bol)
4642           (prog1
4643               (1+ (point-at-eol))
4644             (gnus-delete-line)))))))
4645
4646 (defun gnus-sort-threads-1 (threads func)
4647   (sort (mapcar (lambda (thread)
4648                   (cons (car thread)
4649                         (and (cdr thread)
4650                              (gnus-sort-threads-1 (cdr thread) func))))
4651                 threads) func))
4652
4653 (defun gnus-sort-threads (threads)
4654   "Sort THREADS."
4655   (if (not gnus-thread-sort-functions)
4656       threads
4657     (gnus-message 8 "Sorting threads...")
4658     (let ((max-lisp-eval-depth 5000))
4659       (prog1 (gnus-sort-threads-1
4660          threads
4661          (gnus-make-sort-function gnus-thread-sort-functions))
4662         (gnus-message 8 "Sorting threads...done")))))
4663
4664 (defun gnus-sort-articles (articles)
4665   "Sort ARTICLES."
4666   (when gnus-article-sort-functions
4667     (gnus-message 7 "Sorting articles...")
4668     (prog1
4669         (setq gnus-newsgroup-headers
4670               (sort articles (gnus-make-sort-function
4671                               gnus-article-sort-functions)))
4672       (gnus-message 7 "Sorting articles...done"))))
4673
4674 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4675 (defmacro gnus-thread-header (thread)
4676   "Return header of first article in THREAD.
4677 Note that THREAD must never, ever be anything else than a variable -
4678 using some other form will lead to serious barfage."
4679   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4680   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4681   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4682         (vector thread) 2))
4683
4684 (defsubst gnus-article-sort-by-number (h1 h2)
4685   "Sort articles by article number."
4686   (< (mail-header-number h1)
4687      (mail-header-number h2)))
4688
4689 (defun gnus-thread-sort-by-number (h1 h2)
4690   "Sort threads by root article number."
4691   (gnus-article-sort-by-number
4692    (gnus-thread-header h1) (gnus-thread-header h2)))
4693
4694 (defsubst gnus-article-sort-by-random (h1 h2)
4695   "Sort articles by article number."
4696   (zerop (random 2)))
4697
4698 (defun gnus-thread-sort-by-random (h1 h2)
4699   "Sort threads by root article number."
4700   (gnus-article-sort-by-random
4701    (gnus-thread-header h1) (gnus-thread-header h2)))
4702
4703 (defsubst gnus-article-sort-by-lines (h1 h2)
4704   "Sort articles by article Lines header."
4705   (< (mail-header-lines h1)
4706      (mail-header-lines h2)))
4707
4708 (defun gnus-thread-sort-by-lines (h1 h2)
4709   "Sort threads by root article Lines header."
4710   (gnus-article-sort-by-lines
4711    (gnus-thread-header h1) (gnus-thread-header h2)))
4712
4713 (defsubst gnus-article-sort-by-chars (h1 h2)
4714   "Sort articles by octet length."
4715   (< (mail-header-chars h1)
4716      (mail-header-chars h2)))
4717
4718 (defun gnus-thread-sort-by-chars (h1 h2)
4719   "Sort threads by root article octet length."
4720   (gnus-article-sort-by-chars
4721    (gnus-thread-header h1) (gnus-thread-header h2)))
4722
4723 (defsubst gnus-article-sort-by-author (h1 h2)
4724   "Sort articles by root author."
4725   (string-lessp
4726    (let ((extract (funcall
4727                    gnus-extract-address-components
4728                    (mail-header-from h1))))
4729      (or (car extract) (cadr extract) ""))
4730    (let ((extract (funcall
4731                    gnus-extract-address-components
4732                    (mail-header-from h2))))
4733      (or (car extract) (cadr extract) ""))))
4734
4735 (defun gnus-thread-sort-by-author (h1 h2)
4736   "Sort threads by root author."
4737   (gnus-article-sort-by-author
4738    (gnus-thread-header h1)  (gnus-thread-header h2)))
4739
4740 (defsubst gnus-article-sort-by-recipient (h1 h2)
4741   "Sort articles by recipient."
4742   (string-lessp
4743    (let ((extract (funcall
4744                    gnus-extract-address-components
4745                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4746      (or (car extract) (cadr extract)))
4747    (let ((extract (funcall
4748                    gnus-extract-address-components
4749                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4750      (or (car extract) (cadr extract)))))
4751
4752 (defun gnus-thread-sort-by-recipient (h1 h2)
4753   "Sort threads by root recipient."
4754   (gnus-article-sort-by-recipient
4755    (gnus-thread-header h1) (gnus-thread-header h2)))
4756
4757 (defsubst gnus-article-sort-by-subject (h1 h2)
4758   "Sort articles by root subject."
4759   (string-lessp
4760    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4761    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4762
4763 (defun gnus-thread-sort-by-subject (h1 h2)
4764   "Sort threads by root subject."
4765   (gnus-article-sort-by-subject
4766    (gnus-thread-header h1) (gnus-thread-header h2)))
4767
4768 (defsubst gnus-article-sort-by-date (h1 h2)
4769   "Sort articles by root article date."
4770   (time-less-p
4771    (gnus-date-get-time (mail-header-date h1))
4772    (gnus-date-get-time (mail-header-date h2))))
4773
4774 (defun gnus-thread-sort-by-date (h1 h2)
4775   "Sort threads by root article date."
4776   (gnus-article-sort-by-date
4777    (gnus-thread-header h1) (gnus-thread-header h2)))
4778
4779 (defsubst gnus-article-sort-by-score (h1 h2)
4780   "Sort articles by root article score.
4781 Unscored articles will be counted as having a score of zero."
4782   (> (or (cdr (assq (mail-header-number h1)
4783                     gnus-newsgroup-scored))
4784          gnus-summary-default-score 0)
4785      (or (cdr (assq (mail-header-number h2)
4786                     gnus-newsgroup-scored))
4787          gnus-summary-default-score 0)))
4788
4789 (defun gnus-thread-sort-by-score (h1 h2)
4790   "Sort threads by root article score."
4791   (gnus-article-sort-by-score
4792    (gnus-thread-header h1) (gnus-thread-header h2)))
4793
4794 (defun gnus-thread-sort-by-total-score (h1 h2)
4795   "Sort threads by the sum of all scores in the thread.
4796 Unscored articles will be counted as having a score of zero."
4797   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4798
4799 (defun gnus-thread-total-score (thread)
4800   ;; This function find the total score of THREAD.
4801   (cond
4802    ((null thread)
4803     0)
4804    ((consp thread)
4805     (if (stringp (car thread))
4806         (apply gnus-thread-score-function 0
4807                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4808       (gnus-thread-total-score-1 thread)))
4809    (t
4810     (gnus-thread-total-score-1 (list thread)))))
4811
4812 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4813   "Sort threads such that the thread with the most recently arrived article comes first."
4814   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4815
4816 (defun gnus-thread-highest-number (thread)
4817   "Return the highest article number in THREAD."
4818   (apply 'max (mapcar (lambda (header)
4819                         (mail-header-number header))
4820                       (message-flatten-list thread))))
4821
4822 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4823   "Sort threads such that the thread with the most recently dated article comes first."
4824   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4825
4826 (defun gnus-thread-latest-date (thread)
4827   "Return the highest article date in THREAD."
4828   (let ((previous-time 0))
4829     (apply 'max
4830            (mapcar
4831             (lambda (header)
4832               (setq previous-time
4833                     (condition-case ()
4834                         (time-to-seconds (mail-header-parse-date
4835                                           (mail-header-date header)))
4836                       (error previous-time))))
4837             (sort
4838              (message-flatten-list thread)
4839              (lambda (h1 h2)
4840                (< (mail-header-number h1)
4841                   (mail-header-number h2))))))))
4842
4843 (defun gnus-thread-total-score-1 (root)
4844   ;; This function find the total score of the thread below ROOT.
4845   (setq root (car root))
4846   (apply gnus-thread-score-function
4847          (or (append
4848               (mapcar 'gnus-thread-total-score
4849                       (cdr (gnus-id-to-thread (mail-header-id root))))
4850               (when (> (mail-header-number root) 0)
4851                 (list (or (cdr (assq (mail-header-number root)
4852                                      gnus-newsgroup-scored))
4853                           gnus-summary-default-score 0))))
4854              (list gnus-summary-default-score)
4855              '(0))))
4856
4857 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4858 (defvar gnus-tmp-prev-subject nil)
4859 (defvar gnus-tmp-false-parent nil)
4860 (defvar gnus-tmp-root-expunged nil)
4861 (defvar gnus-tmp-dummy-line nil)
4862
4863 (eval-when-compile (defvar gnus-tmp-header))
4864 (defun gnus-extra-header (type &optional header)
4865   "Return the extra header of TYPE."
4866   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4867       ""))
4868
4869 (defvar gnus-tmp-thread-tree-header-string "")
4870
4871 (defcustom gnus-sum-thread-tree-root "> "
4872   "With %B spec, used for the root of a thread.
4873 If nil, use subject instead."
4874   :version "22.1"
4875   :type '(radio (const :format "%v  " nil) string)
4876   :group 'gnus-thread)
4877
4878 (defcustom gnus-sum-thread-tree-false-root "> "
4879   "With %B spec, used for a false root of a thread.
4880 If nil, use subject instead."
4881   :version "22.1"
4882   :type '(radio (const :format "%v  " nil) string)
4883   :group 'gnus-thread)
4884
4885 (defcustom gnus-sum-thread-tree-single-indent ""
4886   "With %B spec, used for a thread with just one message.
4887 If nil, use subject instead."
4888   :version "22.1"
4889   :type '(radio (const :format "%v  " nil) string)
4890   :group 'gnus-thread)
4891
4892 (defcustom gnus-sum-thread-tree-vertical "| "
4893   "With %B spec, used for drawing a vertical line."
4894   :version "22.1"
4895   :type 'string
4896   :group 'gnus-thread)
4897
4898 (defcustom gnus-sum-thread-tree-indent "  "
4899   "With %B spec, used for indenting."
4900   :version "22.1"
4901   :type 'string
4902   :group 'gnus-thread)
4903
4904 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4905   "With %B spec, used for a leaf with brothers."
4906   :version "22.1"
4907   :type 'string
4908   :group 'gnus-thread)
4909
4910 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4911   "With %B spec, used for a leaf without brothers."
4912   :version "22.1"
4913   :type 'string
4914   :group 'gnus-thread)
4915
4916 (defun gnus-summary-prepare-threads (threads)
4917   "Prepare summary buffer from THREADS and indentation LEVEL.
4918 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4919 or a straight list of headers."
4920   (gnus-message 7 "Generating summary...")
4921
4922   (setq gnus-newsgroup-threads threads)
4923   (beginning-of-line)
4924
4925   (let ((gnus-tmp-level 0)
4926         (default-score (or gnus-summary-default-score 0))
4927         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4928         (building-line-count gnus-summary-display-while-building)
4929         (building-count (integerp gnus-summary-display-while-building))
4930         thread number subject stack state gnus-tmp-gathered beg-match
4931         new-roots gnus-tmp-new-adopts thread-end simp-subject
4932         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4933         gnus-tmp-replied gnus-tmp-subject-or-nil
4934         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4935         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4936         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4937         tree-stack)
4938
4939     (setq gnus-tmp-prev-subject nil
4940           gnus-tmp-thread-tree-header-string "")
4941
4942     (if (vectorp (car threads))
4943         ;; If this is a straight (sic) list of headers, then a
4944         ;; threaded summary display isn't required, so we just create
4945         ;; an unthreaded one.
4946         (gnus-summary-prepare-unthreaded threads)
4947
4948       ;; Do the threaded display.
4949
4950       (if gnus-summary-display-while-building
4951           (switch-to-buffer (buffer-name)))
4952       (while (or threads stack gnus-tmp-new-adopts new-roots)
4953
4954         (if (and (= gnus-tmp-level 0)
4955                  (or (not stack)
4956                      (= (caar stack) 0))
4957                  (not gnus-tmp-false-parent)
4958                  (or gnus-tmp-new-adopts new-roots))
4959             (if gnus-tmp-new-adopts
4960                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4961                       thread (list (car gnus-tmp-new-adopts))
4962                       gnus-tmp-header (caar thread)
4963                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4964               (when new-roots
4965                 (setq thread (list (car new-roots))
4966                       gnus-tmp-header (caar thread)
4967                       new-roots (cdr new-roots))))
4968
4969           (if threads
4970               ;; If there are some threads, we do them before the
4971               ;; threads on the stack.
4972               (setq thread threads
4973                     gnus-tmp-header (caar thread))
4974             ;; There were no current threads, so we pop something off
4975             ;; the stack.
4976             (setq state (car stack)
4977                   gnus-tmp-level (car state)
4978                   tree-stack (cadr state)
4979                   thread (caddr state)
4980                   stack (cdr stack)
4981                   gnus-tmp-header (caar thread))))
4982
4983         (setq gnus-tmp-false-parent nil)
4984         (setq gnus-tmp-root-expunged nil)
4985         (setq thread-end nil)
4986
4987         (if (stringp gnus-tmp-header)
4988             ;; The header is a dummy root.
4989             (cond
4990              ((eq gnus-summary-make-false-root 'adopt)
4991               ;; We let the first article adopt the rest.
4992               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4993                                                (cddar thread)))
4994               (setq gnus-tmp-gathered
4995                     (nconc (mapcar
4996                             (lambda (h) (mail-header-number (car h)))
4997                             (cddar thread))
4998                            gnus-tmp-gathered))
4999               (setq thread (cons (list (caar thread)
5000                                        (cadar thread))
5001                                  (cdr thread)))
5002               (setq gnus-tmp-level -1
5003                     gnus-tmp-false-parent t))
5004              ((eq gnus-summary-make-false-root 'empty)
5005               ;; We print adopted articles with empty subject fields.
5006               (setq gnus-tmp-gathered
5007                     (nconc (mapcar
5008                             (lambda (h) (mail-header-number (car h)))
5009                             (cddar thread))
5010                            gnus-tmp-gathered))
5011               (setq gnus-tmp-level -1))
5012              ((eq gnus-summary-make-false-root 'dummy)
5013               ;; We remember that we probably want to output a dummy
5014               ;; root.
5015               (setq gnus-tmp-dummy-line gnus-tmp-header)
5016               (setq gnus-tmp-prev-subject gnus-tmp-header))
5017              (t
5018               ;; We do not make a root for the gathered
5019               ;; sub-threads at all.
5020               (setq gnus-tmp-level -1)))
5021
5022           (setq number (mail-header-number gnus-tmp-header)
5023                 subject (mail-header-subject gnus-tmp-header)
5024                 simp-subject (gnus-simplify-subject-fully subject))
5025
5026           (cond
5027            ;; If the thread has changed subject, we might want to make
5028            ;; this subthread into a root.
5029            ((and (null gnus-thread-ignore-subject)
5030                  (not (zerop gnus-tmp-level))
5031                  gnus-tmp-prev-subject
5032                  (not (string= gnus-tmp-prev-subject simp-subject)))
5033             (setq new-roots (nconc new-roots (list (car thread)))
5034                   thread-end t
5035                   gnus-tmp-header nil))
5036            ;; If the article lies outside the current limit,
5037            ;; then we do not display it.
5038            ((not (memq number gnus-newsgroup-limit))
5039             (setq gnus-tmp-gathered
5040                   (nconc (mapcar
5041                           (lambda (h) (mail-header-number (car h)))
5042                           (cdar thread))
5043                          gnus-tmp-gathered))
5044             (setq gnus-tmp-new-adopts (if (cdar thread)
5045                                           (append gnus-tmp-new-adopts
5046                                                   (cdar thread))
5047                                         gnus-tmp-new-adopts)
5048                   thread-end t
5049                   gnus-tmp-header nil)
5050             (when (zerop gnus-tmp-level)
5051               (setq gnus-tmp-root-expunged t)))
5052            ;; Perhaps this article is to be marked as read?
5053            ((and gnus-summary-mark-below
5054                  (< (or (cdr (assq number gnus-newsgroup-scored))
5055                         default-score)
5056                     gnus-summary-mark-below)
5057                  ;; Don't touch sparse articles.
5058                  (not (gnus-summary-article-sparse-p number))
5059                  (not (gnus-summary-article-ancient-p number)))
5060             (setq gnus-newsgroup-unreads
5061                   (delq number gnus-newsgroup-unreads))
5062             (if gnus-newsgroup-auto-expire
5063                 (setq gnus-newsgroup-expirable
5064                       (gnus-add-to-sorted-list
5065                        gnus-newsgroup-expirable number))
5066               (push (cons number gnus-low-score-mark)
5067                     gnus-newsgroup-reads))))
5068
5069           (when gnus-tmp-header
5070             ;; We may have an old dummy line to output before this
5071             ;; article.
5072             (when (and gnus-tmp-dummy-line
5073                        (gnus-subject-equal
5074                         gnus-tmp-dummy-line
5075                         (mail-header-subject gnus-tmp-header)))
5076               (gnus-summary-insert-dummy-line
5077                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5078               (setq gnus-tmp-dummy-line nil))
5079
5080             ;; Compute the mark.
5081             (setq gnus-tmp-unread (gnus-article-mark number))
5082
5083             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5084                                   gnus-tmp-header gnus-tmp-level)
5085                   gnus-newsgroup-data)
5086
5087             ;; Actually insert the line.
5088             (setq
5089              gnus-tmp-subject-or-nil
5090              (cond
5091               ((and gnus-thread-ignore-subject
5092                     gnus-tmp-prev-subject
5093                     (not (string= gnus-tmp-prev-subject simp-subject)))
5094                subject)
5095               ((zerop gnus-tmp-level)
5096                (if (and (eq gnus-summary-make-false-root 'empty)
5097                         (memq number gnus-tmp-gathered)
5098                         gnus-tmp-prev-subject
5099                         (string= gnus-tmp-prev-subject simp-subject))
5100                    gnus-summary-same-subject
5101                  subject))
5102               (t gnus-summary-same-subject)))
5103             (if (and (eq gnus-summary-make-false-root 'adopt)
5104                      (= gnus-tmp-level 1)
5105                      (memq number gnus-tmp-gathered))
5106                 (setq gnus-tmp-opening-bracket ?\<
5107                       gnus-tmp-closing-bracket ?\>)
5108               (setq gnus-tmp-opening-bracket ?\[
5109                     gnus-tmp-closing-bracket ?\]))
5110             (setq
5111              gnus-tmp-indentation
5112              (aref gnus-thread-indent-array gnus-tmp-level)
5113              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5114              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5115                                 gnus-summary-default-score 0)
5116              gnus-tmp-score-char
5117              (if (or (null gnus-summary-default-score)
5118                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5119                          gnus-summary-zcore-fuzz))
5120                  ?                      ;Whitespace
5121                (if (< gnus-tmp-score gnus-summary-default-score)
5122                    gnus-score-below-mark gnus-score-over-mark))
5123              gnus-tmp-replied
5124              (cond ((memq number gnus-newsgroup-processable)
5125                     gnus-process-mark)
5126                    ((memq number gnus-newsgroup-cached)
5127                     gnus-cached-mark)
5128                    ((memq number gnus-newsgroup-replied)
5129                     gnus-replied-mark)
5130                    ((memq number gnus-newsgroup-forwarded)
5131                     gnus-forwarded-mark)
5132                    ((memq number gnus-newsgroup-saved)
5133                     gnus-saved-mark)
5134                    ((memq number gnus-newsgroup-recent)
5135                     gnus-recent-mark)
5136                    ((memq number gnus-newsgroup-unseen)
5137                     gnus-unseen-mark)
5138                    (t gnus-no-mark))
5139              gnus-tmp-downloaded
5140              (cond ((memq number gnus-newsgroup-undownloaded)
5141                     gnus-undownloaded-mark)
5142                    (gnus-newsgroup-agentized
5143                     gnus-downloaded-mark)
5144                    (t
5145                     gnus-no-mark))
5146              gnus-tmp-from (mail-header-from gnus-tmp-header)
5147              gnus-tmp-name
5148              (cond
5149               ((string-match "<[^>]+> *$" gnus-tmp-from)
5150                (setq beg-match (match-beginning 0))
5151                (or (and (string-match "^\".+\"" gnus-tmp-from)
5152                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5153                    (substring gnus-tmp-from 0 beg-match)))
5154               ((string-match "(.+)" gnus-tmp-from)
5155                (substring gnus-tmp-from
5156                           (1+ (match-beginning 0)) (1- (match-end 0))))
5157               (t gnus-tmp-from))
5158
5159              ;; Do the %B string
5160              gnus-tmp-thread-tree-header-string
5161              (cond
5162               ((not gnus-show-threads) "")
5163               ((zerop gnus-tmp-level)
5164                (cond ((cdar thread)
5165                       (or gnus-sum-thread-tree-root subject))
5166                      (gnus-tmp-new-adopts
5167                       (or gnus-sum-thread-tree-false-root subject))
5168                      (t
5169                       (or gnus-sum-thread-tree-single-indent subject))))
5170               (t
5171                (concat (apply 'concat
5172                               (mapcar (lambda (item)
5173                                         (if (= item 1)
5174                                             gnus-sum-thread-tree-vertical
5175                                           gnus-sum-thread-tree-indent))
5176                                       (cdr (reverse tree-stack))))
5177                        (if (nth 1 thread)
5178                            gnus-sum-thread-tree-leaf-with-other
5179                          gnus-sum-thread-tree-single-leaf)))))
5180             (when (string= gnus-tmp-name "")
5181               (setq gnus-tmp-name gnus-tmp-from))
5182             (unless (numberp gnus-tmp-lines)
5183               (setq gnus-tmp-lines -1))
5184             (if (= gnus-tmp-lines -1)
5185                 (setq gnus-tmp-lines "?")
5186               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5187               (gnus-put-text-property
5188              (point)
5189              (progn (eval gnus-summary-line-format-spec) (point))
5190                'gnus-number number)
5191             (when gnus-visual-p
5192               (forward-line -1)
5193               (gnus-run-hooks 'gnus-summary-update-hook)
5194               (forward-line 1))
5195
5196             (setq gnus-tmp-prev-subject simp-subject)))
5197
5198         (when (nth 1 thread)
5199           (push (list (max 0 gnus-tmp-level)
5200                       (copy-sequence tree-stack)
5201                       (nthcdr 1 thread))
5202                 stack))
5203         (push (if (nth 1 thread) 1 0) tree-stack)
5204         (incf gnus-tmp-level)
5205         (setq threads (if thread-end nil (cdar thread)))
5206         (if gnus-summary-display-while-building
5207             (if building-count
5208                 (progn
5209                   ;; use a set frequency
5210                   (setq building-line-count (1- building-line-count))
5211                   (when (= building-line-count 0)
5212                     (sit-for 0)
5213                     (setq building-line-count
5214                           gnus-summary-display-while-building)))
5215               ;; always
5216               (sit-for 0)))
5217         (unless threads
5218           (setq gnus-tmp-level 0)))))
5219   (gnus-message 7 "Generating summary...done"))
5220
5221 (defun gnus-summary-prepare-unthreaded (headers)
5222   "Generate an unthreaded summary buffer based on HEADERS."
5223   (let (header number mark)
5224
5225     (beginning-of-line)
5226
5227     (while headers
5228       ;; We may have to root out some bad articles...
5229       (when (memq (setq number (mail-header-number
5230                                 (setq header (pop headers))))
5231                   gnus-newsgroup-limit)
5232         ;; Mark article as read when it has a low score.
5233         (when (and gnus-summary-mark-below
5234                    (< (or (cdr (assq number gnus-newsgroup-scored))
5235                           gnus-summary-default-score 0)
5236                       gnus-summary-mark-below)
5237                    (not (gnus-summary-article-ancient-p number)))
5238           (setq gnus-newsgroup-unreads
5239                 (delq number gnus-newsgroup-unreads))
5240           (if gnus-newsgroup-auto-expire
5241               (push number gnus-newsgroup-expirable)
5242             (push (cons number gnus-low-score-mark)
5243                   gnus-newsgroup-reads)))
5244
5245         (setq mark (gnus-article-mark number))
5246         (push (gnus-data-make number mark (1+ (point)) header 0)
5247               gnus-newsgroup-data)
5248         (gnus-summary-insert-line
5249          header 0 number
5250          (memq number gnus-newsgroup-undownloaded)
5251          mark (memq number gnus-newsgroup-replied)
5252          (memq number gnus-newsgroup-expirable)
5253          (mail-header-subject header) nil
5254          (cdr (assq number gnus-newsgroup-scored))
5255          (memq number gnus-newsgroup-processable))))))
5256
5257 (defun gnus-summary-remove-list-identifiers ()
5258   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5259   (let ((regexp (if (consp gnus-list-identifiers)
5260                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5261                   gnus-list-identifiers))
5262         changed subject)
5263     (when regexp
5264       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5265       (dolist (header gnus-newsgroup-headers)
5266         (setq subject (mail-header-subject header)
5267               changed nil)
5268         (while (string-match regexp subject)
5269           (setq subject
5270                 (concat (substring subject 0 (match-beginning 1))
5271                         (substring subject (match-end 0)))
5272                 changed t))
5273         (when changed
5274           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5275             (setq subject
5276                   (concat (substring subject 0 (match-beginning 1))
5277                           (substring subject (match-end 1)))))
5278           (mail-header-set-subject header subject))))))
5279
5280 (defun gnus-fetch-headers (articles)
5281   "Fetch headers of ARTICLES."
5282   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5283     (gnus-message 5 "Fetching headers for %s..." name)
5284     (prog1
5285         (if (eq 'nov
5286                 (setq gnus-headers-retrieved-by
5287                       (gnus-retrieve-headers
5288                        articles gnus-newsgroup-name
5289                        ;; We might want to fetch old headers, but
5290                        ;; not if there is only 1 article.
5291                        (and (or (and
5292                                  (not (eq gnus-fetch-old-headers 'some))
5293                                  (not (numberp gnus-fetch-old-headers)))
5294                                 (> (length articles) 1))
5295                             gnus-fetch-old-headers))))
5296             (gnus-get-newsgroup-headers-xover
5297              articles nil nil gnus-newsgroup-name t)
5298           (gnus-get-newsgroup-headers))
5299       (gnus-message 5 "Fetching headers for %s...done" name))))
5300
5301 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5302   "Select newsgroup GROUP.
5303 If READ-ALL is non-nil, all articles in the group are selected.
5304 If SELECT-ARTICLES, only select those articles from GROUP."
5305   (let* ((entry (gnus-group-entry group))
5306          ;;!!! Dirty hack; should be removed.
5307          (gnus-summary-ignore-duplicates
5308           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5309               t
5310             gnus-summary-ignore-duplicates))
5311          (info (nth 2 entry))
5312          articles fetched-articles cached)
5313
5314     (unless (gnus-check-server
5315              (set (make-local-variable 'gnus-current-select-method)
5316                   (gnus-find-method-for-group group)))
5317       (error "Couldn't open server"))
5318
5319     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5320         (gnus-activate-group group)     ; Or we can activate it...
5321         (progn                          ; Or we bug out.
5322           (when (equal major-mode 'gnus-summary-mode)
5323             (gnus-kill-buffer (current-buffer)))
5324           (error "Couldn't activate group %s: %s"
5325                  group (gnus-status-message group))))
5326
5327     (unless (gnus-request-group group t)
5328       (when (equal major-mode 'gnus-summary-mode)
5329         (gnus-kill-buffer (current-buffer)))
5330       (error "Couldn't request group %s: %s"
5331              group (gnus-status-message group)))
5332
5333     (when gnus-agent
5334       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5335
5336       (setq gnus-summary-use-undownloaded-faces
5337             (gnus-agent-find-parameter
5338              group
5339              'agent-enable-undownloaded-faces)))
5340
5341     (setq gnus-newsgroup-name group
5342           gnus-newsgroup-unselected nil
5343           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5344
5345     (let ((display (gnus-group-find-parameter group 'display)))
5346       (setq gnus-newsgroup-display
5347             (cond
5348              ((not (zerop (or (car-safe read-all) 0)))
5349               ;; The user entered the group with C-u SPC/RET, let's show
5350               ;; all articles.
5351               'gnus-not-ignore)
5352              ((eq display 'all)
5353               'gnus-not-ignore)
5354              ((arrayp display)
5355               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5356              ((numberp display)
5357               ;; The following is probably the "correct" solution, but
5358               ;; it makes Gnus fetch all headers and then limit the
5359               ;; articles (which is slow), so instead we hack the
5360               ;; select-articles parameter instead. -- Simon Josefsson
5361               ;; <jas@kth.se>
5362               ;;
5363               ;; (gnus-byte-compile
5364               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5365               ;;                         display)))))
5366               (setq select-articles
5367                     (gnus-uncompress-range
5368                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5369                              (if (> tmp 0)
5370                                  tmp
5371                                1))
5372                            (cdr (gnus-active group)))))
5373               nil)
5374              (t
5375               nil))))
5376
5377     (gnus-summary-setup-default-charset)
5378
5379     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5380     (when (gnus-virtual-group-p group)
5381       (setq cached gnus-newsgroup-cached))
5382
5383     (setq gnus-newsgroup-unreads
5384           (gnus-sorted-ndifference
5385            (gnus-sorted-ndifference gnus-newsgroup-unreads
5386                                     gnus-newsgroup-marked)
5387            gnus-newsgroup-dormant))
5388
5389     (setq gnus-newsgroup-processable nil)
5390
5391     (gnus-update-read-articles group gnus-newsgroup-unreads)
5392
5393     ;; Adjust and set lists of article marks.
5394     (when info
5395       (gnus-adjust-marked-articles info))
5396     (if (setq articles select-articles)
5397         (setq gnus-newsgroup-unselected
5398               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5399       (setq articles (gnus-articles-to-read group read-all)))
5400
5401     (cond
5402      ((null articles)
5403       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5404       'quit)
5405      ((eq articles 0) nil)
5406      (t
5407       ;; Init the dependencies hash table.
5408       (setq gnus-newsgroup-dependencies
5409             (gnus-make-hashtable (length articles)))
5410       (gnus-set-global-variables)
5411       ;; Retrieve the headers and read them in.
5412
5413       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5414
5415       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5416       (when cached
5417         (setq gnus-newsgroup-cached cached))
5418
5419       ;; Suppress duplicates?
5420       (when gnus-suppress-duplicates
5421         (gnus-dup-suppress-articles))
5422
5423       ;; Set the initial limit.
5424       (setq gnus-newsgroup-limit (copy-sequence articles))
5425       ;; Remove canceled articles from the list of unread articles.
5426       (setq fetched-articles
5427             (mapcar (lambda (headers) (mail-header-number headers))
5428                     gnus-newsgroup-headers))
5429       (setq gnus-newsgroup-articles fetched-articles)
5430       (setq gnus-newsgroup-unreads
5431             (gnus-sorted-nintersection
5432              gnus-newsgroup-unreads fetched-articles))
5433       (gnus-compute-unseen-list)
5434
5435       ;; Removed marked articles that do not exist.
5436       (gnus-update-missing-marks
5437        (gnus-sorted-difference articles fetched-articles))
5438       ;; We might want to build some more threads first.
5439       (when (and gnus-fetch-old-headers
5440                  (eq gnus-headers-retrieved-by 'nov))
5441         (if (eq gnus-fetch-old-headers 'invisible)
5442             (gnus-build-all-threads)
5443           (gnus-build-old-threads)))
5444       ;; Let the Gnus agent mark articles as read.
5445       (when gnus-agent
5446         (gnus-agent-get-undownloaded-list))
5447       ;; Remove list identifiers from subject
5448       (when gnus-list-identifiers
5449         (gnus-summary-remove-list-identifiers))
5450       ;; Check whether auto-expire is to be done in this group.
5451       (setq gnus-newsgroup-auto-expire
5452             (gnus-group-auto-expirable-p group))
5453       ;; Set up the article buffer now, if necessary.
5454       (unless gnus-single-article-buffer
5455         (gnus-article-setup-buffer))
5456       ;; First and last article in this newsgroup.
5457       (when gnus-newsgroup-headers
5458         (setq gnus-newsgroup-begin
5459               (mail-header-number (car gnus-newsgroup-headers))
5460               gnus-newsgroup-end
5461               (mail-header-number
5462                (gnus-last-element gnus-newsgroup-headers))))
5463       ;; GROUP is successfully selected.
5464       (or gnus-newsgroup-headers t)))))
5465
5466 (defun gnus-compute-unseen-list ()
5467   ;; The `seen' marks are treated specially.
5468   (if (not gnus-newsgroup-seen)
5469       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5470     (setq gnus-newsgroup-unseen
5471           (gnus-inverse-list-range-intersection
5472            gnus-newsgroup-articles gnus-newsgroup-seen))))
5473
5474 (defun gnus-summary-display-make-predicate (display)
5475   (require 'gnus-agent)
5476   (when (= (length display) 1)
5477     (setq display (car display)))
5478   (unless gnus-summary-display-cache
5479     (dolist (elem (append '((unread . unread)
5480                             (read . read)
5481                             (unseen . unseen))
5482                           gnus-article-mark-lists))
5483       (push (cons (cdr elem)
5484                   (gnus-byte-compile
5485                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5486             gnus-summary-display-cache)))
5487   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5488         (gnus-category-predicate-cache gnus-summary-display-cache))
5489     (gnus-get-predicate display)))
5490
5491 ;; Uses the dynamically bound `number' variable.
5492 (eval-when-compile
5493   (defvar number))
5494 (defun gnus-article-marked-p (type &optional article)
5495   (let ((article (or article number)))
5496     (cond
5497      ((eq type 'tick)
5498       (memq article gnus-newsgroup-marked))
5499      ((eq type 'spam)
5500       (memq article gnus-newsgroup-spam-marked))
5501      ((eq type 'unsend)
5502       (memq article gnus-newsgroup-unsendable))
5503      ((eq type 'undownload)
5504       (memq article gnus-newsgroup-undownloaded))
5505      ((eq type 'download)
5506       (memq article gnus-newsgroup-downloadable))
5507      ((eq type 'unread)
5508       (memq article gnus-newsgroup-unreads))
5509      ((eq type 'read)
5510       (memq article gnus-newsgroup-reads))
5511      ((eq type 'dormant)
5512       (memq article gnus-newsgroup-dormant) )
5513      ((eq type 'expire)
5514       (memq article gnus-newsgroup-expirable))
5515      ((eq type 'reply)
5516       (memq article gnus-newsgroup-replied))
5517      ((eq type 'killed)
5518       (memq article gnus-newsgroup-killed))
5519      ((eq type 'bookmark)
5520       (assq article gnus-newsgroup-bookmarks))
5521      ((eq type 'score)
5522       (assq article gnus-newsgroup-scored))
5523      ((eq type 'save)
5524       (memq article gnus-newsgroup-saved))
5525      ((eq type 'cache)
5526       (memq article gnus-newsgroup-cached))
5527      ((eq type 'forward)
5528       (memq article gnus-newsgroup-forwarded))
5529      ((eq type 'seen)
5530       (not (memq article gnus-newsgroup-unseen)))
5531      ((eq type 'recent)
5532       (memq article gnus-newsgroup-recent))
5533      (t t))))
5534
5535 (defun gnus-articles-to-read (group &optional read-all)
5536   "Find out what articles the user wants to read."
5537   (let* ((articles
5538           ;; Select all articles if `read-all' is non-nil, or if there
5539           ;; are no unread articles.
5540           (if (or read-all
5541                   (and (zerop (length gnus-newsgroup-marked))
5542                        (zerop (length gnus-newsgroup-unreads)))
5543                   ;; Fetch all if the predicate is non-nil.
5544                   gnus-newsgroup-display)
5545               ;; We want to select the headers for all the articles in
5546               ;; the group, so we select either all the active
5547               ;; articles in the group, or (if that's nil), the
5548               ;; articles in the cache.
5549               (or
5550                (gnus-uncompress-range (gnus-active group))
5551                (gnus-cache-articles-in-group group))
5552             ;; Select only the "normal" subset of articles.
5553             (gnus-sorted-nunion
5554              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5555              gnus-newsgroup-unreads)))
5556          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5557          (scored (length scored-list))
5558          (number (length articles))
5559          (marked (+ (length gnus-newsgroup-marked)
5560                     (length gnus-newsgroup-dormant)))
5561          (select
5562           (cond
5563            ((numberp read-all)
5564             read-all)
5565            ((numberp gnus-newsgroup-display)
5566             gnus-newsgroup-display)
5567            (t
5568             (condition-case ()
5569                 (cond
5570                  ((and (or (<= scored marked) (= scored number))
5571                        (numberp gnus-large-newsgroup)
5572                        (> number gnus-large-newsgroup))
5573                   (let* ((cursor-in-echo-area nil)
5574                          (initial (gnus-parameter-large-newsgroup-initial
5575                                    gnus-newsgroup-name))
5576                          (input
5577                           (read-string
5578                            (format
5579                             "How many articles from %s (%s %d): "
5580                             (gnus-limit-string
5581                              (gnus-group-decoded-name gnus-newsgroup-name)
5582                              35)
5583                             (if initial "max" "default")
5584                             number)
5585                            (if initial
5586                                (cons (number-to-string initial)
5587                                      0)))))
5588                     (if (string-match "^[ \t]*$" input) number input)))
5589                  ((and (> scored marked) (< scored number)
5590                        (> (- scored number) 20))
5591                   (let ((input
5592                          (read-string
5593                           (format "%s %s (%d scored, %d total): "
5594                                   "How many articles from"
5595                                   (gnus-group-decoded-name group)
5596                                   scored number))))
5597                     (if (string-match "^[ \t]*$" input)
5598                         number input)))
5599                  (t number))
5600               (quit
5601                (message "Quit getting the articles to read")
5602                nil))))))
5603     (setq select (if (stringp select) (string-to-number select) select))
5604     (if (or (null select) (zerop select))
5605         select
5606       (if (and (not (zerop scored)) (<= (abs select) scored))
5607           (progn
5608             (setq articles (sort scored-list '<))
5609             (setq number (length articles)))
5610         (setq articles (copy-sequence articles)))
5611
5612       (when (< (abs select) number)
5613         (if (< select 0)
5614             ;; Select the N oldest articles.
5615             (setcdr (nthcdr (1- (abs select)) articles) nil)
5616           ;; Select the N most recent articles.
5617           (setq articles (nthcdr (- number select) articles))))
5618       (setq gnus-newsgroup-unselected
5619             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5620       (when gnus-alter-articles-to-read-function
5621         (setq articles
5622               (sort
5623                (funcall gnus-alter-articles-to-read-function
5624                         gnus-newsgroup-name articles)
5625                '<)))
5626       articles)))
5627
5628 (defun gnus-killed-articles (killed articles)
5629   (let (out)
5630     (while articles
5631       (when (inline (gnus-member-of-range (car articles) killed))
5632         (push (car articles) out))
5633       (setq articles (cdr articles)))
5634     out))
5635
5636 (defun gnus-uncompress-marks (marks)
5637   "Uncompress the mark ranges in MARKS."
5638   (let ((uncompressed '(score bookmark))
5639         out)
5640     (while marks
5641       (if (memq (caar marks) uncompressed)
5642           (push (car marks) out)
5643         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5644       (setq marks (cdr marks)))
5645     out))
5646
5647 (defun gnus-article-mark-to-type (mark)
5648   "Return the type of MARK."
5649   (or (cadr (assq mark gnus-article-special-mark-lists))
5650       'list))
5651
5652 (defun gnus-article-unpropagatable-p (mark)
5653   "Return whether MARK should be propagated to back end."
5654   (memq mark gnus-article-unpropagated-mark-lists))
5655
5656 (defun gnus-adjust-marked-articles (info)
5657   "Set all article lists and remove all marks that are no longer valid."
5658   (let* ((marked-lists (gnus-info-marks info))
5659          (active (gnus-active (gnus-info-group info)))
5660          (min (car active))
5661          (max (cdr active))
5662          (types gnus-article-mark-lists)
5663          marks var articles article mark mark-type
5664          bgn end)
5665
5666     (dolist (marks marked-lists)
5667       (setq mark (car marks)
5668             mark-type (gnus-article-mark-to-type mark)
5669             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5670
5671       ;; We set the variable according to the type of the marks list,
5672       ;; and then adjust the marks to a subset of the active articles.
5673       (cond
5674        ;; Adjust "simple" lists - compressed yet unsorted
5675        ((eq mark-type 'list)
5676         ;; Simultaneously uncompress and clip to active range
5677         ;; See gnus-uncompress-range for a description of possible marks
5678         (let (l lh)
5679           (if (not (cadr marks))
5680               (set var nil)
5681             (setq articles (if (numberp (cddr marks))
5682                                (list (cdr marks))
5683                              (cdr marks))
5684                   lh (cons nil nil)
5685                   l lh)
5686
5687             (while (setq article (pop articles))
5688               (cond ((consp article)
5689                      (setq bgn (max (car article) min)
5690                            end (min (cdr article) max))
5691                      (while (<= bgn end)
5692                        (setq l (setcdr l (cons bgn nil))
5693                              bgn (1+ bgn))))
5694                     ((and (<= min article)
5695                           (>= max article))
5696                      (setq l (setcdr l (cons article nil))))))
5697             (set var (cdr lh)))))
5698        ;; Adjust assocs.
5699        ((eq mark-type 'tuple)
5700         (set var (setq articles (cdr marks)))
5701         (when (not (listp (cdr (symbol-value var))))
5702           (set var (list (symbol-value var))))
5703         (when (not (listp (cdr articles)))
5704           (setq articles (list articles)))
5705         (while articles
5706           (when (or (not (consp (setq article (pop articles))))
5707                     (< (car article) min)
5708                     (> (car article) max))
5709             (set var (delq article (symbol-value var))))))
5710        ;; Adjust ranges (sloppily).
5711        ((eq mark-type 'range)
5712         (cond
5713          ((eq mark 'seen)
5714           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5715           ;; It should be (seen (NUM1 . NUM2)).
5716           (when (numberp (cddr marks))
5717             (setcdr marks (list (cdr marks))))
5718           (setq articles (cdr marks))
5719           (while (and articles
5720                       (or (and (consp (car articles))
5721                                (> min (cdar articles)))
5722                           (and (numberp (car articles))
5723                                (> min (car articles)))))
5724             (pop articles))
5725           (set var articles))))))))
5726
5727 (defun gnus-update-missing-marks (missing)
5728   "Go through the list of MISSING articles and remove them from the mark lists."
5729   (when missing
5730     (let (var m)
5731       ;; Go through all types.
5732       (dolist (elem gnus-article-mark-lists)
5733         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5734           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5735           (when (symbol-value var)
5736             ;; This list has articles.  So we delete all missing
5737             ;; articles from it.
5738             (setq m missing)
5739             (while m
5740               (set var (delq (pop m) (symbol-value var))))))))))
5741
5742 (defun gnus-update-marks ()
5743   "Enter the various lists of marked articles into the newsgroup info list."
5744   (let ((types gnus-article-mark-lists)
5745         (info (gnus-get-info gnus-newsgroup-name))
5746         type list newmarked symbol delta-marks)
5747     (when info
5748       ;; Add all marks lists to the list of marks lists.
5749       (while (setq type (pop types))
5750         (setq list (symbol-value
5751                     (setq symbol
5752                           (intern (format "gnus-newsgroup-%s" (car type))))))
5753
5754         (when list
5755           ;; Get rid of the entries of the articles that have the
5756           ;; default score.
5757           (when (and (eq (cdr type) 'score)
5758                      gnus-save-score
5759                      list)
5760             (let* ((arts list)
5761                    (prev (cons nil list))
5762                    (all prev))
5763               (while arts
5764                 (if (or (not (consp (car arts)))
5765                         (= (cdar arts) gnus-summary-default-score))
5766                     (setcdr prev (cdr arts))
5767                   (setq prev arts))
5768                 (setq arts (cdr arts)))
5769               (setq list (cdr all)))))
5770
5771         (when (eq (cdr type) 'seen)
5772           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5773
5774         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5775           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5776
5777         (when (and (gnus-check-backend-function
5778                     'request-set-mark gnus-newsgroup-name)
5779                    (not (gnus-article-unpropagatable-p (cdr type))))
5780           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5781                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5782                  (add (gnus-remove-from-range
5783                        (gnus-copy-sequence list) old)))
5784             (when add
5785               (push (list add 'add (list (cdr type))) delta-marks))
5786             (when del
5787               (push (list del 'del (list (cdr type))) delta-marks))))
5788
5789         (when list
5790           (push (cons (cdr type) list) newmarked)))
5791
5792       (when delta-marks
5793         (unless (gnus-check-group gnus-newsgroup-name)
5794           (error "Can't open server for %s" gnus-newsgroup-name))
5795         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5796
5797       ;; Enter these new marks into the info of the group.
5798       (if (nthcdr 3 info)
5799           (setcar (nthcdr 3 info) newmarked)
5800         ;; Add the marks lists to the end of the info.
5801         (when newmarked
5802           (setcdr (nthcdr 2 info) (list newmarked))))
5803
5804       ;; Cut off the end of the info if there's nothing else there.
5805       (let ((i 5))
5806         (while (and (> i 2)
5807                     (not (nth i info)))
5808           (when (nthcdr (decf i) info)
5809             (setcdr (nthcdr i info) nil)))))))
5810
5811 (defun gnus-set-mode-line (where)
5812   "Set the mode line of the article or summary buffers.
5813 If WHERE is `summary', the summary mode line format will be used."
5814   ;; Is this mode line one we keep updated?
5815   (when (and (memq where gnus-updated-mode-lines)
5816              (symbol-value
5817               (intern (format "gnus-%s-mode-line-format-spec" where))))
5818     (let (mode-string)
5819       (save-excursion
5820         ;; We evaluate this in the summary buffer since these
5821         ;; variables are buffer-local to that buffer.
5822         (set-buffer gnus-summary-buffer)
5823        ;; We bind all these variables that are used in the `eval' form
5824         ;; below.
5825         (let* ((mformat (symbol-value
5826                          (intern
5827                           (format "gnus-%s-mode-line-format-spec" where))))
5828                (gnus-tmp-group-name (gnus-group-decoded-name
5829                                      gnus-newsgroup-name))
5830                (gnus-tmp-article-number (or gnus-current-article 0))
5831                (gnus-tmp-unread gnus-newsgroup-unreads)
5832                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5833                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5834                (gnus-tmp-unread-and-unselected
5835                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5836                             (zerop gnus-tmp-unselected))
5837                        "")
5838                       ((zerop gnus-tmp-unselected)
5839                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5840                       (t (format "{%d(+%d) more}"
5841                                  gnus-tmp-unread-and-unticked
5842                                  gnus-tmp-unselected))))
5843                (gnus-tmp-subject
5844                 (if (and gnus-current-headers
5845                          (vectorp gnus-current-headers))
5846                     (gnus-mode-string-quote
5847                      (mail-header-subject gnus-current-headers))
5848                   ""))
5849                bufname-length max-len
5850                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5851           (setq mode-string (eval mformat))
5852           (setq bufname-length (if (string-match "%b" mode-string)
5853                                    (- (length
5854                                        (buffer-name
5855                                         (if (eq where 'summary)
5856                                             nil
5857                                           (get-buffer gnus-article-buffer))))
5858                                       2)
5859                                  0))
5860           (setq max-len (max 4 (if gnus-mode-non-string-length
5861                                    (- (window-width)
5862                                       gnus-mode-non-string-length
5863                                       bufname-length)
5864                                  (length mode-string))))
5865           ;; We might have to chop a bit of the string off...
5866           (when (> (length mode-string) max-len)
5867             (setq mode-string
5868                   (concat (truncate-string-to-width mode-string (- max-len 3))
5869                           "...")))
5870           ;; Pad the mode string a bit.
5871           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5872       ;; Update the mode line.
5873       (setq mode-line-buffer-identification
5874             (gnus-mode-line-buffer-identification (list mode-string)))
5875       (set-buffer-modified-p t))))
5876
5877 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5878   "Go through the HEADERS list and add all Xrefs to a hash table.
5879 The resulting hash table is returned, or nil if no Xrefs were found."
5880   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5881          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5882          (xref-hashtb (gnus-make-hashtable))
5883          start group entry number xrefs header)
5884     (while headers
5885       (setq header (pop headers))
5886       (when (and (setq xrefs (mail-header-xref header))
5887                  (not (memq (setq number (mail-header-number header))
5888                             unreads)))
5889         (setq start 0)
5890         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5891           (setq start (match-end 0))
5892           (setq group (if prefix
5893                           (concat prefix (substring xrefs (match-beginning 1)
5894                                                     (match-end 1)))
5895                         (substring xrefs (match-beginning 1) (match-end 1))))
5896           (setq number
5897                 (string-to-number (substring xrefs (match-beginning 2)
5898                                           (match-end 2))))
5899           (if (setq entry (gnus-gethash group xref-hashtb))
5900               (setcdr entry (cons number (cdr entry)))
5901             (gnus-sethash group (cons number nil) xref-hashtb)))))
5902     (and start xref-hashtb)))
5903
5904 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5905   "Look through all the headers and mark the Xrefs as read."
5906   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5907         name info xref-hashtb idlist method nth4)
5908     (save-excursion
5909       (set-buffer gnus-group-buffer)
5910       (when (setq xref-hashtb
5911                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5912         (mapatoms
5913          (lambda (group)
5914            (unless (string= from-newsgroup (setq name (symbol-name group)))
5915              (setq idlist (symbol-value group))
5916              ;; Dead groups are not updated.
5917              (and (prog1
5918                       (setq info (gnus-get-info name))
5919                     (when (stringp (setq nth4 (gnus-info-method info)))
5920                       (setq nth4 (gnus-server-to-method nth4))))
5921                   ;; Only do the xrefs if the group has the same
5922                   ;; select method as the group we have just read.
5923                   (or (gnus-methods-equal-p
5924                        nth4 (gnus-find-method-for-group from-newsgroup))
5925                       virtual
5926                       (equal nth4 (setq method (gnus-find-method-for-group
5927                                                 from-newsgroup)))
5928                       (and (equal (car nth4) (car method))
5929                            (equal (nth 1 nth4) (nth 1 method))))
5930                   gnus-use-cross-reference
5931                   (or (not (eq gnus-use-cross-reference t))
5932                       virtual
5933                       ;; Only do cross-references on subscribed
5934                       ;; groups, if that is what is wanted.
5935                       (<= (gnus-info-level info) gnus-level-subscribed))
5936                   (gnus-group-make-articles-read name idlist))))
5937          xref-hashtb)))))
5938
5939 (defun gnus-compute-read-articles (group articles)
5940   (let* ((entry (gnus-group-entry group))
5941          (info (nth 2 entry))
5942          (active (gnus-active group))
5943          ninfo)
5944     (when entry
5945       ;; First peel off all invalid article numbers.
5946       (when active
5947         (let ((ids articles)
5948               id first)
5949           (while (setq id (pop ids))
5950             (when (and first (> id (cdr active)))
5951               ;; We'll end up in this situation in one particular
5952               ;; obscure situation.  If you re-scan a group and get
5953               ;; a new article that is cross-posted to a different
5954               ;; group that has not been re-scanned, you might get
5955               ;; crossposted article that has a higher number than
5956               ;; Gnus believes possible.  So we re-activate this
5957               ;; group as well.  This might mean doing the
5958               ;; crossposting thingy will *increase* the number
5959               ;; of articles in some groups.  Tsk, tsk.
5960               (setq active (or (gnus-activate-group group) active)))
5961             (when (or (> id (cdr active))
5962                       (< id (car active)))
5963               (setq articles (delq id articles))))))
5964       ;; If the read list is nil, we init it.
5965       (if (and active
5966                (null (gnus-info-read info))
5967                (> (car active) 1))
5968           (setq ninfo (cons 1 (1- (car active))))
5969         (setq ninfo (gnus-info-read info)))
5970       ;; Then we add the read articles to the range.
5971       (gnus-add-to-range
5972        ninfo (setq articles (sort articles '<))))))
5973
5974 (defun gnus-group-make-articles-read (group articles)
5975   "Update the info of GROUP to say that ARTICLES are read."
5976   (let* ((num 0)
5977          (entry (gnus-group-entry group))
5978          (info (nth 2 entry))
5979          (active (gnus-active group))
5980          range)
5981     (when entry
5982       (setq range (gnus-compute-read-articles group articles))
5983       (with-current-buffer gnus-group-buffer
5984         (gnus-undo-register
5985           `(progn
5986              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5987              (gnus-info-set-read ',info ',(gnus-info-read info))
5988              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5989              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5990              (gnus-group-update-group ,group t))))
5991       ;; Add the read articles to the range.
5992       (gnus-info-set-read info range)
5993       (gnus-request-set-mark group (list (list range 'add '(read))))
5994       ;; Then we have to re-compute how many unread
5995       ;; articles there are in this group.
5996       (when active
5997         (cond
5998          ((not range)
5999           (setq num (- (1+ (cdr active)) (car active))))
6000          ((not (listp (cdr range)))
6001           (setq num (- (cdr active) (- (1+ (cdr range))
6002                                        (car range)))))
6003          (t
6004           (while range
6005             (if (numberp (car range))
6006                 (setq num (1+ num))
6007               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6008             (setq range (cdr range)))
6009           (setq num (- (cdr active) num))))
6010         ;; Update the number of unread articles.
6011         (setcar entry num)
6012         ;; Update the group buffer.
6013         (unless (gnus-ephemeral-group-p group)
6014           (gnus-group-update-group group t))))))
6015
6016 (defvar gnus-newsgroup-none-id 0)
6017
6018 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6019   (let ((cur nntp-server-buffer)
6020         (dependencies
6021          (or dependencies
6022              (with-current-buffer gnus-summary-buffer
6023                gnus-newsgroup-dependencies)))
6024         headers id end ref number
6025         (mail-parse-charset gnus-newsgroup-charset)
6026         (mail-parse-ignored-charsets
6027          (save-excursion (condition-case nil
6028                              (set-buffer gnus-summary-buffer)
6029                            (error))
6030                          gnus-newsgroup-ignored-charsets)))
6031     (save-excursion
6032       (set-buffer nntp-server-buffer)
6033       ;; Translate all TAB characters into SPACE characters.
6034       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6035       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6036       (ietf-drums-unfold-fws)
6037       (gnus-run-hooks 'gnus-parse-headers-hook)
6038       (let ((case-fold-search t)
6039             in-reply-to header p lines chars)
6040         (goto-char (point-min))
6041         ;; Search to the beginning of the next header.  Error messages
6042         ;; do not begin with 2 or 3.
6043         (while (re-search-forward "^[23][0-9]+ " nil t)
6044           (setq id nil
6045                 ref nil)
6046           ;; This implementation of this function, with nine
6047           ;; search-forwards instead of the one re-search-forward and
6048           ;; a case (which basically was the old function) is actually
6049           ;; about twice as fast, even though it looks messier.  You
6050           ;; can't have everything, I guess.  Speed and elegance
6051           ;; doesn't always go hand in hand.
6052           (setq
6053            header
6054            (vector
6055             ;; Number.
6056             (prog1
6057                 (setq number (read cur))
6058               (end-of-line)
6059               (setq p (point))
6060               (narrow-to-region (point)
6061                                 (or (and (search-forward "\n.\n" nil t)
6062                                          (- (point) 2))
6063                                     (point))))
6064             ;; Subject.
6065             (progn
6066               (goto-char p)
6067               (if (search-forward "\nsubject:" nil t)
6068                   (funcall gnus-decode-encoded-word-function
6069                            (nnheader-header-value))
6070                 "(none)"))
6071             ;; From.
6072             (progn
6073               (goto-char p)
6074               (if (search-forward "\nfrom:" nil t)
6075                   (funcall gnus-decode-encoded-word-function
6076                            (nnheader-header-value))
6077                 "(nobody)"))
6078             ;; Date.
6079             (progn
6080               (goto-char p)
6081               (if (search-forward "\ndate:" nil t)
6082                   (nnheader-header-value) ""))
6083             ;; Message-ID.
6084             (progn
6085               (goto-char p)
6086               (setq id (if (re-search-forward
6087                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6088                            ;; We do it this way to make sure the Message-ID
6089                            ;; is (somewhat) syntactically valid.
6090                            (buffer-substring (match-beginning 1)
6091                                              (match-end 1))
6092                          ;; If there was no message-id, we just fake one
6093                          ;; to make subsequent routines simpler.
6094                          (nnheader-generate-fake-message-id number))))
6095             ;; References.
6096             (progn
6097               (goto-char p)
6098               (if (search-forward "\nreferences:" nil t)
6099                   (progn
6100                     (setq end (point))
6101                     (prog1
6102                         (nnheader-header-value)
6103                       (setq ref
6104                             (buffer-substring
6105                              (progn
6106                                (end-of-line)
6107                                (search-backward ">" end t)
6108                                (1+ (point)))
6109                              (progn
6110                                (search-backward "<" end t)
6111                                (point))))))
6112                 ;; Get the references from the in-reply-to header if there
6113                 ;; were no references and the in-reply-to header looks
6114                 ;; promising.
6115                 (if (and (search-forward "\nin-reply-to:" nil t)
6116                          (setq in-reply-to (nnheader-header-value))
6117                          (string-match "<[^>]+>" in-reply-to))
6118                     (let (ref2)
6119                       (setq ref (substring in-reply-to (match-beginning 0)
6120                                            (match-end 0)))
6121                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6122                         (setq ref2 (substring in-reply-to (match-beginning 0)
6123                                               (match-end 0)))
6124                         (when (> (length ref2) (length ref))
6125                           (setq ref ref2)))
6126                       ref)
6127                   (setq ref nil))))
6128             ;; Chars.
6129             (progn
6130               (goto-char p)
6131               (if (search-forward "\nchars: " nil t)
6132                   (if (numberp (setq chars (ignore-errors (read cur))))
6133                       chars -1)
6134                 -1))
6135             ;; Lines.
6136             (progn
6137               (goto-char p)
6138               (if (search-forward "\nlines: " nil t)
6139                   (if (numberp (setq lines (ignore-errors (read cur))))
6140                       lines -1)
6141                 -1))
6142             ;; Xref.
6143             (progn
6144               (goto-char p)
6145               (and (search-forward "\nxref:" nil t)
6146                    (nnheader-header-value)))
6147             ;; Extra.
6148             (when gnus-extra-headers
6149               (let ((extra gnus-extra-headers)
6150                     out)
6151                 (while extra
6152                   (goto-char p)
6153                   (when (search-forward
6154                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6155                     (push (cons (car extra) (nnheader-header-value))
6156                           out))
6157                   (pop extra))
6158                 out))))
6159           (when (equal id ref)
6160             (setq ref nil))
6161
6162           (when gnus-alter-header-function
6163             (funcall gnus-alter-header-function header)
6164             (setq id (mail-header-id header)
6165                   ref (gnus-parent-id (mail-header-references header))))
6166
6167           (when (setq header
6168                       (gnus-dependencies-add-header
6169                        header dependencies force-new))
6170             (push header headers))
6171           (goto-char (point-max))
6172           (widen))
6173         (nreverse headers)))))
6174
6175 ;; Goes through the xover lines and returns a list of vectors
6176 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6177                                                   force-new dependencies
6178                                                   group also-fetch-heads)
6179   "Parse the news overview data in the server buffer.
6180 Return a list of headers that match SEQUENCE (see
6181 `nntp-retrieve-headers')."
6182   ;; Get the Xref when the users reads the articles since most/some
6183   ;; NNTP servers do not include Xrefs when using XOVER.
6184   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6185   (let ((mail-parse-charset gnus-newsgroup-charset)
6186         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6187         (cur nntp-server-buffer)
6188         (dependencies (or dependencies gnus-newsgroup-dependencies))
6189         (allp (cond
6190                ((eq gnus-read-all-available-headers t)
6191                 t)
6192                ((and (stringp gnus-read-all-available-headers)
6193                      group)
6194                 (string-match gnus-read-all-available-headers group))
6195                (t
6196                 nil)))
6197         number headers header)
6198     (save-excursion
6199       (set-buffer nntp-server-buffer)
6200       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6201       ;; Allow the user to mangle the headers before parsing them.
6202       (gnus-run-hooks 'gnus-parse-headers-hook)
6203       (goto-char (point-min))
6204       (gnus-parse-without-error
6205         (while (and (or sequence allp)
6206                     (not (eobp)))
6207           (setq number (read cur))
6208           (when (not allp)
6209             (while (and sequence
6210                         (< (car sequence) number))
6211               (setq sequence (cdr sequence))))
6212           (when (and (or allp
6213                          (and sequence
6214                               (eq number (car sequence))))
6215                      (progn
6216                        (setq sequence (cdr sequence))
6217                        (setq header (inline
6218                                       (gnus-nov-parse-line
6219                                        number dependencies force-new)))))
6220             (push header headers))
6221           (forward-line 1)))
6222       ;; A common bug in inn is that if you have posted an article and
6223       ;; then retrieves the active file, it will answer correctly --
6224       ;; the new article is included.  However, a NOV entry for the
6225       ;; article may not have been generated yet, so this may fail.
6226       ;; We work around this problem by retrieving the last few
6227       ;; headers using HEAD.
6228       (if (or (not also-fetch-heads)
6229               (not sequence))
6230           ;; We (probably) got all the headers.
6231           (nreverse headers)
6232         (let ((gnus-nov-is-evil t))
6233           (nconc
6234            (nreverse headers)
6235            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6236              (gnus-get-newsgroup-headers))))))))
6237
6238 (defun gnus-article-get-xrefs ()
6239   "Fill in the Xref value in `gnus-current-headers', if necessary.
6240 This is meant to be called in `gnus-article-internal-prepare-hook'."
6241   (let ((headers (with-current-buffer gnus-summary-buffer
6242                    gnus-current-headers)))
6243     (or (not gnus-use-cross-reference)
6244         (not headers)
6245         (and (mail-header-xref headers)
6246              (not (string= (mail-header-xref headers) "")))
6247         (let ((case-fold-search t)
6248               xref)
6249           (save-restriction
6250             (nnheader-narrow-to-headers)
6251             (goto-char (point-min))
6252             (when (or (and (not (eobp))
6253                            (eq (downcase (char-after)) ?x)
6254                            (looking-at "Xref:"))
6255                       (search-forward "\nXref:" nil t))
6256               (goto-char (1+ (match-end 0)))
6257               (setq xref (buffer-substring (point) (point-at-eol)))
6258               (mail-header-set-xref headers xref)))))))
6259
6260 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6261   "Find article ID and insert the summary line for that article.
6262 OLD-HEADER can either be a header or a line number to insert
6263 the subject line on."
6264   (let* ((line (and (numberp old-header) old-header))
6265          (old-header (and (vectorp old-header) old-header))
6266          (header (cond ((and old-header use-old-header)
6267                         old-header)
6268                        ((and (numberp id)
6269                              (gnus-number-to-header id))
6270                         (gnus-number-to-header id))
6271                        (t
6272                         (gnus-read-header id))))
6273          (number (and (numberp id) id))
6274          d)
6275     (when header
6276       ;; Rebuild the thread that this article is part of and go to the
6277       ;; article we have fetched.
6278       (when (and (not gnus-show-threads)
6279                  old-header)
6280         (when (and number
6281                    (setq d (gnus-data-find (mail-header-number old-header))))
6282           (goto-char (gnus-data-pos d))
6283           (gnus-data-remove
6284            number
6285            (- (point-at-bol)
6286               (prog1
6287                   (1+ (point-at-eol))
6288                 (gnus-delete-line))))))
6289       ;; Remove list identifiers from subject.
6290       (when gnus-list-identifiers
6291         (let ((gnus-newsgroup-headers (list header)))
6292           (gnus-summary-remove-list-identifiers)))
6293       (when old-header
6294         (mail-header-set-number header (mail-header-number old-header)))
6295       (setq gnus-newsgroup-sparse
6296             (delq (setq number (mail-header-number header))
6297                   gnus-newsgroup-sparse))
6298       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6299       (push number gnus-newsgroup-limit)
6300       (gnus-rebuild-thread (mail-header-id header) line)
6301       (gnus-summary-goto-subject number nil t))
6302     (when (and (numberp number)
6303                (> number 0))
6304       ;; We have to update the boundaries even if we can't fetch the
6305       ;; article if ID is a number -- so that the next `P' or `N'
6306       ;; command will fetch the previous (or next) article even
6307       ;; if the one we tried to fetch this time has been canceled.
6308       (when (> number gnus-newsgroup-end)
6309         (setq gnus-newsgroup-end number))
6310       (when (< number gnus-newsgroup-begin)
6311         (setq gnus-newsgroup-begin number))
6312       (setq gnus-newsgroup-unselected
6313             (delq number gnus-newsgroup-unselected)))
6314     ;; Report back a success?
6315     (and header (mail-header-number header))))
6316
6317 ;;; Process/prefix in the summary buffer
6318
6319 (defun gnus-summary-work-articles (n)
6320   "Return a list of articles to be worked upon.
6321 The prefix argument, the list of process marked articles, and the
6322 current article will be taken into consideration."
6323   (save-excursion
6324     (set-buffer gnus-summary-buffer)
6325     (cond
6326      (n
6327       ;; A numerical prefix has been given.
6328       (setq n (prefix-numeric-value n))
6329       (let ((backward (< n 0))
6330             (n (abs (prefix-numeric-value n)))
6331             articles article)
6332         (save-excursion
6333           (while
6334               (and (> n 0)
6335                    (push (setq article (gnus-summary-article-number))
6336                          articles)
6337                    (if backward
6338                        (gnus-summary-find-prev nil article)
6339                      (gnus-summary-find-next nil article)))
6340             (decf n)))
6341         (nreverse articles)))
6342      ((and (gnus-region-active-p) (mark))
6343       (message "region active")
6344       ;; Work on the region between point and mark.
6345       (let ((max (max (point) (mark)))
6346             articles article)
6347         (save-excursion
6348           (goto-char (min (point) (mark)))
6349           (while
6350               (and
6351                (push (setq article (gnus-summary-article-number)) articles)
6352                (gnus-summary-find-next nil article)
6353                (< (point) max)))
6354           (nreverse articles))))
6355      (gnus-newsgroup-processable
6356       ;; There are process-marked articles present.
6357       ;; Save current state.
6358       (gnus-summary-save-process-mark)
6359       ;; Return the list.
6360       (reverse gnus-newsgroup-processable))
6361      (t
6362       ;; Just return the current article.
6363       (list (gnus-summary-article-number))))))
6364
6365 (defmacro gnus-summary-iterate (arg &rest forms)
6366   "Iterate over the process/prefixed articles and do FORMS.
6367 ARG is the interactive prefix given to the command.  FORMS will be
6368 executed with point over the summary line of the articles."
6369   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6370     `(let ((,articles (gnus-summary-work-articles ,arg)))
6371        (while ,articles
6372          (gnus-summary-goto-subject (car ,articles))
6373          ,@forms
6374          (pop ,articles)))))
6375
6376 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6377 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6378
6379 (defun gnus-summary-save-process-mark ()
6380   "Push the current set of process marked articles on the stack."
6381   (interactive)
6382   (push (copy-sequence gnus-newsgroup-processable)
6383         gnus-newsgroup-process-stack))
6384
6385 (defun gnus-summary-kill-process-mark ()
6386   "Push the current set of process marked articles on the stack and unmark."
6387   (interactive)
6388   (gnus-summary-save-process-mark)
6389   (gnus-summary-unmark-all-processable))
6390
6391 (defun gnus-summary-yank-process-mark ()
6392   "Pop the last process mark state off the stack and restore it."
6393   (interactive)
6394   (unless gnus-newsgroup-process-stack
6395     (error "Empty mark stack"))
6396   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6397
6398 (defun gnus-summary-process-mark-set (set)
6399   "Make SET into the current process marked articles."
6400   (gnus-summary-unmark-all-processable)
6401   (mapc 'gnus-summary-set-process-mark set))
6402
6403 ;;; Searching and stuff
6404
6405 (defun gnus-summary-search-group (&optional backward use-level)
6406   "Search for next unread newsgroup.
6407 If optional argument BACKWARD is non-nil, search backward instead."
6408   (save-excursion
6409     (set-buffer gnus-group-buffer)
6410     (when (gnus-group-search-forward
6411            backward nil (if use-level (gnus-group-group-level) nil))
6412       (gnus-group-group-name))))
6413
6414 (defun gnus-summary-best-group (&optional exclude-group)
6415   "Find the name of the best unread group.
6416 If EXCLUDE-GROUP, do not go to this group."
6417   (with-current-buffer gnus-group-buffer
6418     (save-excursion
6419       (gnus-group-best-unread-group exclude-group))))
6420
6421 (defun gnus-summary-find-next (&optional unread article backward)
6422   (if backward
6423       (gnus-summary-find-prev unread article)
6424     (let* ((dummy (gnus-summary-article-intangible-p))
6425            (article (or article (gnus-summary-article-number)))
6426            (data (gnus-data-find-list article))
6427            result)
6428       (when (and (not dummy)
6429                  (or (not gnus-summary-check-current)
6430                      (not unread)
6431                      (not (gnus-data-unread-p (car data)))))
6432         (setq data (cdr data)))
6433       (when (setq result
6434                   (if unread
6435                       (progn
6436                         (while data
6437                           (unless (memq (gnus-data-number (car data))
6438                                         (cond
6439                                          ((eq gnus-auto-goto-ignores
6440                                               'always-undownloaded)
6441                                           gnus-newsgroup-undownloaded)
6442                                          (gnus-plugged
6443                                           nil)
6444                                          ((eq gnus-auto-goto-ignores
6445                                               'unfetched)
6446                                           gnus-newsgroup-unfetched)
6447                                          ((eq gnus-auto-goto-ignores
6448                                               'undownloaded)
6449                                           gnus-newsgroup-undownloaded)))
6450                             (when (gnus-data-unread-p (car data))
6451                               (setq result (car data)
6452                                     data nil)))
6453                           (setq data (cdr data)))
6454                         result)
6455                     (car data)))
6456         (goto-char (gnus-data-pos result))
6457         (gnus-data-number result)))))
6458
6459 (defun gnus-summary-find-prev (&optional unread article)
6460   (let* ((eobp (eobp))
6461          (article (or article (gnus-summary-article-number)))
6462          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6463          result)
6464     (when (and (not eobp)
6465                (or (not gnus-summary-check-current)
6466                    (not unread)
6467                    (not (gnus-data-unread-p (car data)))))
6468       (setq data (cdr data)))
6469     (when (setq result
6470                 (if unread
6471                     (progn
6472                       (while data
6473                         (unless (memq (gnus-data-number (car data))
6474                                       (cond
6475                                        ((eq gnus-auto-goto-ignores
6476                                             'always-undownloaded)
6477                                         gnus-newsgroup-undownloaded)
6478                                        (gnus-plugged
6479                                         nil)
6480                                        ((eq gnus-auto-goto-ignores
6481                                             'unfetched)
6482                                         gnus-newsgroup-unfetched)
6483                                        ((eq gnus-auto-goto-ignores
6484                                             'undownloaded)
6485                                         gnus-newsgroup-undownloaded)))
6486                           (when (gnus-data-unread-p (car data))
6487                             (setq result (car data)
6488                                   data nil)))
6489                         (setq data (cdr data)))
6490                       result)
6491                   (car data)))
6492       (goto-char (gnus-data-pos result))
6493       (gnus-data-number result))))
6494
6495 (defun gnus-summary-find-subject (subject &optional unread backward article)
6496   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6497          (article (or article (gnus-summary-article-number)))
6498          (articles (gnus-data-list backward))
6499          (arts (gnus-data-find-list article articles))
6500          result)
6501     (when (or (not gnus-summary-check-current)
6502               (not unread)
6503               (not (gnus-data-unread-p (car arts))))
6504       (setq arts (cdr arts)))
6505     (while arts
6506       (and (or (not unread)
6507                (gnus-data-unread-p (car arts)))
6508            (vectorp (gnus-data-header (car arts)))
6509            (gnus-subject-equal
6510             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6511            (setq result (car arts)
6512                  arts nil))
6513       (setq arts (cdr arts)))
6514     (and result
6515          (goto-char (gnus-data-pos result))
6516          (gnus-data-number result))))
6517
6518 (defun gnus-summary-search-forward (&optional unread subject backward)
6519   "Search forward for an article.
6520 If UNREAD, look for unread articles.  If SUBJECT, look for
6521 articles with that subject.  If BACKWARD, search backward instead."
6522   (cond (subject (gnus-summary-find-subject subject unread backward))
6523         (backward (gnus-summary-find-prev unread))
6524         (t (gnus-summary-find-next unread))))
6525
6526 (defun gnus-recenter (&optional n)
6527   "Center point in window and redisplay frame.
6528 Also do horizontal recentering."
6529   (interactive "P")
6530   (when (and gnus-auto-center-summary
6531              (not (eq gnus-auto-center-summary 'vertical)))
6532     (gnus-horizontal-recenter))
6533   (recenter n))
6534
6535 (defun gnus-summary-recenter ()
6536   "Center point in the summary window.
6537 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6538 displayed, no centering will be performed."
6539   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6540   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6541   (interactive)
6542   ;; The user has to want it.
6543   (when gnus-auto-center-summary
6544     (let* ((top (cond ((< (window-height) 4) 0)
6545                       ((< (window-height) 7) 1)
6546                       (t (if (numberp gnus-auto-center-summary)
6547                              gnus-auto-center-summary
6548                            (/ (1- (window-height)) 2)))))
6549            (height (1- (window-height)))
6550            (bottom (save-excursion (goto-char (point-max))
6551                                    (forward-line (- height))
6552                                    (point)))
6553            (window (get-buffer-window (current-buffer))))
6554       (when (get-buffer-window gnus-article-buffer)
6555         ;; Only do recentering when the article buffer is displayed,
6556         ;; Set the window start to either `bottom', which is the biggest
6557         ;; possible valid number, or the second line from the top,
6558         ;; whichever is the least.
6559         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6560           (if (> bottom top-pos)
6561               ;; Keep the second line from the top visible
6562               (set-window-start window top-pos t)
6563             ;; Try to keep the bottom line visible; if it's partially
6564             ;; obscured, either scroll one more line to make it fully
6565             ;; visible, or revert to using TOP-POS.
6566             (save-excursion
6567               (goto-char (point-max))
6568               (forward-line -1)
6569               (let ((last-line-start (point)))
6570                 (goto-char bottom)
6571                 (set-window-start window (point) t)
6572                 (when (not (pos-visible-in-window-p last-line-start window))
6573                   (forward-line 1)
6574                   (set-window-start window (min (point) top-pos) t)))))))
6575       ;; Do horizontal recentering while we're at it.
6576       (when (and (get-buffer-window (current-buffer) t)
6577                  (not (eq gnus-auto-center-summary 'vertical)))
6578         (let ((selected (selected-window)))
6579           (select-window (get-buffer-window (current-buffer) t))
6580           (gnus-summary-position-point)
6581           (gnus-horizontal-recenter)
6582           (select-window selected))))))
6583
6584 (defun gnus-summary-jump-to-group (newsgroup)
6585   "Move point to NEWSGROUP in group mode buffer."
6586   ;; Keep update point of group mode buffer if visible.
6587   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6588       (save-window-excursion
6589         ;; Take care of tree window mode.
6590         (when (get-buffer-window gnus-group-buffer)
6591           (pop-to-buffer gnus-group-buffer))
6592         (gnus-group-jump-to-group newsgroup))
6593     (save-excursion
6594       ;; Take care of tree window mode.
6595       (if (get-buffer-window gnus-group-buffer)
6596           (pop-to-buffer gnus-group-buffer)
6597         (set-buffer gnus-group-buffer))
6598       (gnus-group-jump-to-group newsgroup))))
6599
6600 ;; This function returns a list of article numbers based on the
6601 ;; difference between the ranges of read articles in this group and
6602 ;; the range of active articles.
6603 (defun gnus-list-of-unread-articles (group)
6604   (let* ((read (gnus-info-read (gnus-get-info group)))
6605          (active (or (gnus-active group) (gnus-activate-group group)))
6606          (last (or (cdr active)
6607                    (error "Group %s couldn't be activated " group)))
6608          first nlast unread)
6609     ;; If none are read, then all are unread.
6610     (if (not read)
6611         (setq first (car active))
6612       ;; If the range of read articles is a single range, then the
6613       ;; first unread article is the article after the last read
6614       ;; article.  Sounds logical, doesn't it?
6615       (if (and (not (listp (cdr read)))
6616                (or (< (car read) (car active))
6617                    (progn (setq read (list read))
6618                           nil)))
6619           (setq first (max (car active) (1+ (cdr read))))
6620         ;; `read' is a list of ranges.
6621         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6622                                   (caar read)))
6623                   1)
6624           (setq first (car active)))
6625         (while read
6626           (when first
6627             (while (< first nlast)
6628               (setq unread (cons first unread)
6629                     first (1+ first))))
6630           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6631           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6632           (setq read (cdr read)))))
6633     ;; And add the last unread articles.
6634     (while (<= first last)
6635       (setq unread (cons first unread)
6636             first (1+ first)))
6637     ;; Return the list of unread articles.
6638     (delq 0 (nreverse unread))))
6639
6640 (defun gnus-list-of-read-articles (group)
6641   "Return a list of unread, unticked and non-dormant articles."
6642   (let* ((info (gnus-get-info group))
6643          (marked (gnus-info-marks info))
6644          (active (gnus-active group)))
6645     (and info active
6646          (gnus-list-range-difference
6647           (gnus-list-range-difference
6648            (gnus-sorted-complement
6649             (gnus-uncompress-range active)
6650             (gnus-list-of-unread-articles group))
6651            (cdr (assq 'dormant marked)))
6652           (cdr (assq 'tick marked))))))
6653
6654 ;; This function returns a sequence of article numbers based on the
6655 ;; difference between the ranges of read articles in this group and
6656 ;; the range of active articles.
6657 (defun gnus-sequence-of-unread-articles (group)
6658   (let* ((read (gnus-info-read (gnus-get-info group)))
6659          (active (or (gnus-active group) (gnus-activate-group group)))
6660          (last (cdr active))
6661          first nlast unread)
6662     ;; If none are read, then all are unread.
6663     (if (not read)
6664         (setq first (car active))
6665       ;; If the range of read articles is a single range, then the
6666       ;; first unread article is the article after the last read
6667       ;; article.  Sounds logical, doesn't it?
6668       (if (and (not (listp (cdr read)))
6669                (or (< (car read) (car active))
6670                    (progn (setq read (list read))
6671                           nil)))
6672           (setq first (max (car active) (1+ (cdr read))))
6673         ;; `read' is a list of ranges.
6674         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6675                                   (caar read)))
6676                   1)
6677           (setq first (car active)))
6678         (while read
6679           (when first
6680             (push (cons first nlast) unread))
6681           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6682           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6683           (setq read (cdr read)))))
6684     ;; And add the last unread articles.
6685     (cond ((not (and first last))
6686            nil)
6687           ((< first last)
6688            (push (cons first last) unread))
6689           ((= first last)
6690            (push first unread)))
6691     ;; Return the sequence of unread articles.
6692     (delq 0 (nreverse unread))))
6693
6694 ;; Various summary commands
6695
6696 (defun gnus-summary-select-article-buffer ()
6697   "Reconfigure windows to show article buffer."
6698   (interactive)
6699   (if (not (gnus-buffer-live-p gnus-article-buffer))
6700       (error "There is no article buffer for this summary buffer")
6701     (gnus-configure-windows 'article)
6702     (select-window (get-buffer-window gnus-article-buffer))))
6703
6704 (defun gnus-summary-universal-argument (arg)
6705   "Perform any operation on all articles that are process/prefixed."
6706   (interactive "P")
6707   (let ((articles (gnus-summary-work-articles arg))
6708         func article)
6709     (if (eq
6710          (setq
6711           func
6712           (key-binding
6713            (read-key-sequence
6714             (substitute-command-keys
6715              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6716          'undefined)
6717         (gnus-error 1 "Undefined key")
6718       (save-excursion
6719         (while articles
6720           (gnus-summary-goto-subject (setq article (pop articles)))
6721           (let (gnus-newsgroup-processable)
6722             (command-execute func))
6723           (gnus-summary-remove-process-mark article)))))
6724   (gnus-summary-position-point))
6725
6726 (defun gnus-summary-toggle-truncation (&optional arg)
6727   "Toggle truncation of summary lines.
6728 With ARG, turn line truncation on if ARG is positive."
6729   (interactive "P")
6730   (setq truncate-lines
6731         (if (null arg) (not truncate-lines)
6732           (> (prefix-numeric-value arg) 0)))
6733   (redraw-display))
6734
6735 (defun gnus-summary-find-for-reselect ()
6736   "Return the number of an article to stay on across a reselect.
6737 The current article is considered, then following articles, then previous
6738 articles.  An article is sought which is not cancelled and isn't a temporary
6739 insertion from another group.  If there's no such then return a dummy 0."
6740   (let (found)
6741     (dolist (rev '(nil t))
6742       (unless found      ; don't demand the reverse list if we don't need it
6743         (let ((data (gnus-data-find-list
6744                      (gnus-summary-article-number) (gnus-data-list rev))))
6745           (while (and data (not found))
6746             (if (and (< 0 (gnus-data-number (car data)))
6747                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6748                 (setq found (gnus-data-number (car data))))
6749             (setq data (cdr data))))))
6750     (or found 0)))
6751
6752 (defun gnus-summary-reselect-current-group (&optional all rescan)
6753   "Exit and then reselect the current newsgroup.
6754 The prefix argument ALL means to select all articles."
6755   (interactive "P")
6756   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6757     (error "Ephemeral groups can't be reselected"))
6758   (let ((current-subject (gnus-summary-find-for-reselect))
6759         (group gnus-newsgroup-name))
6760     (setq gnus-newsgroup-begin nil)
6761     (gnus-summary-exit nil 'leave-hidden)
6762     ;; We have to adjust the point of group mode buffer because
6763     ;; point was moved to the next unread newsgroup by exiting.
6764     (gnus-summary-jump-to-group group)
6765     (when rescan
6766       (save-excursion
6767         (gnus-group-get-new-news-this-group 1)))
6768     (gnus-group-read-group all t)
6769     (gnus-summary-goto-subject current-subject nil t)))
6770
6771 (defun gnus-summary-rescan-group (&optional all)
6772   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6773   (interactive "P")
6774   (gnus-summary-reselect-current-group all t))
6775
6776 (defun gnus-summary-update-info (&optional non-destructive)
6777   (save-excursion
6778     (let ((group gnus-newsgroup-name))
6779       (when group
6780         (when gnus-newsgroup-kill-headers
6781           (setq gnus-newsgroup-killed
6782                 (gnus-compress-sequence
6783                  (gnus-sorted-union
6784                   (gnus-list-range-intersection
6785                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6786                   gnus-newsgroup-unreads)
6787                  t)))
6788         (unless (listp (cdr gnus-newsgroup-killed))
6789           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6790         (let ((headers gnus-newsgroup-headers))
6791           ;; Set the new ranges of read articles.
6792           (with-current-buffer gnus-group-buffer
6793             (gnus-undo-force-boundary))
6794           (gnus-update-read-articles
6795            group (gnus-sorted-union
6796                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6797           ;; Set the current article marks.
6798           (let ((gnus-newsgroup-scored
6799                  (if (and (not gnus-save-score)
6800                           (not non-destructive))
6801                      nil
6802                    gnus-newsgroup-scored)))
6803             (save-excursion
6804               (gnus-update-marks)))
6805           ;; Do the cross-ref thing.
6806           (when gnus-use-cross-reference
6807             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6808           ;; Do not switch windows but change the buffer to work.
6809           (set-buffer gnus-group-buffer)
6810           (unless (gnus-ephemeral-group-p group)
6811             (gnus-group-update-group group)))))))
6812
6813 (defun gnus-summary-save-newsrc (&optional force)
6814   "Save the current number of read/marked articles in the dribble buffer.
6815 The dribble buffer will then be saved.
6816 If FORCE (the prefix), also save the .newsrc file(s)."
6817   (interactive "P")
6818   (gnus-summary-update-info t)
6819   (if force
6820       (gnus-save-newsrc-file)
6821     (gnus-dribble-save)))
6822
6823 (defun gnus-summary-exit (&optional temporary leave-hidden)
6824   "Exit reading current newsgroup, and then return to group selection mode.
6825 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6826   (interactive)
6827   (gnus-set-global-variables)
6828   (when (gnus-buffer-live-p gnus-article-buffer)
6829     (save-excursion
6830       (set-buffer gnus-article-buffer)
6831       (mm-destroy-parts gnus-article-mime-handles)
6832       ;; Set it to nil for safety reason.
6833       (setq gnus-article-mime-handle-alist nil)
6834       (setq gnus-article-mime-handles nil)))
6835   (gnus-kill-save-kill-buffer)
6836   (gnus-async-halt-prefetch)
6837   (let* ((group gnus-newsgroup-name)
6838          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6839          (gnus-group-is-exiting-p t)
6840          (mode major-mode)
6841          (group-point nil)
6842          (buf (current-buffer)))
6843     (unless quit-config
6844       ;; Do adaptive scoring, and possibly save score files.
6845       (when gnus-newsgroup-adaptive
6846         (gnus-score-adaptive))
6847       (when gnus-use-scoring
6848         (gnus-score-save)))
6849     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6850     ;; If we have several article buffers, we kill them at exit.
6851     (unless gnus-single-article-buffer
6852       (gnus-kill-buffer gnus-article-buffer)
6853       (gnus-kill-buffer gnus-original-article-buffer)
6854       (setq gnus-article-current nil))
6855     (when gnus-use-cache
6856       (gnus-cache-possibly-remove-articles)
6857       (gnus-cache-save-buffers))
6858     (gnus-async-prefetch-remove-group group)
6859     (when gnus-suppress-duplicates
6860       (gnus-dup-enter-articles))
6861     (when gnus-use-trees
6862       (gnus-tree-close group))
6863     (when gnus-use-cache
6864       (gnus-cache-write-active))
6865     ;; Remove entries for this group.
6866     (nnmail-purge-split-history (gnus-group-real-name group))
6867     ;; Make all changes in this group permanent.
6868     (unless quit-config
6869       (gnus-run-hooks 'gnus-exit-group-hook)
6870       (gnus-summary-update-info))
6871     (gnus-close-group group)
6872     ;; Make sure where we were, and go to next newsgroup.
6873     (set-buffer gnus-group-buffer)
6874     (unless quit-config
6875       (gnus-group-jump-to-group group))
6876     (gnus-run-hooks 'gnus-summary-exit-hook)
6877     (unless (or quit-config
6878                 ;; If this group has disappeared from the summary
6879                 ;; buffer, don't skip forwards.
6880                 (not (string= group (gnus-group-group-name))))
6881       (gnus-group-next-unread-group 1))
6882     (setq group-point (point))
6883     (if temporary
6884         nil                             ;Nothing to do.
6885       ;; If we have several article buffers, we kill them at exit.
6886       (unless gnus-single-article-buffer
6887         (gnus-kill-buffer gnus-article-buffer)
6888         (gnus-kill-buffer gnus-original-article-buffer)
6889         (setq gnus-article-current nil))
6890       (set-buffer buf)
6891       (if (not gnus-kill-summary-on-exit)
6892           (progn
6893             (gnus-deaden-summary)
6894             (setq mode nil))
6895         ;; We set all buffer-local variables to nil.  It is unclear why
6896         ;; this is needed, but if we don't, buffer-local variables are
6897         ;; not garbage-collected, it seems.  This would the lead to en
6898         ;; ever-growing Emacs.
6899         (gnus-summary-clear-local-variables)
6900         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6901           (gnus-summary-clear-local-variables))
6902         (when (get-buffer gnus-article-buffer)
6903           (bury-buffer gnus-article-buffer))
6904         ;; Return to group mode buffer.
6905         (when (eq mode 'gnus-summary-mode)
6906           (gnus-kill-buffer buf)))
6907       (setq gnus-current-select-method gnus-select-method)
6908       (set-buffer gnus-group-buffer)
6909       (if quit-config
6910           (gnus-handle-ephemeral-exit quit-config)
6911         (goto-char group-point)
6912         ;; If gnus-group-buffer is already displayed, make sure we also move
6913         ;; the cursor in the window that displays it.
6914         (let ((win (get-buffer-window (current-buffer) 0)))
6915           (if win (set-window-point win (point))))
6916         (unless leave-hidden
6917           (gnus-configure-windows 'group 'force)))
6918       ;; Clear the current group name.
6919       (unless quit-config
6920         (setq gnus-newsgroup-name nil)))))
6921
6922 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6923 (defun gnus-summary-exit-no-update (&optional no-questions)
6924   "Quit reading current newsgroup without updating read article info."
6925   (interactive)
6926   (let* ((group gnus-newsgroup-name)
6927          (gnus-group-is-exiting-p t)
6928          (gnus-group-is-exiting-without-update-p t)
6929          (quit-config (gnus-group-quit-config group)))
6930     (when (or no-questions
6931               gnus-expert-user
6932               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6933       (gnus-async-halt-prefetch)
6934       (run-hooks 'gnus-summary-prepare-exit-hook)
6935       (when (gnus-buffer-live-p gnus-article-buffer)
6936         (save-excursion
6937           (set-buffer gnus-article-buffer)
6938           (mm-destroy-parts gnus-article-mime-handles)
6939           ;; Set it to nil for safety reason.
6940           (setq gnus-article-mime-handle-alist nil)
6941           (setq gnus-article-mime-handles nil)))
6942       ;; If we have several article buffers, we kill them at exit.
6943       (unless gnus-single-article-buffer
6944         (gnus-kill-buffer gnus-article-buffer)
6945         (gnus-kill-buffer gnus-original-article-buffer)
6946         (setq gnus-article-current nil))
6947       (if (not gnus-kill-summary-on-exit)
6948           (gnus-deaden-summary)
6949         (gnus-close-group group)
6950         (gnus-summary-clear-local-variables)
6951         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6952           (gnus-summary-clear-local-variables))
6953         (gnus-kill-buffer gnus-summary-buffer))
6954       (unless gnus-single-article-buffer
6955         (setq gnus-article-current nil))
6956       (when gnus-use-trees
6957         (gnus-tree-close group))
6958       (gnus-async-prefetch-remove-group group)
6959       (when (get-buffer gnus-article-buffer)
6960         (bury-buffer gnus-article-buffer))
6961       ;; Return to the group buffer.
6962       (gnus-configure-windows 'group 'force)
6963       ;; Clear the current group name.
6964       (setq gnus-newsgroup-name nil)
6965       (unless (gnus-ephemeral-group-p group)
6966         (gnus-group-update-group group))
6967       (when (equal (gnus-group-group-name) group)
6968         (gnus-group-next-unread-group 1))
6969       (when quit-config
6970         (gnus-handle-ephemeral-exit quit-config)))))
6971
6972 (defun gnus-handle-ephemeral-exit (quit-config)
6973   "Handle movement when leaving an ephemeral group.
6974 The state which existed when entering the ephemeral is reset."
6975   (if (not (buffer-name (car quit-config)))
6976       (gnus-configure-windows 'group 'force)
6977     (set-buffer (car quit-config))
6978     (cond ((eq major-mode 'gnus-summary-mode)
6979            (gnus-set-global-variables))
6980           ((eq major-mode 'gnus-article-mode)
6981            (save-excursion
6982              ;; The `gnus-summary-buffer' variable may point
6983              ;; to the old summary buffer when using a single
6984              ;; article buffer.
6985              (unless (gnus-buffer-live-p gnus-summary-buffer)
6986                (set-buffer gnus-group-buffer))
6987              (set-buffer gnus-summary-buffer)
6988              (gnus-set-global-variables))))
6989     (if (or (eq (cdr quit-config) 'article)
6990             (eq (cdr quit-config) 'pick))
6991         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6992             (gnus-configure-windows 'pick 'force)
6993           (gnus-configure-windows (cdr quit-config) 'force))
6994       (gnus-configure-windows (cdr quit-config) 'force))
6995     (when (eq major-mode 'gnus-summary-mode)
6996       (gnus-summary-recenter)
6997       (gnus-summary-position-point))))
6998
6999 ;;; Dead summaries.
7000
7001 (defvar gnus-dead-summary-mode-map nil)
7002
7003 (unless gnus-dead-summary-mode-map
7004   (setq gnus-dead-summary-mode-map (make-keymap))
7005   (suppress-keymap gnus-dead-summary-mode-map)
7006   (substitute-key-definition
7007    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7008   (dolist (key '("\C-d" "\r" "\177" [delete]))
7009     (define-key gnus-dead-summary-mode-map
7010       key 'gnus-summary-wake-up-the-dead))
7011   (dolist (key '("q" "Q"))
7012     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7013
7014 (defvar gnus-dead-summary-mode nil
7015   "Minor mode for Gnus summary buffers.")
7016
7017 (defun gnus-dead-summary-mode (&optional arg)
7018   "Minor mode for Gnus summary buffers."
7019   (interactive "P")
7020   (when (eq major-mode 'gnus-summary-mode)
7021     (make-local-variable 'gnus-dead-summary-mode)
7022     (setq gnus-dead-summary-mode
7023           (if (null arg) (not gnus-dead-summary-mode)
7024             (> (prefix-numeric-value arg) 0)))
7025     (when gnus-dead-summary-mode
7026       (add-minor-mode
7027        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7028
7029 (defun gnus-deaden-summary ()
7030   "Make the current summary buffer into a dead summary buffer."
7031   ;; Kill any previous dead summary buffer.
7032   (when (and gnus-dead-summary
7033              (buffer-name gnus-dead-summary))
7034     (with-current-buffer gnus-dead-summary
7035       (when gnus-dead-summary-mode
7036         (kill-buffer (current-buffer)))))
7037   ;; Make this the current dead summary.
7038   (setq gnus-dead-summary (current-buffer))
7039   (gnus-dead-summary-mode 1)
7040   (let ((name (buffer-name)))
7041     (when (string-match "Summary" name)
7042       (rename-buffer
7043        (concat (substring name 0 (match-beginning 0)) "Dead "
7044                (substring name (match-beginning 0)))
7045        t)
7046       (bury-buffer))))
7047
7048 (defun gnus-kill-or-deaden-summary (buffer)
7049   "Kill or deaden the summary BUFFER."
7050   (save-excursion
7051     (when (and (buffer-name buffer)
7052                (not gnus-single-article-buffer))
7053       (with-current-buffer buffer
7054         (gnus-kill-buffer gnus-article-buffer)
7055         (gnus-kill-buffer gnus-original-article-buffer)))
7056     (cond
7057      ;; Kill the buffer.
7058      (gnus-kill-summary-on-exit
7059       (when (and gnus-use-trees
7060                  (gnus-buffer-exists-p buffer))
7061         (save-excursion
7062           (set-buffer buffer)
7063           (gnus-tree-close gnus-newsgroup-name)))
7064       (gnus-kill-buffer buffer))
7065      ;; Deaden the buffer.
7066      ((gnus-buffer-exists-p buffer)
7067       (save-excursion
7068         (set-buffer buffer)
7069         (gnus-deaden-summary))))))
7070
7071 (defun gnus-summary-wake-up-the-dead (&rest args)
7072   "Wake up the dead summary buffer."
7073   (interactive)
7074   (gnus-dead-summary-mode -1)
7075   (let ((name (buffer-name)))
7076     (when (string-match "Dead " name)
7077       (rename-buffer
7078        (concat (substring name 0 (match-beginning 0))
7079                (substring name (match-end 0)))
7080        t)))
7081   (gnus-message 3 "This dead summary is now alive again"))
7082
7083 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7084 (defun gnus-summary-fetch-faq (&optional faq-dir)
7085   "Fetch the FAQ for the current group.
7086 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7087 in."
7088   (interactive
7089    (list
7090     (when current-prefix-arg
7091       (completing-read
7092        "FAQ dir: " (and (listp gnus-group-faq-directory)
7093                         (mapcar 'list
7094                                 gnus-group-faq-directory))))))
7095   (let (gnus-faq-buffer)
7096     (when (setq gnus-faq-buffer
7097                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7098       (gnus-configure-windows 'summary-faq))))
7099
7100 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7101 (defun gnus-summary-describe-group (&optional force)
7102   "Describe the current newsgroup."
7103   (interactive "P")
7104   (gnus-group-describe-group force gnus-newsgroup-name))
7105
7106 (defun gnus-summary-describe-briefly ()
7107   "Describe summary mode commands briefly."
7108   (interactive)
7109   (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")))
7110
7111 ;; Walking around group mode buffer from summary mode.
7112
7113 (defun gnus-summary-next-group (&optional no-article target-group backward)
7114   "Exit current newsgroup and then select next unread newsgroup.
7115 If prefix argument NO-ARTICLE is non-nil, no article is selected
7116 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7117 previous group instead."
7118   (interactive "P")
7119   ;; Stop pre-fetching.
7120   (gnus-async-halt-prefetch)
7121   (let ((current-group gnus-newsgroup-name)
7122         (current-buffer (current-buffer))
7123         entered)
7124     ;; First we semi-exit this group to update Xrefs and all variables.
7125     ;; We can't do a real exit, because the window conf must remain
7126     ;; the same in case the user is prompted for info, and we don't
7127     ;; want the window conf to change before that...
7128     (gnus-summary-exit t)
7129     (while (not entered)
7130       ;; Then we find what group we are supposed to enter.
7131       (set-buffer gnus-group-buffer)
7132       (gnus-group-jump-to-group current-group)
7133       (setq target-group
7134             (or target-group
7135                 (if (eq gnus-keep-same-level 'best)
7136                     (gnus-summary-best-group gnus-newsgroup-name)
7137                   (gnus-summary-search-group backward gnus-keep-same-level))))
7138       (if (not target-group)
7139           ;; There are no further groups, so we return to the group
7140           ;; buffer.
7141           (progn
7142             (gnus-message 5 "Returning to the group buffer")
7143             (setq entered t)
7144             (when (gnus-buffer-live-p current-buffer)
7145               (set-buffer current-buffer)
7146               (gnus-summary-exit))
7147             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7148         ;; We try to enter the target group.
7149         (gnus-group-jump-to-group target-group)
7150         (let ((unreads (gnus-group-group-unread)))
7151           (if (and (or (eq t unreads)
7152                        (and unreads (not (zerop unreads))))
7153                    (gnus-summary-read-group
7154                     target-group nil no-article
7155                     (and (buffer-name current-buffer) current-buffer)
7156                     nil backward))
7157               (setq entered t)
7158             (setq current-group target-group
7159                   target-group nil)))))))
7160
7161 (defun gnus-summary-prev-group (&optional no-article)
7162   "Exit current newsgroup and then select previous unread newsgroup.
7163 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7164   (interactive "P")
7165   (gnus-summary-next-group no-article nil t))
7166
7167 ;; Walking around summary lines.
7168
7169 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7170   "Go to the first subject satisfying any non-nil constraint.
7171 If UNREAD is non-nil, the article should be unread.
7172 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7173 If UNSEEN is non-nil, the article should be unseen.
7174 Returns the article selected or nil if there are no matching articles."
7175   (interactive "P")
7176   (cond
7177    ;; Empty summary.
7178    ((null gnus-newsgroup-data)
7179     (gnus-message 3 "No articles in the group")
7180     nil)
7181    ;; Pick the first article.
7182    ((not (or unread undownloaded unseen))
7183     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7184     (gnus-data-number (car gnus-newsgroup-data)))
7185    ;; Find the first unread article.
7186    (t
7187     (let ((data gnus-newsgroup-data))
7188       (while (and data
7189                   (let ((num (gnus-data-number (car data))))
7190                     (or (memq num gnus-newsgroup-unfetched)
7191                         (not (or (and unread
7192                                       (memq num gnus-newsgroup-unreads))
7193                                  (and undownloaded
7194                                       (memq num gnus-newsgroup-undownloaded))
7195                                  (and unseen
7196                                       (memq num gnus-newsgroup-unseen)))))))
7197         (setq data (cdr data)))
7198       (prog1
7199           (if data
7200               (progn
7201                 (goto-char (gnus-data-pos (car data)))
7202                 (gnus-data-number (car data)))
7203             (gnus-message 3 "No more%s articles"
7204                           (let* ((r (when unread " unread"))
7205                                  (d (when undownloaded " undownloaded"))
7206                                  (s (when unseen " unseen"))
7207                                  (l (delq nil (list r d s))))
7208                             (cond ((= 3 (length l))
7209                                    (concat r "," d ", or" s))
7210                                   ((= 2 (length l))
7211                                    (concat (car l) ", or" (cadr l)))
7212                                   ((= 1 (length l))
7213                                    (car l))
7214                                   (t
7215                                    ""))))
7216             nil
7217             )
7218         (gnus-summary-position-point))))))
7219
7220 (defun gnus-summary-next-subject (n &optional unread dont-display)
7221   "Go to next N'th summary line.
7222 If N is negative, go to the previous N'th subject line.
7223 If UNREAD is non-nil, only unread articles are selected.
7224 The difference between N and the actual number of steps taken is
7225 returned."
7226   (interactive "p")
7227   (let ((backward (< n 0))
7228         (n (abs n)))
7229     (while (and (> n 0)
7230                 (if backward
7231                     (gnus-summary-find-prev unread)
7232                   (gnus-summary-find-next unread)))
7233       (unless (zerop (setq n (1- n)))
7234         (gnus-summary-show-thread)))
7235     (when (/= 0 n)
7236       (gnus-message 7 "No more%s articles"
7237                     (if unread " unread" "")))
7238     (unless dont-display
7239       (gnus-summary-recenter)
7240       (gnus-summary-position-point))
7241     n))
7242
7243 (defun gnus-summary-next-unread-subject (n)
7244   "Go to next N'th unread summary line."
7245   (interactive "p")
7246   (gnus-summary-next-subject n t))
7247
7248 (defun gnus-summary-prev-subject (n &optional unread)
7249   "Go to previous N'th summary line.
7250 If optional argument UNREAD is non-nil, only unread article is selected."
7251   (interactive "p")
7252   (gnus-summary-next-subject (- n) unread))
7253
7254 (defun gnus-summary-prev-unread-subject (n)
7255   "Go to previous N'th unread summary line."
7256   (interactive "p")
7257   (gnus-summary-next-subject (- n) t))
7258
7259 (defun gnus-summary-goto-subjects (articles)
7260   "Insert the subject header for ARTICLES in the current buffer."
7261   (save-excursion
7262     (dolist (article articles)
7263       (gnus-summary-goto-subject article t)))
7264   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7265   (gnus-summary-position-point))
7266
7267 (defun gnus-summary-goto-subject (article &optional force silent)
7268   "Go the subject line of ARTICLE.
7269 If FORCE, also allow jumping to articles not currently shown."
7270   (interactive "nArticle number: ")
7271   (unless (numberp article)
7272     (error "Article %s is not a number" article))
7273   (let ((b (point))
7274         (data (gnus-data-find article)))
7275     ;; We read in the article if we have to.
7276     (and (not data)
7277          force
7278          (gnus-summary-insert-subject
7279           article
7280           (if (or (numberp force) (vectorp force)) force)
7281           t)
7282          (setq data (gnus-data-find article)))
7283     (goto-char b)
7284     (if (not data)
7285         (progn
7286           (unless silent
7287             (gnus-message 3 "Can't find article %d" article))
7288           nil)
7289       (let ((pt (gnus-data-pos data)))
7290         (goto-char pt)
7291         (gnus-summary-set-article-display-arrow pt))
7292       (gnus-summary-position-point)
7293       article)))
7294
7295 ;; Walking around summary lines with displaying articles.
7296
7297 (defun gnus-summary-expand-window (&optional arg)
7298   "Make the summary buffer take up the entire Emacs frame.
7299 Given a prefix, will force an `article' buffer configuration."
7300   (interactive "P")
7301   (if arg
7302       (gnus-configure-windows 'article 'force)
7303     (gnus-configure-windows 'summary 'force)))
7304
7305 (defun gnus-summary-display-article (article &optional all-header)
7306   "Display ARTICLE in article buffer."
7307   (when (gnus-buffer-live-p gnus-article-buffer)
7308     (with-current-buffer gnus-article-buffer
7309       (mm-enable-multibyte)))
7310   (gnus-set-global-variables)
7311   (when (gnus-buffer-live-p gnus-article-buffer)
7312     (with-current-buffer gnus-article-buffer
7313       (setq gnus-article-charset gnus-newsgroup-charset)
7314       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7315       (mm-enable-multibyte)))
7316   (if (null article)
7317       nil
7318     (prog1
7319         (if gnus-summary-display-article-function
7320             (funcall gnus-summary-display-article-function article all-header)
7321           (gnus-article-prepare article all-header))
7322       (gnus-run-hooks 'gnus-select-article-hook)
7323       (when (and gnus-current-article
7324                  (not (zerop gnus-current-article)))
7325         (gnus-summary-goto-subject gnus-current-article))
7326       (gnus-summary-recenter)
7327       (when (and gnus-use-trees gnus-show-threads)
7328         (gnus-possibly-generate-tree article)
7329         (gnus-highlight-selected-tree article))
7330       ;; Successfully display article.
7331       (gnus-article-set-window-start
7332        (cdr (assq article gnus-newsgroup-bookmarks))))))
7333
7334 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7335   "Select the current article.
7336 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7337 non-nil, the article will be re-fetched even if it already present in
7338 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7339 be displayed."
7340   ;; Make sure we are in the summary buffer to work around bbdb bug.
7341   (unless (eq major-mode 'gnus-summary-mode)
7342     (set-buffer gnus-summary-buffer))
7343   (let ((article (or article (gnus-summary-article-number)))
7344         (all-headers (not (not all-headers))) ;Must be t or nil.
7345         gnus-summary-display-article-function)
7346     (and (not pseudo)
7347          (gnus-summary-article-pseudo-p article)
7348          (error "This is a pseudo-article"))
7349     (save-excursion
7350       (set-buffer gnus-summary-buffer)
7351       (if (or (and gnus-single-article-buffer
7352                    (or (null gnus-current-article)
7353                        (null gnus-article-current)
7354                        (null (get-buffer gnus-article-buffer))
7355                        (not (eq article (cdr gnus-article-current)))
7356                        (not (equal (car gnus-article-current)
7357                                    gnus-newsgroup-name))))
7358               (and (not gnus-single-article-buffer)
7359                    (or (null gnus-current-article)
7360                        (not (eq gnus-current-article article))))
7361               force)
7362           ;; The requested article is different from the current article.
7363           (progn
7364             (gnus-summary-display-article article all-headers)
7365             (when (gnus-buffer-live-p gnus-article-buffer)
7366               (with-current-buffer gnus-article-buffer
7367                 (if (not gnus-article-decoded-p) ;; a local variable
7368                     (mm-disable-multibyte))))
7369             (gnus-article-set-window-start
7370              (cdr (assq article gnus-newsgroup-bookmarks)))
7371             article)
7372         'old))))
7373
7374 (defun gnus-summary-force-verify-and-decrypt ()
7375   "Display buttons for signed/encrypted parts and verify/decrypt them."
7376   (interactive)
7377   (let ((mm-verify-option 'known)
7378         (mm-decrypt-option 'known)
7379         (gnus-article-emulate-mime t)
7380         (gnus-buttonized-mime-types (append (list "multipart/signed"
7381                                                   "multipart/encrypted")
7382                                             gnus-buttonized-mime-types)))
7383     (gnus-summary-select-article nil 'force)))
7384
7385 (defun gnus-summary-set-current-mark (&optional current-mark)
7386   "Obsolete function."
7387   nil)
7388
7389 (defun gnus-summary-next-article (&optional unread subject backward push)
7390   "Select the next article.
7391 If UNREAD, only unread articles are selected.
7392 If SUBJECT, only articles with SUBJECT are selected.
7393 If BACKWARD, the previous article is selected instead of the next."
7394   (interactive "P")
7395   (cond
7396    ;; Is there such an article?
7397    ((and (gnus-summary-search-forward unread subject backward)
7398          (or (gnus-summary-display-article (gnus-summary-article-number))
7399              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7400     (gnus-summary-position-point))
7401    ;; If not, we try the first unread, if that is wanted.
7402    ((and subject
7403          gnus-auto-select-same
7404          (gnus-summary-first-unread-article))
7405     (gnus-summary-position-point)
7406     (gnus-message 6 "Wrapped"))
7407    ;; Try to get next/previous article not displayed in this group.
7408    ((and gnus-auto-extend-newsgroup
7409          (not unread) (not subject))
7410     (gnus-summary-goto-article
7411      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7412      nil (count-lines (point-min) (point))))
7413    ;; Go to next/previous group.
7414    (t
7415     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7416       (gnus-summary-jump-to-group gnus-newsgroup-name))
7417     (let ((cmd last-command-char)
7418           (point
7419            (with-current-buffer gnus-group-buffer
7420              (point)))
7421           (group
7422            (if (eq gnus-keep-same-level 'best)
7423                (gnus-summary-best-group gnus-newsgroup-name)
7424              (gnus-summary-search-group backward gnus-keep-same-level))))
7425       ;; For some reason, the group window gets selected.  We change
7426       ;; it back.
7427       (select-window (get-buffer-window (current-buffer)))
7428       ;; Select next unread newsgroup automagically.
7429       (cond
7430        ((or (not gnus-auto-select-next)
7431             (not cmd))
7432         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7433        ((or (eq gnus-auto-select-next 'quietly)
7434             (and (eq gnus-auto-select-next 'slightly-quietly)
7435                  push)
7436             (and (eq gnus-auto-select-next 'almost-quietly)
7437                  (gnus-summary-last-article-p)))
7438         ;; Select quietly.
7439         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7440             (gnus-summary-exit)
7441           (gnus-message 7 "No more%s articles (%s)..."
7442                         (if unread " unread" "")
7443                         (if group (concat "selecting " group)
7444                           "exiting"))
7445           (gnus-summary-next-group nil group backward)))
7446        (t
7447         (when (gnus-key-press-event-p last-input-event)
7448           (gnus-summary-walk-group-buffer
7449            gnus-newsgroup-name cmd unread backward point))))))))
7450
7451 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7452   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7453                       (?\C-p (gnus-group-prev-unread-group 1))))
7454         (cursor-in-echo-area t)
7455         keve key group ended prompt)
7456     (save-excursion
7457       (set-buffer gnus-group-buffer)
7458       (goto-char start)
7459       (setq group
7460             (if (eq gnus-keep-same-level 'best)
7461                 (gnus-summary-best-group gnus-newsgroup-name)
7462               (gnus-summary-search-group backward gnus-keep-same-level))))
7463     (while (not ended)
7464       (setq prompt
7465             (format
7466              "No more%s articles%s " (if unread " unread" "")
7467              (if (and group
7468                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7469                  (format " (Type %s for %s [%s])"
7470                          (single-key-description cmd)
7471                          (gnus-group-decoded-name group)
7472                          (gnus-group-unread group))
7473                (format " (Type %s to exit %s)"
7474                        (single-key-description cmd)
7475                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7476       ;; Confirm auto selection.
7477       (setq key (car (setq keve (gnus-read-event-char prompt)))
7478             ended t)
7479       (cond
7480        ((assq key keystrokes)
7481         (let ((obuf (current-buffer)))
7482           (switch-to-buffer gnus-group-buffer)
7483           (when group
7484             (gnus-group-jump-to-group group))
7485           (eval (cadr (assq key keystrokes)))
7486           (setq group (gnus-group-group-name))
7487           (switch-to-buffer obuf))
7488         (setq ended nil))
7489        ((equal key cmd)
7490         (if (or (not group)
7491                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7492             (gnus-summary-exit)
7493           (gnus-summary-next-group nil group backward)))
7494        (t
7495         (push (cdr keve) unread-command-events))))))
7496
7497 (defun gnus-summary-next-unread-article ()
7498   "Select unread article after current one."
7499   (interactive)
7500   (gnus-summary-next-article
7501    (or (not (eq gnus-summary-goto-unread 'never))
7502        (gnus-summary-last-article-p (gnus-summary-article-number)))
7503    (and gnus-auto-select-same
7504         (gnus-summary-article-subject))))
7505
7506 (defun gnus-summary-prev-article (&optional unread subject)
7507   "Select the article after the current one.
7508 If UNREAD is non-nil, only unread articles are selected."
7509   (interactive "P")
7510   (gnus-summary-next-article unread subject t))
7511
7512 (defun gnus-summary-prev-unread-article ()
7513   "Select unread article before current one."
7514   (interactive)
7515   (gnus-summary-prev-article
7516    (or (not (eq gnus-summary-goto-unread 'never))
7517        (gnus-summary-first-article-p (gnus-summary-article-number)))
7518    (and gnus-auto-select-same
7519         (gnus-summary-article-subject))))
7520
7521 (defun gnus-summary-next-page (&optional lines circular stop)
7522   "Show next page of the selected article.
7523 If at the end of the current article, select the next article.
7524 LINES says how many lines should be scrolled up.
7525
7526 If CIRCULAR is non-nil, go to the start of the article instead of
7527 selecting the next article when reaching the end of the current
7528 article.
7529
7530 If STOP is non-nil, just stop when reaching the end of the message.
7531
7532 Also see the variable `gnus-article-skip-boring'."
7533   (interactive "P")
7534   (setq gnus-summary-buffer (current-buffer))
7535   (gnus-set-global-variables)
7536   (let ((article (gnus-summary-article-number))
7537         (article-window (get-buffer-window gnus-article-buffer t))
7538         endp)
7539     ;; If the buffer is empty, we have no article.
7540     (unless article
7541       (error "No article to select"))
7542     (gnus-configure-windows 'article)
7543     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7544         (if (and (eq gnus-summary-goto-unread 'never)
7545                  (not (gnus-summary-last-article-p article)))
7546             (gnus-summary-next-article)
7547           (gnus-summary-next-unread-article))
7548       (if (or (null gnus-current-article)
7549               (null gnus-article-current)
7550               (/= article (cdr gnus-article-current))
7551               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7552           ;; Selected subject is different from current article's.
7553           (gnus-summary-display-article article)
7554         (when article-window
7555           (gnus-eval-in-buffer-window gnus-article-buffer
7556             (setq endp (or (gnus-article-next-page lines)
7557                            (gnus-article-only-boring-p))))
7558           (when endp
7559             (cond (stop
7560                    (gnus-message 3 "End of message"))
7561                   (circular
7562                    (gnus-summary-beginning-of-article))
7563                   (lines
7564                    (gnus-message 3 "End of message"))
7565                   ((null lines)
7566                    (if (and (eq gnus-summary-goto-unread 'never)
7567                             (not (gnus-summary-last-article-p article)))
7568                        (gnus-summary-next-article)
7569                      (gnus-summary-next-unread-article))))))))
7570     (gnus-summary-recenter)
7571     (gnus-summary-position-point)))
7572
7573 (defun gnus-summary-prev-page (&optional lines move)
7574   "Show previous page of selected article.
7575 Argument LINES specifies lines to be scrolled down.
7576 If MOVE, move to the previous unread article if point is at
7577 the beginning of the buffer."
7578   (interactive "P")
7579   (let ((article (gnus-summary-article-number))
7580         (article-window (get-buffer-window gnus-article-buffer t))
7581         endp)
7582     (gnus-configure-windows 'article)
7583     (if (or (null gnus-current-article)
7584             (null gnus-article-current)
7585             (/= article (cdr gnus-article-current))
7586             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7587         ;; Selected subject is different from current article's.
7588         (gnus-summary-display-article article)
7589       (gnus-summary-recenter)
7590       (when article-window
7591         (gnus-eval-in-buffer-window gnus-article-buffer
7592           (setq endp (gnus-article-prev-page lines)))
7593         (when (and move endp)
7594           (cond (lines
7595                  (gnus-message 3 "Beginning of message"))
7596                 ((null lines)
7597                  (if (and (eq gnus-summary-goto-unread 'never)
7598                           (not (gnus-summary-first-article-p article)))
7599                      (gnus-summary-prev-article)
7600                    (gnus-summary-prev-unread-article))))))))
7601   (gnus-summary-position-point))
7602
7603 (defun gnus-summary-prev-page-or-article (&optional lines)
7604   "Show previous page of selected article.
7605 Argument LINES specifies lines to be scrolled down.
7606 If at the beginning of the article, go to the next article."
7607   (interactive "P")
7608   (gnus-summary-prev-page lines t))
7609
7610 (defun gnus-summary-scroll-up (lines)
7611   "Scroll up (or down) one line current article.
7612 Argument LINES specifies lines to be scrolled up (or down if negative)."
7613   (interactive "p")
7614   (gnus-configure-windows 'article)
7615   (gnus-summary-show-thread)
7616   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7617     (gnus-eval-in-buffer-window gnus-article-buffer
7618       (cond ((> lines 0)
7619              (when (gnus-article-next-page lines)
7620                (gnus-message 3 "End of message")))
7621             ((< lines 0)
7622              (gnus-article-prev-page (- lines))))))
7623   (gnus-summary-recenter)
7624   (gnus-summary-position-point))
7625
7626 (defun gnus-summary-scroll-down (lines)
7627   "Scroll down (or up) one line current article.
7628 Argument LINES specifies lines to be scrolled down (or up if negative)."
7629   (interactive "p")
7630   (gnus-summary-scroll-up (- lines)))
7631
7632 (defun gnus-summary-next-same-subject ()
7633   "Select next article which has the same subject as current one."
7634   (interactive)
7635   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7636
7637 (defun gnus-summary-prev-same-subject ()
7638   "Select previous article which has the same subject as current one."
7639   (interactive)
7640   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7641
7642 (defun gnus-summary-next-unread-same-subject ()
7643   "Select next unread article which has the same subject as current one."
7644   (interactive)
7645   (gnus-summary-next-article t (gnus-summary-article-subject)))
7646
7647 (defun gnus-summary-prev-unread-same-subject ()
7648   "Select previous unread article which has the same subject as current one."
7649   (interactive)
7650   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7651
7652 (defun gnus-summary-first-unread-article ()
7653   "Select the first unread article.
7654 Return nil if there are no unread articles."
7655   (interactive)
7656   (prog1
7657       (when (gnus-summary-first-subject t)
7658         (gnus-summary-show-thread)
7659         (gnus-summary-first-subject t)
7660         (gnus-summary-display-article (gnus-summary-article-number)))
7661     (gnus-summary-position-point)))
7662
7663 (defun gnus-summary-first-unread-subject ()
7664   "Place the point on the subject line of the first unread article.
7665 Return nil if there are no unread articles."
7666   (interactive)
7667   (prog1
7668       (when (gnus-summary-first-subject t)
7669         (gnus-summary-show-thread)
7670         (gnus-summary-first-subject t))
7671     (gnus-summary-position-point)))
7672
7673 (defun gnus-summary-first-unseen-subject ()
7674   "Place the point on the subject line of the first unseen article.
7675 Return nil if there are no unseen articles."
7676   (interactive)
7677   (prog1
7678       (when (gnus-summary-first-subject nil nil t)
7679         (gnus-summary-show-thread)
7680         (gnus-summary-first-subject nil nil t))
7681     (gnus-summary-position-point)))
7682
7683 (defun gnus-summary-first-unseen-or-unread-subject ()
7684   "Place the point on the subject line of the first unseen article or,
7685 if all article have been seen, on the subject line of the first unread
7686 article."
7687   (interactive)
7688   (prog1
7689       (unless (when (gnus-summary-first-subject nil nil t)
7690                 (gnus-summary-show-thread)
7691                 (gnus-summary-first-subject nil nil t))
7692         (when (gnus-summary-first-subject t)
7693           (gnus-summary-show-thread)
7694           (gnus-summary-first-subject t)))
7695     (gnus-summary-position-point)))
7696
7697 (defun gnus-summary-first-article ()
7698   "Select the first article.
7699 Return nil if there are no articles."
7700   (interactive)
7701   (prog1
7702       (when (gnus-summary-first-subject)
7703         (gnus-summary-show-thread)
7704         (gnus-summary-first-subject)
7705         (gnus-summary-display-article (gnus-summary-article-number)))
7706     (gnus-summary-position-point)))
7707
7708 (defun gnus-summary-best-unread-article (&optional arg)
7709   "Select the unread article with the highest score.
7710 If given a prefix argument, select the next unread article that has a
7711 score higher than the default score."
7712   (interactive "P")
7713   (let ((article (if arg
7714                      (gnus-summary-better-unread-subject)
7715                    (gnus-summary-best-unread-subject))))
7716     (if article
7717         (gnus-summary-goto-article article)
7718       (error "No unread articles"))))
7719
7720 (defun gnus-summary-best-unread-subject ()
7721   "Select the unread subject with the highest score."
7722   (interactive)
7723   (let ((best -1000000)
7724         (data gnus-newsgroup-data)
7725         article score)
7726     (while data
7727       (and (gnus-data-unread-p (car data))
7728            (> (setq score
7729                     (gnus-summary-article-score (gnus-data-number (car data))))
7730               best)
7731            (setq best score
7732                  article (gnus-data-number (car data))))
7733       (setq data (cdr data)))
7734     (when article
7735       (gnus-summary-goto-subject article))
7736     (gnus-summary-position-point)
7737     article))
7738
7739 (defun gnus-summary-better-unread-subject ()
7740   "Select the first unread subject that has a score over the default score."
7741   (interactive)
7742   (let ((data gnus-newsgroup-data)
7743         article score)
7744     (while (and (setq article (gnus-data-number (car data)))
7745                 (or (gnus-data-read-p (car data))
7746                     (not (> (gnus-summary-article-score article)
7747                             gnus-summary-default-score))))
7748       (setq data (cdr data)))
7749     (when article
7750       (gnus-summary-goto-subject article))
7751     (gnus-summary-position-point)
7752     article))
7753
7754 (defun gnus-summary-last-subject ()
7755   "Go to the last displayed subject line in the group."
7756   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7757     (when article
7758       (gnus-summary-goto-subject article))))
7759
7760 (defun gnus-summary-goto-article (article &optional all-headers force)
7761   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7762 If ALL-HEADERS is non-nil, no header lines are hidden.
7763 If FORCE, go to the article even if it isn't displayed.  If FORCE
7764 is a number, it is the line the article is to be displayed on."
7765   (interactive
7766    (list
7767     (completing-read
7768      "Article number or Message-ID: "
7769      (mapcar (lambda (number) (list (int-to-string number)))
7770              gnus-newsgroup-limit))
7771     current-prefix-arg
7772     t))
7773   (prog1
7774       (if (and (stringp article)
7775                (string-match "@\\|%40" article))
7776           (gnus-summary-refer-article article)
7777         (when (stringp article)
7778           (setq article (string-to-number article)))
7779         (if (gnus-summary-goto-subject article force)
7780             (gnus-summary-display-article article all-headers)
7781           (gnus-message 4 "Couldn't go to article %s" article) nil))
7782     (gnus-summary-position-point)))
7783
7784 (defun gnus-summary-goto-last-article ()
7785   "Go to the previously read article."
7786   (interactive)
7787   (prog1
7788       (when gnus-last-article
7789         (gnus-summary-goto-article gnus-last-article nil t))
7790     (gnus-summary-position-point)))
7791
7792 (defun gnus-summary-pop-article (number)
7793   "Pop one article off the history and go to the previous.
7794 NUMBER articles will be popped off."
7795   (interactive "p")
7796   (let (to)
7797     (setq gnus-newsgroup-history
7798           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7799     (if to
7800         (gnus-summary-goto-article (car to) nil t)
7801       (error "Article history empty")))
7802   (gnus-summary-position-point))
7803
7804 ;; Summary commands and functions for limiting the summary buffer.
7805
7806 (defun gnus-summary-limit-to-articles (n)
7807   "Limit the summary buffer to the next N articles.
7808 If not given a prefix, use the process marked articles instead."
7809   (interactive "P")
7810   (prog1
7811       (let ((articles (gnus-summary-work-articles n)))
7812         (setq gnus-newsgroup-processable nil)
7813         (gnus-summary-limit articles))
7814     (gnus-summary-position-point)))
7815
7816 (defun gnus-summary-pop-limit (&optional total)
7817   "Restore the previous limit.
7818 If given a prefix, remove all limits."
7819   (interactive "P")
7820   (when total
7821     (setq gnus-newsgroup-limits
7822           (list (mapcar (lambda (h) (mail-header-number h))
7823                         gnus-newsgroup-headers))))
7824   (unless gnus-newsgroup-limits
7825     (error "No limit to pop"))
7826   (prog1
7827       (gnus-summary-limit nil 'pop)
7828     (gnus-summary-position-point)))
7829
7830 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7831   "Limit the summary buffer to articles that have subjects that match a regexp.
7832 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7833   (interactive
7834    (list (read-string (if current-prefix-arg
7835                           "Exclude subject (regexp): "
7836                         "Limit to subject (regexp): "))
7837          nil current-prefix-arg))
7838   (unless header
7839     (setq header "subject"))
7840   (when (not (equal "" subject))
7841     (prog1
7842         (let ((articles (gnus-summary-find-matching
7843                          (or header "subject") subject 'all nil nil
7844                          not-matching)))
7845           (unless articles
7846             (error "Found no matches for \"%s\"" subject))
7847           (gnus-summary-limit articles))
7848       (gnus-summary-position-point))))
7849
7850 (defun gnus-summary-limit-to-author (from &optional not-matching)
7851   "Limit the summary buffer to articles that have authors that match a regexp.
7852 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7853   (interactive
7854    (list (read-string (if current-prefix-arg
7855                           "Exclude author (regexp): "
7856                         "Limit to author (regexp): "))
7857          current-prefix-arg))
7858   (gnus-summary-limit-to-subject from "from" not-matching))
7859
7860 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7861   "Limit the summary buffer to articles with the given RECIPIENT.
7862
7863 If NOT-MATCHING, exclude RECIPIENT.
7864
7865 To and Cc headers are checked.  You need to include them in
7866 `nnmail-extra-headers'."
7867   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7868   (interactive
7869    (list (read-string (format "%s recipient (regexp): "
7870                               (if current-prefix-arg "Exclude" "Limit to")))
7871          current-prefix-arg))
7872   (when (not (equal "" recipient))
7873     (prog1 (let* ((to
7874                    (if (memq 'To nnmail-extra-headers)
7875                        (gnus-summary-find-matching
7876                         (cons 'extra 'To) recipient 'all nil nil
7877                         not-matching)
7878                      (gnus-message
7879                       1 "`To' isn't present in `nnmail-extra-headers'")
7880                      (sit-for 1)
7881                      nil))
7882                   (cc
7883                    (if (memq 'Cc nnmail-extra-headers)
7884                        (gnus-summary-find-matching
7885                         (cons 'extra 'Cc) recipient 'all nil nil
7886                         not-matching)
7887                      (gnus-message
7888                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7889                      (sit-for 1)
7890                      nil))
7891                   (articles
7892                    (if not-matching
7893                        ;; We need the numbers that are in both lists:
7894                        (mapcar (lambda (a)
7895                                  (and (memq a to) a))
7896                                cc)
7897                      (nconc to cc))))
7898              (unless articles
7899                (error "Found no matches for \"%s\"" recipient))
7900              (gnus-summary-limit articles))
7901       (gnus-summary-position-point))))
7902
7903 (defun gnus-summary-limit-to-age (age &optional younger-p)
7904   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7905 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7906 articles that are younger than AGE days."
7907   (interactive
7908    (let ((younger current-prefix-arg)
7909          (days-got nil)
7910          days)
7911      (while (not days-got)
7912        (setq days (if younger
7913                       (read-string "Limit to articles younger than (in days, older when negative): ")
7914                     (read-string
7915                      "Limit to articles older than (in days, younger when negative): ")))
7916        (when (> (length days) 0)
7917          (setq days (read days)))
7918        (if (numberp days)
7919            (progn
7920              (setq days-got t)
7921              (when (< days 0)
7922                (setq younger (not younger))
7923                (setq days (* days -1))))
7924          (message "Please enter a number.")
7925          (sleep-for 1)))
7926      (list days younger)))
7927   (prog1
7928       (let ((data gnus-newsgroup-data)
7929             (cutoff (days-to-time age))
7930             articles d date is-younger)
7931         (while (setq d (pop data))
7932           (when (and (vectorp (gnus-data-header d))
7933                      (setq date (mail-header-date (gnus-data-header d))))
7934             (setq is-younger (time-less-p
7935                               (time-since (condition-case ()
7936                                               (date-to-time date)
7937                                             (error '(0 0))))
7938                               cutoff))
7939             (when (if younger-p
7940                       is-younger
7941                     (not is-younger))
7942               (push (gnus-data-number d) articles))))
7943         (gnus-summary-limit (nreverse articles)))
7944     (gnus-summary-position-point)))
7945
7946 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7947   "Limit the summary buffer to articles that match an 'extra' header."
7948   (interactive
7949    (let ((header
7950           (intern
7951            (gnus-completing-read-with-default
7952             (symbol-name (car gnus-extra-headers))
7953             (if current-prefix-arg
7954                 "Exclude extra header"
7955               "Limit extra header")
7956             (mapcar (lambda (x)
7957                       (cons (symbol-name x) x))
7958                     gnus-extra-headers)
7959             nil
7960             t))))
7961      (list header
7962            (read-string (format "%s header %s (regexp): "
7963                                 (if current-prefix-arg "Exclude" "Limit to")
7964                                 header))
7965            current-prefix-arg)))
7966   (when (not (equal "" regexp))
7967     (prog1
7968         (let ((articles (gnus-summary-find-matching
7969                          (cons 'extra header) regexp 'all nil nil
7970                          not-matching)))
7971           (unless articles
7972             (error "Found no matches for \"%s\"" regexp))
7973           (gnus-summary-limit articles))
7974       (gnus-summary-position-point))))
7975
7976 (defun gnus-summary-limit-to-display-predicate ()
7977   "Limit the summary buffer to the predicated in the `display' group parameter."
7978   (interactive)
7979   (unless gnus-newsgroup-display
7980     (error "There is no `display' group parameter"))
7981   (let (articles)
7982     (dolist (number gnus-newsgroup-articles)
7983       (when (funcall gnus-newsgroup-display)
7984         (push number articles)))
7985     (gnus-summary-limit articles))
7986   (gnus-summary-position-point))
7987
7988 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7989 (make-obsolete
7990  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7991
7992 (defun gnus-summary-limit-to-unread (&optional all)
7993   "Limit the summary buffer to articles that are not marked as read.
7994 If ALL is non-nil, limit strictly to unread articles."
7995   (interactive "P")
7996   (if all
7997       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7998     (gnus-summary-limit-to-marks
7999      ;; Concat all the marks that say that an article is read and have
8000      ;; those removed.
8001      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8002            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8003            gnus-low-score-mark gnus-expirable-mark
8004            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8005            gnus-duplicate-mark gnus-souped-mark)
8006      'reverse)))
8007
8008 (defun gnus-summary-limit-to-bodies (match &optional reverse)
8009   "Limit the summary buffer to articles that have bodies that match MATCH.
8010 If REVERSE (the prefix), limit to articles that don't match."
8011   (interactive "sMatch body (regexp): \nP")
8012   (let ((articles nil)
8013         (gnus-select-article-hook nil)  ;Disable hook.
8014         (gnus-article-prepare-hook nil)
8015         (gnus-use-article-prefetch nil)
8016         (gnus-keep-backlog nil)
8017         (gnus-break-pages nil)
8018         (gnus-summary-display-arrow nil)
8019         (gnus-updated-mode-lines nil)
8020         (gnus-auto-center-summary nil)
8021         (gnus-display-mime-function nil))
8022     (dolist (data gnus-newsgroup-data)
8023       (let (gnus-mark-article-hook)
8024         (gnus-summary-select-article t t nil (gnus-data-number data)))
8025       (save-excursion
8026         (set-buffer gnus-article-buffer)
8027         (article-goto-body)
8028         (let* ((case-fold-search t)
8029                (found (re-search-forward match nil t)))
8030           (when (or (and found
8031                          (not reverse))
8032                     (and (not found)
8033                          reverse))
8034             (push (gnus-data-number data) articles)))))
8035     (if (not articles)
8036         (message "No messages matched")
8037       (gnus-summary-limit articles)))
8038   (gnus-summary-position-point))
8039
8040 (defun gnus-summary-limit-to-replied (&optional unreplied)
8041   "Limit the summary buffer to replied articles.
8042 If UNREPLIED (the prefix), limit to unreplied articles."
8043   (interactive "P")
8044   (if unreplied
8045       (gnus-summary-limit
8046        (gnus-set-difference gnus-newsgroup-articles
8047         gnus-newsgroup-replied))
8048     (gnus-summary-limit gnus-newsgroup-replied))
8049   (gnus-summary-position-point))
8050
8051 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8052 (make-obsolete 'gnus-summary-delete-marked-with
8053                'gnus-summary-limit-exclude-marks)
8054
8055 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8056   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8057 If REVERSE, limit the summary buffer to articles that are marked
8058 with MARKS.  MARKS can either be a string of marks or a list of marks.
8059 Returns how many articles were removed."
8060   (interactive "sMarks: ")
8061   (gnus-summary-limit-to-marks marks t))
8062
8063 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8064   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8065 If REVERSE (the prefix), limit the summary buffer to articles that are
8066 not marked with MARKS.  MARKS can either be a string of marks or a
8067 list of marks.
8068 Returns how many articles were removed."
8069   (interactive "sMarks: \nP")
8070   (prog1
8071       (let ((data gnus-newsgroup-data)
8072             (marks (if (listp marks) marks
8073                      (append marks nil))) ; Transform to list.
8074             articles)
8075         (while data
8076           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8077                   (memq (gnus-data-mark (car data)) marks))
8078             (push (gnus-data-number (car data)) articles))
8079           (setq data (cdr data)))
8080         (gnus-summary-limit articles))
8081     (gnus-summary-position-point)))
8082
8083 (defun gnus-summary-limit-to-score (score)
8084   "Limit to articles with score at or above SCORE."
8085   (interactive "NLimit to articles with score of at least: ")
8086   (let ((data gnus-newsgroup-data)
8087         articles)
8088     (while data
8089       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8090                 score)
8091         (push (gnus-data-number (car data)) articles))
8092       (setq data (cdr data)))
8093     (prog1
8094         (gnus-summary-limit articles)
8095       (gnus-summary-position-point))))
8096
8097 (defun gnus-summary-limit-to-unseen ()
8098   "Limit to unseen articles."
8099   (interactive)
8100   (prog1
8101       (gnus-summary-limit gnus-newsgroup-unseen)
8102     (gnus-summary-position-point)))
8103
8104 (defun gnus-summary-limit-include-thread (id)
8105   "Display all the hidden articles that is in the thread with ID in it.
8106 When called interactively, ID is the Message-ID of the current
8107 article."
8108   (interactive (list (mail-header-id (gnus-summary-article-header))))
8109   (let ((articles (gnus-articles-in-thread
8110                    (gnus-id-to-thread (gnus-root-id id)))))
8111     (prog1
8112         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8113       (gnus-summary-limit-include-matching-articles
8114        "subject"
8115        (regexp-quote (gnus-simplify-subject-re
8116                       (mail-header-subject (gnus-id-to-header id)))))
8117       (gnus-summary-position-point))))
8118
8119 (defun gnus-summary-limit-include-matching-articles (header regexp)
8120   "Display all the hidden articles that have HEADERs that match REGEXP."
8121   (interactive (list (read-string "Match on header: ")
8122                      (read-string "Regexp: ")))
8123   (let ((articles (gnus-find-matching-articles header regexp)))
8124     (prog1
8125         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8126       (gnus-summary-position-point))))
8127
8128 (defun gnus-summary-insert-dormant-articles ()
8129   "Insert all the dormant articles for this group into the current buffer."
8130   (interactive)
8131   (let ((gnus-verbose (max 6 gnus-verbose)))
8132     (if (not gnus-newsgroup-dormant)
8133         (gnus-message 3 "No cached articles for this group")
8134       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8135
8136 (defun gnus-summary-limit-include-dormant ()
8137   "Display all the hidden articles that are marked as dormant.
8138 Note that this command only works on a subset of the articles currently
8139 fetched for this group."
8140   (interactive)
8141   (unless gnus-newsgroup-dormant
8142     (error "There are no dormant articles in this group"))
8143   (prog1
8144       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8145     (gnus-summary-position-point)))
8146
8147 (defun gnus-summary-limit-exclude-dormant ()
8148   "Hide all dormant articles."
8149   (interactive)
8150   (prog1
8151       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8152     (gnus-summary-position-point)))
8153
8154 (defun gnus-summary-limit-exclude-childless-dormant ()
8155   "Hide all dormant articles that have no children."
8156   (interactive)
8157   (let ((data (gnus-data-list t))
8158         articles d children)
8159     ;; Find all articles that are either not dormant or have
8160     ;; children.
8161     (while (setq d (pop data))
8162       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8163                 (and (setq children
8164                            (gnus-article-children (gnus-data-number d)))
8165                      (let (found)
8166                        (while children
8167                          (when (memq (car children) articles)
8168                            (setq children nil
8169                                  found t))
8170                          (pop children))
8171                        found)))
8172         (push (gnus-data-number d) articles)))
8173     ;; Do the limiting.
8174     (prog1
8175         (gnus-summary-limit articles)
8176       (gnus-summary-position-point))))
8177
8178 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8179   "Mark all unread excluded articles as read.
8180 If ALL, mark even excluded ticked and dormants as read."
8181   (interactive "P")
8182   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8183   (let ((articles (gnus-sorted-ndifference
8184                    (sort
8185                     (mapcar (lambda (h) (mail-header-number h))
8186                             gnus-newsgroup-headers)
8187                     '<)
8188                    gnus-newsgroup-limit))
8189         article)
8190     (setq gnus-newsgroup-unreads
8191           (gnus-sorted-intersection gnus-newsgroup-unreads
8192                                     gnus-newsgroup-limit))
8193     (if all
8194         (setq gnus-newsgroup-dormant nil
8195               gnus-newsgroup-marked nil
8196               gnus-newsgroup-reads
8197               (nconc
8198                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8199                gnus-newsgroup-reads))
8200       (while (setq article (pop articles))
8201         (unless (or (memq article gnus-newsgroup-dormant)
8202                     (memq article gnus-newsgroup-marked))
8203           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8204
8205 (defun gnus-summary-limit (articles &optional pop)
8206   (if pop
8207       ;; We pop the previous limit off the stack and use that.
8208       (setq articles (car gnus-newsgroup-limits)
8209             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8210     ;; We use the new limit, so we push the old limit on the stack.
8211     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8212   ;; Set the limit.
8213   (setq gnus-newsgroup-limit articles)
8214   (let ((total (length gnus-newsgroup-data))
8215         (data (gnus-data-find-list (gnus-summary-article-number)))
8216         (gnus-summary-mark-below nil)   ; Inhibit this.
8217         found)
8218     ;; This will do all the work of generating the new summary buffer
8219     ;; according to the new limit.
8220     (gnus-summary-prepare)
8221     ;; Hide any threads, possibly.
8222     (gnus-summary-maybe-hide-threads)
8223     ;; Try to return to the article you were at, or one in the
8224     ;; neighborhood.
8225     (when data
8226       ;; We try to find some article after the current one.
8227       (while data
8228         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8229           (setq data nil
8230                 found t))
8231         (setq data (cdr data))))
8232     (unless found
8233       ;; If there is no data, that means that we were after the last
8234       ;; article.  The same goes when we can't find any articles
8235       ;; after the current one.
8236       (goto-char (point-max))
8237       (gnus-summary-find-prev))
8238     (gnus-set-mode-line 'summary)
8239     ;; We return how many articles were removed from the summary
8240     ;; buffer as a result of the new limit.
8241     (- total (length gnus-newsgroup-data))))
8242
8243 (defsubst gnus-invisible-cut-children (threads)
8244   (let ((num 0))
8245     (while threads
8246       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8247         (incf num))
8248       (pop threads))
8249     (< num 2)))
8250
8251 (defsubst gnus-cut-thread (thread)
8252   "Go forwards in the thread until we find an article that we want to display."
8253   (when (or (eq gnus-fetch-old-headers 'some)
8254             (eq gnus-fetch-old-headers 'invisible)
8255             (numberp gnus-fetch-old-headers)
8256             (eq gnus-build-sparse-threads 'some)
8257             (eq gnus-build-sparse-threads 'more))
8258     ;; Deal with old-fetched headers and sparse threads.
8259     (while (and
8260             thread
8261             (or
8262              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8263              (gnus-summary-article-ancient-p
8264               (mail-header-number (car thread))))
8265             (if (or (<= (length (cdr thread)) 1)
8266                     (eq gnus-fetch-old-headers 'invisible))
8267                 (setq gnus-newsgroup-limit
8268                       (delq (mail-header-number (car thread))
8269                             gnus-newsgroup-limit)
8270                       thread (cadr thread))
8271               (when (gnus-invisible-cut-children (cdr thread))
8272                 (let ((th (cdr thread)))
8273                   (while th
8274                     (if (memq (mail-header-number (caar th))
8275                               gnus-newsgroup-limit)
8276                         (setq thread (car th)
8277                               th nil)
8278                       (setq th (cdr th))))))))))
8279   thread)
8280
8281 (defun gnus-cut-threads (threads)
8282   "Cut off all uninteresting articles from the beginning of THREADS."
8283   (when (or (eq gnus-fetch-old-headers 'some)
8284             (eq gnus-fetch-old-headers 'invisible)
8285             (numberp gnus-fetch-old-headers)
8286             (eq gnus-build-sparse-threads 'some)
8287             (eq gnus-build-sparse-threads 'more))
8288     (let ((th threads))
8289       (while th
8290         (setcar th (gnus-cut-thread (car th)))
8291         (setq th (cdr th)))))
8292   ;; Remove nixed out threads.
8293   (delq nil threads))
8294
8295 (defun gnus-summary-initial-limit (&optional show-if-empty)
8296   "Figure out what the initial limit is supposed to be on group entry.
8297 This entails weeding out unwanted dormants, low-scored articles,
8298 fetch-old-headers verbiage, and so on."
8299   ;; Most groups have nothing to remove.
8300   (if (or gnus-inhibit-limiting
8301           (and (null gnus-newsgroup-dormant)
8302                (eq gnus-newsgroup-display 'gnus-not-ignore)
8303                (not (eq gnus-fetch-old-headers 'some))
8304                (not (numberp gnus-fetch-old-headers))
8305                (not (eq gnus-fetch-old-headers 'invisible))
8306                (null gnus-summary-expunge-below)
8307                (not (eq gnus-build-sparse-threads 'some))
8308                (not (eq gnus-build-sparse-threads 'more))
8309                (null gnus-thread-expunge-below)
8310                (not gnus-use-nocem)))
8311       ()                                ; Do nothing.
8312     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8313     (setq gnus-newsgroup-limit nil)
8314     (mapatoms
8315      (lambda (node)
8316        (unless (car (symbol-value node))
8317          ;; These threads have no parents -- they are roots.
8318          (let ((nodes (cdr (symbol-value node)))
8319                thread)
8320            (while nodes
8321              (if (and gnus-thread-expunge-below
8322                       (< (gnus-thread-total-score (car nodes))
8323                          gnus-thread-expunge-below))
8324                  (gnus-expunge-thread (pop nodes))
8325                (setq thread (pop nodes))
8326                (gnus-summary-limit-children thread))))))
8327      gnus-newsgroup-dependencies)
8328     ;; If this limitation resulted in an empty group, we might
8329     ;; pop the previous limit and use it instead.
8330     (when (and (not gnus-newsgroup-limit)
8331                show-if-empty)
8332       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8333     gnus-newsgroup-limit))
8334
8335 (defun gnus-summary-limit-children (thread)
8336   "Return 1 if this subthread is visible and 0 if it is not."
8337   ;; First we get the number of visible children to this thread.  This
8338   ;; is done by recursing down the thread using this function, so this
8339   ;; will really go down to a leaf article first, before slowly
8340   ;; working its way up towards the root.
8341   (when thread
8342     (let* ((max-lisp-eval-depth 5000)
8343            (children
8344            (if (cdr thread)
8345                (apply '+ (mapcar 'gnus-summary-limit-children
8346                                  (cdr thread)))
8347              0))
8348           (number (mail-header-number (car thread)))
8349           score)
8350       (if (and
8351            (not (memq number gnus-newsgroup-marked))
8352            (or
8353             ;; If this article is dormant and has absolutely no visible
8354             ;; children, then this article isn't visible.
8355             (and (memq number gnus-newsgroup-dormant)
8356                  (zerop children))
8357             ;; If this is "fetch-old-headered" and there is no
8358             ;; visible children, then we don't want this article.
8359             (and (or (eq gnus-fetch-old-headers 'some)
8360                      (numberp gnus-fetch-old-headers))
8361                  (gnus-summary-article-ancient-p number)
8362                  (zerop children))
8363             ;; If this is "fetch-old-headered" and `invisible', then
8364             ;; we don't want this article.
8365             (and (eq gnus-fetch-old-headers 'invisible)
8366                  (gnus-summary-article-ancient-p number))
8367             ;; If this is a sparsely inserted article with no children,
8368             ;; we don't want it.
8369             (and (eq gnus-build-sparse-threads 'some)
8370                  (gnus-summary-article-sparse-p number)
8371                  (zerop children))
8372             ;; If we use expunging, and this article is really
8373             ;; low-scored, then we don't want this article.
8374             (when (and gnus-summary-expunge-below
8375                        (< (setq score
8376                                 (or (cdr (assq number gnus-newsgroup-scored))
8377                                     gnus-summary-default-score))
8378                           gnus-summary-expunge-below))
8379               ;; We increase the expunge-tally here, but that has
8380               ;; nothing to do with the limits, really.
8381               (incf gnus-newsgroup-expunged-tally)
8382               ;; We also mark as read here, if that's wanted.
8383               (when (and gnus-summary-mark-below
8384                          (< score gnus-summary-mark-below))
8385                 (setq gnus-newsgroup-unreads
8386                       (delq number gnus-newsgroup-unreads))
8387                 (if gnus-newsgroup-auto-expire
8388                     (push number gnus-newsgroup-expirable)
8389                   (push (cons number gnus-low-score-mark)
8390                         gnus-newsgroup-reads)))
8391               t)
8392             ;; Do the `display' group parameter.
8393             (and gnus-newsgroup-display
8394                  (not (funcall gnus-newsgroup-display)))
8395             ;; Check NoCeM things.
8396             (when (and gnus-use-nocem
8397                        (gnus-nocem-unwanted-article-p
8398                         (mail-header-id (car thread))))
8399               (setq gnus-newsgroup-unreads
8400                     (delq number gnus-newsgroup-unreads))
8401               t)))
8402           ;; Nope, invisible article.
8403           0
8404         ;; Ok, this article is to be visible, so we add it to the limit
8405         ;; and return 1.
8406         (push number gnus-newsgroup-limit)
8407         1))))
8408
8409 (defun gnus-expunge-thread (thread)
8410   "Mark all articles in THREAD as read."
8411   (let* ((number (mail-header-number (car thread))))
8412     (incf gnus-newsgroup-expunged-tally)
8413     ;; We also mark as read here, if that's wanted.
8414     (setq gnus-newsgroup-unreads
8415           (delq number gnus-newsgroup-unreads))
8416     (if gnus-newsgroup-auto-expire
8417         (push number gnus-newsgroup-expirable)
8418       (push (cons number gnus-low-score-mark)
8419             gnus-newsgroup-reads)))
8420   ;; Go recursively through all subthreads.
8421   (mapcar 'gnus-expunge-thread (cdr thread)))
8422
8423 ;; Summary article oriented commands
8424
8425 (defun gnus-summary-refer-parent-article (n)
8426   "Refer parent article N times.
8427 If N is negative, go to ancestor -N instead.
8428 The difference between N and the number of articles fetched is returned."
8429   (interactive "p")
8430   (let ((skip 1)
8431         error header ref)
8432     (when (not (natnump n))
8433       (setq skip (abs n)
8434             n 1))
8435     (while (and (> n 0)
8436                 (not error))
8437       (setq header (gnus-summary-article-header))
8438       (if (and (eq (mail-header-number header)
8439                    (cdr gnus-article-current))
8440                (equal gnus-newsgroup-name
8441                       (car gnus-article-current)))
8442           ;; If we try to find the parent of the currently
8443           ;; displayed article, then we take a look at the actual
8444           ;; References header, since this is slightly more
8445           ;; reliable than the References field we got from the
8446           ;; server.
8447           (save-excursion
8448             (set-buffer gnus-original-article-buffer)
8449             (nnheader-narrow-to-headers)
8450             (unless (setq ref (message-fetch-field "references"))
8451               (when (setq ref (message-fetch-field "in-reply-to"))
8452                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8453             (widen))
8454         (setq ref
8455               ;; It's not the current article, so we take a bet on
8456               ;; the value we got from the server.
8457               (mail-header-references header)))
8458       (if (and ref
8459                (not (equal ref "")))
8460           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8461             (gnus-message 1 "Couldn't find parent"))
8462         (gnus-message 1 "No references in article %d"
8463                       (gnus-summary-article-number))
8464         (setq error t))
8465       (decf n))
8466     (gnus-summary-position-point)
8467     n))
8468
8469 (defun gnus-summary-refer-references ()
8470   "Fetch all articles mentioned in the References header.
8471 Return the number of articles fetched."
8472   (interactive)
8473   (let ((ref (mail-header-references (gnus-summary-article-header)))
8474         (current (gnus-summary-article-number))
8475         (n 0))
8476     (if (or (not ref)
8477             (equal ref ""))
8478         (error "No References in the current article")
8479       ;; For each Message-ID in the References header...
8480       (while (string-match "<[^>]*>" ref)
8481         (incf n)
8482         ;; ... fetch that article.
8483         (gnus-summary-refer-article
8484          (prog1 (match-string 0 ref)
8485            (setq ref (substring ref (match-end 0))))))
8486       (gnus-summary-goto-subject current)
8487       (gnus-summary-position-point)
8488       n)))
8489
8490 (defun gnus-summary-refer-thread (&optional limit)
8491   "Fetch all articles in the current thread.
8492 If LIMIT (the numerical prefix), fetch that many old headers instead
8493 of what's specified by the `gnus-refer-thread-limit' variable."
8494   (interactive "P")
8495   (let ((id (mail-header-id (gnus-summary-article-header)))
8496         (limit (if limit (prefix-numeric-value limit)
8497                  gnus-refer-thread-limit)))
8498     (unless (eq gnus-fetch-old-headers 'invisible)
8499       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8500       ;; Retrieve the headers and read them in.
8501       (if (eq (if (numberp limit)
8502                   (gnus-retrieve-headers
8503                    (list (min
8504                           (+ (mail-header-number
8505                               (gnus-summary-article-header))
8506                              limit)
8507                           gnus-newsgroup-end))
8508                    gnus-newsgroup-name (* limit 2))
8509                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8510                 ;; headers.
8511                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8512                                        gnus-newsgroup-name limit))
8513               'nov)
8514           (gnus-build-all-threads)
8515         (error "Can't fetch thread from back ends that don't support NOV"))
8516       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8517     (gnus-summary-limit-include-thread id)))
8518
8519 (defun gnus-summary-refer-article (message-id)
8520   "Fetch an article specified by MESSAGE-ID."
8521   (interactive "sMessage-ID: ")
8522   (when (and (stringp message-id)
8523              (not (zerop (length message-id))))
8524     (setq message-id (gnus-replace-in-string message-id " " ""))
8525     ;; Construct the correct Message-ID if necessary.
8526     ;; Suggested by tale@pawl.rpi.edu.
8527     (unless (string-match "^<" message-id)
8528       (setq message-id (concat "<" message-id)))
8529     (unless (string-match ">$" message-id)
8530       (setq message-id (concat message-id ">")))
8531     ;; People often post MIDs from URLs, so unhex it:
8532     (unless (string-match "@" message-id)
8533       (setq message-id (gnus-url-unhex-string message-id)))
8534     (let* ((header (gnus-id-to-header message-id))
8535            (sparse (and header
8536                         (gnus-summary-article-sparse-p
8537                          (mail-header-number header))
8538                         (memq (mail-header-number header)
8539                               gnus-newsgroup-limit)))
8540            number)
8541       (cond
8542        ;; If the article is present in the buffer we just go to it.
8543        ((and header
8544              (or (not (gnus-summary-article-sparse-p
8545                        (mail-header-number header)))
8546                  sparse))
8547         (prog1
8548             (gnus-summary-goto-article
8549              (mail-header-number header) nil t)
8550           (when sparse
8551             (gnus-summary-update-article (mail-header-number header)))))
8552        (t
8553         ;; We fetch the article.
8554         (catch 'found
8555           (dolist (gnus-override-method (gnus-refer-article-methods))
8556             (when (and (gnus-check-server gnus-override-method)
8557                        ;; Fetch the header,
8558                        (setq number (gnus-summary-insert-subject message-id)))
8559               ;; and display the article.
8560               (gnus-summary-select-article nil nil nil number)
8561               (throw 'found t)))
8562           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8563
8564 (defun gnus-refer-article-methods ()
8565   "Return a list of referable methods."
8566   (cond
8567    ;; No method, so we default to current and native.
8568    ((null gnus-refer-article-method)
8569     (list gnus-current-select-method gnus-select-method))
8570    ;; Current.
8571    ((eq 'current gnus-refer-article-method)
8572     (list gnus-current-select-method))
8573    ;; List of select methods.
8574    ((not (and (symbolp (car gnus-refer-article-method))
8575               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8576     (let (out)
8577       (dolist (method gnus-refer-article-method)
8578         (push (if (eq 'current method)
8579                   gnus-current-select-method
8580                 method)
8581               out))
8582       (nreverse out)))
8583    ;; One single select method.
8584    (t
8585     (list gnus-refer-article-method))))
8586
8587 (defun gnus-summary-edit-parameters ()
8588   "Edit the group parameters of the current group."
8589   (interactive)
8590   (gnus-group-edit-group gnus-newsgroup-name 'params))
8591
8592 (defun gnus-summary-customize-parameters ()
8593   "Customize the group parameters of the current group."
8594   (interactive)
8595   (gnus-group-customize gnus-newsgroup-name))
8596
8597 (defun gnus-summary-enter-digest-group (&optional force)
8598   "Enter an nndoc group based on the current article.
8599 If FORCE, force a digest interpretation.  If not, try
8600 to guess what the document format is."
8601   (interactive "P")
8602   (let ((conf gnus-current-window-configuration))
8603     (save-window-excursion
8604       (save-excursion
8605         (let (gnus-article-prepare-hook
8606               gnus-display-mime-function
8607               gnus-break-pages)
8608           (gnus-summary-select-article))))
8609     (setq gnus-current-window-configuration conf)
8610     (let* ((name (format "%s-%d"
8611                          (gnus-group-prefixed-name
8612                           gnus-newsgroup-name (list 'nndoc ""))
8613                          (with-current-buffer gnus-summary-buffer
8614                            gnus-current-article)))
8615            (ogroup gnus-newsgroup-name)
8616            (params (append (gnus-info-params (gnus-get-info ogroup))
8617                            (list (cons 'to-group ogroup))
8618                            (list (cons 'parent-group ogroup))
8619                            (list (cons 'save-article-group ogroup))))
8620            (case-fold-search t)
8621            (buf (current-buffer))
8622            dig to-address)
8623       (save-excursion
8624         (set-buffer gnus-original-article-buffer)
8625         ;; Have the digest group inherit the main mail address of
8626         ;; the parent article.
8627         (when (setq to-address (or (gnus-fetch-field "reply-to")
8628                                    (gnus-fetch-field "from")))
8629           (setq params (append
8630                         (list (cons 'to-address
8631                                     (funcall gnus-decode-encoded-word-function
8632                                              to-address))))))
8633         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8634         (insert-buffer-substring gnus-original-article-buffer)
8635         ;; Remove lines that may lead nndoc to misinterpret the
8636         ;; document type.
8637         (narrow-to-region
8638          (goto-char (point-min))
8639          (or (search-forward "\n\n" nil t) (point)))
8640         (goto-char (point-min))
8641         (delete-matching-lines "^Path:\\|^From ")
8642         (widen))
8643       (unwind-protect
8644           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8645                     (gnus-newsgroup-ephemeral-ignored-charsets
8646                      gnus-newsgroup-ignored-charsets))
8647                 (gnus-group-read-ephemeral-group
8648                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8649                               (nndoc-article-type
8650                                ,(if force 'mbox 'guess)))
8651                  t nil nil nil
8652                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8653                                                         "ADAPT")))))
8654               ;; Make all postings to this group go to the parent group.
8655               (nconc (gnus-info-params (gnus-get-info name))
8656                      params)
8657             ;; Couldn't select this doc group.
8658             (switch-to-buffer buf)
8659             (gnus-set-global-variables)
8660             (gnus-configure-windows 'summary)
8661             (gnus-message 3 "Article couldn't be entered?"))
8662         (kill-buffer dig)))))
8663
8664 (defun gnus-summary-read-document (n)
8665   "Open a new group based on the current article(s).
8666 This will allow you to read digests and other similar
8667 documents as newsgroups.
8668 Obeys the standard process/prefix convention."
8669   (interactive "P")
8670   (let* ((ogroup gnus-newsgroup-name)
8671          (params (append (gnus-info-params (gnus-get-info ogroup))
8672                          (list (cons 'to-group ogroup))))
8673          group egroup groups vgroup)
8674     (dolist (article (gnus-summary-work-articles n))
8675       (setq group (format "%s-%d" gnus-newsgroup-name article))
8676       (gnus-summary-remove-process-mark article)
8677       (when (gnus-summary-display-article article)
8678         (save-excursion
8679           (with-temp-buffer
8680             (insert-buffer-substring gnus-original-article-buffer)
8681             ;; Remove some headers that may lead nndoc to make
8682             ;; the wrong guess.
8683             (message-narrow-to-head)
8684             (goto-char (point-min))
8685             (delete-matching-lines "^Path:\\|^From ")
8686             (widen)
8687             (if (setq egroup
8688                       (gnus-group-read-ephemeral-group
8689                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8690                                      (nndoc-article-type guess))
8691                        t nil t))
8692                 (progn
8693             ;; Make all postings to this group go to the parent group.
8694                   (nconc (gnus-info-params (gnus-get-info egroup))
8695                          params)
8696                   (push egroup groups))
8697               ;; Couldn't select this doc group.
8698               (gnus-error 3 "Article couldn't be entered"))))))
8699     ;; Now we have selected all the documents.
8700     (cond
8701      ((not groups)
8702       (error "None of the articles could be interpreted as documents"))
8703      ((gnus-group-read-ephemeral-group
8704        (setq vgroup (format
8705                      "nnvirtual:%s-%s" gnus-newsgroup-name
8706                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8707        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8708        t
8709        (cons (current-buffer) 'summary)))
8710      (t
8711       (error "Couldn't select virtual nndoc group")))))
8712
8713 (defun gnus-summary-isearch-article (&optional regexp-p)
8714   "Do incremental search forward on the current article.
8715 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8716   (interactive "P")
8717   (gnus-summary-select-article)
8718   (gnus-configure-windows 'article)
8719   (gnus-eval-in-buffer-window gnus-article-buffer
8720     (save-restriction
8721       (widen)
8722       (isearch-forward regexp-p))))
8723
8724 (defun gnus-summary-repeat-search-article-forward ()
8725   "Repeat the previous search forwards."
8726   (interactive)
8727   (unless gnus-last-search-regexp
8728     (error "No previous search"))
8729   (gnus-summary-search-article-forward gnus-last-search-regexp))
8730
8731 (defun gnus-summary-repeat-search-article-backward ()
8732   "Repeat the previous search backwards."
8733   (interactive)
8734   (unless gnus-last-search-regexp
8735     (error "No previous search"))
8736   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8737
8738 (defun gnus-summary-search-article-forward (regexp &optional backward)
8739   "Search for an article containing REGEXP forward.
8740 If BACKWARD, search backward instead."
8741   (interactive
8742    (list (read-string
8743           (format "Search article %s (regexp%s): "
8744                   (if current-prefix-arg "backward" "forward")
8745                   (if gnus-last-search-regexp
8746                       (concat ", default " gnus-last-search-regexp)
8747                     "")))
8748          current-prefix-arg))
8749   (if (string-equal regexp "")
8750       (setq regexp (or gnus-last-search-regexp ""))
8751     (setq gnus-last-search-regexp regexp)
8752     (setq gnus-article-before-search gnus-current-article))
8753   ;; Intentionally set gnus-last-article.
8754   (setq gnus-last-article gnus-article-before-search)
8755   (let ((gnus-last-article gnus-last-article))
8756     (if (gnus-summary-search-article regexp backward)
8757         (gnus-summary-show-thread)
8758       (signal 'search-failed (list regexp)))))
8759
8760 (defun gnus-summary-search-article-backward (regexp)
8761   "Search for an article containing REGEXP backward."
8762   (interactive
8763    (list (read-string
8764           (format "Search article backward (regexp%s): "
8765                   (if gnus-last-search-regexp
8766                       (concat ", default " gnus-last-search-regexp)
8767                     "")))))
8768   (gnus-summary-search-article-forward regexp 'backward))
8769
8770 (defun gnus-summary-search-article (regexp &optional backward)
8771   "Search for an article containing REGEXP.
8772 Optional argument BACKWARD means do search for backward.
8773 `gnus-select-article-hook' is not called during the search."
8774   ;; We have to require this here to make sure that the following
8775   ;; dynamic binding isn't shadowed by autoloading.
8776   (require 'gnus-async)
8777   (require 'gnus-art)
8778   (let ((gnus-select-article-hook nil)  ;Disable hook.
8779         (gnus-article-prepare-hook nil)
8780         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8781         (gnus-use-article-prefetch nil)
8782         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8783         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8784         (gnus-visual nil)
8785         (gnus-keep-backlog nil)
8786         (gnus-break-pages nil)
8787         (gnus-summary-display-arrow nil)
8788         (gnus-updated-mode-lines nil)
8789         (gnus-auto-center-summary nil)
8790         (sum (current-buffer))
8791         (gnus-display-mime-function nil)
8792         (found nil)
8793         point)
8794     (gnus-save-hidden-threads
8795       (gnus-summary-select-article)
8796       (set-buffer gnus-article-buffer)
8797       (goto-char (window-point (get-buffer-window (current-buffer))))
8798       (when backward
8799         (forward-line -1))
8800       (while (not found)
8801         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8802         (if (if backward
8803                 (re-search-backward regexp nil t)
8804               (re-search-forward regexp nil t))
8805             ;; We found the regexp.
8806             (progn
8807               (setq found 'found)
8808               (beginning-of-line)
8809               (set-window-start
8810                (get-buffer-window (current-buffer))
8811                (point))
8812               (forward-line 1)
8813               (set-window-point
8814                (get-buffer-window (current-buffer))
8815                (point))
8816               (set-buffer sum)
8817               (setq point (point)))
8818           ;; We didn't find it, so we go to the next article.
8819           (set-buffer sum)
8820           (setq found 'not)
8821           (while (eq found 'not)
8822             (if (not (if backward (gnus-summary-find-prev)
8823                        (gnus-summary-find-next)))
8824                 ;; No more articles.
8825                 (setq found t)
8826               ;; Select the next article and adjust point.
8827               (unless (gnus-summary-article-sparse-p
8828                        (gnus-summary-article-number))
8829                 (setq found nil)
8830                 (gnus-summary-select-article)
8831                 (set-buffer gnus-article-buffer)
8832                 (widen)
8833                 (goto-char (if backward (point-max) (point-min))))))))
8834       (gnus-message 7 ""))
8835     ;; Return whether we found the regexp.
8836     (when (eq found 'found)
8837       (goto-char point)
8838       (gnus-summary-show-thread)
8839       (gnus-summary-goto-subject gnus-current-article)
8840       (gnus-summary-position-point)
8841       t)))
8842
8843 (defun gnus-find-matching-articles (header regexp)
8844   "Return a list of all articles that match REGEXP on HEADER.
8845 This search includes all articles in the current group that Gnus has
8846 fetched headers for, whether they are displayed or not."
8847   (let ((articles nil)
8848         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8849         (case-fold-search t))
8850     (dolist (header gnus-newsgroup-headers)
8851       (when (string-match regexp (funcall func header))
8852         (push (mail-header-number header) articles)))
8853     (nreverse articles)))
8854
8855 (defun gnus-summary-find-matching (header regexp &optional backward unread
8856                                           not-case-fold not-matching)
8857   "Return a list of all articles that match REGEXP on HEADER.
8858 The search stars on the current article and goes forwards unless
8859 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8860 If UNREAD is non-nil, only unread articles will
8861 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8862 in the comparisons. If NOT-MATCHING, return a list of all articles that
8863 not match REGEXP on HEADER."
8864   (let ((case-fold-search (not not-case-fold))
8865         articles d func)
8866     (if (consp header)
8867         (if (eq (car header) 'extra)
8868             (setq func
8869                   `(lambda (h)
8870                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8871                          "")))
8872           (error "%s is an invalid header" header))
8873       (unless (fboundp (intern (concat "mail-header-" header)))
8874         (error "%s is not a valid header" header))
8875       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8876     (dolist (d (if (eq backward 'all)
8877                    gnus-newsgroup-data
8878                  (gnus-data-find-list
8879                   (gnus-summary-article-number)
8880                   (gnus-data-list backward))))
8881       (when (and (or (not unread)       ; We want all articles...
8882                      (gnus-data-unread-p d)) ; Or just unreads.
8883                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8884                  (if not-matching
8885                      (not (string-match
8886                            regexp
8887                            (funcall func (gnus-data-header d))))
8888                    (string-match regexp
8889                                  (funcall func (gnus-data-header d)))))
8890         (push (gnus-data-number d) articles))) ; Success!
8891     (nreverse articles)))
8892
8893 (defun gnus-summary-execute-command (header regexp command &optional backward)
8894   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8895 If HEADER is an empty string (or nil), the match is done on the entire
8896 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8897   (interactive
8898    (list (let ((completion-ignore-case t))
8899            (completing-read
8900             "Header name: "
8901             (mapcar (lambda (header) (list (format "%s" header)))
8902                     (append
8903                      '("Number" "Subject" "From" "Lines" "Date"
8904                        "Message-ID" "Xref" "References" "Body")
8905                      gnus-extra-headers))
8906             nil 'require-match))
8907          (read-string "Regexp: ")
8908          (read-key-sequence "Command: ")
8909          current-prefix-arg))
8910   (when (equal header "Body")
8911     (setq header ""))
8912   ;; Hidden thread subtrees must be searched as well.
8913   (gnus-summary-show-all-threads)
8914   ;; We don't want to change current point nor window configuration.
8915   (save-excursion
8916     (save-window-excursion
8917       (let (gnus-visual
8918             gnus-treat-strip-trailing-blank-lines
8919             gnus-treat-strip-leading-blank-lines
8920             gnus-treat-strip-multiple-blank-lines
8921             gnus-treat-hide-boring-headers
8922             gnus-treat-fold-newsgroups
8923             gnus-article-prepare-hook)
8924         (gnus-message 6 "Executing %s..." (key-description command))
8925         ;; We'd like to execute COMMAND interactively so as to give arguments.
8926         (gnus-execute header regexp
8927                       `(call-interactively ',(key-binding command))
8928                       backward)
8929         (gnus-message 6 "Executing %s...done" (key-description command))))))
8930
8931 (defun gnus-summary-beginning-of-article ()
8932   "Scroll the article back to the beginning."
8933   (interactive)
8934   (gnus-summary-select-article)
8935   (gnus-configure-windows 'article)
8936   (gnus-eval-in-buffer-window gnus-article-buffer
8937     (widen)
8938     (goto-char (point-min))
8939     (when gnus-break-pages
8940       (gnus-narrow-to-page))))
8941
8942 (defun gnus-summary-end-of-article ()
8943   "Scroll to the end of the article."
8944   (interactive)
8945   (gnus-summary-select-article)
8946   (gnus-configure-windows 'article)
8947   (gnus-eval-in-buffer-window gnus-article-buffer
8948     (widen)
8949     (goto-char (point-max))
8950     (recenter -3)
8951     (when gnus-break-pages
8952       (when (re-search-backward page-delimiter nil t)
8953         (narrow-to-region (match-end 0) (point-max)))
8954       (gnus-narrow-to-page))))
8955
8956 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8957   "Truncate to LEN and quote all \"(\"'s in STRING."
8958   (gnus-replace-in-string (if (and len (> (length string) len))
8959                               (substring string 0 len)
8960                             string)
8961                           "[()]" "\\\\\\&"))
8962
8963 (defun gnus-summary-print-article (&optional filename n)
8964   "Generate and print a PostScript image of the process-marked (mail) articles.
8965
8966 If used interactively, print the current article if none are
8967 process-marked.  With prefix arg, prompt the user for the name of the
8968 file to save in.
8969
8970 When used from Lisp, accept two optional args FILENAME and N.  N means
8971 to print the next N articles.  If N is negative, print the N previous
8972 articles.  If N is nil and articles have been marked with the process
8973 mark, print these instead.
8974
8975 If the optional first argument FILENAME is nil, send the image to the
8976 printer.  If FILENAME is a string, save the PostScript image in a file with
8977 that name.  If FILENAME is a number, prompt the user for the name of the file
8978 to save in."
8979   (interactive (list (ps-print-preprint current-prefix-arg)))
8980   (dolist (article (gnus-summary-work-articles n))
8981     (gnus-summary-select-article nil nil 'pseudo article)
8982     (gnus-eval-in-buffer-window gnus-article-buffer
8983       (gnus-print-buffer))
8984     (gnus-summary-remove-process-mark article))
8985   (ps-despool filename))
8986
8987 (defun gnus-print-buffer ()
8988   (let ((buffer (generate-new-buffer " *print*")))
8989     (unwind-protect
8990         (progn
8991           (copy-to-buffer buffer (point-min) (point-max))
8992           (set-buffer buffer)
8993           (gnus-remove-text-with-property 'gnus-decoration)
8994           (when (gnus-visual-p 'article-highlight 'highlight)
8995             ;; Copy-to-buffer doesn't copy overlay.  So redo
8996             ;; highlight.
8997             (let ((gnus-article-buffer buffer))
8998               (gnus-article-highlight-citation t)
8999               (gnus-article-highlight-signature)
9000               (gnus-article-emphasize)
9001               (gnus-article-delete-invisible-text)))
9002           (let ((ps-left-header
9003                  (list
9004                   (concat "("
9005                           (gnus-summary-print-truncate-and-quote
9006                            (mail-header-subject gnus-current-headers)
9007                            66) ")")
9008                   (concat "("
9009                           (gnus-summary-print-truncate-and-quote
9010                            (mail-header-from gnus-current-headers)
9011                            45) ")")))
9012                 (ps-right-header
9013                  (list
9014                   "/pagenumberstring load"
9015                   (concat "("
9016                           (mail-header-date gnus-current-headers) ")"))))
9017             (gnus-run-hooks 'gnus-ps-print-hook)
9018             (save-excursion
9019               (if window-system
9020                   (ps-spool-buffer-with-faces)
9021                 (ps-spool-buffer)))))
9022       (kill-buffer buffer))))
9023
9024 (defun gnus-summary-show-article (&optional arg)
9025   "Force redisplaying of the current article.
9026 If ARG (the prefix) is a number, show the article with the charset
9027 defined in `gnus-summary-show-article-charset-alist', or the charset
9028 input.
9029 If ARG (the prefix) is non-nil and not a number, show the raw article
9030 without any article massaging functions being run.  Normally, the key
9031 strokes are `C-u g'."
9032   (interactive "P")
9033   (cond
9034    ((numberp arg)
9035     (gnus-summary-show-article t)
9036     (let ((gnus-newsgroup-charset
9037            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9038                (mm-read-coding-system
9039                 "View as charset: " ;; actually it is coding system.
9040                 (with-current-buffer gnus-article-buffer
9041                   (mm-detect-coding-region (point) (point-max))))))
9042           (gnus-newsgroup-ignored-charsets 'gnus-all))
9043       (gnus-summary-select-article nil 'force)
9044       (let ((deps gnus-newsgroup-dependencies)
9045             head header lines)
9046         (save-excursion
9047           (set-buffer gnus-original-article-buffer)
9048           (save-restriction
9049             (message-narrow-to-head)
9050             (setq head (buffer-string))
9051             (goto-char (point-min))
9052             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9053               (goto-char (point-max))
9054               (widen)
9055               (setq lines (1- (count-lines (point) (point-max))))))
9056           (with-temp-buffer
9057             (insert (format "211 %d Article retrieved.\n"
9058                             (cdr gnus-article-current)))
9059             (insert head)
9060             (if lines (insert (format "Lines: %d\n" lines)))
9061             (insert ".\n")
9062             (let ((nntp-server-buffer (current-buffer)))
9063               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9064         (gnus-data-set-header
9065          (gnus-data-find (cdr gnus-article-current))
9066          header)
9067         (gnus-summary-update-article-line
9068          (cdr gnus-article-current) header)
9069         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9070           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9071    ((not arg)
9072     ;; Select the article the normal way.
9073     (gnus-summary-select-article nil 'force))
9074    (t
9075     ;; We have to require this here to make sure that the following
9076     ;; dynamic binding isn't shadowed by autoloading.
9077     (require 'gnus-async)
9078     (require 'gnus-art)
9079     ;; Bind the article treatment functions to nil.
9080     (let ((gnus-have-all-headers t)
9081           gnus-article-prepare-hook
9082           gnus-article-decode-hook
9083           gnus-display-mime-function
9084           gnus-break-pages)
9085       ;; Destroy any MIME parts.
9086       (when (gnus-buffer-live-p gnus-article-buffer)
9087         (save-excursion
9088           (set-buffer gnus-article-buffer)
9089           (mm-destroy-parts gnus-article-mime-handles)
9090           ;; Set it to nil for safety reason.
9091           (setq gnus-article-mime-handle-alist nil)
9092           (setq gnus-article-mime-handles nil)))
9093       (gnus-summary-select-article nil 'force))))
9094   (gnus-summary-goto-subject gnus-current-article)
9095   (gnus-summary-position-point))
9096
9097 (defun gnus-summary-show-raw-article ()
9098   "Show the raw article without any article massaging functions being run."
9099   (interactive)
9100   (gnus-summary-show-article t))
9101
9102 (defun gnus-summary-verbose-headers (&optional arg)
9103   "Toggle permanent full header display.
9104 If ARG is a positive number, turn header display on.
9105 If ARG is a negative number, turn header display off."
9106   (interactive "P")
9107   (setq gnus-show-all-headers
9108         (cond ((or (not (numberp arg))
9109                    (zerop arg))
9110                (not gnus-show-all-headers))
9111               ((natnump arg)
9112                t)))
9113   (gnus-summary-show-article))
9114
9115 (defun gnus-summary-toggle-header (&optional arg)
9116   "Show the headers if they are hidden, or hide them if they are shown.
9117 If ARG is a positive number, show the entire header.
9118 If ARG is a negative number, hide the unwanted header lines."
9119   (interactive "P")
9120   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9121                      (get-buffer-window gnus-article-buffer t))))
9122     (with-current-buffer gnus-article-buffer
9123       (widen)
9124       (article-narrow-to-head)
9125       (let* ((buffer-read-only nil)
9126              (inhibit-point-motion-hooks t)
9127              (hidden (if (numberp arg)
9128                          (>= arg 0)
9129                        (or (not (looking-at "[^ \t\n]+:"))
9130                            (gnus-article-hidden-text-p 'headers))))
9131              s e)
9132         (delete-region (point-min) (point-max))
9133         (with-current-buffer gnus-original-article-buffer
9134           (goto-char (setq s (point-min)))
9135           (setq e (if (search-forward "\n\n" nil t)
9136                       (1- (point))
9137                     (point-max))))
9138         (insert-buffer-substring gnus-original-article-buffer s e)
9139         (run-hooks 'gnus-article-decode-hook)
9140         (if hidden
9141             (let ((gnus-treat-hide-headers nil)
9142                   (gnus-treat-hide-boring-headers nil))
9143               (gnus-delete-wash-type 'headers)
9144               (gnus-treat-article 'head))
9145           (gnus-treat-article 'head))
9146         (widen)
9147         (if window
9148             (set-window-start window (goto-char (point-min))))
9149         (if gnus-break-pages
9150             (gnus-narrow-to-page)
9151           (when (gnus-visual-p 'page-marker)
9152             (let ((buffer-read-only nil))
9153               (gnus-remove-text-with-property 'gnus-prev)
9154               (gnus-remove-text-with-property 'gnus-next))))
9155         (gnus-set-mode-line 'article)))))
9156
9157 (defun gnus-summary-show-all-headers ()
9158   "Make all header lines visible."
9159   (interactive)
9160   (gnus-summary-toggle-header 1))
9161
9162 (defun gnus-summary-caesar-message (&optional arg)
9163   "Caesar rotate the current article by 13.
9164 The numerical prefix specifies how many places to rotate each letter
9165 forward."
9166   (interactive "P")
9167   (gnus-summary-select-article)
9168   (let ((mail-header-separator ""))
9169     (gnus-eval-in-buffer-window gnus-article-buffer
9170       (save-restriction
9171         (widen)
9172         (let ((start (window-start))
9173               buffer-read-only)
9174           (message-caesar-buffer-body arg)
9175           (set-window-start (get-buffer-window (current-buffer)) start)))))
9176   ;; Create buttons and stuff...
9177   (gnus-treat-article nil))
9178
9179 (defun gnus-summary-idna-message (&optional arg)
9180   "Decode IDNA encoded domain names in the current articles.
9181 IDNA encoded domain names looks like `xn--bar'.  If a string
9182 remain unencoded after running this function, it is likely an
9183 invalid IDNA string (`xn--bar' is invalid).
9184
9185 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9186 installed for this command to work."
9187   (interactive "P")
9188   (if (not (and (condition-case nil (require 'idna)
9189                   (file-error))
9190                 (mm-coding-system-p 'utf-8)
9191                 (executable-find (symbol-value 'idna-program))))
9192       (gnus-message
9193        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9194     (gnus-summary-select-article)
9195     (let ((mail-header-separator ""))
9196       (gnus-eval-in-buffer-window gnus-article-buffer
9197         (save-restriction
9198           (widen)
9199           (let ((start (window-start))
9200                 buffer-read-only)
9201             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9202               (replace-match (idna-to-unicode (match-string 1))))
9203             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9204
9205 (defun gnus-summary-morse-message (&optional arg)
9206   "Morse decode the current article."
9207   (interactive "P")
9208   (gnus-summary-select-article)
9209   (let ((mail-header-separator ""))
9210     (gnus-eval-in-buffer-window gnus-article-buffer
9211       (save-excursion
9212         (save-restriction
9213           (widen)
9214           (let ((pos (window-start))
9215                 buffer-read-only)
9216             (goto-char (point-min))
9217             (when (message-goto-body)
9218               (gnus-narrow-to-body))
9219             (goto-char (point-min))
9220             (while (search-forward "·" (point-max) t)
9221               (replace-match "."))
9222             (unmorse-region (point-min) (point-max))
9223             (widen)
9224             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9225
9226 (defun gnus-summary-stop-page-breaking ()
9227   "Stop page breaking in the current article."
9228   (interactive)
9229   (gnus-summary-select-article)
9230   (gnus-eval-in-buffer-window gnus-article-buffer
9231     (widen)
9232     (when (gnus-visual-p 'page-marker)
9233       (let ((buffer-read-only nil))
9234         (gnus-remove-text-with-property 'gnus-prev)
9235         (gnus-remove-text-with-property 'gnus-next))
9236       (setq gnus-page-broken nil))))
9237
9238 (defun gnus-summary-move-article (&optional n to-newsgroup
9239                                             select-method action)
9240   "Move the current article to a different newsgroup.
9241 If N is a positive number, move the N next articles.
9242 If N is a negative number, move the N previous articles.
9243 If N is nil and any articles have been marked with the process mark,
9244 move those articles instead.
9245 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9246 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9247 re-spool using this method.
9248
9249 When called interactively with TO-NEWSGROUP being nil, the value of
9250 the variable `gnus-move-split-methods' is used for finding a default
9251 for the target newsgroup.
9252
9253 For this function to work, both the current newsgroup and the
9254 newsgroup that you want to move to have to support the `request-move'
9255 and `request-accept' functions.
9256
9257 ACTION can be either `move' (the default), `crosspost' or `copy'."
9258   (interactive "P")
9259   (unless action
9260     (setq action 'move))
9261   ;; Check whether the source group supports the required functions.
9262   (cond ((and (eq action 'move)
9263               (not (gnus-check-backend-function
9264                     'request-move-article gnus-newsgroup-name)))
9265          (error "The current group does not support article moving"))
9266         ((and (eq action 'crosspost)
9267               (not (gnus-check-backend-function
9268                     'request-replace-article gnus-newsgroup-name)))
9269          (error "The current group does not support article editing")))
9270   (let ((articles (gnus-summary-work-articles n))
9271         (prefix (if (gnus-check-backend-function
9272                      'request-move-article gnus-newsgroup-name)
9273                     (funcall gnus-move-group-prefix-function
9274                              gnus-newsgroup-name)
9275                   ""))
9276         (names '((move "Move" "Moving")
9277                  (copy "Copy" "Copying")
9278                  (crosspost "Crosspost" "Crossposting")))
9279         (copy-buf (save-excursion
9280                     (nnheader-set-temp-buffer " *copy article*")))
9281         art-group to-method new-xref article to-groups articles-to-update-marks)
9282     (unless (assq action names)
9283       (error "Unknown action %s" action))
9284     ;; Read the newsgroup name.
9285     (when (and (not to-newsgroup)
9286                (not select-method))
9287       (if (and gnus-move-split-methods
9288                (not
9289                 (and (memq gnus-current-article articles)
9290                      (gnus-buffer-live-p gnus-original-article-buffer))))
9291           ;; When `gnus-move-split-methods' is non-nil, we have to
9292           ;; select an article to give `gnus-read-move-group-name' an
9293           ;; opportunity to suggest an appropriate default.  However,
9294           ;; we needn't render or mark the article.
9295           (let ((gnus-display-mime-function nil)
9296                 (gnus-article-prepare-hook nil)
9297                 (gnus-mark-article-hook nil))
9298             (gnus-summary-select-article nil nil nil (car articles))))
9299       (setq to-newsgroup
9300             (gnus-read-move-group-name
9301              (cadr (assq action names))
9302              (symbol-value (intern (format "gnus-current-%s-group" action)))
9303              articles prefix))
9304       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9305     (setq to-method (or select-method
9306                         (gnus-server-to-method
9307                          (gnus-group-method to-newsgroup))))
9308     ;; Check the method we are to move this article to...
9309     (unless (gnus-check-backend-function
9310              'request-accept-article (car to-method))
9311       (error "%s does not support article copying" (car to-method)))
9312     (unless (gnus-check-server to-method)
9313       (error "Can't open server %s" (car to-method)))
9314     (gnus-message 6 "%s to %s: %s..."
9315                   (caddr (assq action names))
9316                   (or (car select-method) to-newsgroup) articles)
9317     (while articles
9318       (setq article (pop articles))
9319       (setq
9320        art-group
9321        (cond
9322         ;; Move the article.
9323         ((eq action 'move)
9324          ;; Remove this article from future suppression.
9325          (gnus-dup-unsuppress-article article)
9326          (let* ((from-method (gnus-find-method-for-group
9327                               gnus-newsgroup-name))
9328                 (to-method (gnus-find-method-for-group
9329                             to-newsgroup))
9330                 (move-is-internal (gnus-method-equal from-method to-method)))
9331          (gnus-request-move-article
9332           article                       ; Article to move
9333           gnus-newsgroup-name           ; From newsgroup
9334           (nth 1 (gnus-find-method-for-group
9335                   gnus-newsgroup-name)) ; Server
9336           (list 'gnus-request-accept-article
9337                 to-newsgroup (list 'quote select-method)
9338                 (not articles) t)       ; Accept form
9339           (not articles)                ; Only save nov last time
9340           move-is-internal)))           ; is this move internal?
9341         ;; Copy the article.
9342         ((eq action 'copy)
9343          (save-excursion
9344            (set-buffer copy-buf)
9345            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9346              (save-restriction
9347                (nnheader-narrow-to-headers)
9348                (dolist (hdr gnus-copy-article-ignored-headers)
9349                  (message-remove-header hdr t)))
9350              (gnus-request-accept-article
9351               to-newsgroup select-method (not articles) t))))
9352         ;; Crosspost the article.
9353         ((eq action 'crosspost)
9354          (let ((xref (message-tokenize-header
9355                       (mail-header-xref (gnus-summary-article-header article))
9356                       " ")))
9357            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9358                                   ":" (number-to-string article)))
9359            (unless xref
9360              (setq xref (list (system-name))))
9361            (setq new-xref
9362                  (concat
9363                   (mapconcat 'identity
9364                              (delete "Xref:" (delete new-xref xref))
9365                              " ")
9366                   " " new-xref))
9367            (save-excursion
9368              (set-buffer copy-buf)
9369              ;; First put the article in the destination group.
9370              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9371              (when (consp (setq art-group
9372                                 (gnus-request-accept-article
9373                                  to-newsgroup select-method (not articles))))
9374                (setq new-xref (concat new-xref " " (car art-group)
9375                                       ":"
9376                                       (number-to-string (cdr art-group))))
9377                ;; Now we have the new Xrefs header, so we insert
9378                ;; it and replace the new article.
9379                (nnheader-replace-header "Xref" new-xref)
9380                (gnus-request-replace-article
9381                 (cdr art-group) to-newsgroup (current-buffer))
9382                art-group))))))
9383       (cond
9384        ((not art-group)
9385         (gnus-message 1 "Couldn't %s article %s: %s"
9386                       (cadr (assq action names)) article
9387                       (nnheader-get-report (car to-method))))
9388        ((eq art-group 'junk)
9389         (when (eq action 'move)
9390           (gnus-summary-mark-article article gnus-canceled-mark)
9391           (gnus-message 4 "Deleted article %s" article)
9392           ;; run the delete hook
9393           (run-hook-with-args 'gnus-summary-article-delete-hook
9394                               action
9395                               (gnus-data-header
9396                                (assoc article (gnus-data-list nil)))
9397                               gnus-newsgroup-name nil
9398                               select-method)))
9399        (t
9400         (let* ((pto-group (gnus-group-prefixed-name
9401                            (car art-group) to-method))
9402                (info (gnus-get-info pto-group))
9403                (to-group (gnus-info-group info))
9404                to-marks)
9405           ;; Update the group that has been moved to.
9406           (when (and info
9407                      (memq action '(move copy)))
9408             (unless (member to-group to-groups)
9409               (push to-group to-groups))
9410
9411             (unless (memq article gnus-newsgroup-unreads)
9412               (push 'read to-marks)
9413               (gnus-info-set-read
9414                info (gnus-add-to-range (gnus-info-read info)
9415                                        (list (cdr art-group)))))
9416
9417             ;; See whether the article is to be put in the cache.
9418             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9419                              gnus-article-mark-lists
9420                            (delete '(expirable . expire)
9421                                    (copy-sequence gnus-article-mark-lists))))
9422                   (to-article (cdr art-group)))
9423
9424               ;; Enter the article into the cache in the new group,
9425               ;; if that is required.
9426               (when gnus-use-cache
9427                 (gnus-cache-possibly-enter-article
9428                  to-group to-article
9429                  (memq article gnus-newsgroup-marked)
9430                  (memq article gnus-newsgroup-dormant)
9431                  (memq article gnus-newsgroup-unreads)))
9432
9433               (when gnus-preserve-marks
9434                 ;; Copy any marks over to the new group.
9435                 (when (and (equal to-group gnus-newsgroup-name)
9436                            (not (memq article gnus-newsgroup-unreads)))
9437                   ;; Mark this article as read in this group.
9438                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9439                   (setcdr (gnus-active to-group) to-article)
9440                   (setcdr gnus-newsgroup-active to-article))
9441
9442                 (while marks
9443                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9444                     (when (memq article (symbol-value
9445                                          (intern (format "gnus-newsgroup-%s"
9446                                                          (caar marks)))))
9447                       (push (cdar marks) to-marks)
9448                       ;; If the other group is the same as this group,
9449                       ;; then we have to add the mark to the list.
9450                       (when (equal to-group gnus-newsgroup-name)
9451                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9452                              (cons to-article
9453                                    (symbol-value
9454                                     (intern (format "gnus-newsgroup-%s"
9455                                                     (caar marks)))))))
9456                       ;; Copy the marks to other group.
9457                       (gnus-add-marked-articles
9458                        to-group (cdar marks) (list to-article) info)))
9459                   (setq marks (cdr marks)))
9460
9461                 (gnus-request-set-mark
9462                  to-group (list (list (list to-article) 'add to-marks))))
9463
9464               (gnus-dribble-enter
9465                (concat "(gnus-group-set-info '"
9466                        (gnus-prin1-to-string (gnus-get-info to-group))
9467                        ")"))))
9468
9469           ;; Update the Xref header in this article to point to
9470           ;; the new crossposted article we have just created.
9471           (when (eq action 'crosspost)
9472             (save-excursion
9473               (set-buffer copy-buf)
9474               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9475               (nnheader-replace-header "Xref" new-xref)
9476               (gnus-request-replace-article
9477                article gnus-newsgroup-name (current-buffer))))
9478
9479           ;; run the move/copy/crosspost/respool hook
9480           (run-hook-with-args 'gnus-summary-article-move-hook
9481                               action
9482                               (gnus-data-header
9483                                (assoc article (gnus-data-list nil)))
9484                               gnus-newsgroup-name
9485                               to-newsgroup
9486                               select-method))
9487
9488         ;;;!!!Why is this necessary?
9489         (set-buffer gnus-summary-buffer)
9490         
9491         (gnus-summary-goto-subject article)
9492         (when (eq action 'move)
9493           (gnus-summary-mark-article article gnus-canceled-mark))))
9494       (push article articles-to-update-marks))
9495
9496     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9497     ;; Re-activate all groups that have been moved to.
9498     (save-excursion
9499       (set-buffer gnus-group-buffer)
9500       (let ((gnus-group-marked to-groups))
9501         (gnus-group-get-new-news-this-group nil t)))
9502     
9503     (gnus-kill-buffer copy-buf)
9504     (gnus-summary-position-point)
9505     (gnus-set-mode-line 'summary)))
9506
9507 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9508   "Copy the current article to some other group.
9509 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9510 When called interactively, if TO-NEWSGROUP is nil, use the value of
9511 the variable `gnus-move-split-methods' for finding a default target
9512 newsgroup.
9513 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9514 re-spool using this method."
9515   (interactive "P")
9516   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9517
9518 (defun gnus-summary-crosspost-article (&optional n)
9519   "Crosspost the current article to some other group."
9520   (interactive "P")
9521   (gnus-summary-move-article n nil nil 'crosspost))
9522
9523 (defcustom gnus-summary-respool-default-method nil
9524   "Default method type for respooling an article.
9525 If nil, use to the current newsgroup method."
9526   :type 'symbol
9527   :group 'gnus-summary-mail)
9528
9529 (defcustom gnus-summary-display-while-building nil
9530   "If non-nil, show and update the summary buffer as it's being built.
9531 If the value is t, update the buffer after every line is inserted.  If
9532 the value is an integer (N), update the display every N lines."
9533   :version "22.1"
9534   :group 'gnus-thread
9535   :type '(choice (const :tag "off" nil)
9536                  number
9537                  (const :tag "frequently" t)))
9538
9539 (defun gnus-summary-respool-article (&optional n method)
9540   "Respool the current article.
9541 The article will be squeezed through the mail spooling process again,
9542 which means that it will be put in some mail newsgroup or other
9543 depending on `nnmail-split-methods'.
9544 If N is a positive number, respool the N next articles.
9545 If N is a negative number, respool the N previous articles.
9546 If N is nil and any articles have been marked with the process mark,
9547 respool those articles instead.
9548
9549 Respooling can be done both from mail groups and \"real\" newsgroups.
9550 In the former case, the articles in question will be moved from the
9551 current group into whatever groups they are destined to.  In the
9552 latter case, they will be copied into the relevant groups."
9553   (interactive
9554    (list current-prefix-arg
9555          (let* ((methods (gnus-methods-using 'respool))
9556                 (methname
9557                  (symbol-name (or gnus-summary-respool-default-method
9558                                   (car (gnus-find-method-for-group
9559                                         gnus-newsgroup-name)))))
9560                 (method
9561                  (gnus-completing-read-with-default
9562                   methname "Backend to use when respooling"
9563                   methods nil t nil 'gnus-mail-method-history))
9564                 ms)
9565            (cond
9566             ((zerop (length (setq ms (gnus-servers-using-backend
9567                                       (intern method)))))
9568              (list (intern method) ""))
9569             ((= 1 (length ms))
9570              (car ms))
9571             (t
9572              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9573                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9574                            ms-alist))))))))
9575   (unless method
9576     (error "No method given for respooling"))
9577   (if (assoc (symbol-name
9578               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9579              (gnus-methods-using 'respool))
9580       (gnus-summary-move-article n nil method)
9581     (gnus-summary-copy-article n nil method)))
9582
9583 (defun gnus-summary-import-article (file &optional edit)
9584   "Import an arbitrary file into a mail newsgroup."
9585   (interactive "fImport file: \nP")
9586   (let ((group gnus-newsgroup-name)
9587         (now (current-time))
9588         atts lines group-art)
9589     (unless (gnus-check-backend-function 'request-accept-article group)
9590       (error "%s does not support article importing" group))
9591     (or (file-readable-p file)
9592         (not (file-regular-p file))
9593         (error "Can't read %s" file))
9594     (save-excursion
9595       (set-buffer (gnus-get-buffer-create " *import file*"))
9596       (erase-buffer)
9597       (nnheader-insert-file-contents file)
9598       (goto-char (point-min))
9599       (if (nnheader-article-p)
9600           (save-restriction
9601             (goto-char (point-min))
9602             (search-forward "\n\n" nil t)
9603             (narrow-to-region (point-min) (1- (point)))
9604             (goto-char (point-min))
9605             (unless (re-search-forward "^date:" nil t)
9606               (goto-char (point-max))
9607               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9608        ;; This doesn't look like an article, so we fudge some headers.
9609         (setq atts (file-attributes file)
9610               lines (count-lines (point-min) (point-max)))
9611         (insert "From: " (read-string "From: ") "\n"
9612                 "Subject: " (read-string "Subject: ") "\n"
9613                 "Date: " (message-make-date (nth 5 atts)) "\n"
9614                 "Message-ID: " (message-make-message-id) "\n"
9615                 "Lines: " (int-to-string lines) "\n"
9616                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9617       (setq group-art (gnus-request-accept-article group nil t))
9618       (kill-buffer (current-buffer)))
9619     (setq gnus-newsgroup-active (gnus-activate-group group))
9620     (forward-line 1)
9621     (gnus-summary-goto-article (cdr group-art) nil t)
9622     (when edit
9623       (gnus-summary-edit-article))))
9624
9625 (defun gnus-summary-create-article ()
9626   "Create an article in a mail newsgroup."
9627   (interactive)
9628   (let ((group gnus-newsgroup-name)
9629         (now (current-time))
9630         group-art)
9631     (unless (gnus-check-backend-function 'request-accept-article group)
9632       (error "%s does not support article importing" group))
9633     (save-excursion
9634       (set-buffer (gnus-get-buffer-create " *import file*"))
9635       (erase-buffer)
9636       (goto-char (point-min))
9637       ;; This doesn't look like an article, so we fudge some headers.
9638       (insert "From: " (read-string "From: ") "\n"
9639               "Subject: " (read-string "Subject: ") "\n"
9640               "Date: " (message-make-date now) "\n"
9641               "Message-ID: " (message-make-message-id) "\n")
9642       (setq group-art (gnus-request-accept-article group nil t))
9643       (kill-buffer (current-buffer)))
9644     (setq gnus-newsgroup-active (gnus-activate-group group))
9645     (forward-line 1)
9646     (gnus-summary-goto-article (cdr group-art) nil t)
9647     (gnus-summary-edit-article)))
9648
9649 (defun gnus-summary-article-posted-p ()
9650   "Say whether the current (mail) article is available from news as well.
9651 This will be the case if the article has both been mailed and posted."
9652   (interactive)
9653   (let ((id (mail-header-references (gnus-summary-article-header)))
9654         (gnus-override-method (car (gnus-refer-article-methods))))
9655     (if (gnus-request-head id "")
9656         (gnus-message 2 "The current message was found on %s"
9657                       gnus-override-method)
9658       (gnus-message 2 "The current message couldn't be found on %s"
9659                     gnus-override-method)
9660       nil)))
9661
9662 (defun gnus-summary-expire-articles (&optional now)
9663   "Expire all articles that are marked as expirable in the current group."
9664   (interactive)
9665   (when (and (not gnus-group-is-exiting-without-update-p)
9666              (gnus-check-backend-function
9667               'request-expire-articles gnus-newsgroup-name))
9668     ;; This backend supports expiry.
9669     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9670            (expirable (if total
9671                           (progn
9672                             ;; We need to update the info for
9673                             ;; this group for `gnus-list-of-read-articles'
9674                             ;; to give us the right answer.
9675                             (gnus-run-hooks 'gnus-exit-group-hook)
9676                             (gnus-summary-update-info)
9677                             (gnus-list-of-read-articles gnus-newsgroup-name))
9678                         (setq gnus-newsgroup-expirable
9679                               (sort gnus-newsgroup-expirable '<))))
9680            (expiry-wait (if now 'immediate
9681                           (gnus-group-find-parameter
9682                            gnus-newsgroup-name 'expiry-wait)))
9683            (nnmail-expiry-target
9684             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9685                 nnmail-expiry-target))
9686            es)
9687       (when expirable
9688         ;; There are expirable articles in this group, so we run them
9689         ;; through the expiry process.
9690         (gnus-message 6 "Expiring articles...")
9691         (unless (gnus-check-group gnus-newsgroup-name)
9692           (error "Can't open server for %s" gnus-newsgroup-name))
9693         ;; The list of articles that weren't expired is returned.
9694         (save-excursion
9695           (if expiry-wait
9696               (let ((nnmail-expiry-wait-function nil)
9697                     (nnmail-expiry-wait expiry-wait))
9698                 (setq es (gnus-request-expire-articles
9699                           expirable gnus-newsgroup-name)))
9700             (setq es (gnus-request-expire-articles
9701                       expirable gnus-newsgroup-name)))
9702           (unless total
9703             (setq gnus-newsgroup-expirable es))
9704           ;; We go through the old list of expirable, and mark all
9705           ;; really expired articles as nonexistent.
9706           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9707             (let ((gnus-use-cache nil))
9708               (dolist (article expirable)
9709                 (when (and (not (memq article es))
9710                            (gnus-data-find article))
9711                   (gnus-summary-mark-article article gnus-canceled-mark)
9712                   (run-hook-with-args 'gnus-summary-article-expire-hook
9713                                       'delete
9714                                       (gnus-data-header
9715                                        (assoc article (gnus-data-list nil)))
9716                                       gnus-newsgroup-name
9717                                       nil
9718                                       nil))))))
9719         (gnus-message 6 "Expiring articles...done")))))
9720
9721 (defun gnus-summary-expire-articles-now ()
9722   "Expunge all expirable articles in the current group.
9723 This means that *all* articles that are marked as expirable will be
9724 deleted forever, right now."
9725   (interactive)
9726   (or gnus-expert-user
9727       (gnus-yes-or-no-p
9728        "Are you really, really sure you want to delete all expirable messages? ")
9729       (error "Phew!"))
9730   (gnus-summary-expire-articles t))
9731
9732 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9733 (defun gnus-summary-delete-article (&optional n)
9734   "Delete the N next (mail) articles.
9735 This command actually deletes articles.  This is not a marking
9736 command.  The article will disappear forever from your life, never to
9737 return.
9738
9739 If N is negative, delete backwards.
9740 If N is nil and articles have been marked with the process mark,
9741 delete these instead.
9742
9743 If `gnus-novice-user' is non-nil you will be asked for
9744 confirmation before the articles are deleted."
9745   (interactive "P")
9746   (unless (gnus-check-backend-function 'request-expire-articles
9747                                        gnus-newsgroup-name)
9748     (error "The current newsgroup does not support article deletion"))
9749   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9750     (error "Couldn't open server"))
9751   ;; Compute the list of articles to delete.
9752   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9753         (nnmail-expiry-target 'delete)
9754         not-deleted)
9755     (if (and gnus-novice-user
9756              (not (gnus-yes-or-no-p
9757                    (format "Do you really want to delete %s forever? "
9758                            (if (> (length articles) 1)
9759                                (format "these %s articles" (length articles))
9760                              "this article")))))
9761         ()
9762       ;; Delete the articles.
9763       (setq not-deleted (gnus-request-expire-articles
9764                          articles gnus-newsgroup-name 'force))
9765       (while articles
9766         (gnus-summary-remove-process-mark (car articles))
9767         ;; The backend might not have been able to delete the article
9768         ;; after all.
9769         (unless (memq (car articles) not-deleted)
9770           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9771         (let* ((article (car articles))
9772                (ghead  (gnus-data-header
9773                                     (assoc article (gnus-data-list nil)))))
9774           (run-hook-with-args 'gnus-summary-article-delete-hook
9775                               'delete ghead gnus-newsgroup-name nil
9776                               nil))
9777         (setq articles (cdr articles)))
9778       (when not-deleted
9779         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9780     (gnus-summary-position-point)
9781     (gnus-set-mode-line 'summary)
9782     not-deleted))
9783
9784 (defun gnus-summary-edit-article (&optional arg)
9785   "Edit the current article.
9786 This will have permanent effect only in mail groups.
9787 If ARG is nil, edit the decoded articles.
9788 If ARG is 1, edit the raw articles.
9789 If ARG is 2, edit the raw articles even in read-only groups.
9790 If ARG is 3, edit the articles with the current handles.
9791 Otherwise, allow editing of articles even in read-only
9792 groups."
9793   (interactive "P")
9794   (let (force raw current-handles)
9795     (cond
9796      ((null arg))
9797      ((eq arg 1)
9798       (setq raw t))
9799      ((eq arg 2)
9800       (setq raw t
9801             force t))
9802      ((eq arg 3)
9803       (setq current-handles
9804             (and (gnus-buffer-live-p gnus-article-buffer)
9805                  (with-current-buffer gnus-article-buffer
9806                    (prog1
9807                        gnus-article-mime-handles
9808                      (setq gnus-article-mime-handles nil))))))
9809      (t
9810       (setq force t)))
9811     (when (and raw (not force)
9812                (member gnus-newsgroup-name '("nndraft:delayed"
9813                                              "nndraft:drafts"
9814                                              "nndraft:queue")))
9815       (error "Can't edit the raw article in group %s"
9816              gnus-newsgroup-name))
9817     (save-excursion
9818       (set-buffer gnus-summary-buffer)
9819       (let ((mail-parse-charset gnus-newsgroup-charset)
9820             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9821         (gnus-set-global-variables)
9822         (when (and (not force)
9823                    (gnus-group-read-only-p))
9824           (error "The current newsgroup does not support article editing"))
9825         (gnus-summary-show-article t)
9826         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9827           (with-current-buffer gnus-article-buffer
9828             (mm-enable-multibyte)))
9829         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9830             (setq raw t))
9831         (gnus-article-edit-article
9832          (if raw 'ignore
9833            `(lambda ()
9834               (let ((mbl mml-buffer-list))
9835                 (setq mml-buffer-list nil)
9836                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9837                   (mime-to-mml ,'current-handles))
9838                 (let ((mbl1 mml-buffer-list))
9839                   (setq mml-buffer-list mbl)
9840                   (set (make-local-variable 'mml-buffer-list) mbl1))
9841                 (gnus-make-local-hook 'kill-buffer-hook)
9842                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9843          `(lambda (no-highlight)
9844             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9845                   (message-options message-options)
9846                   (message-options-set-recipient)
9847                   (mail-parse-ignored-charsets
9848                    ',gnus-newsgroup-ignored-charsets))
9849               ,(if (not raw) '(progn
9850                                 (mml-to-mime)
9851                                 (mml-destroy-buffers)
9852                                 (remove-hook 'kill-buffer-hook
9853                                              'mml-destroy-buffers t)
9854                                 (kill-local-variable 'mml-buffer-list)))
9855               (gnus-summary-edit-article-done
9856                ,(or (mail-header-references gnus-current-headers) "")
9857                ,(gnus-group-read-only-p)
9858                ,gnus-summary-buffer no-highlight))))))))
9859
9860 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9861
9862 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9863                                                  no-highlight)
9864   "Make edits to the current article permanent."
9865   (interactive)
9866   (save-excursion
9867    ;; The buffer restriction contains the entire article if it exists.
9868     (when (article-goto-body)
9869       (let ((lines (count-lines (point) (point-max)))
9870             (length (- (point-max) (point)))
9871             (case-fold-search t)
9872             (body (copy-marker (point))))
9873         (goto-char (point-min))
9874         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9875           (delete-region (match-beginning 1) (match-end 1))
9876           (insert (number-to-string length)))
9877         (goto-char (point-min))
9878         (when (re-search-forward
9879                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9880           (delete-region (match-beginning 1) (match-end 1))
9881           (insert (number-to-string length)))
9882         (goto-char (point-min))
9883         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9884           (delete-region (match-beginning 1) (match-end 1))
9885           (insert (number-to-string lines))))))
9886   ;; Replace the article.
9887   (let ((buf (current-buffer)))
9888     (with-temp-buffer
9889       (insert-buffer-substring buf)
9890
9891       (if (and (not read-only)
9892                (not (gnus-request-replace-article
9893                      (cdr gnus-article-current) (car gnus-article-current)
9894                      (current-buffer) t)))
9895           (error "Couldn't replace article")
9896         ;; Update the summary buffer.
9897         (if (and references
9898                  (equal (message-tokenize-header references " ")
9899                         (message-tokenize-header
9900                          (or (message-fetch-field "references") "") " ")))
9901             ;; We only have to update this line.
9902             (save-excursion
9903               (save-restriction
9904                 (message-narrow-to-head)
9905                 (let ((head (buffer-substring-no-properties
9906                              (point-min) (point-max)))
9907                       header)
9908                   (with-temp-buffer
9909                     (insert (format "211 %d Article retrieved.\n"
9910                                     (cdr gnus-article-current)))
9911                     (insert head)
9912                     (insert ".\n")
9913                     (let ((nntp-server-buffer (current-buffer)))
9914                       (setq header (car (gnus-get-newsgroup-headers
9915                                          nil t))))
9916                     (save-excursion
9917                       (set-buffer gnus-summary-buffer)
9918                       (gnus-data-set-header
9919                        (gnus-data-find (cdr gnus-article-current))
9920                        header)
9921                       (gnus-summary-update-article-line
9922                        (cdr gnus-article-current) header)
9923                       (if (gnus-summary-goto-subject
9924                            (cdr gnus-article-current) nil t)
9925                           (gnus-summary-update-secondary-mark
9926                            (cdr gnus-article-current))))))))
9927           ;; Update threads.
9928           (set-buffer (or buffer gnus-summary-buffer))
9929           (gnus-summary-update-article (cdr gnus-article-current))
9930           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9931               (gnus-summary-update-secondary-mark
9932                (cdr gnus-article-current))))
9933         ;; Prettify the article buffer again.
9934         (unless no-highlight
9935           (save-excursion
9936             (set-buffer gnus-article-buffer)
9937             ;;;!!! Fix this -- article should be rehighlighted.
9938             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9939             (set-buffer gnus-original-article-buffer)
9940             (gnus-request-article
9941              (cdr gnus-article-current)
9942              (car gnus-article-current) (current-buffer))))
9943         ;; Prettify the summary buffer line.
9944         (when (gnus-visual-p 'summary-highlight 'highlight)
9945           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9946
9947 (defun gnus-summary-edit-wash (key)
9948   "Perform editing command KEY in the article buffer."
9949   (interactive
9950    (list
9951     (progn
9952       (message "%s" (concat (this-command-keys) "- "))
9953       (read-char))))
9954   (message "")
9955   (gnus-summary-edit-article)
9956   (execute-kbd-macro (concat (this-command-keys) key))
9957   (gnus-article-edit-done))
9958
9959 ;;; Respooling
9960
9961 (defun gnus-summary-respool-query (&optional silent trace)
9962   "Query where the respool algorithm would put this article."
9963   (interactive)
9964   (let (gnus-mark-article-hook)
9965     (gnus-summary-select-article)
9966     (save-excursion
9967       (set-buffer gnus-original-article-buffer)
9968       (let ((groups (nnmail-article-group 'identity trace)))
9969         (unless silent
9970           (if groups
9971               (message "This message would go to %s"
9972                        (mapconcat 'car groups ", "))
9973             (message "This message would go to no groups"))
9974           groups)))))
9975
9976 (defun gnus-summary-respool-trace ()
9977   "Trace where the respool algorithm would put this article.
9978 Display a buffer showing all fancy splitting patterns which matched."
9979   (interactive)
9980   (gnus-summary-respool-query nil t))
9981
9982 ;; Summary marking commands.
9983
9984 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9985   "Mark articles which has the same subject as read, and then select the next.
9986 If UNMARK is positive, remove any kind of mark.
9987 If UNMARK is negative, tick articles."
9988   (interactive "P")
9989   (when unmark
9990     (setq unmark (prefix-numeric-value unmark)))
9991   (let ((count
9992          (gnus-summary-mark-same-subject
9993           (gnus-summary-article-subject) unmark)))
9994     ;; Select next unread article.  If auto-select-same mode, should
9995     ;; select the first unread article.
9996     (gnus-summary-next-article t (and gnus-auto-select-same
9997                                       (gnus-summary-article-subject)))
9998     (gnus-message 7 "%d article%s marked as %s"
9999                   count (if (= count 1) " is" "s are")
10000                   (if unmark "unread" "read"))))
10001
10002 (defun gnus-summary-kill-same-subject (&optional unmark)
10003   "Mark articles which has the same subject as read.
10004 If UNMARK is positive, remove any kind of mark.
10005 If UNMARK is negative, tick articles."
10006   (interactive "P")
10007   (when unmark
10008     (setq unmark (prefix-numeric-value unmark)))
10009   (let ((count
10010          (gnus-summary-mark-same-subject
10011           (gnus-summary-article-subject) unmark)))
10012     ;; If marked as read, go to next unread subject.
10013     (when (null unmark)
10014       ;; Go to next unread subject.
10015       (gnus-summary-next-subject 1 t))
10016     (gnus-message 7 "%d articles are marked as %s"
10017                   count (if unmark "unread" "read"))))
10018
10019 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10020   "Mark articles with same SUBJECT as read, and return marked number.
10021 If optional argument UNMARK is positive, remove any kinds of marks.
10022 If optional argument UNMARK is negative, mark articles as unread instead."
10023   (let ((count 1))
10024     (save-excursion
10025       (cond
10026        ((null unmark)                   ; Mark as read.
10027         (while (and
10028                 (progn
10029                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10030                   (gnus-summary-show-thread) t)
10031                 (gnus-summary-find-subject subject))
10032           (setq count (1+ count))))
10033        ((> unmark 0)                    ; Tick.
10034         (while (and
10035                 (progn
10036                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10037                   (gnus-summary-show-thread) t)
10038                 (gnus-summary-find-subject subject))
10039           (setq count (1+ count))))
10040        (t                               ; Mark as unread.
10041         (while (and
10042                 (progn
10043                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10044                   (gnus-summary-show-thread) t)
10045                 (gnus-summary-find-subject subject))
10046           (setq count (1+ count)))))
10047       (gnus-set-mode-line 'summary)
10048       ;; Return the number of marked articles.
10049       count)))
10050
10051 (defun gnus-summary-mark-as-processable (n &optional unmark)
10052   "Set the process mark on the next N articles.
10053 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10054 the process mark instead.  The difference between N and the actual
10055 number of articles marked is returned."
10056   (interactive "P")
10057   (if (and (null n) (gnus-region-active-p))
10058       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10059     (setq n (prefix-numeric-value n))
10060     (let ((backward (< n 0))
10061           (n (abs n)))
10062       (while (and
10063               (> n 0)
10064               (if unmark
10065                   (gnus-summary-remove-process-mark
10066                    (gnus-summary-article-number))
10067                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10068               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10069         (setq n (1- n)))
10070       (when (/= 0 n)
10071         (gnus-message 7 "No more articles"))
10072       (gnus-summary-recenter)
10073       (gnus-summary-position-point)
10074       n)))
10075
10076 (defun gnus-summary-unmark-as-processable (n)
10077   "Remove the process mark from the next N articles.
10078 If N is negative, unmark backward instead.  The difference between N and
10079 the actual number of articles unmarked is returned."
10080   (interactive "P")
10081   (gnus-summary-mark-as-processable n t))
10082
10083 (defun gnus-summary-unmark-all-processable ()
10084   "Remove the process mark from all articles."
10085   (interactive)
10086   (save-excursion
10087     (while gnus-newsgroup-processable
10088       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10089   (gnus-summary-position-point))
10090
10091 (defun gnus-summary-add-mark (article type)
10092   "Mark ARTICLE with a mark of TYPE."
10093   (let ((vtype (car (assq type gnus-article-mark-lists)))
10094         var)
10095     (if (not vtype)
10096         (error "No such mark type: %s" type)
10097       (setq var (intern (format "gnus-newsgroup-%s" type)))
10098       (set var (cons article (symbol-value var)))
10099       (if (memq type '(processable cached replied forwarded recent saved))
10100           (gnus-summary-update-secondary-mark article)
10101         ;;; !!! This is bogus.  We should find out what primary
10102         ;;; !!! mark we want to set.
10103         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10104
10105 (defun gnus-summary-mark-as-expirable (n)
10106   "Mark N articles forward as expirable.
10107 If N is negative, mark backward instead.  The difference between N and
10108 the actual number of articles marked is returned."
10109   (interactive "p")
10110   (gnus-summary-mark-forward n gnus-expirable-mark))
10111
10112 (defun gnus-summary-mark-as-spam (n)
10113   "Mark N articles forward as spam.
10114 If N is negative, mark backward instead.  The difference between N and
10115 the actual number of articles marked is returned."
10116   (interactive "p")
10117   (gnus-summary-mark-forward n gnus-spam-mark))
10118
10119 (defun gnus-summary-mark-article-as-replied (article)
10120   "Mark ARTICLE as replied to and update the summary line.
10121 ARTICLE can also be a list of articles."
10122   (interactive (list (gnus-summary-article-number)))
10123   (let ((articles (if (listp article) article (list article))))
10124     (dolist (article articles)
10125       (unless (numberp article)
10126         (error "%s is not a number" article))
10127       (push article gnus-newsgroup-replied)
10128       (let ((buffer-read-only nil))
10129         (when (gnus-summary-goto-subject article nil t)
10130           (gnus-summary-update-secondary-mark article))))))
10131
10132 (defun gnus-summary-mark-article-as-forwarded (article)
10133   "Mark ARTICLE as forwarded and update the summary line.
10134 ARTICLE can also be a list of articles."
10135   (let ((articles (if (listp article) article (list article))))
10136     (dolist (article articles)
10137       (push article gnus-newsgroup-forwarded)
10138       (let ((buffer-read-only nil))
10139         (when (gnus-summary-goto-subject article nil t)
10140           (gnus-summary-update-secondary-mark article))))))
10141
10142 (defun gnus-summary-set-bookmark (article)
10143   "Set a bookmark in current article."
10144   (interactive (list (gnus-summary-article-number)))
10145   (when (or (not (get-buffer gnus-article-buffer))
10146             (not gnus-current-article)
10147             (not gnus-article-current)
10148             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10149     (error "No current article selected"))
10150   ;; Remove old bookmark, if one exists.
10151   (gnus-pull article gnus-newsgroup-bookmarks)
10152   ;; Set the new bookmark, which is on the form
10153   ;; (article-number . line-number-in-body).
10154   (push
10155    (cons article
10156          (with-current-buffer gnus-article-buffer
10157            (count-lines
10158             (min (point)
10159                  (save-excursion
10160                    (article-goto-body)
10161                    (point)))
10162             (point))))
10163    gnus-newsgroup-bookmarks)
10164   (gnus-message 6 "A bookmark has been added to the current article."))
10165
10166 (defun gnus-summary-remove-bookmark (article)
10167   "Remove the bookmark from the current article."
10168   (interactive (list (gnus-summary-article-number)))
10169   ;; Remove old bookmark, if one exists.
10170   (if (not (assq article gnus-newsgroup-bookmarks))
10171       (gnus-message 6 "No bookmark in current article.")
10172     (gnus-pull article gnus-newsgroup-bookmarks)
10173     (gnus-message 6 "Removed bookmark.")))
10174
10175 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10176 (defun gnus-summary-mark-as-dormant (n)
10177   "Mark N articles forward as dormant.
10178 If N is negative, mark backward instead.  The difference between N and
10179 the actual number of articles marked is returned."
10180   (interactive "p")
10181   (gnus-summary-mark-forward n gnus-dormant-mark))
10182
10183 (defun gnus-summary-set-process-mark (article)
10184   "Set the process mark on ARTICLE and update the summary line."
10185   (setq gnus-newsgroup-processable
10186         (cons article
10187               (delq article gnus-newsgroup-processable)))
10188   (when (gnus-summary-goto-subject article)
10189     (gnus-summary-show-thread)
10190     (gnus-summary-goto-subject article)
10191     (gnus-summary-update-secondary-mark article)))
10192
10193 (defun gnus-summary-remove-process-mark (&rest articles)
10194   "Remove the process mark from ARTICLES and update the summary line."
10195   (dolist (article articles)
10196     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10197     (when (gnus-summary-goto-subject article)
10198       (gnus-summary-show-thread)
10199       (gnus-summary-goto-subject article)
10200       (gnus-summary-update-secondary-mark article)))
10201   t)
10202
10203 (defun gnus-summary-set-saved-mark (article)
10204   "Set the process mark on ARTICLE and update the summary line."
10205   (push article gnus-newsgroup-saved)
10206   (when (gnus-summary-goto-subject article)
10207     (gnus-summary-update-secondary-mark article)))
10208
10209 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10210   "Mark N articles as read forwards.
10211 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10212 The difference between N and the actual number of articles marked is
10213 returned.
10214 If NO-EXPIRE, auto-expiry will be inhibited."
10215   (interactive "p")
10216   (gnus-summary-show-thread)
10217   (let ((backward (< n 0))
10218         (gnus-summary-goto-unread
10219          (and gnus-summary-goto-unread
10220               (not (eq gnus-summary-goto-unread 'never))
10221               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10222                                     gnus-ticked-mark gnus-dormant-mark)))))
10223         (n (abs n))
10224         (mark (or mark gnus-del-mark)))
10225     (while (and (> n 0)
10226                 (gnus-summary-mark-article nil mark no-expire)
10227                 (zerop (gnus-summary-next-subject
10228                         (if backward -1 1)
10229                         (and gnus-summary-goto-unread
10230                              (not (eq gnus-summary-goto-unread 'never)))
10231                         t)))
10232       (setq n (1- n)))
10233     (when (/= 0 n)
10234       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10235     (gnus-summary-recenter)
10236     (gnus-summary-position-point)
10237     (gnus-set-mode-line 'summary)
10238     n))
10239
10240 (defun gnus-summary-mark-article-as-read (mark)
10241   "Mark the current article quickly as read with MARK."
10242   (let ((article (gnus-summary-article-number)))
10243     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10244     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10245     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10246     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10247     (push (cons article mark) gnus-newsgroup-reads)
10248     ;; Possibly remove from cache, if that is used.
10249     (when gnus-use-cache
10250       (gnus-cache-enter-remove-article article))
10251     ;; Allow the backend to change the mark.
10252     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10253     ;; Check for auto-expiry.
10254     (when (and gnus-newsgroup-auto-expire
10255                (memq mark gnus-auto-expirable-marks))
10256       (setq mark gnus-expirable-mark)
10257       ;; Let the backend know about the mark change.
10258       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10259       (push article gnus-newsgroup-expirable))
10260     ;; Set the mark in the buffer.
10261     (gnus-summary-update-mark mark 'unread)
10262     t))
10263
10264 (defun gnus-summary-mark-article-as-unread (mark)
10265   "Mark the current article quickly as unread with MARK."
10266   (let* ((article (gnus-summary-article-number))
10267          (old-mark (gnus-summary-article-mark article)))
10268     ;; Allow the backend to change the mark.
10269     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10270     (if (eq mark old-mark)
10271         t
10272       (if (<= article 0)
10273           (progn
10274             (gnus-error 1 "Can't mark negative article numbers")
10275             nil)
10276         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10277         (setq gnus-newsgroup-spam-marked
10278               (delq article gnus-newsgroup-spam-marked))
10279         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10280         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10281         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10282         (cond ((= mark gnus-ticked-mark)
10283                (setq gnus-newsgroup-marked
10284                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10285                                               article)))
10286               ((= mark gnus-spam-mark)
10287                (setq gnus-newsgroup-spam-marked
10288                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10289                                               article)))
10290               ((= mark gnus-dormant-mark)
10291                (setq gnus-newsgroup-dormant
10292                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10293                                               article)))
10294               (t
10295                (setq gnus-newsgroup-unreads
10296                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10297                                               article))))
10298         (gnus-pull article gnus-newsgroup-reads)
10299
10300         ;; See whether the article is to be put in the cache.
10301         (and gnus-use-cache
10302              (vectorp (gnus-summary-article-header article))
10303              (save-excursion
10304                (gnus-cache-possibly-enter-article
10305                 gnus-newsgroup-name article
10306                 (= mark gnus-ticked-mark)
10307                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10308
10309         ;; Fix the mark.
10310         (gnus-summary-update-mark mark 'unread)
10311         t))))
10312
10313 (defun gnus-summary-mark-article (&optional article mark no-expire)
10314   "Mark ARTICLE with MARK.  MARK can be any character.
10315 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10316 `??' (dormant) and `?E' (expirable).
10317 If MARK is nil, then the default character `?r' is used.
10318 If ARTICLE is nil, then the article on the current line will be
10319 marked.
10320 If NO-EXPIRE, auto-expiry will be inhibited."
10321   ;; The mark might be a string.
10322   (when (stringp mark)
10323     (setq mark (aref mark 0)))
10324   ;; If no mark is given, then we check auto-expiring.
10325   (when (null mark)
10326     (setq mark gnus-del-mark))
10327   (when (and (not no-expire)
10328              gnus-newsgroup-auto-expire
10329              (memq mark gnus-auto-expirable-marks))
10330     (setq mark gnus-expirable-mark))
10331   (let ((article (or article (gnus-summary-article-number)))
10332         (old-mark (gnus-summary-article-mark article)))
10333     ;; Allow the backend to change the mark.
10334     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10335     (if (eq mark old-mark)
10336         t
10337       (unless article
10338         (error "No article on current line"))
10339       (if (not (if (or (= mark gnus-unread-mark)
10340                        (= mark gnus-ticked-mark)
10341                        (= mark gnus-spam-mark)
10342                        (= mark gnus-dormant-mark))
10343                    (gnus-mark-article-as-unread article mark)
10344                  (gnus-mark-article-as-read article mark)))
10345           t
10346         ;; See whether the article is to be put in the cache.
10347         (and gnus-use-cache
10348              (not (= mark gnus-canceled-mark))
10349              (vectorp (gnus-summary-article-header article))
10350              (save-excursion
10351                (gnus-cache-possibly-enter-article
10352                 gnus-newsgroup-name article
10353                 (= mark gnus-ticked-mark)
10354                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10355
10356         (when (gnus-summary-goto-subject article nil t)
10357           (let ((buffer-read-only nil))
10358             (gnus-summary-show-thread)
10359             ;; Fix the mark.
10360             (gnus-summary-update-mark mark 'unread)
10361             t))))))
10362
10363 (defun gnus-summary-update-secondary-mark (article)
10364   "Update the secondary (read, process, cache) mark."
10365   (gnus-summary-update-mark
10366    (cond ((memq article gnus-newsgroup-processable)
10367           gnus-process-mark)
10368          ((memq article gnus-newsgroup-cached)
10369           gnus-cached-mark)
10370          ((memq article gnus-newsgroup-replied)
10371           gnus-replied-mark)
10372          ((memq article gnus-newsgroup-forwarded)
10373           gnus-forwarded-mark)
10374          ((memq article gnus-newsgroup-saved)
10375           gnus-saved-mark)
10376          ((memq article gnus-newsgroup-recent)
10377           gnus-recent-mark)
10378          ((memq article gnus-newsgroup-unseen)
10379           gnus-unseen-mark)
10380          (t gnus-no-mark))
10381    'replied)
10382   (when (gnus-visual-p 'summary-highlight 'highlight)
10383     (gnus-run-hooks 'gnus-summary-update-hook))
10384   t)
10385
10386 (defun gnus-summary-update-download-mark (article)
10387   "Update the download mark."
10388   (gnus-summary-update-mark
10389    (cond ((memq article gnus-newsgroup-undownloaded)
10390           gnus-undownloaded-mark)
10391          (gnus-newsgroup-agentized
10392           gnus-downloaded-mark)
10393          (t
10394           gnus-no-mark))
10395    'download)
10396   (gnus-summary-update-line t)
10397   t)
10398
10399 (defun gnus-summary-update-mark (mark type)
10400   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10401         (buffer-read-only nil))
10402     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10403     (when forward
10404       (when (looking-at "\r")
10405         (incf forward))
10406       (when (<= (+ forward (point)) (point-max))
10407         ;; Go to the right position on the line.
10408         (goto-char (+ forward (point)))
10409         ;; Replace the old mark with the new mark.
10410         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10411         ;; Optionally update the marks by some user rule.
10412         (when (eq type 'unread)
10413           (gnus-data-set-mark
10414            (gnus-data-find (gnus-summary-article-number)) mark)
10415           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10416
10417 (defun gnus-mark-article-as-read (article &optional mark)
10418   "Enter ARTICLE in the pertinent lists and remove it from others."
10419   ;; Make the article expirable.
10420   (let ((mark (or mark gnus-del-mark)))
10421     (setq gnus-newsgroup-expirable
10422           (if (= mark gnus-expirable-mark)
10423               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10424             (delq article gnus-newsgroup-expirable)))
10425     ;; Remove from unread and marked lists.
10426     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10427     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10428     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10429     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10430     (push (cons article mark) gnus-newsgroup-reads)
10431     ;; Possibly remove from cache, if that is used.
10432     (when gnus-use-cache
10433       (gnus-cache-enter-remove-article article))
10434     t))
10435
10436 (defun gnus-mark-article-as-unread (article &optional mark)
10437   "Enter ARTICLE in the pertinent lists and remove it from others."
10438   (let ((mark (or mark gnus-ticked-mark)))
10439     (if (<= article 0)
10440         (progn
10441           (gnus-error 1 "Can't mark negative article numbers")
10442           nil)
10443       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10444             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10445             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10446             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10447             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10448
10449       ;; Unsuppress duplicates?
10450       (when gnus-suppress-duplicates
10451         (gnus-dup-unsuppress-article article))
10452
10453       (cond ((= mark gnus-ticked-mark)
10454              (setq gnus-newsgroup-marked
10455                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10456             ((= mark gnus-spam-mark)
10457              (setq gnus-newsgroup-spam-marked
10458                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10459                                             article)))
10460             ((= mark gnus-dormant-mark)
10461              (setq gnus-newsgroup-dormant
10462                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10463             (t
10464              (setq gnus-newsgroup-unreads
10465                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10466       (gnus-pull article gnus-newsgroup-reads)
10467       t)))
10468
10469 (defalias 'gnus-summary-mark-as-unread-forward
10470   'gnus-summary-tick-article-forward)
10471 (make-obsolete 'gnus-summary-mark-as-unread-forward
10472                'gnus-summary-tick-article-forward)
10473 (defun gnus-summary-tick-article-forward (n)
10474   "Tick N articles forwards.
10475 If N is negative, tick backwards instead.
10476 The difference between N and the number of articles ticked is returned."
10477   (interactive "p")
10478   (gnus-summary-mark-forward n gnus-ticked-mark))
10479
10480 (defalias 'gnus-summary-mark-as-unread-backward
10481   'gnus-summary-tick-article-backward)
10482 (make-obsolete 'gnus-summary-mark-as-unread-backward
10483                'gnus-summary-tick-article-backward)
10484 (defun gnus-summary-tick-article-backward (n)
10485   "Tick N articles backwards.
10486 The difference between N and the number of articles ticked is returned."
10487   (interactive "p")
10488   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10489
10490 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10491 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10492 (defun gnus-summary-tick-article (&optional article clear-mark)
10493   "Mark current article as unread.
10494 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10495 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10496   (interactive)
10497   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10498                                        gnus-ticked-mark)))
10499
10500 (defun gnus-summary-mark-as-read-forward (n)
10501   "Mark N articles as read forwards.
10502 If N is negative, mark backwards instead.
10503 The difference between N and the actual number of articles marked is
10504 returned."
10505   (interactive "p")
10506   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10507
10508 (defun gnus-summary-mark-as-read-backward (n)
10509   "Mark the N articles as read backwards.
10510 The difference between N and the actual number of articles marked is
10511 returned."
10512   (interactive "p")
10513   (gnus-summary-mark-forward
10514    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10515
10516 (defun gnus-summary-mark-as-read (&optional article mark)
10517   "Mark current article as read.
10518 ARTICLE specifies the article to be marked as read.
10519 MARK specifies a string to be inserted at the beginning of the line."
10520   (gnus-summary-mark-article article mark))
10521
10522 (defun gnus-summary-clear-mark-forward (n)
10523   "Clear marks from N articles forward.
10524 If N is negative, clear backward instead.
10525 The difference between N and the number of marks cleared is returned."
10526   (interactive "p")
10527   (gnus-summary-mark-forward n gnus-unread-mark))
10528
10529 (defun gnus-summary-clear-mark-backward (n)
10530   "Clear marks from N articles backward.
10531 The difference between N and the number of marks cleared is returned."
10532   (interactive "p")
10533   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10534
10535 (defun gnus-summary-mark-unread-as-read ()
10536   "Intended to be used by `gnus-summary-mark-article-hook'."
10537   (when (memq gnus-current-article gnus-newsgroup-unreads)
10538     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10539
10540 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10541   "Intended to be used by `gnus-summary-mark-article-hook'."
10542   (let ((mark (gnus-summary-article-mark)))
10543     (when (or (gnus-unread-mark-p mark)
10544               (gnus-read-mark-p mark))
10545       (gnus-summary-mark-article gnus-current-article
10546                                  (or new-mark gnus-read-mark)))))
10547
10548 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10549   "Intended to be used by `gnus-summary-mark-article-hook'."
10550   (let ((mark (gnus-summary-article-mark)))
10551     (when (or (gnus-unread-mark-p mark)
10552               (gnus-read-mark-p mark))
10553       (gnus-summary-mark-article (gnus-summary-article-number)
10554                                  (or new-mark gnus-read-mark)))))
10555
10556 (defun gnus-summary-mark-unread-as-ticked ()
10557   "Intended to be used by `gnus-summary-mark-article-hook'."
10558   (when (memq gnus-current-article gnus-newsgroup-unreads)
10559     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10560
10561 (defun gnus-summary-mark-region-as-read (point mark all)
10562   "Mark all unread articles between point and mark as read.
10563 If given a prefix, mark all articles between point and mark as read,
10564 even ticked and dormant ones."
10565   (interactive "r\nP")
10566   (save-excursion
10567     (let (article)
10568       (goto-char point)
10569       (beginning-of-line)
10570       (while (and
10571               (< (point) mark)
10572               (progn
10573                 (when (or all
10574                           (memq (setq article (gnus-summary-article-number))
10575                                 gnus-newsgroup-unreads))
10576                   (gnus-summary-mark-article article gnus-del-mark))
10577                 t)
10578               (gnus-summary-find-next))))))
10579
10580 (defun gnus-summary-mark-below (score mark)
10581   "Mark articles with score less than SCORE with MARK."
10582   (interactive "P\ncMark: ")
10583   (setq score (if score
10584                   (prefix-numeric-value score)
10585                 (or gnus-summary-default-score 0)))
10586   (save-excursion
10587     (set-buffer gnus-summary-buffer)
10588     (goto-char (point-min))
10589     (while
10590         (progn
10591           (and (< (gnus-summary-article-score) score)
10592                (gnus-summary-mark-article nil mark))
10593           (gnus-summary-find-next)))))
10594
10595 (defun gnus-summary-kill-below (&optional score)
10596   "Mark articles with score below SCORE as read."
10597   (interactive "P")
10598   (gnus-summary-mark-below score gnus-killed-mark))
10599
10600 (defun gnus-summary-clear-above (&optional score)
10601   "Clear all marks from articles with score above SCORE."
10602   (interactive "P")
10603   (gnus-summary-mark-above score gnus-unread-mark))
10604
10605 (defun gnus-summary-tick-above (&optional score)
10606   "Tick all articles with score above SCORE."
10607   (interactive "P")
10608   (gnus-summary-mark-above score gnus-ticked-mark))
10609
10610 (defun gnus-summary-mark-above (score mark)
10611   "Mark articles with score over SCORE with MARK."
10612   (interactive "P\ncMark: ")
10613   (setq score (if score
10614                   (prefix-numeric-value score)
10615                 (or gnus-summary-default-score 0)))
10616   (save-excursion
10617     (set-buffer gnus-summary-buffer)
10618     (goto-char (point-min))
10619     (while (and (progn
10620                   (when (> (gnus-summary-article-score) score)
10621                     (gnus-summary-mark-article nil mark))
10622                   t)
10623                 (gnus-summary-find-next)))))
10624
10625 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10626 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10627 (defun gnus-summary-limit-include-expunged (&optional no-error)
10628   "Display all the hidden articles that were expunged for low scores."
10629   (interactive)
10630   (let ((buffer-read-only nil))
10631     (let ((scored gnus-newsgroup-scored)
10632           headers h)
10633       (while scored
10634         (unless (gnus-summary-article-header (caar scored))
10635           (and (setq h (gnus-number-to-header (caar scored)))
10636                (< (cdar scored) gnus-summary-expunge-below)
10637                (push h headers)))
10638         (setq scored (cdr scored)))
10639       (if (not headers)
10640           (when (not no-error)
10641             (error "No expunged articles hidden"))
10642         (goto-char (point-min))
10643         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10644         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10645         (mapcar (lambda (x) (push (mail-header-number x)
10646                                   gnus-newsgroup-limit))
10647                 headers)
10648         (gnus-summary-prepare-unthreaded (nreverse headers))
10649         (goto-char (point-min))
10650         (gnus-summary-position-point)
10651         t))))
10652
10653 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10654   "Mark all unread articles in this newsgroup as read.
10655 If prefix argument ALL is non-nil, ticked and dormant articles will
10656 also be marked as read.
10657 If QUIETLY is non-nil, no questions will be asked.
10658
10659 If TO-HERE is non-nil, it should be a point in the buffer.  All
10660 articles before (after, if REVERSE is set) this point will be marked
10661 as read.
10662
10663 Note that this function will only catch up the unread article
10664 in the current summary buffer limitation.
10665
10666 The number of articles marked as read is returned."
10667   (interactive "P")
10668   (prog1
10669       (save-excursion
10670         (when (or quietly
10671                   (not gnus-interactive-catchup) ;Without confirmation?
10672                   gnus-expert-user
10673                   (gnus-y-or-n-p
10674                    (if all
10675                        "Mark absolutely all articles as read? "
10676                      "Mark all unread articles as read? ")))
10677           (if (and not-mark
10678                    (not gnus-newsgroup-adaptive)
10679                    (not gnus-newsgroup-auto-expire)
10680                    (not gnus-suppress-duplicates)
10681                    (or (not gnus-use-cache)
10682                        (eq gnus-use-cache 'passive)))
10683               (progn
10684                 (when all
10685                   (setq gnus-newsgroup-marked nil
10686                         gnus-newsgroup-spam-marked nil
10687                         gnus-newsgroup-dormant nil))
10688                 (setq gnus-newsgroup-unreads
10689                       (gnus-sorted-nunion
10690                        (gnus-intersection gnus-newsgroup-unreads
10691                                           gnus-newsgroup-downloadable)
10692                        gnus-newsgroup-unfetched)))
10693             ;; We actually mark all articles as canceled, which we
10694             ;; have to do when using auto-expiry or adaptive scoring.
10695             (gnus-summary-show-all-threads)
10696             (if (and to-here reverse)
10697                 (progn
10698                   (goto-char to-here)
10699                   (gnus-summary-mark-current-read-and-unread-as-read
10700                    gnus-catchup-mark)
10701                   (while (gnus-summary-find-next (not all))
10702                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10703               (when (gnus-summary-first-subject (not all))
10704                 (while (and
10705                         (if to-here (< (point) to-here) t)
10706                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10707                         (gnus-summary-find-next (not all))))))
10708             (gnus-set-mode-line 'summary))
10709           t))
10710     (gnus-summary-position-point)))
10711
10712 (defun gnus-summary-catchup-to-here (&optional all)
10713   "Mark all unticked articles before the current one as read.
10714 If ALL is non-nil, also mark ticked and dormant articles as read."
10715   (interactive "P")
10716   (save-excursion
10717     (gnus-save-hidden-threads
10718       (let ((beg (point)))
10719         ;; We check that there are unread articles.
10720         (when (or all (gnus-summary-find-prev))
10721           (gnus-summary-catchup all t beg)))))
10722   (gnus-summary-position-point))
10723
10724 (defun gnus-summary-catchup-from-here (&optional all)
10725   "Mark all unticked articles after (and including) the current one as read.
10726 If ALL is non-nil, also mark ticked and dormant articles as read."
10727   (interactive "P")
10728   (save-excursion
10729     (gnus-save-hidden-threads
10730       (let ((beg (point)))
10731         ;; We check that there are unread articles.
10732         (when (or all (gnus-summary-find-next))
10733           (gnus-summary-catchup all t beg nil t)))))
10734   (gnus-summary-position-point))
10735
10736 (defun gnus-summary-catchup-all (&optional quietly)
10737   "Mark all articles in this newsgroup as read.
10738 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10739 instead, which marks only unread articles as read."
10740   (interactive "P")
10741   (gnus-summary-catchup t quietly))
10742
10743 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10744   "Mark all unread articles in this group as read, then exit.
10745 If prefix argument ALL is non-nil, all articles are marked as read.
10746 If QUIETLY is non-nil, no questions will be asked."
10747   (interactive "P")
10748   (when (gnus-summary-catchup all quietly nil 'fast)
10749     ;; Select next newsgroup or exit.
10750     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10751              (eq gnus-auto-select-next 'quietly))
10752         (gnus-summary-next-group nil)
10753       (gnus-summary-exit))))
10754
10755 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10756   "Mark all articles in this newsgroup as read, and then exit.
10757 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10758 instead, which marks only unread articles as read."
10759   (interactive "P")
10760   (gnus-summary-catchup-and-exit t quietly))
10761
10762 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10763   "Mark all articles in this group as read and select the next group.
10764 If given a prefix, mark all articles, unread as well as ticked, as
10765 read."
10766   (interactive "P")
10767   (save-excursion
10768     (gnus-summary-catchup all))
10769   (gnus-summary-next-group))
10770
10771 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10772   "Mark all articles in this group as read and select the previous group.
10773 If given a prefix, mark all articles, unread as well as ticked, as
10774 read."
10775   (interactive "P")
10776   (save-excursion
10777     (gnus-summary-catchup all))
10778   (gnus-summary-next-group nil nil t))
10779
10780 ;;;
10781 ;;; with article
10782 ;;;
10783
10784 (defmacro gnus-with-article (article &rest forms)
10785   "Select ARTICLE and perform FORMS in the original article buffer.
10786 Then replace the article with the result."
10787   `(progn
10788      ;; We don't want the article to be marked as read.
10789      (let (gnus-mark-article-hook)
10790        (gnus-summary-select-article t t nil ,article))
10791      (set-buffer gnus-original-article-buffer)
10792      ,@forms
10793      (if (not (gnus-check-backend-function
10794                'request-replace-article (car gnus-article-current)))
10795          (gnus-message 5 "Read-only group; not replacing")
10796        (unless (gnus-request-replace-article
10797                 ,article (car gnus-article-current)
10798                 (current-buffer) t)
10799          (error "Couldn't replace article")))
10800      ;; The cache and backlog have to be flushed somewhat.
10801      (when gnus-keep-backlog
10802        (gnus-backlog-remove-article
10803         (car gnus-article-current) (cdr gnus-article-current)))
10804      (when gnus-use-cache
10805        (gnus-cache-update-article
10806         (car gnus-article-current) (cdr gnus-article-current)))))
10807
10808 (put 'gnus-with-article 'lisp-indent-function 1)
10809 (put 'gnus-with-article 'edebug-form-spec '(form body))
10810
10811 ;; Thread-based commands.
10812
10813 (defun gnus-summary-articles-in-thread (&optional article)
10814   "Return a list of all articles in the current thread.
10815 If ARTICLE is non-nil, return all articles in the thread that starts
10816 with that article."
10817   (let* ((article (or article (gnus-summary-article-number)))
10818          (data (gnus-data-find-list article))
10819          (top-level (gnus-data-level (car data)))
10820          (top-subject
10821           (cond ((null gnus-thread-operation-ignore-subject)
10822                  (gnus-simplify-subject-re
10823                   (mail-header-subject (gnus-data-header (car data)))))
10824                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10825                  (gnus-simplify-subject-fuzzy
10826                   (mail-header-subject (gnus-data-header (car data)))))
10827                 (t nil)))
10828          (end-point (save-excursion
10829                       (if (gnus-summary-go-to-next-thread)
10830                           (point) (point-max))))
10831          articles)
10832     (while (and data
10833                 (< (gnus-data-pos (car data)) end-point))
10834       (when (or (not top-subject)
10835                 (string= top-subject
10836                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10837                              (gnus-simplify-subject-fuzzy
10838                               (mail-header-subject
10839                                (gnus-data-header (car data))))
10840                            (gnus-simplify-subject-re
10841                             (mail-header-subject
10842                              (gnus-data-header (car data)))))))
10843         (push (gnus-data-number (car data)) articles))
10844       (unless (and (setq data (cdr data))
10845                    (> (gnus-data-level (car data)) top-level))
10846         (setq data nil)))
10847     ;; Return the list of articles.
10848     (nreverse articles)))
10849
10850 (defun gnus-summary-rethread-current ()
10851   "Rethread the thread the current article is part of."
10852   (interactive)
10853   (let* ((gnus-show-threads t)
10854          (article (gnus-summary-article-number))
10855          (id (mail-header-id (gnus-summary-article-header)))
10856          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10857     (unless id
10858       (error "No article on the current line"))
10859     (gnus-rebuild-thread id)
10860     (gnus-summary-goto-subject article)))
10861
10862 (defun gnus-summary-reparent-thread ()
10863   "Make the current article child of the marked (or previous) article.
10864
10865 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10866 is non-nil or the Subject: of both articles are the same."
10867   (interactive)
10868   (unless (not (gnus-group-read-only-p))
10869     (error "The current newsgroup does not support article editing"))
10870   (unless (<= (length gnus-newsgroup-processable) 1)
10871     (error "No more than one article may be marked"))
10872   (let ((child (gnus-summary-article-number))
10873         ;; First grab the marked article, otherwise one line up.
10874         (parent (if (not (null gnus-newsgroup-processable))
10875                     (car gnus-newsgroup-processable)
10876                   (save-excursion
10877                     (if (eq (forward-line -1) 0)
10878                         (gnus-summary-article-number)
10879                       (error "Beginning of summary buffer"))))))
10880     (gnus-summary-reparent-children parent (list child))))
10881
10882 (defun gnus-summary-reparent-children (parent children)
10883   "Make PARENT the parent of CHILDREN.
10884 When called interactively, PARENT is is current article and
10885 CHILDREN are the process-marked articles."
10886   (interactive
10887    (list (gnus-summary-article-number)
10888          (gnus-summary-work-articles nil)))
10889   (dolist (child children)
10890     (save-window-excursion
10891       (let ((gnus-article-buffer " *reparent*"))
10892         (unless (not (eq parent child))
10893           (error "An article may not be self-referential"))
10894         (let ((message-id (mail-header-id
10895                            (gnus-summary-article-header parent))))
10896           (unless (and message-id (not (equal message-id "")))
10897             (error "No message-id in desired parent"))
10898           (gnus-with-article child
10899             (save-restriction
10900               (goto-char (point-min))
10901               (message-narrow-to-head)
10902               (if (re-search-forward "^References: " nil t)
10903                   (progn
10904                     (re-search-forward "^[^ \t]" nil t)
10905                     (forward-line -1)
10906                     (end-of-line)
10907                     (insert " " message-id))
10908                 (insert "References: " message-id "\n"))))
10909           (set-buffer gnus-summary-buffer)
10910           (gnus-summary-unmark-all-processable)
10911           (gnus-summary-update-article child)
10912           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10913             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10914           (gnus-summary-rethread-current)
10915           (gnus-message 3 "Article %d is now the child of article %d"
10916                         child parent))))))
10917
10918 (defun gnus-summary-toggle-threads (&optional arg)
10919   "Toggle showing conversation threads.
10920 If ARG is positive number, turn showing conversation threads on."
10921   (interactive "P")
10922   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10923     (setq gnus-show-threads
10924           (if (null arg) (not gnus-show-threads)
10925             (> (prefix-numeric-value arg) 0)))
10926     (gnus-summary-prepare)
10927     (gnus-summary-goto-subject current)
10928     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10929     (gnus-summary-position-point)))
10930
10931 (defun gnus-summary-show-all-threads ()
10932   "Show all threads."
10933   (interactive)
10934   (save-excursion
10935     (let ((buffer-read-only nil))
10936       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10937   (gnus-summary-position-point))
10938
10939 (defun gnus-summary-show-thread ()
10940   "Show thread subtrees.
10941 Returns nil if no thread was there to be shown."
10942   (interactive)
10943   (let ((buffer-read-only nil)
10944         (orig (point))
10945         (end (point-at-eol))
10946         ;; Leave point at bol
10947         (beg (progn (beginning-of-line) (point))))
10948     (prog1
10949         ;; Any hidden lines here?
10950         (search-forward "\r" end t)
10951       (subst-char-in-region beg end ?\^M ?\n t)
10952       (goto-char orig)
10953       (gnus-summary-position-point))))
10954
10955 (defun gnus-summary-maybe-hide-threads ()
10956   "If requested, hide the threads that should be hidden."
10957   (when (and gnus-show-threads
10958              gnus-thread-hide-subtree)
10959     (gnus-summary-hide-all-threads
10960      (if (or (consp gnus-thread-hide-subtree)
10961              (functionp gnus-thread-hide-subtree))
10962          (gnus-make-predicate gnus-thread-hide-subtree)
10963        nil))))
10964
10965 ;;; Hiding predicates.
10966
10967 (defun gnus-article-unread-p (header)
10968   (memq (mail-header-number header) gnus-newsgroup-unreads))
10969
10970 (defun gnus-article-unseen-p (header)
10971   (memq (mail-header-number header) gnus-newsgroup-unseen))
10972
10973 (defun gnus-map-articles (predicate articles)
10974   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10975   (apply 'gnus-or (mapcar predicate
10976                           (mapcar (lambda (number)
10977                                     (gnus-summary-article-header number))
10978                                   articles))))
10979
10980 (defun gnus-summary-hide-all-threads (&optional predicate)
10981   "Hide all thread subtrees.
10982 If PREDICATE is supplied, threads that satisfy this predicate
10983 will not be hidden."
10984   (interactive)
10985   (save-excursion
10986     (goto-char (point-min))
10987     (let ((end nil))
10988       (while (not end)
10989         (when (or (not predicate)
10990                   (gnus-map-articles
10991                    predicate (gnus-summary-article-children)))
10992             (gnus-summary-hide-thread))
10993         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10994   (gnus-summary-position-point))
10995
10996 (defun gnus-summary-hide-thread ()
10997   "Hide thread subtrees.
10998 If PREDICATE is supplied, threads that satisfy this predicate
10999 will not be hidden.
11000 Returns nil if no threads were there to be hidden."
11001   (interactive)
11002   (let ((buffer-read-only nil)
11003         (start (point))
11004         (article (gnus-summary-article-number)))
11005     (goto-char start)
11006     ;; Go forward until either the buffer ends or the subthread
11007     ;; ends.
11008     (when (and (not (eobp))
11009                (or (zerop (gnus-summary-next-thread 1 t))
11010                    (goto-char (point-max))))
11011       (prog1
11012           (if (and (> (point) start)
11013                    (search-backward "\n" start t))
11014               (progn
11015                 (subst-char-in-region start (point) ?\n ?\^M)
11016                 (gnus-summary-goto-subject article))
11017             (goto-char start)
11018             nil)))))
11019
11020 (defun gnus-summary-go-to-next-thread (&optional previous)
11021   "Go to the same level (or less) next thread.
11022 If PREVIOUS is non-nil, go to previous thread instead.
11023 Return the article number moved to, or nil if moving was impossible."
11024   (let ((level (gnus-summary-thread-level))
11025         (way (if previous -1 1))
11026         (beg (point)))
11027     (forward-line way)
11028     (while (and (not (eobp))
11029                 (< level (gnus-summary-thread-level)))
11030       (forward-line way))
11031     (if (eobp)
11032         (progn
11033           (goto-char beg)
11034           nil)
11035       (setq beg (point))
11036       (prog1
11037           (gnus-summary-article-number)
11038         (goto-char beg)))))
11039
11040 (defun gnus-summary-next-thread (n &optional silent)
11041   "Go to the same level next N'th thread.
11042 If N is negative, search backward instead.
11043 Returns the difference between N and the number of skips actually
11044 done.
11045
11046 If SILENT, don't output messages."
11047   (interactive "p")
11048   (let ((backward (< n 0))
11049         (n (abs n)))
11050     (while (and (> n 0)
11051                 (gnus-summary-go-to-next-thread backward))
11052       (decf n))
11053     (unless silent
11054       (gnus-summary-position-point))
11055     (when (and (not silent) (/= 0 n))
11056       (gnus-message 7 "No more threads"))
11057     n))
11058
11059 (defun gnus-summary-prev-thread (n)
11060   "Go to the same level previous N'th thread.
11061 Returns the difference between N and the number of skips actually
11062 done."
11063   (interactive "p")
11064   (gnus-summary-next-thread (- n)))
11065
11066 (defun gnus-summary-go-down-thread ()
11067   "Go down one level in the current thread."
11068   (let ((children (gnus-summary-article-children)))
11069     (when children
11070       (gnus-summary-goto-subject (car children)))))
11071
11072 (defun gnus-summary-go-up-thread ()
11073   "Go up one level in the current thread."
11074   (let ((parent (gnus-summary-article-parent)))
11075     (when parent
11076       (gnus-summary-goto-subject parent))))
11077
11078 (defun gnus-summary-down-thread (n)
11079   "Go down thread N steps.
11080 If N is negative, go up instead.
11081 Returns the difference between N and how many steps down that were
11082 taken."
11083   (interactive "p")
11084   (let ((up (< n 0))
11085         (n (abs n)))
11086     (while (and (> n 0)
11087                 (if up (gnus-summary-go-up-thread)
11088                   (gnus-summary-go-down-thread)))
11089       (setq n (1- n)))
11090     (gnus-summary-position-point)
11091     (when (/= 0 n)
11092       (gnus-message 7 "Can't go further"))
11093     n))
11094
11095 (defun gnus-summary-up-thread (n)
11096   "Go up thread N steps.
11097 If N is negative, go down instead.
11098 Returns the difference between N and how many steps down that were
11099 taken."
11100   (interactive "p")
11101   (gnus-summary-down-thread (- n)))
11102
11103 (defun gnus-summary-top-thread ()
11104   "Go to the top of the thread."
11105   (interactive)
11106   (while (gnus-summary-go-up-thread))
11107   (gnus-summary-article-number))
11108
11109 (defun gnus-summary-kill-thread (&optional unmark)
11110   "Mark articles under current thread as read.
11111 If the prefix argument is positive, remove any kinds of marks.
11112 If the prefix argument is negative, tick articles instead."
11113   (interactive "P")
11114   (when unmark
11115     (setq unmark (prefix-numeric-value unmark)))
11116   (let ((articles (gnus-summary-articles-in-thread)))
11117     (save-excursion
11118       ;; Expand the thread.
11119       (gnus-summary-show-thread)
11120       ;; Mark all the articles.
11121       (while articles
11122         (gnus-summary-goto-subject (car articles))
11123         (cond ((null unmark)
11124                (gnus-summary-mark-article-as-read gnus-killed-mark))
11125               ((> unmark 0)
11126                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11127               (t
11128                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11129         (setq articles (cdr articles))))
11130     ;; Hide killed subtrees.
11131     (and (null unmark)
11132          gnus-thread-hide-killed
11133          (gnus-summary-hide-thread))
11134     ;; If marked as read, go to next unread subject.
11135     (when (null unmark)
11136       ;; Go to next unread subject.
11137       (gnus-summary-next-subject 1 t)))
11138   (gnus-set-mode-line 'summary))
11139
11140 ;; Summary sorting commands
11141
11142 (defun gnus-summary-sort-by-number (&optional reverse)
11143   "Sort the summary buffer by article number.
11144 Argument REVERSE means reverse order."
11145   (interactive "P")
11146   (gnus-summary-sort 'number reverse))
11147
11148 (defun gnus-summary-sort-by-random (&optional reverse)
11149   "Randomize the order in the summary buffer.
11150 Argument REVERSE means to randomize in reverse order."
11151   (interactive "P")
11152   (gnus-summary-sort 'random reverse))
11153
11154 (defun gnus-summary-sort-by-author (&optional reverse)
11155   "Sort the summary buffer by author name alphabetically.
11156 If `case-fold-search' is non-nil, case of letters is ignored.
11157 Argument REVERSE means reverse order."
11158   (interactive "P")
11159   (gnus-summary-sort 'author reverse))
11160
11161 (defun gnus-summary-sort-by-recipient (&optional reverse)
11162   "Sort the summary buffer by recipient name alphabetically.
11163 If `case-fold-search' is non-nil, case of letters is ignored.
11164 Argument REVERSE means reverse order."
11165   (interactive "P")
11166   (gnus-summary-sort 'recipient reverse))
11167
11168 (defun gnus-summary-sort-by-subject (&optional reverse)
11169   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11170 If `case-fold-search' is non-nil, case of letters is ignored.
11171 Argument REVERSE means reverse order."
11172   (interactive "P")
11173   (gnus-summary-sort 'subject reverse))
11174
11175 (defun gnus-summary-sort-by-date (&optional reverse)
11176   "Sort the summary buffer by date.
11177 Argument REVERSE means reverse order."
11178   (interactive "P")
11179   (gnus-summary-sort 'date reverse))
11180
11181 (defun gnus-summary-sort-by-score (&optional reverse)
11182   "Sort the summary buffer by score.
11183 Argument REVERSE means reverse order."
11184   (interactive "P")
11185   (gnus-summary-sort 'score reverse))
11186
11187 (defun gnus-summary-sort-by-lines (&optional reverse)
11188   "Sort the summary buffer by the number of lines.
11189 Argument REVERSE means reverse order."
11190   (interactive "P")
11191   (gnus-summary-sort 'lines reverse))
11192
11193 (defun gnus-summary-sort-by-chars (&optional reverse)
11194   "Sort the summary buffer by article length.
11195 Argument REVERSE means reverse order."
11196   (interactive "P")
11197   (gnus-summary-sort 'chars reverse))
11198
11199 (defun gnus-summary-sort-by-original (&optional reverse)
11200   "Sort the summary buffer using the default sorting method.
11201 Argument REVERSE means reverse order."
11202   (interactive "P")
11203   (let* ((buffer-read-only)
11204          (gnus-summary-prepare-hook nil))
11205     ;; We do the sorting by regenerating the threads.
11206     (gnus-summary-prepare)
11207     ;; Hide subthreads if needed.
11208     (gnus-summary-maybe-hide-threads)))
11209
11210 (defun gnus-summary-sort (predicate reverse)
11211   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11212   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11213          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11214          (gnus-thread-sort-functions
11215           (if (not reverse)
11216               thread
11217             `(lambda (t1 t2)
11218                (,thread t2 t1))))
11219          (gnus-sort-gathered-threads-function
11220           gnus-thread-sort-functions)
11221          (gnus-article-sort-functions
11222           (if (not reverse)
11223               article
11224             `(lambda (t1 t2)
11225                (,article t2 t1))))
11226          (buffer-read-only)
11227          (gnus-summary-prepare-hook nil))
11228     ;; We do the sorting by regenerating the threads.
11229     (gnus-summary-prepare)
11230     ;; Hide subthreads if needed.
11231     (gnus-summary-maybe-hide-threads)))
11232
11233 ;; Summary saving commands.
11234
11235 (defun gnus-summary-save-article (&optional n not-saved)
11236   "Save the current article using the default saver function.
11237 If N is a positive number, save the N next articles.
11238 If N is a negative number, save the N previous articles.
11239 If N is nil and any articles have been marked with the process mark,
11240 save those articles instead.
11241 The variable `gnus-default-article-saver' specifies the saver function.
11242
11243 If the optional second argument NOT-SAVED is non-nil, articles saved
11244 will not be marked as saved."
11245   (interactive "P")
11246   (require 'gnus-art)
11247   (let* ((articles (gnus-summary-work-articles n))
11248          (save-buffer (save-excursion
11249                         (nnheader-set-temp-buffer " *Gnus Save*")))
11250          (num (length articles))
11251          ;; Whether to save decoded articles or raw articles.
11252          (decode (when gnus-article-save-coding-system
11253                    (get gnus-default-article-saver :decode)))
11254          ;; When saving many articles, use the other function to save
11255          ;; articles other than the first one if it is specified and
11256          ;; `gnus-prompt-before-saving' is not set to `always'.
11257          (saver2 (unless (eq gnus-prompt-before-saving 'always)
11258                    (get gnus-default-article-saver :function)))
11259          (gnus-default-article-saver gnus-default-article-saver)
11260          header file)
11261     (dolist (article articles)
11262       (setq header (gnus-summary-article-header article))
11263       (if (not (vectorp header))
11264           ;; This is a pseudo-article.
11265           (if (assq 'name header)
11266               (gnus-copy-file (cdr (assq 'name header)))
11267             (gnus-message 1 "Article %d is unsaveable" article))
11268         ;; This is a real article.
11269         (save-window-excursion
11270           (let ((gnus-display-mime-function (when decode
11271                                               gnus-display-mime-function))
11272                 (gnus-article-prepare-hook (when decode
11273                                              gnus-article-prepare-hook)))
11274             (gnus-summary-select-article (not decode) nil nil article)
11275             (gnus-summary-goto-subject article)))
11276         (save-excursion
11277           (set-buffer save-buffer)
11278           (erase-buffer)
11279           (insert-buffer-substring (if decode
11280                                        gnus-article-buffer
11281                                      gnus-original-article-buffer)))
11282         (setq file (gnus-article-save save-buffer file num))
11283         (gnus-summary-remove-process-mark article)
11284         (unless not-saved
11285           (gnus-summary-set-saved-mark article)))
11286       (when saver2
11287         (setq gnus-default-article-saver saver2
11288               saver2 nil)))
11289     (gnus-kill-buffer save-buffer)
11290     (gnus-summary-position-point)
11291     (gnus-set-mode-line 'summary)
11292     n))
11293
11294 (defun gnus-summary-pipe-output (&optional arg headers)
11295   "Pipe the current article to a subprocess.
11296 If N is a positive number, pipe the N next articles.
11297 If N is a negative number, pipe the N previous articles.
11298 If N is nil and any articles have been marked with the process mark,
11299 pipe those articles instead.
11300 If HEADERS (the symbolic prefix), include the headers, too."
11301   (interactive (gnus-interactive "P\ny"))
11302   (require 'gnus-art)
11303   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11304         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11305     (gnus-summary-save-article arg t))
11306   (let ((buffer (get-buffer "*Shell Command Output*")))
11307     (when (and buffer
11308                (not (zerop (buffer-size buffer))))
11309       (gnus-configure-windows 'pipe))))
11310
11311 (defun gnus-summary-save-article-mail (&optional arg)
11312   "Append the current article to a Unix mail box file.
11313 If N is a positive number, save the N next articles.
11314 If N is a negative number, save the N previous articles.
11315 If N is nil and any articles have been marked with the process mark,
11316 save those articles instead."
11317   (interactive "P")
11318   (require 'gnus-art)
11319   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11320     (gnus-summary-save-article arg)))
11321
11322 (defun gnus-summary-save-article-rmail (&optional arg)
11323   "Append the current article to an rmail file.
11324 If N is a positive number, save the N next articles.
11325 If N is a negative number, save the N previous articles.
11326 If N is nil and any articles have been marked with the process mark,
11327 save those articles instead."
11328   (interactive "P")
11329   (require 'gnus-art)
11330   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11331     (gnus-summary-save-article arg)))
11332
11333 (defun gnus-summary-save-article-file (&optional arg)
11334   "Append the current article to a file.
11335 If N is a positive number, save the N next articles.
11336 If N is a negative number, save the N previous articles.
11337 If N is nil and any articles have been marked with the process mark,
11338 save those articles instead."
11339   (interactive "P")
11340   (require 'gnus-art)
11341   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11342     (gnus-summary-save-article arg)))
11343
11344 (defun gnus-summary-write-article-file (&optional arg)
11345   "Write the current article to a file, deleting the previous file.
11346 If N is a positive number, save the N next articles.
11347 If N is a negative number, save the N previous articles.
11348 If N is nil and any articles have been marked with the process mark,
11349 save those articles instead."
11350   (interactive "P")
11351   (require 'gnus-art)
11352   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11353     (gnus-summary-save-article arg)))
11354
11355 (defun gnus-summary-save-article-body-file (&optional arg)
11356   "Append the current article body to a file.
11357 If N is a positive number, save the N next articles.
11358 If N is a negative number, save the N previous articles.
11359 If N is nil and any articles have been marked with the process mark,
11360 save those articles instead."
11361   (interactive "P")
11362   (require 'gnus-art)
11363   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11364     (gnus-summary-save-article arg)))
11365
11366 (defun gnus-summary-write-article-body-file (&optional arg)
11367   "Write the current article body to a file, deleting the previous file.
11368 If N is a positive number, save the N next articles.
11369 If N is a negative number, save the N previous articles.
11370 If N is nil and any articles have been marked with the process mark,
11371 save those articles instead."
11372   (interactive "P")
11373   (require 'gnus-art)
11374   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11375     (gnus-summary-save-article arg)))
11376
11377 (defun gnus-summary-muttprint (&optional arg)
11378   "Print the current article using Muttprint.
11379 If N is a positive number, save the N next articles.
11380 If N is a negative number, save the N previous articles.
11381 If N is nil and any articles have been marked with the process mark,
11382 save those articles instead."
11383   (interactive "P")
11384   (require 'gnus-art)
11385   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11386     (gnus-summary-save-article arg t)))
11387
11388 (defun gnus-summary-pipe-message (program)
11389   "Pipe the current article through PROGRAM."
11390   (interactive "sProgram: ")
11391   (gnus-summary-select-article)
11392   (let ((mail-header-separator ""))
11393     (gnus-eval-in-buffer-window gnus-article-buffer
11394       (save-restriction
11395         (widen)
11396         (let ((start (window-start))
11397               buffer-read-only)
11398           (message-pipe-buffer-body program)
11399           (set-window-start (get-buffer-window (current-buffer)) start))))))
11400
11401 (defun gnus-get-split-value (methods)
11402   "Return a value based on the split METHODS."
11403   (let (split-name method result match)
11404     (when methods
11405       (save-excursion
11406         (set-buffer gnus-original-article-buffer)
11407         (save-restriction
11408           (nnheader-narrow-to-headers)
11409           (while (and methods (not split-name))
11410             (goto-char (point-min))
11411             (setq method (pop methods))
11412             (setq match (car method))
11413             (when (cond
11414                    ((stringp match)
11415                     ;; Regular expression.
11416                     (ignore-errors
11417                       (re-search-forward match nil t)))
11418                    ((functionp match)
11419                     ;; Function.
11420                     (save-restriction
11421                       (widen)
11422                       (setq result (funcall match gnus-newsgroup-name))))
11423                    ((consp match)
11424                     ;; Form.
11425                     (save-restriction
11426                       (widen)
11427                       (setq result (eval match)))))
11428               (setq split-name (cdr method))
11429               (cond ((stringp result)
11430                      (push (expand-file-name
11431                             result gnus-article-save-directory)
11432                            split-name))
11433                     ((consp result)
11434                      (setq split-name (append result split-name)))))))))
11435     (nreverse split-name)))
11436
11437 (defun gnus-valid-move-group-p (group)
11438   (and (boundp group)
11439        (symbol-name group)
11440        (symbol-value group)
11441        (gnus-get-function (gnus-find-method-for-group
11442                            (symbol-name group)) 'request-accept-article t)))
11443
11444 (defun gnus-read-move-group-name (prompt default articles prefix)
11445   "Read a group name."
11446   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11447          (minibuffer-confirm-incomplete nil) ; XEmacs
11448          (prom
11449           (format "%s %s to"
11450                   prompt
11451                   (if (> (length articles) 1)
11452                       (format "these %d articles" (length articles))
11453                     "this article")))
11454          (to-newsgroup
11455           (cond
11456            ((null split-name)
11457             (gnus-completing-read-with-default
11458              default prom
11459              gnus-active-hashtb
11460              'gnus-valid-move-group-p
11461              nil prefix
11462              'gnus-group-history))
11463            ((= 1 (length split-name))
11464             (gnus-completing-read-with-default
11465              (car split-name) prom
11466              gnus-active-hashtb
11467              'gnus-valid-move-group-p
11468              nil nil
11469              'gnus-group-history))
11470            (t
11471             (gnus-completing-read-with-default
11472              nil prom
11473              (mapcar 'list (nreverse split-name))
11474              nil nil nil
11475              'gnus-group-history))))
11476          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11477     (when to-newsgroup
11478       (if (or (string= to-newsgroup "")
11479               (string= to-newsgroup prefix))
11480           (setq to-newsgroup default))
11481       (unless to-newsgroup
11482         (error "No group name entered"))
11483       (or (gnus-active to-newsgroup)
11484           (gnus-activate-group to-newsgroup nil nil to-method)
11485           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11486                                      to-newsgroup))
11487               (or (and (gnus-request-create-group to-newsgroup to-method)
11488                        (gnus-activate-group
11489                         to-newsgroup nil nil to-method)
11490                        (gnus-subscribe-group to-newsgroup))
11491                   (error "Couldn't create group %s" to-newsgroup)))
11492           (error "No such group: %s" to-newsgroup)))
11493     to-newsgroup))
11494
11495 (defvar gnus-summary-save-parts-counter)
11496
11497 (defun gnus-summary-save-parts (type dir n &optional reverse)
11498   "Save parts matching TYPE to DIR.
11499 If REVERSE, save parts that do not match TYPE."
11500   (interactive
11501    (list (read-string "Save parts of type: "
11502                       (or (car gnus-summary-save-parts-type-history)
11503                           gnus-summary-save-parts-default-mime)
11504                       'gnus-summary-save-parts-type-history)
11505          (setq gnus-summary-save-parts-last-directory
11506                (read-file-name "Save to directory: "
11507                                gnus-summary-save-parts-last-directory
11508                                nil t))
11509          current-prefix-arg))
11510   (gnus-summary-iterate n
11511     (let ((gnus-display-mime-function nil)
11512           (gnus-inhibit-treatment t))
11513       (gnus-summary-select-article))
11514     (save-excursion
11515       (set-buffer gnus-article-buffer)
11516       (let ((handles (or gnus-article-mime-handles
11517                          (mm-dissect-buffer nil gnus-article-loose-mime)
11518                          (and gnus-article-emulate-mime
11519                               (mm-uu-dissect))))
11520             (gnus-summary-save-parts-counter 1))
11521         (when handles
11522           (gnus-summary-save-parts-1 type dir handles reverse)
11523           (unless gnus-article-mime-handles ;; Don't destroy this case.
11524             (mm-destroy-parts handles)))))))
11525
11526 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11527   (if (stringp (car handle))
11528       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11529               (cdr handle))
11530     (when (if reverse
11531               (not (string-match type (mm-handle-media-type handle)))
11532             (string-match type (mm-handle-media-type handle)))
11533       (let ((file (expand-file-name
11534                    (gnus-map-function
11535                     mm-file-name-rewrite-functions
11536                     (file-name-nondirectory
11537                      (or
11538                       (mail-content-type-get
11539                        (mm-handle-disposition handle) 'filename)
11540                       (mail-content-type-get
11541                        (mm-handle-type handle) 'name)
11542                       (format "%s.%d.%d" gnus-newsgroup-name
11543                               (cdr gnus-article-current)
11544                               gnus-summary-save-parts-counter))))
11545                    dir)))
11546         (incf gnus-summary-save-parts-counter)
11547         (unless (file-exists-p file)
11548           (mm-save-part-to-file handle file))))))
11549
11550 ;; Summary extract commands
11551
11552 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11553   (let ((buffer-read-only nil)
11554         (article (gnus-summary-article-number))
11555         after-article b e)
11556     (unless (gnus-summary-goto-subject article)
11557       (error "No such article: %d" article))
11558     (gnus-summary-position-point)
11559     ;; If all commands are to be bunched up on one line, we collect
11560     ;; them here.
11561     (unless gnus-view-pseudos-separately
11562       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11563             files action)
11564         (while ps
11565           (setq action (cdr (assq 'action (car ps))))
11566           (setq files (list (cdr (assq 'name (car ps)))))
11567           (while (and ps (cdr ps)
11568                       (string= (or action "1")
11569                                (or (cdr (assq 'action (cadr ps))) "2")))
11570             (push (cdr (assq 'name (cadr ps))) files)
11571             (setcdr ps (cddr ps)))
11572           (when files
11573             (when (not (string-match "%s" action))
11574               (push " " files))
11575             (push " " files)
11576             (when (assq 'execute (car ps))
11577               (setcdr (assq 'execute (car ps))
11578                       (funcall (if (string-match "%s" action)
11579                                    'format 'concat)
11580                                action
11581                                (mapconcat
11582                                 (lambda (f)
11583                                   (if (equal f " ")
11584                                       f
11585                                     (shell-quote-argument f)))
11586                                 files " ")))))
11587           (setq ps (cdr ps)))))
11588     (if (and gnus-view-pseudos (not not-view))
11589         (while pslist
11590           (when (assq 'execute (car pslist))
11591             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11592                                   (eq gnus-view-pseudos 'not-confirm)))
11593           (setq pslist (cdr pslist)))
11594       (save-excursion
11595         (while pslist
11596           (setq after-article (or (cdr (assq 'article (car pslist)))
11597                                   (gnus-summary-article-number)))
11598           (gnus-summary-goto-subject after-article)
11599           (forward-line 1)
11600           (setq b (point))
11601           (insert "    " (file-name-nondirectory
11602                           (cdr (assq 'name (car pslist))))
11603                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11604           (setq e (point))
11605           (forward-line -1)             ; back to `b'
11606           (gnus-add-text-properties
11607            b (1- e) (list 'gnus-number gnus-reffed-article-number
11608                           gnus-mouse-face-prop gnus-mouse-face))
11609           (gnus-data-enter
11610            after-article gnus-reffed-article-number
11611            gnus-unread-mark b (car pslist) 0 (- e b))
11612           (setq gnus-newsgroup-unreads
11613                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11614                                          gnus-reffed-article-number))
11615           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11616           (setq pslist (cdr pslist)))))))
11617
11618 (defun gnus-pseudos< (p1 p2)
11619   (let ((c1 (cdr (assq 'action p1)))
11620         (c2 (cdr (assq 'action p2))))
11621     (and c1 c2 (string< c1 c2))))
11622
11623 (defun gnus-request-pseudo-article (props)
11624   (cond ((assq 'execute props)
11625          (gnus-execute-command (cdr (assq 'execute props)))))
11626   (let ((gnus-current-article (gnus-summary-article-number)))
11627     (gnus-run-hooks 'gnus-mark-article-hook)))
11628
11629 (defun gnus-execute-command (command &optional automatic)
11630   (save-excursion
11631     (gnus-article-setup-buffer)
11632     (set-buffer gnus-article-buffer)
11633     (setq buffer-read-only nil)
11634     (let ((command (if automatic command
11635                      (read-string "Command: " (cons command 0)))))
11636       (erase-buffer)
11637       (insert "$ " command "\n\n")
11638       (if gnus-view-pseudo-asynchronously
11639           (start-process "gnus-execute" (current-buffer) shell-file-name
11640                          shell-command-switch command)
11641         (call-process shell-file-name nil t nil
11642                       shell-command-switch command)))))
11643
11644 ;; Summary kill commands.
11645
11646 (defun gnus-summary-edit-global-kill (article)
11647   "Edit the \"global\" kill file."
11648   (interactive (list (gnus-summary-article-number)))
11649   (gnus-group-edit-global-kill article))
11650
11651 (defun gnus-summary-edit-local-kill ()
11652   "Edit a local kill file applied to the current newsgroup."
11653   (interactive)
11654   (setq gnus-current-headers (gnus-summary-article-header))
11655   (gnus-group-edit-local-kill
11656    (gnus-summary-article-number) gnus-newsgroup-name))
11657
11658 ;;; Header reading.
11659
11660 (defun gnus-read-header (id &optional header)
11661   "Read the headers of article ID and enter them into the Gnus system."
11662   (let ((group gnus-newsgroup-name)
11663         (gnus-override-method
11664          (or
11665           gnus-override-method
11666           (and (gnus-news-group-p gnus-newsgroup-name)
11667                (car (gnus-refer-article-methods)))))
11668         where)
11669     ;; First we check to see whether the header in question is already
11670     ;; fetched.
11671     (if (stringp id)
11672         ;; This is a Message-ID.
11673         (setq header (or header (gnus-id-to-header id)))
11674       ;; This is an article number.
11675       (setq header (or header (gnus-summary-article-header id))))
11676     (if (and header
11677              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11678         ;; We have found the header.
11679         header
11680       ;; We have to really fetch the header to this article.
11681       (save-excursion
11682         (set-buffer nntp-server-buffer)
11683         (when (setq where (gnus-request-head id group))
11684           (nnheader-fold-continuation-lines)
11685           (goto-char (point-max))
11686           (insert ".\n")
11687           (goto-char (point-min))
11688           (insert "211 ")
11689           (princ (cond
11690                   ((numberp id) id)
11691                   ((cdr where) (cdr where))
11692                   (header (mail-header-number header))
11693                   (t gnus-reffed-article-number))
11694                  (current-buffer))
11695           (insert " Article retrieved.\n"))
11696         (if (or (not where)
11697                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11698             ()                          ; Malformed head.
11699           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11700             (when (and (stringp id)
11701                        (or
11702                         (not (string= (gnus-group-real-name group)
11703                                       (car where)))
11704                         (not (gnus-server-equal gnus-override-method
11705                                                 (gnus-group-method group)))))
11706               ;; If we fetched by Message-ID and the article came from
11707               ;; a different group (or server), we fudge some bogus
11708               ;; article numbers for this article.
11709               (mail-header-set-number header gnus-reffed-article-number))
11710             (save-excursion
11711               (set-buffer gnus-summary-buffer)
11712               (decf gnus-reffed-article-number)
11713               (gnus-remove-header (mail-header-number header))
11714               (push header gnus-newsgroup-headers)
11715               (setq gnus-current-headers header)
11716               (push (mail-header-number header) gnus-newsgroup-limit)))
11717           header)))))
11718
11719 (defun gnus-remove-header (number)
11720   "Remove header NUMBER from `gnus-newsgroup-headers'."
11721   (if (and gnus-newsgroup-headers
11722            (= number (mail-header-number (car gnus-newsgroup-headers))))
11723       (pop gnus-newsgroup-headers)
11724     (let ((headers gnus-newsgroup-headers))
11725       (while (and (cdr headers)
11726                   (not (= number (mail-header-number (cadr headers)))))
11727         (pop headers))
11728       (when (cdr headers)
11729         (setcdr headers (cddr headers))))))
11730
11731 ;;;
11732 ;;; summary highlights
11733 ;;;
11734
11735 (defun gnus-highlight-selected-summary ()
11736   "Highlight selected article in summary buffer."
11737   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11738   (when gnus-summary-selected-face
11739     (save-excursion
11740       (let* ((beg (point-at-bol))
11741              (end (point-at-eol))
11742              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11743              (from (if (get-text-property beg gnus-mouse-face-prop)
11744                        beg
11745                      (or (next-single-property-change
11746                           beg gnus-mouse-face-prop nil end)
11747                          beg)))
11748              (to
11749               (if (= from end)
11750                   (- from 2)
11751                 (or (next-single-property-change
11752                      from gnus-mouse-face-prop nil end)
11753                     end))))
11754         ;; If no mouse-face prop on line we will have to = from = end,
11755         ;; so we highlight the entire line instead.
11756         (when (= (+ to 2) from)
11757           (setq from beg)
11758           (setq to end))
11759         (if gnus-newsgroup-selected-overlay
11760             ;; Move old overlay.
11761             (gnus-move-overlay
11762              gnus-newsgroup-selected-overlay from to (current-buffer))
11763           ;; Create new overlay.
11764           (gnus-overlay-put
11765            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11766            'face gnus-summary-selected-face))))))
11767
11768 (defvar gnus-summary-highlight-line-cached nil)
11769 (defvar gnus-summary-highlight-line-trigger nil)
11770
11771 (defun gnus-summary-highlight-line-0 ()
11772   (if (and (eq gnus-summary-highlight-line-trigger
11773                gnus-summary-highlight)
11774            gnus-summary-highlight-line-cached)
11775       gnus-summary-highlight-line-cached
11776     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11777           gnus-summary-highlight-line-cached
11778           (let* ((cond (list 'cond))
11779                  (c cond)
11780                  (list gnus-summary-highlight))
11781             (while list
11782               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11783                               nil))
11784               (setq c (cdr c)
11785                     list (cdr list)))
11786             (gnus-byte-compile (list 'lambda nil cond))))))
11787
11788 (defun gnus-summary-highlight-line ()
11789   "Highlight current line according to `gnus-summary-highlight'."
11790   (let* ((beg (point-at-bol))
11791          (article (or (gnus-summary-article-number) gnus-current-article))
11792          (score (or (cdr (assq article
11793                                gnus-newsgroup-scored))
11794                     gnus-summary-default-score 0))
11795          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11796          (inhibit-read-only t)
11797          (default gnus-summary-default-score)
11798          (default-high gnus-summary-default-high-score)
11799          (default-low gnus-summary-default-low-score)
11800          (uncached (and gnus-summary-use-undownloaded-faces
11801                         (memq article gnus-newsgroup-undownloaded)
11802                         (not (memq article gnus-newsgroup-cached)))))
11803     (let ((face (funcall (gnus-summary-highlight-line-0))))
11804       (unless (eq face (get-text-property beg 'face))
11805         (gnus-put-text-property-excluding-characters-with-faces
11806          beg (point-at-eol) 'face
11807          (setq face (if (boundp face) (symbol-value face) face)))
11808         (when gnus-summary-highlight-line-function
11809           (funcall gnus-summary-highlight-line-function article face))))))
11810
11811 (defun gnus-update-read-articles (group unread &optional compute)
11812   "Update the list of read articles in GROUP.
11813 UNREAD is a sorted list."
11814   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11815         (info (gnus-get-info group))
11816         (prev 1)
11817         read)
11818     (if (or (not info) (not active))
11819         ;; There is no info on this group if it was, in fact,
11820         ;; killed.  Gnus stores no information on killed groups, so
11821         ;; there's nothing to be done.
11822         ;; One could store the information somewhere temporarily,
11823         ;; perhaps...  Hmmm...
11824         ()
11825       ;; Remove any negative articles numbers.
11826       (while (and unread (< (car unread) 0))
11827         (setq unread (cdr unread)))
11828       ;; Remove any expired article numbers
11829       (while (and unread (< (car unread) (car active)))
11830         (setq unread (cdr unread)))
11831       ;; Compute the ranges of read articles by looking at the list of
11832       ;; unread articles.
11833       (while unread
11834         (when (/= (car unread) prev)
11835           (push (if (= prev (1- (car unread))) prev
11836                   (cons prev (1- (car unread))))
11837                 read))
11838         (setq prev (1+ (car unread)))
11839         (setq unread (cdr unread)))
11840       (when (<= prev (cdr active))
11841         (push (cons prev (cdr active)) read))
11842       (setq read (if (> (length read) 1) (nreverse read) read))
11843       (if compute
11844           read
11845         (save-excursion
11846           (let (setmarkundo)
11847             ;; Propagate the read marks to the backend.
11848             (when (gnus-check-backend-function 'request-set-mark group)
11849               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11850                     (add (gnus-remove-from-range read (gnus-info-read info))))
11851                 (when (or add del)
11852                   (unless (gnus-check-group group)
11853                     (error "Can't open server for %s" group))
11854                   (gnus-request-set-mark
11855                    group (delq nil (list (if add (list add 'add '(read)))
11856                                          (if del (list del 'del '(read))))))
11857                   (setq setmarkundo
11858                         `(gnus-request-set-mark
11859                           ,group
11860                           ',(delq nil (list
11861                                        (if del (list del 'add '(read)))
11862                                        (if add (list add 'del '(read))))))))))
11863             (set-buffer gnus-group-buffer)
11864             (gnus-undo-register
11865               `(progn
11866                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11867                  (gnus-info-set-read ',info ',(gnus-info-read info))
11868                  (gnus-get-unread-articles-in-group ',info
11869                                                     (gnus-active ,group))
11870                  (gnus-group-update-group ,group t)
11871                  ,setmarkundo))))
11872         ;; Enter this list into the group info.
11873         (gnus-info-set-read info read)
11874         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11875         (gnus-get-unread-articles-in-group info (gnus-active group))
11876         t))))
11877
11878 (defun gnus-offer-save-summaries ()
11879   "Offer to save all active summary buffers."
11880   (let (buffers)
11881     ;; Go through all buffers and find all summaries.
11882     (dolist (buffer (buffer-list))
11883       (when (and (setq buffer (buffer-name buffer))
11884                  (string-match "Summary" buffer)
11885                  (with-current-buffer buffer
11886                    ;; We check that this is, indeed, a summary buffer.
11887                    (and (eq major-mode 'gnus-summary-mode)
11888                         ;; Also make sure this isn't bogus.
11889                         gnus-newsgroup-prepared
11890                         ;; Also make sure that this isn't a
11891                         ;; dead summary buffer.
11892                         (not gnus-dead-summary-mode))))
11893         (push buffer buffers)))
11894     ;; Go through all these summary buffers and offer to save them.
11895     (when buffers
11896       (save-excursion
11897         (map-y-or-n-p
11898          "Update summary buffer %s? "
11899          (lambda (buf)
11900            (switch-to-buffer buf)
11901            (gnus-summary-exit))
11902          buffers)))))
11903
11904 (defun gnus-summary-setup-default-charset ()
11905   "Setup newsgroup default charset."
11906   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11907       (setq gnus-newsgroup-charset nil)
11908     (let* ((ignored-charsets
11909             (or gnus-newsgroup-ephemeral-ignored-charsets
11910                 (append
11911                  (and gnus-newsgroup-name
11912                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11913                  gnus-newsgroup-ignored-charsets))))
11914       (setq gnus-newsgroup-charset
11915             (or gnus-newsgroup-ephemeral-charset
11916                 (and gnus-newsgroup-name
11917                      (gnus-parameter-charset gnus-newsgroup-name))
11918                 gnus-default-charset))
11919       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11920            ignored-charsets))))
11921
11922 ;;;
11923 ;;; Mime Commands
11924 ;;;
11925
11926 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11927   "Display the current article buffer fully MIME-buttonized.
11928 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11929 treated as multipart/mixed."
11930   (interactive "P")
11931   (require 'gnus-art)
11932   (let ((gnus-unbuttonized-mime-types nil)
11933         (gnus-mime-display-multipart-as-mixed show-all-parts))
11934     (gnus-summary-show-article)))
11935
11936 (defun gnus-summary-repair-multipart (article)
11937   "Add a Content-Type header to a multipart article without one."
11938   (interactive (list (gnus-summary-article-number)))
11939   (gnus-with-article article
11940     (message-narrow-to-head)
11941     (message-remove-header "Mime-Version")
11942     (goto-char (point-max))
11943     (insert "Mime-Version: 1.0\n")
11944     (widen)
11945     (when (search-forward "\n--" nil t)
11946       (let ((separator (buffer-substring (point) (point-at-eol))))
11947         (message-narrow-to-head)
11948         (message-remove-header "Content-Type")
11949         (goto-char (point-max))
11950         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11951                         separator))
11952         (widen))))
11953   (let (gnus-mark-article-hook)
11954     (gnus-summary-select-article t t nil article)))
11955
11956 (defun gnus-summary-toggle-display-buttonized ()
11957   "Toggle the buttonizing of the article buffer."
11958   (interactive)
11959   (require 'gnus-art)
11960   (if (setq gnus-inhibit-mime-unbuttonizing
11961             (not gnus-inhibit-mime-unbuttonizing))
11962       (let ((gnus-unbuttonized-mime-types nil))
11963         (gnus-summary-show-article))
11964     (gnus-summary-show-article)))
11965
11966 ;;;
11967 ;;; Generic summary marking commands
11968 ;;;
11969
11970 (defvar gnus-summary-marking-alist
11971   '((read gnus-del-mark "d")
11972     (unread gnus-unread-mark "u")
11973     (ticked gnus-ticked-mark "!")
11974     (dormant gnus-dormant-mark "?")
11975     (expirable gnus-expirable-mark "e"))
11976   "An alist of names/marks/keystrokes.")
11977
11978 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11979 (defvar gnus-summary-mark-map)
11980
11981 (defun gnus-summary-make-all-marking-commands ()
11982   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11983   (dolist (elem gnus-summary-marking-alist)
11984     (apply 'gnus-summary-make-marking-command elem)))
11985
11986 (defun gnus-summary-make-marking-command (name mark keystroke)
11987   (let ((map (make-sparse-keymap)))
11988     (define-key gnus-summary-generic-mark-map keystroke map)
11989     (dolist (lway `((next "next" next nil "n")
11990                     (next-unread "next unread" next t "N")
11991                     (prev "previous" prev nil "p")
11992                     (prev-unread "previous unread" prev t "P")
11993                     (nomove "" nil nil ,keystroke)))
11994       (let ((func (gnus-summary-make-marking-command-1
11995                    mark (car lway) lway name)))
11996         (setq func (eval func))
11997         (define-key map (nth 4 lway) func)))))
11998
11999 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12000   `(defun ,(intern
12001             (format "gnus-summary-put-mark-as-%s%s"
12002                     name (if (eq way 'nomove)
12003                              ""
12004                            (concat "-" (symbol-name way)))))
12005      (n)
12006      ,(format
12007        "Mark the current article as %s%s.
12008 If N, the prefix, then repeat N times.
12009 If N is negative, move in reverse order.
12010 The difference between N and the actual number of articles marked is
12011 returned."
12012        name (cadr lway))
12013      (interactive "p")
12014      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12015
12016 (defun gnus-summary-generic-mark (n mark move unread)
12017   "Mark N articles with MARK."
12018   (unless (eq major-mode 'gnus-summary-mode)
12019     (error "This command can only be used in the summary buffer"))
12020   (gnus-summary-show-thread)
12021   (let ((nummove
12022          (cond
12023           ((eq move 'next) 1)
12024           ((eq move 'prev) -1)
12025           (t 0))))
12026     (if (zerop nummove)
12027         (setq n 1)
12028       (when (< n 0)
12029         (setq n (abs n)
12030               nummove (* -1 nummove))))
12031     (while (and (> n 0)
12032                 (gnus-summary-mark-article nil mark)
12033                 (zerop (gnus-summary-next-subject nummove unread t)))
12034       (setq n (1- n)))
12035     (when (/= 0 n)
12036       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12037     (gnus-summary-recenter)
12038     (gnus-summary-position-point)
12039     (gnus-set-mode-line 'summary)
12040     n))
12041
12042 (defun gnus-summary-insert-articles (articles)
12043   (when (setq articles
12044               (gnus-sorted-difference articles
12045                                       (mapcar (lambda (h)
12046                                                 (mail-header-number h))
12047                                               gnus-newsgroup-headers)))
12048     (setq gnus-newsgroup-headers
12049           (gnus-merge 'list
12050                       gnus-newsgroup-headers
12051                       (gnus-fetch-headers articles)
12052                       'gnus-article-sort-by-number))
12053     ;; Suppress duplicates?
12054     (when gnus-suppress-duplicates
12055       (gnus-dup-suppress-articles))
12056
12057     ;; We might want to build some more threads first.
12058     (when (and gnus-fetch-old-headers
12059                (eq gnus-headers-retrieved-by 'nov))
12060       (if (eq gnus-fetch-old-headers 'invisible)
12061           (gnus-build-all-threads)
12062         (gnus-build-old-threads)))
12063     ;; Let the Gnus agent mark articles as read.
12064     (when gnus-agent
12065       (gnus-agent-get-undownloaded-list))
12066     ;; Remove list identifiers from subject
12067     (when gnus-list-identifiers
12068       (gnus-summary-remove-list-identifiers))
12069     ;; First and last article in this newsgroup.
12070     (when gnus-newsgroup-headers
12071       (setq gnus-newsgroup-begin
12072             (mail-header-number (car gnus-newsgroup-headers))
12073             gnus-newsgroup-end
12074             (mail-header-number
12075              (gnus-last-element gnus-newsgroup-headers))))
12076     (when gnus-use-scoring
12077       (gnus-possibly-score-headers))))
12078
12079 (defun gnus-summary-insert-old-articles (&optional all)
12080   "Insert all old articles in this group.
12081 If ALL is non-nil, already read articles become readable.
12082 If ALL is a number, fetch this number of articles."
12083   (interactive "P")
12084   (prog1
12085       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12086             older len)
12087         (setq older
12088               ;; Some nntp servers lie about their active range.  When
12089               ;; this happens, the active range can be in the millions.
12090               ;; Use a compressed range to avoid creating a huge list.
12091               (gnus-range-difference (list gnus-newsgroup-active) old))
12092         (setq len (gnus-range-length older))
12093         (cond
12094          ((null older) nil)
12095          ((numberp all)
12096           (if (< all len)
12097               (let ((older-range (nreverse older)))
12098                 (setq older nil)
12099
12100                 (while (> all 0)
12101                   (let* ((r (pop older-range))
12102                          (min (if (numberp r) r (car r)))
12103                          (max (if (numberp r) r (cdr r))))
12104                     (while (and (<= min max)
12105                                 (> all 0))
12106                       (push max older)
12107                       (setq all (1- all)
12108                             max (1- max))))))
12109             (setq older (gnus-uncompress-range older))))
12110          (all
12111           (setq older (gnus-uncompress-range older)))
12112          (t
12113           (when (and (numberp gnus-large-newsgroup)
12114                    (> len gnus-large-newsgroup))
12115               (let* ((cursor-in-echo-area nil)
12116                      (initial (gnus-parameter-large-newsgroup-initial
12117                                gnus-newsgroup-name))
12118                      (input
12119                       (read-string
12120                        (format
12121                         "How many articles from %s (%s %d): "
12122                         (gnus-limit-string
12123                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12124                         (if initial "max" "default")
12125                         len)
12126                        (if initial
12127                            (cons (number-to-string initial)
12128                                  0)))))
12129                 (unless (string-match "^[ \t]*$" input)
12130                   (setq all (string-to-number input))
12131                   (if (< all len)
12132                       (let ((older-range (nreverse older)))
12133                         (setq older nil)
12134
12135                         (while (> all 0)
12136                           (let* ((r (pop older-range))
12137                                  (min (if (numberp r) r (car r)))
12138                                  (max (if (numberp r) r (cdr r))))
12139                             (while (and (<= min max)
12140                                         (> all 0))
12141                               (push max older)
12142                               (setq all (1- all)
12143                                     max (1- max))))))))))
12144           (setq older (gnus-uncompress-range older))))
12145         (if (not older)
12146             (message "No old news.")
12147           (gnus-summary-insert-articles older)
12148           (gnus-summary-limit (gnus-sorted-nunion old older))))
12149     (gnus-summary-position-point)))
12150
12151 (defun gnus-summary-insert-new-articles ()
12152   "Insert all new articles in this group."
12153   (interactive)
12154   (prog1
12155       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12156             (old-active gnus-newsgroup-active)
12157             (nnmail-fetched-sources (list t))
12158             i new)
12159         (setq gnus-newsgroup-active
12160               (gnus-activate-group gnus-newsgroup-name 'scan))
12161         (setq i (cdr gnus-newsgroup-active))
12162         (while (> i (cdr old-active))
12163           (push i new)
12164           (decf i))
12165         (if (not new)
12166             (message "No gnus is bad news")
12167           (gnus-summary-insert-articles new)
12168           (setq gnus-newsgroup-unreads
12169                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12170           (gnus-summary-limit (gnus-sorted-nunion old new))))
12171     (gnus-summary-position-point)))
12172
12173 (gnus-summary-make-all-marking-commands)
12174
12175 (gnus-ems-redefine)
12176
12177 (provide 'gnus-sum)
12178
12179 (run-hooks 'gnus-sum-load-hook)
12180
12181 ;; Local Variables:
12182 ;; coding: iso-8859-1
12183 ;; End:
12184
12185 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12186 ;;; gnus-sum.el ends here