(gnus-summary-recenter): Remove debug messages.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-mode))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-fetch-old-headers nil
66   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
67 If an unread article in the group refers to an older, already
68 read (or just marked as read) article, the old article will not
69 normally be displayed in the Summary buffer.  If this variable is
70 t, Gnus will attempt to grab the headers to the old articles, and
71 thereby build complete threads.  If it has the value `some', all
72 old headers will be fetched but only enough headers to connect
73 otherwise loose threads will be displayed.  This variable can
74 also be a number.  In that case, no more than that number of old
75 headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work.
79
80 This feature can seriously impact performance it ignores all
81 locally cached header entries."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const :tag "on" t)
85                  (const some)
86                  (const invisible)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 500
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "*If non-nil, select the article under point.
312 Which article this is is controlled by the `gnus-auto-select-subject'
313 variable.
314
315 If you want to prevent automatic selection of articles in some
316 newsgroups, set the variable to nil in `gnus-select-group-hook'."
317   :group 'gnus-group-select
318   :type '(choice (const :tag "none" nil)
319                  (sexp :menu-tag "first" t)))
320
321 (defcustom gnus-auto-select-subject 'unread
322   "*Says what subject to place under point when entering a group.
323
324 This variable can either be the symbols `first' (place point on the
325 first subject), `unread' (place point on the subject line of the first
326 unread article), `best' (place point on the subject line of the
327 higest-scored article), `unseen' (place point on the subject line of
328 the first unseen article), `unseen-or-unread' (place point on the subject
329 line of the first unseen article or, if all article have been seen, on the
330 subject line of the first unread article), or a function to be called to
331 place point on some subject line."
332   :version "22.1"
333   :group 'gnus-group-select
334   :type '(choice (const best)
335                  (const unread)
336                  (const first)
337                  (const unseen)
338                  (const unseen-or-unread)))
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols nil (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :version "22.1"
377   :group 'gnus-summary-maneuvering
378   :type '(choice (const :tag "None" nil)
379                  (const :tag "Undownloaded when unplugged" undownloaded)
380                  (const :tag "Undownloaded" always-undownloaded)
381                  (const :tag "Unfetched" unfetched)))
382
383 (defcustom gnus-summary-check-current nil
384   "*If non-nil, consider the current article when moving.
385 The \"unread\" movement commands will stay on the same line if the
386 current article is unread."
387   :group 'gnus-summary-maneuvering
388   :type 'boolean)
389
390 (defcustom gnus-auto-center-summary 2
391   "*If non-nil, always center the current summary buffer.
392 In particular, if `vertical' do only vertical recentering.  If non-nil
393 and non-`vertical', do both horizontal and vertical recentering."
394   :group 'gnus-summary-maneuvering
395   :type '(choice (const :tag "none" nil)
396                  (const vertical)
397                  (integer :tag "height")
398                  (sexp :menu-tag "both" t)))
399
400 (defvar gnus-auto-center-group t
401   "*If non-nil, always center the group buffer.")
402
403 (defcustom gnus-show-all-headers nil
404   "*If non-nil, don't hide any headers."
405   :group 'gnus-article-hiding
406   :group 'gnus-article-headers
407   :type 'boolean)
408
409 (defcustom gnus-summary-ignore-duplicates nil
410   "*If non-nil, ignore articles with identical Message-ID headers."
411   :group 'gnus-summary
412   :type 'boolean)
413
414 (defcustom gnus-single-article-buffer t
415   "*If non-nil, display all articles in the same buffer.
416 If nil, each group will get its own article buffer."
417   :group 'gnus-article-various
418   :type 'boolean)
419
420 (defcustom gnus-break-pages t
421   "*If non-nil, do page breaking on articles.
422 The page delimiter is specified by the `gnus-page-delimiter'
423 variable."
424   :group 'gnus-article-various
425   :type 'boolean)
426
427 (defcustom gnus-move-split-methods nil
428   "*Variable used to suggest where articles are to be moved to.
429 It uses the same syntax as the `gnus-split-methods' variable.
430 However, whereas `gnus-split-methods' specifies file names as targets,
431 this variable specifies group names."
432   :group 'gnus-summary-mail
433   :type '(repeat (choice (list :value (fun) function)
434                          (cons :value ("" "") regexp (repeat string))
435                          (sexp :value nil))))
436
437 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
438   "Function used to compute default prefix for article move/copy/etc prompts.
439 The function should take one argument, a group name, and return a
440 string with the suggested prefix."
441   :group 'gnus-summary-mail
442   :type 'function)
443
444 ;; FIXME: Although the custom type is `character' for the following variables,
445 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
446
447 (defcustom gnus-unread-mark ?           ;Whitespace
448   "*Mark used for unread articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ticked-mark ?!
453   "*Mark used for ticked articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-dormant-mark ??
458   "*Mark used for dormant articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-del-mark ?r
463   "*Mark used for del'd articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-read-mark ?R
468   "*Mark used for read articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-expirable-mark ?E
473   "*Mark used for expirable articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-killed-mark ?K
478   "*Mark used for killed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-spam-mark ?$
483   "*Mark used for spam articles."
484   :version "22.1"
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :version "22.1"
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :version "22.1"
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-cached-mark ?*
526   "*Mark used for articles that are in the cache."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-saved-mark ?S
531   "*Mark used for articles that have been saved."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-unseen-mark ?.
536   "*Mark used for articles that haven't been seen."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-no-mark ?               ;Whitespace
542   "*Mark used for articles that have no other secondary mark."
543   :version "22.1"
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-ancient-mark ?O
548   "*Mark used for ancient articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-sparse-mark ?Q
553   "*Mark used for sparsely reffed articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-canceled-mark ?G
558   "*Mark used for canceled articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-duplicate-mark ?M
563   "*Mark used for duplicate articles."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-undownloaded-mark ?-
568   "*Mark used for articles that weren't downloaded."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloaded-mark ?+
574   "*Mark used for articles that were downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-downloadable-mark ?%
579   "*Mark used for articles that are to be downloaded."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unsendable-mark ?=
584   "*Mark used for articles that won't be sent."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-over-mark ?+
589   "*Score mark used for articles with high scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-score-below-mark ?-
594   "*Score mark used for articles with low scores."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-empty-thread-mark ?     ;Whitespace
599   "*There is no thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-not-empty-thread-mark ?=
604   "*There is a thread under the article."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-view-pseudo-asynchronously nil
609   "*If non-nil, Gnus will view pseudo-articles asynchronously."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-auto-expirable-marks
614   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
615         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
616         gnus-souped-mark gnus-duplicate-mark)
617   "*The list of marks converted into expiration if a group is auto-expirable."
618   :version "21.1"
619   :group 'gnus-summary
620   :type '(repeat character))
621
622 (defcustom gnus-inhibit-user-auto-expire t
623   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
624   :version "21.1"
625   :group 'gnus-summary
626   :type 'boolean)
627
628 (defcustom gnus-view-pseudos nil
629   "*If `automatic', pseudo-articles will be viewed automatically.
630 If `not-confirm', pseudos will be viewed automatically, and the user
631 will not be asked to confirm the command."
632   :group 'gnus-extract-view
633   :type '(choice (const :tag "off" nil)
634                  (const automatic)
635                  (const not-confirm)))
636
637 (defcustom gnus-view-pseudos-separately t
638   "*If non-nil, one pseudo-article will be created for each file to be viewed.
639 If nil, all files that use the same viewing command will be given as a
640 list of parameters to that command."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-insert-pseudo-articles t
645   "*If non-nil, insert pseudo-articles when decoding articles."
646   :group 'gnus-extract-view
647   :type 'boolean)
648
649 (defcustom gnus-summary-dummy-line-format
650   "   %(:                             :%) %S\n"
651   "*The format specification for the dummy roots in the summary buffer.
652 It works along the same lines as a normal formatting string,
653 with some simple extensions.
654
655 %S  The subject
656
657 General format specifiers can also be used.
658 See `(gnus)Formatting Variables'."
659   :link '(custom-manual "(gnus)Formatting Variables")
660   :group 'gnus-threading
661   :type 'string)
662
663 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
664   "*The format specification for the summary mode line.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions:
667
668 %G  Group name
669 %p  Unprefixed group name
670 %A  Current article number
671 %z  Current article score
672 %V  Gnus version
673 %U  Number of unread articles in the group
674 %e  Number of unselected articles in the group
675 %Z  A string with unread/unselected article counts
676 %g  Shortish group name
677 %S  Subject of the current article
678 %u  User-defined spec
679 %s  Current score file name
680 %d  Number of dormant articles
681 %r  Number of articles that have been marked as read in this session
682 %E  Number of articles expunged by the score files"
683   :group 'gnus-summary-format
684   :type 'string)
685
686 (defcustom gnus-list-identifiers nil
687   "Regexp that matches list identifiers to be removed from subject.
688 This can also be a list of regexps."
689   :version "21.1"
690   :group 'gnus-summary-format
691   :group 'gnus-article-hiding
692   :type '(choice (const :tag "none" nil)
693                  (regexp :value ".*")
694                  (repeat :value (".*") regexp)))
695
696 (defcustom gnus-summary-mark-below 0
697   "*Mark all articles with a score below this variable as read.
698 This variable is local to each summary buffer and usually set by the
699 score file."
700   :group 'gnus-score-default
701   :type 'integer)
702
703 (defun gnus-widget-reversible-match (widget value)
704   "Ignoring WIDGET, convert VALUE to internal form.
705 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
706   ;; (debug value)
707   (or (symbolp value)
708       (and (listp value)
709            (eq (length value) 2)
710            (eq (nth 0 value) 'not)
711            (symbolp (nth 1 value)))))
712
713 (defun gnus-widget-reversible-to-internal (widget value)
714   "Ignoring WIDGET, convert VALUE to internal form.
715 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
716 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
717   ;; (debug value)
718   (if (atom value)
719       (list value nil)
720     (list (nth 1 value) t)))
721
722 (defun gnus-widget-reversible-to-external (widget value)
723   "Ignoring WIDGET, convert VALUE to external form.
724 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
725 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
726   ;; (debug value)
727   (if (nth 1 value)
728       (list 'not (nth 0 value))
729     (nth 0 value)))
730
731 (define-widget 'gnus-widget-reversible 'group
732   "A `group' that convert values."
733   :match 'gnus-widget-reversible-match
734   :value-to-internal 'gnus-widget-reversible-to-internal
735   :value-to-external 'gnus-widget-reversible-to-external)
736
737 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
738   "*List of functions used for sorting articles in the summary buffer.
739
740 Each function takes two articles and returns non-nil if the first
741 article should be sorted before the other.  If you use more than one
742 function, the primary sort function should be the last.  You should
743 probably always include `gnus-article-sort-by-number' in the list of
744 sorting functions -- preferably first.  Also note that sorting by date
745 is often much slower than sorting by number, and the sorting order is
746 very similar.  (Sorting by date means sorting by the time the message
747 was sent, sorting by number means sorting by arrival time.)
748
749 Each item can also be a list `(not F)' where F is a function;
750 this reverses the sort order.
751
752 Ready-made functions include `gnus-article-sort-by-number',
753 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
754 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
755 and `gnus-article-sort-by-score'.
756
757 When threading is turned on, the variable `gnus-thread-sort-functions'
758 controls how articles are sorted."
759   :group 'gnus-summary-sort
760   :type '(repeat (gnus-widget-reversible
761                   (choice (function-item gnus-article-sort-by-number)
762                           (function-item gnus-article-sort-by-author)
763                           (function-item gnus-article-sort-by-subject)
764                           (function-item gnus-article-sort-by-date)
765                           (function-item gnus-article-sort-by-score)
766                           (function-item gnus-article-sort-by-random)
767                           (function :tag "other"))
768                   (boolean :tag "Reverse order"))))
769
770
771 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
772   "*List of functions used for sorting threads in the summary buffer.
773 By default, threads are sorted by article number.
774
775 Each function takes two threads and returns non-nil if the first
776 thread should be sorted before the other.  If you use more than one
777 function, the primary sort function should be the last.  You should
778 probably always include `gnus-thread-sort-by-number' in the list of
779 sorting functions -- preferably first.  Also note that sorting by date
780 is often much slower than sorting by number, and the sorting order is
781 very similar.  (Sorting by date means sorting by the time the message
782 was sent, sorting by number means sorting by arrival time.)
783
784 Each list item can also be a list `(not F)' where F is a
785 function; this specifies reversed sort order.
786
787 Ready-made functions include `gnus-thread-sort-by-number',
788 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
789 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
790 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
791 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
792 and `gnus-thread-sort-by-total-score' (see
793 `gnus-thread-score-function').
794
795 When threading is turned off, the variable
796 `gnus-article-sort-functions' controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat
799           (gnus-widget-reversible
800            (choice (function-item gnus-thread-sort-by-number)
801                    (function-item gnus-thread-sort-by-author)
802                    (function-item gnus-thread-sort-by-recipient)
803                    (function-item gnus-thread-sort-by-subject)
804                    (function-item gnus-thread-sort-by-date)
805                    (function-item gnus-thread-sort-by-score)
806                    (function-item gnus-thread-sort-by-most-recent-number)
807                    (function-item gnus-thread-sort-by-most-recent-date)
808                    (function-item gnus-thread-sort-by-random)
809                    (function-item gnus-thread-sort-by-total-score)
810                    (function :tag "other"))
811            (boolean :tag "Reverse order"))))
812
813 (defcustom gnus-thread-score-function '+
814   "*Function used for calculating the total score of a thread.
815
816 The function is called with the scores of the article and each
817 subthread and should then return the score of the thread.
818
819 Some functions you can use are `+', `max', or `min'."
820   :group 'gnus-summary-sort
821   :type 'function)
822
823 (defcustom gnus-summary-expunge-below nil
824   "All articles that have a score less than this variable will be expunged.
825 This variable is local to the summary buffers."
826   :group 'gnus-score-default
827   :type '(choice (const :tag "off" nil)
828                  integer))
829
830 (defcustom gnus-thread-expunge-below nil
831   "All threads that have a total score less than this variable will be expunged.
832 See `gnus-thread-score-function' for en explanation of what a
833 \"thread score\" is.
834
835 This variable is local to the summary buffers."
836   :group 'gnus-threading
837   :group 'gnus-score-default
838   :type '(choice (const :tag "off" nil)
839                  integer))
840
841 (defcustom gnus-summary-mode-hook nil
842   "*A hook for Gnus summary mode.
843 This hook is run before any variables are set in the summary buffer."
844   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
845   :group 'gnus-summary-various
846   :type 'hook)
847
848 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
849 (when (featurep 'xemacs)
850   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
851   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
852   (add-hook 'gnus-summary-mode-hook
853             'gnus-xmas-switch-horizontal-scrollbar-off))
854
855 (defcustom gnus-summary-menu-hook nil
856   "*Hook run after the creation of the summary mode menu."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-summary-exit-hook nil
861   "*A hook called on exit from the summary buffer.
862 It will be called with point in the group buffer."
863   :group 'gnus-summary-exit
864   :type 'hook)
865
866 (defcustom gnus-summary-prepare-hook nil
867   "*A hook called after the summary buffer has been generated.
868 If you want to modify the summary buffer, you can use this hook."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-prepared-hook nil
873   "*A hook called as the last thing after the summary buffer has been generated."
874   :group 'gnus-summary-various
875   :type 'hook)
876
877 (defcustom gnus-summary-generate-hook nil
878   "*A hook run just before generating the summary buffer.
879 This hook is commonly used to customize threading variables and the
880 like."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-select-group-hook nil
885   "*A hook called when a newsgroup is selected.
886
887 If you'd like to simplify subjects like the
888 `gnus-summary-next-same-subject' command does, you can use the
889 following hook:
890
891  (add-hook gnus-select-group-hook
892            (lambda ()
893              (mapcar (lambda (header)
894                        (mail-header-set-subject
895                         header
896                         (gnus-simplify-subject
897                          (mail-header-subject header) 're-only)))
898                      gnus-newsgroup-headers)))"
899   :group 'gnus-group-select
900   :type 'hook)
901
902 (defcustom gnus-select-article-hook nil
903   "*A hook called when an article is selected."
904   :group 'gnus-summary-choose
905   :options '(gnus-agent-fetch-selected-article)
906   :type 'hook)
907
908 (defcustom gnus-visual-mark-article-hook
909   (list 'gnus-highlight-selected-summary)
910   "*Hook run after selecting an article in the summary buffer.
911 It is meant to be used for highlighting the article in some way.  It
912 is not run if `gnus-visual' is nil."
913   :group 'gnus-summary-visual
914   :type 'hook)
915
916 (defcustom gnus-parse-headers-hook nil
917   "*A hook called before parsing the headers."
918   :group 'gnus-various
919   :type 'hook)
920
921 (defcustom gnus-exit-group-hook nil
922   "*A hook called when exiting summary mode.
923 This hook is not called from the non-updating exit commands like `Q'."
924   :group 'gnus-various
925   :type 'hook)
926
927 (defcustom gnus-summary-update-hook
928   (list 'gnus-summary-highlight-line)
929   "*A hook called when a summary line is changed.
930 The hook will not be called if `gnus-visual' is nil.
931
932 The default function `gnus-summary-highlight-line' will
933 highlight the line according to the `gnus-summary-highlight'
934 variable."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
939   "*A hook called when an article is selected for the first time.
940 The hook is intended to mark an article as read (or unread)
941 automatically when it is selected."
942   :group 'gnus-summary-choose
943   :type 'hook)
944
945 (defcustom gnus-group-no-more-groups-hook nil
946   "*A hook run when returning to group mode having no more (unread) groups."
947   :group 'gnus-group-select
948   :type 'hook)
949
950 (defcustom gnus-ps-print-hook nil
951   "*A hook run before ps-printing something from Gnus."
952   :group 'gnus-summary
953   :type 'hook)
954
955 (defcustom gnus-summary-article-move-hook nil
956   "*A hook called after an article is moved, copied, respooled, or crossposted."
957   :version "22.1"
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-article-delete-hook nil
962   "*A hook called after an article is deleted."
963   :version "22.1"
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :version "22.1"
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "22.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded)
995     (uncached
996      . gnus-summary-normal-undownloaded)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread)
1020     ((> score default-high)
1021      . gnus-summary-high-read)
1022     ((< score default-low)
1023      . gnus-summary-low-read)
1024     (t
1025      . gnus-summary-normal-read))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1055   "Function used to decode a string with encoded words.")
1056
1057 (defvar gnus-decode-encoded-address-function
1058   'mail-decode-encoded-address-string
1059   "Function used to decode addresses with encoded words.")
1060
1061 (defcustom gnus-extra-headers '(To Newsgroups)
1062   "*Extra headers to parse."
1063   :version "21.1"
1064   :group 'gnus-summary
1065   :type '(repeat symbol))
1066
1067 (defcustom gnus-ignored-from-addresses
1068   (and user-mail-address
1069        (not (string= user-mail-address ""))
1070        (regexp-quote user-mail-address))
1071   "*Regexp of From headers that may be suppressed in favor of To headers."
1072   :version "21.1"
1073   :group 'gnus-summary
1074   :type 'regexp)
1075
1076 (defcustom gnus-summary-to-prefix "-> "
1077   "*String prefixed to the To field in the summary line when
1078 using `gnus-ignored-from-addresses'."
1079   :version "22.1"
1080   :group 'gnus-summary
1081   :type 'string)
1082
1083 (defcustom gnus-summary-newsgroup-prefix "=> "
1084   "*String prefixed to the Newsgroup field in the summary
1085 line when using `gnus-ignored-from-addresses'."
1086   :version "22.1"
1087   :group 'gnus-summary
1088   :type 'string)
1089
1090 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1091   "List of charsets that should be ignored.
1092 When these charsets are used in the \"charset\" parameter, the
1093 default charset will be used instead."
1094   :version "21.1"
1095   :type '(repeat symbol)
1096   :group 'gnus-charset)
1097
1098 (gnus-define-group-parameter
1099  ignored-charsets
1100  :type list
1101  :function-document
1102  "Return the ignored charsets of GROUP."
1103  :variable gnus-group-ignored-charsets-alist
1104  :variable-default
1105  '(("alt\\.chinese\\.text" iso-8859-1))
1106  :variable-document
1107  "Alist of regexps (to match group names) and charsets that should be ignored.
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead."
1110  :variable-group gnus-charset
1111  :variable-type '(repeat (cons (regexp :tag "Group")
1112                                (repeat symbol)))
1113  :parameter-type '(choice :tag "Ignored charsets"
1114                           :value nil
1115                           (repeat (symbol)))
1116  :parameter-document       "\
1117 List of charsets that should be ignored.
1118
1119 When these charsets are used in the \"charset\" parameter, the
1120 default charset will be used instead.")
1121
1122 (defcustom gnus-group-highlight-words-alist nil
1123   "Alist of group regexps and highlight regexps.
1124 This variable uses the same syntax as `gnus-emphasis-alist'."
1125   :version "21.1"
1126   :type '(repeat (cons (regexp :tag "Group")
1127                        (repeat (list (regexp :tag "Highlight regexp")
1128                                      (number :tag "Group for entire word" 0)
1129                                      (number :tag "Group for displayed part" 0)
1130                                      (symbol :tag "Face"
1131                                              gnus-emphasis-highlight-words)))))
1132   :group 'gnus-summary-visual)
1133
1134 (defcustom gnus-summary-show-article-charset-alist
1135   nil
1136   "Alist of number and charset.
1137 The article will be shown with the charset corresponding to the
1138 numbered argument.
1139 For example: ((1 . cn-gb-2312) (2 . big5))."
1140   :version "21.1"
1141   :type '(repeat (cons (number :tag "Argument" 1)
1142                        (symbol :tag "Charset")))
1143   :group 'gnus-charset)
1144
1145 (defcustom gnus-preserve-marks t
1146   "Whether marks are preserved when moving, copying and respooling messages."
1147   :version "21.1"
1148   :type 'boolean
1149   :group 'gnus-summary-marks)
1150
1151 (defcustom gnus-alter-articles-to-read-function nil
1152   "Function to be called to alter the list of articles to be selected."
1153   :type '(choice (const nil) function)
1154   :group 'gnus-summary)
1155
1156 (defcustom gnus-orphan-score nil
1157   "*All orphans get this score added.  Set in the score file."
1158   :group 'gnus-score-default
1159   :type '(choice (const nil)
1160                  integer))
1161
1162 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1163   "*A regexp to match MIME parts when saving multiple parts of a
1164 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1165 This regexp will be used by default when prompting the user for which
1166 type of files to save."
1167   :group 'gnus-summary
1168   :type 'regexp)
1169
1170 (defcustom gnus-read-all-available-headers nil
1171   "Whether Gnus should parse all headers made available to it.
1172 This is mostly relevant for slow back ends where the user may
1173 wish to widen the summary buffer to include all headers
1174 that were fetched.  Say, for nnultimate groups."
1175   :version "22.1"
1176   :group 'gnus-summary
1177   :type '(choice boolean regexp))
1178
1179 (defcustom gnus-summary-muttprint-program "muttprint"
1180   "Command (and optional arguments) used to run Muttprint."
1181   :version "22.1"
1182   :group 'gnus-summary
1183   :type 'string)
1184
1185 (defcustom gnus-article-loose-mime t
1186   "If non-nil, don't require MIME-Version header.
1187 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1188 supply the MIME-Version header or deliberately strip it from the mail.
1189 If non-nil (the default), Gnus will treat some articles as MIME
1190 even if the MIME-Version header is missing."
1191   :version "22.1"
1192   :type 'boolean
1193   :group 'gnus-article-mime)
1194
1195 (defcustom gnus-article-emulate-mime t
1196   "If non-nil, use MIME emulation for uuencode and the like.
1197 This means that Gnus will search message bodies for text that look
1198 like uuencoded bits, yEncoded bits, and so on, and present that using
1199 the normal Gnus MIME machinery."
1200   :version "22.1"
1201   :type 'boolean
1202   :group 'gnus-article-mime)
1203
1204 ;;; Internal variables
1205
1206 (defvar gnus-summary-display-cache nil)
1207 (defvar gnus-article-mime-handles nil)
1208 (defvar gnus-article-decoded-p nil)
1209 (defvar gnus-article-charset nil)
1210 (defvar gnus-article-ignored-charsets nil)
1211 (defvar gnus-scores-exclude-files nil)
1212 (defvar gnus-page-broken nil)
1213
1214 (defvar gnus-original-article nil)
1215 (defvar gnus-article-internal-prepare-hook nil)
1216 (defvar gnus-newsgroup-process-stack nil)
1217
1218 (defvar gnus-thread-indent-array nil)
1219 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1220 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1221   "Function called to sort the articles within a thread after it has been gathered together.")
1222
1223 (defvar gnus-summary-save-parts-type-history nil)
1224 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1225
1226 ;; Avoid highlighting in kill files.
1227 (defvar gnus-summary-inhibit-highlight nil)
1228 (defvar gnus-newsgroup-selected-overlay nil)
1229 (defvar gnus-inhibit-limiting nil)
1230 (defvar gnus-newsgroup-adaptive-score-file nil)
1231 (defvar gnus-current-score-file nil)
1232 (defvar gnus-current-move-group nil)
1233 (defvar gnus-current-copy-group nil)
1234 (defvar gnus-current-crosspost-group nil)
1235 (defvar gnus-newsgroup-display nil)
1236
1237 (defvar gnus-newsgroup-dependencies nil)
1238 (defvar gnus-newsgroup-adaptive nil)
1239 (defvar gnus-summary-display-article-function nil)
1240 (defvar gnus-summary-highlight-line-function nil
1241   "Function called after highlighting a summary line.")
1242
1243 (defvar gnus-summary-line-format-alist
1244   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1245     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1246     (?s gnus-tmp-subject-or-nil ?s)
1247     (?n gnus-tmp-name ?s)
1248     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1249         ?s)
1250     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1251             gnus-tmp-from) ?s)
1252     (?F gnus-tmp-from ?s)
1253     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1254     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1255     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1256     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1257     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1258     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1259     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1260     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1261     (?L gnus-tmp-lines ?s)
1262     (?O gnus-tmp-downloaded ?c)
1263     (?I gnus-tmp-indentation ?s)
1264     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1265     (?R gnus-tmp-replied ?c)
1266     (?\[ gnus-tmp-opening-bracket ?c)
1267     (?\] gnus-tmp-closing-bracket ?c)
1268     (?\> (make-string gnus-tmp-level ? ) ?s)
1269     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1270     (?i gnus-tmp-score ?d)
1271     (?z gnus-tmp-score-char ?c)
1272     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1273     (?U gnus-tmp-unread ?c)
1274     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1275         ?s)
1276     (?t (gnus-summary-number-of-articles-in-thread
1277          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1278         ?d)
1279     (?e (gnus-summary-number-of-articles-in-thread
1280          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1281         ?c)
1282     (?u gnus-tmp-user-defined ?s)
1283     (?P (gnus-pick-line-number) ?d)
1284     (?B gnus-tmp-thread-tree-header-string ?s)
1285     (user-date (gnus-user-date
1286                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1287   "An alist of format specifications that can appear in summary lines.
1288 These are paired with what variables they correspond with, along with
1289 the type of the variable (string, integer, character, etc).")
1290
1291 (defvar gnus-summary-dummy-line-format-alist
1292   `((?S gnus-tmp-subject ?s)
1293     (?N gnus-tmp-number ?d)
1294     (?u gnus-tmp-user-defined ?s)))
1295
1296 (defvar gnus-summary-mode-line-format-alist
1297   `((?G gnus-tmp-group-name ?s)
1298     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1299     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1300     (?A gnus-tmp-article-number ?d)
1301     (?Z gnus-tmp-unread-and-unselected ?s)
1302     (?V gnus-version ?s)
1303     (?U gnus-tmp-unread-and-unticked ?d)
1304     (?S gnus-tmp-subject ?s)
1305     (?e gnus-tmp-unselected ?d)
1306     (?u gnus-tmp-user-defined ?s)
1307     (?d (length gnus-newsgroup-dormant) ?d)
1308     (?t (length gnus-newsgroup-marked) ?d)
1309     (?h (length gnus-newsgroup-spam-marked) ?d)
1310     (?r (length gnus-newsgroup-reads) ?d)
1311     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1312     (?E gnus-newsgroup-expunged-tally ?d)
1313     (?s (gnus-current-score-file-nondirectory) ?s)))
1314
1315 (defvar gnus-last-search-regexp nil
1316   "Default regexp for article search command.")
1317
1318 (defvar gnus-last-shell-command nil
1319   "Default shell command on article.")
1320
1321 (defvar gnus-newsgroup-agentized nil
1322   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1323 (defvar gnus-newsgroup-begin nil)
1324 (defvar gnus-newsgroup-end nil)
1325 (defvar gnus-newsgroup-last-rmail nil)
1326 (defvar gnus-newsgroup-last-mail nil)
1327 (defvar gnus-newsgroup-last-folder nil)
1328 (defvar gnus-newsgroup-last-file nil)
1329 (defvar gnus-newsgroup-last-directory nil)
1330 (defvar gnus-newsgroup-auto-expire nil)
1331 (defvar gnus-newsgroup-active nil)
1332
1333 (defvar gnus-newsgroup-data nil)
1334 (defvar gnus-newsgroup-data-reverse nil)
1335 (defvar gnus-newsgroup-limit nil)
1336 (defvar gnus-newsgroup-limits nil)
1337 (defvar gnus-summary-use-undownloaded-faces nil)
1338
1339 (defvar gnus-newsgroup-unreads nil
1340   "Sorted list of unread articles in the current newsgroup.")
1341
1342 (defvar gnus-newsgroup-unselected nil
1343   "Sorted list of unselected unread articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-reads nil
1346   "Alist of read articles and article marks in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-expunged-tally nil)
1349
1350 (defvar gnus-newsgroup-marked nil
1351   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1352
1353 (defvar gnus-newsgroup-spam-marked nil
1354   "List of ranges of articles that have been marked as spam.")
1355
1356 (defvar gnus-newsgroup-killed nil
1357   "List of ranges of articles that have been through the scoring process.")
1358
1359 (defvar gnus-newsgroup-cached nil
1360   "Sorted list of articles that come from the article cache.")
1361
1362 (defvar gnus-newsgroup-saved nil
1363   "List of articles that have been saved.")
1364
1365 (defvar gnus-newsgroup-kill-headers nil)
1366
1367 (defvar gnus-newsgroup-replied nil
1368   "List of articles that have been replied to in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-forwarded nil
1371   "List of articles that have been forwarded in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-recent nil
1374   "List of articles that have are recent in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-expirable nil
1377   "Sorted list of articles in the current newsgroup that can be expired.")
1378
1379 (defvar gnus-newsgroup-processable nil
1380   "List of articles in the current newsgroup that can be processed.")
1381
1382 (defvar gnus-newsgroup-downloadable nil
1383   "Sorted list of articles in the current newsgroup that can be processed.")
1384
1385 (defvar gnus-newsgroup-unfetched nil
1386   "Sorted list of articles in the current newsgroup whose headers have
1387 not been fetched into the agent.
1388
1389 This list will always be a subset of gnus-newsgroup-undownloaded.")
1390
1391 (defvar gnus-newsgroup-undownloaded nil
1392   "List of articles in the current newsgroup that haven't been downloaded.")
1393
1394 (defvar gnus-newsgroup-unsendable nil
1395   "List of articles in the current newsgroup that won't be sent.")
1396
1397 (defvar gnus-newsgroup-bookmarks nil
1398   "List of articles in the current newsgroup that have bookmarks.")
1399
1400 (defvar gnus-newsgroup-dormant nil
1401   "Sorted list of dormant articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-unseen nil
1404   "List of unseen articles in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-seen nil
1407   "Range of seen articles in the current newsgroup.")
1408
1409 (defvar gnus-newsgroup-articles nil
1410   "List of articles in the current newsgroup.")
1411
1412 (defvar gnus-newsgroup-scored nil
1413   "List of scored articles in the current newsgroup.")
1414
1415 (defvar gnus-newsgroup-headers nil
1416   "List of article headers in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-threads nil)
1419
1420 (defvar gnus-newsgroup-prepared nil
1421   "Whether the current group has been prepared properly.")
1422
1423 (defvar gnus-newsgroup-ancient nil
1424   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-sparse nil)
1427
1428 (defvar gnus-current-article nil)
1429 (defvar gnus-article-current nil)
1430 (defvar gnus-current-headers nil)
1431 (defvar gnus-have-all-headers nil)
1432 (defvar gnus-last-article nil)
1433 (defvar gnus-newsgroup-history nil)
1434 (defvar gnus-newsgroup-charset nil)
1435 (defvar gnus-newsgroup-ephemeral-charset nil)
1436 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1437
1438 (defvar gnus-article-before-search nil)
1439
1440 (defvar gnus-summary-local-variables
1441   '(gnus-newsgroup-name
1442     gnus-newsgroup-begin gnus-newsgroup-end
1443     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1444     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1445     gnus-newsgroup-last-directory
1446     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1447     gnus-newsgroup-unselected gnus-newsgroup-marked
1448     gnus-newsgroup-spam-marked
1449     gnus-newsgroup-reads gnus-newsgroup-saved
1450     gnus-newsgroup-replied gnus-newsgroup-forwarded
1451     gnus-newsgroup-recent
1452     gnus-newsgroup-expirable
1453     gnus-newsgroup-processable gnus-newsgroup-killed
1454     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1455     gnus-newsgroup-unfetched
1456     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1457     gnus-newsgroup-seen gnus-newsgroup-articles
1458     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1459     gnus-newsgroup-headers gnus-newsgroup-threads
1460     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1461     gnus-current-article gnus-current-headers gnus-have-all-headers
1462     gnus-last-article gnus-article-internal-prepare-hook
1463     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1464     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1465     gnus-thread-expunge-below
1466     gnus-score-alist gnus-current-score-file
1467     (gnus-summary-expunge-below . global)
1468     (gnus-summary-mark-below . global)
1469     (gnus-orphan-score . global)
1470     gnus-newsgroup-active gnus-scores-exclude-files
1471     gnus-newsgroup-history gnus-newsgroup-ancient
1472     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1473     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1474     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1475     (gnus-newsgroup-expunged-tally . 0)
1476     gnus-cache-removable-articles gnus-newsgroup-cached
1477     gnus-newsgroup-data gnus-newsgroup-data-reverse
1478     gnus-newsgroup-limit gnus-newsgroup-limits
1479     gnus-newsgroup-charset gnus-newsgroup-display
1480     gnus-summary-use-undownloaded-faces)
1481   "Variables that are buffer-local to the summary buffers.")
1482
1483 (defvar gnus-newsgroup-variables nil
1484   "A list of variables that have separate values in different newsgroups.
1485 A list of newsgroup (summary buffer) local variables, or cons of
1486 variables and their default expressions to be evalled (when the default
1487 values are not nil), that should be made global while the summary buffer
1488 is active.
1489
1490 Note: The default expressions will be evaluated (using function `eval')
1491 before assignment to the local variable rather than just assigned to it.
1492 If the default expression is the symbol `global', that symbol will not
1493 be evaluated but the global value of the local variable will be used
1494 instead.
1495
1496 These variables can be used to set variables in the group parameters
1497 while still allowing them to affect operations done in other buffers.
1498 For example:
1499
1500 \(setq gnus-newsgroup-variables
1501      '(message-use-followup-to
1502        (gnus-visible-headers .
1503          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1504 ")
1505
1506 ;; Byte-compiler warning.
1507 (eval-when-compile
1508   ;; Bind features so that require will believe that gnus-sum has
1509   ;; already been loaded (avoids infinite recursion)
1510   (let ((features (cons 'gnus-sum features)))
1511     ;; Several of the declarations in gnus-sum are needed to load the
1512     ;; following files. Right now, these definitions have been
1513     ;; compiled but not defined (evaluated).  We could either do a
1514     ;; eval-and-compile about all of the declarations or evaluate the
1515     ;; source file.
1516     (if (boundp 'gnus-newsgroup-variables)
1517         nil
1518       (load "gnus-sum.el" t t t))
1519     (require 'gnus)
1520     (require 'gnus-art)))
1521
1522 ;; MIME stuff.
1523
1524 (defvar gnus-decode-encoded-word-methods
1525   '(mail-decode-encoded-word-string)
1526   "List of methods used to decode encoded words.
1527
1528 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1529 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1530 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1531 whose names match REGEXP.
1532
1533 For example:
1534 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1535  mail-decode-encoded-word-string
1536  (\"chinese\" . rfc1843-decode-string))")
1537
1538 (defvar gnus-decode-encoded-word-methods-cache nil)
1539
1540 (defun gnus-multi-decode-encoded-word-string (string)
1541   "Apply the functions from `gnus-encoded-word-methods' that match."
1542   (unless (and gnus-decode-encoded-word-methods-cache
1543                (eq gnus-newsgroup-name
1544                    (car gnus-decode-encoded-word-methods-cache)))
1545     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1546     (mapcar (lambda (x)
1547               (if (symbolp x)
1548                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1549                 (if (and gnus-newsgroup-name
1550                          (string-match (car x) gnus-newsgroup-name))
1551                     (nconc gnus-decode-encoded-word-methods-cache
1552                            (list (cdr x))))))
1553             gnus-decode-encoded-word-methods))
1554   (let ((xlist gnus-decode-encoded-word-methods-cache))
1555     (pop xlist)
1556     (while xlist
1557       (setq string (funcall (pop xlist) string))))
1558   string)
1559
1560 ;; Subject simplification.
1561
1562 (defun gnus-simplify-whitespace (str)
1563   "Remove excessive whitespace from STR."
1564   ;; Multiple spaces.
1565   (while (string-match "[ \t][ \t]+" str)
1566     (setq str (concat (substring str 0 (match-beginning 0))
1567                         " "
1568                         (substring str (match-end 0)))))
1569   ;; Leading spaces.
1570   (when (string-match "^[ \t]+" str)
1571     (setq str (substring str (match-end 0))))
1572   ;; Trailing spaces.
1573   (when (string-match "[ \t]+$" str)
1574     (setq str (substring str 0 (match-beginning 0))))
1575   str)
1576
1577 (defun gnus-simplify-all-whitespace (str)
1578   "Remove all whitespace from STR."
1579   (while (string-match "[ \t\n]+" str)
1580     (setq str (replace-match "" nil nil str)))
1581   str)
1582
1583 (defsubst gnus-simplify-subject-re (subject)
1584   "Remove \"Re:\" from subject lines."
1585   (if (string-match message-subject-re-regexp subject)
1586       (substring subject (match-end 0))
1587     subject))
1588
1589 (defun gnus-simplify-subject (subject &optional re-only)
1590   "Remove `Re:' and words in parentheses.
1591 If RE-ONLY is non-nil, strip leading `Re:'s only."
1592   (let ((case-fold-search t))           ;Ignore case.
1593     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1594     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1595       (setq subject (substring subject (match-end 0))))
1596     ;; Remove uninteresting prefixes.
1597     (when (and (not re-only)
1598                gnus-simplify-ignored-prefixes
1599                (string-match gnus-simplify-ignored-prefixes subject))
1600       (setq subject (substring subject (match-end 0))))
1601     ;; Remove words in parentheses from end.
1602     (unless re-only
1603       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1604         (setq subject (substring subject 0 (match-beginning 0)))))
1605     ;; Return subject string.
1606     subject))
1607
1608 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1609 ;; all whitespace.
1610 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1611   (goto-char (point-min))
1612   (while (re-search-forward regexp nil t)
1613     (replace-match (or newtext ""))))
1614
1615 (defun gnus-simplify-buffer-fuzzy ()
1616   "Simplify string in the buffer fuzzily.
1617 The string in the accessible portion of the current buffer is simplified.
1618 It is assumed to be a single-line subject.
1619 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1620 matter is removed.  Additional things can be deleted by setting
1621 `gnus-simplify-subject-fuzzy-regexp'."
1622   (let ((case-fold-search t)
1623         (modified-tick))
1624     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1625
1626     (while (not (eq modified-tick (buffer-modified-tick)))
1627       (setq modified-tick (buffer-modified-tick))
1628       (cond
1629        ((listp gnus-simplify-subject-fuzzy-regexp)
1630         (mapcar 'gnus-simplify-buffer-fuzzy-step
1631                 gnus-simplify-subject-fuzzy-regexp))
1632        (gnus-simplify-subject-fuzzy-regexp
1633         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1634       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1635       (gnus-simplify-buffer-fuzzy-step
1636        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1637       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1638
1639     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1640     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1641     (gnus-simplify-buffer-fuzzy-step " $")
1642     (gnus-simplify-buffer-fuzzy-step "^ +")))
1643
1644 (defun gnus-simplify-subject-fuzzy (subject)
1645   "Simplify a subject string fuzzily.
1646 See `gnus-simplify-buffer-fuzzy' for details."
1647   (save-excursion
1648     (gnus-set-work-buffer)
1649     (let ((case-fold-search t))
1650       ;; Remove uninteresting prefixes.
1651       (when (and gnus-simplify-ignored-prefixes
1652                  (string-match gnus-simplify-ignored-prefixes subject))
1653         (setq subject (substring subject (match-end 0))))
1654       (insert subject)
1655       (inline (gnus-simplify-buffer-fuzzy))
1656       (buffer-string))))
1657
1658 (defsubst gnus-simplify-subject-fully (subject)
1659   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1660   (cond
1661    (gnus-simplify-subject-functions
1662     (gnus-map-function gnus-simplify-subject-functions subject))
1663    ((null gnus-summary-gather-subject-limit)
1664     (gnus-simplify-subject-re subject))
1665    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1666     (gnus-simplify-subject-fuzzy subject))
1667    ((numberp gnus-summary-gather-subject-limit)
1668     (gnus-limit-string (gnus-simplify-subject-re subject)
1669                        gnus-summary-gather-subject-limit))
1670    (t
1671     subject)))
1672
1673 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1674   "Check whether two subjects are equal.
1675 If optional argument SIMPLE-FIRST is t, first argument is already
1676 simplified."
1677   (cond
1678    ((null simple-first)
1679     (equal (gnus-simplify-subject-fully s1)
1680            (gnus-simplify-subject-fully s2)))
1681    (t
1682     (equal s1
1683            (gnus-simplify-subject-fully s2)))))
1684
1685 (defun gnus-summary-bubble-group ()
1686   "Increase the score of the current group.
1687 This is a handy function to add to `gnus-summary-exit-hook' to
1688 increase the score of each group you read."
1689   (gnus-group-add-score gnus-newsgroup-name))
1690
1691 \f
1692 ;;;
1693 ;;; Gnus summary mode
1694 ;;;
1695
1696 (put 'gnus-summary-mode 'mode-class 'special)
1697
1698 (defvar gnus-article-commands-menu)
1699
1700 ;; Non-orthogonal keys
1701
1702 (gnus-define-keys gnus-summary-mode-map
1703   " " gnus-summary-next-page
1704   "\177" gnus-summary-prev-page
1705   [delete] gnus-summary-prev-page
1706   [backspace] gnus-summary-prev-page
1707   "\r" gnus-summary-scroll-up
1708   "\M-\r" gnus-summary-scroll-down
1709   "n" gnus-summary-next-unread-article
1710   "p" gnus-summary-prev-unread-article
1711   "N" gnus-summary-next-article
1712   "P" gnus-summary-prev-article
1713   "\M-\C-n" gnus-summary-next-same-subject
1714   "\M-\C-p" gnus-summary-prev-same-subject
1715   "\M-n" gnus-summary-next-unread-subject
1716   "\M-p" gnus-summary-prev-unread-subject
1717   "." gnus-summary-first-unread-article
1718   "," gnus-summary-best-unread-article
1719   "\M-s" gnus-summary-search-article-forward
1720   "\M-r" gnus-summary-search-article-backward
1721   "\M-S" gnus-summary-repeat-search-article-forward
1722   "\M-R" gnus-summary-repeat-search-article-backward
1723   "<" gnus-summary-beginning-of-article
1724   ">" gnus-summary-end-of-article
1725   "j" gnus-summary-goto-article
1726   "^" gnus-summary-refer-parent-article
1727   "\M-^" gnus-summary-refer-article
1728   "u" gnus-summary-tick-article-forward
1729   "!" gnus-summary-tick-article-forward
1730   "U" gnus-summary-tick-article-backward
1731   "d" gnus-summary-mark-as-read-forward
1732   "D" gnus-summary-mark-as-read-backward
1733   "E" gnus-summary-mark-as-expirable
1734   "\M-u" gnus-summary-clear-mark-forward
1735   "\M-U" gnus-summary-clear-mark-backward
1736   "k" gnus-summary-kill-same-subject-and-select
1737   "\C-k" gnus-summary-kill-same-subject
1738   "\M-\C-k" gnus-summary-kill-thread
1739   "\M-\C-l" gnus-summary-lower-thread
1740   "e" gnus-summary-edit-article
1741   "#" gnus-summary-mark-as-processable
1742   "\M-#" gnus-summary-unmark-as-processable
1743   "\M-\C-t" gnus-summary-toggle-threads
1744   "\M-\C-s" gnus-summary-show-thread
1745   "\M-\C-h" gnus-summary-hide-thread
1746   "\M-\C-f" gnus-summary-next-thread
1747   "\M-\C-b" gnus-summary-prev-thread
1748   [(meta down)] gnus-summary-next-thread
1749   [(meta up)] gnus-summary-prev-thread
1750   "\M-\C-u" gnus-summary-up-thread
1751   "\M-\C-d" gnus-summary-down-thread
1752   "&" gnus-summary-execute-command
1753   "c" gnus-summary-catchup-and-exit
1754   "\C-w" gnus-summary-mark-region-as-read
1755   "\C-t" gnus-summary-toggle-truncation
1756   "?" gnus-summary-mark-as-dormant
1757   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1758   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1759   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1760   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1761   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1762   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1763   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1764   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1765   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1766   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1767   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1768   "=" gnus-summary-expand-window
1769   "\C-x\C-s" gnus-summary-reselect-current-group
1770   "\M-g" gnus-summary-rescan-group
1771   "w" gnus-summary-stop-page-breaking
1772   "\C-c\C-r" gnus-summary-caesar-message
1773   "f" gnus-summary-followup
1774   "F" gnus-summary-followup-with-original
1775   "C" gnus-summary-cancel-article
1776   "r" gnus-summary-reply
1777   "R" gnus-summary-reply-with-original
1778   "\C-c\C-f" gnus-summary-mail-forward
1779   "o" gnus-summary-save-article
1780   "\C-o" gnus-summary-save-article-mail
1781   "|" gnus-summary-pipe-output
1782   "\M-k" gnus-summary-edit-local-kill
1783   "\M-K" gnus-summary-edit-global-kill
1784   ;; "V" gnus-version
1785   "\C-c\C-d" gnus-summary-describe-group
1786   "q" gnus-summary-exit
1787   "Q" gnus-summary-exit-no-update
1788   "\C-c\C-i" gnus-info-find-node
1789   gnus-mouse-2 gnus-mouse-pick-article
1790   [follow-link] mouse-face
1791   "m" gnus-summary-mail-other-window
1792   "a" gnus-summary-post-news
1793   "i" gnus-summary-news-other-window
1794   "x" gnus-summary-limit-to-unread
1795   "s" gnus-summary-isearch-article
1796   "t" gnus-summary-toggle-header
1797   "g" gnus-summary-show-article
1798   "l" gnus-summary-goto-last-article
1799   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1800   "\C-d" gnus-summary-enter-digest-group
1801   "\M-\C-d" gnus-summary-read-document
1802   "\M-\C-e" gnus-summary-edit-parameters
1803   "\M-\C-a" gnus-summary-customize-parameters
1804   "\C-c\C-b" gnus-bug
1805   "*" gnus-cache-enter-article
1806   "\M-*" gnus-cache-remove-article
1807   "\M-&" gnus-summary-universal-argument
1808   "\C-l" gnus-recenter
1809   "I" gnus-summary-increase-score
1810   "L" gnus-summary-lower-score
1811   "\M-i" gnus-symbolic-argument
1812   "h" gnus-summary-select-article-buffer
1813
1814   "b" gnus-article-view-part
1815   "\M-t" gnus-summary-toggle-display-buttonized
1816
1817   "V" gnus-summary-score-map
1818   "X" gnus-uu-extract-map
1819   "S" gnus-summary-send-map)
1820
1821 ;; Sort of orthogonal keymap
1822 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1823   "t" gnus-summary-tick-article-forward
1824   "!" gnus-summary-tick-article-forward
1825   "d" gnus-summary-mark-as-read-forward
1826   "r" gnus-summary-mark-as-read-forward
1827   "c" gnus-summary-clear-mark-forward
1828   " " gnus-summary-clear-mark-forward
1829   "e" gnus-summary-mark-as-expirable
1830   "x" gnus-summary-mark-as-expirable
1831   "?" gnus-summary-mark-as-dormant
1832   "b" gnus-summary-set-bookmark
1833   "B" gnus-summary-remove-bookmark
1834   "#" gnus-summary-mark-as-processable
1835   "\M-#" gnus-summary-unmark-as-processable
1836   "S" gnus-summary-limit-include-expunged
1837   "C" gnus-summary-catchup
1838   "H" gnus-summary-catchup-to-here
1839   "h" gnus-summary-catchup-from-here
1840   "\C-c" gnus-summary-catchup-all
1841   "k" gnus-summary-kill-same-subject-and-select
1842   "K" gnus-summary-kill-same-subject
1843   "P" gnus-uu-mark-map)
1844
1845 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1846   "c" gnus-summary-clear-above
1847   "u" gnus-summary-tick-above
1848   "m" gnus-summary-mark-above
1849   "k" gnus-summary-kill-below)
1850
1851 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1852   "/" gnus-summary-limit-to-subject
1853   "n" gnus-summary-limit-to-articles
1854   "b" gnus-summary-limit-to-bodies
1855   "h" gnus-summary-limit-to-headers
1856   "w" gnus-summary-pop-limit
1857   "s" gnus-summary-limit-to-subject
1858   "a" gnus-summary-limit-to-author
1859   "u" gnus-summary-limit-to-unread
1860   "m" gnus-summary-limit-to-marks
1861   "M" gnus-summary-limit-exclude-marks
1862   "v" gnus-summary-limit-to-score
1863   "*" gnus-summary-limit-include-cached
1864   "D" gnus-summary-limit-include-dormant
1865   "T" gnus-summary-limit-include-thread
1866   "d" gnus-summary-limit-exclude-dormant
1867   "t" gnus-summary-limit-to-age
1868   "." gnus-summary-limit-to-unseen
1869   "x" gnus-summary-limit-to-extra
1870   "p" gnus-summary-limit-to-display-predicate
1871   "E" gnus-summary-limit-include-expunged
1872   "c" gnus-summary-limit-exclude-childless-dormant
1873   "C" gnus-summary-limit-mark-excluded-as-read
1874   "o" gnus-summary-insert-old-articles
1875   "N" gnus-summary-insert-new-articles
1876   "r" gnus-summary-limit-to-replied
1877   "R" gnus-summary-limit-to-recipient)
1878
1879 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1880   "n" gnus-summary-next-unread-article
1881   "p" gnus-summary-prev-unread-article
1882   "N" gnus-summary-next-article
1883   "P" gnus-summary-prev-article
1884   "\C-n" gnus-summary-next-same-subject
1885   "\C-p" gnus-summary-prev-same-subject
1886   "\M-n" gnus-summary-next-unread-subject
1887   "\M-p" gnus-summary-prev-unread-subject
1888   "f" gnus-summary-first-unread-article
1889   "b" gnus-summary-best-unread-article
1890   "j" gnus-summary-goto-article
1891   "g" gnus-summary-goto-subject
1892   "l" gnus-summary-goto-last-article
1893   "o" gnus-summary-pop-article)
1894
1895 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1896   "k" gnus-summary-kill-thread
1897   "l" gnus-summary-lower-thread
1898   "i" gnus-summary-raise-thread
1899   "T" gnus-summary-toggle-threads
1900   "t" gnus-summary-rethread-current
1901   "^" gnus-summary-reparent-thread
1902   "\M-^" gnus-summary-reparent-children
1903   "s" gnus-summary-show-thread
1904   "S" gnus-summary-show-all-threads
1905   "h" gnus-summary-hide-thread
1906   "H" gnus-summary-hide-all-threads
1907   "n" gnus-summary-next-thread
1908   "p" gnus-summary-prev-thread
1909   "u" gnus-summary-up-thread
1910   "o" gnus-summary-top-thread
1911   "d" gnus-summary-down-thread
1912   "#" gnus-uu-mark-thread
1913   "\M-#" gnus-uu-unmark-thread)
1914
1915 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1916   "g" gnus-summary-prepare
1917   "c" gnus-summary-insert-cached-articles
1918   "d" gnus-summary-insert-dormant-articles)
1919
1920 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1921   "c" gnus-summary-catchup-and-exit
1922   "C" gnus-summary-catchup-all-and-exit
1923   "E" gnus-summary-exit-no-update
1924   "Q" gnus-summary-exit
1925   "Z" gnus-summary-exit
1926   "n" gnus-summary-catchup-and-goto-next-group
1927   "p" gnus-summary-catchup-and-goto-prev-group
1928   "R" gnus-summary-reselect-current-group
1929   "G" gnus-summary-rescan-group
1930   "N" gnus-summary-next-group
1931   "s" gnus-summary-save-newsrc
1932   "P" gnus-summary-prev-group)
1933
1934 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1935   " " gnus-summary-next-page
1936   "n" gnus-summary-next-page
1937   "\177" gnus-summary-prev-page
1938   [delete] gnus-summary-prev-page
1939   "p" gnus-summary-prev-page
1940   "\r" gnus-summary-scroll-up
1941   "\M-\r" gnus-summary-scroll-down
1942   "<" gnus-summary-beginning-of-article
1943   ">" gnus-summary-end-of-article
1944   "b" gnus-summary-beginning-of-article
1945   "e" gnus-summary-end-of-article
1946   "^" gnus-summary-refer-parent-article
1947   "r" gnus-summary-refer-parent-article
1948   "D" gnus-summary-enter-digest-group
1949   "R" gnus-summary-refer-references
1950   "T" gnus-summary-refer-thread
1951   "g" gnus-summary-show-article
1952   "s" gnus-summary-isearch-article
1953   "P" gnus-summary-print-article
1954   "M" gnus-mailing-list-insinuate
1955   "t" gnus-article-babel)
1956
1957 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1958   "b" gnus-article-add-buttons
1959   "B" gnus-article-add-buttons-to-head
1960   "o" gnus-article-treat-overstrike
1961   "e" gnus-article-emphasize
1962   "w" gnus-article-fill-cited-article
1963   "Q" gnus-article-fill-long-lines
1964   "L" gnus-article-toggle-truncate-lines
1965   "C" gnus-article-capitalize-sentences
1966   "c" gnus-article-remove-cr
1967   "q" gnus-article-de-quoted-unreadable
1968   "6" gnus-article-de-base64-unreadable
1969   "Z" gnus-article-decode-HZ
1970   "A" gnus-article-treat-ansi-sequences
1971   "h" gnus-article-wash-html
1972   "u" gnus-article-unsplit-urls
1973   "s" gnus-summary-force-verify-and-decrypt
1974   "f" gnus-article-display-x-face
1975   "l" gnus-summary-stop-page-breaking
1976   "r" gnus-summary-caesar-message
1977   "m" gnus-summary-morse-message
1978   "t" gnus-summary-toggle-header
1979   "g" gnus-treat-smiley
1980   "v" gnus-summary-verbose-headers
1981   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1982   "p" gnus-article-verify-x-pgp-sig
1983   "d" gnus-article-treat-dumbquotes
1984   "i" gnus-summary-idna-message)
1985
1986 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1987   ;; mnemonic: deuglif*Y*
1988   "u" gnus-article-outlook-unwrap-lines
1989   "a" gnus-article-outlook-repair-attribution
1990   "c" gnus-article-outlook-rearrange-citation
1991   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1992
1993 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1994   "a" gnus-article-hide
1995   "h" gnus-article-hide-headers
1996   "b" gnus-article-hide-boring-headers
1997   "s" gnus-article-hide-signature
1998   "c" gnus-article-hide-citation
1999   "C" gnus-article-hide-citation-in-followups
2000   "l" gnus-article-hide-list-identifiers
2001   "B" gnus-article-strip-banner
2002   "P" gnus-article-hide-pem
2003   "\C-c" gnus-article-hide-citation-maybe)
2004
2005 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2006   "a" gnus-article-highlight
2007   "h" gnus-article-highlight-headers
2008   "c" gnus-article-highlight-citation
2009   "s" gnus-article-highlight-signature)
2010
2011 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2012   "f" gnus-article-treat-fold-headers
2013   "u" gnus-article-treat-unfold-headers
2014   "n" gnus-article-treat-fold-newsgroups)
2015
2016 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2017   "x" gnus-article-display-x-face
2018   "d" gnus-article-display-face
2019   "s" gnus-treat-smiley
2020   "D" gnus-article-remove-images
2021   "f" gnus-treat-from-picon
2022   "m" gnus-treat-mail-picon
2023   "n" gnus-treat-newsgroups-picon)
2024
2025 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2026   "w" gnus-article-decode-mime-words
2027   "c" gnus-article-decode-charset
2028   "v" gnus-mime-view-all-parts
2029   "b" gnus-article-view-part)
2030
2031 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2032   "z" gnus-article-date-ut
2033   "u" gnus-article-date-ut
2034   "l" gnus-article-date-local
2035   "p" gnus-article-date-english
2036   "e" gnus-article-date-lapsed
2037   "o" gnus-article-date-original
2038   "i" gnus-article-date-iso8601
2039   "s" gnus-article-date-user)
2040
2041 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2042   "t" gnus-article-remove-trailing-blank-lines
2043   "l" gnus-article-strip-leading-blank-lines
2044   "m" gnus-article-strip-multiple-blank-lines
2045   "a" gnus-article-strip-blank-lines
2046   "A" gnus-article-strip-all-blank-lines
2047   "s" gnus-article-strip-leading-space
2048   "e" gnus-article-strip-trailing-space
2049   "w" gnus-article-remove-leading-whitespace)
2050
2051 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2052   "v" gnus-version
2053   "f" gnus-summary-fetch-faq
2054   "d" gnus-summary-describe-group
2055   "h" gnus-summary-describe-briefly
2056   "i" gnus-info-find-node
2057   "c" gnus-group-fetch-charter
2058   "C" gnus-group-fetch-control)
2059
2060 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2061   "e" gnus-summary-expire-articles
2062   "\M-\C-e" gnus-summary-expire-articles-now
2063   "\177" gnus-summary-delete-article
2064   [delete] gnus-summary-delete-article
2065   [backspace] gnus-summary-delete-article
2066   "m" gnus-summary-move-article
2067   "r" gnus-summary-respool-article
2068   "w" gnus-summary-edit-article
2069   "c" gnus-summary-copy-article
2070   "B" gnus-summary-crosspost-article
2071   "q" gnus-summary-respool-query
2072   "t" gnus-summary-respool-trace
2073   "i" gnus-summary-import-article
2074   "I" gnus-summary-create-article
2075   "p" gnus-summary-article-posted-p)
2076
2077 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2078   "o" gnus-summary-save-article
2079   "m" gnus-summary-save-article-mail
2080   "F" gnus-summary-write-article-file
2081   "r" gnus-summary-save-article-rmail
2082   "f" gnus-summary-save-article-file
2083   "b" gnus-summary-save-article-body-file
2084   "B" gnus-summary-write-article-body-file
2085   "h" gnus-summary-save-article-folder
2086   "v" gnus-summary-save-article-vm
2087   "p" gnus-summary-pipe-output
2088   "P" gnus-summary-muttprint
2089   "s" gnus-soup-add-article)
2090
2091 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2092   "b" gnus-summary-display-buttonized
2093   "m" gnus-summary-repair-multipart
2094   "v" gnus-article-view-part
2095   "o" gnus-article-save-part
2096   "O" gnus-article-save-part-and-strip
2097   "r" gnus-article-replace-part
2098   "d" gnus-article-delete-part
2099   "t" gnus-article-view-part-as-type
2100   "j" gnus-article-jump-to-part
2101   "c" gnus-article-copy-part
2102   "C" gnus-article-view-part-as-charset
2103   "e" gnus-article-view-part-externally
2104   "H" gnus-article-browse-html-article
2105   "E" gnus-article-encrypt-body
2106   "i" gnus-article-inline-part
2107   "|" gnus-article-pipe-part)
2108
2109 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2110   "p" gnus-summary-mark-as-processable
2111   "u" gnus-summary-unmark-as-processable
2112   "U" gnus-summary-unmark-all-processable
2113   "v" gnus-uu-mark-over
2114   "s" gnus-uu-mark-series
2115   "r" gnus-uu-mark-region
2116   "g" gnus-uu-unmark-region
2117   "R" gnus-uu-mark-by-regexp
2118   "G" gnus-uu-unmark-by-regexp
2119   "t" gnus-uu-mark-thread
2120   "T" gnus-uu-unmark-thread
2121   "a" gnus-uu-mark-all
2122   "b" gnus-uu-mark-buffer
2123   "S" gnus-uu-mark-sparse
2124   "k" gnus-summary-kill-process-mark
2125   "y" gnus-summary-yank-process-mark
2126   "w" gnus-summary-save-process-mark
2127   "i" gnus-uu-invert-processable)
2128
2129 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2130   ;;"x" gnus-uu-extract-any
2131   "m" gnus-summary-save-parts
2132   "u" gnus-uu-decode-uu
2133   "U" gnus-uu-decode-uu-and-save
2134   "s" gnus-uu-decode-unshar
2135   "S" gnus-uu-decode-unshar-and-save
2136   "o" gnus-uu-decode-save
2137   "O" gnus-uu-decode-save
2138   "b" gnus-uu-decode-binhex
2139   "B" gnus-uu-decode-binhex
2140   "p" gnus-uu-decode-postscript
2141   "P" gnus-uu-decode-postscript-and-save)
2142
2143 (gnus-define-keys
2144     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2145   "u" gnus-uu-decode-uu-view
2146   "U" gnus-uu-decode-uu-and-save-view
2147   "s" gnus-uu-decode-unshar-view
2148   "S" gnus-uu-decode-unshar-and-save-view
2149   "o" gnus-uu-decode-save-view
2150   "O" gnus-uu-decode-save-view
2151   "b" gnus-uu-decode-binhex-view
2152   "B" gnus-uu-decode-binhex-view
2153   "p" gnus-uu-decode-postscript-view
2154   "P" gnus-uu-decode-postscript-and-save-view)
2155
2156 (defvar gnus-article-post-menu nil)
2157
2158 (defconst gnus-summary-menu-maxlen 20)
2159
2160 (defun gnus-summary-menu-split (menu)
2161   ;; If we have lots of elements, divide them into groups of 20
2162   ;; and make a pane (or submenu) for each one.
2163   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2164       (let ((menu menu) sublists next
2165             (i 1))
2166         (while menu
2167           ;; Pull off the next gnus-summary-menu-maxlen elements
2168           ;; and make them the next element of sublist.
2169           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2170           (if next
2171               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2172                       nil))
2173           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2174                                              (aref (car (last menu)) 0)) menu)
2175                                sublists))
2176           (setq i (1+ i))
2177           (setq menu next))
2178         (nreverse sublists))
2179     ;; Few elements--put them all in one pane.
2180     menu))
2181
2182 (defun gnus-summary-make-menu-bar ()
2183   (gnus-turn-off-edit-menu 'summary)
2184
2185   (unless (boundp 'gnus-summary-misc-menu)
2186
2187     (easy-menu-define
2188       gnus-summary-kill-menu gnus-summary-mode-map ""
2189       (cons
2190        "Score"
2191        (nconc
2192         (list
2193          ["Customize" gnus-score-customize t])
2194         (gnus-make-score-map 'increase)
2195         (gnus-make-score-map 'lower)
2196         '(("Mark"
2197            ["Kill below" gnus-summary-kill-below t]
2198            ["Mark above" gnus-summary-mark-above t]
2199            ["Tick above" gnus-summary-tick-above t]
2200            ["Clear above" gnus-summary-clear-above t])
2201           ["Current score" gnus-summary-current-score t]
2202           ["Set score" gnus-summary-set-score t]
2203           ["Switch current score file..." gnus-score-change-score-file t]
2204           ["Set mark below..." gnus-score-set-mark-below t]
2205           ["Set expunge below..." gnus-score-set-expunge-below t]
2206           ["Edit current score file" gnus-score-edit-current-scores t]
2207           ["Edit score file" gnus-score-edit-file t]
2208           ["Trace score" gnus-score-find-trace t]
2209           ["Find words" gnus-score-find-favourite-words t]
2210           ["Rescore buffer" gnus-summary-rescore t]
2211           ["Increase score..." gnus-summary-increase-score t]
2212           ["Lower score..." gnus-summary-lower-score t]))))
2213
2214     ;; Define both the Article menu in the summary buffer and the
2215     ;; equivalent Commands menu in the article buffer here for
2216     ;; consistency.
2217     (let ((innards
2218            `(("Hide"
2219               ["All" gnus-article-hide t]
2220               ["Headers" gnus-article-hide-headers t]
2221               ["Signature" gnus-article-hide-signature t]
2222               ["Citation" gnus-article-hide-citation t]
2223               ["List identifiers" gnus-article-hide-list-identifiers t]
2224               ["Banner" gnus-article-strip-banner t]
2225               ["Boring headers" gnus-article-hide-boring-headers t])
2226              ("Highlight"
2227               ["All" gnus-article-highlight t]
2228               ["Headers" gnus-article-highlight-headers t]
2229               ["Signature" gnus-article-highlight-signature t]
2230               ["Citation" gnus-article-highlight-citation t])
2231              ("MIME"
2232               ["Words" gnus-article-decode-mime-words t]
2233               ["Charset" gnus-article-decode-charset t]
2234               ["QP" gnus-article-de-quoted-unreadable t]
2235               ["Base64" gnus-article-de-base64-unreadable t]
2236               ["View MIME buttons" gnus-summary-display-buttonized t]
2237               ["View all" gnus-mime-view-all-parts t]
2238               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2239               ["Encrypt body" gnus-article-encrypt-body
2240                :active (not (gnus-group-read-only-p))
2241                ,@(if (featurep 'xemacs) nil
2242                    '(:help "Encrypt the message body on disk"))]
2243               ["Extract all parts..." gnus-summary-save-parts t]
2244               ("Multipart"
2245                ["Repair multipart" gnus-summary-repair-multipart t]
2246                ["Pipe part..." gnus-article-pipe-part t]
2247                ["Inline part" gnus-article-inline-part t]
2248                ["View part as type..." gnus-article-view-part-as-type t]
2249                ["Encrypt body" gnus-article-encrypt-body
2250                 :active (not (gnus-group-read-only-p))
2251                ,@(if (featurep 'xemacs) nil
2252                    '(:help "Encrypt the message body on disk"))]
2253                ["View part externally" gnus-article-view-part-externally t]
2254                ["View HTML parts in browser" gnus-article-browse-html-article t]
2255                ["View part with charset..." gnus-article-view-part-as-charset t]
2256                ["Copy part" gnus-article-copy-part t]
2257                ["Save part..." gnus-article-save-part t]
2258                ["View part" gnus-article-view-part t]))
2259              ("Date"
2260               ["Local" gnus-article-date-local t]
2261               ["ISO8601" gnus-article-date-iso8601 t]
2262               ["UT" gnus-article-date-ut t]
2263               ["Original" gnus-article-date-original t]
2264               ["Lapsed" gnus-article-date-lapsed t]
2265               ["User-defined" gnus-article-date-user t])
2266              ("Display"
2267               ["Remove images" gnus-article-remove-images t]
2268               ["Toggle smiley" gnus-treat-smiley t]
2269               ["Show X-Face" gnus-article-display-x-face t]
2270               ["Show picons in From" gnus-treat-from-picon t]
2271               ["Show picons in mail headers" gnus-treat-mail-picon t]
2272               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2273               ("View as different encoding"
2274                ,@(gnus-summary-menu-split
2275                   (mapcar
2276                    (lambda (cs)
2277                      ;; Since easymenu under Emacs doesn't allow
2278                      ;; lambda forms for menu commands, we should
2279                      ;; provide intern'ed function symbols.
2280                      (let ((command (intern (format "\
2281 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2282                        (fset command
2283                              `(lambda ()
2284                                 (interactive)
2285                                 (let ((gnus-summary-show-article-charset-alist
2286                                        '((1 . ,cs))))
2287                                   (gnus-summary-show-article 1))))
2288                        `[,(symbol-name cs) ,command t]))
2289                    (sort (if (fboundp 'coding-system-list)
2290                              (coding-system-list)
2291                            (mapcar 'car mm-mime-mule-charset-alist))
2292                          'string<)))))
2293              ("Washing"
2294               ("Remove Blanks"
2295                ["Leading" gnus-article-strip-leading-blank-lines t]
2296                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2297                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2298                ["All of the above" gnus-article-strip-blank-lines t]
2299                ["All" gnus-article-strip-all-blank-lines t]
2300                ["Leading space" gnus-article-strip-leading-space t]
2301                ["Trailing space" gnus-article-strip-trailing-space t]
2302                ["Leading space in headers"
2303                 gnus-article-remove-leading-whitespace t])
2304               ["Overstrike" gnus-article-treat-overstrike t]
2305               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2306               ["Emphasis" gnus-article-emphasize t]
2307               ["Word wrap" gnus-article-fill-cited-article t]
2308               ["Fill long lines" gnus-article-fill-long-lines t]
2309               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2310               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2311               ["Remove CR" gnus-article-remove-cr t]
2312               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2313               ["Base64" gnus-article-de-base64-unreadable t]
2314               ["Rot 13" gnus-summary-caesar-message
2315                ,@(if (featurep 'xemacs) '(t)
2316                    '(:help "\"Caesar rotate\" article by 13"))]
2317               ["De-IDNA" gnus-summary-idna-message t]
2318               ["Morse decode" gnus-summary-morse-message t]
2319               ["Unix pipe..." gnus-summary-pipe-message t]
2320               ["Add buttons" gnus-article-add-buttons t]
2321               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2322               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2323               ["Verbose header" gnus-summary-verbose-headers t]
2324               ["Toggle header" gnus-summary-toggle-header t]
2325               ["Unfold headers" gnus-article-treat-unfold-headers t]
2326               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2327               ["Html" gnus-article-wash-html t]
2328               ["Unsplit URLs" gnus-article-unsplit-urls t]
2329               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2330               ["Decode HZ" gnus-article-decode-HZ t]
2331               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2332               ("(Outlook) Deuglify"
2333                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2334                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2335                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2336                ["Full (Outlook) deuglify"
2337                 gnus-article-outlook-deuglify-article t])
2338               )
2339              ("Output"
2340               ["Save in default format..." gnus-summary-save-article
2341                ,@(if (featurep 'xemacs) '(t)
2342                    '(:help "Save article using default method"))]
2343               ["Save in file..." gnus-summary-save-article-file
2344                ,@(if (featurep 'xemacs) '(t)
2345                    '(:help "Save article in file"))]
2346               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2347               ["Save in MH folder..." gnus-summary-save-article-folder t]
2348               ["Save in VM folder..." gnus-summary-save-article-vm t]
2349               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2350               ["Save body in file..." gnus-summary-save-article-body-file t]
2351               ["Pipe through a filter..." gnus-summary-pipe-output t]
2352               ["Add to SOUP packet" gnus-soup-add-article t]
2353               ["Print with Muttprint..." gnus-summary-muttprint t]
2354               ["Print" gnus-summary-print-article
2355                ,@(if (featurep 'xemacs) '(t)
2356                    '(:help "Generate and print a PostScript image"))])
2357              ("Copy, move,... (Backend)"
2358               ,@(if (featurep 'xemacs) nil
2359                   '(:help "Copying, moving, expiring articles..."))
2360               ["Respool article..." gnus-summary-respool-article t]
2361               ["Move article..." gnus-summary-move-article
2362                (gnus-check-backend-function
2363                 'request-move-article gnus-newsgroup-name)]
2364               ["Copy article..." gnus-summary-copy-article t]
2365               ["Crosspost article..." gnus-summary-crosspost-article
2366                (gnus-check-backend-function
2367                 'request-replace-article gnus-newsgroup-name)]
2368               ["Import file..." gnus-summary-import-article
2369                (gnus-check-backend-function
2370                 'request-accept-article gnus-newsgroup-name)]
2371               ["Create article..." gnus-summary-create-article
2372                (gnus-check-backend-function
2373                 'request-accept-article gnus-newsgroup-name)]
2374               ["Check if posted" gnus-summary-article-posted-p t]
2375               ["Edit article" gnus-summary-edit-article
2376                (not (gnus-group-read-only-p))]
2377               ["Delete article" gnus-summary-delete-article
2378                (gnus-check-backend-function
2379                 'request-expire-articles gnus-newsgroup-name)]
2380               ["Query respool" gnus-summary-respool-query t]
2381               ["Trace respool" gnus-summary-respool-trace t]
2382               ["Delete expirable articles" gnus-summary-expire-articles-now
2383                (gnus-check-backend-function
2384                 'request-expire-articles gnus-newsgroup-name)])
2385              ("Extract"
2386               ["Uudecode" gnus-uu-decode-uu
2387                ,@(if (featurep 'xemacs) '(t)
2388                    '(:help "Decode uuencoded article(s)"))]
2389               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2390               ["Unshar" gnus-uu-decode-unshar t]
2391               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2392               ["Save" gnus-uu-decode-save t]
2393               ["Binhex" gnus-uu-decode-binhex t]
2394               ["Postscript" gnus-uu-decode-postscript t]
2395               ["All MIME parts" gnus-summary-save-parts t])
2396              ("Cache"
2397               ["Enter article" gnus-cache-enter-article t]
2398               ["Remove article" gnus-cache-remove-article t])
2399              ["Translate" gnus-article-babel t]
2400              ["Select article buffer" gnus-summary-select-article-buffer t]
2401              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2402              ["Isearch article..." gnus-summary-isearch-article t]
2403              ["Beginning of the article" gnus-summary-beginning-of-article t]
2404              ["End of the article" gnus-summary-end-of-article t]
2405              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2406              ["Fetch referenced articles" gnus-summary-refer-references t]
2407              ["Fetch current thread" gnus-summary-refer-thread t]
2408              ["Fetch article with id..." gnus-summary-refer-article t]
2409              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2410              ["Redisplay" gnus-summary-show-article t]
2411              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2412       (easy-menu-define
2413         gnus-summary-article-menu gnus-summary-mode-map ""
2414         (cons "Article" innards))
2415
2416       (if (not (keymapp gnus-summary-article-menu))
2417           (easy-menu-define
2418             gnus-article-commands-menu gnus-article-mode-map ""
2419             (cons "Commands" innards))
2420         ;; in Emacs, don't share menu.
2421         (setq gnus-article-commands-menu
2422               (copy-keymap gnus-summary-article-menu))
2423         (define-key gnus-article-mode-map [menu-bar commands]
2424           (cons "Commands" gnus-article-commands-menu))))
2425
2426     (easy-menu-define
2427       gnus-summary-thread-menu gnus-summary-mode-map ""
2428       '("Threads"
2429         ["Find all messages in thread" gnus-summary-refer-thread t]
2430         ["Toggle threading" gnus-summary-toggle-threads t]
2431         ["Hide threads" gnus-summary-hide-all-threads t]
2432         ["Show threads" gnus-summary-show-all-threads t]
2433         ["Hide thread" gnus-summary-hide-thread t]
2434         ["Show thread" gnus-summary-show-thread t]
2435         ["Go to next thread" gnus-summary-next-thread t]
2436         ["Go to previous thread" gnus-summary-prev-thread t]
2437         ["Go down thread" gnus-summary-down-thread t]
2438         ["Go up thread" gnus-summary-up-thread t]
2439         ["Top of thread" gnus-summary-top-thread t]
2440         ["Mark thread as read" gnus-summary-kill-thread t]
2441         ["Lower thread score" gnus-summary-lower-thread t]
2442         ["Raise thread score" gnus-summary-raise-thread t]
2443         ["Rethread current" gnus-summary-rethread-current t]))
2444
2445     (easy-menu-define
2446       gnus-summary-post-menu gnus-summary-mode-map ""
2447       `("Post"
2448         ["Send a message (mail or news)" gnus-summary-post-news
2449          ,@(if (featurep 'xemacs) '(t)
2450              '(:help "Compose a new message (mail or news)"))]
2451         ["Followup" gnus-summary-followup
2452          ,@(if (featurep 'xemacs) '(t)
2453              '(:help "Post followup to this article"))]
2454         ["Followup and yank" gnus-summary-followup-with-original
2455          ,@(if (featurep 'xemacs) '(t)
2456              '(:help "Post followup to this article, quoting its contents"))]
2457         ["Supersede article" gnus-summary-supersede-article t]
2458         ["Cancel article" gnus-summary-cancel-article
2459          ,@(if (featurep 'xemacs) '(t)
2460              '(:help "Cancel an article you posted"))]
2461         ["Reply" gnus-summary-reply t]
2462         ["Reply and yank" gnus-summary-reply-with-original t]
2463         ["Wide reply" gnus-summary-wide-reply t]
2464         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2465          ,@(if (featurep 'xemacs) '(t)
2466              '(:help "Mail a reply, quoting this article"))]
2467         ["Very wide reply" gnus-summary-very-wide-reply t]
2468         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2469          ,@(if (featurep 'xemacs) '(t)
2470              '(:help "Mail a very wide reply, quoting this article"))]
2471         ["Mail forward" gnus-summary-mail-forward t]
2472         ["Post forward" gnus-summary-post-forward t]
2473         ["Digest and mail" gnus-uu-digest-mail-forward t]
2474         ["Digest and post" gnus-uu-digest-post-forward t]
2475         ["Resend message" gnus-summary-resend-message t]
2476         ["Resend message edit" gnus-summary-resend-message-edit t]
2477         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2478         ["Send a mail" gnus-summary-mail-other-window t]
2479         ["Create a local message" gnus-summary-news-other-window t]
2480         ["Uuencode and post" gnus-uu-post-news
2481          ,@(if (featurep 'xemacs) '(t)
2482              '(:help "Post a uuencoded article"))]
2483         ["Followup via news" gnus-summary-followup-to-mail t]
2484         ["Followup via news and yank"
2485          gnus-summary-followup-to-mail-with-original t]
2486         ;;("Draft"
2487         ;;["Send" gnus-summary-send-draft t]
2488         ;;["Send bounced" gnus-resend-bounced-mail t])
2489         ))
2490
2491     (cond
2492      ((not (keymapp gnus-summary-post-menu))
2493       (setq gnus-article-post-menu gnus-summary-post-menu))
2494      ((not gnus-article-post-menu)
2495       ;; Don't share post menu.
2496       (setq gnus-article-post-menu
2497             (copy-keymap gnus-summary-post-menu))))
2498     (define-key gnus-article-mode-map [menu-bar post]
2499       (cons "Post" gnus-article-post-menu))
2500
2501     (easy-menu-define
2502       gnus-summary-misc-menu gnus-summary-mode-map ""
2503       `("Gnus"
2504         ("Mark Read"
2505          ["Mark as read" gnus-summary-mark-as-read-forward t]
2506          ["Mark same subject and select"
2507           gnus-summary-kill-same-subject-and-select t]
2508          ["Mark same subject" gnus-summary-kill-same-subject t]
2509          ["Catchup" gnus-summary-catchup
2510           ,@(if (featurep 'xemacs) '(t)
2511               '(:help "Mark unread articles in this group as read"))]
2512          ["Catchup all" gnus-summary-catchup-all t]
2513          ["Catchup to here" gnus-summary-catchup-to-here t]
2514          ["Catchup from here" gnus-summary-catchup-from-here t]
2515          ["Catchup region" gnus-summary-mark-region-as-read
2516           (gnus-mark-active-p)]
2517          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2518         ("Mark Various"
2519          ["Tick" gnus-summary-tick-article-forward t]
2520          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2521          ["Remove marks" gnus-summary-clear-mark-forward t]
2522          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2523          ["Set bookmark" gnus-summary-set-bookmark t]
2524          ["Remove bookmark" gnus-summary-remove-bookmark t])
2525         ("Limit to"
2526          ["Marks..." gnus-summary-limit-to-marks t]
2527          ["Subject..." gnus-summary-limit-to-subject t]
2528          ["Author..." gnus-summary-limit-to-author t]
2529          ["Recipient..." gnus-summary-limit-to-recipient t]
2530          ["Age..." gnus-summary-limit-to-age t]
2531          ["Extra..." gnus-summary-limit-to-extra t]
2532          ["Score..." gnus-summary-limit-to-score t]
2533          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2534          ["Unread" gnus-summary-limit-to-unread t]
2535          ["Unseen" gnus-summary-limit-to-unseen t]
2536          ["Replied" gnus-summary-limit-to-replied t]
2537          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2538          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2539          ["Pop limit" gnus-summary-pop-limit t]
2540          ["Show dormant" gnus-summary-limit-include-dormant t]
2541          ["Hide childless dormant"
2542           gnus-summary-limit-exclude-childless-dormant t]
2543          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2544          ["Hide marked" gnus-summary-limit-exclude-marks t]
2545          ["Show expunged" gnus-summary-limit-include-expunged t])
2546         ("Process Mark"
2547          ["Set mark" gnus-summary-mark-as-processable t]
2548          ["Remove mark" gnus-summary-unmark-as-processable t]
2549          ["Remove all marks" gnus-summary-unmark-all-processable t]
2550          ["Invert marks" gnus-uu-invert-processable t]
2551          ["Mark above" gnus-uu-mark-over t]
2552          ["Mark series" gnus-uu-mark-series t]
2553          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2554          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2555          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2556          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2557          ["Mark all" gnus-uu-mark-all t]
2558          ["Mark buffer" gnus-uu-mark-buffer t]
2559          ["Mark sparse" gnus-uu-mark-sparse t]
2560          ["Mark thread" gnus-uu-mark-thread t]
2561          ["Unmark thread" gnus-uu-unmark-thread t]
2562          ("Process Mark Sets"
2563           ["Kill" gnus-summary-kill-process-mark t]
2564           ["Yank" gnus-summary-yank-process-mark
2565            gnus-newsgroup-process-stack]
2566           ["Save" gnus-summary-save-process-mark t]
2567           ["Run command on marked..." gnus-summary-universal-argument t]))
2568         ("Scroll article"
2569          ["Page forward" gnus-summary-next-page
2570           ,@(if (featurep 'xemacs) '(t)
2571               '(:help "Show next page of article"))]
2572          ["Page backward" gnus-summary-prev-page
2573           ,@(if (featurep 'xemacs) '(t)
2574               '(:help "Show previous page of article"))]
2575          ["Line forward" gnus-summary-scroll-up t])
2576         ("Move"
2577          ["Next unread article" gnus-summary-next-unread-article t]
2578          ["Previous unread article" gnus-summary-prev-unread-article t]
2579          ["Next article" gnus-summary-next-article t]
2580          ["Previous article" gnus-summary-prev-article t]
2581          ["Next unread subject" gnus-summary-next-unread-subject t]
2582          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2583          ["Next article same subject" gnus-summary-next-same-subject t]
2584          ["Previous article same subject" gnus-summary-prev-same-subject t]
2585          ["First unread article" gnus-summary-first-unread-article t]
2586          ["Best unread article" gnus-summary-best-unread-article t]
2587          ["Go to subject number..." gnus-summary-goto-subject t]
2588          ["Go to article number..." gnus-summary-goto-article t]
2589          ["Go to the last article" gnus-summary-goto-last-article t]
2590          ["Pop article off history" gnus-summary-pop-article t])
2591         ("Sort"
2592          ["Sort by number" gnus-summary-sort-by-number t]
2593          ["Sort by author" gnus-summary-sort-by-author t]
2594          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2595          ["Sort by subject" gnus-summary-sort-by-subject t]
2596          ["Sort by date" gnus-summary-sort-by-date t]
2597          ["Sort by score" gnus-summary-sort-by-score t]
2598          ["Sort by lines" gnus-summary-sort-by-lines t]
2599          ["Sort by characters" gnus-summary-sort-by-chars t]
2600          ["Randomize" gnus-summary-sort-by-random t]
2601          ["Original sort" gnus-summary-sort-by-original t])
2602         ("Help"
2603          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2604          ["Describe group" gnus-summary-describe-group t]
2605          ["Fetch charter" gnus-group-fetch-charter
2606           ,@(if (featurep 'xemacs) nil
2607               '(:help "Display the charter of the current group"))]
2608          ["Fetch control message" gnus-group-fetch-control
2609           ,@(if (featurep 'xemacs) nil
2610               '(:help "Display the archived control message for the current group"))]
2611          ["Read manual" gnus-info-find-node t])
2612         ("Modes"
2613          ["Pick and read" gnus-pick-mode t]
2614          ["Binary" gnus-binary-mode t])
2615         ("Regeneration"
2616          ["Regenerate" gnus-summary-prepare t]
2617          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2618          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2619          ["Toggle threading" gnus-summary-toggle-threads t])
2620         ["See old articles" gnus-summary-insert-old-articles t]
2621         ["See new articles" gnus-summary-insert-new-articles t]
2622         ["Filter articles..." gnus-summary-execute-command t]
2623         ["Run command on articles..." gnus-summary-universal-argument t]
2624         ["Search articles forward..." gnus-summary-search-article-forward t]
2625         ["Search articles backward..." gnus-summary-search-article-backward t]
2626         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2627         ["Expand window" gnus-summary-expand-window t]
2628         ["Expire expirable articles" gnus-summary-expire-articles
2629          (gnus-check-backend-function
2630           'request-expire-articles gnus-newsgroup-name)]
2631         ["Edit local kill file" gnus-summary-edit-local-kill t]
2632         ["Edit main kill file" gnus-summary-edit-global-kill t]
2633         ["Edit group parameters" gnus-summary-edit-parameters t]
2634         ["Customize group parameters" gnus-summary-customize-parameters t]
2635         ["Send a bug report" gnus-bug t]
2636         ("Exit"
2637          ["Catchup and exit" gnus-summary-catchup-and-exit
2638           ,@(if (featurep 'xemacs) '(t)
2639               '(:help "Mark unread articles in this group as read, then exit"))]
2640          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2641          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2642          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2643          ["Exit group" gnus-summary-exit
2644           ,@(if (featurep 'xemacs) '(t)
2645               '(:help "Exit current group, return to group selection mode"))]
2646          ["Exit group without updating" gnus-summary-exit-no-update t]
2647          ["Exit and goto next group" gnus-summary-next-group t]
2648          ["Exit and goto prev group" gnus-summary-prev-group t]
2649          ["Reselect group" gnus-summary-reselect-current-group t]
2650          ["Rescan group" gnus-summary-rescan-group t]
2651          ["Update dribble" gnus-summary-save-newsrc t])))
2652
2653     (gnus-run-hooks 'gnus-summary-menu-hook)))
2654
2655 (defvar gnus-summary-tool-bar-map nil)
2656
2657 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2658 ;; affect _new_ message buffers.  We might add a function that walks thru all
2659 ;; summary-mode buffers and force the update.
2660 (defun gnus-summary-tool-bar-update (&optional symbol value)
2661   "Update summary mode toolbar.
2662 Setter function for custom variables."
2663   (setq-default gnus-summary-tool-bar-map nil)
2664   (when symbol
2665     ;; When used as ":set" function:
2666     (set-default symbol value))
2667   (when (gnus-buffer-live-p gnus-summary-buffer)
2668     (with-current-buffer gnus-summary-buffer
2669       (gnus-summary-make-tool-bar))))
2670
2671 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2672                                      'gnus-summary-tool-bar-gnome
2673                                    'gnus-summary-tool-bar-retro)
2674   "Specifies the Gnus summary tool bar.
2675
2676 It can be either a list or a symbol refering to a list.  See
2677 `gmm-tool-bar-from-list' for the format of the list.  The
2678 default key map is `gnus-summary-mode-map'.
2679
2680 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2681 `gnus-summary-tool-bar-retro'."
2682   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2683                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2684                  (repeat :tag "User defined list" gmm-tool-bar-item)
2685                  (symbol))
2686   :version "23.0" ;; No Gnus
2687   :initialize 'custom-initialize-default
2688   :set 'gnus-summary-tool-bar-update
2689   :group 'gnus-summary)
2690
2691 (defcustom gnus-summary-tool-bar-gnome
2692   '((gnus-summary-post-news "mail/compose" nil)
2693     (gnus-summary-insert-new-articles "mail/inbox" nil
2694                                       :visible (or (not gnus-agent)
2695                                                    gnus-plugged))
2696     (gnus-summary-reply-with-original "mail/reply")
2697     (gnus-summary-reply "mail/reply" nil :visible nil)
2698     (gnus-summary-followup-with-original "mail/reply-all")
2699     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2700     (gnus-summary-mail-forward "mail/forward")
2701     (gnus-summary-save-article "mail/save")
2702     (gnus-summary-search-article-forward "search" nil :visible nil)
2703     (gnus-summary-print-article "print")
2704     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2705     ;; Some new commands that may need more suitable icons:
2706     (gnus-summary-save-newsrc "save" nil :visible nil)
2707     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2708     (gnus-summary-prev-article "left-arrow")
2709     (gnus-summary-next-article "right-arrow")
2710     (gnus-summary-next-page "next-page")
2711     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2712     ;;
2713     ;; Maybe some sort-by-... could be added:
2714     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2715     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2716     (gnus-summary-mark-as-expirable
2717      "delete" nil
2718      :visible (gnus-check-backend-function 'request-expire-articles
2719                                            gnus-newsgroup-name))
2720     (gnus-summary-mark-as-spam
2721      "mail/spam" t
2722      :visible (and (fboundp 'spam-group-ham-contents-p)
2723                    (spam-group-ham-contents-p gnus-newsgroup-name))
2724      :help "Mark as spam")
2725     (gnus-summary-mark-as-read-forward
2726      "mail/not-spam" nil
2727      :visible (and (fboundp 'spam-group-spam-contents-p)
2728                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2729     ;;
2730     (gnus-summary-exit "exit")
2731     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2732     (gnus-info-find-node "help"))
2733   "List of functions for the summary tool bar (GNOME style).
2734
2735 See `gmm-tool-bar-from-list' for the format of the list."
2736   :type '(repeat gmm-tool-bar-item)
2737   :version "23.0" ;; No Gnus
2738   :initialize 'custom-initialize-default
2739   :set 'gnus-summary-tool-bar-update
2740   :group 'gnus-summary)
2741
2742 (defcustom gnus-summary-tool-bar-retro
2743   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2744     (gnus-summary-next-unread-article "gnus/next-ur")
2745     (gnus-summary-post-news "gnus/post")
2746     (gnus-summary-followup-with-original "gnus/fuwo")
2747     (gnus-summary-followup "gnus/followup")
2748     (gnus-summary-reply-with-original "gnus/reply-wo")
2749     (gnus-summary-reply "gnus/reply")
2750     (gnus-summary-caesar-message "gnus/rot13")
2751     (gnus-uu-decode-uu "gnus/uu-decode")
2752     (gnus-summary-save-article-file "gnus/save-aif")
2753     (gnus-summary-save-article "gnus/save-art")
2754     (gnus-uu-post-news "gnus/uu-post")
2755     (gnus-summary-catchup "gnus/catchup")
2756     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2757     (gnus-summary-exit "gnus/exit-summ")
2758     ;; Some new command that may need more suitable icons:
2759     (gnus-summary-print-article "gnus/print" nil :visible nil)
2760     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2761     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2762     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2763     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2764     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2765     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2766     ;;
2767     (gnus-info-find-node "gnus/help" nil :visible nil))
2768   "List of functions for the summary tool bar (retro look).
2769
2770 See `gmm-tool-bar-from-list' for the format of the list."
2771   :type '(repeat gmm-tool-bar-item)
2772   :version "23.0" ;; No Gnus
2773   :initialize 'custom-initialize-default
2774   :set 'gnus-summary-tool-bar-update
2775   :group 'gnus-summary)
2776
2777 (defcustom gnus-summary-tool-bar-zap-list t
2778   "List of icon items from the global tool bar.
2779 These items are not displayed in the Gnus summary mode tool bar.
2780
2781 See `gmm-tool-bar-from-list' for the format of the list."
2782   :type 'gmm-tool-bar-zap-list
2783   :version "23.0" ;; No Gnus
2784   :initialize 'custom-initialize-default
2785   :set 'gnus-summary-tool-bar-update
2786   :group 'gnus-summary)
2787
2788 (defvar image-load-path)
2789
2790 (defun gnus-summary-make-tool-bar (&optional force)
2791   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2792 When FORCE, rebuild the tool bar."
2793   (when (and (not (featurep 'xemacs))
2794              (boundp 'tool-bar-mode)
2795              tool-bar-mode
2796              (or (not gnus-summary-tool-bar-map) force))
2797     (let* ((load-path
2798             (gmm-image-load-path-for-library "gnus"
2799                                              "mail/save.xpm"
2800                                              nil t))
2801            (image-load-path (cons (car load-path)
2802                                   (when (boundp 'image-load-path)
2803                                     image-load-path)))
2804            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2805                                         gnus-summary-tool-bar-zap-list
2806                                         'gnus-summary-mode-map)))
2807       (when map
2808         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2809         ;; uses it's value.
2810         (setq gnus-summary-tool-bar-map map))))
2811   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2812
2813 (defun gnus-score-set-default (var value)
2814   "A version of set that updates the GNU Emacs menu-bar."
2815   (set var value)
2816   ;; It is the message that forces the active status to be updated.
2817   (message ""))
2818
2819 (defun gnus-make-score-map (type)
2820   "Make a summary score map of type TYPE."
2821   (if t
2822       nil
2823     (let ((headers '(("author" "from" string)
2824                      ("subject" "subject" string)
2825                      ("article body" "body" string)
2826                      ("article head" "head" string)
2827                      ("xref" "xref" string)
2828                      ("extra header" "extra" string)
2829                      ("lines" "lines" number)
2830                      ("followups to author" "followup" string)))
2831           (types '((number ("less than" <)
2832                            ("greater than" >)
2833                            ("equal" =))
2834                    (string ("substring" s)
2835                            ("exact string" e)
2836                            ("fuzzy string" f)
2837                            ("regexp" r))))
2838           (perms '(("temporary" (current-time-string))
2839                    ("permanent" nil)
2840                    ("immediate" now)))
2841           header)
2842       (list
2843        (apply
2844         'nconc
2845         (list
2846          (if (eq type 'lower)
2847              "Lower score"
2848            "Increase score"))
2849         (let (outh)
2850           (while headers
2851             (setq header (car headers))
2852             (setq outh
2853                   (cons
2854                    (apply
2855                     'nconc
2856                     (list (car header))
2857                     (let ((ts (cdr (assoc (nth 2 header) types)))
2858                           outt)
2859                       (while ts
2860                         (setq outt
2861                               (cons
2862                                (apply
2863                                 'nconc
2864                                 (list (caar ts))
2865                                 (let ((ps perms)
2866                                       outp)
2867                                   (while ps
2868                                     (setq outp
2869                                           (cons
2870                                            (vector
2871                                             (caar ps)
2872                                             (list
2873                                              'gnus-summary-score-entry
2874                                              (nth 1 header)
2875                                              (if (or (string= (nth 1 header)
2876                                                               "head")
2877                                                      (string= (nth 1 header)
2878                                                               "body"))
2879                                                  ""
2880                                                (list 'gnus-summary-header
2881                                                      (nth 1 header)))
2882                                              (list 'quote (nth 1 (car ts)))
2883                                              (list 'gnus-score-delta-default
2884                                                    nil)
2885                                              (nth 1 (car ps))
2886                                              t)
2887                                             t)
2888                                            outp))
2889                                     (setq ps (cdr ps)))
2890                                   (list (nreverse outp))))
2891                                outt))
2892                         (setq ts (cdr ts)))
2893                       (list (nreverse outt))))
2894                    outh))
2895             (setq headers (cdr headers)))
2896           (list (nreverse outh))))))))
2897
2898 \f
2899
2900 (defun gnus-summary-mode (&optional group)
2901   "Major mode for reading articles.
2902
2903 All normal editing commands are switched off.
2904 \\<gnus-summary-mode-map>
2905 Each line in this buffer represents one article.  To read an
2906 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2907 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2908 respectively.
2909
2910 You can also post articles and send mail from this buffer.  To
2911 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2912 of an article, type `\\[gnus-summary-reply]'.
2913
2914 There are approx. one gazillion commands you can execute in this
2915 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2916
2917 The following commands are available:
2918
2919 \\{gnus-summary-mode-map}"
2920   (interactive)
2921   (kill-all-local-variables)
2922   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2923     (gnus-summary-make-local-variables))
2924   (gnus-summary-make-local-variables)
2925   (setq gnus-newsgroup-name group)
2926   (when (gnus-visual-p 'summary-menu 'menu)
2927     (gnus-summary-make-menu-bar)
2928     (gnus-summary-make-tool-bar))
2929   (gnus-make-thread-indent-array)
2930   (gnus-simplify-mode-line)
2931   (setq major-mode 'gnus-summary-mode)
2932   (setq mode-name "Summary")
2933   (make-local-variable 'minor-mode-alist)
2934   (use-local-map gnus-summary-mode-map)
2935   (buffer-disable-undo)
2936   (setq buffer-read-only t              ;Disable modification
2937         show-trailing-whitespace nil)
2938   (setq truncate-lines t)
2939   (setq selective-display t)
2940   (setq selective-display-ellipses t)   ;Display `...'
2941   (gnus-summary-set-display-table)
2942   (gnus-set-default-directory)
2943   (make-local-variable 'gnus-summary-line-format)
2944   (make-local-variable 'gnus-summary-line-format-spec)
2945   (make-local-variable 'gnus-summary-dummy-line-format)
2946   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2947   (make-local-variable 'gnus-summary-mark-positions)
2948   (gnus-make-local-hook 'pre-command-hook)
2949   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2950   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2951   (turn-on-gnus-mailing-list-mode)
2952   (mm-enable-multibyte)
2953   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2954   (gnus-update-summary-mark-positions))
2955
2956 (defun gnus-summary-make-local-variables ()
2957   "Make all the local summary buffer variables."
2958   (let (global)
2959     (dolist (local gnus-summary-local-variables)
2960       (if (consp local)
2961           (progn
2962             (if (eq (cdr local) 'global)
2963                 ;; Copy the global value of the variable.
2964                 (setq global (symbol-value (car local)))
2965               ;; Use the value from the list.
2966               (setq global (eval (cdr local))))
2967             (set (make-local-variable (car local)) global))
2968         ;; Simple nil-valued local variable.
2969         (set (make-local-variable local) nil)))))
2970
2971 (defun gnus-summary-clear-local-variables ()
2972   (let ((locals gnus-summary-local-variables))
2973     (while locals
2974       (if (consp (car locals))
2975           (and (symbolp (caar locals))
2976                (set (caar locals) nil))
2977         (and (symbolp (car locals))
2978              (set (car locals) nil)))
2979       (setq locals (cdr locals)))))
2980
2981 ;; Summary data functions.
2982
2983 (defmacro gnus-data-number (data)
2984   `(car ,data))
2985
2986 (defmacro gnus-data-set-number (data number)
2987   `(setcar ,data ,number))
2988
2989 (defmacro gnus-data-mark (data)
2990   `(nth 1 ,data))
2991
2992 (defmacro gnus-data-set-mark (data mark)
2993   `(setcar (nthcdr 1 ,data) ,mark))
2994
2995 (defmacro gnus-data-pos (data)
2996   `(nth 2 ,data))
2997
2998 (defmacro gnus-data-set-pos (data pos)
2999   `(setcar (nthcdr 2 ,data) ,pos))
3000
3001 (defmacro gnus-data-header (data)
3002   `(nth 3 ,data))
3003
3004 (defmacro gnus-data-set-header (data header)
3005   `(setf (nth 3 ,data) ,header))
3006
3007 (defmacro gnus-data-level (data)
3008   `(nth 4 ,data))
3009
3010 (defmacro gnus-data-unread-p (data)
3011   `(= (nth 1 ,data) gnus-unread-mark))
3012
3013 (defmacro gnus-data-read-p (data)
3014   `(/= (nth 1 ,data) gnus-unread-mark))
3015
3016 (defmacro gnus-data-pseudo-p (data)
3017   `(consp (nth 3 ,data)))
3018
3019 (defmacro gnus-data-find (number)
3020   `(assq ,number gnus-newsgroup-data))
3021
3022 (defmacro gnus-data-find-list (number &optional data)
3023   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3024      (memq (assq ,number bdata)
3025            bdata)))
3026
3027 (defmacro gnus-data-make (number mark pos header level)
3028   `(list ,number ,mark ,pos ,header ,level))
3029
3030 (defun gnus-data-enter (after-article number mark pos header level offset)
3031   (let ((data (gnus-data-find-list after-article)))
3032     (unless data
3033       (error "No such article: %d" after-article))
3034     (setcdr data (cons (gnus-data-make number mark pos header level)
3035                        (cdr data)))
3036     (setq gnus-newsgroup-data-reverse nil)
3037     (gnus-data-update-list (cddr data) offset)))
3038
3039 (defun gnus-data-enter-list (after-article list &optional offset)
3040   (when list
3041     (let ((data (and after-article (gnus-data-find-list after-article)))
3042           (ilist list))
3043       (if (not (or data
3044                    after-article))
3045           (let ((odata gnus-newsgroup-data))
3046             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3047             (when offset
3048               (gnus-data-update-list odata offset)))
3049         ;; Find the last element in the list to be spliced into the main
3050         ;; list.
3051         (setq list (last list))
3052         (if (not data)
3053             (progn
3054               (setcdr list gnus-newsgroup-data)
3055               (setq gnus-newsgroup-data ilist)
3056               (when offset
3057                 (gnus-data-update-list (cdr list) offset)))
3058           (setcdr list (cdr data))
3059           (setcdr data ilist)
3060           (when offset
3061             (gnus-data-update-list (cdr list) offset))))
3062       (setq gnus-newsgroup-data-reverse nil))))
3063
3064 (defun gnus-data-remove (article &optional offset)
3065   (let ((data gnus-newsgroup-data))
3066     (if (= (gnus-data-number (car data)) article)
3067         (progn
3068           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3069                 gnus-newsgroup-data-reverse nil)
3070           (when offset
3071             (gnus-data-update-list gnus-newsgroup-data offset)))
3072       (while (cdr data)
3073         (when (= (gnus-data-number (cadr data)) article)
3074           (setcdr data (cddr data))
3075           (when offset
3076             (gnus-data-update-list (cdr data) offset))
3077           (setq data nil
3078                 gnus-newsgroup-data-reverse nil))
3079         (setq data (cdr data))))))
3080
3081 (defmacro gnus-data-list (backward)
3082   `(if ,backward
3083        (or gnus-newsgroup-data-reverse
3084            (setq gnus-newsgroup-data-reverse
3085                  (reverse gnus-newsgroup-data)))
3086      gnus-newsgroup-data))
3087
3088 (defun gnus-data-update-list (data offset)
3089   "Add OFFSET to the POS of all data entries in DATA."
3090   (setq gnus-newsgroup-data-reverse nil)
3091   (while data
3092     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3093     (setq data (cdr data))))
3094
3095 (defun gnus-summary-article-pseudo-p (article)
3096   "Say whether this article is a pseudo article or not."
3097   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3098
3099 (defmacro gnus-summary-article-sparse-p (article)
3100   "Say whether this article is a sparse article or not."
3101   `(memq ,article gnus-newsgroup-sparse))
3102
3103 (defmacro gnus-summary-article-ancient-p (article)
3104   "Say whether this article is a sparse article or not."
3105   `(memq ,article gnus-newsgroup-ancient))
3106
3107 (defun gnus-article-parent-p (number)
3108   "Say whether this article is a parent or not."
3109   (let ((data (gnus-data-find-list number)))
3110     (and (cdr data)              ; There has to be an article after...
3111          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3112             (gnus-data-level (nth 1 data))))))
3113
3114 (defun gnus-article-children (number)
3115   "Return a list of all children to NUMBER."
3116   (let* ((data (gnus-data-find-list number))
3117          (level (gnus-data-level (car data)))
3118          children)
3119     (setq data (cdr data))
3120     (while (and data
3121                 (= (gnus-data-level (car data)) (1+ level)))
3122       (push (gnus-data-number (car data)) children)
3123       (setq data (cdr data)))
3124     children))
3125
3126 (defmacro gnus-summary-skip-intangible ()
3127   "If the current article is intangible, then jump to a different article."
3128   '(let ((to (get-text-property (point) 'gnus-intangible)))
3129      (and to (gnus-summary-goto-subject to))))
3130
3131 (defmacro gnus-summary-article-intangible-p ()
3132   "Say whether this article is intangible or not."
3133   '(get-text-property (point) 'gnus-intangible))
3134
3135 (defun gnus-article-read-p (article)
3136   "Say whether ARTICLE is read or not."
3137   (not (or (memq article gnus-newsgroup-marked)
3138            (memq article gnus-newsgroup-spam-marked)
3139            (memq article gnus-newsgroup-unreads)
3140            (memq article gnus-newsgroup-unselected)
3141            (memq article gnus-newsgroup-dormant))))
3142
3143 ;; Some summary mode macros.
3144
3145 (defmacro gnus-summary-article-number ()
3146   "The article number of the article on the current line.
3147 If there isn't an article number here, then we return the current
3148 article number."
3149   '(progn
3150      (gnus-summary-skip-intangible)
3151      (or (get-text-property (point) 'gnus-number)
3152          (gnus-summary-last-subject))))
3153
3154 (defmacro gnus-summary-article-header (&optional number)
3155   "Return the header of article NUMBER."
3156   `(gnus-data-header (gnus-data-find
3157                       ,(or number '(gnus-summary-article-number)))))
3158
3159 (defmacro gnus-summary-thread-level (&optional number)
3160   "Return the level of thread that starts with article NUMBER."
3161   `(if (and (eq gnus-summary-make-false-root 'dummy)
3162             (get-text-property (point) 'gnus-intangible))
3163        0
3164      (gnus-data-level (gnus-data-find
3165                        ,(or number '(gnus-summary-article-number))))))
3166
3167 (defmacro gnus-summary-article-mark (&optional number)
3168   "Return the mark of article NUMBER."
3169   `(gnus-data-mark (gnus-data-find
3170                     ,(or number '(gnus-summary-article-number)))))
3171
3172 (defmacro gnus-summary-article-pos (&optional number)
3173   "Return the position of the line of article NUMBER."
3174   `(gnus-data-pos (gnus-data-find
3175                    ,(or number '(gnus-summary-article-number)))))
3176
3177 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3178 (defmacro gnus-summary-article-subject (&optional number)
3179   "Return current subject string or nil if nothing."
3180   `(let ((headers
3181           ,(if number
3182                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3183              '(gnus-data-header (assq (gnus-summary-article-number)
3184                                       gnus-newsgroup-data)))))
3185      (and headers
3186           (vectorp headers)
3187           (mail-header-subject headers))))
3188
3189 (defmacro gnus-summary-article-score (&optional number)
3190   "Return current article score."
3191   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3192                   gnus-newsgroup-scored))
3193        gnus-summary-default-score 0))
3194
3195 (defun gnus-summary-article-children (&optional number)
3196   "Return a list of article numbers that are children of article NUMBER."
3197   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3198          (level (gnus-data-level (car data)))
3199          l children)
3200     (while (and (setq data (cdr data))
3201                 (> (setq l (gnus-data-level (car data))) level))
3202       (and (= (1+ level) l)
3203            (push (gnus-data-number (car data))
3204                  children)))
3205     (nreverse children)))
3206
3207 (defun gnus-summary-article-parent (&optional number)
3208   "Return the article number of the parent of article NUMBER."
3209   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3210                                     (gnus-data-list t)))
3211          (level (gnus-data-level (car data))))
3212     (if (zerop level)
3213         ()                              ; This is a root.
3214       ;; We search until we find an article with a level less than
3215       ;; this one.  That function has to be the parent.
3216       (while (and (setq data (cdr data))
3217                   (not (< (gnus-data-level (car data)) level))))
3218       (and data (gnus-data-number (car data))))))
3219
3220 (defun gnus-unread-mark-p (mark)
3221   "Say whether MARK is the unread mark."
3222   (= mark gnus-unread-mark))
3223
3224 (defun gnus-read-mark-p (mark)
3225   "Say whether MARK is one of the marks that mark as read.
3226 This is all marks except unread, ticked, dormant, and expirable."
3227   (not (or (= mark gnus-unread-mark)
3228            (= mark gnus-ticked-mark)
3229            (= mark gnus-spam-mark)
3230            (= mark gnus-dormant-mark)
3231            (= mark gnus-expirable-mark))))
3232
3233 (defmacro gnus-article-mark (number)
3234   "Return the MARK of article NUMBER.
3235 This macro should only be used when computing the mark the \"first\"
3236 time; i.e., when generating the summary lines.  After that,
3237 `gnus-summary-article-mark' should be used to examine the
3238 marks of articles."
3239   `(cond
3240     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3241     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3242     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3243     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3244     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3245     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3246     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3247     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3248            gnus-ancient-mark))))
3249
3250 ;; Saving hidden threads.
3251
3252 (defmacro gnus-save-hidden-threads (&rest forms)
3253   "Save hidden threads, eval FORMS, and restore the hidden threads."
3254   (let ((config (make-symbol "config")))
3255     `(let ((,config (gnus-hidden-threads-configuration)))
3256        (unwind-protect
3257            (save-excursion
3258              ,@forms)
3259          (gnus-restore-hidden-threads-configuration ,config)))))
3260 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3261 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3262
3263 (defun gnus-data-compute-positions ()
3264   "Compute the positions of all articles."
3265   (setq gnus-newsgroup-data-reverse nil)
3266   (let ((data gnus-newsgroup-data))
3267     (save-excursion
3268       (gnus-save-hidden-threads
3269         (gnus-summary-show-all-threads)
3270         (goto-char (point-min))
3271         (while data
3272           (while (get-text-property (point) 'gnus-intangible)
3273             (forward-line 1))
3274           (gnus-data-set-pos (car data) (+ (point) 3))
3275           (setq data (cdr data))
3276           (forward-line 1))))))
3277
3278 (defun gnus-hidden-threads-configuration ()
3279   "Return the current hidden threads configuration."
3280   (save-excursion
3281     (let (config)
3282       (goto-char (point-min))
3283       (while (search-forward "\r" nil t)
3284         (push (1- (point)) config))
3285       config)))
3286
3287 (defun gnus-restore-hidden-threads-configuration (config)
3288   "Restore hidden threads configuration from CONFIG."
3289   (save-excursion
3290     (let (point buffer-read-only)
3291       (while (setq point (pop config))
3292         (when (and (< point (point-max))
3293                    (goto-char point)
3294                    (eq (char-after) ?\n))
3295           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3296
3297 ;; Various summary mode internalish functions.
3298
3299 (defun gnus-mouse-pick-article (e)
3300   (interactive "e")
3301   (mouse-set-point e)
3302   (gnus-summary-next-page nil t))
3303
3304 (defun gnus-summary-set-display-table ()
3305   "Change the display table.
3306 Odd characters have a tendency to mess
3307 up nicely formatted displays - we make all possible glyphs
3308 display only a single character."
3309
3310   ;; We start from the standard display table, if any.
3311   (let ((table (or (copy-sequence standard-display-table)
3312                    (make-display-table)))
3313         (i 32))
3314     ;; Nix out all the control chars...
3315     (while (>= (setq i (1- i)) 0)
3316       (aset table i [??]))
3317    ;; ... but not newline and cr, of course.  (cr is necessary for the
3318     ;; selective display).
3319     (aset table ?\n nil)
3320     (aset table ?\r nil)
3321     ;; We keep TAB as well.
3322     (aset table ?\t nil)
3323     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3324     ;; Emacs 23 (unicode), that are not set already.
3325     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3326                  160
3327                256)))
3328       (while (>= (setq i (1- i)) 127)
3329         ;; Only modify if the entry is nil.
3330         (unless (aref table i)
3331           (aset table i [??]))))
3332     (setq buffer-display-table table)))
3333
3334 (defun gnus-summary-set-article-display-arrow (pos)
3335   "Update the overlay arrow to point to line at position POS."
3336   (when (and gnus-summary-display-arrow
3337              (boundp 'overlay-arrow-position)
3338              (boundp 'overlay-arrow-string))
3339     (save-excursion
3340       (goto-char pos)
3341       (beginning-of-line)
3342       (unless overlay-arrow-position
3343         (setq overlay-arrow-position (make-marker)))
3344       (setq overlay-arrow-string "=>"
3345             overlay-arrow-position (set-marker overlay-arrow-position
3346                                                (point)
3347                                                (current-buffer))))))
3348
3349 (defun gnus-summary-setup-buffer (group)
3350   "Initialize summary buffer."
3351   (let ((buffer (gnus-summary-buffer-name group))
3352         (dead-name (concat "*Dead Summary "
3353                            (gnus-group-decoded-name group) "*")))
3354     ;; If a dead summary buffer exists, we kill it.
3355     (when (gnus-buffer-live-p dead-name)
3356       (gnus-kill-buffer dead-name))
3357     (if (get-buffer buffer)
3358         (progn
3359           (set-buffer buffer)
3360           (setq gnus-summary-buffer (current-buffer))
3361           (not gnus-newsgroup-prepared))
3362       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3363       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3364       (gnus-summary-mode group)
3365       (when gnus-carpal
3366         (gnus-carpal-setup-buffer 'summary))
3367       (when (gnus-group-quit-config group)
3368         (set (make-local-variable 'gnus-single-article-buffer) nil))
3369       (make-local-variable 'gnus-article-buffer)
3370       (make-local-variable 'gnus-article-current)
3371       (make-local-variable 'gnus-original-article-buffer)
3372       (setq gnus-newsgroup-name group)
3373       ;; Set any local variables in the group parameters.
3374       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3375       t)))
3376
3377 (defun gnus-set-global-variables ()
3378   "Set the global equivalents of the buffer-local variables.
3379 They are set to the latest values they had.  These reflect the summary
3380 buffer that was in action when the last article was fetched."
3381   (when (eq major-mode 'gnus-summary-mode)
3382     (setq gnus-summary-buffer (current-buffer))
3383     (let ((name gnus-newsgroup-name)
3384           (marked gnus-newsgroup-marked)
3385           (spam gnus-newsgroup-spam-marked)
3386           (unread gnus-newsgroup-unreads)
3387           (headers gnus-current-headers)
3388           (data gnus-newsgroup-data)
3389           (summary gnus-summary-buffer)
3390           (article-buffer gnus-article-buffer)
3391           (original gnus-original-article-buffer)
3392           (gac gnus-article-current)
3393           (reffed gnus-reffed-article-number)
3394           (score-file gnus-current-score-file)
3395           (default-charset gnus-newsgroup-charset)
3396           vlist)
3397       (let ((locals gnus-newsgroup-variables))
3398         (while locals
3399           (if (consp (car locals))
3400               (push (eval (caar locals)) vlist)
3401             (push (eval (car locals)) vlist))
3402           (setq locals (cdr locals)))
3403         (setq vlist (nreverse vlist)))
3404       (with-current-buffer gnus-group-buffer
3405         (setq gnus-newsgroup-name name
3406               gnus-newsgroup-marked marked
3407               gnus-newsgroup-spam-marked spam
3408               gnus-newsgroup-unreads unread
3409               gnus-current-headers headers
3410               gnus-newsgroup-data data
3411               gnus-article-current gac
3412               gnus-summary-buffer summary
3413               gnus-article-buffer article-buffer
3414               gnus-original-article-buffer original
3415               gnus-reffed-article-number reffed
3416               gnus-current-score-file score-file
3417               gnus-newsgroup-charset default-charset)
3418         (let ((locals gnus-newsgroup-variables))
3419           (while locals
3420             (if (consp (car locals))
3421                 (set (caar locals) (pop vlist))
3422               (set (car locals) (pop vlist)))
3423             (setq locals (cdr locals))))
3424         ;; The article buffer also has local variables.
3425         (when (gnus-buffer-live-p gnus-article-buffer)
3426           (set-buffer gnus-article-buffer)
3427           (setq gnus-summary-buffer summary))))))
3428
3429 (defun gnus-summary-article-unread-p (article)
3430   "Say whether ARTICLE is unread or not."
3431   (memq article gnus-newsgroup-unreads))
3432
3433 (defun gnus-summary-first-article-p (&optional article)
3434   "Return whether ARTICLE is the first article in the buffer."
3435   (if (not (setq article (or article (gnus-summary-article-number))))
3436       nil
3437     (eq article (caar gnus-newsgroup-data))))
3438
3439 (defun gnus-summary-last-article-p (&optional article)
3440   "Return whether ARTICLE is the last article in the buffer."
3441   (if (not (setq article (or article (gnus-summary-article-number))))
3442       ;; All non-existent numbers are the last article.  :-)
3443       t
3444     (not (cdr (gnus-data-find-list article)))))
3445
3446 (defun gnus-make-thread-indent-array (&optional n)
3447   (when (or n
3448             (progn (setq n 200) nil)
3449             (null gnus-thread-indent-array)
3450             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3451     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3452           gnus-thread-indent-array-level gnus-thread-indent-level)
3453     (while (>= n 0)
3454       (aset gnus-thread-indent-array n
3455             (make-string (* n gnus-thread-indent-level) ? ))
3456       (setq n (1- n)))))
3457
3458 (defun gnus-update-summary-mark-positions ()
3459   "Compute where the summary marks are to go."
3460   (save-excursion
3461     (when (gnus-buffer-exists-p gnus-summary-buffer)
3462       (set-buffer gnus-summary-buffer))
3463     (let ((spec gnus-summary-line-format-spec)
3464           pos)
3465       (save-excursion
3466         (gnus-set-work-buffer)
3467         (let ((gnus-tmp-unread ?Z)
3468               (gnus-replied-mark ?Z)
3469               (gnus-score-below-mark ?Z)
3470               (gnus-score-over-mark ?Z)
3471               (gnus-undownloaded-mark ?Z)
3472               (gnus-summary-line-format-spec spec)
3473               (gnus-newsgroup-downloadable '(0))
3474               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3475               case-fold-search ignores)
3476           ;; Here, all marks are bound to Z.
3477           (gnus-summary-insert-line header
3478                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3479           (goto-char (point-min))
3480           ;; Memorize the positions of the same characters as dummy marks.
3481           (while (re-search-forward "[A-D]" nil t)
3482             (push (point) ignores))
3483           (erase-buffer)
3484           ;; We use A-D as dummy marks in order to know column positions
3485           ;; where marks should be inserted.
3486           (setq gnus-tmp-unread ?A
3487                 gnus-replied-mark ?B
3488                 gnus-score-below-mark ?C
3489                 gnus-score-over-mark ?C
3490                 gnus-undownloaded-mark ?D)
3491           (gnus-summary-insert-line header
3492                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3493           ;; Ignore characters which aren't dummy marks.
3494           (dolist (p ignores)
3495             (delete-region (goto-char (1- p)) p)
3496             (insert ?Z))
3497           (goto-char (point-min))
3498           (setq pos (list (cons 'unread
3499                                 (and (search-forward "A" nil t)
3500                                      (- (point) (point-min) 1)))))
3501           (goto-char (point-min))
3502           (push (cons 'replied (and (search-forward "B" nil t)
3503                                     (- (point) (point-min) 1)))
3504                 pos)
3505           (goto-char (point-min))
3506           (push (cons 'score (and (search-forward "C" nil t)
3507                                   (- (point) (point-min) 1)))
3508                 pos)
3509           (goto-char (point-min))
3510           (push (cons 'download (and (search-forward "D" nil t)
3511                                      (- (point) (point-min) 1)))
3512                 pos)))
3513       (setq gnus-summary-mark-positions pos))))
3514
3515 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3516   "Insert a dummy root in the summary buffer."
3517   (beginning-of-line)
3518   (gnus-add-text-properties
3519    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3520    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3521
3522 (defun gnus-summary-extract-address-component (from)
3523   (or (car (funcall gnus-extract-address-components from))
3524       from))
3525
3526 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3527   (let ((mail-parse-charset gnus-newsgroup-charset)
3528         ; Is it really necessary to do this next part for each summary line?
3529         ; Luckily, doesn't seem to slow things down much.
3530         (mail-parse-ignored-charsets
3531          (with-current-buffer gnus-summary-buffer
3532            gnus-newsgroup-ignored-charsets)))
3533     (or
3534      (and gnus-ignored-from-addresses
3535           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3536           (let ((extra-headers (mail-header-extra header))
3537                 to
3538                 newsgroups)
3539             (cond
3540              ((setq to (cdr (assq 'To extra-headers)))
3541               (concat gnus-summary-to-prefix
3542                       (inline
3543                         (gnus-summary-extract-address-component
3544                          (funcall gnus-decode-encoded-address-function to)))))
3545              ((setq newsgroups
3546                     (or
3547                      (cdr (assq 'Newsgroups extra-headers))
3548                      (and
3549                       (memq 'Newsgroups gnus-extra-headers)
3550                       (eq (car (gnus-find-method-for-group
3551                                 gnus-newsgroup-name)) 'nntp)
3552                       (gnus-group-real-name gnus-newsgroup-name))))
3553               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3554      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3555
3556 (defun gnus-summary-insert-line (gnus-tmp-header
3557                                  gnus-tmp-level gnus-tmp-current
3558                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3559                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3560                                  &optional gnus-tmp-dummy gnus-tmp-score
3561                                  gnus-tmp-process)
3562   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3563       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3564                                           gnus-tmp-level)))
3565   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3566          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3567          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3568          (gnus-tmp-score-char
3569           (if (or (null gnus-summary-default-score)
3570                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3571                       gnus-summary-zcore-fuzz))
3572               ?                         ;Whitespace
3573             (if (< gnus-tmp-score gnus-summary-default-score)
3574                 gnus-score-below-mark gnus-score-over-mark)))
3575          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3576          (gnus-tmp-replied
3577           (cond (gnus-tmp-process gnus-process-mark)
3578                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3579                  gnus-cached-mark)
3580                 (gnus-tmp-replied gnus-replied-mark)
3581                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3582                  gnus-forwarded-mark)
3583                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3584                  gnus-saved-mark)
3585                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3586                  gnus-recent-mark)
3587                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3588                  gnus-unseen-mark)
3589                 (t gnus-no-mark)))
3590          (gnus-tmp-downloaded
3591           (cond (undownloaded
3592                  gnus-undownloaded-mark)
3593                 (gnus-newsgroup-agentized
3594                  gnus-downloaded-mark)
3595                 (t
3596                  gnus-no-mark)))
3597          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3598          (gnus-tmp-name
3599           (cond
3600            ((string-match "<[^>]+> *$" gnus-tmp-from)
3601             (let ((beg (match-beginning 0)))
3602               (or (and (string-match "^\".+\"" gnus-tmp-from)
3603                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3604                   (substring gnus-tmp-from 0 beg))))
3605            ((string-match "(.+)" gnus-tmp-from)
3606             (substring gnus-tmp-from
3607                        (1+ (match-beginning 0)) (1- (match-end 0))))
3608            (t gnus-tmp-from)))
3609          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3610          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3611          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3612          (buffer-read-only nil))
3613     (when (string= gnus-tmp-name "")
3614       (setq gnus-tmp-name gnus-tmp-from))
3615     (unless (numberp gnus-tmp-lines)
3616       (setq gnus-tmp-lines -1))
3617     (if (= gnus-tmp-lines -1)
3618         (setq gnus-tmp-lines "?")
3619       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3620       (gnus-put-text-property
3621      (point)
3622      (progn (eval gnus-summary-line-format-spec) (point))
3623        'gnus-number gnus-tmp-number)
3624     (when (gnus-visual-p 'summary-highlight 'highlight)
3625       (forward-line -1)
3626       (gnus-run-hooks 'gnus-summary-update-hook)
3627       (forward-line 1))))
3628
3629 (defun gnus-summary-update-line (&optional dont-update)
3630   "Update summary line after change."
3631   (when (and gnus-summary-default-score
3632              (not gnus-summary-inhibit-highlight))
3633     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3634            (article (gnus-summary-article-number))
3635            (score (gnus-summary-article-score article)))
3636       (unless dont-update
3637         (if (and gnus-summary-mark-below
3638                  (< (gnus-summary-article-score)
3639                     gnus-summary-mark-below))
3640             ;; This article has a low score, so we mark it as read.
3641             (when (memq article gnus-newsgroup-unreads)
3642               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3643           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3644             ;; This article was previously marked as read on account
3645             ;; of a low score, but now it has risen, so we mark it as
3646             ;; unread.
3647             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3648         (gnus-summary-update-mark
3649          (if (or (null gnus-summary-default-score)
3650                  (<= (abs (- score gnus-summary-default-score))
3651                      gnus-summary-zcore-fuzz))
3652              ?                          ;Whitespace
3653            (if (< score gnus-summary-default-score)
3654                gnus-score-below-mark gnus-score-over-mark))
3655          'score))
3656       ;; Do visual highlighting.
3657       (when (gnus-visual-p 'summary-highlight 'highlight)
3658         (gnus-run-hooks 'gnus-summary-update-hook)))))
3659
3660 (defvar gnus-tmp-new-adopts nil)
3661
3662 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3663   "Return the number of articles in THREAD.
3664 This may be 0 in some cases -- if none of the articles in
3665 the thread are to be displayed."
3666   (let* ((number
3667          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3668           (cond
3669            ((not (listp thread))
3670             1)
3671            ((and (consp thread) (cdr thread))
3672             (apply
3673              '+ 1 (mapcar
3674                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3675            ((null thread)
3676             1)
3677            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3678             1)
3679            (t 0))))
3680     (when (and level (zerop level) gnus-tmp-new-adopts)
3681       (incf number
3682             (apply '+ (mapcar
3683                        'gnus-summary-number-of-articles-in-thread
3684                        gnus-tmp-new-adopts))))
3685     (if char
3686         (if (> number 1) gnus-not-empty-thread-mark
3687           gnus-empty-thread-mark)
3688       number)))
3689
3690 (defsubst gnus-summary-line-message-size (head)
3691   "Return pretty-printed version of message size.
3692 This function is intended to be used in
3693 `gnus-summary-line-format-alist'."
3694   (let ((c (or (mail-header-chars head) -1)))
3695     (cond ((< c 0) "n/a")               ; chars not available
3696           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3697           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3698           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3699           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3700
3701
3702 (defun gnus-summary-set-local-parameters (group)
3703   "Go through the local params of GROUP and set all variable specs in that list."
3704   (let ((vars '(quit-config)))          ; Ignore quit-config.
3705     (dolist (elem (gnus-group-find-parameter group))
3706       (and (consp elem)                 ; Has to be a cons.
3707            (consp (cdr elem))           ; The cdr has to be a list.
3708            (symbolp (car elem))         ; Has to be a symbol in there.
3709            (not (memq (car elem) vars))
3710            (ignore-errors               ; So we set it.
3711              (push (car elem) vars)
3712              (make-local-variable (car elem))
3713              (set (car elem) (eval (nth 1 elem))))))))
3714
3715 (defun gnus-summary-read-group (group &optional show-all no-article
3716                                       kill-buffer no-display backward
3717                                       select-articles)
3718   "Start reading news in newsgroup GROUP.
3719 If SHOW-ALL is non-nil, already read articles are also listed.
3720 If NO-ARTICLE is non-nil, no article is selected initially.
3721 If NO-DISPLAY, don't generate a summary buffer."
3722   (let (result)
3723     (while (and group
3724                 (null (setq result
3725                             (let ((gnus-auto-select-next nil))
3726                               (or (gnus-summary-read-group-1
3727                                    group show-all no-article
3728                                    kill-buffer no-display
3729                                    select-articles)
3730                                   (setq show-all nil
3731                                         select-articles nil)))))
3732                 (eq gnus-auto-select-next 'quietly))
3733       (set-buffer gnus-group-buffer)
3734       ;; The entry function called above goes to the next
3735       ;; group automatically, so we go two groups back
3736       ;; if we are searching for the previous group.
3737       (when backward
3738         (gnus-group-prev-unread-group 2))
3739       (if (not (equal group (gnus-group-group-name)))
3740           (setq group (gnus-group-group-name))
3741         (setq group nil)))
3742     result))
3743
3744 (defun gnus-summary-read-group-1 (group show-all no-article
3745                                         kill-buffer no-display
3746                                         &optional select-articles)
3747   ;; Killed foreign groups can't be entered.
3748   ;;  (when (and (not (gnus-group-native-p group))
3749   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3750   ;;    (error "Dead non-native groups can't be entered"))
3751   (gnus-message 5 "Retrieving newsgroup: %s..."
3752                 (gnus-group-decoded-name group))
3753   (let* ((new-group (gnus-summary-setup-buffer group))
3754          (quit-config (gnus-group-quit-config group))
3755          (did-select (and new-group (gnus-select-newsgroup
3756                                      group show-all select-articles))))
3757     (cond
3758      ;; This summary buffer exists already, so we just select it.
3759      ((not new-group)
3760       (gnus-set-global-variables)
3761       (when kill-buffer
3762         (gnus-kill-or-deaden-summary kill-buffer))
3763       (gnus-configure-windows 'summary 'force)
3764       (gnus-set-mode-line 'summary)
3765       (gnus-summary-position-point)
3766       (message "")
3767       t)
3768      ;; We couldn't select this group.
3769      ((null did-select)
3770       (when (and (eq major-mode 'gnus-summary-mode)
3771                  (not (equal (current-buffer) kill-buffer)))
3772         (kill-buffer (current-buffer))
3773         (if (not quit-config)
3774             (progn
3775               ;; Update the info -- marks might need to be removed,
3776               ;; for instance.
3777               (gnus-summary-update-info)
3778               (set-buffer gnus-group-buffer)
3779               (gnus-group-jump-to-group group)
3780               (gnus-group-next-unread-group 1))
3781           (gnus-handle-ephemeral-exit quit-config)))
3782       (let ((grpinfo (gnus-get-info group)))
3783         (if (null (gnus-info-read grpinfo))
3784             (gnus-message 3 "Group %s contains no messages"
3785                           (gnus-group-decoded-name group))
3786           (gnus-message 3 "Can't select group")))
3787       nil)
3788      ;; The user did a `C-g' while prompting for number of articles,
3789      ;; so we exit this group.
3790      ((eq did-select 'quit)
3791       (and (eq major-mode 'gnus-summary-mode)
3792            (not (equal (current-buffer) kill-buffer))
3793            (kill-buffer (current-buffer)))
3794       (when kill-buffer
3795         (gnus-kill-or-deaden-summary kill-buffer))
3796       (if (not quit-config)
3797           (progn
3798             (set-buffer gnus-group-buffer)
3799             (gnus-group-jump-to-group group)
3800             (gnus-group-next-unread-group 1)
3801             (gnus-configure-windows 'group 'force))
3802         (gnus-handle-ephemeral-exit quit-config))
3803       ;; Finally signal the quit.
3804       (signal 'quit nil))
3805      ;; The group was successfully selected.
3806      (t
3807       (gnus-set-global-variables)
3808       ;; Save the active value in effect when the group was entered.
3809       (setq gnus-newsgroup-active
3810             (gnus-copy-sequence
3811              (gnus-active gnus-newsgroup-name)))
3812       ;; You can change the summary buffer in some way with this hook.
3813       (gnus-run-hooks 'gnus-select-group-hook)
3814       (when (memq 'summary (gnus-update-format-specifications
3815                             nil 'summary 'summary-mode 'summary-dummy))
3816         ;; The format specification for the summary line was updated,
3817         ;; so we need to update the mark positions as well.
3818         (gnus-update-summary-mark-positions))
3819       ;; Do score processing.
3820       (when gnus-use-scoring
3821         (gnus-possibly-score-headers))
3822       ;; Check whether to fill in the gaps in the threads.
3823       (when gnus-build-sparse-threads
3824         (gnus-build-sparse-threads))
3825       ;; Find the initial limit.
3826       (if show-all
3827           (let ((gnus-newsgroup-dormant nil))
3828             (gnus-summary-initial-limit show-all))
3829         (gnus-summary-initial-limit show-all))
3830       ;; Generate the summary buffer.
3831       (unless no-display
3832         (gnus-summary-prepare))
3833       (when gnus-use-trees
3834         (gnus-tree-open group)
3835         (setq gnus-summary-highlight-line-function
3836               'gnus-tree-highlight-article))
3837       ;; If the summary buffer is empty, but there are some low-scored
3838       ;; articles or some excluded dormants, we include these in the
3839       ;; buffer.
3840       (when (and (zerop (buffer-size))
3841                  (not no-display))
3842         (cond (gnus-newsgroup-dormant
3843                (gnus-summary-limit-include-dormant))
3844               ((and gnus-newsgroup-scored show-all)
3845                (gnus-summary-limit-include-expunged t))))
3846       ;; Function `gnus-apply-kill-file' must be called in this hook.
3847       (gnus-run-hooks 'gnus-apply-kill-hook)
3848       (if (and (zerop (buffer-size))
3849                (not no-display))
3850           (progn
3851             ;; This newsgroup is empty.
3852             (gnus-summary-catchup-and-exit nil t)
3853             (gnus-message 6 "No unread news")
3854             (when kill-buffer
3855               (gnus-kill-or-deaden-summary kill-buffer))
3856             ;; Return nil from this function.
3857             nil)
3858         ;; Hide conversation thread subtrees.  We cannot do this in
3859         ;; gnus-summary-prepare-hook since kill processing may not
3860         ;; work with hidden articles.
3861         (gnus-summary-maybe-hide-threads)
3862         (when kill-buffer
3863           (gnus-kill-or-deaden-summary kill-buffer))
3864         (gnus-summary-auto-select-subject)
3865         ;; Show first unread article if requested.
3866         (if (and (not no-article)
3867                  (not no-display)
3868                  gnus-newsgroup-unreads
3869                  gnus-auto-select-first)
3870             (progn
3871               (gnus-configure-windows 'summary)
3872               (let ((art (gnus-summary-article-number)))
3873                 (unless (and (not gnus-plugged)
3874                              (or (memq art gnus-newsgroup-undownloaded)
3875                                  (memq art gnus-newsgroup-downloadable)))
3876                   (gnus-summary-goto-article art))))
3877           ;; Don't select any articles.
3878           (gnus-summary-position-point)
3879           (gnus-configure-windows 'summary 'force)
3880           (gnus-set-mode-line 'summary))
3881         (when (and gnus-auto-center-group
3882                    (get-buffer-window gnus-group-buffer t))
3883           ;; Gotta use windows, because recenter does weird stuff if
3884           ;; the current buffer ain't the displayed window.
3885           (let ((owin (selected-window)))
3886             (select-window (get-buffer-window gnus-group-buffer t))
3887             (when (gnus-group-goto-group group)
3888               (recenter))
3889             (select-window owin)))
3890         ;; Mark this buffer as "prepared".
3891         (setq gnus-newsgroup-prepared t)
3892         (gnus-run-hooks 'gnus-summary-prepared-hook)
3893         (unless (gnus-ephemeral-group-p group)
3894           (gnus-group-update-group group))
3895         t)))))
3896
3897 (defun gnus-summary-auto-select-subject ()
3898   "Select the subject line on initial group entry."
3899   (goto-char (point-min))
3900   (cond
3901    ((eq gnus-auto-select-subject 'best)
3902     (gnus-summary-best-unread-subject))
3903    ((eq gnus-auto-select-subject 'unread)
3904     (gnus-summary-first-unread-subject))
3905    ((eq gnus-auto-select-subject 'unseen)
3906     (gnus-summary-first-unseen-subject))
3907    ((eq gnus-auto-select-subject 'unseen-or-unread)
3908     (gnus-summary-first-unseen-or-unread-subject))
3909    ((eq gnus-auto-select-subject 'first)
3910     ;; Do nothing.
3911     )
3912    ((functionp gnus-auto-select-subject)
3913     (funcall gnus-auto-select-subject))))
3914
3915 (defun gnus-summary-prepare ()
3916   "Generate the summary buffer."
3917   (interactive)
3918   (let ((buffer-read-only nil))
3919     (erase-buffer)
3920     (setq gnus-newsgroup-data nil
3921           gnus-newsgroup-data-reverse nil)
3922     (gnus-run-hooks 'gnus-summary-generate-hook)
3923     ;; Generate the buffer, either with threads or without.
3924     (when gnus-newsgroup-headers
3925       (gnus-summary-prepare-threads
3926        (if gnus-show-threads
3927            (gnus-sort-gathered-threads
3928             (funcall gnus-summary-thread-gathering-function
3929                      (gnus-sort-threads
3930                       (gnus-cut-threads (gnus-make-threads)))))
3931          ;; Unthreaded display.
3932          (gnus-sort-articles gnus-newsgroup-headers))))
3933     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3934     ;; Call hooks for modifying summary buffer.
3935     (goto-char (point-min))
3936     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3937
3938 (defsubst gnus-general-simplify-subject (subject)
3939   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3940   (setq subject
3941         (cond
3942          ;; Truncate the subject.
3943          (gnus-simplify-subject-functions
3944           (gnus-map-function gnus-simplify-subject-functions subject))
3945          ((numberp gnus-summary-gather-subject-limit)
3946           (setq subject (gnus-simplify-subject-re subject))
3947           (if (> (length subject) gnus-summary-gather-subject-limit)
3948               (substring subject 0 gnus-summary-gather-subject-limit)
3949             subject))
3950          ;; Fuzzily simplify it.
3951          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3952           (gnus-simplify-subject-fuzzy subject))
3953          ;; Just remove the leading "Re:".
3954          (t
3955           (gnus-simplify-subject-re subject))))
3956
3957   (if (and gnus-summary-gather-exclude-subject
3958            (string-match gnus-summary-gather-exclude-subject subject))
3959       nil                         ; This article shouldn't be gathered
3960     subject))
3961
3962 (defun gnus-summary-simplify-subject-query ()
3963   "Query where the respool algorithm would put this article."
3964   (interactive)
3965   (gnus-summary-select-article)
3966   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3967
3968 (defun gnus-gather-threads-by-subject (threads)
3969   "Gather threads by looking at Subject headers."
3970   (if (not gnus-summary-make-false-root)
3971       threads
3972     (let ((hashtb (gnus-make-hashtable 1024))
3973           (prev threads)
3974           (result threads)
3975           subject hthread whole-subject)
3976       (while threads
3977         (setq subject (gnus-general-simplify-subject
3978                        (setq whole-subject (mail-header-subject
3979                                             (caar threads)))))
3980         (when subject
3981           (if (setq hthread (gnus-gethash subject hashtb))
3982               (progn
3983                 ;; We enter a dummy root into the thread, if we
3984                 ;; haven't done that already.
3985                 (unless (stringp (caar hthread))
3986                   (setcar hthread (list whole-subject (car hthread))))
3987                 ;; We add this new gathered thread to this gathered
3988                 ;; thread.
3989                 (setcdr (car hthread)
3990                         (nconc (cdar hthread) (list (car threads))))
3991                 ;; Remove it from the list of threads.
3992                 (setcdr prev (cdr threads))
3993                 (setq threads prev))
3994             ;; Enter this thread into the hash table.
3995             (gnus-sethash subject
3996                           (if gnus-summary-make-false-root-always
3997                               (progn
3998                                 ;; If you want a dummy root above all
3999                                 ;; threads...
4000                                 (setcar threads (list whole-subject
4001                                                       (car threads)))
4002                                 threads)
4003                             threads)
4004                           hashtb)))
4005         (setq prev threads)
4006         (setq threads (cdr threads)))
4007       result)))
4008
4009 (defun gnus-gather-threads-by-references (threads)
4010   "Gather threads by looking at References headers."
4011   (let ((idhashtb (gnus-make-hashtable 1024))
4012         (thhashtb (gnus-make-hashtable 1024))
4013         (prev threads)
4014         (result threads)
4015         ids references id gthread gid entered ref)
4016     (while threads
4017       (when (setq references (mail-header-references (caar threads)))
4018         (setq id (mail-header-id (caar threads))
4019               ids (inline (gnus-split-references references))
4020               entered nil)
4021         (while (setq ref (pop ids))
4022           (setq ids (delete ref ids))
4023           (if (not (setq gid (gnus-gethash ref idhashtb)))
4024               (progn
4025                 (gnus-sethash ref id idhashtb)
4026                 (gnus-sethash id threads thhashtb))
4027             (setq gthread (gnus-gethash gid thhashtb))
4028             (unless entered
4029               ;; We enter a dummy root into the thread, if we
4030               ;; haven't done that already.
4031               (unless (stringp (caar gthread))
4032                 (setcar gthread (list (mail-header-subject (caar gthread))
4033                                       (car gthread))))
4034               ;; We add this new gathered thread to this gathered
4035               ;; thread.
4036               (setcdr (car gthread)
4037                       (nconc (cdar gthread) (list (car threads)))))
4038             ;; Add it into the thread hash table.
4039             (gnus-sethash id gthread thhashtb)
4040             (setq entered t)
4041             ;; Remove it from the list of threads.
4042             (setcdr prev (cdr threads))
4043             (setq threads prev))))
4044       (setq prev threads)
4045       (setq threads (cdr threads)))
4046     result))
4047
4048 (defun gnus-sort-gathered-threads (threads)
4049   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4050   (let ((result threads))
4051     (while threads
4052       (when (stringp (caar threads))
4053         (setcdr (car threads)
4054                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4055       (setq threads (cdr threads)))
4056     result))
4057
4058 (defun gnus-thread-loop-p (root thread)
4059   "Say whether ROOT is in THREAD."
4060   (let ((stack (list thread))
4061         (infloop 0)
4062         th)
4063     (while (setq thread (pop stack))
4064       (setq th (cdr thread))
4065       (while (and th
4066                   (not (eq (caar th) root)))
4067         (pop th))
4068       (if th
4069           ;; We have found a loop.
4070           (let (ref-dep)
4071             (setcdr thread (delq (car th) (cdr thread)))
4072             (if (boundp (setq ref-dep (intern "none"
4073                                               gnus-newsgroup-dependencies)))
4074                 (setcdr (symbol-value ref-dep)
4075                         (nconc (cdr (symbol-value ref-dep))
4076                                (list (car th))))
4077               (set ref-dep (list nil (car th))))
4078             (setq infloop 1
4079                   stack nil))
4080         ;; Push all the subthreads onto the stack.
4081         (push (cdr thread) stack)))
4082     infloop))
4083
4084 (defun gnus-make-threads ()
4085   "Go through the dependency hashtb and find the roots.  Return all threads."
4086   (let (threads)
4087     (while (catch 'infloop
4088              (mapatoms
4089               (lambda (refs)
4090                 ;; Deal with self-referencing References loops.
4091                 (when (and (car (symbol-value refs))
4092                            (not (zerop
4093                                  (apply
4094                                   '+
4095                                   (mapcar
4096                                    (lambda (thread)
4097                                      (gnus-thread-loop-p
4098                                       (car (symbol-value refs)) thread))
4099                                    (cdr (symbol-value refs)))))))
4100                   (setq threads nil)
4101                   (throw 'infloop t))
4102                 (unless (car (symbol-value refs))
4103                   ;; These threads do not refer back to any other
4104                   ;; articles, so they're roots.
4105                   (setq threads (append (cdr (symbol-value refs)) threads))))
4106               gnus-newsgroup-dependencies)))
4107     threads))
4108
4109 ;; Build the thread tree.
4110 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4111   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4112
4113 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4114 if it was already present.
4115
4116 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4117 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4118 Message-IDs will be renamed to a unique Message-ID before being
4119 entered.
4120
4121 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4122   (let* ((id (mail-header-id header))
4123          (id-dep (and id (intern id dependencies)))
4124          parent-id ref ref-dep ref-header replaced)
4125     ;; Enter this `header' in the `dependencies' table.
4126     (cond
4127      ((not id-dep)
4128       (setq header nil))
4129      ;; The first two cases do the normal part: enter a new `header'
4130      ;; in the `dependencies' table.
4131      ((not (boundp id-dep))
4132       (set id-dep (list header)))
4133      ((null (car (symbol-value id-dep)))
4134       (setcar (symbol-value id-dep) header))
4135
4136      ;; From here the `header' was already present in the
4137      ;; `dependencies' table.
4138      (force-new
4139       ;; Overrides an existing entry;
4140       ;; just set the header part of the entry.
4141       (setcar (symbol-value id-dep) header)
4142       (setq replaced t))
4143
4144      ;; Renames the existing `header' to a unique Message-ID.
4145      ((not gnus-summary-ignore-duplicates)
4146       ;; An article with this Message-ID has already been seen.
4147       ;; We rename the Message-ID.
4148       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4149            (list header))
4150       (mail-header-set-id header id))
4151
4152      ;; The last case ignores an existing entry, except it adds any
4153      ;; additional Xrefs (in case the two articles came from different
4154      ;; servers.
4155      ;; Also sets `header' to `nil' meaning that the `dependencies'
4156      ;; table was *not* modified.
4157      (t
4158       (mail-header-set-xref
4159        (car (symbol-value id-dep))
4160        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4161                    "")
4162                (or (mail-header-xref header) "")))
4163       (setq header nil)))
4164
4165     (when (and header (not replaced))
4166       ;; First check that we are not creating a References loop.
4167       (setq parent-id (gnus-parent-id (mail-header-references header)))
4168       (setq ref parent-id)
4169       (while (and ref
4170                   (setq ref-dep (intern-soft ref dependencies))
4171                   (boundp ref-dep)
4172                   (setq ref-header (car (symbol-value ref-dep))))
4173         (if (string= id ref)
4174             ;; Yuk!  This is a reference loop.  Make the article be a
4175             ;; root article.
4176             (progn
4177               (mail-header-set-references (car (symbol-value id-dep)) "none")
4178               (setq ref nil)
4179               (setq parent-id nil))
4180           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4181       (setq ref-dep (intern (or parent-id "none") dependencies))
4182       (if (boundp ref-dep)
4183           (setcdr (symbol-value ref-dep)
4184                   (nconc (cdr (symbol-value ref-dep))
4185                          (list (symbol-value id-dep))))
4186         (set ref-dep (list nil (symbol-value id-dep)))))
4187     header))
4188
4189 (defun gnus-extract-message-id-from-in-reply-to (string)
4190   (if (string-match "<[^>]+>" string)
4191       (substring string (match-beginning 0) (match-end 0))
4192     nil))
4193
4194 (defun gnus-build-sparse-threads ()
4195   (let ((headers gnus-newsgroup-headers)
4196         (mail-parse-charset gnus-newsgroup-charset)
4197         (gnus-summary-ignore-duplicates t)
4198         header references generation relations
4199         subject child end new-child date)
4200     ;; First we create an alist of generations/relations, where
4201     ;; generations is how much we trust the relation, and the relation
4202     ;; is parent/child.
4203     (gnus-message 7 "Making sparse threads...")
4204     (save-excursion
4205       (nnheader-set-temp-buffer " *gnus sparse threads*")
4206       (while (setq header (pop headers))
4207         (when (and (setq references (mail-header-references header))
4208                    (not (string= references "")))
4209           (insert references)
4210           (setq child (mail-header-id header)
4211                 subject (mail-header-subject header)
4212                 date (mail-header-date header)
4213                 generation 0)
4214           (while (search-backward ">" nil t)
4215             (setq end (1+ (point)))
4216             (when (search-backward "<" nil t)
4217               (setq new-child (buffer-substring (point) end))
4218               (push (list (incf generation)
4219                           child (setq child new-child)
4220                           subject date)
4221                     relations)))
4222           (when child
4223             (push (list (1+ generation) child nil subject) relations))
4224           (erase-buffer)))
4225       (kill-buffer (current-buffer)))
4226     ;; Sort over trustworthiness.
4227     (mapcar
4228      (lambda (relation)
4229        (when (gnus-dependencies-add-header
4230               (make-full-mail-header
4231                gnus-reffed-article-number
4232                (nth 3 relation) "" (or (nth 4 relation) "")
4233                (nth 1 relation)
4234                (or (nth 2 relation) "") 0 0 "")
4235               gnus-newsgroup-dependencies nil)
4236          (push gnus-reffed-article-number gnus-newsgroup-limit)
4237          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4238          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4239                gnus-newsgroup-reads)
4240          (decf gnus-reffed-article-number)))
4241      (sort relations 'car-less-than-car))
4242     (gnus-message 7 "Making sparse threads...done")))
4243
4244 (defun gnus-build-old-threads ()
4245   ;; Look at all the articles that refer back to old articles, and
4246   ;; fetch the headers for the articles that aren't there.  This will
4247   ;; build complete threads - if the roots haven't been expired by the
4248   ;; server, that is.
4249   (let ((mail-parse-charset gnus-newsgroup-charset)
4250         id heads)
4251     (mapatoms
4252      (lambda (refs)
4253        (when (not (car (symbol-value refs)))
4254          (setq heads (cdr (symbol-value refs)))
4255          (while heads
4256            (if (memq (mail-header-number (caar heads))
4257                      gnus-newsgroup-dormant)
4258                (setq heads (cdr heads))
4259              (setq id (symbol-name refs))
4260              (while (and (setq id (gnus-build-get-header id))
4261                          (not (car (gnus-id-to-thread id)))))
4262              (setq heads nil)))))
4263      gnus-newsgroup-dependencies)))
4264
4265 (defsubst gnus-remove-odd-characters (string)
4266   "Translate STRING into something that doesn't contain weird characters."
4267   (mm-subst-char-in-string
4268    ?\r ?\-
4269    (mm-subst-char-in-string ?\n ?\- string t) t))
4270
4271 ;; This function has to be called with point after the article number
4272 ;; on the beginning of the line.
4273 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4274   (let ((eol (point-at-eol))
4275         (buffer (current-buffer))
4276         header references in-reply-to)
4277
4278     ;; overview: [num subject from date id refs chars lines misc]
4279     (unwind-protect
4280         (let (x)
4281           (narrow-to-region (point) eol)
4282           (unless (eobp)
4283             (forward-char))
4284
4285           (setq header
4286                 (make-full-mail-header
4287                  number                 ; number
4288                  (condition-case ()     ; subject
4289                      (gnus-remove-odd-characters
4290                       (funcall gnus-decode-encoded-word-function
4291                                (setq x (nnheader-nov-field))))
4292                    (error x))
4293                  (condition-case ()     ; from
4294                      (gnus-remove-odd-characters
4295                       (funcall gnus-decode-encoded-address-function
4296                                (setq x (nnheader-nov-field))))
4297                    (error x))
4298                  (nnheader-nov-field)   ; date
4299                  (nnheader-nov-read-message-id number)  ; id
4300                  (setq references (nnheader-nov-field)) ; refs
4301                  (nnheader-nov-read-integer) ; chars
4302                  (nnheader-nov-read-integer) ; lines
4303                  (unless (eobp)
4304                    (if (looking-at "Xref: ")
4305                        (goto-char (match-end 0)))
4306                    (nnheader-nov-field)) ; Xref
4307                  (nnheader-nov-parse-extra)))) ; extra
4308
4309       (widen))
4310
4311     (when (and (string= references "")
4312                (setq in-reply-to (mail-header-extra header))
4313                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4314       (mail-header-set-references
4315        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4316
4317     (when gnus-alter-header-function
4318       (funcall gnus-alter-header-function header))
4319     (gnus-dependencies-add-header header dependencies force-new)))
4320
4321 (defun gnus-build-get-header (id)
4322   "Look through the buffer of NOV lines and find the header to ID.
4323 Enter this line into the dependencies hash table, and return
4324 the id of the parent article (if any)."
4325   (let ((deps gnus-newsgroup-dependencies)
4326         found header)
4327     (prog1
4328         (save-excursion
4329           (set-buffer nntp-server-buffer)
4330           (let ((case-fold-search nil))
4331             (goto-char (point-min))
4332             (while (and (not found)
4333                         (search-forward id nil t))
4334               (beginning-of-line)
4335               (setq found (looking-at
4336                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4337                                    (regexp-quote id))))
4338               (or found (beginning-of-line 2)))
4339             (when found
4340               (beginning-of-line)
4341               (and
4342                (setq header (gnus-nov-parse-line
4343                              (read (current-buffer)) deps))
4344                (gnus-parent-id (mail-header-references header))))))
4345       (when header
4346         (let ((number (mail-header-number header)))
4347           (push number gnus-newsgroup-limit)
4348           (push header gnus-newsgroup-headers)
4349           (if (memq number gnus-newsgroup-unselected)
4350               (progn
4351                 (setq gnus-newsgroup-unreads
4352                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4353                                                number))
4354                 (setq gnus-newsgroup-unselected
4355                       (delq number gnus-newsgroup-unselected)))
4356             (push number gnus-newsgroup-ancient)))))))
4357
4358 (defun gnus-build-all-threads ()
4359   "Read all the headers."
4360   (let ((gnus-summary-ignore-duplicates t)
4361         (mail-parse-charset gnus-newsgroup-charset)
4362         (dependencies gnus-newsgroup-dependencies)
4363         header article)
4364     (save-excursion
4365       (set-buffer nntp-server-buffer)
4366       (let ((case-fold-search nil))
4367         (goto-char (point-min))
4368         (while (not (eobp))
4369           (ignore-errors
4370             (setq article (read (current-buffer))
4371                   header (gnus-nov-parse-line article dependencies)))
4372           (when header
4373             (with-current-buffer gnus-summary-buffer
4374               (push header gnus-newsgroup-headers)
4375               (if (memq (setq article (mail-header-number header))
4376                         gnus-newsgroup-unselected)
4377                   (progn
4378                     (setq gnus-newsgroup-unreads
4379                           (gnus-add-to-sorted-list
4380                            gnus-newsgroup-unreads article))
4381                     (setq gnus-newsgroup-unselected
4382                           (delq article gnus-newsgroup-unselected)))
4383                 (push article gnus-newsgroup-ancient)))
4384             (forward-line 1)))))))
4385
4386 (defun gnus-summary-update-article-line (article header)
4387   "Update the line for ARTICLE using HEADER."
4388   (let* ((id (mail-header-id header))
4389          (thread (gnus-id-to-thread id)))
4390     (unless thread
4391       (error "Article in no thread"))
4392     ;; Update the thread.
4393     (setcar thread header)
4394     (gnus-summary-goto-subject article)
4395     (let* ((datal (gnus-data-find-list article))
4396            (data (car datal))
4397            (buffer-read-only nil)
4398            (level (gnus-summary-thread-level)))
4399       (gnus-delete-line)
4400       (let ((inserted (- (point)
4401                          (progn
4402                            (gnus-summary-insert-line
4403                             header level nil
4404                             (memq article gnus-newsgroup-undownloaded)
4405                             (gnus-article-mark article)
4406                             (memq article gnus-newsgroup-replied)
4407                             (memq article gnus-newsgroup-expirable)
4408                             ;; Only insert the Subject string when it's different
4409                             ;; from the previous Subject string.
4410                             (if (and
4411                                  gnus-show-threads
4412                                  (gnus-subject-equal
4413                                   (condition-case ()
4414                                       (mail-header-subject
4415                                        (gnus-data-header
4416                                         (cadr
4417                                          (gnus-data-find-list
4418                                           article
4419                                           (gnus-data-list t)))))
4420                                     ;; Error on the side of excessive subjects.
4421                                     (error ""))
4422                                   (mail-header-subject header)))
4423                                 ""
4424                               (mail-header-subject header))
4425                             nil (cdr (assq article gnus-newsgroup-scored))
4426                             (memq article gnus-newsgroup-processable))
4427                            (point)))))
4428         (when (cdr datal)
4429           (gnus-data-update-list
4430            (cdr datal)
4431            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4432
4433 (defun gnus-summary-update-article (article &optional iheader)
4434   "Update ARTICLE in the summary buffer."
4435   (set-buffer gnus-summary-buffer)
4436   (let* ((header (gnus-summary-article-header article))
4437          (id (mail-header-id header))
4438          (data (gnus-data-find article))
4439          (thread (gnus-id-to-thread id))
4440          (references (mail-header-references header))
4441          (parent
4442           (gnus-id-to-thread
4443            (or (gnus-parent-id
4444                 (when (and references
4445                            (not (equal "" references)))
4446                   references))
4447                "none")))
4448          (buffer-read-only nil)
4449          (old (car thread)))
4450     (when thread
4451       (unless iheader
4452         (setcar thread nil)
4453         (when parent
4454           (delq thread parent)))
4455       (if (gnus-summary-insert-subject id header)
4456           ;; Set the (possibly) new article number in the data structure.
4457           (gnus-data-set-number data (gnus-id-to-article id))
4458         (setcar thread old)
4459         nil))))
4460
4461 (defun gnus-rebuild-thread (id &optional line)
4462   "Rebuild the thread containing ID.
4463 If LINE, insert the rebuilt thread starting on line LINE."
4464   (let ((buffer-read-only nil)
4465         old-pos current thread data)
4466     (if (not gnus-show-threads)
4467         (setq thread (list (car (gnus-id-to-thread id))))
4468       ;; Get the thread this article is part of.
4469       (setq thread (gnus-remove-thread id)))
4470     (setq old-pos (point-at-bol))
4471     (setq current (save-excursion
4472                     (and (re-search-backward "[\r\n]" nil t)
4473                          (gnus-summary-article-number))))
4474     ;; If this is a gathered thread, we have to go some re-gathering.
4475     (when (stringp (car thread))
4476       (let ((subject (car thread))
4477             roots thr)
4478         (setq thread (cdr thread))
4479         (while thread
4480           (unless (memq (setq thr (gnus-id-to-thread
4481                                    (gnus-root-id
4482                                     (mail-header-id (caar thread)))))
4483                         roots)
4484             (push thr roots))
4485           (setq thread (cdr thread)))
4486         ;; We now have all (unique) roots.
4487         (if (= (length roots) 1)
4488             ;; All the loose roots are now one solid root.
4489             (setq thread (car roots))
4490           (setq thread (cons subject (gnus-sort-threads roots))))))
4491     (let (threads)
4492       ;; We then insert this thread into the summary buffer.
4493       (when line
4494         (goto-char (point-min))
4495         (forward-line (1- line)))
4496       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4497         (if gnus-show-threads
4498             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4499           (gnus-summary-prepare-unthreaded thread))
4500         (setq data (nreverse gnus-newsgroup-data))
4501         (setq threads gnus-newsgroup-threads))
4502       ;; We splice the new data into the data structure.
4503       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4504       ;;!!! then we want to insert at the beginning of the buffer.
4505       ;;!!! That happens to be true with Gnus now, but that may
4506       ;;!!! change in the future.  Perhaps.
4507       (gnus-data-enter-list
4508        (if line nil current) data (- (point) old-pos))
4509       (setq gnus-newsgroup-threads
4510             (nconc threads gnus-newsgroup-threads))
4511       (gnus-data-compute-positions))))
4512
4513 (defun gnus-number-to-header (number)
4514   "Return the header for article NUMBER."
4515   (let ((headers gnus-newsgroup-headers))
4516     (while (and headers
4517                 (not (= number (mail-header-number (car headers)))))
4518       (pop headers))
4519     (when headers
4520       (car headers))))
4521
4522 (defun gnus-parent-headers (in-headers &optional generation)
4523   "Return the headers of the GENERATIONeth parent of HEADERS."
4524   (unless generation
4525     (setq generation 1))
4526   (let ((parent t)
4527         (headers in-headers)
4528         references)
4529     (while (and parent
4530                 (not (zerop generation))
4531                 (setq references (mail-header-references headers)))
4532       (setq headers (if (and references
4533                              (setq parent (gnus-parent-id references)))
4534                         (car (gnus-id-to-thread parent))
4535                       nil))
4536       (decf generation))
4537     (and (not (eq headers in-headers))
4538          headers)))
4539
4540 (defun gnus-id-to-thread (id)
4541   "Return the (sub-)thread where ID appears."
4542   (gnus-gethash id gnus-newsgroup-dependencies))
4543
4544 (defun gnus-id-to-article (id)
4545   "Return the article number of ID."
4546   (let ((thread (gnus-id-to-thread id)))
4547     (when (and thread
4548                (car thread))
4549       (mail-header-number (car thread)))))
4550
4551 (defun gnus-id-to-header (id)
4552   "Return the article headers of ID."
4553   (car (gnus-id-to-thread id)))
4554
4555 (defun gnus-article-displayed-root-p (article)
4556   "Say whether ARTICLE is a root(ish) article."
4557   (let ((level (gnus-summary-thread-level article))
4558         (refs (mail-header-references  (gnus-summary-article-header article)))
4559         particle)
4560     (cond
4561      ((null level) nil)
4562      ((zerop level) t)
4563      ((null refs) t)
4564      ((null (gnus-parent-id refs)) t)
4565      ((and (= 1 level)
4566            (null (setq particle (gnus-id-to-article
4567                                  (gnus-parent-id refs))))
4568            (null (gnus-summary-thread-level particle)))))))
4569
4570 (defun gnus-root-id (id)
4571   "Return the id of the root of the thread where ID appears."
4572   (let (last-id prev)
4573     (while (and id (setq prev (car (gnus-id-to-thread id))))
4574       (setq last-id id
4575             id (gnus-parent-id (mail-header-references prev))))
4576     last-id))
4577
4578 (defun gnus-articles-in-thread (thread)
4579   "Return the list of articles in THREAD."
4580   (cons (mail-header-number (car thread))
4581         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4582
4583 (defun gnus-remove-thread (id &optional dont-remove)
4584   "Remove the thread that has ID in it."
4585   (let (headers thread last-id)
4586     ;; First go up in this thread until we find the root.
4587     (setq last-id (gnus-root-id id)
4588           headers (message-flatten-list (gnus-id-to-thread last-id)))
4589     ;; We have now found the real root of this thread.  It might have
4590     ;; been gathered into some loose thread, so we have to search
4591     ;; through the threads to find the thread we wanted.
4592     (let ((threads gnus-newsgroup-threads)
4593           sub)
4594       (while threads
4595         (setq sub (car threads))
4596         (if (stringp (car sub))
4597             ;; This is a gathered thread, so we look at the roots
4598             ;; below it to find whether this article is in this
4599             ;; gathered root.
4600             (progn
4601               (setq sub (cdr sub))
4602               (while sub
4603                 (when (member (caar sub) headers)
4604                   (setq thread (car threads)
4605                         threads nil
4606                         sub nil))
4607                 (setq sub (cdr sub))))
4608           ;; It's an ordinary thread, so we check it.
4609           (when (eq (car sub) (car headers))
4610             (setq thread sub
4611                   threads nil)))
4612         (setq threads (cdr threads)))
4613       ;; If this article is in no thread, then it's a root.
4614       (if thread
4615           (unless dont-remove
4616             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4617         (setq thread (gnus-id-to-thread last-id)))
4618       (when thread
4619         (prog1
4620             thread                      ; We return this thread.
4621           (unless dont-remove
4622             (if (stringp (car thread))
4623                 (progn
4624                   ;; If we use dummy roots, then we have to remove the
4625                   ;; dummy root as well.
4626                   (when (eq gnus-summary-make-false-root 'dummy)
4627                     ;; We go to the dummy root by going to
4628                     ;; the first sub-"thread", and then one line up.
4629                     (gnus-summary-goto-article
4630                      (mail-header-number (caadr thread)))
4631                     (forward-line -1)
4632                     (gnus-delete-line)
4633                     (gnus-data-compute-positions))
4634                   (setq thread (cdr thread))
4635                   (while thread
4636                     (gnus-remove-thread-1 (car thread))
4637                     (setq thread (cdr thread))))
4638               (gnus-remove-thread-1 thread))))))))
4639
4640 (defun gnus-remove-thread-1 (thread)
4641   "Remove the thread THREAD recursively."
4642   (let ((number (mail-header-number (pop thread)))
4643         d)
4644     (setq thread (reverse thread))
4645     (while thread
4646       (gnus-remove-thread-1 (pop thread)))
4647     (when (setq d (gnus-data-find number))
4648       (goto-char (gnus-data-pos d))
4649       (gnus-summary-show-thread)
4650       (gnus-data-remove
4651        number
4652        (- (point-at-bol)
4653           (prog1
4654               (1+ (point-at-eol))
4655             (gnus-delete-line)))))))
4656
4657 (defun gnus-sort-threads-recursive (threads func)
4658   (sort (mapcar (lambda (thread)
4659                   (cons (car thread)
4660                         (and (cdr thread)
4661                              (gnus-sort-threads-recursive (cdr thread) func))))
4662                 threads) func))
4663
4664 (defun gnus-sort-threads-loop (threads func)
4665   (let* ((superthread (cons nil threads))
4666          (stack (list (cons superthread threads)))
4667          remaining-threads thread)
4668     (while stack
4669       (setq remaining-threads (cdr (car stack)))
4670       (if remaining-threads
4671           (progn (setq thread (car remaining-threads))
4672                  (setcdr (car stack) (cdr remaining-threads))
4673                  (if (cdr thread)
4674                      (push (cons thread (cdr thread)) stack)))
4675         (setq thread (caar stack))
4676         (setcdr thread (sort (cdr thread) func))
4677         (pop stack)))
4678     (cdr superthread)))
4679
4680 (defun gnus-sort-threads (threads)
4681   "Sort THREADS."
4682   (if (not gnus-thread-sort-functions)
4683       threads
4684     (gnus-message 8 "Sorting threads...")
4685     (prog1
4686         (condition-case nil
4687             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4688               (gnus-sort-threads-recursive
4689                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4690           ;; Even after binding max-lisp-eval-depth, the recursive
4691           ;; sorter might fail for very long threads.  In that case,
4692           ;; try using a (less well-tested) non-recursive sorter.
4693           (error (gnus-sort-threads-loop
4694                   threads (gnus-make-sort-function
4695                            gnus-thread-sort-functions))))
4696       (gnus-message 8 "Sorting threads...done"))))
4697
4698 (defun gnus-sort-articles (articles)
4699   "Sort ARTICLES."
4700   (when gnus-article-sort-functions
4701     (gnus-message 7 "Sorting articles...")
4702     (prog1
4703         (setq gnus-newsgroup-headers
4704               (sort articles (gnus-make-sort-function
4705                               gnus-article-sort-functions)))
4706       (gnus-message 7 "Sorting articles...done"))))
4707
4708 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4709 (defmacro gnus-thread-header (thread)
4710   "Return header of first article in THREAD.
4711 Note that THREAD must never, ever be anything else than a variable -
4712 using some other form will lead to serious barfage."
4713   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4714   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4715   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4716         (vector thread) 2))
4717
4718 (defsubst gnus-article-sort-by-number (h1 h2)
4719   "Sort articles by article number."
4720   (< (mail-header-number h1)
4721      (mail-header-number h2)))
4722
4723 (defun gnus-thread-sort-by-number (h1 h2)
4724   "Sort threads by root article number."
4725   (gnus-article-sort-by-number
4726    (gnus-thread-header h1) (gnus-thread-header h2)))
4727
4728 (defsubst gnus-article-sort-by-random (h1 h2)
4729   "Sort articles by article number."
4730   (zerop (random 2)))
4731
4732 (defun gnus-thread-sort-by-random (h1 h2)
4733   "Sort threads by root article number."
4734   (gnus-article-sort-by-random
4735    (gnus-thread-header h1) (gnus-thread-header h2)))
4736
4737 (defsubst gnus-article-sort-by-lines (h1 h2)
4738   "Sort articles by article Lines header."
4739   (< (mail-header-lines h1)
4740      (mail-header-lines h2)))
4741
4742 (defun gnus-thread-sort-by-lines (h1 h2)
4743   "Sort threads by root article Lines header."
4744   (gnus-article-sort-by-lines
4745    (gnus-thread-header h1) (gnus-thread-header h2)))
4746
4747 (defsubst gnus-article-sort-by-chars (h1 h2)
4748   "Sort articles by octet length."
4749   (< (mail-header-chars h1)
4750      (mail-header-chars h2)))
4751
4752 (defun gnus-thread-sort-by-chars (h1 h2)
4753   "Sort threads by root article octet length."
4754   (gnus-article-sort-by-chars
4755    (gnus-thread-header h1) (gnus-thread-header h2)))
4756
4757 (defsubst gnus-article-sort-by-author (h1 h2)
4758   "Sort articles by root author."
4759   (string-lessp
4760    (let ((extract (funcall
4761                    gnus-extract-address-components
4762                    (mail-header-from h1))))
4763      (or (car extract) (cadr extract) ""))
4764    (let ((extract (funcall
4765                    gnus-extract-address-components
4766                    (mail-header-from h2))))
4767      (or (car extract) (cadr extract) ""))))
4768
4769 (defun gnus-thread-sort-by-author (h1 h2)
4770   "Sort threads by root author."
4771   (gnus-article-sort-by-author
4772    (gnus-thread-header h1)  (gnus-thread-header h2)))
4773
4774 (defsubst gnus-article-sort-by-recipient (h1 h2)
4775   "Sort articles by recipient."
4776   (string-lessp
4777    (let ((extract (funcall
4778                    gnus-extract-address-components
4779                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4780      (or (car extract) (cadr extract)))
4781    (let ((extract (funcall
4782                    gnus-extract-address-components
4783                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4784      (or (car extract) (cadr extract)))))
4785
4786 (defun gnus-thread-sort-by-recipient (h1 h2)
4787   "Sort threads by root recipient."
4788   (gnus-article-sort-by-recipient
4789    (gnus-thread-header h1) (gnus-thread-header h2)))
4790
4791 (defsubst gnus-article-sort-by-subject (h1 h2)
4792   "Sort articles by root subject."
4793   (string-lessp
4794    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4795    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4796
4797 (defun gnus-thread-sort-by-subject (h1 h2)
4798   "Sort threads by root subject."
4799   (gnus-article-sort-by-subject
4800    (gnus-thread-header h1) (gnus-thread-header h2)))
4801
4802 (defsubst gnus-article-sort-by-date (h1 h2)
4803   "Sort articles by root article date."
4804   (time-less-p
4805    (gnus-date-get-time (mail-header-date h1))
4806    (gnus-date-get-time (mail-header-date h2))))
4807
4808 (defun gnus-thread-sort-by-date (h1 h2)
4809   "Sort threads by root article date."
4810   (gnus-article-sort-by-date
4811    (gnus-thread-header h1) (gnus-thread-header h2)))
4812
4813 (defsubst gnus-article-sort-by-score (h1 h2)
4814   "Sort articles by root article score.
4815 Unscored articles will be counted as having a score of zero."
4816   (> (or (cdr (assq (mail-header-number h1)
4817                     gnus-newsgroup-scored))
4818          gnus-summary-default-score 0)
4819      (or (cdr (assq (mail-header-number h2)
4820                     gnus-newsgroup-scored))
4821          gnus-summary-default-score 0)))
4822
4823 (defun gnus-thread-sort-by-score (h1 h2)
4824   "Sort threads by root article score."
4825   (gnus-article-sort-by-score
4826    (gnus-thread-header h1) (gnus-thread-header h2)))
4827
4828 (defun gnus-thread-sort-by-total-score (h1 h2)
4829   "Sort threads by the sum of all scores in the thread.
4830 Unscored articles will be counted as having a score of zero."
4831   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4832
4833 (defun gnus-thread-total-score (thread)
4834   ;; This function find the total score of THREAD.
4835   (cond
4836    ((null thread)
4837     0)
4838    ((consp thread)
4839     (if (stringp (car thread))
4840         (apply gnus-thread-score-function 0
4841                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4842       (gnus-thread-total-score-1 thread)))
4843    (t
4844     (gnus-thread-total-score-1 (list thread)))))
4845
4846 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4847   "Sort threads such that the thread with the most recently arrived article comes first."
4848   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4849
4850 (defun gnus-thread-highest-number (thread)
4851   "Return the highest article number in THREAD."
4852   (apply 'max (mapcar (lambda (header)
4853                         (mail-header-number header))
4854                       (message-flatten-list thread))))
4855
4856 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4857   "Sort threads such that the thread with the most recently dated article comes first."
4858   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4859
4860 (defun gnus-thread-latest-date (thread)
4861   "Return the highest article date in THREAD."
4862   (let ((previous-time 0))
4863     (apply 'max
4864            (mapcar
4865             (lambda (header)
4866               (setq previous-time
4867                     (condition-case ()
4868                         (time-to-seconds (mail-header-parse-date
4869                                           (mail-header-date header)))
4870                       (error previous-time))))
4871             (sort
4872              (message-flatten-list thread)
4873              (lambda (h1 h2)
4874                (< (mail-header-number h1)
4875                   (mail-header-number h2))))))))
4876
4877 (defun gnus-thread-total-score-1 (root)
4878   ;; This function find the total score of the thread below ROOT.
4879   (setq root (car root))
4880   (apply gnus-thread-score-function
4881          (or (append
4882               (mapcar 'gnus-thread-total-score
4883                       (cdr (gnus-id-to-thread (mail-header-id root))))
4884               (when (> (mail-header-number root) 0)
4885                 (list (or (cdr (assq (mail-header-number root)
4886                                      gnus-newsgroup-scored))
4887                           gnus-summary-default-score 0))))
4888              (list gnus-summary-default-score)
4889              '(0))))
4890
4891 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4892 (defvar gnus-tmp-prev-subject nil)
4893 (defvar gnus-tmp-false-parent nil)
4894 (defvar gnus-tmp-root-expunged nil)
4895 (defvar gnus-tmp-dummy-line nil)
4896
4897 (eval-when-compile (defvar gnus-tmp-header))
4898 (defun gnus-extra-header (type &optional header)
4899   "Return the extra header of TYPE."
4900   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4901       ""))
4902
4903 (defvar gnus-tmp-thread-tree-header-string "")
4904
4905 (defcustom gnus-sum-thread-tree-root "> "
4906   "With %B spec, used for the root of a thread.
4907 If nil, use subject instead."
4908   :version "22.1"
4909   :type '(radio (const :format "%v  " nil) string)
4910   :group 'gnus-thread)
4911
4912 (defcustom gnus-sum-thread-tree-false-root "> "
4913   "With %B spec, used for a false root of a thread.
4914 If nil, use subject instead."
4915   :version "22.1"
4916   :type '(radio (const :format "%v  " nil) string)
4917   :group 'gnus-thread)
4918
4919 (defcustom gnus-sum-thread-tree-single-indent ""
4920   "With %B spec, used for a thread with just one message.
4921 If nil, use subject instead."
4922   :version "22.1"
4923   :type '(radio (const :format "%v  " nil) string)
4924   :group 'gnus-thread)
4925
4926 (defcustom gnus-sum-thread-tree-vertical "| "
4927   "With %B spec, used for drawing a vertical line."
4928   :version "22.1"
4929   :type 'string
4930   :group 'gnus-thread)
4931
4932 (defcustom gnus-sum-thread-tree-indent "  "
4933   "With %B spec, used for indenting."
4934   :version "22.1"
4935   :type 'string
4936   :group 'gnus-thread)
4937
4938 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4939   "With %B spec, used for a leaf with brothers."
4940   :version "22.1"
4941   :type 'string
4942   :group 'gnus-thread)
4943
4944 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4945   "With %B spec, used for a leaf without brothers."
4946   :version "22.1"
4947   :type 'string
4948   :group 'gnus-thread)
4949
4950 (defun gnus-summary-prepare-threads (threads)
4951   "Prepare summary buffer from THREADS and indentation LEVEL.
4952 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4953 or a straight list of headers."
4954   (gnus-message 7 "Generating summary...")
4955
4956   (setq gnus-newsgroup-threads threads)
4957   (beginning-of-line)
4958
4959   (let ((gnus-tmp-level 0)
4960         (default-score (or gnus-summary-default-score 0))
4961         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4962         (building-line-count gnus-summary-display-while-building)
4963         (building-count (integerp gnus-summary-display-while-building))
4964         thread number subject stack state gnus-tmp-gathered beg-match
4965         new-roots gnus-tmp-new-adopts thread-end simp-subject
4966         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4967         gnus-tmp-replied gnus-tmp-subject-or-nil
4968         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4969         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4970         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4971         tree-stack)
4972
4973     (setq gnus-tmp-prev-subject nil
4974           gnus-tmp-thread-tree-header-string "")
4975
4976     (if (vectorp (car threads))
4977         ;; If this is a straight (sic) list of headers, then a
4978         ;; threaded summary display isn't required, so we just create
4979         ;; an unthreaded one.
4980         (gnus-summary-prepare-unthreaded threads)
4981
4982       ;; Do the threaded display.
4983
4984       (if gnus-summary-display-while-building
4985           (switch-to-buffer (buffer-name)))
4986       (while (or threads stack gnus-tmp-new-adopts new-roots)
4987
4988         (if (and (= gnus-tmp-level 0)
4989                  (or (not stack)
4990                      (= (caar stack) 0))
4991                  (not gnus-tmp-false-parent)
4992                  (or gnus-tmp-new-adopts new-roots))
4993             (if gnus-tmp-new-adopts
4994                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4995                       thread (list (car gnus-tmp-new-adopts))
4996                       gnus-tmp-header (caar thread)
4997                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4998               (when new-roots
4999                 (setq thread (list (car new-roots))
5000                       gnus-tmp-header (caar thread)
5001                       new-roots (cdr new-roots))))
5002
5003           (if threads
5004               ;; If there are some threads, we do them before the
5005               ;; threads on the stack.
5006               (setq thread threads
5007                     gnus-tmp-header (caar thread))
5008             ;; There were no current threads, so we pop something off
5009             ;; the stack.
5010             (setq state (car stack)
5011                   gnus-tmp-level (car state)
5012                   tree-stack (cadr state)
5013                   thread (caddr state)
5014                   stack (cdr stack)
5015                   gnus-tmp-header (caar thread))))
5016
5017         (setq gnus-tmp-false-parent nil)
5018         (setq gnus-tmp-root-expunged nil)
5019         (setq thread-end nil)
5020
5021         (if (stringp gnus-tmp-header)
5022             ;; The header is a dummy root.
5023             (cond
5024              ((eq gnus-summary-make-false-root 'adopt)
5025               ;; We let the first article adopt the rest.
5026               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5027                                                (cddar thread)))
5028               (setq gnus-tmp-gathered
5029                     (nconc (mapcar
5030                             (lambda (h) (mail-header-number (car h)))
5031                             (cddar thread))
5032                            gnus-tmp-gathered))
5033               (setq thread (cons (list (caar thread)
5034                                        (cadar thread))
5035                                  (cdr thread)))
5036               (setq gnus-tmp-level -1
5037                     gnus-tmp-false-parent t))
5038              ((eq gnus-summary-make-false-root 'empty)
5039               ;; We print adopted articles with empty subject fields.
5040               (setq gnus-tmp-gathered
5041                     (nconc (mapcar
5042                             (lambda (h) (mail-header-number (car h)))
5043                             (cddar thread))
5044                            gnus-tmp-gathered))
5045               (setq gnus-tmp-level -1))
5046              ((eq gnus-summary-make-false-root 'dummy)
5047               ;; We remember that we probably want to output a dummy
5048               ;; root.
5049               (setq gnus-tmp-dummy-line gnus-tmp-header)
5050               (setq gnus-tmp-prev-subject gnus-tmp-header))
5051              (t
5052               ;; We do not make a root for the gathered
5053               ;; sub-threads at all.
5054               (setq gnus-tmp-level -1)))
5055
5056           (setq number (mail-header-number gnus-tmp-header)
5057                 subject (mail-header-subject gnus-tmp-header)
5058                 simp-subject (gnus-simplify-subject-fully subject))
5059
5060           (cond
5061            ;; If the thread has changed subject, we might want to make
5062            ;; this subthread into a root.
5063            ((and (null gnus-thread-ignore-subject)
5064                  (not (zerop gnus-tmp-level))
5065                  gnus-tmp-prev-subject
5066                  (not (string= gnus-tmp-prev-subject simp-subject)))
5067             (setq new-roots (nconc new-roots (list (car thread)))
5068                   thread-end t
5069                   gnus-tmp-header nil))
5070            ;; If the article lies outside the current limit,
5071            ;; then we do not display it.
5072            ((not (memq number gnus-newsgroup-limit))
5073             (setq gnus-tmp-gathered
5074                   (nconc (mapcar
5075                           (lambda (h) (mail-header-number (car h)))
5076                           (cdar thread))
5077                          gnus-tmp-gathered))
5078             (setq gnus-tmp-new-adopts (if (cdar thread)
5079                                           (append gnus-tmp-new-adopts
5080                                                   (cdar thread))
5081                                         gnus-tmp-new-adopts)
5082                   thread-end t
5083                   gnus-tmp-header nil)
5084             (when (zerop gnus-tmp-level)
5085               (setq gnus-tmp-root-expunged t)))
5086            ;; Perhaps this article is to be marked as read?
5087            ((and gnus-summary-mark-below
5088                  (< (or (cdr (assq number gnus-newsgroup-scored))
5089                         default-score)
5090                     gnus-summary-mark-below)
5091                  ;; Don't touch sparse articles.
5092                  (not (gnus-summary-article-sparse-p number))
5093                  (not (gnus-summary-article-ancient-p number)))
5094             (setq gnus-newsgroup-unreads
5095                   (delq number gnus-newsgroup-unreads))
5096             (if gnus-newsgroup-auto-expire
5097                 (setq gnus-newsgroup-expirable
5098                       (gnus-add-to-sorted-list
5099                        gnus-newsgroup-expirable number))
5100               (push (cons number gnus-low-score-mark)
5101                     gnus-newsgroup-reads))))
5102
5103           (when gnus-tmp-header
5104             ;; We may have an old dummy line to output before this
5105             ;; article.
5106             (when (and gnus-tmp-dummy-line
5107                        (gnus-subject-equal
5108                         gnus-tmp-dummy-line
5109                         (mail-header-subject gnus-tmp-header)))
5110               (gnus-summary-insert-dummy-line
5111                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5112               (setq gnus-tmp-dummy-line nil))
5113
5114             ;; Compute the mark.
5115             (setq gnus-tmp-unread (gnus-article-mark number))
5116
5117             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5118                                   gnus-tmp-header gnus-tmp-level)
5119                   gnus-newsgroup-data)
5120
5121             ;; Actually insert the line.
5122             (setq
5123              gnus-tmp-subject-or-nil
5124              (cond
5125               ((and gnus-thread-ignore-subject
5126                     gnus-tmp-prev-subject
5127                     (not (string= gnus-tmp-prev-subject simp-subject)))
5128                subject)
5129               ((zerop gnus-tmp-level)
5130                (if (and (eq gnus-summary-make-false-root 'empty)
5131                         (memq number gnus-tmp-gathered)
5132                         gnus-tmp-prev-subject
5133                         (string= gnus-tmp-prev-subject simp-subject))
5134                    gnus-summary-same-subject
5135                  subject))
5136               (t gnus-summary-same-subject)))
5137             (if (and (eq gnus-summary-make-false-root 'adopt)
5138                      (= gnus-tmp-level 1)
5139                      (memq number gnus-tmp-gathered))
5140                 (setq gnus-tmp-opening-bracket ?\<
5141                       gnus-tmp-closing-bracket ?\>)
5142               (setq gnus-tmp-opening-bracket ?\[
5143                     gnus-tmp-closing-bracket ?\]))
5144             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5145                 (gnus-make-thread-indent-array
5146                  (max (* 2 (length gnus-thread-indent-array))
5147                       gnus-tmp-level)))
5148             (setq
5149              gnus-tmp-indentation
5150              (aref gnus-thread-indent-array gnus-tmp-level)
5151              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5152              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5153                                 gnus-summary-default-score 0)
5154              gnus-tmp-score-char
5155              (if (or (null gnus-summary-default-score)
5156                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5157                          gnus-summary-zcore-fuzz))
5158                  ?                      ;Whitespace
5159                (if (< gnus-tmp-score gnus-summary-default-score)
5160                    gnus-score-below-mark gnus-score-over-mark))
5161              gnus-tmp-replied
5162              (cond ((memq number gnus-newsgroup-processable)
5163                     gnus-process-mark)
5164                    ((memq number gnus-newsgroup-cached)
5165                     gnus-cached-mark)
5166                    ((memq number gnus-newsgroup-replied)
5167                     gnus-replied-mark)
5168                    ((memq number gnus-newsgroup-forwarded)
5169                     gnus-forwarded-mark)
5170                    ((memq number gnus-newsgroup-saved)
5171                     gnus-saved-mark)
5172                    ((memq number gnus-newsgroup-recent)
5173                     gnus-recent-mark)
5174                    ((memq number gnus-newsgroup-unseen)
5175                     gnus-unseen-mark)
5176                    (t gnus-no-mark))
5177              gnus-tmp-downloaded
5178              (cond ((memq number gnus-newsgroup-undownloaded)
5179                     gnus-undownloaded-mark)
5180                    (gnus-newsgroup-agentized
5181                     gnus-downloaded-mark)
5182                    (t
5183                     gnus-no-mark))
5184              gnus-tmp-from (mail-header-from gnus-tmp-header)
5185              gnus-tmp-name
5186              (cond
5187               ((string-match "<[^>]+> *$" gnus-tmp-from)
5188                (setq beg-match (match-beginning 0))
5189                (or (and (string-match "^\".+\"" gnus-tmp-from)
5190                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5191                    (substring gnus-tmp-from 0 beg-match)))
5192               ((string-match "(.+)" gnus-tmp-from)
5193                (substring gnus-tmp-from
5194                           (1+ (match-beginning 0)) (1- (match-end 0))))
5195               (t gnus-tmp-from))
5196
5197              ;; Do the %B string
5198              gnus-tmp-thread-tree-header-string
5199              (cond
5200               ((not gnus-show-threads) "")
5201               ((zerop gnus-tmp-level)
5202                (cond ((cdar thread)
5203                       (or gnus-sum-thread-tree-root subject))
5204                      (gnus-tmp-new-adopts
5205                       (or gnus-sum-thread-tree-false-root subject))
5206                      (t
5207                       (or gnus-sum-thread-tree-single-indent subject))))
5208               (t
5209                (concat (apply 'concat
5210                               (mapcar (lambda (item)
5211                                         (if (= item 1)
5212                                             gnus-sum-thread-tree-vertical
5213                                           gnus-sum-thread-tree-indent))
5214                                       (cdr (reverse tree-stack))))
5215                        (if (nth 1 thread)
5216                            gnus-sum-thread-tree-leaf-with-other
5217                          gnus-sum-thread-tree-single-leaf)))))
5218             (when (string= gnus-tmp-name "")
5219               (setq gnus-tmp-name gnus-tmp-from))
5220             (unless (numberp gnus-tmp-lines)
5221               (setq gnus-tmp-lines -1))
5222             (if (= gnus-tmp-lines -1)
5223                 (setq gnus-tmp-lines "?")
5224               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5225               (gnus-put-text-property
5226              (point)
5227              (progn (eval gnus-summary-line-format-spec) (point))
5228                'gnus-number number)
5229             (when gnus-visual-p
5230               (forward-line -1)
5231               (gnus-run-hooks 'gnus-summary-update-hook)
5232               (forward-line 1))
5233
5234             (setq gnus-tmp-prev-subject simp-subject)))
5235
5236         (when (nth 1 thread)
5237           (push (list (max 0 gnus-tmp-level)
5238                       (copy-sequence tree-stack)
5239                       (nthcdr 1 thread))
5240                 stack))
5241         (push (if (nth 1 thread) 1 0) tree-stack)
5242         (incf gnus-tmp-level)
5243         (setq threads (if thread-end nil (cdar thread)))
5244         (if gnus-summary-display-while-building
5245             (if building-count
5246                 (progn
5247                   ;; use a set frequency
5248                   (setq building-line-count (1- building-line-count))
5249                   (when (= building-line-count 0)
5250                     (sit-for 0)
5251                     (setq building-line-count
5252                           gnus-summary-display-while-building)))
5253               ;; always
5254               (sit-for 0)))
5255         (unless threads
5256           (setq gnus-tmp-level 0)))))
5257   (gnus-message 7 "Generating summary...done"))
5258
5259 (defun gnus-summary-prepare-unthreaded (headers)
5260   "Generate an unthreaded summary buffer based on HEADERS."
5261   (let (header number mark)
5262
5263     (beginning-of-line)
5264
5265     (while headers
5266       ;; We may have to root out some bad articles...
5267       (when (memq (setq number (mail-header-number
5268                                 (setq header (pop headers))))
5269                   gnus-newsgroup-limit)
5270         ;; Mark article as read when it has a low score.
5271         (when (and gnus-summary-mark-below
5272                    (< (or (cdr (assq number gnus-newsgroup-scored))
5273                           gnus-summary-default-score 0)
5274                       gnus-summary-mark-below)
5275                    (not (gnus-summary-article-ancient-p number)))
5276           (setq gnus-newsgroup-unreads
5277                 (delq number gnus-newsgroup-unreads))
5278           (if gnus-newsgroup-auto-expire
5279               (push number gnus-newsgroup-expirable)
5280             (push (cons number gnus-low-score-mark)
5281                   gnus-newsgroup-reads)))
5282
5283         (setq mark (gnus-article-mark number))
5284         (push (gnus-data-make number mark (1+ (point)) header 0)
5285               gnus-newsgroup-data)
5286         (gnus-summary-insert-line
5287          header 0 number
5288          (memq number gnus-newsgroup-undownloaded)
5289          mark (memq number gnus-newsgroup-replied)
5290          (memq number gnus-newsgroup-expirable)
5291          (mail-header-subject header) nil
5292          (cdr (assq number gnus-newsgroup-scored))
5293          (memq number gnus-newsgroup-processable))))))
5294
5295 (defun gnus-summary-remove-list-identifiers ()
5296   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5297   (let ((regexp (if (consp gnus-list-identifiers)
5298                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5299                   gnus-list-identifiers))
5300         changed subject)
5301     (when regexp
5302       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5303       (dolist (header gnus-newsgroup-headers)
5304         (setq subject (mail-header-subject header)
5305               changed nil)
5306         (while (string-match regexp subject)
5307           (setq subject
5308                 (concat (substring subject 0 (match-beginning 1))
5309                         (substring subject (match-end 0)))
5310                 changed t))
5311         (when changed
5312           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5313             (setq subject
5314                   (concat (substring subject 0 (match-beginning 1))
5315                           (substring subject (match-end 1)))))
5316           (mail-header-set-subject header subject))))))
5317
5318 (defun gnus-fetch-headers (articles)
5319   "Fetch headers of ARTICLES."
5320   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5321     (gnus-message 5 "Fetching headers for %s..." name)
5322     (prog1
5323         (if (eq 'nov
5324                 (setq gnus-headers-retrieved-by
5325                       (gnus-retrieve-headers
5326                        articles gnus-newsgroup-name
5327                        ;; We might want to fetch old headers, but
5328                        ;; not if there is only 1 article.
5329                        (and (or (and
5330                                  (not (eq gnus-fetch-old-headers 'some))
5331                                  (not (numberp gnus-fetch-old-headers)))
5332                                 (> (length articles) 1))
5333                             gnus-fetch-old-headers))))
5334             (gnus-get-newsgroup-headers-xover
5335              articles nil nil gnus-newsgroup-name t)
5336           (gnus-get-newsgroup-headers))
5337       (gnus-message 5 "Fetching headers for %s...done" name))))
5338
5339 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5340   "Select newsgroup GROUP.
5341 If READ-ALL is non-nil, all articles in the group are selected.
5342 If SELECT-ARTICLES, only select those articles from GROUP."
5343   (let* ((entry (gnus-group-entry group))
5344          ;;!!! Dirty hack; should be removed.
5345          (gnus-summary-ignore-duplicates
5346           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5347               t
5348             gnus-summary-ignore-duplicates))
5349          (info (nth 2 entry))
5350          articles fetched-articles cached)
5351
5352     (unless (gnus-check-server
5353              (set (make-local-variable 'gnus-current-select-method)
5354                   (gnus-find-method-for-group group)))
5355       (error "Couldn't open server"))
5356
5357     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5358         (gnus-activate-group group)     ; Or we can activate it...
5359         (progn                          ; Or we bug out.
5360           (when (equal major-mode 'gnus-summary-mode)
5361             (gnus-kill-buffer (current-buffer)))
5362           (error "Couldn't activate group %s: %s"
5363                  (gnus-group-decoded-name group) (gnus-status-message group))))
5364
5365     (unless (gnus-request-group group t)
5366       (when (equal major-mode 'gnus-summary-mode)
5367         (gnus-kill-buffer (current-buffer)))
5368       (error "Couldn't request group %s: %s"
5369              (gnus-group-decoded-name group) (gnus-status-message group)))
5370
5371     (when gnus-agent
5372       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5373
5374       (setq gnus-summary-use-undownloaded-faces
5375             (gnus-agent-find-parameter
5376              group
5377              'agent-enable-undownloaded-faces)))
5378
5379     (setq gnus-newsgroup-name group
5380           gnus-newsgroup-unselected nil
5381           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5382
5383     (let ((display (gnus-group-find-parameter group 'display)))
5384       (setq gnus-newsgroup-display
5385             (cond
5386              ((not (zerop (or (car-safe read-all) 0)))
5387               ;; The user entered the group with C-u SPC/RET, let's show
5388               ;; all articles.
5389               'gnus-not-ignore)
5390              ((eq display 'all)
5391               'gnus-not-ignore)
5392              ((arrayp display)
5393               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5394              ((numberp display)
5395               ;; The following is probably the "correct" solution, but
5396               ;; it makes Gnus fetch all headers and then limit the
5397               ;; articles (which is slow), so instead we hack the
5398               ;; select-articles parameter instead. -- Simon Josefsson
5399               ;; <jas@kth.se>
5400               ;;
5401               ;; (gnus-byte-compile
5402               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5403               ;;                         display)))))
5404               (setq select-articles
5405                     (gnus-uncompress-range
5406                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5407                              (if (> tmp 0)
5408                                  tmp
5409                                1))
5410                            (cdr (gnus-active group)))))
5411               nil)
5412              (t
5413               nil))))
5414
5415     (gnus-summary-setup-default-charset)
5416
5417     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5418     (when (gnus-virtual-group-p group)
5419       (setq cached gnus-newsgroup-cached))
5420
5421     (setq gnus-newsgroup-unreads
5422           (gnus-sorted-ndifference
5423            (gnus-sorted-ndifference gnus-newsgroup-unreads
5424                                     gnus-newsgroup-marked)
5425            gnus-newsgroup-dormant))
5426
5427     (setq gnus-newsgroup-processable nil)
5428
5429     (gnus-update-read-articles group gnus-newsgroup-unreads)
5430
5431     ;; Adjust and set lists of article marks.
5432     (when info
5433       (gnus-adjust-marked-articles info))
5434     (if (setq articles select-articles)
5435         (setq gnus-newsgroup-unselected
5436               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5437       (setq articles (gnus-articles-to-read group read-all)))
5438
5439     (cond
5440      ((null articles)
5441       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5442       'quit)
5443      ((eq articles 0) nil)
5444      (t
5445       ;; Init the dependencies hash table.
5446       (setq gnus-newsgroup-dependencies
5447             (gnus-make-hashtable (length articles)))
5448       (gnus-set-global-variables)
5449       ;; Retrieve the headers and read them in.
5450
5451       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5452
5453       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5454       (when cached
5455         (setq gnus-newsgroup-cached cached))
5456
5457       ;; Suppress duplicates?
5458       (when gnus-suppress-duplicates
5459         (gnus-dup-suppress-articles))
5460
5461       ;; Set the initial limit.
5462       (setq gnus-newsgroup-limit (copy-sequence articles))
5463       ;; Remove canceled articles from the list of unread articles.
5464       (setq fetched-articles
5465             (mapcar (lambda (headers) (mail-header-number headers))
5466                     gnus-newsgroup-headers))
5467       (setq gnus-newsgroup-articles fetched-articles)
5468       (setq gnus-newsgroup-unreads
5469             (gnus-sorted-nintersection
5470              gnus-newsgroup-unreads fetched-articles))
5471       (gnus-compute-unseen-list)
5472
5473       ;; Removed marked articles that do not exist.
5474       (gnus-update-missing-marks
5475        (gnus-sorted-difference articles fetched-articles))
5476       ;; We might want to build some more threads first.
5477       (when (and gnus-fetch-old-headers
5478                  (eq gnus-headers-retrieved-by 'nov))
5479         (if (eq gnus-fetch-old-headers 'invisible)
5480             (gnus-build-all-threads)
5481           (gnus-build-old-threads)))
5482       ;; Let the Gnus agent mark articles as read.
5483       (when gnus-agent
5484         (gnus-agent-get-undownloaded-list))
5485       ;; Remove list identifiers from subject
5486       (when gnus-list-identifiers
5487         (gnus-summary-remove-list-identifiers))
5488       ;; Check whether auto-expire is to be done in this group.
5489       (setq gnus-newsgroup-auto-expire
5490             (gnus-group-auto-expirable-p group))
5491       ;; Set up the article buffer now, if necessary.
5492       (unless (and gnus-single-article-buffer
5493                    (equal gnus-article-buffer "*Article*"))
5494         (gnus-article-setup-buffer))
5495       ;; First and last article in this newsgroup.
5496       (when gnus-newsgroup-headers
5497         (setq gnus-newsgroup-begin
5498               (mail-header-number (car gnus-newsgroup-headers))
5499               gnus-newsgroup-end
5500               (mail-header-number
5501                (gnus-last-element gnus-newsgroup-headers))))
5502       ;; GROUP is successfully selected.
5503       (or gnus-newsgroup-headers t)))))
5504
5505 (defun gnus-compute-unseen-list ()
5506   ;; The `seen' marks are treated specially.
5507   (if (not gnus-newsgroup-seen)
5508       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5509     (setq gnus-newsgroup-unseen
5510           (gnus-inverse-list-range-intersection
5511            gnus-newsgroup-articles gnus-newsgroup-seen))))
5512
5513 (defun gnus-summary-display-make-predicate (display)
5514   (require 'gnus-agent)
5515   (when (= (length display) 1)
5516     (setq display (car display)))
5517   (unless gnus-summary-display-cache
5518     (dolist (elem (append '((unread . unread)
5519                             (read . read)
5520                             (unseen . unseen))
5521                           gnus-article-mark-lists))
5522       (push (cons (cdr elem)
5523                   (gnus-byte-compile
5524                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5525             gnus-summary-display-cache)))
5526   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5527         (gnus-category-predicate-cache gnus-summary-display-cache))
5528     (gnus-get-predicate display)))
5529
5530 ;; Uses the dynamically bound `number' variable.
5531 (eval-when-compile
5532   (defvar number))
5533 (defun gnus-article-marked-p (type &optional article)
5534   (let ((article (or article number)))
5535     (cond
5536      ((eq type 'tick)
5537       (memq article gnus-newsgroup-marked))
5538      ((eq type 'spam)
5539       (memq article gnus-newsgroup-spam-marked))
5540      ((eq type 'unsend)
5541       (memq article gnus-newsgroup-unsendable))
5542      ((eq type 'undownload)
5543       (memq article gnus-newsgroup-undownloaded))
5544      ((eq type 'download)
5545       (memq article gnus-newsgroup-downloadable))
5546      ((eq type 'unread)
5547       (memq article gnus-newsgroup-unreads))
5548      ((eq type 'read)
5549       (memq article gnus-newsgroup-reads))
5550      ((eq type 'dormant)
5551       (memq article gnus-newsgroup-dormant) )
5552      ((eq type 'expire)
5553       (memq article gnus-newsgroup-expirable))
5554      ((eq type 'reply)
5555       (memq article gnus-newsgroup-replied))
5556      ((eq type 'killed)
5557       (memq article gnus-newsgroup-killed))
5558      ((eq type 'bookmark)
5559       (assq article gnus-newsgroup-bookmarks))
5560      ((eq type 'score)
5561       (assq article gnus-newsgroup-scored))
5562      ((eq type 'save)
5563       (memq article gnus-newsgroup-saved))
5564      ((eq type 'cache)
5565       (memq article gnus-newsgroup-cached))
5566      ((eq type 'forward)
5567       (memq article gnus-newsgroup-forwarded))
5568      ((eq type 'seen)
5569       (not (memq article gnus-newsgroup-unseen)))
5570      ((eq type 'recent)
5571       (memq article gnus-newsgroup-recent))
5572      (t t))))
5573
5574 (defun gnus-articles-to-read (group &optional read-all)
5575   "Find out what articles the user wants to read."
5576   (let* ((articles
5577           ;; Select all articles if `read-all' is non-nil, or if there
5578           ;; are no unread articles.
5579           (if (or read-all
5580                   (and (zerop (length gnus-newsgroup-marked))
5581                        (zerop (length gnus-newsgroup-unreads)))
5582                   ;; Fetch all if the predicate is non-nil.
5583                   gnus-newsgroup-display)
5584               ;; We want to select the headers for all the articles in
5585               ;; the group, so we select either all the active
5586               ;; articles in the group, or (if that's nil), the
5587               ;; articles in the cache.
5588               (or
5589                (gnus-uncompress-range (gnus-active group))
5590                (gnus-cache-articles-in-group group))
5591             ;; Select only the "normal" subset of articles.
5592             (gnus-sorted-nunion
5593              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5594              gnus-newsgroup-unreads)))
5595          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5596          (scored (length scored-list))
5597          (number (length articles))
5598          (marked (+ (length gnus-newsgroup-marked)
5599                     (length gnus-newsgroup-dormant)))
5600          (select
5601           (cond
5602            ((numberp read-all)
5603             read-all)
5604            ((numberp gnus-newsgroup-display)
5605             gnus-newsgroup-display)
5606            (t
5607             (condition-case ()
5608                 (cond
5609                  ((and (or (<= scored marked) (= scored number))
5610                        (numberp gnus-large-newsgroup)
5611                        (> number gnus-large-newsgroup))
5612                   (let* ((cursor-in-echo-area nil)
5613                          (initial (gnus-parameter-large-newsgroup-initial
5614                                    gnus-newsgroup-name))
5615                          (input
5616                           (read-string
5617                            (format
5618                             "How many articles from %s (%s %d): "
5619                             (gnus-limit-string
5620                              (gnus-group-decoded-name gnus-newsgroup-name)
5621                              35)
5622                             (if initial "max" "default")
5623                             number)
5624                            (if initial
5625                                (cons (number-to-string initial)
5626                                      0)))))
5627                     (if (string-match "^[ \t]*$" input) number input)))
5628                  ((and (> scored marked) (< scored number)
5629                        (> (- scored number) 20))
5630                   (let ((input
5631                          (read-string
5632                           (format "%s %s (%d scored, %d total): "
5633                                   "How many articles from"
5634                                   (gnus-group-decoded-name group)
5635                                   scored number))))
5636                     (if (string-match "^[ \t]*$" input)
5637                         number input)))
5638                  (t number))
5639               (quit
5640                (message "Quit getting the articles to read")
5641                nil))))))
5642     (setq select (if (stringp select) (string-to-number select) select))
5643     (if (or (null select) (zerop select))
5644         select
5645       (if (and (not (zerop scored)) (<= (abs select) scored))
5646           (progn
5647             (setq articles (sort scored-list '<))
5648             (setq number (length articles)))
5649         (setq articles (copy-sequence articles)))
5650
5651       (when (< (abs select) number)
5652         (if (< select 0)
5653             ;; Select the N oldest articles.
5654             (setcdr (nthcdr (1- (abs select)) articles) nil)
5655           ;; Select the N most recent articles.
5656           (setq articles (nthcdr (- number select) articles))))
5657       (setq gnus-newsgroup-unselected
5658             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5659       (when gnus-alter-articles-to-read-function
5660         (setq articles
5661               (sort
5662                (funcall gnus-alter-articles-to-read-function
5663                         gnus-newsgroup-name articles)
5664                '<)))
5665       articles)))
5666
5667 (defun gnus-killed-articles (killed articles)
5668   (let (out)
5669     (while articles
5670       (when (inline (gnus-member-of-range (car articles) killed))
5671         (push (car articles) out))
5672       (setq articles (cdr articles)))
5673     out))
5674
5675 (defun gnus-uncompress-marks (marks)
5676   "Uncompress the mark ranges in MARKS."
5677   (let ((uncompressed '(score bookmark))
5678         out)
5679     (while marks
5680       (if (memq (caar marks) uncompressed)
5681           (push (car marks) out)
5682         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5683       (setq marks (cdr marks)))
5684     out))
5685
5686 (defun gnus-article-mark-to-type (mark)
5687   "Return the type of MARK."
5688   (or (cadr (assq mark gnus-article-special-mark-lists))
5689       'list))
5690
5691 (defun gnus-article-unpropagatable-p (mark)
5692   "Return whether MARK should be propagated to back end."
5693   (memq mark gnus-article-unpropagated-mark-lists))
5694
5695 (defun gnus-adjust-marked-articles (info)
5696   "Set all article lists and remove all marks that are no longer valid."
5697   (let* ((marked-lists (gnus-info-marks info))
5698          (active (gnus-active (gnus-info-group info)))
5699          (min (car active))
5700          (max (cdr active))
5701          (types gnus-article-mark-lists)
5702          marks var articles article mark mark-type
5703          bgn end)
5704
5705     (dolist (marks marked-lists)
5706       (setq mark (car marks)
5707             mark-type (gnus-article-mark-to-type mark)
5708             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5709
5710       ;; We set the variable according to the type of the marks list,
5711       ;; and then adjust the marks to a subset of the active articles.
5712       (cond
5713        ;; Adjust "simple" lists - compressed yet unsorted
5714        ((eq mark-type 'list)
5715         ;; Simultaneously uncompress and clip to active range
5716         ;; See gnus-uncompress-range for a description of possible marks
5717         (let (l lh)
5718           (if (not (cadr marks))
5719               (set var nil)
5720             (setq articles (if (numberp (cddr marks))
5721                                (list (cdr marks))
5722                              (cdr marks))
5723                   lh (cons nil nil)
5724                   l lh)
5725
5726             (while (setq article (pop articles))
5727               (cond ((consp article)
5728                      (setq bgn (max (car article) min)
5729                            end (min (cdr article) max))
5730                      (while (<= bgn end)
5731                        (setq l (setcdr l (cons bgn nil))
5732                              bgn (1+ bgn))))
5733                     ((and (<= min article)
5734                           (>= max article))
5735                      (setq l (setcdr l (cons article nil))))))
5736             (set var (cdr lh)))))
5737        ;; Adjust assocs.
5738        ((eq mark-type 'tuple)
5739         (set var (setq articles (cdr marks)))
5740         (when (not (listp (cdr (symbol-value var))))
5741           (set var (list (symbol-value var))))
5742         (when (not (listp (cdr articles)))
5743           (setq articles (list articles)))
5744         (while articles
5745           (when (or (not (consp (setq article (pop articles))))
5746                     (< (car article) min)
5747                     (> (car article) max))
5748             (set var (delq article (symbol-value var))))))
5749        ;; Adjust ranges (sloppily).
5750        ((eq mark-type 'range)
5751         (cond
5752          ((eq mark 'seen)
5753           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5754           ;; It should be (seen (NUM1 . NUM2)).
5755           (when (numberp (cddr marks))
5756             (setcdr marks (list (cdr marks))))
5757           (setq articles (cdr marks))
5758           (while (and articles
5759                       (or (and (consp (car articles))
5760                                (> min (cdar articles)))
5761                           (and (numberp (car articles))
5762                                (> min (car articles)))))
5763             (pop articles))
5764           (set var articles))))))))
5765
5766 (defun gnus-update-missing-marks (missing)
5767   "Go through the list of MISSING articles and remove them from the mark lists."
5768   (when missing
5769     (let (var m)
5770       ;; Go through all types.
5771       (dolist (elem gnus-article-mark-lists)
5772         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5773           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5774           (when (symbol-value var)
5775             ;; This list has articles.  So we delete all missing
5776             ;; articles from it.
5777             (setq m missing)
5778             (while m
5779               (set var (delq (pop m) (symbol-value var))))))))))
5780
5781 (defun gnus-update-marks ()
5782   "Enter the various lists of marked articles into the newsgroup info list."
5783   (let ((types gnus-article-mark-lists)
5784         (info (gnus-get-info gnus-newsgroup-name))
5785         type list newmarked symbol delta-marks)
5786     (when info
5787       ;; Add all marks lists to the list of marks lists.
5788       (while (setq type (pop types))
5789         (setq list (symbol-value
5790                     (setq symbol
5791                           (intern (format "gnus-newsgroup-%s" (car type))))))
5792
5793         (when list
5794           ;; Get rid of the entries of the articles that have the
5795           ;; default score.
5796           (when (and (eq (cdr type) 'score)
5797                      gnus-save-score
5798                      list)
5799             (let* ((arts list)
5800                    (prev (cons nil list))
5801                    (all prev))
5802               (while arts
5803                 (if (or (not (consp (car arts)))
5804                         (= (cdar arts) gnus-summary-default-score))
5805                     (setcdr prev (cdr arts))
5806                   (setq prev arts))
5807                 (setq arts (cdr arts)))
5808               (setq list (cdr all)))))
5809
5810         (when (eq (cdr type) 'seen)
5811           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5812
5813         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5814           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5815
5816         (when (and (gnus-check-backend-function
5817                     'request-set-mark gnus-newsgroup-name)
5818                    (not (gnus-article-unpropagatable-p (cdr type))))
5819           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5820                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5821                  (add (gnus-remove-from-range
5822                        (gnus-copy-sequence list) old)))
5823             (when add
5824               (push (list add 'add (list (cdr type))) delta-marks))
5825             (when del
5826               (push (list del 'del (list (cdr type))) delta-marks))))
5827
5828         (when list
5829           (push (cons (cdr type) list) newmarked)))
5830
5831       (when delta-marks
5832         (unless (gnus-check-group gnus-newsgroup-name)
5833           (error "Can't open server for %s" gnus-newsgroup-name))
5834         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5835
5836       ;; Enter these new marks into the info of the group.
5837       (if (nthcdr 3 info)
5838           (setcar (nthcdr 3 info) newmarked)
5839         ;; Add the marks lists to the end of the info.
5840         (when newmarked
5841           (setcdr (nthcdr 2 info) (list newmarked))))
5842
5843       ;; Cut off the end of the info if there's nothing else there.
5844       (let ((i 5))
5845         (while (and (> i 2)
5846                     (not (nth i info)))
5847           (when (nthcdr (decf i) info)
5848             (setcdr (nthcdr i info) nil)))))))
5849
5850 (defun gnus-set-mode-line (where)
5851   "Set the mode line of the article or summary buffers.
5852 If WHERE is `summary', the summary mode line format will be used."
5853   ;; Is this mode line one we keep updated?
5854   (when (and (memq where gnus-updated-mode-lines)
5855              (symbol-value
5856               (intern (format "gnus-%s-mode-line-format-spec" where))))
5857     (let (mode-string)
5858       (save-excursion
5859         ;; We evaluate this in the summary buffer since these
5860         ;; variables are buffer-local to that buffer.
5861         (set-buffer gnus-summary-buffer)
5862        ;; We bind all these variables that are used in the `eval' form
5863         ;; below.
5864         (let* ((mformat (symbol-value
5865                          (intern
5866                           (format "gnus-%s-mode-line-format-spec" where))))
5867                (gnus-tmp-group-name (gnus-mode-string-quote
5868                                      (gnus-group-decoded-name
5869                                       gnus-newsgroup-name)))
5870                (gnus-tmp-article-number (or gnus-current-article 0))
5871                (gnus-tmp-unread gnus-newsgroup-unreads)
5872                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5873                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5874                (gnus-tmp-unread-and-unselected
5875                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5876                             (zerop gnus-tmp-unselected))
5877                        "")
5878                       ((zerop gnus-tmp-unselected)
5879                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5880                       (t (format "{%d(+%d) more}"
5881                                  gnus-tmp-unread-and-unticked
5882                                  gnus-tmp-unselected))))
5883                (gnus-tmp-subject
5884                 (if (and gnus-current-headers
5885                          (vectorp gnus-current-headers))
5886                     (gnus-mode-string-quote
5887                      (mail-header-subject gnus-current-headers))
5888                   ""))
5889                bufname-length max-len
5890                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5891           (setq mode-string (eval mformat))
5892           (setq bufname-length (if (string-match "%b" mode-string)
5893                                    (- (length
5894                                        (buffer-name
5895                                         (if (eq where 'summary)
5896                                             nil
5897                                           (get-buffer gnus-article-buffer))))
5898                                       2)
5899                                  0))
5900           (setq max-len (max 4 (if gnus-mode-non-string-length
5901                                    (- (window-width)
5902                                       gnus-mode-non-string-length
5903                                       bufname-length)
5904                                  (length mode-string))))
5905           ;; We might have to chop a bit of the string off...
5906           (when (> (length mode-string) max-len)
5907             (setq mode-string
5908                   (concat (truncate-string-to-width mode-string (- max-len 3))
5909                           "...")))
5910           ;; Pad the mode string a bit.
5911           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5912       ;; Update the mode line.
5913       (setq mode-line-buffer-identification
5914             (gnus-mode-line-buffer-identification (list mode-string)))
5915       (set-buffer-modified-p t))))
5916
5917 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5918   "Go through the HEADERS list and add all Xrefs to a hash table.
5919 The resulting hash table is returned, or nil if no Xrefs were found."
5920   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5921          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5922          (xref-hashtb (gnus-make-hashtable))
5923          start group entry number xrefs header)
5924     (while headers
5925       (setq header (pop headers))
5926       (when (and (setq xrefs (mail-header-xref header))
5927                  (not (memq (setq number (mail-header-number header))
5928                             unreads)))
5929         (setq start 0)
5930         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5931           (setq start (match-end 0))
5932           (setq group (if prefix
5933                           (concat prefix (substring xrefs (match-beginning 1)
5934                                                     (match-end 1)))
5935                         (substring xrefs (match-beginning 1) (match-end 1))))
5936           (setq number
5937                 (string-to-number (substring xrefs (match-beginning 2)
5938                                           (match-end 2))))
5939           (if (setq entry (gnus-gethash group xref-hashtb))
5940               (setcdr entry (cons number (cdr entry)))
5941             (gnus-sethash group (cons number nil) xref-hashtb)))))
5942     (and start xref-hashtb)))
5943
5944 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5945   "Look through all the headers and mark the Xrefs as read."
5946   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5947         name info xref-hashtb idlist method nth4)
5948     (save-excursion
5949       (set-buffer gnus-group-buffer)
5950       (when (setq xref-hashtb
5951                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5952         (mapatoms
5953          (lambda (group)
5954            (unless (string= from-newsgroup (setq name (symbol-name group)))
5955              (setq idlist (symbol-value group))
5956              ;; Dead groups are not updated.
5957              (and (prog1
5958                       (setq info (gnus-get-info name))
5959                     (when (stringp (setq nth4 (gnus-info-method info)))
5960                       (setq nth4 (gnus-server-to-method nth4))))
5961                   ;; Only do the xrefs if the group has the same
5962                   ;; select method as the group we have just read.
5963                   (or (gnus-methods-equal-p
5964                        nth4 (gnus-find-method-for-group from-newsgroup))
5965                       virtual
5966                       (equal nth4 (setq method (gnus-find-method-for-group
5967                                                 from-newsgroup)))
5968                       (and (equal (car nth4) (car method))
5969                            (equal (nth 1 nth4) (nth 1 method))))
5970                   gnus-use-cross-reference
5971                   (or (not (eq gnus-use-cross-reference t))
5972                       virtual
5973                       ;; Only do cross-references on subscribed
5974                       ;; groups, if that is what is wanted.
5975                       (<= (gnus-info-level info) gnus-level-subscribed))
5976                   (gnus-group-make-articles-read name idlist))))
5977          xref-hashtb)))))
5978
5979 (defun gnus-compute-read-articles (group articles)
5980   (let* ((entry (gnus-group-entry group))
5981          (info (nth 2 entry))
5982          (active (gnus-active group))
5983          ninfo)
5984     (when entry
5985       ;; First peel off all invalid article numbers.
5986       (when active
5987         (let ((ids articles)
5988               id first)
5989           (while (setq id (pop ids))
5990             (when (and first (> id (cdr active)))
5991               ;; We'll end up in this situation in one particular
5992               ;; obscure situation.  If you re-scan a group and get
5993               ;; a new article that is cross-posted to a different
5994               ;; group that has not been re-scanned, you might get
5995               ;; crossposted article that has a higher number than
5996               ;; Gnus believes possible.  So we re-activate this
5997               ;; group as well.  This might mean doing the
5998               ;; crossposting thingy will *increase* the number
5999               ;; of articles in some groups.  Tsk, tsk.
6000               (setq active (or (gnus-activate-group group) active)))
6001             (when (or (> id (cdr active))
6002                       (< id (car active)))
6003               (setq articles (delq id articles))))))
6004       ;; If the read list is nil, we init it.
6005       (if (and active
6006                (null (gnus-info-read info))
6007                (> (car active) 1))
6008           (setq ninfo (cons 1 (1- (car active))))
6009         (setq ninfo (gnus-info-read info)))
6010       ;; Then we add the read articles to the range.
6011       (gnus-add-to-range
6012        ninfo (setq articles (sort articles '<))))))
6013
6014 (defun gnus-group-make-articles-read (group articles)
6015   "Update the info of GROUP to say that ARTICLES are read."
6016   (let* ((num 0)
6017          (entry (gnus-group-entry group))
6018          (info (nth 2 entry))
6019          (active (gnus-active group))
6020          range)
6021     (when entry
6022       (setq range (gnus-compute-read-articles group articles))
6023       (with-current-buffer gnus-group-buffer
6024         (gnus-undo-register
6025           `(progn
6026              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6027              (gnus-info-set-read ',info ',(gnus-info-read info))
6028              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6029              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6030              (gnus-group-update-group ,group t))))
6031       ;; Add the read articles to the range.
6032       (gnus-info-set-read info range)
6033       (gnus-request-set-mark group (list (list range 'add '(read))))
6034       ;; Then we have to re-compute how many unread
6035       ;; articles there are in this group.
6036       (when active
6037         (cond
6038          ((not range)
6039           (setq num (- (1+ (cdr active)) (car active))))
6040          ((not (listp (cdr range)))
6041           (setq num (- (cdr active) (- (1+ (cdr range))
6042                                        (car range)))))
6043          (t
6044           (while range
6045             (if (numberp (car range))
6046                 (setq num (1+ num))
6047               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6048             (setq range (cdr range)))
6049           (setq num (- (cdr active) num))))
6050         ;; Update the number of unread articles.
6051         (setcar entry num)
6052         ;; Update the group buffer.
6053         (unless (gnus-ephemeral-group-p group)
6054           (gnus-group-update-group group t))))))
6055
6056 (defvar gnus-newsgroup-none-id 0)
6057
6058 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6059   (let ((cur nntp-server-buffer)
6060         (dependencies
6061          (or dependencies
6062              (with-current-buffer gnus-summary-buffer
6063                gnus-newsgroup-dependencies)))
6064         headers id end ref number
6065         (mail-parse-charset gnus-newsgroup-charset)
6066         (mail-parse-ignored-charsets
6067          (save-excursion (condition-case nil
6068                              (set-buffer gnus-summary-buffer)
6069                            (error))
6070                          gnus-newsgroup-ignored-charsets)))
6071     (save-excursion
6072       (set-buffer nntp-server-buffer)
6073       ;; Translate all TAB characters into SPACE characters.
6074       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6075       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6076       (ietf-drums-unfold-fws)
6077       (gnus-run-hooks 'gnus-parse-headers-hook)
6078       (let ((case-fold-search t)
6079             in-reply-to header p lines chars)
6080         (goto-char (point-min))
6081         ;; Search to the beginning of the next header.  Error messages
6082         ;; do not begin with 2 or 3.
6083         (while (re-search-forward "^[23][0-9]+ " nil t)
6084           (setq id nil
6085                 ref nil)
6086           ;; This implementation of this function, with nine
6087           ;; search-forwards instead of the one re-search-forward and
6088           ;; a case (which basically was the old function) is actually
6089           ;; about twice as fast, even though it looks messier.  You
6090           ;; can't have everything, I guess.  Speed and elegance
6091           ;; doesn't always go hand in hand.
6092           (setq
6093            header
6094            (vector
6095             ;; Number.
6096             (prog1
6097                 (setq number (read cur))
6098               (end-of-line)
6099               (setq p (point))
6100               (narrow-to-region (point)
6101                                 (or (and (search-forward "\n.\n" nil t)
6102                                          (- (point) 2))
6103                                     (point))))
6104             ;; Subject.
6105             (progn
6106               (goto-char p)
6107               (if (search-forward "\nsubject:" nil t)
6108                   (funcall gnus-decode-encoded-word-function
6109                            (nnheader-header-value))
6110                 "(none)"))
6111             ;; From.
6112             (progn
6113               (goto-char p)
6114               (if (search-forward "\nfrom:" nil t)
6115                   (funcall gnus-decode-encoded-address-function
6116                            (nnheader-header-value))
6117                 "(nobody)"))
6118             ;; Date.
6119             (progn
6120               (goto-char p)
6121               (if (search-forward "\ndate:" nil t)
6122                   (nnheader-header-value) ""))
6123             ;; Message-ID.
6124             (progn
6125               (goto-char p)
6126               (setq id (if (re-search-forward
6127                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6128                            ;; We do it this way to make sure the Message-ID
6129                            ;; is (somewhat) syntactically valid.
6130                            (buffer-substring (match-beginning 1)
6131                                              (match-end 1))
6132                          ;; If there was no message-id, we just fake one
6133                          ;; to make subsequent routines simpler.
6134                          (nnheader-generate-fake-message-id number))))
6135             ;; References.
6136             (progn
6137               (goto-char p)
6138               (if (search-forward "\nreferences:" nil t)
6139                   (progn
6140                     (setq end (point))
6141                     (prog1
6142                         (nnheader-header-value)
6143                       (setq ref
6144                             (buffer-substring
6145                              (progn
6146                                (end-of-line)
6147                                (search-backward ">" end t)
6148                                (1+ (point)))
6149                              (progn
6150                                (search-backward "<" end t)
6151                                (point))))))
6152                 ;; Get the references from the in-reply-to header if there
6153                 ;; were no references and the in-reply-to header looks
6154                 ;; promising.
6155                 (if (and (search-forward "\nin-reply-to:" nil t)
6156                          (setq in-reply-to (nnheader-header-value))
6157                          (string-match "<[^>]+>" in-reply-to))
6158                     (let (ref2)
6159                       (setq ref (substring in-reply-to (match-beginning 0)
6160                                            (match-end 0)))
6161                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6162                         (setq ref2 (substring in-reply-to (match-beginning 0)
6163                                               (match-end 0)))
6164                         (when (> (length ref2) (length ref))
6165                           (setq ref ref2)))
6166                       ref)
6167                   (setq ref nil))))
6168             ;; Chars.
6169             (progn
6170               (goto-char p)
6171               (if (search-forward "\nchars: " nil t)
6172                   (if (numberp (setq chars (ignore-errors (read cur))))
6173                       chars -1)
6174                 -1))
6175             ;; Lines.
6176             (progn
6177               (goto-char p)
6178               (if (search-forward "\nlines: " nil t)
6179                   (if (numberp (setq lines (ignore-errors (read cur))))
6180                       lines -1)
6181                 -1))
6182             ;; Xref.
6183             (progn
6184               (goto-char p)
6185               (and (search-forward "\nxref:" nil t)
6186                    (nnheader-header-value)))
6187             ;; Extra.
6188             (when gnus-extra-headers
6189               (let ((extra gnus-extra-headers)
6190                     out)
6191                 (while extra
6192                   (goto-char p)
6193                   (when (search-forward
6194                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6195                     (push (cons (car extra) (nnheader-header-value))
6196                           out))
6197                   (pop extra))
6198                 out))))
6199           (when (equal id ref)
6200             (setq ref nil))
6201
6202           (when gnus-alter-header-function
6203             (funcall gnus-alter-header-function header)
6204             (setq id (mail-header-id header)
6205                   ref (gnus-parent-id (mail-header-references header))))
6206
6207           (when (setq header
6208                       (gnus-dependencies-add-header
6209                        header dependencies force-new))
6210             (push header headers))
6211           (goto-char (point-max))
6212           (widen))
6213         (nreverse headers)))))
6214
6215 ;; Goes through the xover lines and returns a list of vectors
6216 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6217                                                   force-new dependencies
6218                                                   group also-fetch-heads)
6219   "Parse the news overview data in the server buffer.
6220 Return a list of headers that match SEQUENCE (see
6221 `nntp-retrieve-headers')."
6222   ;; Get the Xref when the users reads the articles since most/some
6223   ;; NNTP servers do not include Xrefs when using XOVER.
6224   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6225   (let ((mail-parse-charset gnus-newsgroup-charset)
6226         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6227         (cur nntp-server-buffer)
6228         (dependencies (or dependencies gnus-newsgroup-dependencies))
6229         (allp (cond
6230                ((eq gnus-read-all-available-headers t)
6231                 t)
6232                ((and (stringp gnus-read-all-available-headers)
6233                      group)
6234                 (string-match gnus-read-all-available-headers group))
6235                (t
6236                 nil)))
6237         number headers header)
6238     (save-excursion
6239       (set-buffer nntp-server-buffer)
6240       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6241       ;; Allow the user to mangle the headers before parsing them.
6242       (gnus-run-hooks 'gnus-parse-headers-hook)
6243       (goto-char (point-min))
6244       (gnus-parse-without-error
6245         (while (and (or sequence allp)
6246                     (not (eobp)))
6247           (setq number (read cur))
6248           (when (not allp)
6249             (while (and sequence
6250                         (< (car sequence) number))
6251               (setq sequence (cdr sequence))))
6252           (when (and (or allp
6253                          (and sequence
6254                               (eq number (car sequence))))
6255                      (progn
6256                        (setq sequence (cdr sequence))
6257                        (setq header (inline
6258                                       (gnus-nov-parse-line
6259                                        number dependencies force-new)))))
6260             (push header headers))
6261           (forward-line 1)))
6262       ;; A common bug in inn is that if you have posted an article and
6263       ;; then retrieves the active file, it will answer correctly --
6264       ;; the new article is included.  However, a NOV entry for the
6265       ;; article may not have been generated yet, so this may fail.
6266       ;; We work around this problem by retrieving the last few
6267       ;; headers using HEAD.
6268       (if (or (not also-fetch-heads)
6269               (not sequence))
6270           ;; We (probably) got all the headers.
6271           (nreverse headers)
6272         (let ((gnus-nov-is-evil t))
6273           (nconc
6274            (nreverse headers)
6275            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6276              (gnus-get-newsgroup-headers))))))))
6277
6278 (defun gnus-article-get-xrefs ()
6279   "Fill in the Xref value in `gnus-current-headers', if necessary.
6280 This is meant to be called in `gnus-article-internal-prepare-hook'."
6281   (let ((headers (with-current-buffer gnus-summary-buffer
6282                    gnus-current-headers)))
6283     (or (not gnus-use-cross-reference)
6284         (not headers)
6285         (and (mail-header-xref headers)
6286              (not (string= (mail-header-xref headers) "")))
6287         (let ((case-fold-search t)
6288               xref)
6289           (save-restriction
6290             (nnheader-narrow-to-headers)
6291             (goto-char (point-min))
6292             (when (or (and (not (eobp))
6293                            (eq (downcase (char-after)) ?x)
6294                            (looking-at "Xref:"))
6295                       (search-forward "\nXref:" nil t))
6296               (goto-char (1+ (match-end 0)))
6297               (setq xref (buffer-substring (point) (point-at-eol)))
6298               (mail-header-set-xref headers xref)))))))
6299
6300 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6301   "Find article ID and insert the summary line for that article.
6302 OLD-HEADER can either be a header or a line number to insert
6303 the subject line on."
6304   (let* ((line (and (numberp old-header) old-header))
6305          (old-header (and (vectorp old-header) old-header))
6306          (header (cond ((and old-header use-old-header)
6307                         old-header)
6308                        ((and (numberp id)
6309                              (gnus-number-to-header id))
6310                         (gnus-number-to-header id))
6311                        (t
6312                         (gnus-read-header id))))
6313          (number (and (numberp id) id))
6314          d)
6315     (when header
6316       ;; Rebuild the thread that this article is part of and go to the
6317       ;; article we have fetched.
6318       (when (and (not gnus-show-threads)
6319                  old-header)
6320         (when (and number
6321                    (setq d (gnus-data-find (mail-header-number old-header))))
6322           (goto-char (gnus-data-pos d))
6323           (gnus-data-remove
6324            number
6325            (- (point-at-bol)
6326               (prog1
6327                   (1+ (point-at-eol))
6328                 (gnus-delete-line))))))
6329       ;; Remove list identifiers from subject.
6330       (when gnus-list-identifiers
6331         (let ((gnus-newsgroup-headers (list header)))
6332           (gnus-summary-remove-list-identifiers)))
6333       (when old-header
6334         (mail-header-set-number header (mail-header-number old-header)))
6335       (setq gnus-newsgroup-sparse
6336             (delq (setq number (mail-header-number header))
6337                   gnus-newsgroup-sparse))
6338       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6339       (push number gnus-newsgroup-limit)
6340       (gnus-rebuild-thread (mail-header-id header) line)
6341       (gnus-summary-goto-subject number nil t))
6342     (when (and (numberp number)
6343                (> number 0))
6344       ;; We have to update the boundaries even if we can't fetch the
6345       ;; article if ID is a number -- so that the next `P' or `N'
6346       ;; command will fetch the previous (or next) article even
6347       ;; if the one we tried to fetch this time has been canceled.
6348       (when (> number gnus-newsgroup-end)
6349         (setq gnus-newsgroup-end number))
6350       (when (< number gnus-newsgroup-begin)
6351         (setq gnus-newsgroup-begin number))
6352       (setq gnus-newsgroup-unselected
6353             (delq number gnus-newsgroup-unselected)))
6354     ;; Report back a success?
6355     (and header (mail-header-number header))))
6356
6357 ;;; Process/prefix in the summary buffer
6358
6359 (defun gnus-summary-work-articles (n)
6360   "Return a list of articles to be worked upon.
6361 The prefix argument, the list of process marked articles, and the
6362 current article will be taken into consideration."
6363   (save-excursion
6364     (set-buffer gnus-summary-buffer)
6365     (cond
6366      (n
6367       ;; A numerical prefix has been given.
6368       (setq n (prefix-numeric-value n))
6369       (let ((backward (< n 0))
6370             (n (abs (prefix-numeric-value n)))
6371             articles article)
6372         (save-excursion
6373           (while
6374               (and (> n 0)
6375                    (push (setq article (gnus-summary-article-number))
6376                          articles)
6377                    (if backward
6378                        (gnus-summary-find-prev nil article)
6379                      (gnus-summary-find-next nil article)))
6380             (decf n)))
6381         (nreverse articles)))
6382      ((and (gnus-region-active-p) (mark))
6383       (message "region active")
6384       ;; Work on the region between point and mark.
6385       (let ((max (max (point) (mark)))
6386             articles article)
6387         (save-excursion
6388           (goto-char (min (point) (mark)))
6389           (while
6390               (and
6391                (push (setq article (gnus-summary-article-number)) articles)
6392                (gnus-summary-find-next nil article)
6393                (< (point) max)))
6394           (nreverse articles))))
6395      (gnus-newsgroup-processable
6396       ;; There are process-marked articles present.
6397       ;; Save current state.
6398       (gnus-summary-save-process-mark)
6399       ;; Return the list.
6400       (reverse gnus-newsgroup-processable))
6401      (t
6402       ;; Just return the current article.
6403       (list (gnus-summary-article-number))))))
6404
6405 (defmacro gnus-summary-iterate (arg &rest forms)
6406   "Iterate over the process/prefixed articles and do FORMS.
6407 ARG is the interactive prefix given to the command.  FORMS will be
6408 executed with point over the summary line of the articles."
6409   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6410     `(let ((,articles (gnus-summary-work-articles ,arg)))
6411        (while ,articles
6412          (gnus-summary-goto-subject (car ,articles))
6413          ,@forms
6414          (pop ,articles)))))
6415
6416 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6417 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6418
6419 (defun gnus-summary-save-process-mark ()
6420   "Push the current set of process marked articles on the stack."
6421   (interactive)
6422   (push (copy-sequence gnus-newsgroup-processable)
6423         gnus-newsgroup-process-stack))
6424
6425 (defun gnus-summary-kill-process-mark ()
6426   "Push the current set of process marked articles on the stack and unmark."
6427   (interactive)
6428   (gnus-summary-save-process-mark)
6429   (gnus-summary-unmark-all-processable))
6430
6431 (defun gnus-summary-yank-process-mark ()
6432   "Pop the last process mark state off the stack and restore it."
6433   (interactive)
6434   (unless gnus-newsgroup-process-stack
6435     (error "Empty mark stack"))
6436   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6437
6438 (defun gnus-summary-process-mark-set (set)
6439   "Make SET into the current process marked articles."
6440   (gnus-summary-unmark-all-processable)
6441   (mapc 'gnus-summary-set-process-mark set))
6442
6443 ;;; Searching and stuff
6444
6445 (defun gnus-summary-search-group (&optional backward use-level)
6446   "Search for next unread newsgroup.
6447 If optional argument BACKWARD is non-nil, search backward instead."
6448   (save-excursion
6449     (set-buffer gnus-group-buffer)
6450     (when (gnus-group-search-forward
6451            backward nil (if use-level (gnus-group-group-level) nil))
6452       (gnus-group-group-name))))
6453
6454 (defun gnus-summary-best-group (&optional exclude-group)
6455   "Find the name of the best unread group.
6456 If EXCLUDE-GROUP, do not go to this group."
6457   (with-current-buffer gnus-group-buffer
6458     (save-excursion
6459       (gnus-group-best-unread-group exclude-group))))
6460
6461 (defun gnus-summary-find-next (&optional unread article backward)
6462   (if backward
6463       (gnus-summary-find-prev unread article)
6464     (let* ((dummy (gnus-summary-article-intangible-p))
6465            (article (or article (gnus-summary-article-number)))
6466            (data (gnus-data-find-list article))
6467            result)
6468       (when (and (not dummy)
6469                  (or (not gnus-summary-check-current)
6470                      (not unread)
6471                      (not (gnus-data-unread-p (car data)))))
6472         (setq data (cdr data)))
6473       (when (setq result
6474                   (if unread
6475                       (progn
6476                         (while data
6477                           (unless (memq (gnus-data-number (car data))
6478                                         (cond
6479                                          ((eq gnus-auto-goto-ignores
6480                                               'always-undownloaded)
6481                                           gnus-newsgroup-undownloaded)
6482                                          (gnus-plugged
6483                                           nil)
6484                                          ((eq gnus-auto-goto-ignores
6485                                               'unfetched)
6486                                           gnus-newsgroup-unfetched)
6487                                          ((eq gnus-auto-goto-ignores
6488                                               'undownloaded)
6489                                           gnus-newsgroup-undownloaded)))
6490                             (when (gnus-data-unread-p (car data))
6491                               (setq result (car data)
6492                                     data nil)))
6493                           (setq data (cdr data)))
6494                         result)
6495                     (car data)))
6496         (goto-char (gnus-data-pos result))
6497         (gnus-data-number result)))))
6498
6499 (defun gnus-summary-find-prev (&optional unread article)
6500   (let* ((eobp (eobp))
6501          (article (or article (gnus-summary-article-number)))
6502          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6503          result)
6504     (when (and (not eobp)
6505                (or (not gnus-summary-check-current)
6506                    (not unread)
6507                    (not (gnus-data-unread-p (car data)))))
6508       (setq data (cdr data)))
6509     (when (setq result
6510                 (if unread
6511                     (progn
6512                       (while data
6513                         (unless (memq (gnus-data-number (car data))
6514                                       (cond
6515                                        ((eq gnus-auto-goto-ignores
6516                                             'always-undownloaded)
6517                                         gnus-newsgroup-undownloaded)
6518                                        (gnus-plugged
6519                                         nil)
6520                                        ((eq gnus-auto-goto-ignores
6521                                             'unfetched)
6522                                         gnus-newsgroup-unfetched)
6523                                        ((eq gnus-auto-goto-ignores
6524                                             'undownloaded)
6525                                         gnus-newsgroup-undownloaded)))
6526                           (when (gnus-data-unread-p (car data))
6527                             (setq result (car data)
6528                                   data nil)))
6529                         (setq data (cdr data)))
6530                       result)
6531                   (car data)))
6532       (goto-char (gnus-data-pos result))
6533       (gnus-data-number result))))
6534
6535 (defun gnus-summary-find-subject (subject &optional unread backward article)
6536   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6537          (article (or article (gnus-summary-article-number)))
6538          (articles (gnus-data-list backward))
6539          (arts (gnus-data-find-list article articles))
6540          result)
6541     (when (or (not gnus-summary-check-current)
6542               (not unread)
6543               (not (gnus-data-unread-p (car arts))))
6544       (setq arts (cdr arts)))
6545     (while arts
6546       (and (or (not unread)
6547                (gnus-data-unread-p (car arts)))
6548            (vectorp (gnus-data-header (car arts)))
6549            (gnus-subject-equal
6550             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6551            (setq result (car arts)
6552                  arts nil))
6553       (setq arts (cdr arts)))
6554     (and result
6555          (goto-char (gnus-data-pos result))
6556          (gnus-data-number result))))
6557
6558 (defun gnus-summary-search-forward (&optional unread subject backward)
6559   "Search forward for an article.
6560 If UNREAD, look for unread articles.  If SUBJECT, look for
6561 articles with that subject.  If BACKWARD, search backward instead."
6562   (cond (subject (gnus-summary-find-subject subject unread backward))
6563         (backward (gnus-summary-find-prev unread))
6564         (t (gnus-summary-find-next unread))))
6565
6566 (defun gnus-recenter (&optional n)
6567   "Center point in window and redisplay frame.
6568 Also do horizontal recentering."
6569   (interactive "P")
6570   (when (and gnus-auto-center-summary
6571              (not (eq gnus-auto-center-summary 'vertical)))
6572     (gnus-horizontal-recenter))
6573   (recenter n))
6574
6575 (defun gnus-summary-recenter ()
6576   "Center point in the summary window.
6577 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6578 displayed, no centering will be performed."
6579   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6580   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6581   (interactive)
6582   ;; The user has to want it.
6583   (when gnus-auto-center-summary
6584     (let* ((top (cond ((< (window-height) 4) 0)
6585                       ((< (window-height) 7) 1)
6586                       (t (if (numberp gnus-auto-center-summary)
6587                              gnus-auto-center-summary
6588                            (/ (1- (window-height)) 2)))))
6589            (height (1- (window-height)))
6590            (bottom (save-excursion (goto-char (point-max))
6591                                    (forward-line (- height))
6592                                    (point)))
6593            (window (get-buffer-window (current-buffer))))
6594       (when (get-buffer-window gnus-article-buffer)
6595         ;; Only do recentering when the article buffer is displayed,
6596         ;; Set the window start to either `bottom', which is the biggest
6597         ;; possible valid number, or the second line from the top,
6598         ;; whichever is the least.
6599         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6600           (if (> bottom top-pos)
6601               ;; Keep the second line from the top visible
6602               (set-window-start window top-pos)
6603             ;; Try to keep the bottom line visible; if it's partially
6604             ;; obscured, either scroll one more line to make it fully
6605             ;; visible, or revert to using TOP-POS.
6606             (save-excursion
6607               (goto-char (point-max))
6608               (forward-line -1)
6609               (let ((last-line-start (point)))
6610                 (goto-char bottom)
6611                 (set-window-start window (point) t)
6612                 (when (not (pos-visible-in-window-p last-line-start window))
6613                   (forward-line 1)
6614                   (set-window-start window (min (point) top-pos) t)))))))
6615       ;; Do horizontal recentering while we're at it.
6616       (when (and (get-buffer-window (current-buffer) t)
6617                  (not (eq gnus-auto-center-summary 'vertical)))
6618         (let ((selected (selected-window)))
6619           (select-window (get-buffer-window (current-buffer) t))
6620           (gnus-summary-position-point)
6621           (gnus-horizontal-recenter)
6622           (select-window selected))))))
6623
6624 (defun gnus-summary-jump-to-group (newsgroup)
6625   "Move point to NEWSGROUP in group mode buffer."
6626   ;; Keep update point of group mode buffer if visible.
6627   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6628       (save-window-excursion
6629         ;; Take care of tree window mode.
6630         (when (get-buffer-window gnus-group-buffer)
6631           (pop-to-buffer gnus-group-buffer))
6632         (gnus-group-jump-to-group newsgroup))
6633     (save-excursion
6634       ;; Take care of tree window mode.
6635       (if (get-buffer-window gnus-group-buffer)
6636           (pop-to-buffer gnus-group-buffer)
6637         (set-buffer gnus-group-buffer))
6638       (gnus-group-jump-to-group newsgroup))))
6639
6640 ;; This function returns a list of article numbers based on the
6641 ;; difference between the ranges of read articles in this group and
6642 ;; the range of active articles.
6643 (defun gnus-list-of-unread-articles (group)
6644   (let* ((read (gnus-info-read (gnus-get-info group)))
6645          (active (or (gnus-active group) (gnus-activate-group group)))
6646          (last (or (cdr active)
6647                    (error "Group %s couldn't be activated " group)))
6648          first nlast unread)
6649     ;; If none are read, then all are unread.
6650     (if (not read)
6651         (setq first (car active))
6652       ;; If the range of read articles is a single range, then the
6653       ;; first unread article is the article after the last read
6654       ;; article.  Sounds logical, doesn't it?
6655       (if (and (not (listp (cdr read)))
6656                (or (< (car read) (car active))
6657                    (progn (setq read (list read))
6658                           nil)))
6659           (setq first (max (car active) (1+ (cdr read))))
6660         ;; `read' is a list of ranges.
6661         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6662                                   (caar read)))
6663                   1)
6664           (setq first (car active)))
6665         (while read
6666           (when first
6667             (while (< first nlast)
6668               (setq unread (cons first unread)
6669                     first (1+ first))))
6670           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6671           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6672           (setq read (cdr read)))))
6673     ;; And add the last unread articles.
6674     (while (<= first last)
6675       (setq unread (cons first unread)
6676             first (1+ first)))
6677     ;; Return the list of unread articles.
6678     (delq 0 (nreverse unread))))
6679
6680 (defun gnus-list-of-read-articles (group)
6681   "Return a list of unread, unticked and non-dormant articles."
6682   (let* ((info (gnus-get-info group))
6683          (marked (gnus-info-marks info))
6684          (active (gnus-active group)))
6685     (and info active
6686          (gnus-list-range-difference
6687           (gnus-list-range-difference
6688            (gnus-sorted-complement
6689             (gnus-uncompress-range active)
6690             (gnus-list-of-unread-articles group))
6691            (cdr (assq 'dormant marked)))
6692           (cdr (assq 'tick marked))))))
6693
6694 ;; This function returns a sequence of article numbers based on the
6695 ;; difference between the ranges of read articles in this group and
6696 ;; the range of active articles.
6697 (defun gnus-sequence-of-unread-articles (group)
6698   (let* ((read (gnus-info-read (gnus-get-info group)))
6699          (active (or (gnus-active group) (gnus-activate-group group)))
6700          (last (cdr active))
6701          first nlast unread)
6702     ;; If none are read, then all are unread.
6703     (if (not read)
6704         (setq first (car active))
6705       ;; If the range of read articles is a single range, then the
6706       ;; first unread article is the article after the last read
6707       ;; article.  Sounds logical, doesn't it?
6708       (if (and (not (listp (cdr read)))
6709                (or (< (car read) (car active))
6710                    (progn (setq read (list read))
6711                           nil)))
6712           (setq first (max (car active) (1+ (cdr read))))
6713         ;; `read' is a list of ranges.
6714         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6715                                   (caar read)))
6716                   1)
6717           (setq first (car active)))
6718         (while read
6719           (when first
6720             (push (cons first nlast) unread))
6721           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6722           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6723           (setq read (cdr read)))))
6724     ;; And add the last unread articles.
6725     (cond ((not (and first last))
6726            nil)
6727           ((< first last)
6728            (push (cons first last) unread))
6729           ((= first last)
6730            (push first unread)))
6731     ;; Return the sequence of unread articles.
6732     (delq 0 (nreverse unread))))
6733
6734 ;; Various summary commands
6735
6736 (defun gnus-summary-select-article-buffer ()
6737   "Reconfigure windows to show article buffer."
6738   (interactive)
6739   (if (not (gnus-buffer-live-p gnus-article-buffer))
6740       (error "There is no article buffer for this summary buffer")
6741     (gnus-configure-windows 'article)
6742     (select-window (get-buffer-window gnus-article-buffer))))
6743
6744 (defun gnus-summary-universal-argument (arg)
6745   "Perform any operation on all articles that are process/prefixed."
6746   (interactive "P")
6747   (let ((articles (gnus-summary-work-articles arg))
6748         func article)
6749     (if (eq
6750          (setq
6751           func
6752           (key-binding
6753            (read-key-sequence
6754             (substitute-command-keys
6755              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6756          'undefined)
6757         (gnus-error 1 "Undefined key")
6758       (save-excursion
6759         (while articles
6760           (gnus-summary-goto-subject (setq article (pop articles)))
6761           (let (gnus-newsgroup-processable)
6762             (command-execute func))
6763           (gnus-summary-remove-process-mark article)))))
6764   (gnus-summary-position-point))
6765
6766 (defun gnus-summary-toggle-truncation (&optional arg)
6767   "Toggle truncation of summary lines.
6768 With ARG, turn line truncation on if ARG is positive."
6769   (interactive "P")
6770   (setq truncate-lines
6771         (if (null arg) (not truncate-lines)
6772           (> (prefix-numeric-value arg) 0)))
6773   (redraw-display))
6774
6775 (defun gnus-summary-find-for-reselect ()
6776   "Return the number of an article to stay on across a reselect.
6777 The current article is considered, then following articles, then previous
6778 articles.  An article is sought which is not cancelled and isn't a temporary
6779 insertion from another group.  If there's no such then return a dummy 0."
6780   (let (found)
6781     (dolist (rev '(nil t))
6782       (unless found      ; don't demand the reverse list if we don't need it
6783         (let ((data (gnus-data-find-list
6784                      (gnus-summary-article-number) (gnus-data-list rev))))
6785           (while (and data (not found))
6786             (if (and (< 0 (gnus-data-number (car data)))
6787                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6788                 (setq found (gnus-data-number (car data))))
6789             (setq data (cdr data))))))
6790     (or found 0)))
6791
6792 (defun gnus-summary-reselect-current-group (&optional all rescan)
6793   "Exit and then reselect the current newsgroup.
6794 The prefix argument ALL means to select all articles."
6795   (interactive "P")
6796   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6797     (error "Ephemeral groups can't be reselected"))
6798   (let ((current-subject (gnus-summary-find-for-reselect))
6799         (group gnus-newsgroup-name))
6800     (setq gnus-newsgroup-begin nil)
6801     (gnus-summary-exit nil 'leave-hidden)
6802     ;; We have to adjust the point of group mode buffer because
6803     ;; point was moved to the next unread newsgroup by exiting.
6804     (gnus-summary-jump-to-group group)
6805     (when rescan
6806       (save-excursion
6807         (gnus-group-get-new-news-this-group 1)))
6808     (gnus-group-read-group all t)
6809     (gnus-summary-goto-subject current-subject nil t)))
6810
6811 (defun gnus-summary-rescan-group (&optional all)
6812   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6813   (interactive "P")
6814   (gnus-summary-reselect-current-group all t))
6815
6816 (defun gnus-summary-update-info (&optional non-destructive)
6817   (save-excursion
6818     (let ((group gnus-newsgroup-name))
6819       (when group
6820         (when gnus-newsgroup-kill-headers
6821           (setq gnus-newsgroup-killed
6822                 (gnus-compress-sequence
6823                  (gnus-sorted-union
6824                   (gnus-list-range-intersection
6825                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6826                   gnus-newsgroup-unreads)
6827                  t)))
6828         (unless (listp (cdr gnus-newsgroup-killed))
6829           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6830         (let ((headers gnus-newsgroup-headers))
6831           ;; Set the new ranges of read articles.
6832           (with-current-buffer gnus-group-buffer
6833             (gnus-undo-force-boundary))
6834           (gnus-update-read-articles
6835            group (gnus-sorted-union
6836                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6837           ;; Set the current article marks.
6838           (let ((gnus-newsgroup-scored
6839                  (if (and (not gnus-save-score)
6840                           (not non-destructive))
6841                      nil
6842                    gnus-newsgroup-scored)))
6843             (save-excursion
6844               (gnus-update-marks)))
6845           ;; Do the cross-ref thing.
6846           (when gnus-use-cross-reference
6847             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6848           ;; Do not switch windows but change the buffer to work.
6849           (set-buffer gnus-group-buffer)
6850           (unless (gnus-ephemeral-group-p group)
6851             (gnus-group-update-group group)))))))
6852
6853 (defun gnus-summary-save-newsrc (&optional force)
6854   "Save the current number of read/marked articles in the dribble buffer.
6855 The dribble buffer will then be saved.
6856 If FORCE (the prefix), also save the .newsrc file(s)."
6857   (interactive "P")
6858   (gnus-summary-update-info t)
6859   (if force
6860       (gnus-save-newsrc-file)
6861     (gnus-dribble-save)))
6862
6863 (defun gnus-summary-exit (&optional temporary leave-hidden)
6864   "Exit reading current newsgroup, and then return to group selection mode.
6865 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6866   (interactive)
6867   (gnus-set-global-variables)
6868   (when (gnus-buffer-live-p gnus-article-buffer)
6869     (save-excursion
6870       (set-buffer gnus-article-buffer)
6871       (mm-destroy-parts gnus-article-mime-handles)
6872       ;; Set it to nil for safety reason.
6873       (setq gnus-article-mime-handle-alist nil)
6874       (setq gnus-article-mime-handles nil)))
6875   (gnus-kill-save-kill-buffer)
6876   (gnus-async-halt-prefetch)
6877   (let* ((group gnus-newsgroup-name)
6878          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6879          (gnus-group-is-exiting-p t)
6880          (mode major-mode)
6881          (group-point nil)
6882          (buf (current-buffer)))
6883     (unless quit-config
6884       ;; Do adaptive scoring, and possibly save score files.
6885       (when gnus-newsgroup-adaptive
6886         (gnus-score-adaptive))
6887       (when gnus-use-scoring
6888         (gnus-score-save)))
6889     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6890     ;; If we have several article buffers, we kill them at exit.
6891     (unless gnus-single-article-buffer
6892       (gnus-kill-buffer gnus-article-buffer)
6893       (gnus-kill-buffer gnus-original-article-buffer)
6894       (setq gnus-article-current nil))
6895     (when gnus-use-cache
6896       (gnus-cache-possibly-remove-articles)
6897       (gnus-cache-save-buffers))
6898     (gnus-async-prefetch-remove-group group)
6899     (when gnus-suppress-duplicates
6900       (gnus-dup-enter-articles))
6901     (when gnus-use-trees
6902       (gnus-tree-close group))
6903     (when gnus-use-cache
6904       (gnus-cache-write-active))
6905     ;; Remove entries for this group.
6906     (nnmail-purge-split-history (gnus-group-real-name group))
6907     ;; Make all changes in this group permanent.
6908     (unless quit-config
6909       (gnus-run-hooks 'gnus-exit-group-hook)
6910       (gnus-summary-update-info))
6911     (gnus-close-group group)
6912     ;; Make sure where we were, and go to next newsgroup.
6913     (set-buffer gnus-group-buffer)
6914     (unless quit-config
6915       (gnus-group-jump-to-group group))
6916     (gnus-run-hooks 'gnus-summary-exit-hook)
6917     (unless (or quit-config
6918                 ;; If this group has disappeared from the summary
6919                 ;; buffer, don't skip forwards.
6920                 (not (string= group (gnus-group-group-name))))
6921       (gnus-group-next-unread-group 1))
6922     (setq group-point (point))
6923     (if temporary
6924         nil                             ;Nothing to do.
6925       ;; If we have several article buffers, we kill them at exit.
6926       (unless gnus-single-article-buffer
6927         (gnus-kill-buffer gnus-article-buffer)
6928         (gnus-kill-buffer gnus-original-article-buffer)
6929         (setq gnus-article-current nil))
6930       (set-buffer buf)
6931       (if (not gnus-kill-summary-on-exit)
6932           (progn
6933             (gnus-deaden-summary)
6934             (setq mode nil))
6935         ;; We set all buffer-local variables to nil.  It is unclear why
6936         ;; this is needed, but if we don't, buffer-local variables are
6937         ;; not garbage-collected, it seems.  This would the lead to en
6938         ;; ever-growing Emacs.
6939         (gnus-summary-clear-local-variables)
6940         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6941           (gnus-summary-clear-local-variables))
6942         (when (get-buffer gnus-article-buffer)
6943           (bury-buffer gnus-article-buffer))
6944         ;; Return to group mode buffer.
6945         (when (eq mode 'gnus-summary-mode)
6946           (gnus-kill-buffer buf)))
6947       (setq gnus-current-select-method gnus-select-method)
6948       (set-buffer gnus-group-buffer)
6949       (if quit-config
6950           (gnus-handle-ephemeral-exit quit-config)
6951         (goto-char group-point)
6952         ;; If gnus-group-buffer is already displayed, make sure we also move
6953         ;; the cursor in the window that displays it.
6954         (let ((win (get-buffer-window (current-buffer) 0)))
6955           (if win (set-window-point win (point))))
6956         (unless leave-hidden
6957           (gnus-configure-windows 'group 'force)))
6958       ;; Clear the current group name.
6959       (unless quit-config
6960         (setq gnus-newsgroup-name nil)))))
6961
6962 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6963 (defun gnus-summary-exit-no-update (&optional no-questions)
6964   "Quit reading current newsgroup without updating read article info."
6965   (interactive)
6966   (let* ((group gnus-newsgroup-name)
6967          (gnus-group-is-exiting-p t)
6968          (gnus-group-is-exiting-without-update-p t)
6969          (quit-config (gnus-group-quit-config group)))
6970     (when (or no-questions
6971               gnus-expert-user
6972               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6973       (gnus-async-halt-prefetch)
6974       (run-hooks 'gnus-summary-prepare-exit-hook)
6975       (when (gnus-buffer-live-p gnus-article-buffer)
6976         (save-excursion
6977           (set-buffer gnus-article-buffer)
6978           (mm-destroy-parts gnus-article-mime-handles)
6979           ;; Set it to nil for safety reason.
6980           (setq gnus-article-mime-handle-alist nil)
6981           (setq gnus-article-mime-handles nil)))
6982       ;; If we have several article buffers, we kill them at exit.
6983       (unless gnus-single-article-buffer
6984         (gnus-kill-buffer gnus-article-buffer)
6985         (gnus-kill-buffer gnus-original-article-buffer)
6986         (setq gnus-article-current nil))
6987       (if (not gnus-kill-summary-on-exit)
6988           (gnus-deaden-summary)
6989         (gnus-close-group group)
6990         (gnus-summary-clear-local-variables)
6991         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6992           (gnus-summary-clear-local-variables))
6993         (gnus-kill-buffer gnus-summary-buffer))
6994       (unless gnus-single-article-buffer
6995         (setq gnus-article-current nil))
6996       (when gnus-use-trees
6997         (gnus-tree-close group))
6998       (gnus-async-prefetch-remove-group group)
6999       (when (get-buffer gnus-article-buffer)
7000         (bury-buffer gnus-article-buffer))
7001       ;; Return to the group buffer.
7002       (gnus-configure-windows 'group 'force)
7003       ;; Clear the current group name.
7004       (setq gnus-newsgroup-name nil)
7005       (unless (gnus-ephemeral-group-p group)
7006         (gnus-group-update-group group))
7007       (when (equal (gnus-group-group-name) group)
7008         (gnus-group-next-unread-group 1))
7009       (when quit-config
7010         (gnus-handle-ephemeral-exit quit-config)))))
7011
7012 (defun gnus-handle-ephemeral-exit (quit-config)
7013   "Handle movement when leaving an ephemeral group.
7014 The state which existed when entering the ephemeral is reset."
7015   (if (not (buffer-name (car quit-config)))
7016       (gnus-configure-windows 'group 'force)
7017     (set-buffer (car quit-config))
7018     (cond ((eq major-mode 'gnus-summary-mode)
7019            (gnus-set-global-variables))
7020           ((eq major-mode 'gnus-article-mode)
7021            (save-excursion
7022              ;; The `gnus-summary-buffer' variable may point
7023              ;; to the old summary buffer when using a single
7024              ;; article buffer.
7025              (unless (gnus-buffer-live-p gnus-summary-buffer)
7026                (set-buffer gnus-group-buffer))
7027              (set-buffer gnus-summary-buffer)
7028              (gnus-set-global-variables))))
7029     (if (or (eq (cdr quit-config) 'article)
7030             (eq (cdr quit-config) 'pick))
7031         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7032             (gnus-configure-windows 'pick 'force)
7033           (gnus-configure-windows (cdr quit-config) 'force))
7034       (gnus-configure-windows (cdr quit-config) 'force))
7035     (when (eq major-mode 'gnus-summary-mode)
7036       (gnus-summary-recenter)
7037       (gnus-summary-position-point))))
7038
7039 ;;; Dead summaries.
7040
7041 (defvar gnus-dead-summary-mode-map nil)
7042
7043 (unless gnus-dead-summary-mode-map
7044   (setq gnus-dead-summary-mode-map (make-keymap))
7045   (suppress-keymap gnus-dead-summary-mode-map)
7046   (substitute-key-definition
7047    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7048   (dolist (key '("\C-d" "\r" "\177" [delete]))
7049     (define-key gnus-dead-summary-mode-map
7050       key 'gnus-summary-wake-up-the-dead))
7051   (dolist (key '("q" "Q"))
7052     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7053
7054 (defvar gnus-dead-summary-mode nil
7055   "Minor mode for Gnus summary buffers.")
7056
7057 (defun gnus-dead-summary-mode (&optional arg)
7058   "Minor mode for Gnus summary buffers."
7059   (interactive "P")
7060   (when (eq major-mode 'gnus-summary-mode)
7061     (make-local-variable 'gnus-dead-summary-mode)
7062     (setq gnus-dead-summary-mode
7063           (if (null arg) (not gnus-dead-summary-mode)
7064             (> (prefix-numeric-value arg) 0)))
7065     (when gnus-dead-summary-mode
7066       (add-minor-mode
7067        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7068
7069 (defun gnus-deaden-summary ()
7070   "Make the current summary buffer into a dead summary buffer."
7071   ;; Kill any previous dead summary buffer.
7072   (when (and gnus-dead-summary
7073              (buffer-name gnus-dead-summary))
7074     (with-current-buffer gnus-dead-summary
7075       (when gnus-dead-summary-mode
7076         (kill-buffer (current-buffer)))))
7077   ;; Make this the current dead summary.
7078   (setq gnus-dead-summary (current-buffer))
7079   (gnus-dead-summary-mode 1)
7080   (let ((name (buffer-name)))
7081     (when (string-match "Summary" name)
7082       (rename-buffer
7083        (concat (substring name 0 (match-beginning 0)) "Dead "
7084                (substring name (match-beginning 0)))
7085        t)
7086       (bury-buffer))))
7087
7088 (defun gnus-kill-or-deaden-summary (buffer)
7089   "Kill or deaden the summary BUFFER."
7090   (save-excursion
7091     (when (and (buffer-name buffer)
7092                (not gnus-single-article-buffer))
7093       (with-current-buffer buffer
7094         (gnus-kill-buffer gnus-article-buffer)
7095         (gnus-kill-buffer gnus-original-article-buffer)))
7096     (cond
7097      ;; Kill the buffer.
7098      (gnus-kill-summary-on-exit
7099       (when (and gnus-use-trees
7100                  (gnus-buffer-exists-p buffer))
7101         (save-excursion
7102           (set-buffer buffer)
7103           (gnus-tree-close gnus-newsgroup-name)))
7104       (gnus-kill-buffer buffer))
7105      ;; Deaden the buffer.
7106      ((gnus-buffer-exists-p buffer)
7107       (save-excursion
7108         (set-buffer buffer)
7109         (gnus-deaden-summary))))))
7110
7111 (defun gnus-summary-wake-up-the-dead (&rest args)
7112   "Wake up the dead summary buffer."
7113   (interactive)
7114   (gnus-dead-summary-mode -1)
7115   (let ((name (buffer-name)))
7116     (when (string-match "Dead " name)
7117       (rename-buffer
7118        (concat (substring name 0 (match-beginning 0))
7119                (substring name (match-end 0)))
7120        t)))
7121   (gnus-message 3 "This dead summary is now alive again"))
7122
7123 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7124 (defun gnus-summary-fetch-faq (&optional faq-dir)
7125   "Fetch the FAQ for the current group.
7126 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7127 in."
7128   (interactive
7129    (list
7130     (when current-prefix-arg
7131       (completing-read
7132        "FAQ dir: " (and (listp gnus-group-faq-directory)
7133                         (mapcar 'list
7134                                 gnus-group-faq-directory))))))
7135   (let (gnus-faq-buffer)
7136     (when (setq gnus-faq-buffer
7137                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7138       (gnus-configure-windows 'summary-faq))))
7139
7140 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7141 (defun gnus-summary-describe-group (&optional force)
7142   "Describe the current newsgroup."
7143   (interactive "P")
7144   (gnus-group-describe-group force gnus-newsgroup-name))
7145
7146 (defun gnus-summary-describe-briefly ()
7147   "Describe summary mode commands briefly."
7148   (interactive)
7149   (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")))
7150
7151 ;; Walking around group mode buffer from summary mode.
7152
7153 (defun gnus-summary-next-group (&optional no-article target-group backward)
7154   "Exit current newsgroup and then select next unread newsgroup.
7155 If prefix argument NO-ARTICLE is non-nil, no article is selected
7156 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7157 previous group instead."
7158   (interactive "P")
7159   ;; Stop pre-fetching.
7160   (gnus-async-halt-prefetch)
7161   (let ((current-group gnus-newsgroup-name)
7162         (current-buffer (current-buffer))
7163         entered)
7164     ;; First we semi-exit this group to update Xrefs and all variables.
7165     ;; We can't do a real exit, because the window conf must remain
7166     ;; the same in case the user is prompted for info, and we don't
7167     ;; want the window conf to change before that...
7168     (gnus-summary-exit t)
7169     (while (not entered)
7170       ;; Then we find what group we are supposed to enter.
7171       (set-buffer gnus-group-buffer)
7172       (gnus-group-jump-to-group current-group)
7173       (setq target-group
7174             (or target-group
7175                 (if (eq gnus-keep-same-level 'best)
7176                     (gnus-summary-best-group gnus-newsgroup-name)
7177                   (gnus-summary-search-group backward gnus-keep-same-level))))
7178       (if (not target-group)
7179           ;; There are no further groups, so we return to the group
7180           ;; buffer.
7181           (progn
7182             (gnus-message 5 "Returning to the group buffer")
7183             (setq entered t)
7184             (when (gnus-buffer-live-p current-buffer)
7185               (set-buffer current-buffer)
7186               (gnus-summary-exit))
7187             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7188         ;; We try to enter the target group.
7189         (gnus-group-jump-to-group target-group)
7190         (let ((unreads (gnus-group-group-unread)))
7191           (if (and (or (eq t unreads)
7192                        (and unreads (not (zerop unreads))))
7193                    (gnus-summary-read-group
7194                     target-group nil no-article
7195                     (and (buffer-name current-buffer) current-buffer)
7196                     nil backward))
7197               (setq entered t)
7198             (setq current-group target-group
7199                   target-group nil)))))))
7200
7201 (defun gnus-summary-prev-group (&optional no-article)
7202   "Exit current newsgroup and then select previous unread newsgroup.
7203 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7204   (interactive "P")
7205   (gnus-summary-next-group no-article nil t))
7206
7207 ;; Walking around summary lines.
7208
7209 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7210   "Go to the first subject satisfying any non-nil constraint.
7211 If UNREAD is non-nil, the article should be unread.
7212 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7213 If UNSEEN is non-nil, the article should be unseen.
7214 Returns the article selected or nil if there are no matching articles."
7215   (interactive "P")
7216   (cond
7217    ;; Empty summary.
7218    ((null gnus-newsgroup-data)
7219     (gnus-message 3 "No articles in the group")
7220     nil)
7221    ;; Pick the first article.
7222    ((not (or unread undownloaded unseen))
7223     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7224     (gnus-data-number (car gnus-newsgroup-data)))
7225    ;; Find the first unread article.
7226    (t
7227     (let ((data gnus-newsgroup-data))
7228       (while (and data
7229                   (let ((num (gnus-data-number (car data))))
7230                     (or (memq num gnus-newsgroup-unfetched)
7231                         (not (or (and unread
7232                                       (memq num gnus-newsgroup-unreads))
7233                                  (and undownloaded
7234                                       (memq num gnus-newsgroup-undownloaded))
7235                                  (and unseen
7236                                       (memq num gnus-newsgroup-unseen)))))))
7237         (setq data (cdr data)))
7238       (prog1
7239           (if data
7240               (progn
7241                 (goto-char (gnus-data-pos (car data)))
7242                 (gnus-data-number (car data)))
7243             (gnus-message 3 "No more%s articles"
7244                           (let* ((r (when unread " unread"))
7245                                  (d (when undownloaded " undownloaded"))
7246                                  (s (when unseen " unseen"))
7247                                  (l (delq nil (list r d s))))
7248                             (cond ((= 3 (length l))
7249                                    (concat r "," d ", or" s))
7250                                   ((= 2 (length l))
7251                                    (concat (car l) ", or" (cadr l)))
7252                                   ((= 1 (length l))
7253                                    (car l))
7254                                   (t
7255                                    ""))))
7256             nil
7257             )
7258         (gnus-summary-position-point))))))
7259
7260 (defun gnus-summary-next-subject (n &optional unread dont-display)
7261   "Go to next N'th summary line.
7262 If N is negative, go to the previous N'th subject line.
7263 If UNREAD is non-nil, only unread articles are selected.
7264 The difference between N and the actual number of steps taken is
7265 returned."
7266   (interactive "p")
7267   (let ((backward (< n 0))
7268         (n (abs n)))
7269     (while (and (> n 0)
7270                 (if backward
7271                     (gnus-summary-find-prev unread)
7272                   (gnus-summary-find-next unread)))
7273       (unless (zerop (setq n (1- n)))
7274         (gnus-summary-show-thread)))
7275     (when (/= 0 n)
7276       (gnus-message 7 "No more%s articles"
7277                     (if unread " unread" "")))
7278     (unless dont-display
7279       (gnus-summary-recenter)
7280       (gnus-summary-position-point))
7281     n))
7282
7283 (defun gnus-summary-next-unread-subject (n)
7284   "Go to next N'th unread summary line."
7285   (interactive "p")
7286   (gnus-summary-next-subject n t))
7287
7288 (defun gnus-summary-prev-subject (n &optional unread)
7289   "Go to previous N'th summary line.
7290 If optional argument UNREAD is non-nil, only unread article is selected."
7291   (interactive "p")
7292   (gnus-summary-next-subject (- n) unread))
7293
7294 (defun gnus-summary-prev-unread-subject (n)
7295   "Go to previous N'th unread summary line."
7296   (interactive "p")
7297   (gnus-summary-next-subject (- n) t))
7298
7299 (defun gnus-summary-goto-subjects (articles)
7300   "Insert the subject header for ARTICLES in the current buffer."
7301   (save-excursion
7302     (dolist (article articles)
7303       (gnus-summary-goto-subject article t)))
7304   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7305   (gnus-summary-position-point))
7306
7307 (defun gnus-summary-goto-subject (article &optional force silent)
7308   "Go the subject line of ARTICLE.
7309 If FORCE, also allow jumping to articles not currently shown."
7310   (interactive "nArticle number: ")
7311   (unless (numberp article)
7312     (error "Article %s is not a number" article))
7313   (let ((b (point))
7314         (data (gnus-data-find article)))
7315     ;; We read in the article if we have to.
7316     (and (not data)
7317          force
7318          (gnus-summary-insert-subject
7319           article
7320           (if (or (numberp force) (vectorp force)) force)
7321           t)
7322          (setq data (gnus-data-find article)))
7323     (goto-char b)
7324     (if (not data)
7325         (progn
7326           (unless silent
7327             (gnus-message 3 "Can't find article %d" article))
7328           nil)
7329       (let ((pt (gnus-data-pos data)))
7330         (goto-char pt)
7331         (gnus-summary-set-article-display-arrow pt))
7332       (gnus-summary-position-point)
7333       article)))
7334
7335 ;; Walking around summary lines with displaying articles.
7336
7337 (defun gnus-summary-expand-window (&optional arg)
7338   "Make the summary buffer take up the entire Emacs frame.
7339 Given a prefix, will force an `article' buffer configuration."
7340   (interactive "P")
7341   (if arg
7342       (gnus-configure-windows 'article 'force)
7343     (gnus-configure-windows 'summary 'force)))
7344
7345 (defun gnus-summary-display-article (article &optional all-header)
7346   "Display ARTICLE in article buffer."
7347   (when (gnus-buffer-live-p gnus-article-buffer)
7348     (with-current-buffer gnus-article-buffer
7349       (mm-enable-multibyte)))
7350   (gnus-set-global-variables)
7351   (when (gnus-buffer-live-p gnus-article-buffer)
7352     (with-current-buffer gnus-article-buffer
7353       (setq gnus-article-charset gnus-newsgroup-charset)
7354       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7355       (mm-enable-multibyte)))
7356   (if (null article)
7357       nil
7358     (prog1
7359         (if gnus-summary-display-article-function
7360             (funcall gnus-summary-display-article-function article all-header)
7361           (gnus-article-prepare article all-header))
7362       (gnus-run-hooks 'gnus-select-article-hook)
7363       (when (and gnus-current-article
7364                  (not (zerop gnus-current-article)))
7365         (gnus-summary-goto-subject gnus-current-article))
7366       (gnus-summary-recenter)
7367       (when (and gnus-use-trees gnus-show-threads)
7368         (gnus-possibly-generate-tree article)
7369         (gnus-highlight-selected-tree article))
7370       ;; Successfully display article.
7371       (gnus-article-set-window-start
7372        (cdr (assq article gnus-newsgroup-bookmarks))))))
7373
7374 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7375   "Select the current article.
7376 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7377 non-nil, the article will be re-fetched even if it already present in
7378 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7379 be displayed."
7380   ;; Make sure we are in the summary buffer to work around bbdb bug.
7381   (unless (eq major-mode 'gnus-summary-mode)
7382     (set-buffer gnus-summary-buffer))
7383   (let ((article (or article (gnus-summary-article-number)))
7384         (all-headers (not (not all-headers))) ;Must be t or nil.
7385         gnus-summary-display-article-function)
7386     (and (not pseudo)
7387          (gnus-summary-article-pseudo-p article)
7388          (error "This is a pseudo-article"))
7389     (save-excursion
7390       (set-buffer gnus-summary-buffer)
7391       (if (or (and gnus-single-article-buffer
7392                    (or (null gnus-current-article)
7393                        (null gnus-article-current)
7394                        (null (get-buffer gnus-article-buffer))
7395                        (not (eq article (cdr gnus-article-current)))
7396                        (not (equal (car gnus-article-current)
7397                                    gnus-newsgroup-name))))
7398               (and (not gnus-single-article-buffer)
7399                    (or (null gnus-current-article)
7400                        (not (eq gnus-current-article article))))
7401               force)
7402           ;; The requested article is different from the current article.
7403           (progn
7404             (gnus-summary-display-article article all-headers)
7405             (when (gnus-buffer-live-p gnus-article-buffer)
7406               (with-current-buffer gnus-article-buffer
7407                 (if (not gnus-article-decoded-p) ;; a local variable
7408                     (mm-disable-multibyte))))
7409             (gnus-article-set-window-start
7410              (cdr (assq article gnus-newsgroup-bookmarks)))
7411             article)
7412         'old))))
7413
7414 (defun gnus-summary-force-verify-and-decrypt ()
7415   "Display buttons for signed/encrypted parts and verify/decrypt them."
7416   (interactive)
7417   (let ((mm-verify-option 'known)
7418         (mm-decrypt-option 'known)
7419         (gnus-article-emulate-mime t)
7420         (gnus-buttonized-mime-types (append (list "multipart/signed"
7421                                                   "multipart/encrypted")
7422                                             gnus-buttonized-mime-types)))
7423     (gnus-summary-select-article nil 'force)))
7424
7425 (defun gnus-summary-set-current-mark (&optional current-mark)
7426   "Obsolete function."
7427   nil)
7428
7429 (defun gnus-summary-next-article (&optional unread subject backward push)
7430   "Select the next article.
7431 If UNREAD, only unread articles are selected.
7432 If SUBJECT, only articles with SUBJECT are selected.
7433 If BACKWARD, the previous article is selected instead of the next."
7434   (interactive "P")
7435   (cond
7436    ;; Is there such an article?
7437    ((and (gnus-summary-search-forward unread subject backward)
7438          (or (gnus-summary-display-article (gnus-summary-article-number))
7439              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7440     (gnus-summary-position-point))
7441    ;; If not, we try the first unread, if that is wanted.
7442    ((and subject
7443          gnus-auto-select-same
7444          (gnus-summary-first-unread-article))
7445     (gnus-summary-position-point)
7446     (gnus-message 6 "Wrapped"))
7447    ;; Try to get next/previous article not displayed in this group.
7448    ((and gnus-auto-extend-newsgroup
7449          (not unread) (not subject))
7450     (gnus-summary-goto-article
7451      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7452      nil (count-lines (point-min) (point))))
7453    ;; Go to next/previous group.
7454    (t
7455     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7456       (gnus-summary-jump-to-group gnus-newsgroup-name))
7457     (let ((cmd last-command-char)
7458           (point
7459            (with-current-buffer gnus-group-buffer
7460              (point)))
7461           (group
7462            (if (eq gnus-keep-same-level 'best)
7463                (gnus-summary-best-group gnus-newsgroup-name)
7464              (gnus-summary-search-group backward gnus-keep-same-level))))
7465       ;; For some reason, the group window gets selected.  We change
7466       ;; it back.
7467       (select-window (get-buffer-window (current-buffer)))
7468       ;; Select next unread newsgroup automagically.
7469       (cond
7470        ((or (not gnus-auto-select-next)
7471             (not cmd))
7472         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7473        ((or (eq gnus-auto-select-next 'quietly)
7474             (and (eq gnus-auto-select-next 'slightly-quietly)
7475                  push)
7476             (and (eq gnus-auto-select-next 'almost-quietly)
7477                  (gnus-summary-last-article-p)))
7478         ;; Select quietly.
7479         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7480             (gnus-summary-exit)
7481           (gnus-message 7 "No more%s articles (%s)..."
7482                         (if unread " unread" "")
7483                         (if group (concat "selecting " group)
7484                           "exiting"))
7485           (gnus-summary-next-group nil group backward)))
7486        (t
7487         (when (gnus-key-press-event-p last-input-event)
7488           (gnus-summary-walk-group-buffer
7489            gnus-newsgroup-name cmd unread backward point))))))))
7490
7491 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7492   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7493                       (?\C-p (gnus-group-prev-unread-group 1))))
7494         (cursor-in-echo-area t)
7495         keve key group ended prompt)
7496     (save-excursion
7497       (set-buffer gnus-group-buffer)
7498       (goto-char start)
7499       (setq group
7500             (if (eq gnus-keep-same-level 'best)
7501                 (gnus-summary-best-group gnus-newsgroup-name)
7502               (gnus-summary-search-group backward gnus-keep-same-level))))
7503     (while (not ended)
7504       (setq prompt
7505             (format
7506              "No more%s articles%s " (if unread " unread" "")
7507              (if (and group
7508                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7509                  (format " (Type %s for %s [%s])"
7510                          (single-key-description cmd)
7511                          (gnus-group-decoded-name group)
7512                          (gnus-group-unread group))
7513                (format " (Type %s to exit %s)"
7514                        (single-key-description cmd)
7515                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7516       ;; Confirm auto selection.
7517       (setq key (car (setq keve (gnus-read-event-char prompt)))
7518             ended t)
7519       (cond
7520        ((assq key keystrokes)
7521         (let ((obuf (current-buffer)))
7522           (switch-to-buffer gnus-group-buffer)
7523           (when group
7524             (gnus-group-jump-to-group group))
7525           (eval (cadr (assq key keystrokes)))
7526           (setq group (gnus-group-group-name))
7527           (switch-to-buffer obuf))
7528         (setq ended nil))
7529        ((equal key cmd)
7530         (if (or (not group)
7531                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7532             (gnus-summary-exit)
7533           (gnus-summary-next-group nil group backward)))
7534        (t
7535         (push (cdr keve) unread-command-events))))))
7536
7537 (defun gnus-summary-next-unread-article ()
7538   "Select unread article after current one."
7539   (interactive)
7540   (gnus-summary-next-article
7541    (or (not (eq gnus-summary-goto-unread 'never))
7542        (gnus-summary-last-article-p (gnus-summary-article-number)))
7543    (and gnus-auto-select-same
7544         (gnus-summary-article-subject))))
7545
7546 (defun gnus-summary-prev-article (&optional unread subject)
7547   "Select the article after the current one.
7548 If UNREAD is non-nil, only unread articles are selected."
7549   (interactive "P")
7550   (gnus-summary-next-article unread subject t))
7551
7552 (defun gnus-summary-prev-unread-article ()
7553   "Select unread article before current one."
7554   (interactive)
7555   (gnus-summary-prev-article
7556    (or (not (eq gnus-summary-goto-unread 'never))
7557        (gnus-summary-first-article-p (gnus-summary-article-number)))
7558    (and gnus-auto-select-same
7559         (gnus-summary-article-subject))))
7560
7561 (defun gnus-summary-next-page (&optional lines circular stop)
7562   "Show next page of the selected article.
7563 If at the end of the current article, select the next article.
7564 LINES says how many lines should be scrolled up.
7565
7566 If CIRCULAR is non-nil, go to the start of the article instead of
7567 selecting the next article when reaching the end of the current
7568 article.
7569
7570 If STOP is non-nil, just stop when reaching the end of the message.
7571
7572 Also see the variable `gnus-article-skip-boring'."
7573   (interactive "P")
7574   (setq gnus-summary-buffer (current-buffer))
7575   (gnus-set-global-variables)
7576   (let ((article (gnus-summary-article-number))
7577         (article-window (get-buffer-window gnus-article-buffer t))
7578         endp)
7579     ;; If the buffer is empty, we have no article.
7580     (unless article
7581       (error "No article to select"))
7582     (gnus-configure-windows 'article)
7583     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7584         (if (and (eq gnus-summary-goto-unread 'never)
7585                  (not (gnus-summary-last-article-p article)))
7586             (gnus-summary-next-article)
7587           (gnus-summary-next-unread-article))
7588       (if (or (null gnus-current-article)
7589               (null gnus-article-current)
7590               (/= article (cdr gnus-article-current))
7591               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7592           ;; Selected subject is different from current article's.
7593           (gnus-summary-display-article article)
7594         (when article-window
7595           (gnus-eval-in-buffer-window gnus-article-buffer
7596             (setq endp (or (gnus-article-next-page lines)
7597                            (gnus-article-only-boring-p))))
7598           (when endp
7599             (cond (stop
7600                    (gnus-message 3 "End of message"))
7601                   (circular
7602                    (gnus-summary-beginning-of-article))
7603                   (lines
7604                    (gnus-message 3 "End of message"))
7605                   ((null lines)
7606                    (if (and (eq gnus-summary-goto-unread 'never)
7607                             (not (gnus-summary-last-article-p article)))
7608                        (gnus-summary-next-article)
7609                      (gnus-summary-next-unread-article))))))))
7610     (gnus-summary-recenter)
7611     (gnus-summary-position-point)))
7612
7613 (defun gnus-summary-prev-page (&optional lines move)
7614   "Show previous page of selected article.
7615 Argument LINES specifies lines to be scrolled down.
7616 If MOVE, move to the previous unread article if point is at
7617 the beginning of the buffer."
7618   (interactive "P")
7619   (let ((article (gnus-summary-article-number))
7620         (article-window (get-buffer-window gnus-article-buffer t))
7621         endp)
7622     (gnus-configure-windows 'article)
7623     (if (or (null gnus-current-article)
7624             (null gnus-article-current)
7625             (/= article (cdr gnus-article-current))
7626             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7627         ;; Selected subject is different from current article's.
7628         (gnus-summary-display-article article)
7629       (gnus-summary-recenter)
7630       (when article-window
7631         (gnus-eval-in-buffer-window gnus-article-buffer
7632           (setq endp (gnus-article-prev-page lines)))
7633         (when (and move endp)
7634           (cond (lines
7635                  (gnus-message 3 "Beginning of message"))
7636                 ((null lines)
7637                  (if (and (eq gnus-summary-goto-unread 'never)
7638                           (not (gnus-summary-first-article-p article)))
7639                      (gnus-summary-prev-article)
7640                    (gnus-summary-prev-unread-article))))))))
7641   (gnus-summary-position-point))
7642
7643 (defun gnus-summary-prev-page-or-article (&optional lines)
7644   "Show previous page of selected article.
7645 Argument LINES specifies lines to be scrolled down.
7646 If at the beginning of the article, go to the next article."
7647   (interactive "P")
7648   (gnus-summary-prev-page lines t))
7649
7650 (defun gnus-summary-scroll-up (lines)
7651   "Scroll up (or down) one line current article.
7652 Argument LINES specifies lines to be scrolled up (or down if negative)."
7653   (interactive "p")
7654   (gnus-configure-windows 'article)
7655   (gnus-summary-show-thread)
7656   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7657     (gnus-eval-in-buffer-window gnus-article-buffer
7658       (cond ((> lines 0)
7659              (when (gnus-article-next-page lines)
7660                (gnus-message 3 "End of message")))
7661             ((< lines 0)
7662              (gnus-article-prev-page (- lines))))))
7663   (gnus-summary-recenter)
7664   (gnus-summary-position-point))
7665
7666 (defun gnus-summary-scroll-down (lines)
7667   "Scroll down (or up) one line current article.
7668 Argument LINES specifies lines to be scrolled down (or up if negative)."
7669   (interactive "p")
7670   (gnus-summary-scroll-up (- lines)))
7671
7672 (defun gnus-summary-next-same-subject ()
7673   "Select next article which has the same subject as current one."
7674   (interactive)
7675   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7676
7677 (defun gnus-summary-prev-same-subject ()
7678   "Select previous article which has the same subject as current one."
7679   (interactive)
7680   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7681
7682 (defun gnus-summary-next-unread-same-subject ()
7683   "Select next unread article which has the same subject as current one."
7684   (interactive)
7685   (gnus-summary-next-article t (gnus-summary-article-subject)))
7686
7687 (defun gnus-summary-prev-unread-same-subject ()
7688   "Select previous unread article which has the same subject as current one."
7689   (interactive)
7690   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7691
7692 (defun gnus-summary-first-unread-article ()
7693   "Select the first unread article.
7694 Return nil if there are no unread articles."
7695   (interactive)
7696   (prog1
7697       (when (gnus-summary-first-subject t)
7698         (gnus-summary-show-thread)
7699         (gnus-summary-first-subject t)
7700         (gnus-summary-display-article (gnus-summary-article-number)))
7701     (gnus-summary-position-point)))
7702
7703 (defun gnus-summary-first-unread-subject ()
7704   "Place the point on the subject line of the first unread article.
7705 Return nil if there are no unread articles."
7706   (interactive)
7707   (prog1
7708       (when (gnus-summary-first-subject t)
7709         (gnus-summary-show-thread)
7710         (gnus-summary-first-subject t))
7711     (gnus-summary-position-point)))
7712
7713 (defun gnus-summary-first-unseen-subject ()
7714   "Place the point on the subject line of the first unseen article.
7715 Return nil if there are no unseen articles."
7716   (interactive)
7717   (prog1
7718       (when (gnus-summary-first-subject nil nil t)
7719         (gnus-summary-show-thread)
7720         (gnus-summary-first-subject nil nil t))
7721     (gnus-summary-position-point)))
7722
7723 (defun gnus-summary-first-unseen-or-unread-subject ()
7724   "Place the point on the subject line of the first unseen article or,
7725 if all article have been seen, on the subject line of the first unread
7726 article."
7727   (interactive)
7728   (prog1
7729       (unless (when (gnus-summary-first-subject nil nil t)
7730                 (gnus-summary-show-thread)
7731                 (gnus-summary-first-subject nil nil t))
7732         (when (gnus-summary-first-subject t)
7733           (gnus-summary-show-thread)
7734           (gnus-summary-first-subject t)))
7735     (gnus-summary-position-point)))
7736
7737 (defun gnus-summary-first-article ()
7738   "Select the first article.
7739 Return nil if there are no articles."
7740   (interactive)
7741   (prog1
7742       (when (gnus-summary-first-subject)
7743         (gnus-summary-show-thread)
7744         (gnus-summary-first-subject)
7745         (gnus-summary-display-article (gnus-summary-article-number)))
7746     (gnus-summary-position-point)))
7747
7748 (defun gnus-summary-best-unread-article (&optional arg)
7749   "Select the unread article with the highest score.
7750 If given a prefix argument, select the next unread article that has a
7751 score higher than the default score."
7752   (interactive "P")
7753   (let ((article (if arg
7754                      (gnus-summary-better-unread-subject)
7755                    (gnus-summary-best-unread-subject))))
7756     (if article
7757         (gnus-summary-goto-article article)
7758       (error "No unread articles"))))
7759
7760 (defun gnus-summary-best-unread-subject ()
7761   "Select the unread subject with the highest score."
7762   (interactive)
7763   (let ((best -1000000)
7764         (data gnus-newsgroup-data)
7765         article score)
7766     (while data
7767       (and (gnus-data-unread-p (car data))
7768            (> (setq score
7769                     (gnus-summary-article-score (gnus-data-number (car data))))
7770               best)
7771            (setq best score
7772                  article (gnus-data-number (car data))))
7773       (setq data (cdr data)))
7774     (when article
7775       (gnus-summary-goto-subject article))
7776     (gnus-summary-position-point)
7777     article))
7778
7779 (defun gnus-summary-better-unread-subject ()
7780   "Select the first unread subject that has a score over the default score."
7781   (interactive)
7782   (let ((data gnus-newsgroup-data)
7783         article score)
7784     (while (and (setq article (gnus-data-number (car data)))
7785                 (or (gnus-data-read-p (car data))
7786                     (not (> (gnus-summary-article-score article)
7787                             gnus-summary-default-score))))
7788       (setq data (cdr data)))
7789     (when article
7790       (gnus-summary-goto-subject article))
7791     (gnus-summary-position-point)
7792     article))
7793
7794 (defun gnus-summary-last-subject ()
7795   "Go to the last displayed subject line in the group."
7796   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7797     (when article
7798       (gnus-summary-goto-subject article))))
7799
7800 (defun gnus-summary-goto-article (article &optional all-headers force)
7801   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7802 If ALL-HEADERS is non-nil, no header lines are hidden.
7803 If FORCE, go to the article even if it isn't displayed.  If FORCE
7804 is a number, it is the line the article is to be displayed on."
7805   (interactive
7806    (list
7807     (completing-read
7808      "Article number or Message-ID: "
7809      (mapcar (lambda (number) (list (int-to-string number)))
7810              gnus-newsgroup-limit))
7811     current-prefix-arg
7812     t))
7813   (prog1
7814       (if (and (stringp article)
7815                (string-match "@\\|%40" article))
7816           (gnus-summary-refer-article article)
7817         (when (stringp article)
7818           (setq article (string-to-number article)))
7819         (if (gnus-summary-goto-subject article force)
7820             (gnus-summary-display-article article all-headers)
7821           (gnus-message 4 "Couldn't go to article %s" article) nil))
7822     (gnus-summary-position-point)))
7823
7824 (defun gnus-summary-goto-last-article ()
7825   "Go to the previously read article."
7826   (interactive)
7827   (prog1
7828       (when gnus-last-article
7829         (gnus-summary-goto-article gnus-last-article nil t))
7830     (gnus-summary-position-point)))
7831
7832 (defun gnus-summary-pop-article (number)
7833   "Pop one article off the history and go to the previous.
7834 NUMBER articles will be popped off."
7835   (interactive "p")
7836   (let (to)
7837     (setq gnus-newsgroup-history
7838           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7839     (if to
7840         (gnus-summary-goto-article (car to) nil t)
7841       (error "Article history empty")))
7842   (gnus-summary-position-point))
7843
7844 ;; Summary commands and functions for limiting the summary buffer.
7845
7846 (defun gnus-summary-limit-to-articles (n)
7847   "Limit the summary buffer to the next N articles.
7848 If not given a prefix, use the process marked articles instead."
7849   (interactive "P")
7850   (prog1
7851       (let ((articles (gnus-summary-work-articles n)))
7852         (setq gnus-newsgroup-processable nil)
7853         (gnus-summary-limit articles))
7854     (gnus-summary-position-point)))
7855
7856 (defun gnus-summary-pop-limit (&optional total)
7857   "Restore the previous limit.
7858 If given a prefix, remove all limits."
7859   (interactive "P")
7860   (when total
7861     (setq gnus-newsgroup-limits
7862           (list (mapcar (lambda (h) (mail-header-number h))
7863                         gnus-newsgroup-headers))))
7864   (unless gnus-newsgroup-limits
7865     (error "No limit to pop"))
7866   (prog1
7867       (gnus-summary-limit nil 'pop)
7868     (gnus-summary-position-point)))
7869
7870 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7871   "Limit the summary buffer to articles that have subjects that match a regexp.
7872 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7873   (interactive
7874    (list (read-string (if current-prefix-arg
7875                           "Exclude subject (regexp): "
7876                         "Limit to subject (regexp): "))
7877          nil current-prefix-arg))
7878   (unless header
7879     (setq header "subject"))
7880   (when (not (equal "" subject))
7881     (prog1
7882         (let ((articles (gnus-summary-find-matching
7883                          (or header "subject") subject 'all nil nil
7884                          not-matching)))
7885           (unless articles
7886             (error "Found no matches for \"%s\"" subject))
7887           (gnus-summary-limit articles))
7888       (gnus-summary-position-point))))
7889
7890 (defun gnus-summary-limit-to-author (from &optional not-matching)
7891   "Limit the summary buffer to articles that have authors that match a regexp.
7892 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7893   (interactive
7894    (list (read-string (if current-prefix-arg
7895                           "Exclude author (regexp): "
7896                         "Limit to author (regexp): "))
7897          current-prefix-arg))
7898   (gnus-summary-limit-to-subject from "from" not-matching))
7899
7900 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7901   "Limit the summary buffer to articles with the given RECIPIENT.
7902
7903 If NOT-MATCHING, exclude RECIPIENT.
7904
7905 To and Cc headers are checked.  You need to include them in
7906 `nnmail-extra-headers'."
7907   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7908   (interactive
7909    (list (read-string (format "%s recipient (regexp): "
7910                               (if current-prefix-arg "Exclude" "Limit to")))
7911          current-prefix-arg))
7912   (when (not (equal "" recipient))
7913     (prog1 (let* ((to
7914                    (if (memq 'To nnmail-extra-headers)
7915                        (gnus-summary-find-matching
7916                         (cons 'extra 'To) recipient 'all nil nil
7917                         not-matching)
7918                      (gnus-message
7919                       1 "`To' isn't present in `nnmail-extra-headers'")
7920                      (sit-for 1)
7921                      nil))
7922                   (cc
7923                    (if (memq 'Cc nnmail-extra-headers)
7924                        (gnus-summary-find-matching
7925                         (cons 'extra 'Cc) recipient 'all nil nil
7926                         not-matching)
7927                      (gnus-message
7928                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7929                      (sit-for 1)
7930                      nil))
7931                   (articles
7932                    (if not-matching
7933                        ;; We need the numbers that are in both lists:
7934                        (mapcar (lambda (a)
7935                                  (and (memq a to) a))
7936                                cc)
7937                      (nconc to cc))))
7938              (unless articles
7939                (error "Found no matches for \"%s\"" recipient))
7940              (gnus-summary-limit articles))
7941       (gnus-summary-position-point))))
7942
7943 (defun gnus-summary-limit-strange-charsets-predicate (header)
7944   (let ((string (concat (mail-header-subject header)
7945                         (mail-header-from header)))
7946         charset found)
7947     (dotimes (i (1- (length string)))
7948       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
7949       (when (string-match "unicode\\|big\\|japanese" charset)
7950         (setq found t)))
7951     found))
7952
7953 (defun gnus-summary-limit-to-predicate (predicate)
7954   "Limit to articles where PREDICATE returns non-nil.
7955 PREDICATE will be called with the header structures of the
7956 articles."
7957   (let ((articles nil)
7958         (case-fold-search t))
7959     (dolist (header gnus-newsgroup-headers)
7960       (when (funcall predicate header)
7961         (push (mail-header-number header) articles)))
7962     (gnus-summary-limit (nreverse articles))))
7963
7964 (defun gnus-summary-limit-to-age (age &optional younger-p)
7965   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7966 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7967 articles that are younger than AGE days."
7968   (interactive
7969    (let ((younger current-prefix-arg)
7970          (days-got nil)
7971          days)
7972      (while (not days-got)
7973        (setq days (if younger
7974                       (read-string "Limit to articles younger than (in days, older when negative): ")
7975                     (read-string
7976                      "Limit to articles older than (in days, younger when negative): ")))
7977        (when (> (length days) 0)
7978          (setq days (read days)))
7979        (if (numberp days)
7980            (progn
7981              (setq days-got t)
7982              (when (< days 0)
7983                (setq younger (not younger))
7984                (setq days (* days -1))))
7985          (message "Please enter a number.")
7986          (sleep-for 1)))
7987      (list days younger)))
7988   (prog1
7989       (let ((data gnus-newsgroup-data)
7990             (cutoff (days-to-time age))
7991             articles d date is-younger)
7992         (while (setq d (pop data))
7993           (when (and (vectorp (gnus-data-header d))
7994                      (setq date (mail-header-date (gnus-data-header d))))
7995             (setq is-younger (time-less-p
7996                               (time-since (condition-case ()
7997                                               (date-to-time date)
7998                                             (error '(0 0))))
7999                               cutoff))
8000             (when (if younger-p
8001                       is-younger
8002                     (not is-younger))
8003               (push (gnus-data-number d) articles))))
8004         (gnus-summary-limit (nreverse articles)))
8005     (gnus-summary-position-point)))
8006
8007 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8008   "Limit the summary buffer to articles that match an 'extra' header."
8009   (interactive
8010    (let ((header
8011           (intern
8012            (gnus-completing-read-with-default
8013             (symbol-name (car gnus-extra-headers))
8014             (if current-prefix-arg
8015                 "Exclude extra header"
8016               "Limit extra header")
8017             (mapcar (lambda (x)
8018                       (cons (symbol-name x) x))
8019                     gnus-extra-headers)
8020             nil
8021             t))))
8022      (list header
8023            (read-string (format "%s header %s (regexp): "
8024                                 (if current-prefix-arg "Exclude" "Limit to")
8025                                 header))
8026            current-prefix-arg)))
8027   (when (not (equal "" regexp))
8028     (prog1
8029         (let ((articles (gnus-summary-find-matching
8030                          (cons 'extra header) regexp 'all nil nil
8031                          not-matching)))
8032           (unless articles
8033             (error "Found no matches for \"%s\"" regexp))
8034           (gnus-summary-limit articles))
8035       (gnus-summary-position-point))))
8036
8037 (defun gnus-summary-limit-to-display-predicate ()
8038   "Limit the summary buffer to the predicated in the `display' group parameter."
8039   (interactive)
8040   (unless gnus-newsgroup-display
8041     (error "There is no `display' group parameter"))
8042   (let (articles)
8043     (dolist (number gnus-newsgroup-articles)
8044       (when (funcall gnus-newsgroup-display)
8045         (push number articles)))
8046     (gnus-summary-limit articles))
8047   (gnus-summary-position-point))
8048
8049 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8050 (make-obsolete
8051  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8052
8053 (defun gnus-summary-limit-to-unread (&optional all)
8054   "Limit the summary buffer to articles that are not marked as read.
8055 If ALL is non-nil, limit strictly to unread articles."
8056   (interactive "P")
8057   (if all
8058       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8059     (gnus-summary-limit-to-marks
8060      ;; Concat all the marks that say that an article is read and have
8061      ;; those removed.
8062      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8063            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8064            gnus-low-score-mark gnus-expirable-mark
8065            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8066            gnus-duplicate-mark gnus-souped-mark)
8067      'reverse)))
8068
8069 (defun gnus-summary-limit-to-headers (match &optional reverse)
8070   "Limit the summary buffer to articles that have headers that match MATCH.
8071 If REVERSE (the prefix), limit to articles that don't match."
8072   (interactive "sMatch headers (regexp): \nP")
8073   (gnus-summary-limit-to-bodies match reverse t))
8074
8075 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8076   "Limit the summary buffer to articles that have bodies that match MATCH.
8077 If REVERSE (the prefix), limit to articles that don't match."
8078   (interactive "sMatch body (regexp): \nP")
8079   (let ((articles nil)
8080         (gnus-select-article-hook nil)  ;Disable hook.
8081         (gnus-article-prepare-hook nil)
8082         (gnus-use-article-prefetch nil)
8083         (gnus-keep-backlog nil)
8084         (gnus-break-pages nil)
8085         (gnus-summary-display-arrow nil)
8086         (gnus-updated-mode-lines nil)
8087         (gnus-auto-center-summary nil)
8088         (gnus-display-mime-function nil))
8089     (dolist (data gnus-newsgroup-data)
8090       (let (gnus-mark-article-hook)
8091         (gnus-summary-select-article t t nil (gnus-data-number data)))
8092       (save-excursion
8093         (set-buffer gnus-article-buffer)
8094         (article-goto-body)
8095         (let* ((case-fold-search t)
8096                (found (if headersp
8097                           (re-search-backward match nil t)
8098                         (re-search-forward match nil t))))
8099           (when (or (and found
8100                          (not reverse))
8101                     (and (not found)
8102                          reverse))
8103             (push (gnus-data-number data) articles)))))
8104     (if (not articles)
8105         (message "No messages matched")
8106       (gnus-summary-limit articles)))
8107   (gnus-summary-position-point))
8108
8109 (defun gnus-summary-limit-to-replied (&optional unreplied)
8110   "Limit the summary buffer to replied articles.
8111 If UNREPLIED (the prefix), limit to unreplied articles."
8112   (interactive "P")
8113   (if unreplied
8114       (gnus-summary-limit
8115        (gnus-set-difference gnus-newsgroup-articles
8116         gnus-newsgroup-replied))
8117     (gnus-summary-limit gnus-newsgroup-replied))
8118   (gnus-summary-position-point))
8119
8120 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8121 (make-obsolete 'gnus-summary-delete-marked-with
8122                'gnus-summary-limit-exclude-marks)
8123
8124 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8125   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8126 If REVERSE, limit the summary buffer to articles that are marked
8127 with MARKS.  MARKS can either be a string of marks or a list of marks.
8128 Returns how many articles were removed."
8129   (interactive "sMarks: ")
8130   (gnus-summary-limit-to-marks marks t))
8131
8132 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8133   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8134 If REVERSE (the prefix), limit the summary buffer to articles that are
8135 not marked with MARKS.  MARKS can either be a string of marks or a
8136 list of marks.
8137 Returns how many articles were removed."
8138   (interactive "sMarks: \nP")
8139   (prog1
8140       (let ((data gnus-newsgroup-data)
8141             (marks (if (listp marks) marks
8142                      (append marks nil))) ; Transform to list.
8143             articles)
8144         (while data
8145           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8146                   (memq (gnus-data-mark (car data)) marks))
8147             (push (gnus-data-number (car data)) articles))
8148           (setq data (cdr data)))
8149         (gnus-summary-limit articles))
8150     (gnus-summary-position-point)))
8151
8152 (defun gnus-summary-limit-to-score (score)
8153   "Limit to articles with score at or above SCORE."
8154   (interactive "NLimit to articles with score of at least: ")
8155   (let ((data gnus-newsgroup-data)
8156         articles)
8157     (while data
8158       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8159                 score)
8160         (push (gnus-data-number (car data)) articles))
8161       (setq data (cdr data)))
8162     (prog1
8163         (gnus-summary-limit articles)
8164       (gnus-summary-position-point))))
8165
8166 (defun gnus-summary-limit-to-unseen ()
8167   "Limit to unseen articles."
8168   (interactive)
8169   (prog1
8170       (gnus-summary-limit gnus-newsgroup-unseen)
8171     (gnus-summary-position-point)))
8172
8173 (defun gnus-summary-limit-include-thread (id)
8174   "Display all the hidden articles that is in the thread with ID in it.
8175 When called interactively, ID is the Message-ID of the current
8176 article."
8177   (interactive (list (mail-header-id (gnus-summary-article-header))))
8178   (let ((articles (gnus-articles-in-thread
8179                    (gnus-id-to-thread (gnus-root-id id)))))
8180     (prog1
8181         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8182       (gnus-summary-limit-include-matching-articles
8183        "subject"
8184        (regexp-quote (gnus-simplify-subject-re
8185                       (mail-header-subject (gnus-id-to-header id)))))
8186       (gnus-summary-position-point))))
8187
8188 (defun gnus-summary-limit-include-matching-articles (header regexp)
8189   "Display all the hidden articles that have HEADERs that match REGEXP."
8190   (interactive (list (read-string "Match on header: ")
8191                      (read-string "Regexp: ")))
8192   (let ((articles (gnus-find-matching-articles header regexp)))
8193     (prog1
8194         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8195       (gnus-summary-position-point))))
8196
8197 (defun gnus-summary-insert-dormant-articles ()
8198   "Insert all the dormant articles for this group into the current buffer."
8199   (interactive)
8200   (let ((gnus-verbose (max 6 gnus-verbose)))
8201     (if (not gnus-newsgroup-dormant)
8202         (gnus-message 3 "No cached articles for this group")
8203       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8204
8205 (defun gnus-summary-limit-include-dormant ()
8206   "Display all the hidden articles that are marked as dormant.
8207 Note that this command only works on a subset of the articles currently
8208 fetched for this group."
8209   (interactive)
8210   (unless gnus-newsgroup-dormant
8211     (error "There are no dormant articles in this group"))
8212   (prog1
8213       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8214     (gnus-summary-position-point)))
8215
8216 (defun gnus-summary-limit-exclude-dormant ()
8217   "Hide all dormant articles."
8218   (interactive)
8219   (prog1
8220       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8221     (gnus-summary-position-point)))
8222
8223 (defun gnus-summary-limit-exclude-childless-dormant ()
8224   "Hide all dormant articles that have no children."
8225   (interactive)
8226   (let ((data (gnus-data-list t))
8227         articles d children)
8228     ;; Find all articles that are either not dormant or have
8229     ;; children.
8230     (while (setq d (pop data))
8231       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8232                 (and (setq children
8233                            (gnus-article-children (gnus-data-number d)))
8234                      (let (found)
8235                        (while children
8236                          (when (memq (car children) articles)
8237                            (setq children nil
8238                                  found t))
8239                          (pop children))
8240                        found)))
8241         (push (gnus-data-number d) articles)))
8242     ;; Do the limiting.
8243     (prog1
8244         (gnus-summary-limit articles)
8245       (gnus-summary-position-point))))
8246
8247 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8248   "Mark all unread excluded articles as read.
8249 If ALL, mark even excluded ticked and dormants as read."
8250   (interactive "P")
8251   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8252   (let ((articles (gnus-sorted-ndifference
8253                    (sort
8254                     (mapcar (lambda (h) (mail-header-number h))
8255                             gnus-newsgroup-headers)
8256                     '<)
8257                    gnus-newsgroup-limit))
8258         article)
8259     (setq gnus-newsgroup-unreads
8260           (gnus-sorted-intersection gnus-newsgroup-unreads
8261                                     gnus-newsgroup-limit))
8262     (if all
8263         (setq gnus-newsgroup-dormant nil
8264               gnus-newsgroup-marked nil
8265               gnus-newsgroup-reads
8266               (nconc
8267                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8268                gnus-newsgroup-reads))
8269       (while (setq article (pop articles))
8270         (unless (or (memq article gnus-newsgroup-dormant)
8271                     (memq article gnus-newsgroup-marked))
8272           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8273
8274 (defun gnus-summary-limit (articles &optional pop)
8275   (if pop
8276       ;; We pop the previous limit off the stack and use that.
8277       (setq articles (car gnus-newsgroup-limits)
8278             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8279     ;; We use the new limit, so we push the old limit on the stack.
8280     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8281   ;; Set the limit.
8282   (setq gnus-newsgroup-limit articles)
8283   (let ((total (length gnus-newsgroup-data))
8284         (data (gnus-data-find-list (gnus-summary-article-number)))
8285         (gnus-summary-mark-below nil)   ; Inhibit this.
8286         found)
8287     ;; This will do all the work of generating the new summary buffer
8288     ;; according to the new limit.
8289     (gnus-summary-prepare)
8290     ;; Hide any threads, possibly.
8291     (gnus-summary-maybe-hide-threads)
8292     ;; Try to return to the article you were at, or one in the
8293     ;; neighborhood.
8294     (when data
8295       ;; We try to find some article after the current one.
8296       (while data
8297         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8298           (setq data nil
8299                 found t))
8300         (setq data (cdr data))))
8301     (unless found
8302       ;; If there is no data, that means that we were after the last
8303       ;; article.  The same goes when we can't find any articles
8304       ;; after the current one.
8305       (goto-char (point-max))
8306       (gnus-summary-find-prev))
8307     (gnus-set-mode-line 'summary)
8308     ;; We return how many articles were removed from the summary
8309     ;; buffer as a result of the new limit.
8310     (- total (length gnus-newsgroup-data))))
8311
8312 (defsubst gnus-invisible-cut-children (threads)
8313   (let ((num 0))
8314     (while threads
8315       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8316         (incf num))
8317       (pop threads))
8318     (< num 2)))
8319
8320 (defsubst gnus-cut-thread (thread)
8321   "Go forwards in the thread until we find an article that we want to display."
8322   (when (or (eq gnus-fetch-old-headers 'some)
8323             (eq gnus-fetch-old-headers 'invisible)
8324             (numberp gnus-fetch-old-headers)
8325             (eq gnus-build-sparse-threads 'some)
8326             (eq gnus-build-sparse-threads 'more))
8327     ;; Deal with old-fetched headers and sparse threads.
8328     (while (and
8329             thread
8330             (or
8331              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8332              (gnus-summary-article-ancient-p
8333               (mail-header-number (car thread))))
8334             (if (or (<= (length (cdr thread)) 1)
8335                     (eq gnus-fetch-old-headers 'invisible))
8336                 (setq gnus-newsgroup-limit
8337                       (delq (mail-header-number (car thread))
8338                             gnus-newsgroup-limit)
8339                       thread (cadr thread))
8340               (when (gnus-invisible-cut-children (cdr thread))
8341                 (let ((th (cdr thread)))
8342                   (while th
8343                     (if (memq (mail-header-number (caar th))
8344                               gnus-newsgroup-limit)
8345                         (setq thread (car th)
8346                               th nil)
8347                       (setq th (cdr th))))))))))
8348   thread)
8349
8350 (defun gnus-cut-threads (threads)
8351   "Cut off all uninteresting articles from the beginning of THREADS."
8352   (when (or (eq gnus-fetch-old-headers 'some)
8353             (eq gnus-fetch-old-headers 'invisible)
8354             (numberp gnus-fetch-old-headers)
8355             (eq gnus-build-sparse-threads 'some)
8356             (eq gnus-build-sparse-threads 'more))
8357     (let ((th threads))
8358       (while th
8359         (setcar th (gnus-cut-thread (car th)))
8360         (setq th (cdr th)))))
8361   ;; Remove nixed out threads.
8362   (delq nil threads))
8363
8364 (defun gnus-summary-initial-limit (&optional show-if-empty)
8365   "Figure out what the initial limit is supposed to be on group entry.
8366 This entails weeding out unwanted dormants, low-scored articles,
8367 fetch-old-headers verbiage, and so on."
8368   ;; Most groups have nothing to remove.
8369   (if (or gnus-inhibit-limiting
8370           (and (null gnus-newsgroup-dormant)
8371                (eq gnus-newsgroup-display 'gnus-not-ignore)
8372                (not (eq gnus-fetch-old-headers 'some))
8373                (not (numberp gnus-fetch-old-headers))
8374                (not (eq gnus-fetch-old-headers 'invisible))
8375                (null gnus-summary-expunge-below)
8376                (not (eq gnus-build-sparse-threads 'some))
8377                (not (eq gnus-build-sparse-threads 'more))
8378                (null gnus-thread-expunge-below)
8379                (not gnus-use-nocem)))
8380       ()                                ; Do nothing.
8381     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8382     (setq gnus-newsgroup-limit nil)
8383     (mapatoms
8384      (lambda (node)
8385        (unless (car (symbol-value node))
8386          ;; These threads have no parents -- they are roots.
8387          (let ((nodes (cdr (symbol-value node)))
8388                thread)
8389            (while nodes
8390              (if (and gnus-thread-expunge-below
8391                       (< (gnus-thread-total-score (car nodes))
8392                          gnus-thread-expunge-below))
8393                  (gnus-expunge-thread (pop nodes))
8394                (setq thread (pop nodes))
8395                (gnus-summary-limit-children thread))))))
8396      gnus-newsgroup-dependencies)
8397     ;; If this limitation resulted in an empty group, we might
8398     ;; pop the previous limit and use it instead.
8399     (when (and (not gnus-newsgroup-limit)
8400                show-if-empty)
8401       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8402     gnus-newsgroup-limit))
8403
8404 (defun gnus-summary-limit-children (thread)
8405   "Return 1 if this subthread is visible and 0 if it is not."
8406   ;; First we get the number of visible children to this thread.  This
8407   ;; is done by recursing down the thread using this function, so this
8408   ;; will really go down to a leaf article first, before slowly
8409   ;; working its way up towards the root.
8410   (when thread
8411     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8412            (children
8413            (if (cdr thread)
8414                (apply '+ (mapcar 'gnus-summary-limit-children
8415                                  (cdr thread)))
8416              0))
8417           (number (mail-header-number (car thread)))
8418           score)
8419       (if (and
8420            (not (memq number gnus-newsgroup-marked))
8421            (or
8422             ;; If this article is dormant and has absolutely no visible
8423             ;; children, then this article isn't visible.
8424             (and (memq number gnus-newsgroup-dormant)
8425                  (zerop children))
8426             ;; If this is "fetch-old-headered" and there is no
8427             ;; visible children, then we don't want this article.
8428             (and (or (eq gnus-fetch-old-headers 'some)
8429                      (numberp gnus-fetch-old-headers))
8430                  (gnus-summary-article-ancient-p number)
8431                  (zerop children))
8432             ;; If this is "fetch-old-headered" and `invisible', then
8433             ;; we don't want this article.
8434             (and (eq gnus-fetch-old-headers 'invisible)
8435                  (gnus-summary-article-ancient-p number))
8436             ;; If this is a sparsely inserted article with no children,
8437             ;; we don't want it.
8438             (and (eq gnus-build-sparse-threads 'some)
8439                  (gnus-summary-article-sparse-p number)
8440                  (zerop children))
8441             ;; If we use expunging, and this article is really
8442             ;; low-scored, then we don't want this article.
8443             (when (and gnus-summary-expunge-below
8444                        (< (setq score
8445                                 (or (cdr (assq number gnus-newsgroup-scored))
8446                                     gnus-summary-default-score))
8447                           gnus-summary-expunge-below))
8448               ;; We increase the expunge-tally here, but that has
8449               ;; nothing to do with the limits, really.
8450               (incf gnus-newsgroup-expunged-tally)
8451               ;; We also mark as read here, if that's wanted.
8452               (when (and gnus-summary-mark-below
8453                          (< score gnus-summary-mark-below))
8454                 (setq gnus-newsgroup-unreads
8455                       (delq number gnus-newsgroup-unreads))
8456                 (if gnus-newsgroup-auto-expire
8457                     (push number gnus-newsgroup-expirable)
8458                   (push (cons number gnus-low-score-mark)
8459                         gnus-newsgroup-reads)))
8460               t)
8461             ;; Do the `display' group parameter.
8462             (and gnus-newsgroup-display
8463                  (not (funcall gnus-newsgroup-display)))
8464             ;; Check NoCeM things.
8465             (when (and gnus-use-nocem
8466                        (gnus-nocem-unwanted-article-p
8467                         (mail-header-id (car thread))))
8468               (setq gnus-newsgroup-unreads
8469                     (delq number gnus-newsgroup-unreads))
8470               t)))
8471           ;; Nope, invisible article.
8472           0
8473         ;; Ok, this article is to be visible, so we add it to the limit
8474         ;; and return 1.
8475         (push number gnus-newsgroup-limit)
8476         1))))
8477
8478 (defun gnus-expunge-thread (thread)
8479   "Mark all articles in THREAD as read."
8480   (let* ((number (mail-header-number (car thread))))
8481     (incf gnus-newsgroup-expunged-tally)
8482     ;; We also mark as read here, if that's wanted.
8483     (setq gnus-newsgroup-unreads
8484           (delq number gnus-newsgroup-unreads))
8485     (if gnus-newsgroup-auto-expire
8486         (push number gnus-newsgroup-expirable)
8487       (push (cons number gnus-low-score-mark)
8488             gnus-newsgroup-reads)))
8489   ;; Go recursively through all subthreads.
8490   (mapcar 'gnus-expunge-thread (cdr thread)))
8491
8492 ;; Summary article oriented commands
8493
8494 (defun gnus-summary-refer-parent-article (n)
8495   "Refer parent article N times.
8496 If N is negative, go to ancestor -N instead.
8497 The difference between N and the number of articles fetched is returned."
8498   (interactive "p")
8499   (let ((skip 1)
8500         error header ref)
8501     (when (not (natnump n))
8502       (setq skip (abs n)
8503             n 1))
8504     (while (and (> n 0)
8505                 (not error))
8506       (setq header (gnus-summary-article-header))
8507       (if (and (eq (mail-header-number header)
8508                    (cdr gnus-article-current))
8509                (equal gnus-newsgroup-name
8510                       (car gnus-article-current)))
8511           ;; If we try to find the parent of the currently
8512           ;; displayed article, then we take a look at the actual
8513           ;; References header, since this is slightly more
8514           ;; reliable than the References field we got from the
8515           ;; server.
8516           (save-excursion
8517             (set-buffer gnus-original-article-buffer)
8518             (nnheader-narrow-to-headers)
8519             (unless (setq ref (message-fetch-field "references"))
8520               (when (setq ref (message-fetch-field "in-reply-to"))
8521                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8522             (widen))
8523         (setq ref
8524               ;; It's not the current article, so we take a bet on
8525               ;; the value we got from the server.
8526               (mail-header-references header)))
8527       (if (and ref
8528                (not (equal ref "")))
8529           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8530             (gnus-message 1 "Couldn't find parent"))
8531         (gnus-message 1 "No references in article %d"
8532                       (gnus-summary-article-number))
8533         (setq error t))
8534       (decf n))
8535     (gnus-summary-position-point)
8536     n))
8537
8538 (defun gnus-summary-refer-references ()
8539   "Fetch all articles mentioned in the References header.
8540 Return the number of articles fetched."
8541   (interactive)
8542   (let ((ref (mail-header-references (gnus-summary-article-header)))
8543         (current (gnus-summary-article-number))
8544         (n 0))
8545     (if (or (not ref)
8546             (equal ref ""))
8547         (error "No References in the current article")
8548       ;; For each Message-ID in the References header...
8549       (while (string-match "<[^>]*>" ref)
8550         (incf n)
8551         ;; ... fetch that article.
8552         (gnus-summary-refer-article
8553          (prog1 (match-string 0 ref)
8554            (setq ref (substring ref (match-end 0))))))
8555       (gnus-summary-goto-subject current)
8556       (gnus-summary-position-point)
8557       n)))
8558
8559 (defun gnus-summary-refer-thread (&optional limit)
8560   "Fetch all articles in the current thread.
8561 If LIMIT (the numerical prefix), fetch that many old headers instead
8562 of what's specified by the `gnus-refer-thread-limit' variable."
8563   (interactive "P")
8564   (let ((id (mail-header-id (gnus-summary-article-header)))
8565         (limit (if limit (prefix-numeric-value limit)
8566                  gnus-refer-thread-limit)))
8567     (unless (eq gnus-fetch-old-headers 'invisible)
8568       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8569       ;; Retrieve the headers and read them in.
8570       (if (eq (if (numberp limit)
8571                   (gnus-retrieve-headers
8572                    (list (min
8573                           (+ (mail-header-number
8574                               (gnus-summary-article-header))
8575                              limit)
8576                           gnus-newsgroup-end))
8577                    gnus-newsgroup-name (* limit 2))
8578                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8579                 ;; headers.
8580                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8581                                        gnus-newsgroup-name limit))
8582               'nov)
8583           (gnus-build-all-threads)
8584         (error "Can't fetch thread from back ends that don't support NOV"))
8585       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8586     (gnus-summary-limit-include-thread id)))
8587
8588 (defun gnus-summary-refer-article (message-id)
8589   "Fetch an article specified by MESSAGE-ID."
8590   (interactive "sMessage-ID: ")
8591   (when (and (stringp message-id)
8592              (not (zerop (length message-id))))
8593     (setq message-id (gnus-replace-in-string message-id " " ""))
8594     ;; Construct the correct Message-ID if necessary.
8595     ;; Suggested by tale@pawl.rpi.edu.
8596     (unless (string-match "^<" message-id)
8597       (setq message-id (concat "<" message-id)))
8598     (unless (string-match ">$" message-id)
8599       (setq message-id (concat message-id ">")))
8600     ;; People often post MIDs from URLs, so unhex it:
8601     (unless (string-match "@" message-id)
8602       (setq message-id (gnus-url-unhex-string message-id)))
8603     (let* ((header (gnus-id-to-header message-id))
8604            (sparse (and header
8605                         (gnus-summary-article-sparse-p
8606                          (mail-header-number header))
8607                         (memq (mail-header-number header)
8608                               gnus-newsgroup-limit)))
8609            number)
8610       (cond
8611        ;; If the article is present in the buffer we just go to it.
8612        ((and header
8613              (or (not (gnus-summary-article-sparse-p
8614                        (mail-header-number header)))
8615                  sparse))
8616         (prog1
8617             (gnus-summary-goto-article
8618              (mail-header-number header) nil t)
8619           (when sparse
8620             (gnus-summary-update-article (mail-header-number header)))))
8621        (t
8622         ;; We fetch the article.
8623         (catch 'found
8624           (dolist (gnus-override-method (gnus-refer-article-methods))
8625             (when (and (gnus-check-server gnus-override-method)
8626                        ;; Fetch the header,
8627                        (setq number (gnus-summary-insert-subject message-id)))
8628               ;; and display the article.
8629               (gnus-summary-select-article nil nil nil number)
8630               (throw 'found t)))
8631           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8632
8633 (defun gnus-refer-article-methods ()
8634   "Return a list of referable methods."
8635   (cond
8636    ;; No method, so we default to current and native.
8637    ((null gnus-refer-article-method)
8638     (list gnus-current-select-method gnus-select-method))
8639    ;; Current.
8640    ((eq 'current gnus-refer-article-method)
8641     (list gnus-current-select-method))
8642    ;; List of select methods.
8643    ((not (and (symbolp (car gnus-refer-article-method))
8644               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8645     (let (out)
8646       (dolist (method gnus-refer-article-method)
8647         (push (if (eq 'current method)
8648                   gnus-current-select-method
8649                 method)
8650               out))
8651       (nreverse out)))
8652    ;; One single select method.
8653    (t
8654     (list gnus-refer-article-method))))
8655
8656 (defun gnus-summary-edit-parameters ()
8657   "Edit the group parameters of the current group."
8658   (interactive)
8659   (gnus-group-edit-group gnus-newsgroup-name 'params))
8660
8661 (defun gnus-summary-customize-parameters ()
8662   "Customize the group parameters of the current group."
8663   (interactive)
8664   (gnus-group-customize gnus-newsgroup-name))
8665
8666 (defun gnus-summary-enter-digest-group (&optional force)
8667   "Enter an nndoc group based on the current article.
8668 If FORCE, force a digest interpretation.  If not, try
8669 to guess what the document format is."
8670   (interactive "P")
8671   (let ((conf gnus-current-window-configuration))
8672     (save-window-excursion
8673       (save-excursion
8674         (let (gnus-article-prepare-hook
8675               gnus-display-mime-function
8676               gnus-break-pages)
8677           (gnus-summary-select-article))))
8678     (setq gnus-current-window-configuration conf)
8679     (let* ((name (format "%s-%d"
8680                          (gnus-group-prefixed-name
8681                           gnus-newsgroup-name (list 'nndoc ""))
8682                          (with-current-buffer gnus-summary-buffer
8683                            gnus-current-article)))
8684            (ogroup gnus-newsgroup-name)
8685            (params (append (gnus-info-params (gnus-get-info ogroup))
8686                            (list (cons 'to-group ogroup))
8687                            (list (cons 'parent-group ogroup))
8688                            (list (cons 'save-article-group ogroup))))
8689            (case-fold-search t)
8690            (buf (current-buffer))
8691            dig to-address)
8692       (save-excursion
8693         (set-buffer gnus-original-article-buffer)
8694         ;; Have the digest group inherit the main mail address of
8695         ;; the parent article.
8696         (when (setq to-address (or (gnus-fetch-field "reply-to")
8697                                    (gnus-fetch-field "from")))
8698           (setq params
8699                 (append
8700                  (list (cons 'to-address
8701                              (funcall gnus-decode-encoded-address-function
8702                                       to-address))))))
8703         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8704         (insert-buffer-substring gnus-original-article-buffer)
8705         ;; Remove lines that may lead nndoc to misinterpret the
8706         ;; document type.
8707         (narrow-to-region
8708          (goto-char (point-min))
8709          (or (search-forward "\n\n" nil t) (point)))
8710         (goto-char (point-min))
8711         (delete-matching-lines "^Path:\\|^From ")
8712         (widen))
8713       (unwind-protect
8714           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8715                     (gnus-newsgroup-ephemeral-ignored-charsets
8716                      gnus-newsgroup-ignored-charsets))
8717                 (gnus-group-read-ephemeral-group
8718                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8719                               (nndoc-article-type
8720                                ,(if force 'mbox 'guess)))
8721                  t nil nil nil
8722                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8723                                                         "ADAPT")))))
8724               ;; Make all postings to this group go to the parent group.
8725               (nconc (gnus-info-params (gnus-get-info name))
8726                      params)
8727             ;; Couldn't select this doc group.
8728             (switch-to-buffer buf)
8729             (gnus-set-global-variables)
8730             (gnus-configure-windows 'summary)
8731             (gnus-message 3 "Article couldn't be entered?"))
8732         (kill-buffer dig)))))
8733
8734 (defun gnus-summary-read-document (n)
8735   "Open a new group based on the current article(s).
8736 This will allow you to read digests and other similar
8737 documents as newsgroups.
8738 Obeys the standard process/prefix convention."
8739   (interactive "P")
8740   (let* ((ogroup gnus-newsgroup-name)
8741          (params (append (gnus-info-params (gnus-get-info ogroup))
8742                          (list (cons 'to-group ogroup))))
8743          group egroup groups vgroup)
8744     (dolist (article (gnus-summary-work-articles n))
8745       (setq group (format "%s-%d" gnus-newsgroup-name article))
8746       (gnus-summary-remove-process-mark article)
8747       (when (gnus-summary-display-article article)
8748         (save-excursion
8749           (with-temp-buffer
8750             (insert-buffer-substring gnus-original-article-buffer)
8751             ;; Remove some headers that may lead nndoc to make
8752             ;; the wrong guess.
8753             (message-narrow-to-head)
8754             (goto-char (point-min))
8755             (delete-matching-lines "^Path:\\|^From ")
8756             (widen)
8757             (if (setq egroup
8758                       (gnus-group-read-ephemeral-group
8759                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8760                                      (nndoc-article-type guess))
8761                        t nil t))
8762                 (progn
8763             ;; Make all postings to this group go to the parent group.
8764                   (nconc (gnus-info-params (gnus-get-info egroup))
8765                          params)
8766                   (push egroup groups))
8767               ;; Couldn't select this doc group.
8768               (gnus-error 3 "Article couldn't be entered"))))))
8769     ;; Now we have selected all the documents.
8770     (cond
8771      ((not groups)
8772       (error "None of the articles could be interpreted as documents"))
8773      ((gnus-group-read-ephemeral-group
8774        (setq vgroup (format
8775                      "nnvirtual:%s-%s" gnus-newsgroup-name
8776                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8777        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8778        t
8779        (cons (current-buffer) 'summary)))
8780      (t
8781       (error "Couldn't select virtual nndoc group")))))
8782
8783 (defun gnus-summary-isearch-article (&optional regexp-p)
8784   "Do incremental search forward on the current article.
8785 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8786   (interactive "P")
8787   (gnus-summary-select-article)
8788   (gnus-configure-windows 'article)
8789   (gnus-eval-in-buffer-window gnus-article-buffer
8790     (save-restriction
8791       (widen)
8792       (isearch-forward regexp-p))))
8793
8794 (defun gnus-summary-repeat-search-article-forward ()
8795   "Repeat the previous search forwards."
8796   (interactive)
8797   (unless gnus-last-search-regexp
8798     (error "No previous search"))
8799   (gnus-summary-search-article-forward gnus-last-search-regexp))
8800
8801 (defun gnus-summary-repeat-search-article-backward ()
8802   "Repeat the previous search backwards."
8803   (interactive)
8804   (unless gnus-last-search-regexp
8805     (error "No previous search"))
8806   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8807
8808 (defun gnus-summary-search-article-forward (regexp &optional backward)
8809   "Search for an article containing REGEXP forward.
8810 If BACKWARD, search backward instead."
8811   (interactive
8812    (list (read-string
8813           (format "Search article %s (regexp%s): "
8814                   (if current-prefix-arg "backward" "forward")
8815                   (if gnus-last-search-regexp
8816                       (concat ", default " gnus-last-search-regexp)
8817                     "")))
8818          current-prefix-arg))
8819   (if (string-equal regexp "")
8820       (setq regexp (or gnus-last-search-regexp ""))
8821     (setq gnus-last-search-regexp regexp)
8822     (setq gnus-article-before-search gnus-current-article))
8823   ;; Intentionally set gnus-last-article.
8824   (setq gnus-last-article gnus-article-before-search)
8825   (let ((gnus-last-article gnus-last-article))
8826     (if (gnus-summary-search-article regexp backward)
8827         (gnus-summary-show-thread)
8828       (signal 'search-failed (list regexp)))))
8829
8830 (defun gnus-summary-search-article-backward (regexp)
8831   "Search for an article containing REGEXP backward."
8832   (interactive
8833    (list (read-string
8834           (format "Search article backward (regexp%s): "
8835                   (if gnus-last-search-regexp
8836                       (concat ", default " gnus-last-search-regexp)
8837                     "")))))
8838   (gnus-summary-search-article-forward regexp 'backward))
8839
8840 (defun gnus-summary-search-article (regexp &optional backward)
8841   "Search for an article containing REGEXP.
8842 Optional argument BACKWARD means do search for backward.
8843 `gnus-select-article-hook' is not called during the search."
8844   ;; We have to require this here to make sure that the following
8845   ;; dynamic binding isn't shadowed by autoloading.
8846   (require 'gnus-async)
8847   (require 'gnus-art)
8848   (let ((gnus-select-article-hook nil)  ;Disable hook.
8849         (gnus-article-prepare-hook nil)
8850         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8851         (gnus-use-article-prefetch nil)
8852         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8853         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8854         (gnus-visual nil)
8855         (gnus-keep-backlog nil)
8856         (gnus-break-pages nil)
8857         (gnus-summary-display-arrow nil)
8858         (gnus-updated-mode-lines nil)
8859         (gnus-auto-center-summary nil)
8860         (sum (current-buffer))
8861         (gnus-display-mime-function nil)
8862         (found nil)
8863         point)
8864     (gnus-save-hidden-threads
8865       (gnus-summary-select-article)
8866       (set-buffer gnus-article-buffer)
8867       (goto-char (window-point (get-buffer-window (current-buffer))))
8868       (when backward
8869         (forward-line -1))
8870       (while (not found)
8871         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8872         (if (if backward
8873                 (re-search-backward regexp nil t)
8874               (re-search-forward regexp nil t))
8875             ;; We found the regexp.
8876             (progn
8877               (setq found 'found)
8878               (beginning-of-line)
8879               (set-window-start
8880                (get-buffer-window (current-buffer))
8881                (point))
8882               (forward-line 1)
8883               (set-window-point
8884                (get-buffer-window (current-buffer))
8885                (point))
8886               (set-buffer sum)
8887               (setq point (point)))
8888           ;; We didn't find it, so we go to the next article.
8889           (set-buffer sum)
8890           (setq found 'not)
8891           (while (eq found 'not)
8892             (if (not (if backward (gnus-summary-find-prev)
8893                        (gnus-summary-find-next)))
8894                 ;; No more articles.
8895                 (setq found t)
8896               ;; Select the next article and adjust point.
8897               (unless (gnus-summary-article-sparse-p
8898                        (gnus-summary-article-number))
8899                 (setq found nil)
8900                 (gnus-summary-select-article)
8901                 (set-buffer gnus-article-buffer)
8902                 (widen)
8903                 (goto-char (if backward (point-max) (point-min))))))))
8904       (gnus-message 7 ""))
8905     ;; Return whether we found the regexp.
8906     (when (eq found 'found)
8907       (goto-char point)
8908       (gnus-summary-show-thread)
8909       (gnus-summary-goto-subject gnus-current-article)
8910       (gnus-summary-position-point)
8911       t)))
8912
8913 (defun gnus-find-matching-articles (header regexp)
8914   "Return a list of all articles that match REGEXP on HEADER.
8915 This search includes all articles in the current group that Gnus has
8916 fetched headers for, whether they are displayed or not."
8917   (let ((articles nil)
8918         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8919         (case-fold-search t))
8920     (dolist (header gnus-newsgroup-headers)
8921       (when (string-match regexp (funcall func header))
8922         (push (mail-header-number header) articles)))
8923     (nreverse articles)))
8924
8925 (defun gnus-summary-find-matching (header regexp &optional backward unread
8926                                           not-case-fold not-matching)
8927   "Return a list of all articles that match REGEXP on HEADER.
8928 The search stars on the current article and goes forwards unless
8929 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8930 If UNREAD is non-nil, only unread articles will
8931 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8932 in the comparisons. If NOT-MATCHING, return a list of all articles that
8933 not match REGEXP on HEADER."
8934   (let ((case-fold-search (not not-case-fold))
8935         articles d func)
8936     (if (consp header)
8937         (if (eq (car header) 'extra)
8938             (setq func
8939                   `(lambda (h)
8940                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8941                          "")))
8942           (error "%s is an invalid header" header))
8943       (unless (fboundp (intern (concat "mail-header-" header)))
8944         (error "%s is not a valid header" header))
8945       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8946     (dolist (d (if (eq backward 'all)
8947                    gnus-newsgroup-data
8948                  (gnus-data-find-list
8949                   (gnus-summary-article-number)
8950                   (gnus-data-list backward))))
8951       (when (and (or (not unread)       ; We want all articles...
8952                      (gnus-data-unread-p d)) ; Or just unreads.
8953                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8954                  (if not-matching
8955                      (not (string-match
8956                            regexp
8957                            (funcall func (gnus-data-header d))))
8958                    (string-match regexp
8959                                  (funcall func (gnus-data-header d)))))
8960         (push (gnus-data-number d) articles))) ; Success!
8961     (nreverse articles)))
8962
8963 (defun gnus-summary-execute-command (header regexp command &optional backward)
8964   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8965 If HEADER is an empty string (or nil), the match is done on the entire
8966 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8967   (interactive
8968    (list (let ((completion-ignore-case t))
8969            (completing-read
8970             "Header name: "
8971             (mapcar (lambda (header) (list (format "%s" header)))
8972                     (append
8973                      '("Number" "Subject" "From" "Lines" "Date"
8974                        "Message-ID" "Xref" "References" "Body")
8975                      gnus-extra-headers))
8976             nil 'require-match))
8977          (read-string "Regexp: ")
8978          (read-key-sequence "Command: ")
8979          current-prefix-arg))
8980   (when (equal header "Body")
8981     (setq header ""))
8982   ;; Hidden thread subtrees must be searched as well.
8983   (gnus-summary-show-all-threads)
8984   ;; We don't want to change current point nor window configuration.
8985   (save-excursion
8986     (save-window-excursion
8987       (let (gnus-visual
8988             gnus-treat-strip-trailing-blank-lines
8989             gnus-treat-strip-leading-blank-lines
8990             gnus-treat-strip-multiple-blank-lines
8991             gnus-treat-hide-boring-headers
8992             gnus-treat-fold-newsgroups
8993             gnus-article-prepare-hook)
8994         (gnus-message 6 "Executing %s..." (key-description command))
8995         ;; We'd like to execute COMMAND interactively so as to give arguments.
8996         (gnus-execute header regexp
8997                       `(call-interactively ',(key-binding command))
8998                       backward)
8999         (gnus-message 6 "Executing %s...done" (key-description command))))))
9000
9001 (defun gnus-summary-beginning-of-article ()
9002   "Scroll the article back to the beginning."
9003   (interactive)
9004   (gnus-summary-select-article)
9005   (gnus-configure-windows 'article)
9006   (gnus-eval-in-buffer-window gnus-article-buffer
9007     (widen)
9008     (goto-char (point-min))
9009     (when gnus-break-pages
9010       (gnus-narrow-to-page))))
9011
9012 (defun gnus-summary-end-of-article ()
9013   "Scroll to the end of the article."
9014   (interactive)
9015   (gnus-summary-select-article)
9016   (gnus-configure-windows 'article)
9017   (gnus-eval-in-buffer-window gnus-article-buffer
9018     (widen)
9019     (goto-char (point-max))
9020     (recenter -3)
9021     (when gnus-break-pages
9022       (when (re-search-backward page-delimiter nil t)
9023         (narrow-to-region (match-end 0) (point-max)))
9024       (gnus-narrow-to-page))))
9025
9026 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9027   "Truncate to LEN and quote all \"(\"'s in STRING."
9028   (gnus-replace-in-string (if (and len (> (length string) len))
9029                               (substring string 0 len)
9030                             string)
9031                           "[()]" "\\\\\\&"))
9032
9033 (defun gnus-summary-print-article (&optional filename n)
9034   "Generate and print a PostScript image of the process-marked (mail) articles.
9035
9036 If used interactively, print the current article if none are
9037 process-marked.  With prefix arg, prompt the user for the name of the
9038 file to save in.
9039
9040 When used from Lisp, accept two optional args FILENAME and N.  N means
9041 to print the next N articles.  If N is negative, print the N previous
9042 articles.  If N is nil and articles have been marked with the process
9043 mark, print these instead.
9044
9045 If the optional first argument FILENAME is nil, send the image to the
9046 printer.  If FILENAME is a string, save the PostScript image in a file with
9047 that name.  If FILENAME is a number, prompt the user for the name of the file
9048 to save in."
9049   (interactive (list (ps-print-preprint current-prefix-arg)))
9050   (dolist (article (gnus-summary-work-articles n))
9051     (gnus-summary-select-article nil nil 'pseudo article)
9052     (gnus-eval-in-buffer-window gnus-article-buffer
9053       (gnus-print-buffer))
9054     (gnus-summary-remove-process-mark article))
9055   (ps-despool filename))
9056
9057 (defun gnus-print-buffer ()
9058   (let ((buffer (generate-new-buffer " *print*")))
9059     (unwind-protect
9060         (progn
9061           (copy-to-buffer buffer (point-min) (point-max))
9062           (set-buffer buffer)
9063           (gnus-remove-text-with-property 'gnus-decoration)
9064           (when (gnus-visual-p 'article-highlight 'highlight)
9065             ;; Copy-to-buffer doesn't copy overlay.  So redo
9066             ;; highlight.
9067             (let ((gnus-article-buffer buffer))
9068               (gnus-article-highlight-citation t)
9069               (gnus-article-highlight-signature)
9070               (gnus-article-emphasize)
9071               (gnus-article-delete-invisible-text)))
9072           (let ((ps-left-header
9073                  (list
9074                   (concat "("
9075                           (gnus-summary-print-truncate-and-quote
9076                            (mail-header-subject gnus-current-headers)
9077                            66) ")")
9078                   (concat "("
9079                           (gnus-summary-print-truncate-and-quote
9080                            (mail-header-from gnus-current-headers)
9081                            45) ")")))
9082                 (ps-right-header
9083                  (list
9084                   "/pagenumberstring load"
9085                   (concat "("
9086                           (mail-header-date gnus-current-headers) ")"))))
9087             (gnus-run-hooks 'gnus-ps-print-hook)
9088             (save-excursion
9089               (if window-system
9090                   (ps-spool-buffer-with-faces)
9091                 (ps-spool-buffer)))))
9092       (kill-buffer buffer))))
9093
9094 (defun gnus-summary-show-article (&optional arg)
9095   "Force redisplaying of the current article.
9096 If ARG (the prefix) is a number, show the article with the charset
9097 defined in `gnus-summary-show-article-charset-alist', or the charset
9098 input.
9099 If ARG (the prefix) is non-nil and not a number, show the raw article
9100 without any article massaging functions being run.  Normally, the key
9101 strokes are `C-u g'."
9102   (interactive "P")
9103   (cond
9104    ((numberp arg)
9105     (gnus-summary-show-article t)
9106     (let ((gnus-newsgroup-charset
9107            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9108                (mm-read-coding-system
9109                 "View as charset: " ;; actually it is coding system.
9110                 (with-current-buffer gnus-article-buffer
9111                   (mm-detect-coding-region (point) (point-max))))))
9112           (gnus-newsgroup-ignored-charsets 'gnus-all))
9113       (gnus-summary-select-article nil 'force)
9114       (let ((deps gnus-newsgroup-dependencies)
9115             head header lines)
9116         (save-excursion
9117           (set-buffer gnus-original-article-buffer)
9118           (save-restriction
9119             (message-narrow-to-head)
9120             (setq head (buffer-string))
9121             (goto-char (point-min))
9122             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9123               (goto-char (point-max))
9124               (widen)
9125               (setq lines (1- (count-lines (point) (point-max))))))
9126           (with-temp-buffer
9127             (insert (format "211 %d Article retrieved.\n"
9128                             (cdr gnus-article-current)))
9129             (insert head)
9130             (if lines (insert (format "Lines: %d\n" lines)))
9131             (insert ".\n")
9132             (let ((nntp-server-buffer (current-buffer)))
9133               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9134         (gnus-data-set-header
9135          (gnus-data-find (cdr gnus-article-current))
9136          header)
9137         (gnus-summary-update-article-line
9138          (cdr gnus-article-current) header)
9139         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9140           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9141    ((not arg)
9142     ;; Select the article the normal way.
9143     (gnus-summary-select-article nil 'force))
9144    (t
9145     ;; We have to require this here to make sure that the following
9146     ;; dynamic binding isn't shadowed by autoloading.
9147     (require 'gnus-async)
9148     (require 'gnus-art)
9149     ;; Bind the article treatment functions to nil.
9150     (let ((gnus-have-all-headers t)
9151           gnus-article-prepare-hook
9152           gnus-article-decode-hook
9153           gnus-display-mime-function
9154           gnus-break-pages)
9155       ;; Destroy any MIME parts.
9156       (when (gnus-buffer-live-p gnus-article-buffer)
9157         (save-excursion
9158           (set-buffer gnus-article-buffer)
9159           (mm-destroy-parts gnus-article-mime-handles)
9160           ;; Set it to nil for safety reason.
9161           (setq gnus-article-mime-handle-alist nil)
9162           (setq gnus-article-mime-handles nil)))
9163       (gnus-summary-select-article nil 'force))))
9164   (gnus-summary-goto-subject gnus-current-article)
9165   (gnus-summary-position-point))
9166
9167 (defun gnus-summary-show-raw-article ()
9168   "Show the raw article without any article massaging functions being run."
9169   (interactive)
9170   (gnus-summary-show-article t))
9171
9172 (defun gnus-summary-verbose-headers (&optional arg)
9173   "Toggle permanent full header display.
9174 If ARG is a positive number, turn header display on.
9175 If ARG is a negative number, turn header display off."
9176   (interactive "P")
9177   (setq gnus-show-all-headers
9178         (cond ((or (not (numberp arg))
9179                    (zerop arg))
9180                (not gnus-show-all-headers))
9181               ((natnump arg)
9182                t)))
9183   (gnus-summary-show-article))
9184
9185 (defun gnus-summary-toggle-header (&optional arg)
9186   "Show the headers if they are hidden, or hide them if they are shown.
9187 If ARG is a positive number, show the entire header.
9188 If ARG is a negative number, hide the unwanted header lines."
9189   (interactive "P")
9190   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9191                      (get-buffer-window gnus-article-buffer t))))
9192     (with-current-buffer gnus-article-buffer
9193       (widen)
9194       (article-narrow-to-head)
9195       (let* ((buffer-read-only nil)
9196              (inhibit-point-motion-hooks t)
9197              (hidden (if (numberp arg)
9198                          (>= arg 0)
9199                        (or (not (looking-at "[^ \t\n]+:"))
9200                            (gnus-article-hidden-text-p 'headers))))
9201              s e)
9202         (delete-region (point-min) (point-max))
9203         (with-current-buffer gnus-original-article-buffer
9204           (goto-char (setq s (point-min)))
9205           (setq e (if (search-forward "\n\n" nil t)
9206                       (1- (point))
9207                     (point-max))))
9208         (insert-buffer-substring gnus-original-article-buffer s e)
9209         (run-hooks 'gnus-article-decode-hook)
9210         (if hidden
9211             (let ((gnus-treat-hide-headers nil)
9212                   (gnus-treat-hide-boring-headers nil))
9213               (gnus-delete-wash-type 'headers)
9214               (gnus-treat-article 'head))
9215           (gnus-treat-article 'head))
9216         (widen)
9217         (if window
9218             (set-window-start window (goto-char (point-min))))
9219         (if gnus-break-pages
9220             (gnus-narrow-to-page)
9221           (when (gnus-visual-p 'page-marker)
9222             (let ((buffer-read-only nil))
9223               (gnus-remove-text-with-property 'gnus-prev)
9224               (gnus-remove-text-with-property 'gnus-next))))
9225         (gnus-set-mode-line 'article)))))
9226
9227 (defun gnus-summary-show-all-headers ()
9228   "Make all header lines visible."
9229   (interactive)
9230   (gnus-summary-toggle-header 1))
9231
9232 (defun gnus-summary-caesar-message (&optional arg)
9233   "Caesar rotate the current article by 13.
9234 With a non-numerical prefix, also rotate headers.  A numerical
9235 prefix specifies how many places to rotate each letter forward."
9236   (interactive "P")
9237   (gnus-summary-select-article)
9238   (let ((mail-header-separator ""))
9239     (gnus-eval-in-buffer-window gnus-article-buffer
9240       (save-restriction
9241         (widen)
9242         (let ((start (window-start))
9243               buffer-read-only)
9244           (if (equal arg '(4))
9245               (message-caesar-buffer-body nil t)
9246             (message-caesar-buffer-body arg))
9247           (set-window-start (get-buffer-window (current-buffer)) start)))))
9248   ;; Create buttons and stuff...
9249   (gnus-treat-article nil))
9250
9251 (defun gnus-summary-idna-message (&optional arg)
9252   "Decode IDNA encoded domain names in the current articles.
9253 IDNA encoded domain names looks like `xn--bar'.  If a string
9254 remain unencoded after running this function, it is likely an
9255 invalid IDNA string (`xn--bar' is invalid).
9256
9257 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9258 installed for this command to work."
9259   (interactive "P")
9260   (if (not (and (condition-case nil (require 'idna)
9261                   (file-error))
9262                 (mm-coding-system-p 'utf-8)
9263                 (executable-find (symbol-value 'idna-program))))
9264       (gnus-message
9265        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9266     (gnus-summary-select-article)
9267     (let ((mail-header-separator ""))
9268       (gnus-eval-in-buffer-window gnus-article-buffer
9269         (save-restriction
9270           (widen)
9271           (let ((start (window-start))
9272                 buffer-read-only)
9273             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9274               (replace-match (idna-to-unicode (match-string 1))))
9275             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9276
9277 (defun gnus-summary-morse-message (&optional arg)
9278   "Morse decode the current article."
9279   (interactive "P")
9280   (gnus-summary-select-article)
9281   (let ((mail-header-separator ""))
9282     (gnus-eval-in-buffer-window gnus-article-buffer
9283       (save-excursion
9284         (save-restriction
9285           (widen)
9286           (let ((pos (window-start))
9287                 buffer-read-only)
9288             (goto-char (point-min))
9289             (when (message-goto-body)
9290               (gnus-narrow-to-body))
9291             (goto-char (point-min))
9292             (while (search-forward "·" (point-max) t)
9293               (replace-match "."))
9294             (unmorse-region (point-min) (point-max))
9295             (widen)
9296             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9297
9298 (defun gnus-summary-stop-page-breaking ()
9299   "Stop page breaking in the current article."
9300   (interactive)
9301   (gnus-summary-select-article)
9302   (gnus-eval-in-buffer-window gnus-article-buffer
9303     (widen)
9304     (when (gnus-visual-p 'page-marker)
9305       (let ((buffer-read-only nil))
9306         (gnus-remove-text-with-property 'gnus-prev)
9307         (gnus-remove-text-with-property 'gnus-next))
9308       (setq gnus-page-broken nil))))
9309
9310 (defun gnus-summary-move-article (&optional n to-newsgroup
9311                                             select-method action)
9312   "Move the current article to a different newsgroup.
9313 If N is a positive number, move the N next articles.
9314 If N is a negative number, move the N previous articles.
9315 If N is nil and any articles have been marked with the process mark,
9316 move those articles instead.
9317 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9318 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9319 re-spool using this method.
9320
9321 When called interactively with TO-NEWSGROUP being nil, the value of
9322 the variable `gnus-move-split-methods' is used for finding a default
9323 for the target newsgroup.
9324
9325 For this function to work, both the current newsgroup and the
9326 newsgroup that you want to move to have to support the `request-move'
9327 and `request-accept' functions.
9328
9329 ACTION can be either `move' (the default), `crosspost' or `copy'."
9330   (interactive "P")
9331   (unless action
9332     (setq action 'move))
9333   ;; Check whether the source group supports the required functions.
9334   (cond ((and (eq action 'move)
9335               (not (gnus-check-backend-function
9336                     'request-move-article gnus-newsgroup-name)))
9337          (error "The current group does not support article moving"))
9338         ((and (eq action 'crosspost)
9339               (not (gnus-check-backend-function
9340                     'request-replace-article gnus-newsgroup-name)))
9341          (error "The current group does not support article editing")))
9342   (let ((articles (gnus-summary-work-articles n))
9343         (prefix (if (gnus-check-backend-function
9344                      'request-move-article gnus-newsgroup-name)
9345                     (funcall gnus-move-group-prefix-function
9346                              gnus-newsgroup-name)
9347                   ""))
9348         (names '((move "Move" "Moving")
9349                  (copy "Copy" "Copying")
9350                  (crosspost "Crosspost" "Crossposting")))
9351         (copy-buf (save-excursion
9352                     (nnheader-set-temp-buffer " *copy article*")))
9353         art-group to-method new-xref article to-groups articles-to-update-marks)
9354     (unless (assq action names)
9355       (error "Unknown action %s" action))
9356     ;; Read the newsgroup name.
9357     (when (and (not to-newsgroup)
9358                (not select-method))
9359       (if (and gnus-move-split-methods
9360                (not
9361                 (and (memq gnus-current-article articles)
9362                      (gnus-buffer-live-p gnus-original-article-buffer))))
9363           ;; When `gnus-move-split-methods' is non-nil, we have to
9364           ;; select an article to give `gnus-read-move-group-name' an
9365           ;; opportunity to suggest an appropriate default.  However,
9366           ;; we needn't render or mark the article.
9367           (let ((gnus-display-mime-function nil)
9368                 (gnus-article-prepare-hook nil)
9369                 (gnus-mark-article-hook nil))
9370             (gnus-summary-select-article nil nil nil (car articles))))
9371       (setq to-newsgroup
9372             (gnus-read-move-group-name
9373              (cadr (assq action names))
9374              (symbol-value (intern (format "gnus-current-%s-group" action)))
9375              articles prefix))
9376       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9377     (setq to-method (or select-method
9378                         (gnus-server-to-method
9379                          (gnus-group-method to-newsgroup))))
9380     ;; Check the method we are to move this article to...
9381     (unless (gnus-check-backend-function
9382              'request-accept-article (car to-method))
9383       (error "%s does not support article copying" (car to-method)))
9384     (unless (gnus-check-server to-method)
9385       (error "Can't open server %s" (car to-method)))
9386     (gnus-message 6 "%s to %s: %s..."
9387                   (caddr (assq action names))
9388                   (or (car select-method) to-newsgroup) articles)
9389     (while articles
9390       (setq article (pop articles))
9391       (setq
9392        art-group
9393        (cond
9394         ;; Move the article.
9395         ((eq action 'move)
9396          ;; Remove this article from future suppression.
9397          (gnus-dup-unsuppress-article article)
9398          (let* ((from-method (gnus-find-method-for-group
9399                               gnus-newsgroup-name))
9400                 (to-method (gnus-find-method-for-group
9401                             to-newsgroup))
9402                 (move-is-internal (gnus-method-equal from-method to-method)))
9403            (gnus-request-move-article
9404             article                     ; Article to move
9405             gnus-newsgroup-name         ; From newsgroup
9406             (nth 1 (gnus-find-method-for-group
9407                     gnus-newsgroup-name)) ; Server
9408             (list 'gnus-request-accept-article
9409                   to-newsgroup (list 'quote select-method)
9410                   (not articles) t)     ; Accept form
9411             (not articles)              ; Only save nov last time
9412             move-is-internal)))         ; is this move internal?
9413         ;; Copy the article.
9414         ((eq action 'copy)
9415          (save-excursion
9416            (set-buffer copy-buf)
9417            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9418              (save-restriction
9419                (nnheader-narrow-to-headers)
9420                (dolist (hdr gnus-copy-article-ignored-headers)
9421                  (message-remove-header hdr t)))
9422              (gnus-request-accept-article
9423               to-newsgroup select-method (not articles) t))))
9424         ;; Crosspost the article.
9425         ((eq action 'crosspost)
9426          (let ((xref (message-tokenize-header
9427                       (mail-header-xref (gnus-summary-article-header article))
9428                       " ")))
9429            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9430                                   ":" (number-to-string article)))
9431            (unless xref
9432              (setq xref (list (system-name))))
9433            (setq new-xref
9434                  (concat
9435                   (mapconcat 'identity
9436                              (delete "Xref:" (delete new-xref xref))
9437                              " ")
9438                   " " new-xref))
9439            (save-excursion
9440              (set-buffer copy-buf)
9441              ;; First put the article in the destination group.
9442              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9443              (when (consp (setq art-group
9444                                 (gnus-request-accept-article
9445                                  to-newsgroup select-method (not articles) t)))
9446                (setq new-xref (concat new-xref " " (car art-group)
9447                                       ":"
9448                                       (number-to-string (cdr art-group))))
9449                ;; Now we have the new Xrefs header, so we insert
9450                ;; it and replace the new article.
9451                (nnheader-replace-header "Xref" new-xref)
9452                (gnus-request-replace-article
9453                 (cdr art-group) to-newsgroup (current-buffer) t)
9454                art-group))))))
9455       (cond
9456        ((not art-group)
9457         (gnus-message 1 "Couldn't %s article %s: %s"
9458                       (cadr (assq action names)) article
9459                       (nnheader-get-report (car to-method))))
9460        ((eq art-group 'junk)
9461         (when (eq action 'move)
9462           (gnus-summary-mark-article article gnus-canceled-mark)
9463           (gnus-message 4 "Deleted article %s" article)
9464           ;; run the delete hook
9465           (run-hook-with-args 'gnus-summary-article-delete-hook
9466                               action
9467                               (gnus-data-header
9468                                (assoc article (gnus-data-list nil)))
9469                               gnus-newsgroup-name nil
9470                               select-method)))
9471        (t
9472         (let* ((pto-group (gnus-group-prefixed-name
9473                            (car art-group) to-method))
9474                (info (gnus-get-info pto-group))
9475                (to-group (gnus-info-group info))
9476                to-marks)
9477           ;; Update the group that has been moved to.
9478           (when (and info
9479                      (memq action '(move copy)))
9480             (unless (member to-group to-groups)
9481               (push to-group to-groups))
9482
9483             (unless (memq article gnus-newsgroup-unreads)
9484               (push 'read to-marks)
9485               (gnus-info-set-read
9486                info (gnus-add-to-range (gnus-info-read info)
9487                                        (list (cdr art-group)))))
9488
9489             ;; See whether the article is to be put in the cache.
9490             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9491                              gnus-article-mark-lists
9492                            (delete '(expirable . expire)
9493                                    (copy-sequence gnus-article-mark-lists))))
9494                   (to-article (cdr art-group)))
9495
9496               ;; Enter the article into the cache in the new group,
9497               ;; if that is required.
9498               (when gnus-use-cache
9499                 (gnus-cache-possibly-enter-article
9500                  to-group to-article
9501                  (memq article gnus-newsgroup-marked)
9502                  (memq article gnus-newsgroup-dormant)
9503                  (memq article gnus-newsgroup-unreads)))
9504
9505               (when gnus-preserve-marks
9506                 ;; Copy any marks over to the new group.
9507                 (when (and (equal to-group gnus-newsgroup-name)
9508                            (not (memq article gnus-newsgroup-unreads)))
9509                   ;; Mark this article as read in this group.
9510                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9511                   (setcdr (gnus-active to-group) to-article)
9512                   (setcdr gnus-newsgroup-active to-article))
9513
9514                 (while marks
9515                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9516                     (when (memq article (symbol-value
9517                                          (intern (format "gnus-newsgroup-%s"
9518                                                          (caar marks)))))
9519                       (push (cdar marks) to-marks)
9520                       ;; If the other group is the same as this group,
9521                       ;; then we have to add the mark to the list.
9522                       (when (equal to-group gnus-newsgroup-name)
9523                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9524                              (cons to-article
9525                                    (symbol-value
9526                                     (intern (format "gnus-newsgroup-%s"
9527                                                     (caar marks)))))))
9528                       ;; Copy the marks to other group.
9529                       (gnus-add-marked-articles
9530                        to-group (cdar marks) (list to-article) info)))
9531                   (setq marks (cdr marks)))
9532
9533                 (gnus-request-set-mark
9534                  to-group (list (list (list to-article) 'add to-marks))))
9535
9536               (gnus-dribble-enter
9537                (concat "(gnus-group-set-info '"
9538                        (gnus-prin1-to-string (gnus-get-info to-group))
9539                        ")"))))
9540
9541           ;; Update the Xref header in this article to point to
9542           ;; the new crossposted article we have just created.
9543           (when (eq action 'crosspost)
9544             (save-excursion
9545               (set-buffer copy-buf)
9546               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9547               (nnheader-replace-header "Xref" new-xref)
9548               (gnus-request-replace-article
9549                article gnus-newsgroup-name (current-buffer) t)))
9550
9551           ;; run the move/copy/crosspost/respool hook
9552           (run-hook-with-args 'gnus-summary-article-move-hook
9553                               action
9554                               (gnus-data-header
9555                                (assoc article (gnus-data-list nil)))
9556                               gnus-newsgroup-name
9557                               to-newsgroup
9558                               select-method))
9559
9560         ;;;!!!Why is this necessary?
9561         (set-buffer gnus-summary-buffer)
9562
9563         (gnus-summary-goto-subject article)
9564         (when (eq action 'move)
9565           (gnus-summary-mark-article article gnus-canceled-mark))))
9566       (push article articles-to-update-marks))
9567
9568     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9569     ;; Re-activate all groups that have been moved to.
9570     (save-excursion
9571       (set-buffer gnus-group-buffer)
9572       (let ((gnus-group-marked to-groups))
9573         (gnus-group-get-new-news-this-group nil t)))
9574
9575     (gnus-kill-buffer copy-buf)
9576     (gnus-summary-position-point)
9577     (gnus-set-mode-line 'summary)))
9578
9579 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9580   "Copy the current article to some other group.
9581 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9582 When called interactively, if TO-NEWSGROUP is nil, use the value of
9583 the variable `gnus-move-split-methods' for finding a default target
9584 newsgroup.
9585 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9586 re-spool using this method."
9587   (interactive "P")
9588   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9589
9590 (defun gnus-summary-crosspost-article (&optional n)
9591   "Crosspost the current article to some other group."
9592   (interactive "P")
9593   (gnus-summary-move-article n nil nil 'crosspost))
9594
9595 (defcustom gnus-summary-respool-default-method nil
9596   "Default method type for respooling an article.
9597 If nil, use to the current newsgroup method."
9598   :type 'symbol
9599   :group 'gnus-summary-mail)
9600
9601 (defcustom gnus-summary-display-while-building nil
9602   "If non-nil, show and update the summary buffer as it's being built.
9603 If the value is t, update the buffer after every line is inserted.  If
9604 the value is an integer (N), update the display every N lines."
9605   :version "22.1"
9606   :group 'gnus-thread
9607   :type '(choice (const :tag "off" nil)
9608                  number
9609                  (const :tag "frequently" t)))
9610
9611 (defun gnus-summary-respool-article (&optional n method)
9612   "Respool the current article.
9613 The article will be squeezed through the mail spooling process again,
9614 which means that it will be put in some mail newsgroup or other
9615 depending on `nnmail-split-methods'.
9616 If N is a positive number, respool the N next articles.
9617 If N is a negative number, respool the N previous articles.
9618 If N is nil and any articles have been marked with the process mark,
9619 respool those articles instead.
9620
9621 Respooling can be done both from mail groups and \"real\" newsgroups.
9622 In the former case, the articles in question will be moved from the
9623 current group into whatever groups they are destined to.  In the
9624 latter case, they will be copied into the relevant groups."
9625   (interactive
9626    (list current-prefix-arg
9627          (let* ((methods (gnus-methods-using 'respool))
9628                 (methname
9629                  (symbol-name (or gnus-summary-respool-default-method
9630                                   (car (gnus-find-method-for-group
9631                                         gnus-newsgroup-name)))))
9632                 (method
9633                  (gnus-completing-read-with-default
9634                   methname "Backend to use when respooling"
9635                   methods nil t nil 'gnus-mail-method-history))
9636                 ms)
9637            (cond
9638             ((zerop (length (setq ms (gnus-servers-using-backend
9639                                       (intern method)))))
9640              (list (intern method) ""))
9641             ((= 1 (length ms))
9642              (car ms))
9643             (t
9644              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9645                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9646                            ms-alist))))))))
9647   (unless method
9648     (error "No method given for respooling"))
9649   (if (assoc (symbol-name
9650               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9651              (gnus-methods-using 'respool))
9652       (gnus-summary-move-article n nil method)
9653     (gnus-summary-copy-article n nil method)))
9654
9655 (defun gnus-summary-import-article (file &optional edit)
9656   "Import an arbitrary file into a mail newsgroup."
9657   (interactive "fImport file: \nP")
9658   (let ((group gnus-newsgroup-name)
9659         (now (current-time))
9660         atts lines group-art)
9661     (unless (gnus-check-backend-function 'request-accept-article group)
9662       (error "%s does not support article importing" group))
9663     (or (file-readable-p file)
9664         (not (file-regular-p file))
9665         (error "Can't read %s" file))
9666     (save-excursion
9667       (set-buffer (gnus-get-buffer-create " *import file*"))
9668       (erase-buffer)
9669       (nnheader-insert-file-contents file)
9670       (goto-char (point-min))
9671       (if (nnheader-article-p)
9672           (save-restriction
9673             (goto-char (point-min))
9674             (search-forward "\n\n" nil t)
9675             (narrow-to-region (point-min) (1- (point)))
9676             (goto-char (point-min))
9677             (unless (re-search-forward "^date:" nil t)
9678               (goto-char (point-max))
9679               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9680        ;; This doesn't look like an article, so we fudge some headers.
9681         (setq atts (file-attributes file)
9682               lines (count-lines (point-min) (point-max)))
9683         (insert "From: " (read-string "From: ") "\n"
9684                 "Subject: " (read-string "Subject: ") "\n"
9685                 "Date: " (message-make-date (nth 5 atts)) "\n"
9686                 "Message-ID: " (message-make-message-id) "\n"
9687                 "Lines: " (int-to-string lines) "\n"
9688                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9689       (setq group-art (gnus-request-accept-article group nil t))
9690       (kill-buffer (current-buffer)))
9691     (setq gnus-newsgroup-active (gnus-activate-group group))
9692     (forward-line 1)
9693     (gnus-summary-goto-article (cdr group-art) nil t)
9694     (when edit
9695       (gnus-summary-edit-article))))
9696
9697 (defun gnus-summary-create-article ()
9698   "Create an article in a mail newsgroup."
9699   (interactive)
9700   (let ((group gnus-newsgroup-name)
9701         (now (current-time))
9702         group-art)
9703     (unless (gnus-check-backend-function 'request-accept-article group)
9704       (error "%s does not support article importing" group))
9705     (save-excursion
9706       (set-buffer (gnus-get-buffer-create " *import file*"))
9707       (erase-buffer)
9708       (goto-char (point-min))
9709       ;; This doesn't look like an article, so we fudge some headers.
9710       (insert "From: " (read-string "From: ") "\n"
9711               "Subject: " (read-string "Subject: ") "\n"
9712               "Date: " (message-make-date now) "\n"
9713               "Message-ID: " (message-make-message-id) "\n")
9714       (setq group-art (gnus-request-accept-article group nil t))
9715       (kill-buffer (current-buffer)))
9716     (setq gnus-newsgroup-active (gnus-activate-group group))
9717     (forward-line 1)
9718     (gnus-summary-goto-article (cdr group-art) nil t)
9719     (gnus-summary-edit-article)))
9720
9721 (defun gnus-summary-article-posted-p ()
9722   "Say whether the current (mail) article is available from news as well.
9723 This will be the case if the article has both been mailed and posted."
9724   (interactive)
9725   (let ((id (mail-header-references (gnus-summary-article-header)))
9726         (gnus-override-method (car (gnus-refer-article-methods))))
9727     (if (gnus-request-head id "")
9728         (gnus-message 2 "The current message was found on %s"
9729                       gnus-override-method)
9730       (gnus-message 2 "The current message couldn't be found on %s"
9731                     gnus-override-method)
9732       nil)))
9733
9734 (defun gnus-summary-expire-articles (&optional now)
9735   "Expire all articles that are marked as expirable in the current group."
9736   (interactive)
9737   (when (and (not gnus-group-is-exiting-without-update-p)
9738              (gnus-check-backend-function
9739               'request-expire-articles gnus-newsgroup-name))
9740     ;; This backend supports expiry.
9741     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9742            (expirable (if total
9743                           (progn
9744                             ;; We need to update the info for
9745                             ;; this group for `gnus-list-of-read-articles'
9746                             ;; to give us the right answer.
9747                             (gnus-run-hooks 'gnus-exit-group-hook)
9748                             (gnus-summary-update-info)
9749                             (gnus-list-of-read-articles gnus-newsgroup-name))
9750                         (setq gnus-newsgroup-expirable
9751                               (sort gnus-newsgroup-expirable '<))))
9752            (expiry-wait (if now 'immediate
9753                           (gnus-group-find-parameter
9754                            gnus-newsgroup-name 'expiry-wait)))
9755            (nnmail-expiry-target
9756             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9757                 nnmail-expiry-target))
9758            es)
9759       (when expirable
9760         ;; There are expirable articles in this group, so we run them
9761         ;; through the expiry process.
9762         (gnus-message 6 "Expiring articles...")
9763         (unless (gnus-check-group gnus-newsgroup-name)
9764           (error "Can't open server for %s" gnus-newsgroup-name))
9765         ;; The list of articles that weren't expired is returned.
9766         (save-excursion
9767           (if expiry-wait
9768               (let ((nnmail-expiry-wait-function nil)
9769                     (nnmail-expiry-wait expiry-wait))
9770                 (setq es (gnus-request-expire-articles
9771                           expirable gnus-newsgroup-name)))
9772             (setq es (gnus-request-expire-articles
9773                       expirable gnus-newsgroup-name)))
9774           (unless total
9775             (setq gnus-newsgroup-expirable es))
9776           ;; We go through the old list of expirable, and mark all
9777           ;; really expired articles as nonexistent.
9778           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9779             (let ((gnus-use-cache nil))
9780               (dolist (article expirable)
9781                 (when (and (not (memq article es))
9782                            (gnus-data-find article))
9783                   (gnus-summary-mark-article article gnus-canceled-mark)
9784                   (run-hook-with-args 'gnus-summary-article-expire-hook
9785                                       'delete
9786                                       (gnus-data-header
9787                                        (assoc article (gnus-data-list nil)))
9788                                       gnus-newsgroup-name
9789                                       nil
9790                                       nil))))))
9791         (gnus-message 6 "Expiring articles...done")))))
9792
9793 (defun gnus-summary-expire-articles-now ()
9794   "Expunge all expirable articles in the current group.
9795 This means that *all* articles that are marked as expirable will be
9796 deleted forever, right now."
9797   (interactive)
9798   (or gnus-expert-user
9799       (gnus-yes-or-no-p
9800        "Are you really, really sure you want to delete all expirable messages? ")
9801       (error "Phew!"))
9802   (gnus-summary-expire-articles t))
9803
9804 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9805 (defun gnus-summary-delete-article (&optional n)
9806   "Delete the N next (mail) articles.
9807 This command actually deletes articles.  This is not a marking
9808 command.  The article will disappear forever from your life, never to
9809 return.
9810
9811 If N is negative, delete backwards.
9812 If N is nil and articles have been marked with the process mark,
9813 delete these instead.
9814
9815 If `gnus-novice-user' is non-nil you will be asked for
9816 confirmation before the articles are deleted."
9817   (interactive "P")
9818   (unless (gnus-check-backend-function 'request-expire-articles
9819                                        gnus-newsgroup-name)
9820     (error "The current newsgroup does not support article deletion"))
9821   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9822     (error "Couldn't open server"))
9823   ;; Compute the list of articles to delete.
9824   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9825         (nnmail-expiry-target 'delete)
9826         not-deleted)
9827     (if (and gnus-novice-user
9828              (not (gnus-yes-or-no-p
9829                    (format "Do you really want to delete %s forever? "
9830                            (if (> (length articles) 1)
9831                                (format "these %s articles" (length articles))
9832                              "this article")))))
9833         ()
9834       ;; Delete the articles.
9835       (setq not-deleted (gnus-request-expire-articles
9836                          articles gnus-newsgroup-name 'force))
9837       (while articles
9838         (gnus-summary-remove-process-mark (car articles))
9839         ;; The backend might not have been able to delete the article
9840         ;; after all.
9841         (unless (memq (car articles) not-deleted)
9842           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9843         (let* ((article (car articles))
9844                (ghead  (gnus-data-header
9845                                     (assoc article (gnus-data-list nil)))))
9846           (run-hook-with-args 'gnus-summary-article-delete-hook
9847                               'delete ghead gnus-newsgroup-name nil
9848                               nil))
9849         (setq articles (cdr articles)))
9850       (when not-deleted
9851         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9852     (gnus-summary-position-point)
9853     (gnus-set-mode-line 'summary)
9854     not-deleted))
9855
9856 (defun gnus-summary-edit-article (&optional arg)
9857   "Edit the current article.
9858 This will have permanent effect only in mail groups.
9859 If ARG is nil, edit the decoded articles.
9860 If ARG is 1, edit the raw articles.
9861 If ARG is 2, edit the raw articles even in read-only groups.
9862 If ARG is 3, edit the articles with the current handles.
9863 Otherwise, allow editing of articles even in read-only
9864 groups."
9865   (interactive "P")
9866   (let (force raw current-handles)
9867     (cond
9868      ((null arg))
9869      ((eq arg 1)
9870       (setq raw t))
9871      ((eq arg 2)
9872       (setq raw t
9873             force t))
9874      ((eq arg 3)
9875       (setq current-handles
9876             (and (gnus-buffer-live-p gnus-article-buffer)
9877                  (with-current-buffer gnus-article-buffer
9878                    (prog1
9879                        gnus-article-mime-handles
9880                      (setq gnus-article-mime-handles nil))))))
9881      (t
9882       (setq force t)))
9883     (when (and raw (not force)
9884                (member gnus-newsgroup-name '("nndraft:delayed"
9885                                              "nndraft:drafts"
9886                                              "nndraft:queue")))
9887       (error "Can't edit the raw article in group %s"
9888              gnus-newsgroup-name))
9889     (save-excursion
9890       (set-buffer gnus-summary-buffer)
9891       (let ((mail-parse-charset gnus-newsgroup-charset)
9892             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9893         (gnus-set-global-variables)
9894         (when (and (not force)
9895                    (gnus-group-read-only-p))
9896           (error "The current newsgroup does not support article editing"))
9897         (gnus-summary-show-article t)
9898         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9899           (with-current-buffer gnus-article-buffer
9900             (mm-enable-multibyte)))
9901         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9902             (setq raw t))
9903         (gnus-article-edit-article
9904          (if raw 'ignore
9905            `(lambda ()
9906               (let ((mbl mml-buffer-list))
9907                 (setq mml-buffer-list nil)
9908                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9909                   (mime-to-mml ,'current-handles))
9910                 (let ((mbl1 mml-buffer-list))
9911                   (setq mml-buffer-list mbl)
9912                   (set (make-local-variable 'mml-buffer-list) mbl1))
9913                 (gnus-make-local-hook 'kill-buffer-hook)
9914                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9915          `(lambda (no-highlight)
9916             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9917                   (message-options message-options)
9918                   (message-options-set-recipient)
9919                   (mail-parse-ignored-charsets
9920                    ',gnus-newsgroup-ignored-charsets))
9921               ,(if (not raw) '(progn
9922                                 (mml-to-mime)
9923                                 (mml-destroy-buffers)
9924                                 (remove-hook 'kill-buffer-hook
9925                                              'mml-destroy-buffers t)
9926                                 (kill-local-variable 'mml-buffer-list)))
9927               (gnus-summary-edit-article-done
9928                ,(or (mail-header-references gnus-current-headers) "")
9929                ,(gnus-group-read-only-p)
9930                ,gnus-summary-buffer no-highlight))))))))
9931
9932 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9933
9934 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9935                                                  no-highlight)
9936   "Make edits to the current article permanent."
9937   (interactive)
9938   (save-excursion
9939    ;; The buffer restriction contains the entire article if it exists.
9940     (when (article-goto-body)
9941       (let ((lines (count-lines (point) (point-max)))
9942             (length (- (point-max) (point)))
9943             (case-fold-search t)
9944             (body (copy-marker (point))))
9945         (goto-char (point-min))
9946         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9947           (delete-region (match-beginning 1) (match-end 1))
9948           (insert (number-to-string length)))
9949         (goto-char (point-min))
9950         (when (re-search-forward
9951                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9952           (delete-region (match-beginning 1) (match-end 1))
9953           (insert (number-to-string length)))
9954         (goto-char (point-min))
9955         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9956           (delete-region (match-beginning 1) (match-end 1))
9957           (insert (number-to-string lines))))))
9958   ;; Replace the article.
9959   (let ((buf (current-buffer)))
9960     (with-temp-buffer
9961       (insert-buffer-substring buf)
9962
9963       (if (and (not read-only)
9964                (not (gnus-request-replace-article
9965                      (cdr gnus-article-current) (car gnus-article-current)
9966                      (current-buffer) t)))
9967           (error "Couldn't replace article")
9968         ;; Update the summary buffer.
9969         (if (and references
9970                  (equal (message-tokenize-header references " ")
9971                         (message-tokenize-header
9972                          (or (message-fetch-field "references") "") " ")))
9973             ;; We only have to update this line.
9974             (save-excursion
9975               (save-restriction
9976                 (message-narrow-to-head)
9977                 (let ((head (buffer-substring-no-properties
9978                              (point-min) (point-max)))
9979                       header)
9980                   (with-temp-buffer
9981                     (insert (format "211 %d Article retrieved.\n"
9982                                     (cdr gnus-article-current)))
9983                     (insert head)
9984                     (insert ".\n")
9985                     (let ((nntp-server-buffer (current-buffer)))
9986                       (setq header (car (gnus-get-newsgroup-headers
9987                                          nil t))))
9988                     (save-excursion
9989                       (set-buffer gnus-summary-buffer)
9990                       (gnus-data-set-header
9991                        (gnus-data-find (cdr gnus-article-current))
9992                        header)
9993                       (gnus-summary-update-article-line
9994                        (cdr gnus-article-current) header)
9995                       (if (gnus-summary-goto-subject
9996                            (cdr gnus-article-current) nil t)
9997                           (gnus-summary-update-secondary-mark
9998                            (cdr gnus-article-current))))))))
9999           ;; Update threads.
10000           (set-buffer (or buffer gnus-summary-buffer))
10001           (gnus-summary-update-article (cdr gnus-article-current))
10002           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10003               (gnus-summary-update-secondary-mark
10004                (cdr gnus-article-current))))
10005         ;; Prettify the article buffer again.
10006         (unless no-highlight
10007           (save-excursion
10008             (set-buffer gnus-article-buffer)
10009             ;;;!!! Fix this -- article should be rehighlighted.
10010             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10011             (set-buffer gnus-original-article-buffer)
10012             (gnus-request-article
10013              (cdr gnus-article-current)
10014              (car gnus-article-current) (current-buffer))))
10015         ;; Prettify the summary buffer line.
10016         (when (gnus-visual-p 'summary-highlight 'highlight)
10017           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10018
10019 (defun gnus-summary-edit-wash (key)
10020   "Perform editing command KEY in the article buffer."
10021   (interactive
10022    (list
10023     (progn
10024       (message "%s" (concat (this-command-keys) "- "))
10025       (read-char))))
10026   (message "")
10027   (gnus-summary-edit-article)
10028   (execute-kbd-macro (concat (this-command-keys) key))
10029   (gnus-article-edit-done))
10030
10031 ;;; Respooling
10032
10033 (defun gnus-summary-respool-query (&optional silent trace)
10034   "Query where the respool algorithm would put this article."
10035   (interactive)
10036   (let (gnus-mark-article-hook)
10037     (gnus-summary-select-article)
10038     (save-excursion
10039       (set-buffer gnus-original-article-buffer)
10040       (let ((groups (nnmail-article-group 'identity trace)))
10041         (unless silent
10042           (if groups
10043               (message "This message would go to %s"
10044                        (mapconcat 'car groups ", "))
10045             (message "This message would go to no groups"))
10046           groups)))))
10047
10048 (defun gnus-summary-respool-trace ()
10049   "Trace where the respool algorithm would put this article.
10050 Display a buffer showing all fancy splitting patterns which matched."
10051   (interactive)
10052   (gnus-summary-respool-query nil t))
10053
10054 ;; Summary marking commands.
10055
10056 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10057   "Mark articles which has the same subject as read, and then select the next.
10058 If UNMARK is positive, remove any kind of mark.
10059 If UNMARK is negative, tick articles."
10060   (interactive "P")
10061   (when unmark
10062     (setq unmark (prefix-numeric-value unmark)))
10063   (let ((count
10064          (gnus-summary-mark-same-subject
10065           (gnus-summary-article-subject) unmark)))
10066     ;; Select next unread article.  If auto-select-same mode, should
10067     ;; select the first unread article.
10068     (gnus-summary-next-article t (and gnus-auto-select-same
10069                                       (gnus-summary-article-subject)))
10070     (gnus-message 7 "%d article%s marked as %s"
10071                   count (if (= count 1) " is" "s are")
10072                   (if unmark "unread" "read"))))
10073
10074 (defun gnus-summary-kill-same-subject (&optional unmark)
10075   "Mark articles which has the same subject as read.
10076 If UNMARK is positive, remove any kind of mark.
10077 If UNMARK is negative, tick articles."
10078   (interactive "P")
10079   (when unmark
10080     (setq unmark (prefix-numeric-value unmark)))
10081   (let ((count
10082          (gnus-summary-mark-same-subject
10083           (gnus-summary-article-subject) unmark)))
10084     ;; If marked as read, go to next unread subject.
10085     (when (null unmark)
10086       ;; Go to next unread subject.
10087       (gnus-summary-next-subject 1 t))
10088     (gnus-message 7 "%d articles are marked as %s"
10089                   count (if unmark "unread" "read"))))
10090
10091 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10092   "Mark articles with same SUBJECT as read, and return marked number.
10093 If optional argument UNMARK is positive, remove any kinds of marks.
10094 If optional argument UNMARK is negative, mark articles as unread instead."
10095   (let ((count 1))
10096     (save-excursion
10097       (cond
10098        ((null unmark)                   ; Mark as read.
10099         (while (and
10100                 (progn
10101                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10102                   (gnus-summary-show-thread) t)
10103                 (gnus-summary-find-subject subject))
10104           (setq count (1+ count))))
10105        ((> unmark 0)                    ; Tick.
10106         (while (and
10107                 (progn
10108                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10109                   (gnus-summary-show-thread) t)
10110                 (gnus-summary-find-subject subject))
10111           (setq count (1+ count))))
10112        (t                               ; Mark as unread.
10113         (while (and
10114                 (progn
10115                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10116                   (gnus-summary-show-thread) t)
10117                 (gnus-summary-find-subject subject))
10118           (setq count (1+ count)))))
10119       (gnus-set-mode-line 'summary)
10120       ;; Return the number of marked articles.
10121       count)))
10122
10123 (defun gnus-summary-mark-as-processable (n &optional unmark)
10124   "Set the process mark on the next N articles.
10125 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10126 the process mark instead.  The difference between N and the actual
10127 number of articles marked is returned."
10128   (interactive "P")
10129   (if (and (null n) (gnus-region-active-p))
10130       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10131     (setq n (prefix-numeric-value n))
10132     (let ((backward (< n 0))
10133           (n (abs n)))
10134       (while (and
10135               (> n 0)
10136               (if unmark
10137                   (gnus-summary-remove-process-mark
10138                    (gnus-summary-article-number))
10139                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10140               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10141         (setq n (1- n)))
10142       (when (/= 0 n)
10143         (gnus-message 7 "No more articles"))
10144       (gnus-summary-recenter)
10145       (gnus-summary-position-point)
10146       n)))
10147
10148 (defun gnus-summary-unmark-as-processable (n)
10149   "Remove the process mark from the next N articles.
10150 If N is negative, unmark backward instead.  The difference between N and
10151 the actual number of articles unmarked is returned."
10152   (interactive "P")
10153   (gnus-summary-mark-as-processable n t))
10154
10155 (defun gnus-summary-unmark-all-processable ()
10156   "Remove the process mark from all articles."
10157   (interactive)
10158   (save-excursion
10159     (while gnus-newsgroup-processable
10160       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10161   (gnus-summary-position-point))
10162
10163 (defun gnus-summary-add-mark (article type)
10164   "Mark ARTICLE with a mark of TYPE."
10165   (let ((vtype (car (assq type gnus-article-mark-lists)))
10166         var)
10167     (if (not vtype)
10168         (error "No such mark type: %s" type)
10169       (setq var (intern (format "gnus-newsgroup-%s" type)))
10170       (set var (cons article (symbol-value var)))
10171       (if (memq type '(processable cached replied forwarded recent saved))
10172           (gnus-summary-update-secondary-mark article)
10173         ;;; !!! This is bogus.  We should find out what primary
10174         ;;; !!! mark we want to set.
10175         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10176
10177 (defun gnus-summary-mark-as-expirable (n)
10178   "Mark N articles forward as expirable.
10179 If N is negative, mark backward instead.  The difference between N and
10180 the actual number of articles marked is returned."
10181   (interactive "p")
10182   (gnus-summary-mark-forward n gnus-expirable-mark))
10183
10184 (defun gnus-summary-mark-as-spam (n)
10185   "Mark N articles forward as spam.
10186 If N is negative, mark backward instead.  The difference between N and
10187 the actual number of articles marked is returned."
10188   (interactive "p")
10189   (gnus-summary-mark-forward n gnus-spam-mark))
10190
10191 (defun gnus-summary-mark-article-as-replied (article)
10192   "Mark ARTICLE as replied to and update the summary line.
10193 ARTICLE can also be a list of articles."
10194   (interactive (list (gnus-summary-article-number)))
10195   (let ((articles (if (listp article) article (list article))))
10196     (dolist (article articles)
10197       (unless (numberp article)
10198         (error "%s is not a number" article))
10199       (push article gnus-newsgroup-replied)
10200       (let ((buffer-read-only nil))
10201         (when (gnus-summary-goto-subject article nil t)
10202           (gnus-summary-update-secondary-mark article))))))
10203
10204 (defun gnus-summary-mark-article-as-forwarded (article)
10205   "Mark ARTICLE as forwarded and update the summary line.
10206 ARTICLE can also be a list of articles."
10207   (let ((articles (if (listp article) article (list article))))
10208     (dolist (article articles)
10209       (push article gnus-newsgroup-forwarded)
10210       (let ((buffer-read-only nil))
10211         (when (gnus-summary-goto-subject article nil t)
10212           (gnus-summary-update-secondary-mark article))))))
10213
10214 (defun gnus-summary-set-bookmark (article)
10215   "Set a bookmark in current article."
10216   (interactive (list (gnus-summary-article-number)))
10217   (when (or (not (get-buffer gnus-article-buffer))
10218             (not gnus-current-article)
10219             (not gnus-article-current)
10220             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10221     (error "No current article selected"))
10222   ;; Remove old bookmark, if one exists.
10223   (gnus-pull article gnus-newsgroup-bookmarks)
10224   ;; Set the new bookmark, which is on the form
10225   ;; (article-number . line-number-in-body).
10226   (push
10227    (cons article
10228          (with-current-buffer gnus-article-buffer
10229            (count-lines
10230             (min (point)
10231                  (save-excursion
10232                    (article-goto-body)
10233                    (point)))
10234             (point))))
10235    gnus-newsgroup-bookmarks)
10236   (gnus-message 6 "A bookmark has been added to the current article."))
10237
10238 (defun gnus-summary-remove-bookmark (article)
10239   "Remove the bookmark from the current article."
10240   (interactive (list (gnus-summary-article-number)))
10241   ;; Remove old bookmark, if one exists.
10242   (if (not (assq article gnus-newsgroup-bookmarks))
10243       (gnus-message 6 "No bookmark in current article.")
10244     (gnus-pull article gnus-newsgroup-bookmarks)
10245     (gnus-message 6 "Removed bookmark.")))
10246
10247 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10248 (defun gnus-summary-mark-as-dormant (n)
10249   "Mark N articles forward as dormant.
10250 If N is negative, mark backward instead.  The difference between N and
10251 the actual number of articles marked is returned."
10252   (interactive "p")
10253   (gnus-summary-mark-forward n gnus-dormant-mark))
10254
10255 (defun gnus-summary-set-process-mark (article)
10256   "Set the process mark on ARTICLE and update the summary line."
10257   (setq gnus-newsgroup-processable
10258         (cons article
10259               (delq article gnus-newsgroup-processable)))
10260   (when (gnus-summary-goto-subject article)
10261     (gnus-summary-show-thread)
10262     (gnus-summary-goto-subject article)
10263     (gnus-summary-update-secondary-mark article)))
10264
10265 (defun gnus-summary-remove-process-mark (&rest articles)
10266   "Remove the process mark from ARTICLES and update the summary line."
10267   (dolist (article articles)
10268     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10269     (when (gnus-summary-goto-subject article)
10270       (gnus-summary-show-thread)
10271       (gnus-summary-goto-subject article)
10272       (gnus-summary-update-secondary-mark article)))
10273   t)
10274
10275 (defun gnus-summary-set-saved-mark (article)
10276   "Set the process mark on ARTICLE and update the summary line."
10277   (push article gnus-newsgroup-saved)
10278   (when (gnus-summary-goto-subject article)
10279     (gnus-summary-update-secondary-mark article)))
10280
10281 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10282   "Mark N articles as read forwards.
10283 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10284 The difference between N and the actual number of articles marked is
10285 returned.
10286 If NO-EXPIRE, auto-expiry will be inhibited."
10287   (interactive "p")
10288   (gnus-summary-show-thread)
10289   (let ((backward (< n 0))
10290         (gnus-summary-goto-unread
10291          (and gnus-summary-goto-unread
10292               (not (eq gnus-summary-goto-unread 'never))
10293               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10294                                     gnus-ticked-mark gnus-dormant-mark)))))
10295         (n (abs n))
10296         (mark (or mark gnus-del-mark)))
10297     (while (and (> n 0)
10298                 (gnus-summary-mark-article nil mark no-expire)
10299                 (zerop (gnus-summary-next-subject
10300                         (if backward -1 1)
10301                         (and gnus-summary-goto-unread
10302                              (not (eq gnus-summary-goto-unread 'never)))
10303                         t)))
10304       (setq n (1- n)))
10305     (when (/= 0 n)
10306       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10307     (gnus-summary-recenter)
10308     (gnus-summary-position-point)
10309     (gnus-set-mode-line 'summary)
10310     n))
10311
10312 (defun gnus-summary-mark-article-as-read (mark)
10313   "Mark the current article quickly as read with MARK."
10314   (let ((article (gnus-summary-article-number)))
10315     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10316     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10317     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10318     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10319     (push (cons article mark) gnus-newsgroup-reads)
10320     ;; Possibly remove from cache, if that is used.
10321     (when gnus-use-cache
10322       (gnus-cache-enter-remove-article article))
10323     ;; Allow the backend to change the mark.
10324     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10325     ;; Check for auto-expiry.
10326     (when (and gnus-newsgroup-auto-expire
10327                (memq mark gnus-auto-expirable-marks))
10328       (setq mark gnus-expirable-mark)
10329       ;; Let the backend know about the mark change.
10330       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10331       (push article gnus-newsgroup-expirable))
10332     ;; Set the mark in the buffer.
10333     (gnus-summary-update-mark mark 'unread)
10334     t))
10335
10336 (defun gnus-summary-mark-article-as-unread (mark)
10337   "Mark the current article quickly as unread with MARK."
10338   (let* ((article (gnus-summary-article-number))
10339          (old-mark (gnus-summary-article-mark article)))
10340     ;; Allow the backend to change the mark.
10341     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10342     (if (eq mark old-mark)
10343         t
10344       (if (<= article 0)
10345           (progn
10346             (gnus-error 1 "Can't mark negative article numbers")
10347             nil)
10348         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10349         (setq gnus-newsgroup-spam-marked
10350               (delq article gnus-newsgroup-spam-marked))
10351         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10352         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10353         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10354         (cond ((= mark gnus-ticked-mark)
10355                (setq gnus-newsgroup-marked
10356                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10357                                               article)))
10358               ((= mark gnus-spam-mark)
10359                (setq gnus-newsgroup-spam-marked
10360                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10361                                               article)))
10362               ((= mark gnus-dormant-mark)
10363                (setq gnus-newsgroup-dormant
10364                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10365                                               article)))
10366               (t
10367                (setq gnus-newsgroup-unreads
10368                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10369                                               article))))
10370         (gnus-pull article gnus-newsgroup-reads)
10371
10372         ;; See whether the article is to be put in the cache.
10373         (and gnus-use-cache
10374              (vectorp (gnus-summary-article-header article))
10375              (save-excursion
10376                (gnus-cache-possibly-enter-article
10377                 gnus-newsgroup-name article
10378                 (= mark gnus-ticked-mark)
10379                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10380
10381         ;; Fix the mark.
10382         (gnus-summary-update-mark mark 'unread)
10383         t))))
10384
10385 (defun gnus-summary-mark-article (&optional article mark no-expire)
10386   "Mark ARTICLE with MARK.  MARK can be any character.
10387 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10388 `??' (dormant) and `?E' (expirable).
10389 If MARK is nil, then the default character `?r' is used.
10390 If ARTICLE is nil, then the article on the current line will be
10391 marked.
10392 If NO-EXPIRE, auto-expiry will be inhibited."
10393   ;; The mark might be a string.
10394   (when (stringp mark)
10395     (setq mark (aref mark 0)))
10396   ;; If no mark is given, then we check auto-expiring.
10397   (when (null mark)
10398     (setq mark gnus-del-mark))
10399   (when (and (not no-expire)
10400              gnus-newsgroup-auto-expire
10401              (memq mark gnus-auto-expirable-marks))
10402     (setq mark gnus-expirable-mark))
10403   (let ((article (or article (gnus-summary-article-number)))
10404         (old-mark (gnus-summary-article-mark article)))
10405     ;; Allow the backend to change the mark.
10406     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10407     (if (eq mark old-mark)
10408         t
10409       (unless article
10410         (error "No article on current line"))
10411       (if (not (if (or (= mark gnus-unread-mark)
10412                        (= mark gnus-ticked-mark)
10413                        (= mark gnus-spam-mark)
10414                        (= mark gnus-dormant-mark))
10415                    (gnus-mark-article-as-unread article mark)
10416                  (gnus-mark-article-as-read article mark)))
10417           t
10418         ;; See whether the article is to be put in the cache.
10419         (and gnus-use-cache
10420              (not (= mark gnus-canceled-mark))
10421              (vectorp (gnus-summary-article-header article))
10422              (save-excursion
10423                (gnus-cache-possibly-enter-article
10424                 gnus-newsgroup-name article
10425                 (= mark gnus-ticked-mark)
10426                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10427
10428         (when (gnus-summary-goto-subject article nil t)
10429           (let ((buffer-read-only nil))
10430             (gnus-summary-show-thread)
10431             ;; Fix the mark.
10432             (gnus-summary-update-mark mark 'unread)
10433             t))))))
10434
10435 (defun gnus-summary-update-secondary-mark (article)
10436   "Update the secondary (read, process, cache) mark."
10437   (gnus-summary-update-mark
10438    (cond ((memq article gnus-newsgroup-processable)
10439           gnus-process-mark)
10440          ((memq article gnus-newsgroup-cached)
10441           gnus-cached-mark)
10442          ((memq article gnus-newsgroup-replied)
10443           gnus-replied-mark)
10444          ((memq article gnus-newsgroup-forwarded)
10445           gnus-forwarded-mark)
10446          ((memq article gnus-newsgroup-saved)
10447           gnus-saved-mark)
10448          ((memq article gnus-newsgroup-recent)
10449           gnus-recent-mark)
10450          ((memq article gnus-newsgroup-unseen)
10451           gnus-unseen-mark)
10452          (t gnus-no-mark))
10453    'replied)
10454   (when (gnus-visual-p 'summary-highlight 'highlight)
10455     (gnus-run-hooks 'gnus-summary-update-hook))
10456   t)
10457
10458 (defun gnus-summary-update-download-mark (article)
10459   "Update the download mark."
10460   (gnus-summary-update-mark
10461    (cond ((memq article gnus-newsgroup-undownloaded)
10462           gnus-undownloaded-mark)
10463          (gnus-newsgroup-agentized
10464           gnus-downloaded-mark)
10465          (t
10466           gnus-no-mark))
10467    'download)
10468   (gnus-summary-update-line t)
10469   t)
10470
10471 (defun gnus-summary-update-mark (mark type)
10472   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10473         (buffer-read-only nil))
10474     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10475     (when forward
10476       (when (looking-at "\r")
10477         (incf forward))
10478       (when (<= (+ forward (point)) (point-max))
10479         ;; Go to the right position on the line.
10480         (goto-char (+ forward (point)))
10481         ;; Replace the old mark with the new mark.
10482         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10483         ;; Optionally update the marks by some user rule.
10484         (when (eq type 'unread)
10485           (gnus-data-set-mark
10486            (gnus-data-find (gnus-summary-article-number)) mark)
10487           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10488
10489 (defun gnus-mark-article-as-read (article &optional mark)
10490   "Enter ARTICLE in the pertinent lists and remove it from others."
10491   ;; Make the article expirable.
10492   (let ((mark (or mark gnus-del-mark)))
10493     (setq gnus-newsgroup-expirable
10494           (if (= mark gnus-expirable-mark)
10495               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10496             (delq article gnus-newsgroup-expirable)))
10497     ;; Remove from unread and marked lists.
10498     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10499     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10500     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10501     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10502     (push (cons article mark) gnus-newsgroup-reads)
10503     ;; Possibly remove from cache, if that is used.
10504     (when gnus-use-cache
10505       (gnus-cache-enter-remove-article article))
10506     t))
10507
10508 (defun gnus-mark-article-as-unread (article &optional mark)
10509   "Enter ARTICLE in the pertinent lists and remove it from others."
10510   (let ((mark (or mark gnus-ticked-mark)))
10511     (if (<= article 0)
10512         (progn
10513           (gnus-error 1 "Can't mark negative article numbers")
10514           nil)
10515       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10516             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10517             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10518             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10519             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10520
10521       ;; Unsuppress duplicates?
10522       (when gnus-suppress-duplicates
10523         (gnus-dup-unsuppress-article article))
10524
10525       (cond ((= mark gnus-ticked-mark)
10526              (setq gnus-newsgroup-marked
10527                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10528             ((= mark gnus-spam-mark)
10529              (setq gnus-newsgroup-spam-marked
10530                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10531                                             article)))
10532             ((= mark gnus-dormant-mark)
10533              (setq gnus-newsgroup-dormant
10534                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10535             (t
10536              (setq gnus-newsgroup-unreads
10537                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10538       (gnus-pull article gnus-newsgroup-reads)
10539       t)))
10540
10541 (defalias 'gnus-summary-mark-as-unread-forward
10542   'gnus-summary-tick-article-forward)
10543 (make-obsolete 'gnus-summary-mark-as-unread-forward
10544                'gnus-summary-tick-article-forward)
10545 (defun gnus-summary-tick-article-forward (n)
10546   "Tick N articles forwards.
10547 If N is negative, tick backwards instead.
10548 The difference between N and the number of articles ticked is returned."
10549   (interactive "p")
10550   (gnus-summary-mark-forward n gnus-ticked-mark))
10551
10552 (defalias 'gnus-summary-mark-as-unread-backward
10553   'gnus-summary-tick-article-backward)
10554 (make-obsolete 'gnus-summary-mark-as-unread-backward
10555                'gnus-summary-tick-article-backward)
10556 (defun gnus-summary-tick-article-backward (n)
10557   "Tick N articles backwards.
10558 The difference between N and the number of articles ticked is returned."
10559   (interactive "p")
10560   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10561
10562 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10563 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10564 (defun gnus-summary-tick-article (&optional article clear-mark)
10565   "Mark current article as unread.
10566 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10567 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10568   (interactive)
10569   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10570                                        gnus-ticked-mark)))
10571
10572 (defun gnus-summary-mark-as-read-forward (n)
10573   "Mark N articles as read forwards.
10574 If N is negative, mark backwards instead.
10575 The difference between N and the actual number of articles marked is
10576 returned."
10577   (interactive "p")
10578   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10579
10580 (defun gnus-summary-mark-as-read-backward (n)
10581   "Mark the N articles as read backwards.
10582 The difference between N and the actual number of articles marked is
10583 returned."
10584   (interactive "p")
10585   (gnus-summary-mark-forward
10586    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10587
10588 (defun gnus-summary-mark-as-read (&optional article mark)
10589   "Mark current article as read.
10590 ARTICLE specifies the article to be marked as read.
10591 MARK specifies a string to be inserted at the beginning of the line."
10592   (gnus-summary-mark-article article mark))
10593
10594 (defun gnus-summary-clear-mark-forward (n)
10595   "Clear marks from N articles forward.
10596 If N is negative, clear backward instead.
10597 The difference between N and the number of marks cleared is returned."
10598   (interactive "p")
10599   (gnus-summary-mark-forward n gnus-unread-mark))
10600
10601 (defun gnus-summary-clear-mark-backward (n)
10602   "Clear marks from N articles backward.
10603 The difference between N and the number of marks cleared is returned."
10604   (interactive "p")
10605   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10606
10607 (defun gnus-summary-mark-unread-as-read ()
10608   "Intended to be used by `gnus-summary-mark-article-hook'."
10609   (when (memq gnus-current-article gnus-newsgroup-unreads)
10610     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10611
10612 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10613   "Intended to be used by `gnus-summary-mark-article-hook'."
10614   (let ((mark (gnus-summary-article-mark)))
10615     (when (or (gnus-unread-mark-p mark)
10616               (gnus-read-mark-p mark))
10617       (gnus-summary-mark-article gnus-current-article
10618                                  (or new-mark gnus-read-mark)))))
10619
10620 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10621   "Intended to be used by `gnus-summary-mark-article-hook'."
10622   (let ((mark (gnus-summary-article-mark)))
10623     (when (or (gnus-unread-mark-p mark)
10624               (gnus-read-mark-p mark))
10625       (gnus-summary-mark-article (gnus-summary-article-number)
10626                                  (or new-mark gnus-read-mark)))))
10627
10628 (defun gnus-summary-mark-unread-as-ticked ()
10629   "Intended to be used by `gnus-summary-mark-article-hook'."
10630   (when (memq gnus-current-article gnus-newsgroup-unreads)
10631     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10632
10633 (defun gnus-summary-mark-region-as-read (point mark all)
10634   "Mark all unread articles between point and mark as read.
10635 If given a prefix, mark all articles between point and mark as read,
10636 even ticked and dormant ones."
10637   (interactive "r\nP")
10638   (save-excursion
10639     (let (article)
10640       (goto-char point)
10641       (beginning-of-line)
10642       (while (and
10643               (< (point) mark)
10644               (progn
10645                 (when (or all
10646                           (memq (setq article (gnus-summary-article-number))
10647                                 gnus-newsgroup-unreads))
10648                   (gnus-summary-mark-article article gnus-del-mark))
10649                 t)
10650               (gnus-summary-find-next))))))
10651
10652 (defun gnus-summary-mark-below (score mark)
10653   "Mark articles with score less than SCORE with MARK."
10654   (interactive "P\ncMark: ")
10655   (setq score (if score
10656                   (prefix-numeric-value score)
10657                 (or gnus-summary-default-score 0)))
10658   (save-excursion
10659     (set-buffer gnus-summary-buffer)
10660     (goto-char (point-min))
10661     (while
10662         (progn
10663           (and (< (gnus-summary-article-score) score)
10664                (gnus-summary-mark-article nil mark))
10665           (gnus-summary-find-next)))))
10666
10667 (defun gnus-summary-kill-below (&optional score)
10668   "Mark articles with score below SCORE as read."
10669   (interactive "P")
10670   (gnus-summary-mark-below score gnus-killed-mark))
10671
10672 (defun gnus-summary-clear-above (&optional score)
10673   "Clear all marks from articles with score above SCORE."
10674   (interactive "P")
10675   (gnus-summary-mark-above score gnus-unread-mark))
10676
10677 (defun gnus-summary-tick-above (&optional score)
10678   "Tick all articles with score above SCORE."
10679   (interactive "P")
10680   (gnus-summary-mark-above score gnus-ticked-mark))
10681
10682 (defun gnus-summary-mark-above (score mark)
10683   "Mark articles with score over SCORE with MARK."
10684   (interactive "P\ncMark: ")
10685   (setq score (if score
10686                   (prefix-numeric-value score)
10687                 (or gnus-summary-default-score 0)))
10688   (save-excursion
10689     (set-buffer gnus-summary-buffer)
10690     (goto-char (point-min))
10691     (while (and (progn
10692                   (when (> (gnus-summary-article-score) score)
10693                     (gnus-summary-mark-article nil mark))
10694                   t)
10695                 (gnus-summary-find-next)))))
10696
10697 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10698 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10699 (defun gnus-summary-limit-include-expunged (&optional no-error)
10700   "Display all the hidden articles that were expunged for low scores."
10701   (interactive)
10702   (let ((buffer-read-only nil))
10703     (let ((scored gnus-newsgroup-scored)
10704           headers h)
10705       (while scored
10706         (unless (gnus-summary-article-header (caar scored))
10707           (and (setq h (gnus-number-to-header (caar scored)))
10708                (< (cdar scored) gnus-summary-expunge-below)
10709                (push h headers)))
10710         (setq scored (cdr scored)))
10711       (if (not headers)
10712           (when (not no-error)
10713             (error "No expunged articles hidden"))
10714         (goto-char (point-min))
10715         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10716         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10717         (mapcar (lambda (x) (push (mail-header-number x)
10718                                   gnus-newsgroup-limit))
10719                 headers)
10720         (gnus-summary-prepare-unthreaded (nreverse headers))
10721         (goto-char (point-min))
10722         (gnus-summary-position-point)
10723         t))))
10724
10725 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10726   "Mark all unread articles in this newsgroup as read.
10727 If prefix argument ALL is non-nil, ticked and dormant articles will
10728 also be marked as read.
10729 If QUIETLY is non-nil, no questions will be asked.
10730
10731 If TO-HERE is non-nil, it should be a point in the buffer.  All
10732 articles before (after, if REVERSE is set) this point will be marked
10733 as read.
10734
10735 Note that this function will only catch up the unread article
10736 in the current summary buffer limitation.
10737
10738 The number of articles marked as read is returned."
10739   (interactive "P")
10740   (prog1
10741       (save-excursion
10742         (when (or quietly
10743                   (not gnus-interactive-catchup) ;Without confirmation?
10744                   gnus-expert-user
10745                   (gnus-y-or-n-p
10746                    (if all
10747                        "Mark absolutely all articles as read? "
10748                      "Mark all unread articles as read? ")))
10749           (if (and not-mark
10750                    (not gnus-newsgroup-adaptive)
10751                    (not gnus-newsgroup-auto-expire)
10752                    (not gnus-suppress-duplicates)
10753                    (or (not gnus-use-cache)
10754                        (eq gnus-use-cache 'passive)))
10755               (progn
10756                 (when all
10757                   (setq gnus-newsgroup-marked nil
10758                         gnus-newsgroup-spam-marked nil
10759                         gnus-newsgroup-dormant nil))
10760                 (setq gnus-newsgroup-unreads
10761                       (gnus-sorted-nunion
10762                        (gnus-sorted-intersection gnus-newsgroup-unreads
10763                                                  gnus-newsgroup-downloadable)
10764                        gnus-newsgroup-unfetched)))
10765             ;; We actually mark all articles as canceled, which we
10766             ;; have to do when using auto-expiry or adaptive scoring.
10767             (gnus-summary-show-all-threads)
10768             (if (and to-here reverse)
10769                 (progn
10770                   (goto-char to-here)
10771                   (gnus-summary-mark-current-read-and-unread-as-read
10772                    gnus-catchup-mark)
10773                   (while (gnus-summary-find-next (not all))
10774                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10775               (when (gnus-summary-first-subject (not all))
10776                 (while (and
10777                         (if to-here (< (point) to-here) t)
10778                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10779                         (gnus-summary-find-next (not all))))))
10780             (gnus-set-mode-line 'summary))
10781           t))
10782     (gnus-summary-position-point)))
10783
10784 (defun gnus-summary-catchup-to-here (&optional all)
10785   "Mark all unticked articles before the current one as read.
10786 If ALL is non-nil, also mark ticked and dormant articles as read."
10787   (interactive "P")
10788   (save-excursion
10789     (gnus-save-hidden-threads
10790       (let ((beg (point)))
10791         ;; We check that there are unread articles.
10792         (when (or all (gnus-summary-find-prev))
10793           (gnus-summary-catchup all t beg)))))
10794   (gnus-summary-position-point))
10795
10796 (defun gnus-summary-catchup-from-here (&optional all)
10797   "Mark all unticked articles after (and including) the current one as read.
10798 If ALL is non-nil, also mark ticked and dormant articles as read."
10799   (interactive "P")
10800   (save-excursion
10801     (gnus-save-hidden-threads
10802       (let ((beg (point)))
10803         ;; We check that there are unread articles.
10804         (when (or all (gnus-summary-find-next))
10805           (gnus-summary-catchup all t beg nil t)))))
10806   (gnus-summary-position-point))
10807
10808 (defun gnus-summary-catchup-all (&optional quietly)
10809   "Mark all articles in this newsgroup as read.
10810 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10811 instead, which marks only unread articles as read."
10812   (interactive "P")
10813   (gnus-summary-catchup t quietly))
10814
10815 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10816   "Mark all unread articles in this group as read, then exit.
10817 If prefix argument ALL is non-nil, all articles are marked as read.
10818 If QUIETLY is non-nil, no questions will be asked."
10819   (interactive "P")
10820   (when (gnus-summary-catchup all quietly nil 'fast)
10821     ;; Select next newsgroup or exit.
10822     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10823              (eq gnus-auto-select-next 'quietly))
10824         (gnus-summary-next-group nil)
10825       (gnus-summary-exit))))
10826
10827 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10828   "Mark all articles in this newsgroup as read, and then exit.
10829 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10830 instead, which marks only unread articles as read."
10831   (interactive "P")
10832   (gnus-summary-catchup-and-exit t quietly))
10833
10834 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10835   "Mark all articles in this group as read and select the next group.
10836 If given a prefix, mark all articles, unread as well as ticked, as
10837 read."
10838   (interactive "P")
10839   (save-excursion
10840     (gnus-summary-catchup all))
10841   (gnus-summary-next-group))
10842
10843 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10844   "Mark all articles in this group as read and select the previous group.
10845 If given a prefix, mark all articles, unread as well as ticked, as
10846 read."
10847   (interactive "P")
10848   (save-excursion
10849     (gnus-summary-catchup all))
10850   (gnus-summary-next-group nil nil t))
10851
10852 ;;;
10853 ;;; with article
10854 ;;;
10855
10856 (defmacro gnus-with-article (article &rest forms)
10857   "Select ARTICLE and perform FORMS in the original article buffer.
10858 Then replace the article with the result."
10859   `(progn
10860      ;; We don't want the article to be marked as read.
10861      (let (gnus-mark-article-hook)
10862        (gnus-summary-select-article t t nil ,article))
10863      (set-buffer gnus-original-article-buffer)
10864      ,@forms
10865      (if (not (gnus-check-backend-function
10866                'request-replace-article (car gnus-article-current)))
10867          (gnus-message 5 "Read-only group; not replacing")
10868        (unless (gnus-request-replace-article
10869                 ,article (car gnus-article-current)
10870                 (current-buffer) t)
10871          (error "Couldn't replace article")))
10872      ;; The cache and backlog have to be flushed somewhat.
10873      (when gnus-keep-backlog
10874        (gnus-backlog-remove-article
10875         (car gnus-article-current) (cdr gnus-article-current)))
10876      (when gnus-use-cache
10877        (gnus-cache-update-article
10878         (car gnus-article-current) (cdr gnus-article-current)))))
10879
10880 (put 'gnus-with-article 'lisp-indent-function 1)
10881 (put 'gnus-with-article 'edebug-form-spec '(form body))
10882
10883 ;; Thread-based commands.
10884
10885 (defun gnus-summary-articles-in-thread (&optional article)
10886   "Return a list of all articles in the current thread.
10887 If ARTICLE is non-nil, return all articles in the thread that starts
10888 with that article."
10889   (let* ((article (or article (gnus-summary-article-number)))
10890          (data (gnus-data-find-list article))
10891          (top-level (gnus-data-level (car data)))
10892          (top-subject
10893           (cond ((null gnus-thread-operation-ignore-subject)
10894                  (gnus-simplify-subject-re
10895                   (mail-header-subject (gnus-data-header (car data)))))
10896                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10897                  (gnus-simplify-subject-fuzzy
10898                   (mail-header-subject (gnus-data-header (car data)))))
10899                 (t nil)))
10900          (end-point (save-excursion
10901                       (if (gnus-summary-go-to-next-thread)
10902                           (point) (point-max))))
10903          articles)
10904     (while (and data
10905                 (< (gnus-data-pos (car data)) end-point))
10906       (when (or (not top-subject)
10907                 (string= top-subject
10908                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10909                              (gnus-simplify-subject-fuzzy
10910                               (mail-header-subject
10911                                (gnus-data-header (car data))))
10912                            (gnus-simplify-subject-re
10913                             (mail-header-subject
10914                              (gnus-data-header (car data)))))))
10915         (push (gnus-data-number (car data)) articles))
10916       (unless (and (setq data (cdr data))
10917                    (> (gnus-data-level (car data)) top-level))
10918         (setq data nil)))
10919     ;; Return the list of articles.
10920     (nreverse articles)))
10921
10922 (defun gnus-summary-rethread-current ()
10923   "Rethread the thread the current article is part of."
10924   (interactive)
10925   (let* ((gnus-show-threads t)
10926          (article (gnus-summary-article-number))
10927          (id (mail-header-id (gnus-summary-article-header)))
10928          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10929     (unless id
10930       (error "No article on the current line"))
10931     (gnus-rebuild-thread id)
10932     (gnus-summary-goto-subject article)))
10933
10934 (defun gnus-summary-reparent-thread ()
10935   "Make the current article child of the marked (or previous) article.
10936
10937 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10938 is non-nil or the Subject: of both articles are the same."
10939   (interactive)
10940   (unless (not (gnus-group-read-only-p))
10941     (error "The current newsgroup does not support article editing"))
10942   (unless (<= (length gnus-newsgroup-processable) 1)
10943     (error "No more than one article may be marked"))
10944   (let ((child (gnus-summary-article-number))
10945         ;; First grab the marked article, otherwise one line up.
10946         (parent (if (not (null gnus-newsgroup-processable))
10947                     (car gnus-newsgroup-processable)
10948                   (save-excursion
10949                     (if (eq (forward-line -1) 0)
10950                         (gnus-summary-article-number)
10951                       (error "Beginning of summary buffer"))))))
10952     (gnus-summary-reparent-children parent (list child))))
10953
10954 (defun gnus-summary-reparent-children (parent children)
10955   "Make PARENT the parent of CHILDREN.
10956 When called interactively, PARENT is is current article and
10957 CHILDREN are the process-marked articles."
10958   (interactive
10959    (list (gnus-summary-article-number)
10960          (gnus-summary-work-articles nil)))
10961   (dolist (child children)
10962     (save-window-excursion
10963       (let ((gnus-article-buffer " *reparent*"))
10964         (unless (not (eq parent child))
10965           (error "An article may not be self-referential"))
10966         (let ((message-id (mail-header-id
10967                            (gnus-summary-article-header parent))))
10968           (unless (and message-id (not (equal message-id "")))
10969             (error "No message-id in desired parent"))
10970           (gnus-with-article child
10971             (save-restriction
10972               (goto-char (point-min))
10973               (message-narrow-to-head)
10974               (if (re-search-forward "^References: " nil t)
10975                   (progn
10976                     (re-search-forward "^[^ \t]" nil t)
10977                     (forward-line -1)
10978                     (end-of-line)
10979                     (insert " " message-id))
10980                 (insert "References: " message-id "\n"))))
10981           (set-buffer gnus-summary-buffer)
10982           (gnus-summary-unmark-all-processable)
10983           (gnus-summary-update-article child)
10984           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10985             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10986           (gnus-summary-rethread-current)
10987           (gnus-message 3 "Article %d is now the child of article %d"
10988                         child parent))))))
10989
10990 (defun gnus-summary-toggle-threads (&optional arg)
10991   "Toggle showing conversation threads.
10992 If ARG is positive number, turn showing conversation threads on."
10993   (interactive "P")
10994   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10995     (setq gnus-show-threads
10996           (if (null arg) (not gnus-show-threads)
10997             (> (prefix-numeric-value arg) 0)))
10998     (gnus-summary-prepare)
10999     (gnus-summary-goto-subject current)
11000     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11001     (gnus-summary-position-point)))
11002
11003 (defun gnus-summary-show-all-threads ()
11004   "Show all threads."
11005   (interactive)
11006   (save-excursion
11007     (let ((buffer-read-only nil))
11008       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11009   (gnus-summary-position-point))
11010
11011 (defun gnus-summary-show-thread ()
11012   "Show thread subtrees.
11013 Returns nil if no thread was there to be shown."
11014   (interactive)
11015   (let ((buffer-read-only nil)
11016         (orig (point))
11017         (end (point-at-eol))
11018         ;; Leave point at bol
11019         (beg (progn (beginning-of-line) (point))))
11020     (prog1
11021         ;; Any hidden lines here?
11022         (search-forward "\r" end t)
11023       (subst-char-in-region beg end ?\^M ?\n t)
11024       (goto-char orig)
11025       (gnus-summary-position-point))))
11026
11027 (defun gnus-summary-maybe-hide-threads ()
11028   "If requested, hide the threads that should be hidden."
11029   (when (and gnus-show-threads
11030              gnus-thread-hide-subtree)
11031     (gnus-summary-hide-all-threads
11032      (if (or (consp gnus-thread-hide-subtree)
11033              (functionp gnus-thread-hide-subtree))
11034          (gnus-make-predicate gnus-thread-hide-subtree)
11035        nil))))
11036
11037 ;;; Hiding predicates.
11038
11039 (defun gnus-article-unread-p (header)
11040   (memq (mail-header-number header) gnus-newsgroup-unreads))
11041
11042 (defun gnus-article-unseen-p (header)
11043   (memq (mail-header-number header) gnus-newsgroup-unseen))
11044
11045 (defun gnus-map-articles (predicate articles)
11046   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11047   (apply 'gnus-or (mapcar predicate
11048                           (mapcar (lambda (number)
11049                                     (gnus-summary-article-header number))
11050                                   articles))))
11051
11052 (defun gnus-summary-hide-all-threads (&optional predicate)
11053   "Hide all thread subtrees.
11054 If PREDICATE is supplied, threads that satisfy this predicate
11055 will not be hidden."
11056   (interactive)
11057   (save-excursion
11058     (goto-char (point-min))
11059     (let ((end nil))
11060       (while (not end)
11061         (when (or (not predicate)
11062                   (gnus-map-articles
11063                    predicate (gnus-summary-article-children)))
11064             (gnus-summary-hide-thread))
11065         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11066   (gnus-summary-position-point))
11067
11068 (defun gnus-summary-hide-thread ()
11069   "Hide thread subtrees.
11070 If PREDICATE is supplied, threads that satisfy this predicate
11071 will not be hidden.
11072 Returns nil if no threads were there to be hidden."
11073   (interactive)
11074   (let ((buffer-read-only nil)
11075         (start (point))
11076         (article (gnus-summary-article-number)))
11077     (goto-char start)
11078     ;; Go forward until either the buffer ends or the subthread
11079     ;; ends.
11080     (when (and (not (eobp))
11081                (or (zerop (gnus-summary-next-thread 1 t))
11082                    (goto-char (point-max))))
11083       (prog1
11084           (if (and (> (point) start)
11085                    (search-backward "\n" start t))
11086               (progn
11087                 (subst-char-in-region start (point) ?\n ?\^M)
11088                 (gnus-summary-goto-subject article))
11089             (goto-char start)
11090             nil)))))
11091
11092 (defun gnus-summary-go-to-next-thread (&optional previous)
11093   "Go to the same level (or less) next thread.
11094 If PREVIOUS is non-nil, go to previous thread instead.
11095 Return the article number moved to, or nil if moving was impossible."
11096   (let ((level (gnus-summary-thread-level))
11097         (way (if previous -1 1))
11098         (beg (point)))
11099     (forward-line way)
11100     (while (and (not (eobp))
11101                 (< level (gnus-summary-thread-level)))
11102       (forward-line way))
11103     (if (eobp)
11104         (progn
11105           (goto-char beg)
11106           nil)
11107       (setq beg (point))
11108       (prog1
11109           (gnus-summary-article-number)
11110         (goto-char beg)))))
11111
11112 (defun gnus-summary-next-thread (n &optional silent)
11113   "Go to the same level next N'th thread.
11114 If N is negative, search backward instead.
11115 Returns the difference between N and the number of skips actually
11116 done.
11117
11118 If SILENT, don't output messages."
11119   (interactive "p")
11120   (let ((backward (< n 0))
11121         (n (abs n)))
11122     (while (and (> n 0)
11123                 (gnus-summary-go-to-next-thread backward))
11124       (decf n))
11125     (unless silent
11126       (gnus-summary-position-point))
11127     (when (and (not silent) (/= 0 n))
11128       (gnus-message 7 "No more threads"))
11129     n))
11130
11131 (defun gnus-summary-prev-thread (n)
11132   "Go to the same level previous N'th thread.
11133 Returns the difference between N and the number of skips actually
11134 done."
11135   (interactive "p")
11136   (gnus-summary-next-thread (- n)))
11137
11138 (defun gnus-summary-go-down-thread ()
11139   "Go down one level in the current thread."
11140   (let ((children (gnus-summary-article-children)))
11141     (when children
11142       (gnus-summary-goto-subject (car children)))))
11143
11144 (defun gnus-summary-go-up-thread ()
11145   "Go up one level in the current thread."
11146   (let ((parent (gnus-summary-article-parent)))
11147     (when parent
11148       (gnus-summary-goto-subject parent))))
11149
11150 (defun gnus-summary-down-thread (n)
11151   "Go down thread N steps.
11152 If N is negative, go up instead.
11153 Returns the difference between N and how many steps down that were
11154 taken."
11155   (interactive "p")
11156   (let ((up (< n 0))
11157         (n (abs n)))
11158     (while (and (> n 0)
11159                 (if up (gnus-summary-go-up-thread)
11160                   (gnus-summary-go-down-thread)))
11161       (setq n (1- n)))
11162     (gnus-summary-position-point)
11163     (when (/= 0 n)
11164       (gnus-message 7 "Can't go further"))
11165     n))
11166
11167 (defun gnus-summary-up-thread (n)
11168   "Go up thread N steps.
11169 If N is negative, go down instead.
11170 Returns the difference between N and how many steps down that were
11171 taken."
11172   (interactive "p")
11173   (gnus-summary-down-thread (- n)))
11174
11175 (defun gnus-summary-top-thread ()
11176   "Go to the top of the thread."
11177   (interactive)
11178   (while (gnus-summary-go-up-thread))
11179   (gnus-summary-article-number))
11180
11181 (defun gnus-summary-kill-thread (&optional unmark)
11182   "Mark articles under current thread as read.
11183 If the prefix argument is positive, remove any kinds of marks.
11184 If the prefix argument is negative, tick articles instead."
11185   (interactive "P")
11186   (when unmark
11187     (setq unmark (prefix-numeric-value unmark)))
11188   (let ((articles (gnus-summary-articles-in-thread)))
11189     (save-excursion
11190       ;; Expand the thread.
11191       (gnus-summary-show-thread)
11192       ;; Mark all the articles.
11193       (while articles
11194         (gnus-summary-goto-subject (car articles))
11195         (cond ((null unmark)
11196                (gnus-summary-mark-article-as-read gnus-killed-mark))
11197               ((> unmark 0)
11198                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11199               (t
11200                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11201         (setq articles (cdr articles))))
11202     ;; Hide killed subtrees.
11203     (and (null unmark)
11204          gnus-thread-hide-killed
11205          (gnus-summary-hide-thread))
11206     ;; If marked as read, go to next unread subject.
11207     (when (null unmark)
11208       ;; Go to next unread subject.
11209       (gnus-summary-next-subject 1 t)))
11210   (gnus-set-mode-line 'summary))
11211
11212 ;; Summary sorting commands
11213
11214 (defun gnus-summary-sort-by-number (&optional reverse)
11215   "Sort the summary buffer by article number.
11216 Argument REVERSE means reverse order."
11217   (interactive "P")
11218   (gnus-summary-sort 'number reverse))
11219
11220 (defun gnus-summary-sort-by-random (&optional reverse)
11221   "Randomize the order in the summary buffer.
11222 Argument REVERSE means to randomize in reverse order."
11223   (interactive "P")
11224   (gnus-summary-sort 'random reverse))
11225
11226 (defun gnus-summary-sort-by-author (&optional reverse)
11227   "Sort the summary buffer by author name alphabetically.
11228 If `case-fold-search' is non-nil, case of letters is ignored.
11229 Argument REVERSE means reverse order."
11230   (interactive "P")
11231   (gnus-summary-sort 'author reverse))
11232
11233 (defun gnus-summary-sort-by-recipient (&optional reverse)
11234   "Sort the summary buffer by recipient name alphabetically.
11235 If `case-fold-search' is non-nil, case of letters is ignored.
11236 Argument REVERSE means reverse order."
11237   (interactive "P")
11238   (gnus-summary-sort 'recipient reverse))
11239
11240 (defun gnus-summary-sort-by-subject (&optional reverse)
11241   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11242 If `case-fold-search' is non-nil, case of letters is ignored.
11243 Argument REVERSE means reverse order."
11244   (interactive "P")
11245   (gnus-summary-sort 'subject reverse))
11246
11247 (defun gnus-summary-sort-by-date (&optional reverse)
11248   "Sort the summary buffer by date.
11249 Argument REVERSE means reverse order."
11250   (interactive "P")
11251   (gnus-summary-sort 'date reverse))
11252
11253 (defun gnus-summary-sort-by-score (&optional reverse)
11254   "Sort the summary buffer by score.
11255 Argument REVERSE means reverse order."
11256   (interactive "P")
11257   (gnus-summary-sort 'score reverse))
11258
11259 (defun gnus-summary-sort-by-lines (&optional reverse)
11260   "Sort the summary buffer by the number of lines.
11261 Argument REVERSE means reverse order."
11262   (interactive "P")
11263   (gnus-summary-sort 'lines reverse))
11264
11265 (defun gnus-summary-sort-by-chars (&optional reverse)
11266   "Sort the summary buffer by article length.
11267 Argument REVERSE means reverse order."
11268   (interactive "P")
11269   (gnus-summary-sort 'chars reverse))
11270
11271 (defun gnus-summary-sort-by-original (&optional reverse)
11272   "Sort the summary buffer using the default sorting method.
11273 Argument REVERSE means reverse order."
11274   (interactive "P")
11275   (let* ((buffer-read-only)
11276          (gnus-summary-prepare-hook nil))
11277     ;; We do the sorting by regenerating the threads.
11278     (gnus-summary-prepare)
11279     ;; Hide subthreads if needed.
11280     (gnus-summary-maybe-hide-threads)))
11281
11282 (defun gnus-summary-sort (predicate reverse)
11283   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11284   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11285          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11286          (gnus-thread-sort-functions
11287           (if (not reverse)
11288               thread
11289             `(lambda (t1 t2)
11290                (,thread t2 t1))))
11291          (gnus-sort-gathered-threads-function
11292           gnus-thread-sort-functions)
11293          (gnus-article-sort-functions
11294           (if (not reverse)
11295               article
11296             `(lambda (t1 t2)
11297                (,article t2 t1))))
11298          (buffer-read-only)
11299          (gnus-summary-prepare-hook nil))
11300     ;; We do the sorting by regenerating the threads.
11301     (gnus-summary-prepare)
11302     ;; Hide subthreads if needed.
11303     (gnus-summary-maybe-hide-threads)))
11304
11305 ;; Summary saving commands.
11306
11307 (defun gnus-summary-save-article (&optional n not-saved)
11308   "Save the current article using the default saver function.
11309 If N is a positive number, save the N next articles.
11310 If N is a negative number, save the N previous articles.
11311 If N is nil and any articles have been marked with the process mark,
11312 save those articles instead.
11313 The variable `gnus-default-article-saver' specifies the saver function.
11314
11315 If the optional second argument NOT-SAVED is non-nil, articles saved
11316 will not be marked as saved."
11317   (interactive "P")
11318   (require 'gnus-art)
11319   (let* ((articles (gnus-summary-work-articles n))
11320          (save-buffer (save-excursion
11321                         (nnheader-set-temp-buffer " *Gnus Save*")))
11322          (num (length articles))
11323          ;; Whether to save decoded articles or raw articles.
11324          (decode (when gnus-article-save-coding-system
11325                    (get gnus-default-article-saver :decode)))
11326          ;; When saving many articles in a single file, use the other
11327          ;; function to save articles other than the first one.
11328          (saver2 (get gnus-default-article-saver :function))
11329          (gnus-prompt-before-saving (if saver2
11330                                         t
11331                                       gnus-prompt-before-saving))
11332          (gnus-default-article-saver gnus-default-article-saver)
11333          header file)
11334     (dolist (article articles)
11335       (setq header (gnus-summary-article-header article))
11336       (if (not (vectorp header))
11337           ;; This is a pseudo-article.
11338           (if (assq 'name header)
11339               (gnus-copy-file (cdr (assq 'name header)))
11340             (gnus-message 1 "Article %d is unsaveable" article))
11341         ;; This is a real article.
11342         (save-window-excursion
11343           (let ((gnus-display-mime-function (when decode
11344                                               gnus-display-mime-function))
11345                 (gnus-article-prepare-hook (when decode
11346                                              gnus-article-prepare-hook)))
11347             (gnus-summary-select-article t nil nil article)
11348             (gnus-summary-goto-subject article)))
11349         (save-excursion
11350           (set-buffer save-buffer)
11351           (erase-buffer)
11352           (insert-buffer-substring (if decode
11353                                        gnus-article-buffer
11354                                      gnus-original-article-buffer)))
11355         (setq file (gnus-article-save save-buffer file num))
11356         (gnus-summary-remove-process-mark article)
11357         (unless not-saved
11358           (gnus-summary-set-saved-mark article)))
11359       (when saver2
11360         (setq gnus-default-article-saver saver2
11361               saver2 nil)))
11362     (gnus-kill-buffer save-buffer)
11363     (gnus-summary-position-point)
11364     (gnus-set-mode-line 'summary)
11365     n))
11366
11367 (defun gnus-summary-pipe-output (&optional arg headers)
11368   "Pipe the current article to a subprocess.
11369 If N is a positive number, pipe the N next articles.
11370 If N is a negative number, pipe the N previous articles.
11371 If N is nil and any articles have been marked with the process mark,
11372 pipe those articles instead.
11373 If HEADERS (the symbolic prefix), include the headers, too."
11374   (interactive (gnus-interactive "P\ny"))
11375   (require 'gnus-art)
11376   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11377         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11378     (gnus-summary-save-article arg t))
11379   (let ((buffer (get-buffer "*Shell Command Output*")))
11380     (when (and buffer
11381                (not (zerop (buffer-size buffer))))
11382       (gnus-configure-windows 'pipe))))
11383
11384 (defun gnus-summary-save-article-mail (&optional arg)
11385   "Append the current article to a Unix mail box file.
11386 If N is a positive number, save the N next articles.
11387 If N is a negative number, save the N previous articles.
11388 If N is nil and any articles have been marked with the process mark,
11389 save those articles instead."
11390   (interactive "P")
11391   (require 'gnus-art)
11392   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11393     (gnus-summary-save-article arg)))
11394
11395 (defun gnus-summary-save-article-rmail (&optional arg)
11396   "Append the current article to an rmail file.
11397 If N is a positive number, save the N next articles.
11398 If N is a negative number, save the N previous articles.
11399 If N is nil and any articles have been marked with the process mark,
11400 save those articles instead."
11401   (interactive "P")
11402   (require 'gnus-art)
11403   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11404     (gnus-summary-save-article arg)))
11405
11406 (defun gnus-summary-save-article-file (&optional arg)
11407   "Append the current article to a file.
11408 If N is a positive number, save the N next articles.
11409 If N is a negative number, save the N previous articles.
11410 If N is nil and any articles have been marked with the process mark,
11411 save those articles instead."
11412   (interactive "P")
11413   (require 'gnus-art)
11414   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11415     (gnus-summary-save-article arg)))
11416
11417 (defun gnus-summary-write-article-file (&optional arg)
11418   "Write the current article to a file, deleting the previous file.
11419 If N is a positive number, save the N next articles.
11420 If N is a negative number, save the N previous articles.
11421 If N is nil and any articles have been marked with the process mark,
11422 save those articles instead."
11423   (interactive "P")
11424   (require 'gnus-art)
11425   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11426     (gnus-summary-save-article arg)))
11427
11428 (defun gnus-summary-save-article-body-file (&optional arg)
11429   "Append the current article body to a file.
11430 If N is a positive number, save the N next articles.
11431 If N is a negative number, save the N previous articles.
11432 If N is nil and any articles have been marked with the process mark,
11433 save those articles instead."
11434   (interactive "P")
11435   (require 'gnus-art)
11436   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11437     (gnus-summary-save-article arg)))
11438
11439 (defun gnus-summary-write-article-body-file (&optional arg)
11440   "Write the current article body to a file, deleting the previous file.
11441 If N is a positive number, save the N next articles.
11442 If N is a negative number, save the N previous articles.
11443 If N is nil and any articles have been marked with the process mark,
11444 save those articles instead."
11445   (interactive "P")
11446   (require 'gnus-art)
11447   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11448     (gnus-summary-save-article arg)))
11449
11450 (defun gnus-summary-muttprint (&optional arg)
11451   "Print the current article using Muttprint.
11452 If N is a positive number, save the N next articles.
11453 If N is a negative number, save the N previous articles.
11454 If N is nil and any articles have been marked with the process mark,
11455 save those articles instead."
11456   (interactive "P")
11457   (require 'gnus-art)
11458   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11459     (gnus-summary-save-article arg t)))
11460
11461 (defun gnus-summary-pipe-message (program)
11462   "Pipe the current article through PROGRAM."
11463   (interactive "sProgram: ")
11464   (gnus-summary-select-article)
11465   (let ((mail-header-separator ""))
11466     (gnus-eval-in-buffer-window gnus-article-buffer
11467       (save-restriction
11468         (widen)
11469         (let ((start (window-start))
11470               buffer-read-only)
11471           (message-pipe-buffer-body program)
11472           (set-window-start (get-buffer-window (current-buffer)) start))))))
11473
11474 (defun gnus-get-split-value (methods)
11475   "Return a value based on the split METHODS."
11476   (let (split-name method result match)
11477     (when methods
11478       (save-excursion
11479         (set-buffer gnus-original-article-buffer)
11480         (save-restriction
11481           (nnheader-narrow-to-headers)
11482           (while (and methods (not split-name))
11483             (goto-char (point-min))
11484             (setq method (pop methods))
11485             (setq match (car method))
11486             (when (cond
11487                    ((stringp match)
11488                     ;; Regular expression.
11489                     (ignore-errors
11490                       (re-search-forward match nil t)))
11491                    ((functionp match)
11492                     ;; Function.
11493                     (save-restriction
11494                       (widen)
11495                       (setq result (funcall match gnus-newsgroup-name))))
11496                    ((consp match)
11497                     ;; Form.
11498                     (save-restriction
11499                       (widen)
11500                       (setq result (eval match)))))
11501               (setq split-name (cdr method))
11502               (cond ((stringp result)
11503                      (push (expand-file-name
11504                             result gnus-article-save-directory)
11505                            split-name))
11506                     ((consp result)
11507                      (setq split-name (append result split-name)))))))))
11508     (nreverse split-name)))
11509
11510 (defun gnus-valid-move-group-p (group)
11511   (and (boundp group)
11512        (symbol-name group)
11513        (symbol-value group)
11514        (gnus-get-function (gnus-find-method-for-group
11515                            (symbol-name group)) 'request-accept-article t)))
11516
11517 (defun gnus-read-move-group-name (prompt default articles prefix)
11518   "Read a group name."
11519   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11520          (minibuffer-confirm-incomplete nil) ; XEmacs
11521          (prom
11522           (format "%s %s to"
11523                   prompt
11524                   (if (> (length articles) 1)
11525                       (format "these %d articles" (length articles))
11526                     "this article")))
11527          (to-newsgroup
11528           (cond
11529            ((null split-name)
11530             (gnus-completing-read-with-default
11531              default prom
11532              gnus-active-hashtb
11533              'gnus-valid-move-group-p
11534              nil prefix
11535              'gnus-group-history))
11536            ((= 1 (length split-name))
11537             (gnus-completing-read-with-default
11538              (car split-name) prom
11539              gnus-active-hashtb
11540              'gnus-valid-move-group-p
11541              nil nil
11542              'gnus-group-history))
11543            (t
11544             (gnus-completing-read-with-default
11545              nil prom
11546              (mapcar 'list (nreverse split-name))
11547              nil nil nil
11548              'gnus-group-history))))
11549          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11550     (when to-newsgroup
11551       (if (or (string= to-newsgroup "")
11552               (string= to-newsgroup prefix))
11553           (setq to-newsgroup default))
11554       (unless to-newsgroup
11555         (error "No group name entered"))
11556       (or (gnus-active to-newsgroup)
11557           (gnus-activate-group to-newsgroup nil nil to-method)
11558           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11559                                      to-newsgroup))
11560               (or (and (gnus-request-create-group to-newsgroup to-method)
11561                        (gnus-activate-group
11562                         to-newsgroup nil nil to-method)
11563                        (gnus-subscribe-group to-newsgroup))
11564                   (error "Couldn't create group %s" to-newsgroup)))
11565           (error "No such group: %s" to-newsgroup)))
11566     to-newsgroup))
11567
11568 (defvar gnus-summary-save-parts-counter)
11569
11570 (defun gnus-summary-save-parts (type dir n &optional reverse)
11571   "Save parts matching TYPE to DIR.
11572 If REVERSE, save parts that do not match TYPE."
11573   (interactive
11574    (list (read-string "Save parts of type: "
11575                       (or (car gnus-summary-save-parts-type-history)
11576                           gnus-summary-save-parts-default-mime)
11577                       'gnus-summary-save-parts-type-history)
11578          (setq gnus-summary-save-parts-last-directory
11579                (read-file-name "Save to directory: "
11580                                gnus-summary-save-parts-last-directory
11581                                nil t))
11582          current-prefix-arg))
11583   (gnus-summary-iterate n
11584     (let ((gnus-display-mime-function nil)
11585           (gnus-inhibit-treatment t))
11586       (gnus-summary-select-article))
11587     (save-excursion
11588       (set-buffer gnus-article-buffer)
11589       (let ((handles (or gnus-article-mime-handles
11590                          (mm-dissect-buffer nil gnus-article-loose-mime)
11591                          (and gnus-article-emulate-mime
11592                               (mm-uu-dissect))))
11593             (gnus-summary-save-parts-counter 1))
11594         (when handles
11595           (gnus-summary-save-parts-1 type dir handles reverse)
11596           (unless gnus-article-mime-handles ;; Don't destroy this case.
11597             (mm-destroy-parts handles)))))))
11598
11599 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11600   (if (stringp (car handle))
11601       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11602               (cdr handle))
11603     (when (if reverse
11604               (not (string-match type (mm-handle-media-type handle)))
11605             (string-match type (mm-handle-media-type handle)))
11606       (let ((file (expand-file-name
11607                    (gnus-map-function
11608                     mm-file-name-rewrite-functions
11609                     (file-name-nondirectory
11610                      (or
11611                       (mail-content-type-get
11612                        (mm-handle-disposition handle) 'filename)
11613                       (mail-content-type-get
11614                        (mm-handle-type handle) 'name)
11615                       (format "%s.%d.%d" gnus-newsgroup-name
11616                               (cdr gnus-article-current)
11617                               gnus-summary-save-parts-counter))))
11618                    dir)))
11619         (incf gnus-summary-save-parts-counter)
11620         (unless (file-exists-p file)
11621           (mm-save-part-to-file handle file))))))
11622
11623 ;; Summary extract commands
11624
11625 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11626   (let ((buffer-read-only nil)
11627         (article (gnus-summary-article-number))
11628         after-article b e)
11629     (unless (gnus-summary-goto-subject article)
11630       (error "No such article: %d" article))
11631     (gnus-summary-position-point)
11632     ;; If all commands are to be bunched up on one line, we collect
11633     ;; them here.
11634     (unless gnus-view-pseudos-separately
11635       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11636             files action)
11637         (while ps
11638           (setq action (cdr (assq 'action (car ps))))
11639           (setq files (list (cdr (assq 'name (car ps)))))
11640           (while (and ps (cdr ps)
11641                       (string= (or action "1")
11642                                (or (cdr (assq 'action (cadr ps))) "2")))
11643             (push (cdr (assq 'name (cadr ps))) files)
11644             (setcdr ps (cddr ps)))
11645           (when files
11646             (when (not (string-match "%s" action))
11647               (push " " files))
11648             (push " " files)
11649             (when (assq 'execute (car ps))
11650               (setcdr (assq 'execute (car ps))
11651                       (funcall (if (string-match "%s" action)
11652                                    'format 'concat)
11653                                action
11654                                (mapconcat
11655                                 (lambda (f)
11656                                   (if (equal f " ")
11657                                       f
11658                                     (shell-quote-argument f)))
11659                                 files " ")))))
11660           (setq ps (cdr ps)))))
11661     (if (and gnus-view-pseudos (not not-view))
11662         (while pslist
11663           (when (assq 'execute (car pslist))
11664             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11665                                   (eq gnus-view-pseudos 'not-confirm)))
11666           (setq pslist (cdr pslist)))
11667       (save-excursion
11668         (while pslist
11669           (setq after-article (or (cdr (assq 'article (car pslist)))
11670                                   (gnus-summary-article-number)))
11671           (gnus-summary-goto-subject after-article)
11672           (forward-line 1)
11673           (setq b (point))
11674           (insert "    " (file-name-nondirectory
11675                           (cdr (assq 'name (car pslist))))
11676                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11677           (setq e (point))
11678           (forward-line -1)             ; back to `b'
11679           (gnus-add-text-properties
11680            b (1- e) (list 'gnus-number gnus-reffed-article-number
11681                           gnus-mouse-face-prop gnus-mouse-face))
11682           (gnus-data-enter
11683            after-article gnus-reffed-article-number
11684            gnus-unread-mark b (car pslist) 0 (- e b))
11685           (setq gnus-newsgroup-unreads
11686                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11687                                          gnus-reffed-article-number))
11688           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11689           (setq pslist (cdr pslist)))))))
11690
11691 (defun gnus-pseudos< (p1 p2)
11692   (let ((c1 (cdr (assq 'action p1)))
11693         (c2 (cdr (assq 'action p2))))
11694     (and c1 c2 (string< c1 c2))))
11695
11696 (defun gnus-request-pseudo-article (props)
11697   (cond ((assq 'execute props)
11698          (gnus-execute-command (cdr (assq 'execute props)))))
11699   (let ((gnus-current-article (gnus-summary-article-number)))
11700     (gnus-run-hooks 'gnus-mark-article-hook)))
11701
11702 (defun gnus-execute-command (command &optional automatic)
11703   (save-excursion
11704     (gnus-article-setup-buffer)
11705     (set-buffer gnus-article-buffer)
11706     (setq buffer-read-only nil)
11707     (let ((command (if automatic command
11708                      (read-string "Command: " (cons command 0)))))
11709       (erase-buffer)
11710       (insert "$ " command "\n\n")
11711       (if gnus-view-pseudo-asynchronously
11712           (start-process "gnus-execute" (current-buffer) shell-file-name
11713                          shell-command-switch command)
11714         (call-process shell-file-name nil t nil
11715                       shell-command-switch command)))))
11716
11717 ;; Summary kill commands.
11718
11719 (defun gnus-summary-edit-global-kill (article)
11720   "Edit the \"global\" kill file."
11721   (interactive (list (gnus-summary-article-number)))
11722   (gnus-group-edit-global-kill article))
11723
11724 (defun gnus-summary-edit-local-kill ()
11725   "Edit a local kill file applied to the current newsgroup."
11726   (interactive)
11727   (setq gnus-current-headers (gnus-summary-article-header))
11728   (gnus-group-edit-local-kill
11729    (gnus-summary-article-number) gnus-newsgroup-name))
11730
11731 ;;; Header reading.
11732
11733 (defun gnus-read-header (id &optional header)
11734   "Read the headers of article ID and enter them into the Gnus system."
11735   (let ((group gnus-newsgroup-name)
11736         (gnus-override-method
11737          (or
11738           gnus-override-method
11739           (and (gnus-news-group-p gnus-newsgroup-name)
11740                (car (gnus-refer-article-methods)))))
11741         where)
11742     ;; First we check to see whether the header in question is already
11743     ;; fetched.
11744     (if (stringp id)
11745         ;; This is a Message-ID.
11746         (setq header (or header (gnus-id-to-header id)))
11747       ;; This is an article number.
11748       (setq header (or header (gnus-summary-article-header id))))
11749     (if (and header
11750              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11751         ;; We have found the header.
11752         header
11753       ;; We have to really fetch the header to this article.
11754       (save-excursion
11755         (set-buffer nntp-server-buffer)
11756         (when (setq where (gnus-request-head id group))
11757           (nnheader-fold-continuation-lines)
11758           (goto-char (point-max))
11759           (insert ".\n")
11760           (goto-char (point-min))
11761           (insert "211 ")
11762           (princ (cond
11763                   ((numberp id) id)
11764                   ((cdr where) (cdr where))
11765                   (header (mail-header-number header))
11766                   (t gnus-reffed-article-number))
11767                  (current-buffer))
11768           (insert " Article retrieved.\n"))
11769         (if (or (not where)
11770                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11771             ()                          ; Malformed head.
11772           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11773             (when (and (stringp id)
11774                        (or
11775                         (not (string= (gnus-group-real-name group)
11776                                       (car where)))
11777                         (not (gnus-server-equal gnus-override-method
11778                                                 (gnus-group-method group)))))
11779               ;; If we fetched by Message-ID and the article came from
11780               ;; a different group (or server), we fudge some bogus
11781               ;; article numbers for this article.
11782               (mail-header-set-number header gnus-reffed-article-number))
11783             (save-excursion
11784               (set-buffer gnus-summary-buffer)
11785               (decf gnus-reffed-article-number)
11786               (gnus-remove-header (mail-header-number header))
11787               (push header gnus-newsgroup-headers)
11788               (setq gnus-current-headers header)
11789               (push (mail-header-number header) gnus-newsgroup-limit)))
11790           header)))))
11791
11792 (defun gnus-remove-header (number)
11793   "Remove header NUMBER from `gnus-newsgroup-headers'."
11794   (if (and gnus-newsgroup-headers
11795            (= number (mail-header-number (car gnus-newsgroup-headers))))
11796       (pop gnus-newsgroup-headers)
11797     (let ((headers gnus-newsgroup-headers))
11798       (while (and (cdr headers)
11799                   (not (= number (mail-header-number (cadr headers)))))
11800         (pop headers))
11801       (when (cdr headers)
11802         (setcdr headers (cddr headers))))))
11803
11804 ;;;
11805 ;;; summary highlights
11806 ;;;
11807
11808 (defun gnus-highlight-selected-summary ()
11809   "Highlight selected article in summary buffer."
11810   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11811   (when gnus-summary-selected-face
11812     (save-excursion
11813       (let* ((beg (point-at-bol))
11814              (end (point-at-eol))
11815              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11816              (from (if (get-text-property beg gnus-mouse-face-prop)
11817                        beg
11818                      (or (next-single-property-change
11819                           beg gnus-mouse-face-prop nil end)
11820                          beg)))
11821              (to
11822               (if (= from end)
11823                   (- from 2)
11824                 (or (next-single-property-change
11825                      from gnus-mouse-face-prop nil end)
11826                     end))))
11827         ;; If no mouse-face prop on line we will have to = from = end,
11828         ;; so we highlight the entire line instead.
11829         (when (= (+ to 2) from)
11830           (setq from beg)
11831           (setq to end))
11832         (if gnus-newsgroup-selected-overlay
11833             ;; Move old overlay.
11834             (gnus-move-overlay
11835              gnus-newsgroup-selected-overlay from to (current-buffer))
11836           ;; Create new overlay.
11837           (gnus-overlay-put
11838            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11839            'face gnus-summary-selected-face))))))
11840
11841 (defvar gnus-summary-highlight-line-cached nil)
11842 (defvar gnus-summary-highlight-line-trigger nil)
11843
11844 (defun gnus-summary-highlight-line-0 ()
11845   (if (and (eq gnus-summary-highlight-line-trigger
11846                gnus-summary-highlight)
11847            gnus-summary-highlight-line-cached)
11848       gnus-summary-highlight-line-cached
11849     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11850           gnus-summary-highlight-line-cached
11851           (let* ((cond (list 'cond))
11852                  (c cond)
11853                  (list gnus-summary-highlight))
11854             (while list
11855               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11856                               nil))
11857               (setq c (cdr c)
11858                     list (cdr list)))
11859             (gnus-byte-compile (list 'lambda nil cond))))))
11860
11861 (defun gnus-summary-highlight-line ()
11862   "Highlight current line according to `gnus-summary-highlight'."
11863   (let* ((beg (point-at-bol))
11864          (article (or (gnus-summary-article-number) gnus-current-article))
11865          (score (or (cdr (assq article
11866                                gnus-newsgroup-scored))
11867                     gnus-summary-default-score 0))
11868          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11869          (inhibit-read-only t)
11870          (default gnus-summary-default-score)
11871          (default-high gnus-summary-default-high-score)
11872          (default-low gnus-summary-default-low-score)
11873          (uncached (and gnus-summary-use-undownloaded-faces
11874                         (memq article gnus-newsgroup-undownloaded)
11875                         (not (memq article gnus-newsgroup-cached)))))
11876     (let ((face (funcall (gnus-summary-highlight-line-0))))
11877       (unless (eq face (get-text-property beg 'face))
11878         (gnus-put-text-property-excluding-characters-with-faces
11879          beg (point-at-eol) 'face
11880          (setq face (if (boundp face) (symbol-value face) face)))
11881         (when gnus-summary-highlight-line-function
11882           (funcall gnus-summary-highlight-line-function article face))))))
11883
11884 (defun gnus-update-read-articles (group unread &optional compute)
11885   "Update the list of read articles in GROUP.
11886 UNREAD is a sorted list."
11887   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11888         (info (gnus-get-info group))
11889         (prev 1)
11890         read)
11891     (if (or (not info) (not active))
11892         ;; There is no info on this group if it was, in fact,
11893         ;; killed.  Gnus stores no information on killed groups, so
11894         ;; there's nothing to be done.
11895         ;; One could store the information somewhere temporarily,
11896         ;; perhaps...  Hmmm...
11897         ()
11898       ;; Remove any negative articles numbers.
11899       (while (and unread (< (car unread) 0))
11900         (setq unread (cdr unread)))
11901       ;; Remove any expired article numbers
11902       (while (and unread (< (car unread) (car active)))
11903         (setq unread (cdr unread)))
11904       ;; Compute the ranges of read articles by looking at the list of
11905       ;; unread articles.
11906       (while unread
11907         (when (/= (car unread) prev)
11908           (push (if (= prev (1- (car unread))) prev
11909                   (cons prev (1- (car unread))))
11910                 read))
11911         (setq prev (1+ (car unread)))
11912         (setq unread (cdr unread)))
11913       (when (<= prev (cdr active))
11914         (push (cons prev (cdr active)) read))
11915       (setq read (if (> (length read) 1) (nreverse read) read))
11916       (if compute
11917           read
11918         (save-excursion
11919           (let (setmarkundo)
11920             ;; Propagate the read marks to the backend.
11921             (when (gnus-check-backend-function 'request-set-mark group)
11922               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11923                     (add (gnus-remove-from-range read (gnus-info-read info))))
11924                 (when (or add del)
11925                   (unless (gnus-check-group group)
11926                     (error "Can't open server for %s" group))
11927                   (gnus-request-set-mark
11928                    group (delq nil (list (if add (list add 'add '(read)))
11929                                          (if del (list del 'del '(read))))))
11930                   (setq setmarkundo
11931                         `(gnus-request-set-mark
11932                           ,group
11933                           ',(delq nil (list
11934                                        (if del (list del 'add '(read)))
11935                                        (if add (list add 'del '(read))))))))))
11936             (set-buffer gnus-group-buffer)
11937             (gnus-undo-register
11938               `(progn
11939                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11940                  (gnus-info-set-read ',info ',(gnus-info-read info))
11941                  (gnus-get-unread-articles-in-group ',info
11942                                                     (gnus-active ,group))
11943                  (gnus-group-update-group ,group t)
11944                  ,setmarkundo))))
11945         ;; Enter this list into the group info.
11946         (gnus-info-set-read info read)
11947         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11948         (gnus-get-unread-articles-in-group info (gnus-active group))
11949         t))))
11950
11951 (defun gnus-offer-save-summaries ()
11952   "Offer to save all active summary buffers."
11953   (let (buffers)
11954     ;; Go through all buffers and find all summaries.
11955     (dolist (buffer (buffer-list))
11956       (when (and (setq buffer (buffer-name buffer))
11957                  (string-match "Summary" buffer)
11958                  (with-current-buffer buffer
11959                    ;; We check that this is, indeed, a summary buffer.
11960                    (and (eq major-mode 'gnus-summary-mode)
11961                         ;; Also make sure this isn't bogus.
11962                         gnus-newsgroup-prepared
11963                         ;; Also make sure that this isn't a
11964                         ;; dead summary buffer.
11965                         (not gnus-dead-summary-mode))))
11966         (push buffer buffers)))
11967     ;; Go through all these summary buffers and offer to save them.
11968     (when buffers
11969       (save-excursion
11970         (map-y-or-n-p
11971          "Update summary buffer %s? "
11972          (lambda (buf)
11973            (switch-to-buffer buf)
11974            (gnus-summary-exit))
11975          buffers)))))
11976
11977 (defun gnus-summary-setup-default-charset ()
11978   "Setup newsgroup default charset."
11979   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11980       (setq gnus-newsgroup-charset nil)
11981     (let* ((ignored-charsets
11982             (or gnus-newsgroup-ephemeral-ignored-charsets
11983                 (append
11984                  (and gnus-newsgroup-name
11985                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11986                  gnus-newsgroup-ignored-charsets))))
11987       (setq gnus-newsgroup-charset
11988             (or gnus-newsgroup-ephemeral-charset
11989                 (and gnus-newsgroup-name
11990                      (gnus-parameter-charset gnus-newsgroup-name))
11991                 gnus-default-charset))
11992       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11993            ignored-charsets))))
11994
11995 ;;;
11996 ;;; Mime Commands
11997 ;;;
11998
11999 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12000   "Display the current article buffer fully MIME-buttonized.
12001 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12002 treated as multipart/mixed."
12003   (interactive "P")
12004   (require 'gnus-art)
12005   (let ((gnus-unbuttonized-mime-types nil)
12006         (gnus-mime-display-multipart-as-mixed show-all-parts))
12007     (gnus-summary-show-article)))
12008
12009 (defun gnus-summary-repair-multipart (article)
12010   "Add a Content-Type header to a multipart article without one."
12011   (interactive (list (gnus-summary-article-number)))
12012   (gnus-with-article article
12013     (message-narrow-to-head)
12014     (message-remove-header "Mime-Version")
12015     (goto-char (point-max))
12016     (insert "Mime-Version: 1.0\n")
12017     (widen)
12018     (when (search-forward "\n--" nil t)
12019       (let ((separator (buffer-substring (point) (point-at-eol))))
12020         (message-narrow-to-head)
12021         (message-remove-header "Content-Type")
12022         (goto-char (point-max))
12023         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12024                         separator))
12025         (widen))))
12026   (let (gnus-mark-article-hook)
12027     (gnus-summary-select-article t t nil article)))
12028
12029 (defun gnus-summary-toggle-display-buttonized ()
12030   "Toggle the buttonizing of the article buffer."
12031   (interactive)
12032   (require 'gnus-art)
12033   (if (setq gnus-inhibit-mime-unbuttonizing
12034             (not gnus-inhibit-mime-unbuttonizing))
12035       (let ((gnus-unbuttonized-mime-types nil))
12036         (gnus-summary-show-article))
12037     (gnus-summary-show-article)))
12038
12039 ;;;
12040 ;;; Generic summary marking commands
12041 ;;;
12042
12043 (defvar gnus-summary-marking-alist
12044   '((read gnus-del-mark "d")
12045     (unread gnus-unread-mark "u")
12046     (ticked gnus-ticked-mark "!")
12047     (dormant gnus-dormant-mark "?")
12048     (expirable gnus-expirable-mark "e"))
12049   "An alist of names/marks/keystrokes.")
12050
12051 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12052 (defvar gnus-summary-mark-map)
12053
12054 (defun gnus-summary-make-all-marking-commands ()
12055   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12056   (dolist (elem gnus-summary-marking-alist)
12057     (apply 'gnus-summary-make-marking-command elem)))
12058
12059 (defun gnus-summary-make-marking-command (name mark keystroke)
12060   (let ((map (make-sparse-keymap)))
12061     (define-key gnus-summary-generic-mark-map keystroke map)
12062     (dolist (lway `((next "next" next nil "n")
12063                     (next-unread "next unread" next t "N")
12064                     (prev "previous" prev nil "p")
12065                     (prev-unread "previous unread" prev t "P")
12066                     (nomove "" nil nil ,keystroke)))
12067       (let ((func (gnus-summary-make-marking-command-1
12068                    mark (car lway) lway name)))
12069         (setq func (eval func))
12070         (define-key map (nth 4 lway) func)))))
12071
12072 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12073   `(defun ,(intern
12074             (format "gnus-summary-put-mark-as-%s%s"
12075                     name (if (eq way 'nomove)
12076                              ""
12077                            (concat "-" (symbol-name way)))))
12078      (n)
12079      ,(format
12080        "Mark the current article as %s%s.
12081 If N, the prefix, then repeat N times.
12082 If N is negative, move in reverse order.
12083 The difference between N and the actual number of articles marked is
12084 returned."
12085        name (cadr lway))
12086      (interactive "p")
12087      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12088
12089 (defun gnus-summary-generic-mark (n mark move unread)
12090   "Mark N articles with MARK."
12091   (unless (eq major-mode 'gnus-summary-mode)
12092     (error "This command can only be used in the summary buffer"))
12093   (gnus-summary-show-thread)
12094   (let ((nummove
12095          (cond
12096           ((eq move 'next) 1)
12097           ((eq move 'prev) -1)
12098           (t 0))))
12099     (if (zerop nummove)
12100         (setq n 1)
12101       (when (< n 0)
12102         (setq n (abs n)
12103               nummove (* -1 nummove))))
12104     (while (and (> n 0)
12105                 (gnus-summary-mark-article nil mark)
12106                 (zerop (gnus-summary-next-subject nummove unread t)))
12107       (setq n (1- n)))
12108     (when (/= 0 n)
12109       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12110     (gnus-summary-recenter)
12111     (gnus-summary-position-point)
12112     (gnus-set-mode-line 'summary)
12113     n))
12114
12115 (defun gnus-summary-insert-articles (articles)
12116   (when (setq articles
12117               (gnus-sorted-difference articles
12118                                       (mapcar (lambda (h)
12119                                                 (mail-header-number h))
12120                                               gnus-newsgroup-headers)))
12121     (setq gnus-newsgroup-headers
12122           (gnus-merge 'list
12123                       gnus-newsgroup-headers
12124                       (gnus-fetch-headers articles)
12125                       'gnus-article-sort-by-number))
12126     ;; Suppress duplicates?
12127     (when gnus-suppress-duplicates
12128       (gnus-dup-suppress-articles))
12129
12130     ;; We might want to build some more threads first.
12131     (when (and gnus-fetch-old-headers
12132                (eq gnus-headers-retrieved-by 'nov))
12133       (if (eq gnus-fetch-old-headers 'invisible)
12134           (gnus-build-all-threads)
12135         (gnus-build-old-threads)))
12136     ;; Let the Gnus agent mark articles as read.
12137     (when gnus-agent
12138       (gnus-agent-get-undownloaded-list))
12139     ;; Remove list identifiers from subject
12140     (when gnus-list-identifiers
12141       (gnus-summary-remove-list-identifiers))
12142     ;; First and last article in this newsgroup.
12143     (when gnus-newsgroup-headers
12144       (setq gnus-newsgroup-begin
12145             (mail-header-number (car gnus-newsgroup-headers))
12146             gnus-newsgroup-end
12147             (mail-header-number
12148              (gnus-last-element gnus-newsgroup-headers))))
12149     (when gnus-use-scoring
12150       (gnus-possibly-score-headers))))
12151
12152 (defun gnus-summary-insert-old-articles (&optional all)
12153   "Insert all old articles in this group.
12154 If ALL is non-nil, already read articles become readable.
12155 If ALL is a number, fetch this number of articles."
12156   (interactive "P")
12157   (prog1
12158       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12159             older len)
12160         (setq older
12161               ;; Some nntp servers lie about their active range.  When
12162               ;; this happens, the active range can be in the millions.
12163               ;; Use a compressed range to avoid creating a huge list.
12164               (gnus-range-difference (list gnus-newsgroup-active) old))
12165         (setq len (gnus-range-length older))
12166         (cond
12167          ((null older) nil)
12168          ((numberp all)
12169           (if (< all len)
12170               (let ((older-range (nreverse older)))
12171                 (setq older nil)
12172
12173                 (while (> all 0)
12174                   (let* ((r (pop older-range))
12175                          (min (if (numberp r) r (car r)))
12176                          (max (if (numberp r) r (cdr r))))
12177                     (while (and (<= min max)
12178                                 (> all 0))
12179                       (push max older)
12180                       (setq all (1- all)
12181                             max (1- max))))))
12182             (setq older (gnus-uncompress-range older))))
12183          (all
12184           (setq older (gnus-uncompress-range older)))
12185          (t
12186           (when (and (numberp gnus-large-newsgroup)
12187                    (> len gnus-large-newsgroup))
12188               (let* ((cursor-in-echo-area nil)
12189                      (initial (gnus-parameter-large-newsgroup-initial
12190                                gnus-newsgroup-name))
12191                      (input
12192                       (read-string
12193                        (format
12194                         "How many articles from %s (%s %d): "
12195                         (gnus-limit-string
12196                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12197                         (if initial "max" "default")
12198                         len)
12199                        (if initial
12200                            (cons (number-to-string initial)
12201                                  0)))))
12202                 (unless (string-match "^[ \t]*$" input)
12203                   (setq all (string-to-number input))
12204                   (if (< all len)
12205                       (let ((older-range (nreverse older)))
12206                         (setq older nil)
12207
12208                         (while (> all 0)
12209                           (let* ((r (pop older-range))
12210                                  (min (if (numberp r) r (car r)))
12211                                  (max (if (numberp r) r (cdr r))))
12212                             (while (and (<= min max)
12213                                         (> all 0))
12214                               (push max older)
12215                               (setq all (1- all)
12216                                     max (1- max))))))))))
12217           (setq older (gnus-uncompress-range older))))
12218         (if (not older)
12219             (message "No old news.")
12220           (gnus-summary-insert-articles older)
12221           (gnus-summary-limit (gnus-sorted-nunion old older))))
12222     (gnus-summary-position-point)))
12223
12224 (defun gnus-summary-insert-new-articles ()
12225   "Insert all new articles in this group."
12226   (interactive)
12227   (prog1
12228       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12229             (old-active gnus-newsgroup-active)
12230             (nnmail-fetched-sources (list t))
12231             i new)
12232         (setq gnus-newsgroup-active
12233               (gnus-activate-group gnus-newsgroup-name 'scan))
12234         (setq i (cdr gnus-newsgroup-active))
12235         (while (> i (cdr old-active))
12236           (push i new)
12237           (decf i))
12238         (if (not new)
12239             (message "No gnus is bad news")
12240           (gnus-summary-insert-articles new)
12241           (setq gnus-newsgroup-unreads
12242                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12243           (gnus-summary-limit (gnus-sorted-nunion old new))))
12244     (gnus-summary-position-point)))
12245
12246 (gnus-summary-make-all-marking-commands)
12247
12248 (gnus-ems-redefine)
12249
12250 (provide 'gnus-sum)
12251
12252 (run-hooks 'gnus-sum-load-hook)
12253
12254 ;; Local Variables:
12255 ;; coding: iso-8859-1
12256 ;; End:
12257
12258 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12259 ;;; gnus-sum.el ends here