b00c1d692dbb50ad72f53651aa9bead2a5735615
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
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 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 If threads are hidden, you have to run the command
252 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
253 to expose hidden threads."
254   :group 'gnus-thread
255   :type 'boolean)
256
257 (defcustom gnus-thread-hide-killed t
258   "*If non-nil, hide killed threads automatically."
259   :group 'gnus-thread
260   :type 'boolean)
261
262 (defcustom gnus-thread-ignore-subject t
263   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
264 If nil, articles that have different subjects from their parents will
265 start separate threads."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-operation-ignore-subject t
270   "*If non-nil, subjects will be ignored when doing thread commands.
271 This affects commands like `gnus-summary-kill-thread' and
272 `gnus-summary-lower-thread'.
273
274 If this variable is nil, articles in the same thread with different
275 subjects will not be included in the operation in question.  If this
276 variable is `fuzzy', only articles that have subjects that are fuzzily
277 equal will be included."
278   :group 'gnus-thread
279   :type '(choice (const :tag "off" nil)
280                  (const fuzzy)
281                  (sexp :tag "on" t)))
282
283 (defcustom gnus-thread-indent-level 4
284   "*Number that says how much each sub-thread should be indented."
285   :group 'gnus-thread
286   :type 'integer)
287
288 (defcustom gnus-auto-extend-newsgroup t
289   "*If non-nil, extend newsgroup forward and backward when requested."
290   :group 'gnus-summary-choose
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-first t
294   "*If nil, don't select the first unread article when entering a group.
295 If this variable is `best', select the highest-scored unread article
296 in the group.  If t, select the first unread article.
297
298 This variable can also be a function to place point on a likely
299 subject line.  Useful values include `gnus-summary-first-unread-subject',
300 `gnus-summary-first-unread-article' and
301 `gnus-summary-best-unread-article'.
302
303 If you want to prevent automatic selection of the first unread article
304 in some newsgroups, set the variable to nil in
305 `gnus-select-group-hook'."
306   :group 'gnus-group-select
307   :type '(choice (const :tag "none" nil)
308                  (const best)
309                  (sexp :menu-tag "first" t)
310                  (function-item gnus-summary-first-unread-subject)
311                  (function-item gnus-summary-first-unread-article)
312                  (function-item gnus-summary-best-unread-article)))
313
314 (defcustom gnus-dont-select-after-jump-to-other-group nil
315   "If non-nil, don't select the first unread article after entering the
316 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
317 it is depend on the value of `gnus-auto-select-first' whether to select
318 or not."
319   :group 'gnus-group-select
320   :type 'boolean)
321
322 (defcustom gnus-auto-select-next t
323   "*If non-nil, offer to go to the next group from the end of the previous.
324 If the value is t and the next newsgroup is empty, Gnus will exit
325 summary mode and go back to group mode.  If the value is neither nil
326 nor t, Gnus will select the following unread newsgroup.  In
327 particular, if the value is the symbol `quietly', the next unread
328 newsgroup will be selected without any confirmation, and if it is
329 `almost-quietly', the next group will be selected without any
330 confirmation if you are located on the last article in the group.
331 Finally, if this variable is `slightly-quietly', the `Z n' command
332 will go to the next group without confirmation."
333   :group 'gnus-summary-maneuvering
334   :type '(choice (const :tag "off" nil)
335                  (const quietly)
336                  (const almost-quietly)
337                  (const slightly-quietly)
338                  (sexp :menu-tag "on" t)))
339
340 (defcustom gnus-auto-select-same nil
341   "*If non-nil, select the next article with the same subject.
342 If there are no more articles with the same subject, go to
343 the first unread article."
344   :group 'gnus-summary-maneuvering
345   :type 'boolean)
346
347 (defcustom gnus-summary-check-current nil
348   "*If non-nil, consider the current article when moving.
349 The \"unread\" movement commands will stay on the same line if the
350 current article is unread."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-center-summary t
355   "*If non-nil, always center the current summary buffer.
356 In particular, if `vertical' do only vertical recentering.  If non-nil
357 and non-`vertical', do both horizontal and vertical recentering."
358   :group 'gnus-summary-maneuvering
359   :type '(choice (const :tag "none" nil)
360                  (const vertical)
361                  (integer :tag "height")
362                  (sexp :menu-tag "both" t)))
363
364 (defcustom gnus-show-all-headers nil
365   "*If non-nil, don't hide any headers."
366   :group 'gnus-article-hiding
367   :group 'gnus-article-headers
368   :type 'boolean)
369
370 (defcustom gnus-summary-ignore-duplicates nil
371   "*If non-nil, ignore articles with identical Message-ID headers."
372   :group 'gnus-summary
373   :type 'boolean)
374
375 (defcustom gnus-single-article-buffer t
376   "*If non-nil, display all articles in the same buffer.
377 If nil, each group will get its own article buffer."
378   :group 'gnus-article-various
379   :type 'boolean)
380
381 (defcustom gnus-break-pages t
382   "*If non-nil, do page breaking on articles.
383 The page delimiter is specified by the `gnus-page-delimiter'
384 variable."
385   :group 'gnus-article-various
386   :type 'boolean)
387
388 (defcustom gnus-show-mime t
389   "*If non-nil, do mime processing of articles.
390 The articles will simply be fed to the function given by
391 `gnus-article-display-method-for-mime'."
392   :group 'gnus-article-mime
393   :type 'boolean)
394
395 (defcustom gnus-move-split-methods nil
396   "*Variable used to suggest where articles are to be moved to.
397 It uses the same syntax as the `gnus-split-methods' variable.
398 However, whereas `gnus-split-methods' specifies file names as targets,
399 this variable specifies group names."
400   :group 'gnus-summary-mail
401   :type '(repeat (choice (list :value (fun) function)
402                          (cons :value ("" "") regexp (repeat string))
403                          (sexp :value nil))))
404
405 (defcustom gnus-unread-mark ?\ ;;;Whitespace
406   "*Mark used for unread articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-ticked-mark ?!
411   "*Mark used for ticked articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-dormant-mark ??
416   "*Mark used for dormant articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-del-mark ?r
421   "*Mark used for del'd articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-read-mark ?R
426   "*Mark used for read articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-expirable-mark ?E
431   "*Mark used for expirable articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-killed-mark ?K
436   "*Mark used for killed articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-souped-mark ?F
441   "*Mark used for souped articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-kill-file-mark ?X
446   "*Mark used for articles killed by kill files."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-low-score-mark ?Y
451   "*Mark used for articles with a low score."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-catchup-mark ?C
456   "*Mark used for articles that are caught up."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-replied-mark ?A
461   "*Mark used for articles that have been replied to."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-forwarded-mark ?F
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-recent-mark ?N
471   "*Mark used for articles that are recent."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-cached-mark ?*
476   "*Mark used for articles that are in the cache."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-saved-mark ?S
481   "*Mark used for articles that have been saved."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unseen-mark ?.
486   "*Mark used for articles that haven't been seen."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-no-mark ?\ ;;;Whitespace
491   "*Mark used for articles that have no other secondary mark."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ancient-mark ?O
496   "*Mark used for ancient articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-sparse-mark ?Q
501   "*Mark used for sparsely reffed articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-canceled-mark ?G
506   "*Mark used for canceled articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-duplicate-mark ?M
511   "*Mark used for duplicate articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-undownloaded-mark ?@
516   "*Mark used for articles that weren't downloaded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-downloadable-mark ?%
521   "*Mark used for articles that are to be downloaded."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-unsendable-mark ?=
526   "*Mark used for articles that won't be sent."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-score-over-mark ?+
531   "*Score mark used for articles with high scores."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-score-below-mark ?-
536   "*Score mark used for articles with low scores."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
541   "*There is no thread under the article."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-not-empty-thread-mark ?=
546   "*There is a thread under the article."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-view-pseudo-asynchronously nil
551   "*If non-nil, Gnus will view pseudo-articles asynchronously."
552   :group 'gnus-extract-view
553   :type 'boolean)
554
555 (defcustom gnus-auto-expirable-marks
556   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
557         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
558         gnus-souped-mark gnus-duplicate-mark)
559   "*The list of marks converted into expiration if a group is auto-expirable."
560   :version "21.1"
561   :group 'gnus-summary
562   :type '(repeat character))
563
564 (defcustom gnus-inhibit-user-auto-expire t
565   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
566   :version "21.1"
567   :group 'gnus-summary
568   :type 'boolean)
569
570 (defcustom gnus-view-pseudos nil
571   "*If `automatic', pseudo-articles will be viewed automatically.
572 If `not-confirm', pseudos will be viewed automatically, and the user
573 will not be asked to confirm the command."
574   :group 'gnus-extract-view
575   :type '(choice (const :tag "off" nil)
576                  (const automatic)
577                  (const not-confirm)))
578
579 (defcustom gnus-view-pseudos-separately t
580   "*If non-nil, one pseudo-article will be created for each file to be viewed.
581 If nil, all files that use the same viewing command will be given as a
582 list of parameters to that command."
583   :group 'gnus-extract-view
584   :type 'boolean)
585
586 (defcustom gnus-insert-pseudo-articles t
587   "*If non-nil, insert pseudo-articles when decoding articles."
588   :group 'gnus-extract-view
589   :type 'boolean)
590
591 (defcustom gnus-summary-dummy-line-format
592   "  %(:                          :%) %S\n"
593   "*The format specification for the dummy roots in the summary buffer.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions.
596
597 %S  The subject"
598   :group 'gnus-threading
599   :type 'string)
600
601 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
602   "*The format specification for the summary mode line.
603 It works along the same lines as a normal formatting string,
604 with some simple extensions:
605
606 %G  Group name
607 %p  Unprefixed group name
608 %A  Current article number
609 %z  Current article score
610 %V  Gnus version
611 %U  Number of unread articles in the group
612 %e  Number of unselected articles in the group
613 %Z  A string with unread/unselected article counts
614 %g  Shortish group name
615 %S  Subject of the current article
616 %u  User-defined spec
617 %s  Current score file name
618 %d  Number of dormant articles
619 %r  Number of articles that have been marked as read in this session
620 %E  Number of articles expunged by the score files"
621   :group 'gnus-summary-format
622   :type 'string)
623
624 (defcustom gnus-list-identifiers nil
625   "Regexp that matches list identifiers to be removed from subject.
626 This can also be a list of regexps."
627   :version "21.1"
628   :group 'gnus-summary-format
629   :group 'gnus-article-hiding
630   :type '(choice (const :tag "none" nil)
631                  (regexp :value ".*")
632                  (repeat :value (".*") regexp)))
633
634 (defcustom gnus-summary-mark-below 0
635   "*Mark all articles with a score below this variable as read.
636 This variable is local to each summary buffer and usually set by the
637 score file."
638   :group 'gnus-score-default
639   :type 'integer)
640
641 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
642   "*List of functions used for sorting articles in the summary buffer.
643
644 Each function takes two articles and returns non-nil if the first
645 article should be sorted before the other.  If you use more than one
646 function, the primary sort function should be the last.  You should
647 probably always include `gnus-article-sort-by-number' in the list of
648 sorting functions -- preferably first.  Also note that sorting by date
649 is often much slower than sorting by number, and the sorting order is
650 very similar.  (Sorting by date means sorting by the time the message
651 was sent, sorting by number means sorting by arrival time.)
652
653 Ready-made functions include `gnus-article-sort-by-number',
654 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
655 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
656
657 When threading is turned on, the variable `gnus-thread-sort-functions'
658 controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-article-sort-by-number)
661                          (function-item gnus-article-sort-by-author)
662                          (function-item gnus-article-sort-by-subject)
663                          (function-item gnus-article-sort-by-date)
664                          (function-item gnus-article-sort-by-score)
665                          (function :tag "other"))))
666
667 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
668   "*List of functions used for sorting threads in the summary buffer.
669 By default, threads are sorted by article number.
670
671 Each function takes two threads and returns non-nil if the first
672 thread should be sorted before the other.  If you use more than one
673 function, the primary sort function should be the last.  You should
674 probably always include `gnus-thread-sort-by-number' in the list of
675 sorting functions -- preferably first.  Also note that sorting by date
676 is often much slower than sorting by number, and the sorting order is
677 very similar.  (Sorting by date means sorting by the time the message
678 was sent, sorting by number means sorting by arrival time.)
679
680 Ready-made functions include `gnus-thread-sort-by-number',
681 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
682 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
683 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
684
685 When threading is turned off, the variable
686 `gnus-article-sort-functions' controls how articles are sorted."
687   :group 'gnus-summary-sort
688   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
689                          (function-item gnus-thread-sort-by-author)
690                          (function-item gnus-thread-sort-by-subject)
691                          (function-item gnus-thread-sort-by-date)
692                          (function-item gnus-thread-sort-by-score)
693                          (function-item gnus-thread-sort-by-total-score)
694                          (function :tag "other"))))
695
696 (defcustom gnus-thread-score-function '+
697   "*Function used for calculating the total score of a thread.
698
699 The function is called with the scores of the article and each
700 subthread and should then return the score of the thread.
701
702 Some functions you can use are `+', `max', or `min'."
703   :group 'gnus-summary-sort
704   :type 'function)
705
706 (defcustom gnus-summary-expunge-below nil
707   "All articles that have a score less than this variable will be expunged.
708 This variable is local to the summary buffers."
709   :group 'gnus-score-default
710   :type '(choice (const :tag "off" nil)
711                  integer))
712
713 (defcustom gnus-thread-expunge-below nil
714   "All threads that have a total score less than this variable will be expunged.
715 See `gnus-thread-score-function' for en explanation of what a
716 \"thread score\" is.
717
718 This variable is local to the summary buffers."
719   :group 'gnus-threading
720   :group 'gnus-score-default
721   :type '(choice (const :tag "off" nil)
722                  integer))
723
724 (defcustom gnus-summary-mode-hook nil
725   "*A hook for Gnus summary mode.
726 This hook is run before any variables are set in the summary buffer."
727   :options '(turn-on-gnus-mailing-list-mode)
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
732 (when (featurep 'xemacs)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
734   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
735   (add-hook 'gnus-summary-mode-hook
736             'gnus-xmas-switch-horizontal-scrollbar-off))
737
738 (defcustom gnus-summary-menu-hook nil
739   "*Hook run after the creation of the summary mode menu."
740   :group 'gnus-summary-visual
741   :type 'hook)
742
743 (defcustom gnus-summary-exit-hook nil
744   "*A hook called on exit from the summary buffer.
745 It will be called with point in the group buffer."
746   :group 'gnus-summary-exit
747   :type 'hook)
748
749 (defcustom gnus-summary-prepare-hook nil
750   "*A hook called after the summary buffer has been generated.
751 If you want to modify the summary buffer, you can use this hook."
752   :group 'gnus-summary-various
753   :type 'hook)
754
755 (defcustom gnus-summary-prepared-hook nil
756   "*A hook called as the last thing after the summary buffer has been generated."
757   :group 'gnus-summary-various
758   :type 'hook)
759
760 (defcustom gnus-summary-generate-hook nil
761   "*A hook run just before generating the summary buffer.
762 This hook is commonly used to customize threading variables and the
763 like."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-select-group-hook nil
768   "*A hook called when a newsgroup is selected.
769
770 If you'd like to simplify subjects like the
771 `gnus-summary-next-same-subject' command does, you can use the
772 following hook:
773
774  (setq gnus-select-group-hook
775       (list
776         (lambda ()
777           (mapcar (lambda (header)
778                      (mail-header-set-subject
779                       header
780                       (gnus-simplify-subject
781                        (mail-header-subject header) 're-only)))
782                   gnus-newsgroup-headers))))"
783   :group 'gnus-group-select
784   :type 'hook)
785
786 (defcustom gnus-select-article-hook nil
787   "*A hook called when an article is selected."
788   :group 'gnus-summary-choose
789   :type 'hook)
790
791 (defcustom gnus-visual-mark-article-hook
792   (list 'gnus-highlight-selected-summary)
793   "*Hook run after selecting an article in the summary buffer.
794 It is meant to be used for highlighting the article in some way.  It
795 is not run if `gnus-visual' is nil."
796   :group 'gnus-summary-visual
797   :type 'hook)
798
799 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
800   "*A hook called before parsing the headers."
801   :group 'gnus-various
802   :type 'hook)
803
804 (defcustom gnus-exit-group-hook nil
805   "*A hook called when exiting summary mode.
806 This hook is not called from the non-updating exit commands like `Q'."
807   :group 'gnus-various
808   :type 'hook)
809
810 (defcustom gnus-summary-update-hook
811   (list 'gnus-summary-highlight-line)
812   "*A hook called when a summary line is changed.
813 The hook will not be called if `gnus-visual' is nil.
814
815 The default function `gnus-summary-highlight-line' will
816 highlight the line according to the `gnus-summary-highlight'
817 variable."
818   :group 'gnus-summary-visual
819   :type 'hook)
820
821 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
822   "*A hook called when an article is selected for the first time.
823 The hook is intended to mark an article as read (or unread)
824 automatically when it is selected."
825   :group 'gnus-summary-choose
826   :type 'hook)
827
828 (defcustom gnus-group-no-more-groups-hook nil
829   "*A hook run when returning to group mode having no more (unread) groups."
830   :group 'gnus-group-select
831   :type 'hook)
832
833 (defcustom gnus-ps-print-hook nil
834   "*A hook run before ps-printing something from Gnus."
835   :group 'gnus-summary
836   :type 'hook)
837
838 (defcustom gnus-summary-display-arrow
839   (and (fboundp 'display-graphic-p)
840        (display-graphic-p))
841   "*If non-nil, display an arrow highlighting the current article."
842   :version "21.1"
843   :group 'gnus-summary
844   :type 'boolean)
845
846 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
847   "Face used for highlighting the current article in the summary buffer."
848   :group 'gnus-summary-visual
849   :type 'face)
850
851 (defcustom gnus-summary-highlight
852   '(((= mark gnus-canceled-mark)
853      . gnus-summary-cancelled-face)
854     ((and (> score default-high)
855           (or (= mark gnus-dormant-mark)
856               (= mark gnus-ticked-mark)))
857      . gnus-summary-high-ticked-face)
858     ((and (< score default-low)
859           (or (= mark gnus-dormant-mark)
860               (= mark gnus-ticked-mark)))
861      . gnus-summary-low-ticked-face)
862     ((or (= mark gnus-dormant-mark)
863          (= mark gnus-ticked-mark))
864      . gnus-summary-normal-ticked-face)
865     ((and (> score default-high) (= mark gnus-ancient-mark))
866      . gnus-summary-high-ancient-face)
867     ((and (< score default-low) (= mark gnus-ancient-mark))
868      . gnus-summary-low-ancient-face)
869     ((= mark gnus-ancient-mark)
870      . gnus-summary-normal-ancient-face)
871     ((and (> score default-high) (= mark gnus-unread-mark))
872      . gnus-summary-high-unread-face)
873     ((and (< score default-low) (= mark gnus-unread-mark))
874      . gnus-summary-low-unread-face)
875     ((= mark gnus-unread-mark)
876      . gnus-summary-normal-unread-face)
877     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
878                                                   gnus-undownloaded-mark)))
879      . gnus-summary-high-unread-face)
880     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
881                                                  gnus-undownloaded-mark)))
882      . gnus-summary-low-unread-face)
883     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
884           (memq article gnus-newsgroup-unreads))
885      . gnus-summary-normal-unread-face)
886     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
887      . gnus-summary-normal-read-face)
888     ((> score default-high)
889      . gnus-summary-high-read-face)
890     ((< score default-low)
891      . gnus-summary-low-read-face)
892     (t
893      . gnus-summary-normal-read-face))
894   "*Controls the highlighting of summary buffer lines.
895
896 A list of (FORM . FACE) pairs.  When deciding how a a particular
897 summary line should be displayed, each form is evaluated.  The content
898 of the face field after the first true form is used.  You can change
899 how those summary lines are displayed, by editing the face field.
900
901 You can use the following variables in the FORM field.
902
903 score:        The article's score
904 default:      The default article score.
905 default-high: The default score for high scored articles.
906 default-low:  The default score for low scored articles.
907 below:        The score below which articles are automatically marked as read.
908 mark:         The articles mark."
909   :group 'gnus-summary-visual
910   :type '(repeat (cons (sexp :tag "Form" nil)
911                        face)))
912
913 (defcustom gnus-alter-header-function nil
914   "Function called to allow alteration of article header structures.
915 The function is called with one parameter, the article header vector,
916 which it may alter in any way.")
917
918 (defvar gnus-decode-encoded-word-function
919   (mime-find-field-decoder 'From 'nov)
920   "Variable that says which function should be used to decode a string with encoded words.")
921
922 (defcustom gnus-extra-headers nil
923   "*Extra headers to parse."
924   :version "21.1"
925   :group 'gnus-summary
926   :type '(repeat symbol))
927
928 (defcustom gnus-ignored-from-addresses
929   (and user-mail-address (regexp-quote user-mail-address))
930   "*Regexp of From headers that may be suppressed in favor of To headers."
931   :version "21.1"
932   :group 'gnus-summary
933   :type 'regexp)
934
935 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
936   "List of charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939   :version "21.1"
940   :type '(repeat symbol)
941   :group 'gnus-charset)
942
943 (gnus-define-group-parameter
944  ignored-charsets
945  :type list
946  :function-document
947  "Return the ignored charsets of GROUP."
948  :variable gnus-group-ignored-charsets-alist
949  :variable-default
950  '(("alt\\.chinese\\.text" iso-8859-1))
951  :variable-document
952  "Alist of regexps (to match group names) and charsets that should be ignored.
953 When these charsets are used in the \"charset\" parameter, the
954 default charset will be used instead."
955  :variable-group gnus-charset
956  :variable-type '(repeat (cons (regexp :tag "Group")
957                                (repeat symbol)))
958  :parameter-type '(choice :tag "Ignored charsets"
959                           :value nil
960                           (repeat (symbol)))
961  :parameter-document       "\
962 List of charsets that should be ignored.
963
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead.")
966
967 (defcustom gnus-group-highlight-words-alist nil
968   "Alist of group regexps and highlight regexps.
969 This variable uses the same syntax as `gnus-emphasis-alist'."
970   :version "21.1"
971   :type '(repeat (cons (regexp :tag "Group")
972                        (repeat (list (regexp :tag "Highlight regexp")
973                                      (number :tag "Group for entire word" 0)
974                                      (number :tag "Group for displayed part" 0)
975                                      (symbol :tag "Face"
976                                              gnus-emphasis-highlight-words)))))
977   :group 'gnus-summary-visual)
978
979 (defcustom gnus-use-wheel nil
980   "Use Intelli-mouse on summary movement"
981   :type 'boolean
982   :group 'gnus-summary-maneuvering)
983
984 (defcustom gnus-wheel-scroll-amount '(5 . 1)
985   "Amount to scroll messages by spinning the mouse wheel.
986 This is actually a cons cell, where the first item is the amount to scroll
987 on a normal wheel event, and the second is the amount to scroll when the
988 wheel is moved with the shift key depressed."
989   :type '(cons (integer :tag "Shift") integer)
990   :group 'gnus-summary-maneuvering)
991
992 (defcustom gnus-wheel-edge-resistance 2
993   "How hard it should be to change the current article
994 by moving the mouse over the edge of the article window."
995   :type 'integer
996   :group 'gnus-summary-maneuvering)
997
998 (defcustom gnus-summary-show-article-charset-alist
999   nil
1000   "Alist of number and charset.
1001 The article will be shown with the charset corresponding to the
1002 numbered argument.
1003 For example: ((1 . cn-gb-2312) (2 . big5))."
1004   :version "21.1"
1005   :type '(repeat (cons (number :tag "Argument" 1)
1006                        (symbol :tag "Charset")))
1007   :group 'gnus-charset)
1008
1009 (defcustom gnus-preserve-marks t
1010   "Whether marks are preserved when moving, copying and respooling messages."
1011   :version "21.1"
1012   :type 'boolean
1013   :group 'gnus-summary-marks)
1014
1015 (defcustom gnus-alter-articles-to-read-function nil
1016   "Function to be called to alter the list of articles to be selected."
1017   :type '(choice (const nil) function)
1018   :group 'gnus-summary)
1019
1020 (defcustom gnus-orphan-score nil
1021   "*All orphans get this score added.  Set in the score file."
1022   :group 'gnus-score-default
1023   :type '(choice (const nil)
1024                  integer))
1025
1026 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1027   "*A regexp to match MIME parts when saving multiple parts of a message
1028 with gnus-summary-save-parts (X m). This regexp will be used by default
1029 when prompting the user for which type of files to save."
1030   :group 'gnus-summary
1031   :type 'regexp)
1032
1033
1034 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1035   "*A regexp to match MIME parts when saving multiple parts of a message
1036 with gnus-summary-save-parts (X m). This regexp will be used by default
1037 when prompting the user for which type of files to save."
1038   :group 'gnus-summary
1039   :type 'regexp)
1040
1041 (defcustom gnus-read-all-available-headers nil
1042   "Whether Gnus should parse all headers made available to it.
1043 This is mostly relevant for slow backends where the user may
1044 wish to widen the summary buffer to include all headers
1045 that were fetched.  Say, for nnultimate groups."
1046   :group 'gnus-summary
1047   :type '(choice boolean regexp))
1048
1049 ;;; Internal variables
1050
1051 (defvar gnus-summary-display-cache nil)
1052 (defvar gnus-article-mime-handles nil)
1053 (defvar gnus-article-decoded-p nil)
1054 (defvar gnus-article-charset nil)
1055 (defvar gnus-article-ignored-charsets nil)
1056 (defvar gnus-scores-exclude-files nil)
1057 (defvar gnus-page-broken nil)
1058 (defvar gnus-inhibit-mime-unbuttonizing nil)
1059
1060 (defvar gnus-original-article nil)
1061 (defvar gnus-article-internal-prepare-hook nil)
1062 (defvar gnus-newsgroup-process-stack nil)
1063
1064 (defvar gnus-thread-indent-array nil)
1065 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1066 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1067   "Function called to sort the articles within a thread after it has been gathered together.")
1068
1069 (defvar gnus-summary-save-parts-type-history nil)
1070 (defvar gnus-summary-save-parts-last-directory nil)
1071
1072 (defvar gnus-summary-save-parts-type-history nil)
1073 (defvar gnus-summary-save-parts-last-directory nil)
1074
1075 ;; Avoid highlighting in kill files.
1076 (defvar gnus-summary-inhibit-highlight nil)
1077 (defvar gnus-newsgroup-selected-overlay nil)
1078 (defvar gnus-inhibit-limiting nil)
1079 (defvar gnus-newsgroup-adaptive-score-file nil)
1080 (defvar gnus-current-score-file nil)
1081 (defvar gnus-current-move-group nil)
1082 (defvar gnus-current-copy-group nil)
1083 (defvar gnus-current-crosspost-group nil)
1084 (defvar gnus-newsgroup-display nil)
1085
1086 (defvar gnus-newsgroup-dependencies nil)
1087 (defvar gnus-newsgroup-adaptive nil)
1088 (defvar gnus-summary-display-article-function nil)
1089 (defvar gnus-summary-highlight-line-function nil
1090   "Function called after highlighting a summary line.")
1091
1092 (defvar gnus-summary-line-format-alist
1093   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1094     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1095     (?s gnus-tmp-subject-or-nil ?s)
1096     (?n gnus-tmp-name ?s)
1097     (?A (std11-address-string
1098          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1099     (?a (or (std11-full-name-string
1100              (car (mime-entity-read-field gnus-tmp-header 'From)))
1101             gnus-tmp-from) ?s)
1102     (?F gnus-tmp-from ?s)
1103     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1104     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1105     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1106     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1107     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1108     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1109     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1110     (?L gnus-tmp-lines ?s)
1111     (?I gnus-tmp-indentation ?s)
1112     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1113     (?R gnus-tmp-replied ?c)
1114     (?\[ gnus-tmp-opening-bracket ?c)
1115     (?\] gnus-tmp-closing-bracket ?c)
1116     (?\> (make-string gnus-tmp-level ? ) ?s)
1117     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1118     (?i gnus-tmp-score ?d)
1119     (?z gnus-tmp-score-char ?c)
1120     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1121     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1122     (?U gnus-tmp-unread ?c)
1123     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1124     (?t (gnus-summary-number-of-articles-in-thread
1125          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1126         ?d)
1127     (?e (gnus-summary-number-of-articles-in-thread
1128          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1129         ?c)
1130     (?u gnus-tmp-user-defined ?s)
1131     (?P (gnus-pick-line-number) ?d)
1132     (?B gnus-tmp-thread-tree-header-string ?s))
1133   "An alist of format specifications that can appear in summary lines.
1134 These are paired with what variables they correspond with, along with
1135 the type of the variable (string, integer, character, etc).")
1136
1137 (defvar gnus-summary-dummy-line-format-alist
1138   `((?S gnus-tmp-subject ?s)
1139     (?N gnus-tmp-number ?d)
1140     (?u gnus-tmp-user-defined ?s)))
1141
1142 (defvar gnus-summary-mode-line-format-alist
1143   `((?G gnus-tmp-group-name ?s)
1144     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1145     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1146     (?A gnus-tmp-article-number ?d)
1147     (?Z gnus-tmp-unread-and-unselected ?s)
1148     (?V gnus-version ?s)
1149     (?U gnus-tmp-unread-and-unticked ?d)
1150     (?S gnus-tmp-subject ?s)
1151     (?e gnus-tmp-unselected ?d)
1152     (?u gnus-tmp-user-defined ?s)
1153     (?d (length gnus-newsgroup-dormant) ?d)
1154     (?t (length gnus-newsgroup-marked) ?d)
1155     (?r (length gnus-newsgroup-reads) ?d)
1156     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1157     (?E gnus-newsgroup-expunged-tally ?d)
1158     (?s (gnus-current-score-file-nondirectory) ?s)))
1159
1160 (defvar gnus-last-search-regexp nil
1161   "Default regexp for article search command.")
1162
1163 (defvar gnus-summary-search-article-matched-data nil
1164   "Last matched data of article search command.  It is the local variable
1165 in `gnus-article-buffer' which consists of the list of start position,
1166 end position and text.")
1167
1168 (defvar gnus-last-shell-command nil
1169   "Default shell command on article.")
1170
1171 (defvar gnus-newsgroup-begin nil)
1172 (defvar gnus-newsgroup-end nil)
1173 (defvar gnus-newsgroup-last-rmail nil)
1174 (defvar gnus-newsgroup-last-mail nil)
1175 (defvar gnus-newsgroup-last-folder nil)
1176 (defvar gnus-newsgroup-last-file nil)
1177 (defvar gnus-newsgroup-auto-expire nil)
1178 (defvar gnus-newsgroup-active nil)
1179
1180 (defvar gnus-newsgroup-data nil)
1181 (defvar gnus-newsgroup-data-reverse nil)
1182 (defvar gnus-newsgroup-limit nil)
1183 (defvar gnus-newsgroup-limits nil)
1184
1185 (defvar gnus-newsgroup-unreads nil
1186   "List of unread articles in the current newsgroup.")
1187
1188 (defvar gnus-newsgroup-unselected nil
1189   "List of unselected unread articles in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-reads nil
1192   "Alist of read articles and article marks in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-expunged-tally nil)
1195
1196 (defvar gnus-newsgroup-marked nil
1197   "List of ticked articles in the current newsgroup (a subset of unread art).")
1198
1199 (defvar gnus-newsgroup-killed nil
1200   "List of ranges of articles that have been through the scoring process.")
1201
1202 (defvar gnus-newsgroup-cached nil
1203   "List of articles that come from the article cache.")
1204
1205 (defvar gnus-newsgroup-saved nil
1206   "List of articles that have been saved.")
1207
1208 (defvar gnus-newsgroup-kill-headers nil)
1209
1210 (defvar gnus-newsgroup-replied nil
1211   "List of articles that have been replied to in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-forwarded nil
1214   "List of articles that have been forwarded in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-recent nil
1217   "List of articles that have are recent in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-expirable nil
1220   "List of articles in the current newsgroup that can be expired.")
1221
1222 (defvar gnus-newsgroup-processable nil
1223   "List of articles in the current newsgroup that can be processed.")
1224
1225 (defvar gnus-newsgroup-downloadable nil
1226   "List of articles in the current newsgroup that can be processed.")
1227
1228 (defvar gnus-newsgroup-undownloaded nil
1229   "List of articles in the current newsgroup that haven't been downloaded..")
1230
1231 (defvar gnus-newsgroup-unsendable nil
1232   "List of articles in the current newsgroup that won't be sent.")
1233
1234 (defvar gnus-newsgroup-bookmarks nil
1235   "List of articles in the current newsgroup that have bookmarks.")
1236
1237 (defvar gnus-newsgroup-dormant nil
1238   "List of dormant articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-unseen nil
1241   "List of unseen articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-seen nil
1244   "Range of seen articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-articles nil
1247   "List of articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-scored nil
1250   "List of scored articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-incorporated nil
1253   "List of incorporated articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-headers nil
1256   "List of article headers in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-threads nil)
1259
1260 (defvar gnus-newsgroup-prepared nil
1261   "Whether the current group has been prepared properly.")
1262
1263 (defvar gnus-newsgroup-ancient nil
1264   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-sparse nil)
1267
1268 (defvar gnus-current-article nil)
1269 (defvar gnus-article-current nil)
1270 (defvar gnus-current-headers nil)
1271 (defvar gnus-have-all-headers nil)
1272 (defvar gnus-last-article nil)
1273 (defvar gnus-newsgroup-history nil)
1274 (defvar gnus-newsgroup-charset nil)
1275 (defvar gnus-newsgroup-ephemeral-charset nil)
1276 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1277
1278 (defvar gnus-article-before-search nil)
1279
1280 (defconst gnus-summary-local-variables
1281   '(gnus-newsgroup-name
1282     gnus-newsgroup-begin gnus-newsgroup-end
1283     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1284     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1285     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1286     gnus-newsgroup-unselected gnus-newsgroup-marked
1287     gnus-newsgroup-reads gnus-newsgroup-saved
1288     gnus-newsgroup-replied gnus-newsgroup-forwarded
1289     gnus-newsgroup-recent
1290     gnus-newsgroup-expirable
1291     gnus-newsgroup-processable gnus-newsgroup-killed
1292     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1293     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1294     gnus-newsgroup-seen gnus-newsgroup-articles
1295     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1296     gnus-newsgroup-headers gnus-newsgroup-threads
1297     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1298     gnus-current-article gnus-current-headers gnus-have-all-headers
1299     gnus-last-article gnus-article-internal-prepare-hook
1300     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1301     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1302     gnus-thread-expunge-below
1303     gnus-score-alist gnus-current-score-file
1304     (gnus-summary-expunge-below . global)
1305     (gnus-summary-mark-below . global)
1306     (gnus-orphan-score . global)
1307     gnus-newsgroup-active gnus-scores-exclude-files
1308     gnus-newsgroup-history gnus-newsgroup-ancient
1309     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1310     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1311     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1312     (gnus-newsgroup-expunged-tally . 0)
1313     gnus-cache-removable-articles gnus-newsgroup-cached
1314     gnus-newsgroup-data gnus-newsgroup-data-reverse
1315     gnus-newsgroup-limit gnus-newsgroup-limits
1316     gnus-newsgroup-charset gnus-newsgroup-display
1317     gnus-newsgroup-incorporated)
1318   "Variables that are buffer-local to the summary buffers.")
1319
1320 (defvar gnus-newsgroup-variables nil
1321   "Variables that have separate values in the newsgroups.")
1322
1323 ;; Byte-compiler warning.
1324 (eval-when-compile (defvar gnus-article-mode-map))
1325
1326 ;; Subject simplification.
1327
1328 (defun gnus-simplify-whitespace (str)
1329   "Remove excessive whitespace from STR."
1330   (let ((mystr str))
1331     ;; Multiple spaces.
1332     (while (string-match "[ \t][ \t]+" mystr)
1333       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1334                           " "
1335                           (substring mystr (match-end 0)))))
1336     ;; Leading spaces.
1337     (when (string-match "^[ \t]+" mystr)
1338       (setq mystr (substring mystr (match-end 0))))
1339     ;; Trailing spaces.
1340     (when (string-match "[ \t]+$" mystr)
1341       (setq mystr (substring mystr 0 (match-beginning 0))))
1342     mystr))
1343
1344 (defsubst gnus-simplify-subject-re (subject)
1345   "Remove \"Re:\" from subject lines."
1346   (if (string-match message-subject-re-regexp subject)
1347       (substring subject (match-end 0))
1348     subject))
1349
1350 (defun gnus-simplify-subject (subject &optional re-only)
1351   "Remove `Re:' and words in parentheses.
1352 If RE-ONLY is non-nil, strip leading `Re:'s only."
1353   (let ((case-fold-search t))           ;Ignore case.
1354     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1355     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1356       (setq subject (substring subject (match-end 0))))
1357     ;; Remove uninteresting prefixes.
1358     (when (and (not re-only)
1359                gnus-simplify-ignored-prefixes
1360                (string-match gnus-simplify-ignored-prefixes subject))
1361       (setq subject (substring subject (match-end 0))))
1362     ;; Remove words in parentheses from end.
1363     (unless re-only
1364       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1365         (setq subject (substring subject 0 (match-beginning 0)))))
1366     ;; Return subject string.
1367     subject))
1368
1369 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1370 ;; all whitespace.
1371 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1372   (goto-char (point-min))
1373   (while (re-search-forward regexp nil t)
1374     (replace-match (or newtext ""))))
1375
1376 (defun gnus-simplify-buffer-fuzzy ()
1377   "Simplify string in the buffer fuzzily.
1378 The string in the accessible portion of the current buffer is simplified.
1379 It is assumed to be a single-line subject.
1380 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1381 matter is removed.  Additional things can be deleted by setting
1382 `gnus-simplify-subject-fuzzy-regexp'."
1383   (let ((case-fold-search t)
1384         (modified-tick))
1385     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1386
1387     (while (not (eq modified-tick (buffer-modified-tick)))
1388       (setq modified-tick (buffer-modified-tick))
1389       (cond
1390        ((listp gnus-simplify-subject-fuzzy-regexp)
1391         (mapcar 'gnus-simplify-buffer-fuzzy-step
1392                 gnus-simplify-subject-fuzzy-regexp))
1393        (gnus-simplify-subject-fuzzy-regexp
1394         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1395       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1396       (gnus-simplify-buffer-fuzzy-step
1397        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1398       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1399
1400     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1401     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1402     (gnus-simplify-buffer-fuzzy-step " $")
1403     (gnus-simplify-buffer-fuzzy-step "^ +")))
1404
1405 (defun gnus-simplify-subject-fuzzy (subject)
1406   "Simplify a subject string fuzzily.
1407 See `gnus-simplify-buffer-fuzzy' for details."
1408   (save-excursion
1409     (gnus-set-work-buffer)
1410     (let ((case-fold-search t))
1411       ;; Remove uninteresting prefixes.
1412       (when (and gnus-simplify-ignored-prefixes
1413                  (string-match gnus-simplify-ignored-prefixes subject))
1414         (setq subject (substring subject (match-end 0))))
1415       (insert subject)
1416       (inline (gnus-simplify-buffer-fuzzy))
1417       (buffer-string))))
1418
1419 (defsubst gnus-simplify-subject-fully (subject)
1420   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1421   (cond
1422    (gnus-simplify-subject-functions
1423     (gnus-map-function gnus-simplify-subject-functions subject))
1424    ((null gnus-summary-gather-subject-limit)
1425     (gnus-simplify-subject-re subject))
1426    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1427     (gnus-simplify-subject-fuzzy subject))
1428    ((numberp gnus-summary-gather-subject-limit)
1429     (gnus-limit-string (gnus-simplify-subject-re subject)
1430                        gnus-summary-gather-subject-limit))
1431    (t
1432     subject)))
1433
1434 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1435   "Check whether two subjects are equal.
1436 If optional argument simple-first is t, first argument is already
1437 simplified."
1438   (cond
1439    ((null simple-first)
1440     (equal (gnus-simplify-subject-fully s1)
1441            (gnus-simplify-subject-fully s2)))
1442    (t
1443     (equal s1
1444            (gnus-simplify-subject-fully s2)))))
1445
1446 (defun gnus-summary-bubble-group ()
1447   "Increase the score of the current group.
1448 This is a handy function to add to `gnus-summary-exit-hook' to
1449 increase the score of each group you read."
1450   (gnus-group-add-score gnus-newsgroup-name))
1451
1452 \f
1453 ;;;
1454 ;;; Gnus summary mode
1455 ;;;
1456
1457 (put 'gnus-summary-mode 'mode-class 'special)
1458
1459 (defvar gnus-article-commands-menu)
1460
1461 (when t
1462   ;; Non-orthogonal keys
1463
1464   (gnus-define-keys gnus-summary-mode-map
1465     " " gnus-summary-next-page
1466     "\177" gnus-summary-prev-page
1467     [delete] gnus-summary-prev-page
1468     [backspace] gnus-summary-prev-page
1469     "\r" gnus-summary-scroll-up
1470     "\M-\r" gnus-summary-scroll-down
1471     "n" gnus-summary-next-unread-article
1472     "p" gnus-summary-prev-unread-article
1473     "N" gnus-summary-next-article
1474     "P" gnus-summary-prev-article
1475     "\M-\C-n" gnus-summary-next-same-subject
1476     "\M-\C-p" gnus-summary-prev-same-subject
1477     "\M-n" gnus-summary-next-unread-subject
1478     "\M-p" gnus-summary-prev-unread-subject
1479     "." gnus-summary-first-unread-article
1480     "," gnus-summary-best-unread-article
1481     "\M-s" gnus-summary-search-article-forward
1482     "\M-r" gnus-summary-search-article-backward
1483     "<" gnus-summary-beginning-of-article
1484     ">" gnus-summary-end-of-article
1485     "j" gnus-summary-goto-article
1486     "^" gnus-summary-refer-parent-article
1487     "\M-^" gnus-summary-refer-article
1488     "u" gnus-summary-tick-article-forward
1489     "!" gnus-summary-tick-article-forward
1490     "U" gnus-summary-tick-article-backward
1491     "d" gnus-summary-mark-as-read-forward
1492     "D" gnus-summary-mark-as-read-backward
1493     "E" gnus-summary-mark-as-expirable
1494     "\M-u" gnus-summary-clear-mark-forward
1495     "\M-U" gnus-summary-clear-mark-backward
1496     "k" gnus-summary-kill-same-subject-and-select
1497     "\C-k" gnus-summary-kill-same-subject
1498     "\M-\C-k" gnus-summary-kill-thread
1499     "\M-\C-l" gnus-summary-lower-thread
1500     "e" gnus-summary-edit-article
1501     "#" gnus-summary-mark-as-processable
1502     "\M-#" gnus-summary-unmark-as-processable
1503     "\M-\C-t" gnus-summary-toggle-threads
1504     "\M-\C-s" gnus-summary-show-thread
1505     "\M-\C-h" gnus-summary-hide-thread
1506     "\M-\C-f" gnus-summary-next-thread
1507     "\M-\C-b" gnus-summary-prev-thread
1508     [(meta down)] gnus-summary-next-thread
1509     [(meta up)] gnus-summary-prev-thread
1510     "\M-\C-u" gnus-summary-up-thread
1511     "\M-\C-d" gnus-summary-down-thread
1512     "&" gnus-summary-execute-command
1513     "c" gnus-summary-catchup-and-exit
1514     "\C-w" gnus-summary-mark-region-as-read
1515     "\C-t" gnus-summary-toggle-truncation
1516     "?" gnus-summary-mark-as-dormant
1517     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1518     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1519     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1520     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1521     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1522     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1523     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1524     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1525     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1526     "=" gnus-summary-expand-window
1527     "\C-x\C-s" gnus-summary-reselect-current-group
1528     "\M-g" gnus-summary-rescan-group
1529     "w" gnus-summary-stop-page-breaking
1530     "\C-c\C-r" gnus-summary-caesar-message
1531     "\M-t" gnus-summary-toggle-mime
1532     "f" gnus-summary-followup
1533     "F" gnus-summary-followup-with-original
1534     "C" gnus-summary-cancel-article
1535     "r" gnus-summary-reply
1536     "R" gnus-summary-reply-with-original
1537     "\C-c\C-f" gnus-summary-mail-forward
1538     "o" gnus-summary-save-article
1539     "\C-o" gnus-summary-save-article-mail
1540     "|" gnus-summary-pipe-output
1541     "\M-k" gnus-summary-edit-local-kill
1542     "\M-K" gnus-summary-edit-global-kill
1543     ;; "V" gnus-version
1544     "\C-c\C-d" gnus-summary-describe-group
1545     "q" gnus-summary-exit
1546     "Q" gnus-summary-exit-no-update
1547     "\C-c\C-i" gnus-info-find-node
1548     gnus-mouse-2 gnus-mouse-pick-article
1549     "m" gnus-summary-mail-other-window
1550     "a" gnus-summary-post-news
1551     "x" gnus-summary-limit-to-unread
1552     "s" gnus-summary-isearch-article
1553     "t" gnus-article-toggle-headers
1554     "g" gnus-summary-show-article
1555     "l" gnus-summary-goto-last-article
1556     "v" gnus-summary-preview-mime-message
1557     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1558     "\C-d" gnus-summary-enter-digest-group
1559     "\M-\C-d" gnus-summary-read-document
1560     "\M-\C-e" gnus-summary-edit-parameters
1561     "\M-\C-a" gnus-summary-customize-parameters
1562     "\C-c\C-b" gnus-bug
1563     "\C-c\C-n" gnus-namazu-search
1564     "*" gnus-cache-enter-article
1565     "\M-*" gnus-cache-remove-article
1566     "\M-&" gnus-summary-universal-argument
1567     "\C-l" gnus-recenter
1568     "I" gnus-summary-increase-score
1569     "L" gnus-summary-lower-score
1570     "\M-i" gnus-symbolic-argument
1571     "h" gnus-summary-select-article-buffer
1572
1573     "V" gnus-summary-score-map
1574     "X" gnus-uu-extract-map
1575     "S" gnus-summary-send-map)
1576
1577   ;; Sort of orthogonal keymap
1578   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1579     "t" gnus-summary-tick-article-forward
1580     "!" gnus-summary-tick-article-forward
1581     "d" gnus-summary-mark-as-read-forward
1582     "r" gnus-summary-mark-as-read-forward
1583     "c" gnus-summary-clear-mark-forward
1584     " " gnus-summary-clear-mark-forward
1585     "e" gnus-summary-mark-as-expirable
1586     "x" gnus-summary-mark-as-expirable
1587     "?" gnus-summary-mark-as-dormant
1588     "b" gnus-summary-set-bookmark
1589     "B" gnus-summary-remove-bookmark
1590     "#" gnus-summary-mark-as-processable
1591     "\M-#" gnus-summary-unmark-as-processable
1592     "S" gnus-summary-limit-include-expunged
1593     "C" gnus-summary-catchup
1594     "H" gnus-summary-catchup-to-here
1595     "h" gnus-summary-catchup-from-here
1596     "\C-c" gnus-summary-catchup-all
1597     "k" gnus-summary-kill-same-subject-and-select
1598     "K" gnus-summary-kill-same-subject
1599     "P" gnus-uu-mark-map)
1600
1601   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1602     "c" gnus-summary-clear-above
1603     "u" gnus-summary-tick-above
1604     "m" gnus-summary-mark-above
1605     "k" gnus-summary-kill-below)
1606
1607   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1608     "/" gnus-summary-limit-to-subject
1609     "n" gnus-summary-limit-to-articles
1610     "w" gnus-summary-pop-limit
1611     "s" gnus-summary-limit-to-subject
1612     "a" gnus-summary-limit-to-author
1613     "u" gnus-summary-limit-to-unread
1614     "m" gnus-summary-limit-to-marks
1615     "M" gnus-summary-limit-exclude-marks
1616     "v" gnus-summary-limit-to-score
1617     "*" gnus-summary-limit-include-cached
1618     "D" gnus-summary-limit-include-dormant
1619     "T" gnus-summary-limit-include-thread
1620     "d" gnus-summary-limit-exclude-dormant
1621     "t" gnus-summary-limit-to-age
1622     "x" gnus-summary-limit-to-extra
1623     "p" gnus-summary-limit-to-display-predicate
1624     "E" gnus-summary-limit-include-expunged
1625     "c" gnus-summary-limit-exclude-childless-dormant
1626     "C" gnus-summary-limit-mark-excluded-as-read
1627     "o" gnus-summary-insert-old-articles
1628     "N" gnus-summary-insert-new-articles)
1629
1630   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1631     "n" gnus-summary-next-unread-article
1632     "p" gnus-summary-prev-unread-article
1633     "N" gnus-summary-next-article
1634     "P" gnus-summary-prev-article
1635     "\C-n" gnus-summary-next-same-subject
1636     "\C-p" gnus-summary-prev-same-subject
1637     "\M-n" gnus-summary-next-unread-subject
1638     "\M-p" gnus-summary-prev-unread-subject
1639     "f" gnus-summary-first-unread-article
1640     "b" gnus-summary-best-unread-article
1641     "j" gnus-summary-goto-article
1642     "g" gnus-summary-goto-subject
1643     "l" gnus-summary-goto-last-article
1644     "o" gnus-summary-pop-article)
1645
1646   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1647     "k" gnus-summary-kill-thread
1648     "l" gnus-summary-lower-thread
1649     "i" gnus-summary-raise-thread
1650     "T" gnus-summary-toggle-threads
1651     "t" gnus-summary-rethread-current
1652     "^" gnus-summary-reparent-thread
1653     "s" gnus-summary-show-thread
1654     "S" gnus-summary-show-all-threads
1655     "h" gnus-summary-hide-thread
1656     "H" gnus-summary-hide-all-threads
1657     "n" gnus-summary-next-thread
1658     "p" gnus-summary-prev-thread
1659     "u" gnus-summary-up-thread
1660     "o" gnus-summary-top-thread
1661     "d" gnus-summary-down-thread
1662     "#" gnus-uu-mark-thread
1663     "\M-#" gnus-uu-unmark-thread)
1664
1665   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1666     "g" gnus-summary-prepare
1667     "c" gnus-summary-insert-cached-articles)
1668
1669   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1670     "c" gnus-summary-catchup-and-exit
1671     "C" gnus-summary-catchup-all-and-exit
1672     "E" gnus-summary-exit-no-update
1673     "J" gnus-summary-jump-to-other-group
1674     "Q" gnus-summary-exit
1675     "Z" gnus-summary-exit
1676     "n" gnus-summary-catchup-and-goto-next-group
1677     "R" gnus-summary-reselect-current-group
1678     "G" gnus-summary-rescan-group
1679     "N" gnus-summary-next-group
1680     "s" gnus-summary-save-newsrc
1681     "P" gnus-summary-prev-group)
1682
1683   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1684     " " gnus-summary-next-page
1685     "n" gnus-summary-next-page
1686     "\177" gnus-summary-prev-page
1687     [delete] gnus-summary-prev-page
1688     "p" gnus-summary-prev-page
1689     "\r" gnus-summary-scroll-up
1690     "\M-\r" gnus-summary-scroll-down
1691     "<" gnus-summary-beginning-of-article
1692     ">" gnus-summary-end-of-article
1693     "b" gnus-summary-beginning-of-article
1694     "e" gnus-summary-end-of-article
1695     "^" gnus-summary-refer-parent-article
1696     "r" gnus-summary-refer-parent-article
1697     "D" gnus-summary-enter-digest-group
1698     "R" gnus-summary-refer-references
1699     "T" gnus-summary-refer-thread
1700     "g" gnus-summary-show-article
1701     "s" gnus-summary-isearch-article
1702     "P" gnus-summary-print-article
1703     "M" gnus-mailing-list-insinuate
1704     "t" gnus-article-babel)
1705
1706   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1707     "b" gnus-article-add-buttons
1708     "B" gnus-article-add-buttons-to-head
1709     "o" gnus-article-treat-overstrike
1710     "e" gnus-article-emphasize
1711     "w" gnus-article-fill-cited-article
1712     "Q" gnus-article-fill-long-lines
1713     "C" gnus-article-capitalize-sentences
1714     "c" gnus-article-remove-cr
1715     "Z" gnus-article-decode-HZ
1716     "f" gnus-article-display-x-face
1717     "l" gnus-summary-stop-page-breaking
1718     "r" gnus-summary-caesar-message
1719     "t" gnus-article-toggle-headers
1720     "g" gnus-summary-toggle-smiley
1721     "v" gnus-summary-verbose-headers
1722     "m" gnus-summary-toggle-mime
1723     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1724     "p" gnus-article-verify-x-pgp-sig
1725     "d" gnus-article-treat-dumbquotes
1726     "s" gnus-smiley-display)
1727
1728   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1729     "a" gnus-article-hide
1730     "h" gnus-article-toggle-headers
1731     "b" gnus-article-hide-boring-headers
1732     "s" gnus-article-hide-signature
1733     "c" gnus-article-hide-citation
1734     "C" gnus-article-hide-citation-in-followups
1735     "l" gnus-article-hide-list-identifiers
1736     "p" gnus-article-hide-pgp
1737     "B" gnus-article-strip-banner
1738     "P" gnus-article-hide-pem
1739     "\C-c" gnus-article-hide-citation-maybe)
1740
1741   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1742     "a" gnus-article-highlight
1743     "h" gnus-article-highlight-headers
1744     "c" gnus-article-highlight-citation
1745     "s" gnus-article-highlight-signature)
1746
1747   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1748     "z" gnus-article-date-ut
1749     "u" gnus-article-date-ut
1750     "l" gnus-article-date-local
1751     "p" gnus-article-date-english
1752     "e" gnus-article-date-lapsed
1753     "o" gnus-article-date-original
1754     "i" gnus-article-date-iso8601
1755     "s" gnus-article-date-user)
1756
1757   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1758     "t" gnus-article-remove-trailing-blank-lines
1759     "l" gnus-article-strip-leading-blank-lines
1760     "m" gnus-article-strip-multiple-blank-lines
1761     "a" gnus-article-strip-blank-lines
1762     "A" gnus-article-strip-all-blank-lines
1763     "s" gnus-article-strip-leading-space
1764     "e" gnus-article-strip-trailing-space
1765     "w" gnus-article-remove-leading-whitespace)
1766
1767   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1768     "v" gnus-version
1769     "f" gnus-summary-fetch-faq
1770     "d" gnus-summary-describe-group
1771     "h" gnus-summary-describe-briefly
1772     "i" gnus-info-find-node)
1773
1774   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1775     "e" gnus-summary-expire-articles
1776     "\M-\C-e" gnus-summary-expire-articles-now
1777     "\177" gnus-summary-delete-article
1778     [delete] gnus-summary-delete-article
1779     [backspace] gnus-summary-delete-article
1780     "m" gnus-summary-move-article
1781     "r" gnus-summary-respool-article
1782     "w" gnus-summary-edit-article
1783     "c" gnus-summary-copy-article
1784     "B" gnus-summary-crosspost-article
1785     "q" gnus-summary-respool-query
1786     "t" gnus-summary-respool-trace
1787     "i" gnus-summary-import-article
1788     "I" gnus-summary-create-article
1789     "p" gnus-summary-article-posted-p)
1790
1791   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1792     "o" gnus-summary-save-article
1793     "m" gnus-summary-save-article-mail
1794     "F" gnus-summary-write-article-file
1795     "r" gnus-summary-save-article-rmail
1796     "f" gnus-summary-save-article-file
1797     "b" gnus-summary-save-article-body-file
1798     "h" gnus-summary-save-article-folder
1799     "v" gnus-summary-save-article-vm
1800     "p" gnus-summary-pipe-output
1801     "s" gnus-soup-add-article)
1802
1803   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1804     "b" gnus-summary-display-buttonized
1805     "m" gnus-summary-repair-multipart
1806     "v" gnus-article-view-part
1807     "o" gnus-article-save-part
1808     "c" gnus-article-copy-part
1809     "C" gnus-article-view-part-as-charset
1810     "e" gnus-article-externalize-part
1811     "E" gnus-article-encrypt-body
1812     "i" gnus-article-inline-part
1813     "|" gnus-article-pipe-part))
1814
1815 (defvar gnus-article-post-menu nil)
1816
1817 (defun gnus-summary-make-menu-bar ()
1818   (gnus-turn-off-edit-menu 'summary)
1819
1820   (unless (boundp 'gnus-summary-misc-menu)
1821
1822     (easy-menu-define
1823      gnus-summary-kill-menu gnus-summary-mode-map ""
1824      (cons
1825       "Score"
1826       (nconc
1827        (list
1828         ["Customize" gnus-score-customize t])
1829        (gnus-make-score-map 'increase)
1830        (gnus-make-score-map 'lower)
1831        '(("Mark"
1832           ["Kill below" gnus-summary-kill-below t]
1833           ["Mark above" gnus-summary-mark-above t]
1834           ["Tick above" gnus-summary-tick-above t]
1835           ["Clear above" gnus-summary-clear-above t])
1836          ["Current score" gnus-summary-current-score t]
1837          ["Set score" gnus-summary-set-score t]
1838          ["Switch current score file..." gnus-score-change-score-file t]
1839          ["Set mark below..." gnus-score-set-mark-below t]
1840          ["Set expunge below..." gnus-score-set-expunge-below t]
1841          ["Edit current score file" gnus-score-edit-current-scores t]
1842          ["Edit score file" gnus-score-edit-file t]
1843          ["Trace score" gnus-score-find-trace t]
1844          ["Find words" gnus-score-find-favourite-words t]
1845          ["Rescore buffer" gnus-summary-rescore t]
1846          ["Increase score..." gnus-summary-increase-score t]
1847          ["Lower score..." gnus-summary-lower-score t]))))
1848
1849     ;; Define both the Article menu in the summary buffer and the equivalent
1850     ;; Commands menu in the article buffer here for consistency.
1851     (let ((innards
1852            `(("Hide"
1853               ["All" gnus-article-hide t]
1854               ["Headers" gnus-article-toggle-headers t]
1855               ["Signature" gnus-article-hide-signature t]
1856               ["Citation" gnus-article-hide-citation t]
1857               ["List identifiers" gnus-article-hide-list-identifiers t]
1858               ["PGP" gnus-article-hide-pgp t]
1859               ["Banner" gnus-article-strip-banner t]
1860               ["Boring headers" gnus-article-hide-boring-headers t])
1861              ("Highlight"
1862               ["All" gnus-article-highlight t]
1863               ["Headers" gnus-article-highlight-headers t]
1864               ["Signature" gnus-article-highlight-signature t]
1865               ["Citation" gnus-article-highlight-citation t])
1866              ("Date"
1867               ["Local" gnus-article-date-local t]
1868               ["ISO8601" gnus-article-date-iso8601 t]
1869               ["UT" gnus-article-date-ut t]
1870               ["Original" gnus-article-date-original t]
1871               ["Lapsed" gnus-article-date-lapsed t]
1872               ["User-defined" gnus-article-date-user t])
1873              ("Washing"
1874               ("Remove Blanks"
1875                ["Leading" gnus-article-strip-leading-blank-lines t]
1876                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1877                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1878                ["All of the above" gnus-article-strip-blank-lines t]
1879                ["All" gnus-article-strip-all-blank-lines t]
1880                ["Leading space" gnus-article-strip-leading-space t]
1881                ["Trailing space" gnus-article-strip-trailing-space t]
1882                ["Leading space in headers"
1883                 gnus-article-remove-leading-whitespace t])
1884               ["Overstrike" gnus-article-treat-overstrike t]
1885               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1886               ["Emphasis" gnus-article-emphasize t]
1887               ["Word wrap" gnus-article-fill-cited-article t]
1888               ["Fill long lines" gnus-article-fill-long-lines t]
1889               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1890               ["CR" gnus-article-remove-cr t]
1891               ["Show X-Face" gnus-article-display-x-face t]
1892               ["Rot 13" gnus-summary-caesar-message
1893                ,@(if (featurep 'xemacs) '(t)
1894                    '(:help "\"Caesar rotate\" article by 13"))]
1895               ["Unix pipe" gnus-summary-pipe-message t]
1896               ["Add buttons" gnus-article-add-buttons t]
1897               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1898               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1899               ["Toggle MIME" gnus-summary-toggle-mime t]
1900               ["Verbose header" gnus-summary-verbose-headers t]
1901               ["Toggle header" gnus-summary-toggle-header t]
1902               ["Toggle smiley" gnus-summary-toggle-smiley t]
1903               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1904               ["HZ" gnus-article-decode-HZ t])
1905              ("Output"
1906               ["Save in default format" gnus-summary-save-article
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Save article using default method"))]
1909               ["Save in file" gnus-summary-save-article-file
1910                ,@(if (featurep 'xemacs) '(t)
1911                    '(:help "Save article in file"))]
1912               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1913               ["Save in MH folder" gnus-summary-save-article-folder t]
1914               ["Save in VM folder" gnus-summary-save-article-vm t]
1915               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1916               ["Save body in file" gnus-summary-save-article-body-file t]
1917               ["Pipe through a filter" gnus-summary-pipe-output t]
1918               ["Add to SOUP packet" gnus-soup-add-article t]
1919               ["Print" gnus-summary-print-article t])
1920              ("Backend"
1921               ["Respool article..." gnus-summary-respool-article t]
1922               ["Move article..." gnus-summary-move-article
1923                (gnus-check-backend-function
1924                 'request-move-article gnus-newsgroup-name)]
1925               ["Copy article..." gnus-summary-copy-article t]
1926               ["Crosspost article..." gnus-summary-crosspost-article
1927                (gnus-check-backend-function
1928                 'request-replace-article gnus-newsgroup-name)]
1929               ["Import file..." gnus-summary-import-article t]
1930               ["Create article..." gnus-summary-create-article t]
1931               ["Check if posted" gnus-summary-article-posted-p t]
1932               ["Edit article" gnus-summary-edit-article
1933                (not (gnus-group-read-only-p))]
1934               ["Delete article" gnus-summary-delete-article
1935                (gnus-check-backend-function
1936                 'request-expire-articles gnus-newsgroup-name)]
1937               ["Query respool" gnus-summary-respool-query t]
1938               ["Trace respool" gnus-summary-respool-trace t]
1939               ["Delete expirable articles" gnus-summary-expire-articles-now
1940                (gnus-check-backend-function
1941                 'request-expire-articles gnus-newsgroup-name)])
1942              ("Extract"
1943               ["Uudecode" gnus-uu-decode-uu
1944                ,@(if (featurep 'xemacs) '(t)
1945                    '(:help "Decode uuencoded article(s)"))]
1946               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1947               ["Unshar" gnus-uu-decode-unshar t]
1948               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1949               ["Save" gnus-uu-decode-save t]
1950               ["Binhex" gnus-uu-decode-binhex t]
1951               ["Postscript" gnus-uu-decode-postscript t])
1952              ("Cache"
1953               ["Enter article" gnus-cache-enter-article t]
1954               ["Remove article" gnus-cache-remove-article t])
1955              ["Translate" gnus-article-babel t]
1956              ["Select article buffer" gnus-summary-select-article-buffer t]
1957              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1958              ["Isearch article..." gnus-summary-isearch-article t]
1959              ["Beginning of the article" gnus-summary-beginning-of-article t]
1960              ["End of the article" gnus-summary-end-of-article t]
1961              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1962              ["Fetch referenced articles" gnus-summary-refer-references t]
1963              ["Fetch current thread" gnus-summary-refer-thread t]
1964              ["Fetch article with id..." gnus-summary-refer-article t]
1965              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1966              ["Redisplay" gnus-summary-show-article t]
1967              ["Raw article" gnus-summary-show-raw-article t])))
1968       (easy-menu-define
1969        gnus-summary-article-menu gnus-summary-mode-map ""
1970        (cons "Article" innards))
1971
1972       (if (not (keymapp gnus-summary-article-menu))
1973           (easy-menu-define
1974            gnus-article-commands-menu gnus-article-mode-map ""
1975            (cons "Commands" innards))
1976         ;; in Emacs, don't share menu.
1977         (setq gnus-article-commands-menu
1978               (copy-keymap gnus-summary-article-menu))
1979         (define-key gnus-article-mode-map [menu-bar commands]
1980           (cons "Commands" gnus-article-commands-menu))))
1981
1982     (easy-menu-define
1983      gnus-summary-thread-menu gnus-summary-mode-map ""
1984      '("Threads"
1985        ["Toggle threading" gnus-summary-toggle-threads t]
1986        ["Hide threads" gnus-summary-hide-all-threads t]
1987        ["Show threads" gnus-summary-show-all-threads t]
1988        ["Hide thread" gnus-summary-hide-thread t]
1989        ["Show thread" gnus-summary-show-thread t]
1990        ["Go to next thread" gnus-summary-next-thread t]
1991        ["Go to previous thread" gnus-summary-prev-thread t]
1992        ["Go down thread" gnus-summary-down-thread t]
1993        ["Go up thread" gnus-summary-up-thread t]
1994        ["Top of thread" gnus-summary-top-thread t]
1995        ["Mark thread as read" gnus-summary-kill-thread t]
1996        ["Lower thread score" gnus-summary-lower-thread t]
1997        ["Raise thread score" gnus-summary-raise-thread t]
1998        ["Rethread current" gnus-summary-rethread-current t]))
1999
2000     (easy-menu-define
2001      gnus-summary-post-menu gnus-summary-mode-map ""
2002      `("Post"
2003        ["Post an article" gnus-summary-post-news
2004         ,@(if (featurep 'xemacs) '(t)
2005             '(:help "Post an article"))]
2006        ["Followup" gnus-summary-followup
2007         ,@(if (featurep 'xemacs) '(t)
2008             '(:help "Post followup to this article"))]
2009        ["Followup and yank" gnus-summary-followup-with-original
2010         ,@(if (featurep 'xemacs) '(t)
2011             '(:help "Post followup to this article, quoting its contents"))]
2012        ["Supersede article" gnus-summary-supersede-article t]
2013        ["Cancel article" gnus-summary-cancel-article
2014         ,@(if (featurep 'xemacs) '(t)
2015             '(:help "Cancel an article you posted"))]
2016        ["Reply" gnus-summary-reply t]
2017        ["Reply and yank" gnus-summary-reply-with-original t]
2018        ["Wide reply" gnus-summary-wide-reply t]
2019        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2020         ,@(if (featurep 'xemacs) '(t)
2021             '(:help "Mail a reply, quoting this article"))]
2022        ["Mail forward" gnus-summary-mail-forward t]
2023        ["Post forward" gnus-summary-post-forward t]
2024        ["Digest and mail" gnus-summary-digest-mail-forward t]
2025        ["Digest and post" gnus-summary-digest-post-forward t]
2026        ["Resend message" gnus-summary-resend-message t]
2027        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2028        ["Send a mail" gnus-summary-mail-other-window t]
2029        ["Uuencode and post" gnus-uu-post-news
2030         ,@(if (featurep 'xemacs) '(t)
2031             '(:help "Post a uuencoded article"))]
2032        ["Followup via news" gnus-summary-followup-to-mail t]
2033        ["Followup via news and yank"
2034         gnus-summary-followup-to-mail-with-original t]
2035        ;;("Draft"
2036        ;;["Send" gnus-summary-send-draft t]
2037        ;;["Send bounced" gnus-resend-bounced-mail t])
2038        ))
2039
2040     (cond
2041      ((not (keymapp gnus-summary-post-menu))
2042       (setq gnus-article-post-menu gnus-summary-post-menu))
2043      ((not gnus-article-post-menu)
2044       ;; Don't share post menu.
2045       (setq gnus-article-post-menu
2046             (copy-keymap gnus-summary-post-menu))))
2047     (define-key gnus-article-mode-map [menu-bar post]
2048       (cons "Post" gnus-article-post-menu))
2049
2050     (easy-menu-define
2051      gnus-summary-misc-menu gnus-summary-mode-map ""
2052      `("Gnus"
2053        ("Mark Read"
2054         ["Mark as read" gnus-summary-mark-as-read-forward t]
2055         ["Mark same subject and select"
2056          gnus-summary-kill-same-subject-and-select t]
2057         ["Mark same subject" gnus-summary-kill-same-subject t]
2058         ["Catchup" gnus-summary-catchup
2059          ,@(if (featurep 'xemacs) '(t)
2060              '(:help "Mark unread articles in this group as read"))]
2061         ["Catchup all" gnus-summary-catchup-all t]
2062         ["Catchup to here" gnus-summary-catchup-to-here t]
2063         ["Catchup from here" gnus-summary-catchup-from-here t]
2064         ["Catchup region" gnus-summary-mark-region-as-read t]
2065         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2066        ("Mark Various"
2067         ["Tick" gnus-summary-tick-article-forward t]
2068         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2069         ["Remove marks" gnus-summary-clear-mark-forward t]
2070         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2071         ["Set bookmark" gnus-summary-set-bookmark t]
2072         ["Remove bookmark" gnus-summary-remove-bookmark t])
2073        ("Mark Limit"
2074         ["Marks..." gnus-summary-limit-to-marks t]
2075         ["Subject..." gnus-summary-limit-to-subject t]
2076         ["Author..." gnus-summary-limit-to-author t]
2077         ["Age..." gnus-summary-limit-to-age t]
2078         ["Extra..." gnus-summary-limit-to-extra t]
2079         ["Score" gnus-summary-limit-to-score t]
2080         ["Score" gnus-summary-limit-to-display-predicate t]
2081         ["Unread" gnus-summary-limit-to-unread t]
2082         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2083         ["Articles" gnus-summary-limit-to-articles t]
2084         ["Pop limit" gnus-summary-pop-limit t]
2085         ["Show dormant" gnus-summary-limit-include-dormant t]
2086         ["Hide childless dormant"
2087          gnus-summary-limit-exclude-childless-dormant t]
2088         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2089         ["Hide marked" gnus-summary-limit-exclude-marks t]
2090         ["Show expunged" gnus-summary-limit-include-expunged t])
2091        ("Process Mark"
2092         ["Set mark" gnus-summary-mark-as-processable t]
2093         ["Remove mark" gnus-summary-unmark-as-processable t]
2094         ["Remove all marks" gnus-summary-unmark-all-processable t]
2095         ["Mark above" gnus-uu-mark-over t]
2096         ["Mark series" gnus-uu-mark-series t]
2097         ["Mark region" gnus-uu-mark-region t]
2098         ["Unmark region" gnus-uu-unmark-region t]
2099         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2100         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2101         ["Mark all" gnus-uu-mark-all t]
2102         ["Mark buffer" gnus-uu-mark-buffer t]
2103         ["Mark sparse" gnus-uu-mark-sparse t]
2104         ["Mark thread" gnus-uu-mark-thread t]
2105         ["Unmark thread" gnus-uu-unmark-thread t]
2106         ("Process Mark Sets"
2107          ["Kill" gnus-summary-kill-process-mark t]
2108          ["Yank" gnus-summary-yank-process-mark
2109           gnus-newsgroup-process-stack]
2110          ["Save" gnus-summary-save-process-mark t]))
2111        ("Scroll article"
2112         ["Page forward" gnus-summary-next-page
2113          ,@(if (featurep 'xemacs) '(t)
2114              '(:help "Show next page of article"))]
2115         ["Page backward" gnus-summary-prev-page
2116          ,@(if (featurep 'xemacs) '(t)
2117              '(:help "Show previous page of article"))]
2118         ["Line forward" gnus-summary-scroll-up t])
2119        ("Move"
2120         ["Next unread article" gnus-summary-next-unread-article t]
2121         ["Previous unread article" gnus-summary-prev-unread-article t]
2122         ["Next article" gnus-summary-next-article t]
2123         ["Previous article" gnus-summary-prev-article t]
2124         ["Next unread subject" gnus-summary-next-unread-subject t]
2125         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2126         ["Next article same subject" gnus-summary-next-same-subject t]
2127         ["Previous article same subject" gnus-summary-prev-same-subject t]
2128         ["First unread article" gnus-summary-first-unread-article t]
2129         ["Best unread article" gnus-summary-best-unread-article t]
2130         ["Go to subject number..." gnus-summary-goto-subject t]
2131         ["Go to article number..." gnus-summary-goto-article t]
2132         ["Go to the last article" gnus-summary-goto-last-article t]
2133         ["Pop article off history" gnus-summary-pop-article t])
2134        ("Sort"
2135         ["Sort by number" gnus-summary-sort-by-number t]
2136         ["Sort by author" gnus-summary-sort-by-author t]
2137         ["Sort by subject" gnus-summary-sort-by-subject t]
2138         ["Sort by date" gnus-summary-sort-by-date t]
2139         ["Sort by score" gnus-summary-sort-by-score t]
2140         ["Sort by lines" gnus-summary-sort-by-lines t]
2141         ["Sort by characters" gnus-summary-sort-by-chars t]
2142         ["Original sort" gnus-summary-sort-by-original t])
2143        ("Help"
2144         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2145         ["Describe group" gnus-summary-describe-group t]
2146         ["Read manual" gnus-info-find-node t])
2147        ("Modes"
2148         ["Pick and read" gnus-pick-mode t]
2149         ["Binary" gnus-binary-mode t])
2150        ("Regeneration"
2151         ["Regenerate" gnus-summary-prepare t]
2152         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2153         ["Toggle threading" gnus-summary-toggle-threads t])
2154        ["See old articles" gnus-summary-insert-old-articles t]
2155        ["See new articles" gnus-summary-insert-new-articles t]
2156        ["Filter articles..." gnus-summary-execute-command t]
2157        ["Run command on subjects..." gnus-summary-universal-argument t]
2158        ["Search articles forward..." gnus-summary-search-article-forward t]
2159        ["Search articles backward..." gnus-summary-search-article-backward t]
2160        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2161        ["Expand window" gnus-summary-expand-window t]
2162        ["Expire expirable articles" gnus-summary-expire-articles
2163         (gnus-check-backend-function
2164          'request-expire-articles gnus-newsgroup-name)]
2165        ["Edit local kill file" gnus-summary-edit-local-kill t]
2166        ["Edit main kill file" gnus-summary-edit-global-kill t]
2167        ["Edit group parameters" gnus-summary-edit-parameters t]
2168        ["Customize group parameters" gnus-summary-customize-parameters t]
2169        ["Send a bug report" gnus-bug t]
2170        ("Exit"
2171         ["Catchup and exit" gnus-summary-catchup-and-exit
2172          ,@(if (featurep 'xemacs) '(t)
2173              '(:help "Mark unread articles in this group as read, then exit"))]
2174         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2175         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2176         ["Exit group" gnus-summary-exit
2177          ,@(if (featurep 'xemacs) '(t)
2178              '(:help "Exit current group, return to group selection mode"))]
2179         ["Exit group without updating" gnus-summary-exit-no-update t]
2180         ["Exit and goto next group" gnus-summary-next-group t]
2181         ["Exit and goto prev group" gnus-summary-prev-group t]
2182         ["Reselect group" gnus-summary-reselect-current-group t]
2183         ["Rescan group" gnus-summary-rescan-group t]
2184         ["Update dribble" gnus-summary-save-newsrc t])))
2185
2186     (gnus-run-hooks 'gnus-summary-menu-hook)))
2187
2188 (defvar gnus-summary-tool-bar-map nil)
2189
2190 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2191 (defun gnus-summary-make-tool-bar ()
2192   (if (and (fboundp 'tool-bar-add-item-from-menu)
2193            (default-value 'tool-bar-mode)
2194            (not gnus-summary-tool-bar-map))
2195       (setq gnus-summary-tool-bar-map
2196             (let ((tool-bar-map (make-sparse-keymap))
2197                   (load-path (mm-image-load-path)))
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-post-news "post" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2206               (tool-bar-add-item-from-menu
2207                'gnus-summary-followup "followup" gnus-summary-mode-map)
2208               (tool-bar-add-item-from-menu
2209                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2210               (tool-bar-add-item-from-menu
2211                'gnus-summary-reply "reply" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2226               (tool-bar-add-item-from-menu
2227                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2228               tool-bar-map)))
2229   (if gnus-summary-tool-bar-map
2230       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2231
2232 (defun gnus-score-set-default (var value)
2233   "A version of set that updates the GNU Emacs menu-bar."
2234   (set var value)
2235   ;; It is the message that forces the active status to be updated.
2236   (message ""))
2237
2238 (defun gnus-make-score-map (type)
2239   "Make a summary score map of type TYPE."
2240   (if t
2241       nil
2242     (let ((headers '(("author" "from" string)
2243                      ("subject" "subject" string)
2244                      ("article body" "body" string)
2245                      ("article head" "head" string)
2246                      ("xref" "xref" string)
2247                      ("extra header" "extra" string)
2248                      ("lines" "lines" number)
2249                      ("followups to author" "followup" string)))
2250           (types '((number ("less than" <)
2251                            ("greater than" >)
2252                            ("equal" =))
2253                    (string ("substring" s)
2254                            ("exact string" e)
2255                            ("fuzzy string" f)
2256                            ("regexp" r))))
2257           (perms '(("temporary" (current-time-string))
2258                    ("permanent" nil)
2259                    ("immediate" now)))
2260           header)
2261       (list
2262        (apply
2263         'nconc
2264         (list
2265          (if (eq type 'lower)
2266              "Lower score"
2267            "Increase score"))
2268         (let (outh)
2269           (while headers
2270             (setq header (car headers))
2271             (setq outh
2272                   (cons
2273                    (apply
2274                     'nconc
2275                     (list (car header))
2276                     (let ((ts (cdr (assoc (nth 2 header) types)))
2277                           outt)
2278                       (while ts
2279                         (setq outt
2280                               (cons
2281                                (apply
2282                                 'nconc
2283                                 (list (caar ts))
2284                                 (let ((ps perms)
2285                                       outp)
2286                                   (while ps
2287                                     (setq outp
2288                                           (cons
2289                                            (vector
2290                                             (caar ps)
2291                                             (list
2292                                              'gnus-summary-score-entry
2293                                              (nth 1 header)
2294                                              (if (or (string= (nth 1 header)
2295                                                               "head")
2296                                                      (string= (nth 1 header)
2297                                                               "body"))
2298                                                  ""
2299                                                (list 'gnus-summary-header
2300                                                      (nth 1 header)))
2301                                              (list 'quote (nth 1 (car ts)))
2302                                              (list 'gnus-score-delta-default
2303                                                    nil)
2304                                              (nth 1 (car ps))
2305                                              t)
2306                                             t)
2307                                            outp))
2308                                     (setq ps (cdr ps)))
2309                                   (list (nreverse outp))))
2310                                outt))
2311                         (setq ts (cdr ts)))
2312                       (list (nreverse outt))))
2313                    outh))
2314             (setq headers (cdr headers)))
2315           (list (nreverse outh))))))))
2316
2317 \f
2318
2319 (defun gnus-summary-mode (&optional group)
2320   "Major mode for reading articles.
2321
2322 All normal editing commands are switched off.
2323 \\<gnus-summary-mode-map>
2324 Each line in this buffer represents one article.  To read an
2325 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2326 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2327 respectively.
2328
2329 You can also post articles and send mail from this buffer.  To
2330 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2331 of an article, type `\\[gnus-summary-reply]'.
2332
2333 There are approx. one gazillion commands you can execute in this
2334 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2335
2336 The following commands are available:
2337
2338 \\{gnus-summary-mode-map}"
2339   (interactive)
2340   (kill-all-local-variables)
2341   (when (gnus-visual-p 'summary-menu 'menu)
2342     (gnus-summary-make-menu-bar)
2343     (gnus-summary-make-tool-bar))
2344   (gnus-summary-make-local-variables)
2345   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2346     (gnus-summary-make-local-variables))
2347   (gnus-make-thread-indent-array)
2348   (gnus-simplify-mode-line)
2349   (setq major-mode 'gnus-summary-mode)
2350   (setq mode-name "Summary")
2351   (make-local-variable 'minor-mode-alist)
2352   (use-local-map gnus-summary-mode-map)
2353   (buffer-disable-undo)
2354   (setq buffer-read-only t)             ;Disable modification
2355   (setq truncate-lines t)
2356   (setq selective-display t)
2357   (setq selective-display-ellipses t)   ;Display `...'
2358   (gnus-summary-set-display-table)
2359   (gnus-set-default-directory)
2360   (setq gnus-newsgroup-name group)
2361   (unless (gnus-news-group-p group)
2362     (setq gnus-newsgroup-incorporated
2363           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2364   (make-local-variable 'gnus-summary-line-format)
2365   (make-local-variable 'gnus-summary-line-format-spec)
2366   (make-local-variable 'gnus-summary-dummy-line-format)
2367   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2368   (make-local-variable 'gnus-summary-mark-positions)
2369   (make-local-hook 'pre-command-hook)
2370   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2371   (gnus-run-hooks 'gnus-summary-mode-hook)
2372   (turn-on-gnus-mailing-list-mode)
2373   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2374   (gnus-update-summary-mark-positions))
2375
2376 (defun gnus-summary-make-local-variables ()
2377   "Make all the local summary buffer variables."
2378   (let (global)
2379     (dolist (local gnus-summary-local-variables)
2380       (if (consp local)
2381           (progn
2382             (if (eq (cdr local) 'global)
2383                 ;; Copy the global value of the variable.
2384                 (setq global (symbol-value (car local)))
2385               ;; Use the value from the list.
2386               (setq global (eval (cdr local))))
2387             (set (make-local-variable (car local)) global))
2388         ;; Simple nil-valued local variable.
2389         (set (make-local-variable local) nil)))))
2390
2391 (defun gnus-summary-clear-local-variables ()
2392   (let ((locals gnus-summary-local-variables))
2393     (while locals
2394       (if (consp (car locals))
2395           (and (vectorp (caar locals))
2396                (set (caar locals) nil))
2397         (and (vectorp (car locals))
2398              (set (car locals) nil)))
2399       (setq locals (cdr locals)))))
2400
2401 ;; Summary data functions.
2402
2403 (defmacro gnus-data-number (data)
2404   `(car ,data))
2405
2406 (defmacro gnus-data-set-number (data number)
2407   `(setcar ,data ,number))
2408
2409 (defmacro gnus-data-mark (data)
2410   `(nth 1 ,data))
2411
2412 (defmacro gnus-data-set-mark (data mark)
2413   `(setcar (nthcdr 1 ,data) ,mark))
2414
2415 (defmacro gnus-data-pos (data)
2416   `(nth 2 ,data))
2417
2418 (defmacro gnus-data-set-pos (data pos)
2419   `(setcar (nthcdr 2 ,data) ,pos))
2420
2421 (defmacro gnus-data-header (data)
2422   `(nth 3 ,data))
2423
2424 (defmacro gnus-data-set-header (data header)
2425   `(setcar (nthcdr 3 ,data) ,header))
2426
2427 (defmacro gnus-data-level (data)
2428   `(nth 4 ,data))
2429
2430 (defmacro gnus-data-unread-p (data)
2431   `(= (nth 1 ,data) gnus-unread-mark))
2432
2433 (defmacro gnus-data-read-p (data)
2434   `(/= (nth 1 ,data) gnus-unread-mark))
2435
2436 (defmacro gnus-data-pseudo-p (data)
2437   `(consp (nth 3 ,data)))
2438
2439 (defmacro gnus-data-find (number)
2440   `(assq ,number gnus-newsgroup-data))
2441
2442 (defmacro gnus-data-find-list (number &optional data)
2443   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2444      (memq (assq ,number bdata)
2445            bdata)))
2446
2447 (defmacro gnus-data-make (number mark pos header level)
2448   `(list ,number ,mark ,pos ,header ,level))
2449
2450 (defun gnus-data-enter (after-article number mark pos header level offset)
2451   (let ((data (gnus-data-find-list after-article)))
2452     (unless data
2453       (error "No such article: %d" after-article))
2454     (setcdr data (cons (gnus-data-make number mark pos header level)
2455                        (cdr data)))
2456     (setq gnus-newsgroup-data-reverse nil)
2457     (gnus-data-update-list (cddr data) offset)))
2458
2459 (defun gnus-data-enter-list (after-article list &optional offset)
2460   (when list
2461     (let ((data (and after-article (gnus-data-find-list after-article)))
2462           (ilist list))
2463       (if (not (or data
2464                    after-article))
2465           (let ((odata gnus-newsgroup-data))
2466             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2467             (when offset
2468               (gnus-data-update-list odata offset)))
2469         ;; Find the last element in the list to be spliced into the main
2470         ;; list.
2471         (while (cdr list)
2472           (setq list (cdr list)))
2473         (if (not data)
2474             (progn
2475               (setcdr list gnus-newsgroup-data)
2476               (setq gnus-newsgroup-data ilist)
2477               (when offset
2478                 (gnus-data-update-list (cdr list) offset)))
2479           (setcdr list (cdr data))
2480           (setcdr data ilist)
2481           (when offset
2482             (gnus-data-update-list (cdr list) offset))))
2483       (setq gnus-newsgroup-data-reverse nil))))
2484
2485 (defun gnus-data-remove (article &optional offset)
2486   (let ((data gnus-newsgroup-data))
2487     (if (= (gnus-data-number (car data)) article)
2488         (progn
2489           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2490                 gnus-newsgroup-data-reverse nil)
2491           (when offset
2492             (gnus-data-update-list gnus-newsgroup-data offset)))
2493       (while (cdr data)
2494         (when (= (gnus-data-number (cadr data)) article)
2495           (setcdr data (cddr data))
2496           (when offset
2497             (gnus-data-update-list (cdr data) offset))
2498           (setq data nil
2499                 gnus-newsgroup-data-reverse nil))
2500         (setq data (cdr data))))))
2501
2502 (defmacro gnus-data-list (backward)
2503   `(if ,backward
2504        (or gnus-newsgroup-data-reverse
2505            (setq gnus-newsgroup-data-reverse
2506                  (reverse gnus-newsgroup-data)))
2507      gnus-newsgroup-data))
2508
2509 (defun gnus-data-update-list (data offset)
2510   "Add OFFSET to the POS of all data entries in DATA."
2511   (setq gnus-newsgroup-data-reverse nil)
2512   (while data
2513     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2514     (setq data (cdr data))))
2515
2516 (defun gnus-summary-article-pseudo-p (article)
2517   "Say whether this article is a pseudo article or not."
2518   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2519
2520 (defmacro gnus-summary-article-sparse-p (article)
2521   "Say whether this article is a sparse article or not."
2522   `(memq ,article gnus-newsgroup-sparse))
2523
2524 (defmacro gnus-summary-article-ancient-p (article)
2525   "Say whether this article is a sparse article or not."
2526   `(memq ,article gnus-newsgroup-ancient))
2527
2528 (defun gnus-article-parent-p (number)
2529   "Say whether this article is a parent or not."
2530   (let ((data (gnus-data-find-list number)))
2531     (and (cdr data)                     ; There has to be an article after...
2532          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2533             (gnus-data-level (nth 1 data))))))
2534
2535 (defun gnus-article-children (number)
2536   "Return a list of all children to NUMBER."
2537   (let* ((data (gnus-data-find-list number))
2538          (level (gnus-data-level (car data)))
2539          children)
2540     (setq data (cdr data))
2541     (while (and data
2542                 (= (gnus-data-level (car data)) (1+ level)))
2543       (push (gnus-data-number (car data)) children)
2544       (setq data (cdr data)))
2545     children))
2546
2547 (defmacro gnus-summary-skip-intangible ()
2548   "If the current article is intangible, then jump to a different article."
2549   '(let ((to (get-text-property (point) 'gnus-intangible)))
2550      (and to (gnus-summary-goto-subject to))))
2551
2552 (defmacro gnus-summary-article-intangible-p ()
2553   "Say whether this article is intangible or not."
2554   '(get-text-property (point) 'gnus-intangible))
2555
2556 (defun gnus-article-read-p (article)
2557   "Say whether ARTICLE is read or not."
2558   (not (or (memq article gnus-newsgroup-marked)
2559            (memq article gnus-newsgroup-unreads)
2560            (memq article gnus-newsgroup-unselected)
2561            (memq article gnus-newsgroup-dormant))))
2562
2563 ;; Some summary mode macros.
2564
2565 (defmacro gnus-summary-article-number ()
2566   "The article number of the article on the current line.
2567 If there isn's an article number here, then we return the current
2568 article number."
2569   '(progn
2570      (gnus-summary-skip-intangible)
2571      (or (get-text-property (point) 'gnus-number)
2572          (gnus-summary-last-subject))))
2573
2574 (defmacro gnus-summary-article-header (&optional number)
2575   "Return the header of article NUMBER."
2576   `(gnus-data-header (gnus-data-find
2577                       ,(or number '(gnus-summary-article-number)))))
2578
2579 (defmacro gnus-summary-thread-level (&optional number)
2580   "Return the level of thread that starts with article NUMBER."
2581   `(if (and (eq gnus-summary-make-false-root 'dummy)
2582             (get-text-property (point) 'gnus-intangible))
2583        0
2584      (gnus-data-level (gnus-data-find
2585                        ,(or number '(gnus-summary-article-number))))))
2586
2587 (defmacro gnus-summary-article-mark (&optional number)
2588   "Return the mark of article NUMBER."
2589   `(gnus-data-mark (gnus-data-find
2590                     ,(or number '(gnus-summary-article-number)))))
2591
2592 (defmacro gnus-summary-article-pos (&optional number)
2593   "Return the position of the line of article NUMBER."
2594   `(gnus-data-pos (gnus-data-find
2595                    ,(or number '(gnus-summary-article-number)))))
2596
2597 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2598 (defmacro gnus-summary-article-subject (&optional number)
2599   "Return current subject string or nil if nothing."
2600   `(let ((headers
2601           ,(if number
2602                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2603              '(gnus-data-header (assq (gnus-summary-article-number)
2604                                       gnus-newsgroup-data)))))
2605      (and headers
2606           (vectorp headers)
2607           (mail-header-subject headers))))
2608
2609 (defmacro gnus-summary-article-score (&optional number)
2610   "Return current article score."
2611   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2612                   gnus-newsgroup-scored))
2613        gnus-summary-default-score 0))
2614
2615 (defun gnus-summary-article-children (&optional number)
2616   "Return a list of article numbers that are children of article NUMBER."
2617   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2618          (level (gnus-data-level (car data)))
2619          l children)
2620     (while (and (setq data (cdr data))
2621                 (> (setq l (gnus-data-level (car data))) level))
2622       (and (= (1+ level) l)
2623            (push (gnus-data-number (car data))
2624                  children)))
2625     (nreverse children)))
2626
2627 (defun gnus-summary-article-parent (&optional number)
2628   "Return the article number of the parent of article NUMBER."
2629   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2630                                     (gnus-data-list t)))
2631          (level (gnus-data-level (car data))))
2632     (if (zerop level)
2633         ()                              ; This is a root.
2634       ;; We search until we find an article with a level less than
2635       ;; this one.  That function has to be the parent.
2636       (while (and (setq data (cdr data))
2637                   (not (< (gnus-data-level (car data)) level))))
2638       (and data (gnus-data-number (car data))))))
2639
2640 (defun gnus-unread-mark-p (mark)
2641   "Say whether MARK is the unread mark."
2642   (= mark gnus-unread-mark))
2643
2644 (defun gnus-read-mark-p (mark)
2645   "Say whether MARK is one of the marks that mark as read.
2646 This is all marks except unread, ticked, dormant, and expirable."
2647   (not (or (= mark gnus-unread-mark)
2648            (= mark gnus-ticked-mark)
2649            (= mark gnus-dormant-mark)
2650            (= mark gnus-expirable-mark))))
2651
2652 (defmacro gnus-article-mark (number)
2653   "Return the MARK of article NUMBER.
2654 This macro should only be used when computing the mark the \"first\"
2655 time; i.e., when generating the summary lines.  After that,
2656 `gnus-summary-article-mark' should be used to examine the
2657 marks of articles."
2658   `(cond
2659     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2660     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2661     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2662     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2663     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2664     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2665     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2666     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2667            gnus-ancient-mark))))
2668
2669 ;; Saving hidden threads.
2670
2671 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2672 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2673
2674 (defmacro gnus-save-hidden-threads (&rest forms)
2675   "Save hidden threads, eval FORMS, and restore the hidden threads."
2676   (let ((config (make-symbol "config")))
2677     `(let ((,config (gnus-hidden-threads-configuration)))
2678        (unwind-protect
2679            (save-excursion
2680              ,@forms)
2681          (gnus-restore-hidden-threads-configuration ,config)))))
2682
2683 (defun gnus-data-compute-positions ()
2684   "Compute the positions of all articles."
2685   (setq gnus-newsgroup-data-reverse nil)
2686   (let ((data gnus-newsgroup-data))
2687     (save-excursion
2688       (gnus-save-hidden-threads
2689         (gnus-summary-show-all-threads)
2690         (goto-char (point-min))
2691         (while data
2692           (while (get-text-property (point) 'gnus-intangible)
2693             (forward-line 1))
2694           (gnus-data-set-pos (car data) (+ (point) 3))
2695           (setq data (cdr data))
2696           (forward-line 1))))))
2697
2698 (defun gnus-hidden-threads-configuration ()
2699   "Return the current hidden threads configuration."
2700   (save-excursion
2701     (let (config)
2702       (goto-char (point-min))
2703       (while (search-forward "\r" nil t)
2704         (push (1- (point)) config))
2705       config)))
2706
2707 (defun gnus-restore-hidden-threads-configuration (config)
2708   "Restore hidden threads configuration from CONFIG."
2709   (save-excursion
2710     (let (point buffer-read-only)
2711       (while (setq point (pop config))
2712         (when (and (< point (point-max))
2713                    (goto-char point)
2714                    (eq (char-after) ?\n))
2715           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2716
2717 ;; Various summary mode internalish functions.
2718
2719 (defun gnus-mouse-pick-article (e)
2720   (interactive "e")
2721   (mouse-set-point e)
2722   (gnus-summary-next-page nil t))
2723
2724 (defun gnus-summary-set-display-table ()
2725   "Change the display table.
2726 Odd characters have a tendency to mess
2727 up nicely formatted displays - we make all possible glyphs
2728 display only a single character."
2729
2730   ;; We start from the standard display table, if any.
2731   (let ((table (or (copy-sequence standard-display-table)
2732                    (make-display-table)))
2733         (i 32))
2734     ;; Nix out all the control chars...
2735     (while (>= (setq i (1- i)) 0)
2736       (aset table i [??]))
2737     ;; ... but not newline and cr, of course.  (cr is necessary for the
2738     ;; selective display).
2739     (aset table ?\n nil)
2740     (aset table ?\r nil)
2741     ;; We keep TAB as well.
2742     (aset table ?\t nil)
2743     ;; We nix out any glyphs over 126 that are not set already.
2744     (let ((i 256))
2745       (while (>= (setq i (1- i)) 127)
2746         ;; Only modify if the entry is nil.
2747         (unless (aref table i)
2748           (aset table i [??]))))
2749     (setq buffer-display-table table)))
2750
2751 (defun gnus-summary-set-article-display-arrow (pos)
2752   "Update the overlay arrow to point to line at position POS."
2753   (when (and gnus-summary-display-arrow
2754              (boundp 'overlay-arrow-position)
2755              (boundp 'overlay-arrow-string))
2756     (save-excursion
2757       (goto-char pos)
2758       (beginning-of-line)
2759       (unless overlay-arrow-position
2760         (setq overlay-arrow-position (make-marker)))
2761       (setq overlay-arrow-string "=>"
2762             overlay-arrow-position (set-marker overlay-arrow-position
2763                                                (point)
2764                                                (current-buffer))))))
2765
2766 (defun gnus-summary-buffer-name (group)
2767   "Return the summary buffer name of GROUP."
2768   (concat "*Summary " group "*"))
2769
2770 (defun gnus-summary-setup-buffer (group)
2771   "Initialize summary buffer."
2772   (let ((buffer (gnus-summary-buffer-name group)))
2773     (if (get-buffer buffer)
2774         (progn
2775           (set-buffer buffer)
2776           (setq gnus-summary-buffer (current-buffer))
2777           (not gnus-newsgroup-prepared))
2778       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2779       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2780       (gnus-summary-mode group)
2781       (when gnus-carpal
2782         (gnus-carpal-setup-buffer 'summary))
2783       (unless gnus-single-article-buffer
2784         (make-local-variable 'gnus-article-buffer)
2785         (make-local-variable 'gnus-article-current)
2786         (make-local-variable 'gnus-original-article-buffer))
2787       (setq gnus-newsgroup-name group)
2788       ;; Set any local variables in the group parameters.
2789       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2790       t)))
2791
2792 (defun gnus-set-global-variables ()
2793   "Set the global equivalents of the buffer-local variables.
2794 They are set to the latest values they had.  These reflect the summary
2795 buffer that was in action when the last article was fetched."
2796   (when (eq major-mode 'gnus-summary-mode)
2797     (setq gnus-summary-buffer (current-buffer))
2798     (let ((name gnus-newsgroup-name)
2799           (marked gnus-newsgroup-marked)
2800           (unread gnus-newsgroup-unreads)
2801           (headers gnus-current-headers)
2802           (data gnus-newsgroup-data)
2803           (summary gnus-summary-buffer)
2804           (article-buffer gnus-article-buffer)
2805           (original gnus-original-article-buffer)
2806           (gac gnus-article-current)
2807           (reffed gnus-reffed-article-number)
2808           (score-file gnus-current-score-file)
2809           (default-charset gnus-newsgroup-charset)
2810           vlist)
2811       (let ((locals gnus-newsgroup-variables))
2812         (while locals
2813           (if (consp (car locals))
2814               (push (eval (caar locals)) vlist)
2815             (push (eval (car locals)) vlist))
2816           (setq locals (cdr locals)))
2817         (setq vlist (nreverse vlist)))
2818       (save-excursion
2819         (set-buffer gnus-group-buffer)
2820         (setq gnus-newsgroup-name name
2821               gnus-newsgroup-marked marked
2822               gnus-newsgroup-unreads unread
2823               gnus-current-headers headers
2824               gnus-newsgroup-data data
2825               gnus-article-current gac
2826               gnus-summary-buffer summary
2827               gnus-article-buffer article-buffer
2828               gnus-original-article-buffer original
2829               gnus-reffed-article-number reffed
2830               gnus-current-score-file score-file
2831               gnus-newsgroup-charset default-charset)
2832         (let ((locals gnus-newsgroup-variables))
2833           (while locals
2834             (if (consp (car locals))
2835                 (set (caar locals) (pop vlist))
2836               (set (car locals) (pop vlist)))
2837             (setq locals (cdr locals))))
2838         ;; The article buffer also has local variables.
2839         (when (gnus-buffer-live-p gnus-article-buffer)
2840           (set-buffer gnus-article-buffer)
2841           (setq gnus-summary-buffer summary))))))
2842
2843 (defun gnus-summary-article-unread-p (article)
2844   "Say whether ARTICLE is unread or not."
2845   (memq article gnus-newsgroup-unreads))
2846
2847 (defun gnus-summary-first-article-p (&optional article)
2848   "Return whether ARTICLE is the first article in the buffer."
2849   (if (not (setq article (or article (gnus-summary-article-number))))
2850       nil
2851     (eq article (caar gnus-newsgroup-data))))
2852
2853 (defun gnus-summary-last-article-p (&optional article)
2854   "Return whether ARTICLE is the last article in the buffer."
2855   (if (not (setq article (or article (gnus-summary-article-number))))
2856       ;; All non-existent numbers are the last article.  :-)
2857       t
2858     (not (cdr (gnus-data-find-list article)))))
2859
2860 (defun gnus-make-thread-indent-array ()
2861   (let ((n 200))
2862     (unless (and gnus-thread-indent-array
2863                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2864       (setq gnus-thread-indent-array (make-vector 201 "")
2865             gnus-thread-indent-array-level gnus-thread-indent-level)
2866       (while (>= n 0)
2867         (aset gnus-thread-indent-array n
2868               (make-string (* n gnus-thread-indent-level) ? ))
2869         (setq n (1- n))))))
2870
2871 (defun gnus-update-summary-mark-positions ()
2872   "Compute where the summary marks are to go."
2873   (save-excursion
2874     (when (gnus-buffer-exists-p gnus-summary-buffer)
2875       (set-buffer gnus-summary-buffer))
2876     (let ((gnus-replied-mark 129)
2877           (gnus-score-below-mark 130)
2878           (gnus-score-over-mark 130)
2879           (gnus-download-mark 131)
2880           (spec gnus-summary-line-format-spec)
2881           gnus-visual pos)
2882       (save-excursion
2883         (gnus-set-work-buffer)
2884         (let ((gnus-summary-line-format-spec spec)
2885               (gnus-newsgroup-downloadable '((0 . t))))
2886           (gnus-summary-insert-line
2887            (make-full-mail-header 0 "" "nobody"
2888                                   "05 Apr 2001 23:33:09 +0400"
2889                                   "" "" 0 0 "" nil)
2890            0 nil 128 t nil "" nil 1)
2891           (goto-char (point-min))
2892           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2893                                              (- (point) 2)))))
2894           (goto-char (point-min))
2895           (push (cons 'replied (and (search-forward "\201" nil t)
2896                                     (- (point) 2)))
2897                 pos)
2898           (goto-char (point-min))
2899           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2900                 pos)
2901           (goto-char (point-min))
2902           (push (cons 'download
2903                       (and (search-forward "\203" nil t) (- (point) 2)))
2904                 pos)))
2905       (setq gnus-summary-mark-positions pos))))
2906
2907 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2908   "Insert a dummy root in the summary buffer."
2909   (beginning-of-line)
2910   (gnus-add-text-properties
2911    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2912    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2913
2914 (defun gnus-summary-from-or-to-or-newsgroups (header)
2915   (let ((to (cdr (assq 'To (mail-header-extra header))))
2916         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2917         (default-mime-charset (with-current-buffer gnus-summary-buffer
2918                                 default-mime-charset)))
2919     (cond
2920      ((and to
2921            gnus-ignored-from-addresses
2922            (string-match gnus-ignored-from-addresses
2923                          (mail-header-from header)))
2924       (concat "-> "
2925               (or (car (funcall gnus-extract-address-components
2926                                 (funcall
2927                                  gnus-decode-encoded-word-function to)))
2928                   (funcall gnus-decode-encoded-word-function to))))
2929      ((and newsgroups
2930            gnus-ignored-from-addresses
2931            (string-match gnus-ignored-from-addresses
2932                          (mail-header-from header)))
2933       (concat "=> " newsgroups))
2934      (t
2935       (or (car (funcall gnus-extract-address-components
2936                         (mail-header-from header)))
2937           (mail-header-from header))))))
2938
2939 (defun gnus-summary-insert-line (gnus-tmp-header
2940                                  gnus-tmp-level gnus-tmp-current
2941                                  gnus-tmp-unread gnus-tmp-replied
2942                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2943                                  &optional gnus-tmp-dummy gnus-tmp-score
2944                                  gnus-tmp-process)
2945   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2946          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2947          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2948          (gnus-tmp-score-char
2949           (if (or (null gnus-summary-default-score)
2950                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2951                       gnus-summary-zcore-fuzz))
2952               ?\ ;;;Whitespace
2953             (if (< gnus-tmp-score gnus-summary-default-score)
2954                 gnus-score-below-mark gnus-score-over-mark)))
2955          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2956          (gnus-tmp-replied
2957           (cond (gnus-tmp-process gnus-process-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2959                  gnus-cached-mark)
2960                 (gnus-tmp-replied gnus-replied-mark)
2961                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2962                  gnus-forwarded-mark)
2963                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2964                  gnus-saved-mark)
2965                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2966                  gnus-recent-mark)
2967                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2968                  gnus-unseen-mark)
2969                 (t gnus-no-mark)))
2970          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2971          (gnus-tmp-name
2972           (cond
2973            ((string-match "<[^>]+> *$" gnus-tmp-from)
2974             (let ((beg (match-beginning 0)))
2975               (or (and (string-match "^\".+\"" gnus-tmp-from)
2976                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2977                   (substring gnus-tmp-from 0 beg))))
2978            ((string-match "(.+)" gnus-tmp-from)
2979             (substring gnus-tmp-from
2980                        (1+ (match-beginning 0)) (1- (match-end 0))))
2981            (t gnus-tmp-from)))
2982          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2983          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2984          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2985          (buffer-read-only nil))
2986     (when (string= gnus-tmp-name "")
2987       (setq gnus-tmp-name gnus-tmp-from))
2988     (unless (numberp gnus-tmp-lines)
2989       (setq gnus-tmp-lines -1))
2990     (if (= gnus-tmp-lines -1)
2991         (setq gnus-tmp-lines "?")
2992       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2993     (gnus-put-text-property-excluding-characters-with-faces
2994      (point)
2995      (progn (eval gnus-summary-line-format-spec) (point))
2996      'gnus-number gnus-tmp-number)
2997     (when (gnus-visual-p 'summary-highlight 'highlight)
2998       (forward-line -1)
2999       (gnus-run-hooks 'gnus-summary-update-hook)
3000       (forward-line 1))))
3001
3002 (defun gnus-summary-update-line (&optional dont-update)
3003   "Update summary line after change."
3004   (when (and gnus-summary-default-score
3005              (not gnus-summary-inhibit-highlight))
3006     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3007            (article (gnus-summary-article-number))
3008            (score (gnus-summary-article-score article)))
3009       (unless dont-update
3010         (if (and gnus-summary-mark-below
3011                  (< (gnus-summary-article-score)
3012                     gnus-summary-mark-below))
3013             ;; This article has a low score, so we mark it as read.
3014             (when (memq article gnus-newsgroup-unreads)
3015               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3016           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3017             ;; This article was previously marked as read on account
3018             ;; of a low score, but now it has risen, so we mark it as
3019             ;; unread.
3020             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3021         (gnus-summary-update-mark
3022          (if (or (null gnus-summary-default-score)
3023                  (<= (abs (- score gnus-summary-default-score))
3024                      gnus-summary-zcore-fuzz))
3025              ?\ ;;;Whitespace
3026            (if (< score gnus-summary-default-score)
3027                gnus-score-below-mark gnus-score-over-mark))
3028          'score))
3029       ;; Do visual highlighting.
3030       (when (gnus-visual-p 'summary-highlight 'highlight)
3031         (gnus-run-hooks 'gnus-summary-update-hook)))))
3032
3033 (defvar gnus-tmp-new-adopts nil)
3034
3035 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3036   "Return the number of articles in THREAD.
3037 This may be 0 in some cases -- if none of the articles in
3038 the thread are to be displayed."
3039   (let* ((number
3040           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3041           (cond
3042            ((not (listp thread))
3043             1)
3044            ((and (consp thread) (cdr thread))
3045             (apply
3046              '+ 1 (mapcar
3047                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3048            ((null thread)
3049             1)
3050            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3051             1)
3052            (t 0))))
3053     (when (and level (zerop level) gnus-tmp-new-adopts)
3054       (incf number
3055             (apply '+ (mapcar
3056                        'gnus-summary-number-of-articles-in-thread
3057                        gnus-tmp-new-adopts))))
3058     (if char
3059         (if (> number 1) gnus-not-empty-thread-mark
3060           gnus-empty-thread-mark)
3061       number)))
3062
3063 (defun gnus-summary-set-local-parameters (group)
3064   "Go through the local params of GROUP and set all variable specs in that list."
3065   (let ((params (gnus-group-find-parameter group))
3066         (vars '(quit-config))           ; Ignore quit-config.
3067         elem)
3068     (while params
3069       (setq elem (car params)
3070             params (cdr params))
3071       (and (consp elem)                 ; Has to be a cons.
3072            (consp (cdr elem))           ; The cdr has to be a list.
3073            (symbolp (car elem))         ; Has to be a symbol in there.
3074            (not (memq (car elem) vars))
3075            (ignore-errors               ; So we set it.
3076              (push (car elem) vars)
3077              (make-local-variable (car elem))
3078              (set (car elem) (eval (nth 1 elem))))))))
3079
3080 (defun gnus-summary-read-group (group &optional show-all no-article
3081                                       kill-buffer no-display backward
3082                                       select-articles)
3083   "Start reading news in newsgroup GROUP.
3084 If SHOW-ALL is non-nil, already read articles are also listed.
3085 If NO-ARTICLE is non-nil, no article is selected initially.
3086 If NO-DISPLAY, don't generate a summary buffer."
3087   (let (result)
3088     (while (and group
3089                 (null (setq result
3090                             (let ((gnus-auto-select-next nil))
3091                               (or (gnus-summary-read-group-1
3092                                    group show-all no-article
3093                                    kill-buffer no-display
3094                                    select-articles)
3095                                   (setq show-all nil
3096                                         select-articles nil)))))
3097                 (eq gnus-auto-select-next 'quietly))
3098       (set-buffer gnus-group-buffer)
3099       ;; The entry function called above goes to the next
3100       ;; group automatically, so we go two groups back
3101       ;; if we are searching for the previous group.
3102       (when backward
3103         (gnus-group-prev-unread-group 2))
3104       (if (not (equal group (gnus-group-group-name)))
3105           (setq group (gnus-group-group-name))
3106         (setq group nil)))
3107     result))
3108
3109 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3110   "Directly jump to the other GROUP from summary buffer.
3111 If SHOW-ALL is non-nil, already read articles are also listed."
3112   (interactive
3113    (if (eq gnus-summary-buffer (current-buffer))
3114        (list (completing-read
3115               "Group: " gnus-active-hashtb nil t
3116               (when (and gnus-newsgroup-name
3117                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3118                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3119               'gnus-group-history)
3120              current-prefix-arg)
3121      (error "%s must be invoked from a gnus summary buffer." this-command)))
3122   (unless (or (zerop (length group))
3123               (and gnus-newsgroup-name
3124                    (string-equal gnus-newsgroup-name group)))
3125     (gnus-summary-exit)
3126     (gnus-summary-read-group group show-all
3127                              gnus-dont-select-after-jump-to-other-group)))
3128
3129 (defun gnus-summary-read-group-1 (group show-all no-article
3130                                         kill-buffer no-display
3131                                         &optional select-articles)
3132   ;; Killed foreign groups can't be entered.
3133   ;;  (when (and (not (gnus-group-native-p group))
3134   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3135   ;;    (error "Dead non-native groups can't be entered"))
3136   (gnus-message 5 "Retrieving newsgroup: %s..."
3137                 (gnus-group-decoded-name group))
3138   (let* ((new-group (gnus-summary-setup-buffer group))
3139          (quit-config (gnus-group-quit-config group))
3140          (did-select (and new-group (gnus-select-newsgroup
3141                                      group show-all select-articles))))
3142     (cond
3143      ;; This summary buffer exists already, so we just select it.
3144      ((not new-group)
3145       (gnus-set-global-variables)
3146       (when kill-buffer
3147         (gnus-kill-or-deaden-summary kill-buffer))
3148       (gnus-configure-windows 'summary 'force)
3149       (gnus-set-mode-line 'summary)
3150       (gnus-summary-position-point)
3151       (message "")
3152       t)
3153      ;; We couldn't select this group.
3154      ((null did-select)
3155       (when (and (eq major-mode 'gnus-summary-mode)
3156                  (not (equal (current-buffer) kill-buffer)))
3157         (kill-buffer (current-buffer))
3158         (if (not quit-config)
3159             (progn
3160               ;; Update the info -- marks might need to be removed,
3161               ;; for instance.
3162               (gnus-summary-update-info)
3163               (set-buffer gnus-group-buffer)
3164               (gnus-group-jump-to-group group)
3165               (gnus-group-next-unread-group 1))
3166           (gnus-handle-ephemeral-exit quit-config)))
3167       (let ((grpinfo (gnus-get-info group)))
3168         (if (null (gnus-info-read grpinfo))
3169             (gnus-message 3 "Group %s contains no messages"
3170                           (gnus-group-decoded-name group))
3171           (gnus-message 3 "Can't select group")))
3172       nil)
3173      ;; The user did a `C-g' while prompting for number of articles,
3174      ;; so we exit this group.
3175      ((eq did-select 'quit)
3176       (and (eq major-mode 'gnus-summary-mode)
3177            (not (equal (current-buffer) kill-buffer))
3178            (kill-buffer (current-buffer)))
3179       (when kill-buffer
3180         (gnus-kill-or-deaden-summary kill-buffer))
3181       (if (not quit-config)
3182           (progn
3183             (set-buffer gnus-group-buffer)
3184             (gnus-group-jump-to-group group)
3185             (gnus-group-next-unread-group 1)
3186             (gnus-configure-windows 'group 'force))
3187         (gnus-handle-ephemeral-exit quit-config))
3188       ;; Finally signal the quit.
3189       (signal 'quit nil))
3190      ;; The group was successfully selected.
3191      (t
3192       (gnus-set-global-variables)
3193       ;; Save the active value in effect when the group was entered.
3194       (setq gnus-newsgroup-active
3195             (gnus-copy-sequence
3196              (gnus-active gnus-newsgroup-name)))
3197       ;; You can change the summary buffer in some way with this hook.
3198       (gnus-run-hooks 'gnus-select-group-hook)
3199       (gnus-update-format-specifications
3200        nil 'summary 'summary-mode 'summary-dummy)
3201       (gnus-update-summary-mark-positions)
3202       ;; Do score processing.
3203       (when gnus-use-scoring
3204         (gnus-possibly-score-headers))
3205       ;; Check whether to fill in the gaps in the threads.
3206       (when gnus-build-sparse-threads
3207         (gnus-build-sparse-threads))
3208       ;; Find the initial limit.
3209       (if gnus-show-threads
3210           (if show-all
3211               (let ((gnus-newsgroup-dormant nil))
3212                 (gnus-summary-initial-limit show-all))
3213             (gnus-summary-initial-limit show-all))
3214         ;; When untreaded, all articles are always shown.
3215         (setq gnus-newsgroup-limit
3216               (mapcar
3217                (lambda (header) (mail-header-number header))
3218                gnus-newsgroup-headers)))
3219       ;; Generate the summary buffer.
3220       (unless no-display
3221         (gnus-summary-prepare))
3222       (when gnus-use-trees
3223         (gnus-tree-open group)
3224         (setq gnus-summary-highlight-line-function
3225               'gnus-tree-highlight-article))
3226       ;; If the summary buffer is empty, but there are some low-scored
3227       ;; articles or some excluded dormants, we include these in the
3228       ;; buffer.
3229       (when (and (zerop (buffer-size))
3230                  (not no-display))
3231         (cond (gnus-newsgroup-dormant
3232                (gnus-summary-limit-include-dormant))
3233               ((and gnus-newsgroup-scored show-all)
3234                (gnus-summary-limit-include-expunged t))))
3235       ;; Function `gnus-apply-kill-file' must be called in this hook.
3236       (gnus-run-hooks 'gnus-apply-kill-hook)
3237       (if (and (zerop (buffer-size))
3238                (not no-display))
3239           (progn
3240             ;; This newsgroup is empty.
3241             (gnus-summary-catchup-and-exit nil t)
3242             (gnus-message 6 "No unread news")
3243             (when kill-buffer
3244               (gnus-kill-or-deaden-summary kill-buffer))
3245             ;; Return nil from this function.
3246             nil)
3247         ;; Hide conversation thread subtrees.  We cannot do this in
3248         ;; gnus-summary-prepare-hook since kill processing may not
3249         ;; work with hidden articles.
3250         (and gnus-show-threads
3251              gnus-thread-hide-subtree
3252              (gnus-summary-hide-all-threads))
3253         (when kill-buffer
3254           (gnus-kill-or-deaden-summary kill-buffer))
3255         ;; Show first unread article if requested.
3256         (if (and (not no-article)
3257                  (not no-display)
3258                  gnus-newsgroup-unreads
3259                  gnus-auto-select-first)
3260             (progn
3261               (gnus-configure-windows 'summary)
3262               (cond
3263                ((eq gnus-auto-select-first 'best)
3264                 (gnus-summary-best-unread-article))
3265                ((eq gnus-auto-select-first t)
3266                 (gnus-summary-first-unread-article))
3267                ((gnus-functionp gnus-auto-select-first)
3268                 (funcall gnus-auto-select-first))))
3269           ;; Don't select any articles, just move point to the first
3270           ;; article in the group.
3271           (goto-char (point-min))
3272           (gnus-summary-position-point)
3273           (gnus-configure-windows 'summary 'force)
3274           (gnus-set-mode-line 'summary))
3275         (when (get-buffer-window gnus-group-buffer t)
3276           ;; Gotta use windows, because recenter does weird stuff if
3277           ;; the current buffer ain't the displayed window.
3278           (let ((owin (selected-window)))
3279             (select-window (get-buffer-window gnus-group-buffer t))
3280             (when (gnus-group-goto-group group)
3281               (recenter))
3282             (select-window owin)))
3283         ;; Mark this buffer as "prepared".
3284         (setq gnus-newsgroup-prepared t)
3285         (gnus-run-hooks 'gnus-summary-prepared-hook)
3286         t)))))
3287
3288 (defun gnus-summary-prepare ()
3289   "Generate the summary buffer."
3290   (interactive)
3291   (let ((buffer-read-only nil))
3292     (erase-buffer)
3293     (setq gnus-newsgroup-data nil
3294           gnus-newsgroup-data-reverse nil)
3295     (gnus-run-hooks 'gnus-summary-generate-hook)
3296     ;; Generate the buffer, either with threads or without.
3297     (when gnus-newsgroup-headers
3298       (gnus-summary-prepare-threads
3299        (if gnus-show-threads
3300            (gnus-sort-gathered-threads
3301             (funcall gnus-summary-thread-gathering-function
3302                      (gnus-sort-threads
3303                       (gnus-cut-threads (gnus-make-threads)))))
3304          ;; Unthreaded display.
3305          (gnus-sort-articles gnus-newsgroup-headers))))
3306     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3307     ;; Call hooks for modifying summary buffer.
3308     (goto-char (point-min))
3309     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3310
3311 (defsubst gnus-general-simplify-subject (subject)
3312   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3313   (setq subject
3314         (cond
3315          ;; Truncate the subject.
3316          (gnus-simplify-subject-functions
3317           (gnus-map-function gnus-simplify-subject-functions subject))
3318          ((numberp gnus-summary-gather-subject-limit)
3319           (setq subject (gnus-simplify-subject-re subject))
3320           (if (> (length subject) gnus-summary-gather-subject-limit)
3321               (substring subject 0 gnus-summary-gather-subject-limit)
3322             subject))
3323          ;; Fuzzily simplify it.
3324          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3325           (gnus-simplify-subject-fuzzy subject))
3326          ;; Just remove the leading "Re:".
3327          (t
3328           (gnus-simplify-subject-re subject))))
3329
3330   (if (and gnus-summary-gather-exclude-subject
3331            (string-match gnus-summary-gather-exclude-subject subject))
3332       nil                               ; This article shouldn't be gathered
3333     subject))
3334
3335 (defun gnus-summary-simplify-subject-query ()
3336   "Query where the respool algorithm would put this article."
3337   (interactive)
3338   (gnus-summary-select-article)
3339   (message "%s"
3340            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3341
3342 (defun gnus-gather-threads-by-subject (threads)
3343   "Gather threads by looking at Subject headers."
3344   (if (not gnus-summary-make-false-root)
3345       threads
3346     (let ((hashtb (gnus-make-hashtable 1024))
3347           (prev threads)
3348           (result threads)
3349           subject hthread whole-subject)
3350       (while threads
3351         (setq subject (gnus-general-simplify-subject
3352                        (setq whole-subject (mail-header-subject
3353                                             (caar threads)))))
3354         (when subject
3355           (if (setq hthread (gnus-gethash subject hashtb))
3356               (progn
3357                 ;; We enter a dummy root into the thread, if we
3358                 ;; haven't done that already.
3359                 (unless (stringp (caar hthread))
3360                   (setcar hthread (list whole-subject (car hthread))))
3361                 ;; We add this new gathered thread to this gathered
3362                 ;; thread.
3363                 (setcdr (car hthread)
3364                         (nconc (cdar hthread) (list (car threads))))
3365                 ;; Remove it from the list of threads.
3366                 (setcdr prev (cdr threads))
3367                 (setq threads prev))
3368             ;; Enter this thread into the hash table.
3369             (gnus-sethash subject threads hashtb)))
3370         (setq prev threads)
3371         (setq threads (cdr threads)))
3372       result)))
3373
3374 (defun gnus-gather-threads-by-references (threads)
3375   "Gather threads by looking at References headers."
3376   (let ((idhashtb (gnus-make-hashtable 1024))
3377         (thhashtb (gnus-make-hashtable 1024))
3378         (prev threads)
3379         (result threads)
3380         ids references id gthread gid entered ref)
3381     (while threads
3382       (when (setq references (mail-header-references (caar threads)))
3383         (setq id (mail-header-id (caar threads))
3384               ids (gnus-split-references references)
3385               entered nil)
3386         (while (setq ref (pop ids))
3387           (setq ids (delete ref ids))
3388           (if (not (setq gid (gnus-gethash ref idhashtb)))
3389               (progn
3390                 (gnus-sethash ref id idhashtb)
3391                 (gnus-sethash id threads thhashtb))
3392             (setq gthread (gnus-gethash gid thhashtb))
3393             (unless entered
3394               ;; We enter a dummy root into the thread, if we
3395               ;; haven't done that already.
3396               (unless (stringp (caar gthread))
3397                 (setcar gthread (list (mail-header-subject (caar gthread))
3398                                       (car gthread))))
3399               ;; We add this new gathered thread to this gathered
3400               ;; thread.
3401               (setcdr (car gthread)
3402                       (nconc (cdar gthread) (list (car threads)))))
3403             ;; Add it into the thread hash table.
3404             (gnus-sethash id gthread thhashtb)
3405             (setq entered t)
3406             ;; Remove it from the list of threads.
3407             (setcdr prev (cdr threads))
3408             (setq threads prev))))
3409       (setq prev threads)
3410       (setq threads (cdr threads)))
3411     result))
3412
3413 (defun gnus-sort-gathered-threads (threads)
3414   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3415   (let ((result threads))
3416     (while threads
3417       (when (stringp (caar threads))
3418         (setcdr (car threads)
3419                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3420       (setq threads (cdr threads)))
3421     result))
3422
3423 (defun gnus-thread-loop-p (root thread)
3424   "Say whether ROOT is in THREAD."
3425   (let ((stack (list thread))
3426         (infloop 0)
3427         th)
3428     (while (setq thread (pop stack))
3429       (setq th (cdr thread))
3430       (while (and th
3431                   (not (eq (caar th) root)))
3432         (pop th))
3433       (if th
3434           ;; We have found a loop.
3435           (let (ref-dep)
3436             (setcdr thread (delq (car th) (cdr thread)))
3437             (if (boundp (setq ref-dep (intern "none"
3438                                               gnus-newsgroup-dependencies)))
3439                 (setcdr (symbol-value ref-dep)
3440                         (nconc (cdr (symbol-value ref-dep))
3441                                (list (car th))))
3442               (set ref-dep (list nil (car th))))
3443             (setq infloop 1
3444                   stack nil))
3445         ;; Push all the subthreads onto the stack.
3446         (push (cdr thread) stack)))
3447     infloop))
3448
3449 (defun gnus-make-threads ()
3450   "Go through the dependency hashtb and find the roots.  Return all threads."
3451   (let (threads)
3452     (while (catch 'infloop
3453              (mapatoms
3454               (lambda (refs)
3455                 ;; Deal with self-referencing References loops.
3456                 (when (and (car (symbol-value refs))
3457                            (not (zerop
3458                                  (apply
3459                                   '+
3460                                   (mapcar
3461                                    (lambda (thread)
3462                                      (gnus-thread-loop-p
3463                                       (car (symbol-value refs)) thread))
3464                                    (cdr (symbol-value refs)))))))
3465                   (setq threads nil)
3466                   (throw 'infloop t))
3467                 (unless (car (symbol-value refs))
3468                   ;; These threads do not refer back to any other articles,
3469                   ;; so they're roots.
3470                   (setq threads (append (cdr (symbol-value refs)) threads))))
3471               gnus-newsgroup-dependencies)))
3472     threads))
3473
3474 ;; Build the thread tree.
3475 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3476   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3477
3478 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3479 if it was already present.
3480
3481 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3482 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3483 Message-IDs will be renamed be renamed to a unique Message-ID before
3484 being entered.
3485
3486 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3487   (let* ((id (mail-header-id header))
3488          (id-dep (and id (intern id dependencies)))
3489          ref ref-dep ref-header)
3490     ;; Enter this `header' in the `dependencies' table.
3491     (cond
3492      ((not id-dep)
3493       (setq header nil))
3494      ;; The first two cases do the normal part: enter a new `header'
3495      ;; in the `dependencies' table.
3496      ((not (boundp id-dep))
3497       (set id-dep (list header)))
3498      ((null (car (symbol-value id-dep)))
3499       (setcar (symbol-value id-dep) header))
3500
3501      ;; From here the `header' was already present in the
3502      ;; `dependencies' table.
3503      (force-new
3504       ;; Overrides an existing entry;
3505       ;; just set the header part of the entry.
3506       (setcar (symbol-value id-dep) header))
3507
3508      ;; Renames the existing `header' to a unique Message-ID.
3509      ((not gnus-summary-ignore-duplicates)
3510       ;; An article with this Message-ID has already been seen.
3511       ;; We rename the Message-ID.
3512       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3513            (list header))
3514       (mail-header-set-id header id))
3515
3516      ;; The last case ignores an existing entry, except it adds any
3517      ;; additional Xrefs (in case the two articles came from different
3518      ;; servers.
3519      ;; Also sets `header' to `nil' meaning that the `dependencies'
3520      ;; table was *not* modified.
3521      (t
3522       (mail-header-set-xref
3523        (car (symbol-value id-dep))
3524        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3525                    "")
3526                (or (mail-header-xref header) "")))
3527       (setq header nil)))
3528
3529     (when header
3530       ;; First check if that we are not creating a References loop.
3531       (setq ref (gnus-parent-id (mail-header-references header)))
3532       (while (and ref
3533                   (setq ref-dep (intern-soft ref dependencies))
3534                   (boundp ref-dep)
3535                   (setq ref-header (car (symbol-value ref-dep))))
3536         (if (string= id ref)
3537             ;; Yuk!  This is a reference loop.  Make the article be a
3538             ;; root article.
3539             (progn
3540               (mail-header-set-references (car (symbol-value id-dep)) "none")
3541               (setq ref nil))
3542           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3543       (setq ref (gnus-parent-id (mail-header-references header)))
3544       (setq ref-dep (intern (or ref "none") dependencies))
3545       (if (boundp ref-dep)
3546           (setcdr (symbol-value ref-dep)
3547                   (nconc (cdr (symbol-value ref-dep))
3548                          (list (symbol-value id-dep))))
3549         (set ref-dep (list nil (symbol-value id-dep)))))
3550     header))
3551
3552 (defun gnus-build-sparse-threads ()
3553   (let ((headers gnus-newsgroup-headers)
3554         (mail-parse-charset gnus-newsgroup-charset)
3555         (gnus-summary-ignore-duplicates t)
3556         header references generation relations
3557         subject child end new-child date)
3558     ;; First we create an alist of generations/relations, where
3559     ;; generations is how much we trust the relation, and the relation
3560     ;; is parent/child.
3561     (gnus-message 7 "Making sparse threads...")
3562     (save-excursion
3563       (nnheader-set-temp-buffer " *gnus sparse threads*")
3564       (while (setq header (pop headers))
3565         (when (and (setq references (mail-header-references header))
3566                    (not (string= references "")))
3567           (insert references)
3568           (setq child (mail-header-id header)
3569                 subject (mail-header-subject header)
3570                 date (mail-header-date header)
3571                 generation 0)
3572           (while (search-backward ">" nil t)
3573             (setq end (1+ (point)))
3574             (when (search-backward "<" nil t)
3575               (setq new-child (buffer-substring (point) end))
3576               (push (list (incf generation)
3577                           child (setq child new-child)
3578                           subject date)
3579                     relations)))
3580           (when child
3581             (push (list (1+ generation) child nil subject) relations))
3582           (erase-buffer)))
3583       (kill-buffer (current-buffer)))
3584     ;; Sort over trustworthiness.
3585     (mapcar
3586      (lambda (relation)
3587        (when (gnus-dependencies-add-header
3588               (make-full-mail-header-from-decoded-header
3589                gnus-reffed-article-number
3590                (nth 3 relation) "" (or (nth 4 relation) "")
3591                (nth 1 relation)
3592                (or (nth 2 relation) "") 0 0 "")
3593               gnus-newsgroup-dependencies nil)
3594          (push gnus-reffed-article-number gnus-newsgroup-limit)
3595          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3596          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3597                gnus-newsgroup-reads)
3598          (decf gnus-reffed-article-number)))
3599      (sort relations 'car-less-than-car))
3600     (gnus-message 7 "Making sparse threads...done")))
3601
3602 (defun gnus-build-old-threads ()
3603   ;; Look at all the articles that refer back to old articles, and
3604   ;; fetch the headers for the articles that aren't there.  This will
3605   ;; build complete threads - if the roots haven't been expired by the
3606   ;; server, that is.
3607   (let ((mail-parse-charset gnus-newsgroup-charset)
3608         id heads)
3609     (mapatoms
3610      (lambda (refs)
3611        (when (not (car (symbol-value refs)))
3612          (setq heads (cdr (symbol-value refs)))
3613          (while heads
3614            (if (memq (mail-header-number (caar heads))
3615                      gnus-newsgroup-dormant)
3616                (setq heads (cdr heads))
3617              (setq id (symbol-name refs))
3618              (while (and (setq id (gnus-build-get-header id))
3619                          (not (car (gnus-id-to-thread id)))))
3620              (setq heads nil)))))
3621      gnus-newsgroup-dependencies)))
3622
3623 ;; This function has to be called with point after the article number
3624 ;; on the beginning of the line.
3625 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3626   (let ((eol (gnus-point-at-eol))
3627         (buffer (current-buffer))
3628         header)
3629
3630     ;; overview: [num subject from date id refs chars lines misc]
3631     (unwind-protect
3632         (progn
3633           (narrow-to-region (point) eol)
3634           (unless (eobp)
3635             (forward-char))
3636
3637           (setq header
3638                 (make-full-mail-header
3639                  number                         ; number
3640                  (nnheader-nov-field)           ; subject
3641                  (nnheader-nov-field)           ; from
3642                  (nnheader-nov-field)           ; date
3643                  (nnheader-nov-read-message-id) ; id
3644                  (nnheader-nov-field)           ; refs
3645                  (nnheader-nov-read-integer)    ; chars
3646                  (nnheader-nov-read-integer)    ; lines
3647                  (unless (eobp)
3648                    (if (looking-at "Xref: ")
3649                        (goto-char (match-end 0)))
3650                    (nnheader-nov-field))        ; Xref
3651                  (nnheader-nov-parse-extra))))  ; extra
3652
3653       (widen))
3654
3655     (when gnus-alter-header-function
3656       (funcall gnus-alter-header-function header))
3657     (gnus-dependencies-add-header header dependencies force-new)))
3658
3659 (defun gnus-build-get-header (id)
3660   "Look through the buffer of NOV lines and find the header to ID.
3661 Enter this line into the dependencies hash table, and return
3662 the id of the parent article (if any)."
3663   (let ((deps gnus-newsgroup-dependencies)
3664         found header)
3665     (prog1
3666         (save-excursion
3667           (set-buffer nntp-server-buffer)
3668           (let ((case-fold-search nil))
3669             (goto-char (point-min))
3670             (while (and (not found)
3671                         (search-forward id nil t))
3672               (beginning-of-line)
3673               (setq found (looking-at
3674                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3675                                    (regexp-quote id))))
3676               (or found (beginning-of-line 2)))
3677             (when found
3678               (beginning-of-line)
3679               (and
3680                (setq header (gnus-nov-parse-line
3681                              (read (current-buffer)) deps))
3682                (gnus-parent-id (mail-header-references header))))))
3683       (when header
3684         (let ((number (mail-header-number header)))
3685           (push number gnus-newsgroup-limit)
3686           (push header gnus-newsgroup-headers)
3687           (if (memq number gnus-newsgroup-unselected)
3688               (progn
3689                 (push number gnus-newsgroup-unreads)
3690                 (setq gnus-newsgroup-unselected
3691                       (delq number gnus-newsgroup-unselected)))
3692             (push number gnus-newsgroup-ancient)))))))
3693
3694 (defun gnus-build-all-threads ()
3695   "Read all the headers."
3696   (let ((gnus-summary-ignore-duplicates t)
3697         (mail-parse-charset gnus-newsgroup-charset)
3698         (dependencies gnus-newsgroup-dependencies)
3699         header article)
3700     (save-excursion
3701       (set-buffer nntp-server-buffer)
3702       (let ((case-fold-search nil))
3703         (goto-char (point-min))
3704         (while (not (eobp))
3705           (ignore-errors
3706             (setq article (read (current-buffer))
3707                   header (gnus-nov-parse-line article dependencies)))
3708           (when header
3709             (save-excursion
3710               (set-buffer gnus-summary-buffer)
3711               (push header gnus-newsgroup-headers)
3712               (if (memq (setq article (mail-header-number header))
3713                         gnus-newsgroup-unselected)
3714                   (progn
3715                     (push article gnus-newsgroup-unreads)
3716                     (setq gnus-newsgroup-unselected
3717                           (delq article gnus-newsgroup-unselected)))
3718                 (push article gnus-newsgroup-ancient)))
3719             (forward-line 1)))))))
3720
3721 (defun gnus-summary-update-article-line (article header)
3722   "Update the line for ARTICLE using HEADERS."
3723   (let* ((id (mail-header-id header))
3724          (thread (gnus-id-to-thread id)))
3725     (unless thread
3726       (error "Article in no thread"))
3727     ;; Update the thread.
3728     (setcar thread header)
3729     (gnus-summary-goto-subject article)
3730     (let* ((datal (gnus-data-find-list article))
3731            (data (car datal))
3732            (length (when (cdr datal)
3733                      (- (gnus-data-pos data)
3734                         (gnus-data-pos (cadr datal)))))
3735            (buffer-read-only nil)
3736            (level (gnus-summary-thread-level)))
3737       (gnus-delete-line)
3738       (gnus-summary-insert-line
3739        header level nil (gnus-article-mark article)
3740        (memq article gnus-newsgroup-replied)
3741        (memq article gnus-newsgroup-expirable)
3742        ;; Only insert the Subject string when it's different
3743        ;; from the previous Subject string.
3744        (if (and
3745             gnus-show-threads
3746             (gnus-subject-equal
3747              (condition-case ()
3748                  (mail-header-subject
3749                   (gnus-data-header
3750                    (cadr
3751                     (gnus-data-find-list
3752                      article
3753                      (gnus-data-list t)))))
3754                ;; Error on the side of excessive subjects.
3755                (error ""))
3756              (mail-header-subject header)))
3757            ""
3758          (mail-header-subject header))
3759        nil (cdr (assq article gnus-newsgroup-scored))
3760        (memq article gnus-newsgroup-processable))
3761       (when length
3762         (gnus-data-update-list
3763          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3764
3765 (defun gnus-summary-update-article (article &optional iheader)
3766   "Update ARTICLE in the summary buffer."
3767   (set-buffer gnus-summary-buffer)
3768   (let* ((header (gnus-summary-article-header article))
3769          (id (mail-header-id header))
3770          (data (gnus-data-find article))
3771          (thread (gnus-id-to-thread id))
3772          (references (mail-header-references header))
3773          (parent
3774           (gnus-id-to-thread
3775            (or (gnus-parent-id
3776                 (when (and references
3777                            (not (equal "" references)))
3778                   references))
3779                "none")))
3780          (buffer-read-only nil)
3781          (old (car thread)))
3782     (when thread
3783       (unless iheader
3784         (setcar thread nil)
3785         (when parent
3786           (delq thread parent)))
3787       (if (gnus-summary-insert-subject id header)
3788           ;; Set the (possibly) new article number in the data structure.
3789           (gnus-data-set-number data (gnus-id-to-article id))
3790         (setcar thread old)
3791         nil))))
3792
3793 (defun gnus-rebuild-thread (id &optional line)
3794   "Rebuild the thread containing ID.
3795 If LINE, insert the rebuilt thread starting on line LINE."
3796   (let ((buffer-read-only nil)
3797         old-pos current thread data)
3798     (if (not gnus-show-threads)
3799         (setq thread (list (car (gnus-id-to-thread id))))
3800       ;; Get the thread this article is part of.
3801       (setq thread (gnus-remove-thread id)))
3802     (setq old-pos (gnus-point-at-bol))
3803     (setq current (save-excursion
3804                     (and (re-search-backward "[\r\n]" nil t)
3805                          (gnus-summary-article-number))))
3806     ;; If this is a gathered thread, we have to go some re-gathering.
3807     (when (stringp (car thread))
3808       (let ((subject (car thread))
3809             roots thr)
3810         (setq thread (cdr thread))
3811         (while thread
3812           (unless (memq (setq thr (gnus-id-to-thread
3813                                    (gnus-root-id
3814                                     (mail-header-id (caar thread)))))
3815                         roots)
3816             (push thr roots))
3817           (setq thread (cdr thread)))
3818         ;; We now have all (unique) roots.
3819         (if (= (length roots) 1)
3820             ;; All the loose roots are now one solid root.
3821             (setq thread (car roots))
3822           (setq thread (cons subject (gnus-sort-threads roots))))))
3823     (let (threads)
3824       ;; We then insert this thread into the summary buffer.
3825       (when line
3826         (goto-char (point-min))
3827         (forward-line (1- line)))
3828       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3829         (if gnus-show-threads
3830             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3831           (gnus-summary-prepare-unthreaded thread))
3832         (setq data (nreverse gnus-newsgroup-data))
3833         (setq threads gnus-newsgroup-threads))
3834       ;; We splice the new data into the data structure.
3835       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3836       ;;!!! then we want to insert at the beginning of the buffer.
3837       ;;!!! That happens to be true with Gnus now, but that may
3838       ;;!!! change in the future.  Perhaps.
3839       (gnus-data-enter-list
3840        (if line nil current) data (- (point) old-pos))
3841       (setq gnus-newsgroup-threads
3842             (nconc threads gnus-newsgroup-threads))
3843       (gnus-data-compute-positions))))
3844
3845 (defun gnus-number-to-header (number)
3846   "Return the header for article NUMBER."
3847   (let ((headers gnus-newsgroup-headers))
3848     (while (and headers
3849                 (not (= number (mail-header-number (car headers)))))
3850       (pop headers))
3851     (when headers
3852       (car headers))))
3853
3854 (defun gnus-parent-headers (in-headers &optional generation)
3855   "Return the headers of the GENERATIONeth parent of HEADERS."
3856   (unless generation
3857     (setq generation 1))
3858   (let ((parent t)
3859         (headers in-headers)
3860         references)
3861     (while (and parent
3862                 (not (zerop generation))
3863                 (setq references (mail-header-references headers)))
3864       (setq headers (if (and references
3865                              (setq parent (gnus-parent-id references)))
3866                         (car (gnus-id-to-thread parent))
3867                       nil))
3868       (decf generation))
3869     (and (not (eq headers in-headers))
3870          headers)))
3871
3872 (defun gnus-id-to-thread (id)
3873   "Return the (sub-)thread where ID appears."
3874   (gnus-gethash id gnus-newsgroup-dependencies))
3875
3876 (defun gnus-id-to-article (id)
3877   "Return the article number of ID."
3878   (let ((thread (gnus-id-to-thread id)))
3879     (when (and thread
3880                (car thread))
3881       (mail-header-number (car thread)))))
3882
3883 (defun gnus-id-to-header (id)
3884   "Return the article headers of ID."
3885   (car (gnus-id-to-thread id)))
3886
3887 (defun gnus-article-displayed-root-p (article)
3888   "Say whether ARTICLE is a root(ish) article."
3889   (let ((level (gnus-summary-thread-level article))
3890         (refs (mail-header-references  (gnus-summary-article-header article)))
3891         particle)
3892     (cond
3893      ((null level) nil)
3894      ((zerop level) t)
3895      ((null refs) t)
3896      ((null (gnus-parent-id refs)) t)
3897      ((and (= 1 level)
3898            (null (setq particle (gnus-id-to-article
3899                                  (gnus-parent-id refs))))
3900            (null (gnus-summary-thread-level particle)))))))
3901
3902 (defun gnus-root-id (id)
3903   "Return the id of the root of the thread where ID appears."
3904   (let (last-id prev)
3905     (while (and id (setq prev (car (gnus-id-to-thread id))))
3906       (setq last-id id
3907             id (gnus-parent-id (mail-header-references prev))))
3908     last-id))
3909
3910 (defun gnus-articles-in-thread (thread)
3911   "Return the list of articles in THREAD."
3912   (cons (mail-header-number (car thread))
3913         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3914
3915 (defun gnus-remove-thread (id &optional dont-remove)
3916   "Remove the thread that has ID in it."
3917   (let (headers thread last-id)
3918     ;; First go up in this thread until we find the root.
3919     (setq last-id (gnus-root-id id)
3920           headers (message-flatten-list (gnus-id-to-thread last-id)))
3921     ;; We have now found the real root of this thread.  It might have
3922     ;; been gathered into some loose thread, so we have to search
3923     ;; through the threads to find the thread we wanted.
3924     (let ((threads gnus-newsgroup-threads)
3925           sub)
3926       (while threads
3927         (setq sub (car threads))
3928         (if (stringp (car sub))
3929             ;; This is a gathered thread, so we look at the roots
3930             ;; below it to find whether this article is in this
3931             ;; gathered root.
3932             (progn
3933               (setq sub (cdr sub))
3934               (while sub
3935                 (when (member (caar sub) headers)
3936                   (setq thread (car threads)
3937                         threads nil
3938                         sub nil))
3939                 (setq sub (cdr sub))))
3940           ;; It's an ordinary thread, so we check it.
3941           (when (eq (car sub) (car headers))
3942             (setq thread sub
3943                   threads nil)))
3944         (setq threads (cdr threads)))
3945       ;; If this article is in no thread, then it's a root.
3946       (if thread
3947           (unless dont-remove
3948             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3949         (setq thread (gnus-id-to-thread last-id)))
3950       (when thread
3951         (prog1
3952             thread                      ; We return this thread.
3953           (unless dont-remove
3954             (if (stringp (car thread))
3955                 (progn
3956                   ;; If we use dummy roots, then we have to remove the
3957                   ;; dummy root as well.
3958                   (when (eq gnus-summary-make-false-root 'dummy)
3959                     ;; We go to the dummy root by going to
3960                     ;; the first sub-"thread", and then one line up.
3961                     (gnus-summary-goto-article
3962                      (mail-header-number (caadr thread)))
3963                     (forward-line -1)
3964                     (gnus-delete-line)
3965                     (gnus-data-compute-positions))
3966                   (setq thread (cdr thread))
3967                   (while thread
3968                     (gnus-remove-thread-1 (car thread))
3969                     (setq thread (cdr thread))))
3970               (gnus-remove-thread-1 thread))))))))
3971
3972 (defun gnus-remove-thread-1 (thread)
3973   "Remove the thread THREAD recursively."
3974   (let ((number (mail-header-number (pop thread)))
3975         d)
3976     (setq thread (reverse thread))
3977     (while thread
3978       (gnus-remove-thread-1 (pop thread)))
3979     (when (setq d (gnus-data-find number))
3980       (goto-char (gnus-data-pos d))
3981       (gnus-summary-show-thread)
3982       (gnus-data-remove
3983        number
3984        (- (gnus-point-at-bol)
3985           (prog1
3986               (1+ (gnus-point-at-eol))
3987             (gnus-delete-line)))))))
3988
3989 (defun gnus-sort-threads-1 (threads func)
3990   (sort (mapcar (lambda (thread)
3991                   (cons (car thread)
3992                         (and (cdr thread)
3993                              (gnus-sort-threads-1 (cdr thread) func))))
3994                 threads) func))
3995
3996 (defun gnus-sort-threads (threads)
3997   "Sort THREADS."
3998   (if (not gnus-thread-sort-functions)
3999       threads
4000     (gnus-message 8 "Sorting threads...")
4001     (prog1
4002         (gnus-sort-threads-1
4003          threads
4004          (gnus-make-sort-function gnus-thread-sort-functions))
4005       (gnus-message 8 "Sorting threads...done"))))
4006
4007 (defun gnus-sort-articles (articles)
4008   "Sort ARTICLES."
4009   (when gnus-article-sort-functions
4010     (gnus-message 7 "Sorting articles...")
4011     (prog1
4012         (setq gnus-newsgroup-headers
4013               (sort articles (gnus-make-sort-function
4014                               gnus-article-sort-functions)))
4015       (gnus-message 7 "Sorting articles...done"))))
4016
4017 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4018 (defmacro gnus-thread-header (thread)
4019   "Return header of first article in THREAD.
4020 Note that THREAD must never, ever be anything else than a variable -
4021 using some other form will lead to serious barfage."
4022   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4023   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4024   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4025         (vector thread) 2))
4026
4027 (defsubst gnus-article-sort-by-number (h1 h2)
4028   "Sort articles by article number."
4029   (< (mail-header-number h1)
4030      (mail-header-number h2)))
4031
4032 (defun gnus-thread-sort-by-number (h1 h2)
4033   "Sort threads by root article number."
4034   (gnus-article-sort-by-number
4035    (gnus-thread-header h1) (gnus-thread-header h2)))
4036
4037 (defsubst gnus-article-sort-by-lines (h1 h2)
4038   "Sort articles by article Lines header."
4039   (< (mail-header-lines h1)
4040      (mail-header-lines h2)))
4041
4042 (defun gnus-thread-sort-by-lines (h1 h2)
4043   "Sort threads by root article Lines header."
4044   (gnus-article-sort-by-lines
4045    (gnus-thread-header h1) (gnus-thread-header h2)))
4046
4047 (defsubst gnus-article-sort-by-chars (h1 h2)
4048   "Sort articles by octet length."
4049   (< (mail-header-chars h1)
4050      (mail-header-chars h2)))
4051
4052 (defun gnus-thread-sort-by-chars (h1 h2)
4053   "Sort threads by root article octet length."
4054   (gnus-article-sort-by-chars
4055    (gnus-thread-header h1) (gnus-thread-header h2)))
4056
4057 (defsubst gnus-article-sort-by-author (h1 h2)
4058   "Sort articles by root author."
4059   (string-lessp
4060    (let ((addr (car (mime-entity-read-field h1 'From))))
4061      (or (std11-full-name-string addr)
4062          (std11-address-string addr)
4063          ""))
4064    (let ((addr (car (mime-entity-read-field h2 'From))))
4065      (or (std11-full-name-string addr)
4066          (std11-address-string addr)
4067          ""))
4068    ))
4069
4070 (defun gnus-thread-sort-by-author (h1 h2)
4071   "Sort threads by root author."
4072   (gnus-article-sort-by-author
4073    (gnus-thread-header h1)  (gnus-thread-header h2)))
4074
4075 (defsubst gnus-article-sort-by-subject (h1 h2)
4076   "Sort articles by root subject."
4077   (string-lessp
4078    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4079    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4080
4081 (defun gnus-thread-sort-by-subject (h1 h2)
4082   "Sort threads by root subject."
4083   (gnus-article-sort-by-subject
4084    (gnus-thread-header h1) (gnus-thread-header h2)))
4085
4086 (defsubst gnus-article-sort-by-date (h1 h2)
4087   "Sort articles by root article date."
4088   (time-less-p
4089    (gnus-date-get-time (mail-header-date h1))
4090    (gnus-date-get-time (mail-header-date h2))))
4091
4092 (defun gnus-thread-sort-by-date (h1 h2)
4093   "Sort threads by root article date."
4094   (gnus-article-sort-by-date
4095    (gnus-thread-header h1) (gnus-thread-header h2)))
4096
4097 (defsubst gnus-article-sort-by-score (h1 h2)
4098   "Sort articles by root article score.
4099 Unscored articles will be counted as having a score of zero."
4100   (> (or (cdr (assq (mail-header-number h1)
4101                     gnus-newsgroup-scored))
4102          gnus-summary-default-score 0)
4103      (or (cdr (assq (mail-header-number h2)
4104                     gnus-newsgroup-scored))
4105          gnus-summary-default-score 0)))
4106
4107 (defun gnus-thread-sort-by-score (h1 h2)
4108   "Sort threads by root article score."
4109   (gnus-article-sort-by-score
4110    (gnus-thread-header h1) (gnus-thread-header h2)))
4111
4112 (defun gnus-thread-sort-by-total-score (h1 h2)
4113   "Sort threads by the sum of all scores in the thread.
4114 Unscored articles will be counted as having a score of zero."
4115   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4116
4117 (defun gnus-thread-total-score (thread)
4118   ;; This function find the total score of THREAD.
4119   (cond ((null thread)
4120          0)
4121         ((consp thread)
4122          (if (stringp (car thread))
4123              (apply gnus-thread-score-function 0
4124                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4125            (gnus-thread-total-score-1 thread)))
4126         (t
4127          (gnus-thread-total-score-1 (list thread)))))
4128
4129 (defun gnus-thread-total-score-1 (root)
4130   ;; This function find the total score of the thread below ROOT.
4131   (setq root (car root))
4132   (apply gnus-thread-score-function
4133          (or (append
4134               (mapcar 'gnus-thread-total-score
4135                       (cdr (gnus-id-to-thread (mail-header-id root))))
4136               (when (> (mail-header-number root) 0)
4137                 (list (or (cdr (assq (mail-header-number root)
4138                                      gnus-newsgroup-scored))
4139                           gnus-summary-default-score 0))))
4140              (list gnus-summary-default-score)
4141              '(0))))
4142
4143 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4144 (defvar gnus-tmp-prev-subject nil)
4145 (defvar gnus-tmp-false-parent nil)
4146 (defvar gnus-tmp-root-expunged nil)
4147 (defvar gnus-tmp-dummy-line nil)
4148
4149 (eval-when-compile (defvar gnus-tmp-header))
4150 (defun gnus-extra-header (type &optional header)
4151   "Return the extra header of TYPE."
4152   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4153       ""))
4154
4155 (defvar gnus-tmp-thread-tree-header-string "")
4156
4157 (defvar gnus-sum-thread-tree-root "> "
4158   "With %B spec, used for the root of a thread.
4159 If nil, use subject instead.")
4160 (defvar gnus-sum-thread-tree-single-indent ""
4161   "With %B spec, used for a thread with just one message.
4162 If nil, use subject instead.")
4163 (defvar gnus-sum-thread-tree-vertical "| "
4164   "With %B spec, used for drawing a vertical line.")
4165 (defvar gnus-sum-thread-tree-indent "  "
4166   "With %B spec, used for indenting.")
4167 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4168   "With %B spec, used for a leaf with brothers.")
4169 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4170   "With %B spec, used for a leaf without brothers.")
4171
4172 (defun gnus-summary-prepare-threads (threads)
4173   "Prepare summary buffer from THREADS and indentation LEVEL.
4174 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4175 or a straight list of headers."
4176   (gnus-message 7 "Generating summary...")
4177
4178   (setq gnus-newsgroup-threads threads)
4179   (beginning-of-line)
4180
4181   (let ((gnus-tmp-level 0)
4182         (default-score (or gnus-summary-default-score 0))
4183         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4184         thread number subject stack state gnus-tmp-gathered beg-match
4185         new-roots gnus-tmp-new-adopts thread-end
4186         gnus-tmp-header gnus-tmp-unread
4187         gnus-tmp-replied gnus-tmp-subject-or-nil
4188         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4189         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4190         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4191         tree-stack)
4192
4193     (setq gnus-tmp-prev-subject nil)
4194
4195     (if (vectorp (car threads))
4196         ;; If this is a straight (sic) list of headers, then a
4197         ;; threaded summary display isn't required, so we just create
4198         ;; an unthreaded one.
4199         (gnus-summary-prepare-unthreaded threads)
4200
4201       ;; Do the threaded display.
4202
4203       (while (or threads stack gnus-tmp-new-adopts new-roots)
4204
4205         (if (and (= gnus-tmp-level 0)
4206                  (or (not stack)
4207                      (= (caar stack) 0))
4208                  (not gnus-tmp-false-parent)
4209                  (or gnus-tmp-new-adopts new-roots))
4210             (if gnus-tmp-new-adopts
4211                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4212                       thread (list (car gnus-tmp-new-adopts))
4213                       gnus-tmp-header (caar thread)
4214                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4215               (when new-roots
4216                 (setq thread (list (car new-roots))
4217                       gnus-tmp-header (caar thread)
4218                       new-roots (cdr new-roots))))
4219
4220           (if threads
4221               ;; If there are some threads, we do them before the
4222               ;; threads on the stack.
4223               (setq thread threads
4224                     gnus-tmp-header (caar thread))
4225             ;; There were no current threads, so we pop something off
4226             ;; the stack.
4227             (setq state (car stack)
4228                   gnus-tmp-level (car state)
4229                   tree-stack (cadr state)
4230                   thread (caddr state)
4231                   stack (cdr stack)
4232                   gnus-tmp-header (caar thread))))
4233
4234         (setq gnus-tmp-false-parent nil)
4235         (setq gnus-tmp-root-expunged nil)
4236         (setq thread-end nil)
4237
4238         (if (stringp gnus-tmp-header)
4239             ;; The header is a dummy root.
4240             (cond
4241              ((eq gnus-summary-make-false-root 'adopt)
4242               ;; We let the first article adopt the rest.
4243               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4244                                                (cddar thread)))
4245               (setq gnus-tmp-gathered
4246                     (nconc (mapcar
4247                             (lambda (h) (mail-header-number (car h)))
4248                             (cddar thread))
4249                            gnus-tmp-gathered))
4250               (setq thread (cons (list (caar thread)
4251                                        (cadar thread))
4252                                  (cdr thread)))
4253               (setq gnus-tmp-level -1
4254                     gnus-tmp-false-parent t))
4255              ((eq gnus-summary-make-false-root 'empty)
4256               ;; We print adopted articles with empty subject fields.
4257               (setq gnus-tmp-gathered
4258                     (nconc (mapcar
4259                             (lambda (h) (mail-header-number (car h)))
4260                             (cddar thread))
4261                            gnus-tmp-gathered))
4262               (setq gnus-tmp-level -1))
4263              ((eq gnus-summary-make-false-root 'dummy)
4264               ;; We remember that we probably want to output a dummy
4265               ;; root.
4266               (setq gnus-tmp-dummy-line gnus-tmp-header)
4267               (setq gnus-tmp-prev-subject gnus-tmp-header))
4268              (t
4269               ;; We do not make a root for the gathered
4270               ;; sub-threads at all.
4271               (setq gnus-tmp-level -1)))
4272
4273           (setq number (mail-header-number gnus-tmp-header)
4274                 subject (mail-header-subject gnus-tmp-header))
4275
4276           (cond
4277            ;; If the thread has changed subject, we might want to make
4278            ;; this subthread into a root.
4279            ((and (null gnus-thread-ignore-subject)
4280                  (not (zerop gnus-tmp-level))
4281                  gnus-tmp-prev-subject
4282                  (not (inline
4283                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4284             (setq new-roots (nconc new-roots (list (car thread)))
4285                   thread-end t
4286                   gnus-tmp-header nil))
4287            ;; If the article lies outside the current limit,
4288            ;; then we do not display it.
4289            ((not (memq number gnus-newsgroup-limit))
4290             (setq gnus-tmp-gathered
4291                   (nconc (mapcar
4292                           (lambda (h) (mail-header-number (car h)))
4293                           (cdar thread))
4294                          gnus-tmp-gathered))
4295             (setq gnus-tmp-new-adopts (if (cdar thread)
4296                                           (append gnus-tmp-new-adopts
4297                                                   (cdar thread))
4298                                         gnus-tmp-new-adopts)
4299                   thread-end t
4300                   gnus-tmp-header nil)
4301             (when (zerop gnus-tmp-level)
4302               (setq gnus-tmp-root-expunged t)))
4303            ;; Perhaps this article is to be marked as read?
4304            ((and gnus-summary-mark-below
4305                  (< (or (cdr (assq number gnus-newsgroup-scored))
4306                         default-score)
4307                     gnus-summary-mark-below)
4308                  ;; Don't touch sparse articles.
4309                  (not (gnus-summary-article-sparse-p number))
4310                  (not (gnus-summary-article-ancient-p number)))
4311             (setq gnus-newsgroup-unreads
4312                   (delq number gnus-newsgroup-unreads))
4313             (if gnus-newsgroup-auto-expire
4314                 (push number gnus-newsgroup-expirable)
4315               (push (cons number gnus-low-score-mark)
4316                     gnus-newsgroup-reads))))
4317
4318           (when gnus-tmp-header
4319             ;; We may have an old dummy line to output before this
4320             ;; article.
4321             (when (and gnus-tmp-dummy-line
4322                        (gnus-subject-equal
4323                         gnus-tmp-dummy-line
4324                         (mail-header-subject gnus-tmp-header)))
4325               (gnus-summary-insert-dummy-line
4326                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4327               (setq gnus-tmp-dummy-line nil))
4328
4329             ;; Compute the mark.
4330             (setq gnus-tmp-unread (gnus-article-mark number))
4331
4332             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4333                                   gnus-tmp-header gnus-tmp-level)
4334                   gnus-newsgroup-data)
4335
4336             ;; Actually insert the line.
4337             (setq
4338              gnus-tmp-subject-or-nil
4339              (cond
4340               ((and gnus-thread-ignore-subject
4341                     gnus-tmp-prev-subject
4342                     (not (inline (gnus-subject-equal
4343                                   gnus-tmp-prev-subject subject))))
4344                subject)
4345               ((zerop gnus-tmp-level)
4346                (if (and (eq gnus-summary-make-false-root 'empty)
4347                         (memq number gnus-tmp-gathered)
4348                         gnus-tmp-prev-subject
4349                         (inline (gnus-subject-equal
4350                                  gnus-tmp-prev-subject subject)))
4351                    gnus-summary-same-subject
4352                  subject))
4353               (t gnus-summary-same-subject)))
4354             (if (and (eq gnus-summary-make-false-root 'adopt)
4355                      (= gnus-tmp-level 1)
4356                      (memq number gnus-tmp-gathered))
4357                 (setq gnus-tmp-opening-bracket ?\<
4358                       gnus-tmp-closing-bracket ?\>)
4359               (setq gnus-tmp-opening-bracket ?\[
4360                     gnus-tmp-closing-bracket ?\]))
4361             (setq
4362              gnus-tmp-indentation
4363              (aref gnus-thread-indent-array gnus-tmp-level)
4364              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4365              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4366                                 gnus-summary-default-score 0)
4367              gnus-tmp-score-char
4368              (if (or (null gnus-summary-default-score)
4369                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4370                          gnus-summary-zcore-fuzz))
4371                  ?\ ;;;Whitespace
4372                (if (< gnus-tmp-score gnus-summary-default-score)
4373                    gnus-score-below-mark gnus-score-over-mark))
4374              gnus-tmp-replied
4375              (cond ((memq number gnus-newsgroup-processable)
4376                     gnus-process-mark)
4377                    ((memq number gnus-newsgroup-cached)
4378                     gnus-cached-mark)
4379                    ((memq number gnus-newsgroup-replied)
4380                     gnus-replied-mark)
4381                    ((memq number gnus-newsgroup-forwarded)
4382                     gnus-forwarded-mark)
4383                    ((memq number gnus-newsgroup-saved)
4384                     gnus-saved-mark)
4385                    ((memq number gnus-newsgroup-recent)
4386                     gnus-recent-mark)
4387                    ((memq number gnus-newsgroup-unseen)
4388                     gnus-unseen-mark)
4389                    (t gnus-no-mark))
4390              gnus-tmp-from (mail-header-from gnus-tmp-header)
4391              gnus-tmp-name
4392              (cond
4393               ((string-match "<[^>]+> *$" gnus-tmp-from)
4394                (setq beg-match (match-beginning 0))
4395                (or (and (string-match "^\".+\"" gnus-tmp-from)
4396                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4397                    (substring gnus-tmp-from 0 beg-match)))
4398               ((string-match "(.+)" gnus-tmp-from)
4399                (substring gnus-tmp-from
4400                           (1+ (match-beginning 0)) (1- (match-end 0))))
4401               (t gnus-tmp-from))
4402              gnus-tmp-thread-tree-header-string
4403              (cond
4404               ((not gnus-show-threads) "")
4405               ((zerop gnus-tmp-level)
4406                (if (cdar thread)
4407                    (or gnus-sum-thread-tree-root subject)
4408                  (or gnus-sum-thread-tree-single-indent subject)))
4409               (t
4410                (concat (apply 'concat
4411                               (mapcar (lambda (item)
4412                                         (if (= item 1)
4413                                             gnus-sum-thread-tree-vertical
4414                                           gnus-sum-thread-tree-indent))
4415                                       (cdr (reverse tree-stack))))
4416                        (if (nth 1 thread)
4417                            gnus-sum-thread-tree-leaf-with-other
4418                          gnus-sum-thread-tree-single-leaf)))))
4419             (when (string= gnus-tmp-name "")
4420               (setq gnus-tmp-name gnus-tmp-from))
4421             (unless (numberp gnus-tmp-lines)
4422               (setq gnus-tmp-lines -1))
4423             (if (= gnus-tmp-lines -1)
4424                 (setq gnus-tmp-lines "?")
4425               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4426             (gnus-put-text-property
4427              (point)
4428              (progn (eval gnus-summary-line-format-spec) (point))
4429              'gnus-number number)
4430             (when gnus-visual-p
4431               (forward-line -1)
4432               (gnus-run-hooks 'gnus-summary-update-hook)
4433               (forward-line 1))
4434
4435             (setq gnus-tmp-prev-subject subject)))
4436
4437         (when (nth 1 thread)
4438           (push (list (max 0 gnus-tmp-level)
4439                       (copy-list tree-stack)
4440                       (nthcdr 1 thread))
4441                 stack))
4442         (push (if (nth 1 thread) 1 0) tree-stack)
4443         (incf gnus-tmp-level)
4444         (setq threads (if thread-end nil (cdar thread)))
4445         (unless threads
4446           (setq gnus-tmp-level 0)))))
4447   (gnus-message 7 "Generating summary...done"))
4448
4449 (defun gnus-summary-prepare-unthreaded (headers)
4450   "Generate an unthreaded summary buffer based on HEADERS."
4451   (let (header number mark)
4452
4453     (beginning-of-line)
4454
4455     (while headers
4456       ;; We may have to root out some bad articles...
4457       (when (memq (setq number (mail-header-number
4458                                 (setq header (pop headers))))
4459                   gnus-newsgroup-limit)
4460         ;; Mark article as read when it has a low score.
4461         (when (and gnus-summary-mark-below
4462                    (< (or (cdr (assq number gnus-newsgroup-scored))
4463                           gnus-summary-default-score 0)
4464                       gnus-summary-mark-below)
4465                    (not (gnus-summary-article-ancient-p number)))
4466           (setq gnus-newsgroup-unreads
4467                 (delq number gnus-newsgroup-unreads))
4468           (if gnus-newsgroup-auto-expire
4469               (push number gnus-newsgroup-expirable)
4470             (push (cons number gnus-low-score-mark)
4471                   gnus-newsgroup-reads)))
4472
4473         (setq mark (gnus-article-mark number))
4474         (push (gnus-data-make number mark (1+ (point)) header 0)
4475               gnus-newsgroup-data)
4476         (gnus-summary-insert-line
4477          header 0 number
4478          mark (memq number gnus-newsgroup-replied)
4479          (memq number gnus-newsgroup-expirable)
4480          (mail-header-subject header) nil
4481          (cdr (assq number gnus-newsgroup-scored))
4482          (memq number gnus-newsgroup-processable))))))
4483
4484 (defun gnus-summary-remove-list-identifiers ()
4485   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4486   (let ((regexp (if (consp gnus-list-identifiers)
4487                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4488                   gnus-list-identifiers))
4489         changed subject)
4490     (when regexp
4491       (dolist (header gnus-newsgroup-headers)
4492         (setq subject (mail-header-subject header)
4493               changed nil)
4494         (while (string-match
4495                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4496                 subject)
4497           (setq subject
4498                 (concat (substring subject 0 (match-beginning 2))
4499                         (substring subject (match-end 0)))
4500                 changed t))
4501         (when (and changed
4502                    (string-match
4503                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4504           (setq subject
4505                 (concat (substring subject 0 (match-beginning 1))
4506                         (substring subject (match-end 1)))))
4507         (when changed
4508           (mail-header-set-subject header subject))))))
4509
4510 (defun gnus-fetch-headers (articles)
4511   "Fetch headers of ARTICLES."
4512   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4513     (gnus-message 5 "Fetching headers for %s..." name)
4514     (prog1
4515         (if (eq 'nov
4516                 (setq gnus-headers-retrieved-by
4517                       (gnus-retrieve-headers
4518                        articles gnus-newsgroup-name
4519                        ;; We might want to fetch old headers, but
4520                        ;; not if there is only 1 article.
4521                        (and (or (and
4522                                  (not (eq gnus-fetch-old-headers 'some))
4523                                  (not (numberp gnus-fetch-old-headers)))
4524                                 (> (length articles) 1))
4525                             gnus-fetch-old-headers))))
4526             (gnus-get-newsgroup-headers-xover
4527              articles nil nil gnus-newsgroup-name t)
4528           (gnus-get-newsgroup-headers))
4529       (gnus-message 5 "Fetching headers for %s...done" name))))
4530
4531 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4532   "Select newsgroup GROUP.
4533 If READ-ALL is non-nil, all articles in the group are selected.
4534 If SELECT-ARTICLES, only select those articles from GROUP."
4535   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4536          ;;!!! Dirty hack; should be removed.
4537          (gnus-summary-ignore-duplicates
4538           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4539               t
4540             gnus-summary-ignore-duplicates))
4541          (info (nth 2 entry))
4542          articles fetched-articles cached)
4543
4544     (unless (gnus-check-server
4545              (setq gnus-current-select-method
4546                    (gnus-find-method-for-group group)))
4547       (error "Couldn't open server"))
4548
4549     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4550         (gnus-activate-group group)     ; Or we can activate it...
4551         (progn                          ; Or we bug out.
4552           (when (equal major-mode 'gnus-summary-mode)
4553             (kill-buffer (current-buffer)))
4554           (error "Couldn't activate group %s: %s"
4555                  group (gnus-status-message group))))
4556
4557     (unless (gnus-request-group group t)
4558       (when (equal major-mode 'gnus-summary-mode)
4559         (kill-buffer (current-buffer)))
4560       (error "Couldn't request group %s: %s"
4561              group (gnus-status-message group)))
4562
4563     (setq gnus-newsgroup-name group
4564           gnus-newsgroup-unselected nil
4565           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4566
4567     (let ((display (gnus-group-find-parameter group 'display)))
4568       (setq gnus-newsgroup-display
4569             (cond
4570              ((eq display 'all)
4571               'gnus-not-ignore)
4572              ((arrayp display)
4573               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4574              (t
4575               nil))))
4576
4577     (gnus-summary-setup-default-charset)
4578
4579     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4580     (when (gnus-virtual-group-p group)
4581       (setq cached gnus-newsgroup-cached))
4582
4583     (setq gnus-newsgroup-unreads
4584           (gnus-set-difference
4585            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4586            gnus-newsgroup-dormant))
4587
4588     (setq gnus-newsgroup-processable nil)
4589
4590     (gnus-update-read-articles group gnus-newsgroup-unreads)
4591
4592     ;; Adjust and set lists of article marks.
4593     (when info
4594       (gnus-adjust-marked-articles info))
4595
4596     (if (setq articles select-articles)
4597         (setq gnus-newsgroup-unselected
4598               (gnus-sorted-intersection
4599                gnus-newsgroup-unreads
4600                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4601       (setq articles (gnus-articles-to-read group read-all)))
4602
4603     (cond
4604      ((null articles)
4605       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4606       'quit)
4607      ((eq articles 0) nil)
4608      (t
4609       ;; Init the dependencies hash table.
4610       (setq gnus-newsgroup-dependencies
4611             (gnus-make-hashtable (length articles)))
4612       (gnus-set-global-variables)
4613       ;; Retrieve the headers and read them in.
4614       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4615
4616       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4617       (when cached
4618         (setq gnus-newsgroup-cached cached))
4619
4620       ;; Suppress duplicates?
4621       (when gnus-suppress-duplicates
4622         (gnus-dup-suppress-articles))
4623
4624       ;; Set the initial limit.
4625       (setq gnus-newsgroup-limit (copy-sequence articles))
4626       ;; Remove canceled articles from the list of unread articles.
4627       (setq fetched-articles
4628             (mapcar (lambda (headers) (mail-header-number headers))
4629                     gnus-newsgroup-headers))
4630       (setq gnus-newsgroup-articles fetched-articles)
4631       (setq gnus-newsgroup-unreads
4632             (gnus-set-sorted-intersection
4633              gnus-newsgroup-unreads fetched-articles))
4634
4635       (let ((marks (assq 'seen (gnus-info-marks info))))
4636         ;; The `seen' marks are treated specially.
4637         (when (setq gnus-newsgroup-seen (cdr marks))
4638           (dolist (article gnus-newsgroup-articles)
4639             (unless (gnus-member-of-range
4640                      article gnus-newsgroup-seen)
4641               (push article gnus-newsgroup-unseen)))))
4642
4643       ;; Removed marked articles that do not exist.
4644       (gnus-update-missing-marks
4645        (gnus-sorted-complement fetched-articles articles))
4646       ;; We might want to build some more threads first.
4647       (when (and gnus-fetch-old-headers
4648                  (eq gnus-headers-retrieved-by 'nov))
4649         (if (eq gnus-fetch-old-headers 'invisible)
4650             (gnus-build-all-threads)
4651           (gnus-build-old-threads)))
4652       ;; Let the Gnus agent mark articles as read.
4653       (when gnus-agent
4654         (gnus-agent-get-undownloaded-list))
4655       ;; Remove list identifiers from subject
4656       (when gnus-list-identifiers
4657         (gnus-summary-remove-list-identifiers))
4658       ;; Check whether auto-expire is to be done in this group.
4659       (setq gnus-newsgroup-auto-expire
4660             (gnus-group-auto-expirable-p group))
4661       ;; Set up the article buffer now, if necessary.
4662       (unless gnus-single-article-buffer
4663         (gnus-article-setup-buffer))
4664       ;; First and last article in this newsgroup.
4665       (when gnus-newsgroup-headers
4666         (setq gnus-newsgroup-begin
4667               (mail-header-number (car gnus-newsgroup-headers))
4668               gnus-newsgroup-end
4669               (mail-header-number
4670                (gnus-last-element gnus-newsgroup-headers))))
4671       ;; GROUP is successfully selected.
4672       (or gnus-newsgroup-headers t)))))
4673
4674 (defun gnus-summary-display-make-predicate (display)
4675   (require 'gnus-agent)
4676   (when (= (length display) 1)
4677     (setq display (car display)))
4678   (unless gnus-summary-display-cache
4679     (dolist (elem (append (list (cons 'read 'read)
4680                                 (cons 'unseen 'unseen))
4681                           gnus-article-mark-lists))
4682       (push (cons (cdr elem)
4683                   (gnus-byte-compile
4684                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4685             gnus-summary-display-cache)))
4686   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4687     (gnus-get-predicate display)))
4688
4689 ;; Uses the dynamically bound `number' variable.
4690 (defvar number)
4691 (defun gnus-article-marked-p (type &optional article)
4692   (let ((article (or article number)))
4693     (cond
4694      ((eq type 'tick)
4695       (memq article gnus-newsgroup-marked))
4696      ((eq type 'unsend)
4697       (memq article gnus-newsgroup-unsendable))
4698      ((eq type 'undownload)
4699       (memq article gnus-newsgroup-undownloaded))
4700      ((eq type 'download)
4701       (memq article gnus-newsgroup-downloadable))
4702      ((eq type 'unread)
4703       (memq article gnus-newsgroup-unreads))
4704      ((eq type 'read)
4705       (memq article gnus-newsgroup-reads))
4706      ((eq type 'dormant)
4707       (memq article gnus-newsgroup-dormant) )
4708      ((eq type 'expire)
4709       (memq article gnus-newsgroup-expirable))
4710      ((eq type 'reply)
4711       (memq article gnus-newsgroup-replied))
4712      ((eq type 'killed)
4713       (memq article gnus-newsgroup-killed))
4714      ((eq type 'bookmark)
4715       (assq article gnus-newsgroup-bookmarks))
4716      ((eq type 'score)
4717       (assq article gnus-newsgroup-scored))
4718      ((eq type 'save)
4719       (memq article gnus-newsgroup-saved))
4720      ((eq type 'cache)
4721       (memq article gnus-newsgroup-cached))
4722      ((eq type 'forward)
4723       (memq article gnus-newsgroup-forwarded))
4724      ((eq type 'seen)
4725       (not (memq article gnus-newsgroup-unseen)))
4726      ((eq type 'recent)
4727       (memq article gnus-newsgroup-recent))
4728      (t t))))
4729
4730 (defun gnus-articles-to-read (group &optional read-all)
4731   "Find out what articles the user wants to read."
4732   (let* ((articles
4733           ;; Select all articles if `read-all' is non-nil, or if there
4734           ;; are no unread articles.
4735           (if (or read-all
4736                   (and (zerop (length gnus-newsgroup-marked))
4737                        (zerop (length gnus-newsgroup-unreads)))
4738                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4739               ;; We want to select the headers for all the articles in
4740               ;; the group, so we select either all the active
4741               ;; articles in the group, or (if that's nil), the
4742               ;; articles in the cache.
4743               (or
4744                (gnus-uncompress-range (gnus-active group))
4745                (gnus-cache-articles-in-group group))
4746             ;; Select only the "normal" subset of articles.
4747             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4748                           (copy-sequence gnus-newsgroup-unreads))
4749                   '<)))
4750          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4751          (scored (length scored-list))
4752          (number (length articles))
4753          (marked (+ (length gnus-newsgroup-marked)
4754                     (length gnus-newsgroup-dormant)))
4755          (select
4756           (cond
4757            ((numberp read-all)
4758             read-all)
4759            (t
4760             (condition-case ()
4761                 (cond
4762                  ((and (or (<= scored marked) (= scored number))
4763                        (natnump gnus-large-newsgroup)
4764                        (> number gnus-large-newsgroup))
4765                   (let* ((cursor-in-echo-area nil)
4766                          (input
4767                           (read-from-minibuffer
4768                            (format
4769                             "How many articles from %s (max %d): "
4770                             (gnus-limit-string
4771                              (gnus-group-decoded-name gnus-newsgroup-name)
4772                              35)
4773                             number)
4774                            (cons (number-to-string gnus-large-newsgroup)
4775                                  0))))
4776                     (if (string-match "^[ \t]*$" input)
4777                         number
4778                       input)))
4779                  ((and (> scored marked) (< scored number)
4780                        (> (- scored number) 20))
4781                   (let ((input
4782                          (read-string
4783                           (format "%s %s (%d scored, %d total): "
4784                                   "How many articles from"
4785                                   (gnus-group-decoded-name group)
4786                                   scored number))))
4787                     (if (string-match "^[ \t]*$" input)
4788                         number input)))
4789                  (t number))
4790               (quit
4791                (message "Quit getting the articles to read")
4792                nil))))))
4793     (setq select (if (stringp select) (string-to-number select) select))
4794     (if (or (null select) (zerop select))
4795         select
4796       (if (and (not (zerop scored)) (<= (abs select) scored))
4797           (progn
4798             (setq articles (sort scored-list '<))
4799             (setq number (length articles)))
4800         (setq articles (copy-sequence articles)))
4801
4802       (when (< (abs select) number)
4803         (if (< select 0)
4804             ;; Select the N oldest articles.
4805             (setcdr (nthcdr (1- (abs select)) articles) nil)
4806           ;; Select the N most recent articles.
4807           (setq articles (nthcdr (- number select) articles))))
4808       (setq gnus-newsgroup-unselected
4809             (gnus-sorted-intersection
4810              gnus-newsgroup-unreads
4811              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4812       (when gnus-alter-articles-to-read-function
4813         (setq gnus-newsgroup-unreads
4814               (sort
4815                (funcall gnus-alter-articles-to-read-function
4816                         gnus-newsgroup-name gnus-newsgroup-unreads)
4817                '<)))
4818       articles)))
4819
4820 (defun gnus-killed-articles (killed articles)
4821   (let (out)
4822     (while articles
4823       (when (inline (gnus-member-of-range (car articles) killed))
4824         (push (car articles) out))
4825       (setq articles (cdr articles)))
4826     out))
4827
4828 (defun gnus-uncompress-marks (marks)
4829   "Uncompress the mark ranges in MARKS."
4830   (let ((uncompressed '(score bookmark))
4831         out)
4832     (while marks
4833       (if (memq (caar marks) uncompressed)
4834           (push (car marks) out)
4835         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4836       (setq marks (cdr marks)))
4837     out))
4838
4839 (defun gnus-article-mark-to-type (mark)
4840   "Return the type of MARK."
4841   (or (cadr (assq mark gnus-article-special-mark-lists))
4842       'list))
4843
4844 (defun gnus-adjust-marked-articles (info)
4845   "Set all article lists and remove all marks that are no longer valid."
4846   (let* ((marked-lists (gnus-info-marks info))
4847          (active (gnus-active (gnus-info-group info)))
4848          (min (car active))
4849          (max (cdr active))
4850          (types gnus-article-mark-lists)
4851          marks var articles article mark mark-type)
4852
4853     (dolist (marks marked-lists)
4854       (setq mark (car marks)
4855             mark-type (gnus-article-mark-to-type mark)
4856             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4857
4858       ;; We set the variable according to the type of the marks list,
4859       ;; and then adjust the marks to a subset of the active articles.
4860       (cond
4861        ;; Adjust "simple" lists.
4862        ((eq mark-type 'list)
4863         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4864         (when (memq mark '(tick dormant expire reply save))
4865           (while articles
4866             (when (or (< (setq article (pop articles)) min) (> article max))
4867               (set var (delq article (symbol-value var)))))))
4868        ;; Adjust assocs.
4869        ((eq mark-type 'tuple)
4870         (set var (setq articles (cdr marks)))
4871         (when (not (listp (cdr (symbol-value var))))
4872           (set var (list (symbol-value var))))
4873         (when (not (listp (cdr articles)))
4874           (setq articles (list articles)))
4875         (while articles
4876           (when (or (not (consp (setq article (pop articles))))
4877                     (< (car article) min)
4878                     (> (car article) max))
4879             (set var (delq article (symbol-value var))))))
4880        ((eq mark-type 'range)
4881         (cond
4882          ((eq mark 'seen))))))))
4883
4884 (defun gnus-update-missing-marks (missing)
4885   "Go through the list of MISSING articles and remove them from the mark lists."
4886   (when missing
4887     (let (var m)
4888       ;; Go through all types.
4889       (dolist (elem gnus-article-mark-lists)
4890         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4891           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4892           (when (symbol-value var)
4893             ;; This list has articles.  So we delete all missing
4894             ;; articles from it.
4895             (setq m missing)
4896             (while m
4897               (set var (delq (pop m) (symbol-value var))))))))))
4898
4899 (defun gnus-update-marks ()
4900   "Enter the various lists of marked articles into the newsgroup info list."
4901   (let ((types gnus-article-mark-lists)
4902         (info (gnus-get-info gnus-newsgroup-name))
4903         (uncompressed '(score bookmark killed seen))
4904         type list newmarked symbol delta-marks)
4905     (when info
4906       ;; Add all marks lists to the list of marks lists.
4907       (while (setq type (pop types))
4908         (setq list (symbol-value
4909                     (setq symbol
4910                           (intern (format "gnus-newsgroup-%s" (car type))))))
4911
4912         (when list
4913           ;; Get rid of the entries of the articles that have the
4914           ;; default score.
4915           (when (and (eq (cdr type) 'score)
4916                      gnus-save-score
4917                      list)
4918             (let* ((arts list)
4919                    (prev (cons nil list))
4920                    (all prev))
4921               (while arts
4922                 (if (or (not (consp (car arts)))
4923                         (= (cdar arts) gnus-summary-default-score))
4924                     (setcdr prev (cdr arts))
4925                   (setq prev arts))
4926                 (setq arts (cdr arts)))
4927               (setq list (cdr all)))))
4928
4929         (when (eq (cdr type) 'seen)
4930           (setq list
4931                 (if list
4932                     (gnus-add-to-range list gnus-newsgroup-unseen)
4933                   (gnus-compress-sequence gnus-newsgroup-articles))))
4934
4935         (unless (memq (cdr type) uncompressed)
4936           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4937
4938         (when (gnus-check-backend-function
4939                'request-set-mark gnus-newsgroup-name)
4940           ;; propagate flags to server, with the following exceptions:
4941           ;; uncompressed:s are not proper flags (they are cons cells)
4942           ;; cache is a internal gnus flag
4943           ;; download are local to one gnus installation (well)
4944           ;; unsend are for nndraft groups only
4945           ;; xxx: generality of this?  this suits nnimap anyway
4946           (unless (memq (cdr type) (append '(cache download unsend)
4947                                            uncompressed))
4948             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4949                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4950                    (add (gnus-remove-from-range
4951                          (gnus-copy-sequence list) old)))
4952               (when add
4953                 (push (list add 'add (list (cdr type))) delta-marks))
4954               (when del
4955                 (push (list del 'del (list (cdr type))) delta-marks)))))
4956
4957         (when list
4958           (push (cons (cdr type) list) newmarked)))
4959
4960       (when delta-marks
4961         (unless (gnus-check-group gnus-newsgroup-name)
4962           (error "Can't open server for %s" gnus-newsgroup-name))
4963         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4964
4965       ;; Enter these new marks into the info of the group.
4966       (if (nthcdr 3 info)
4967           (setcar (nthcdr 3 info) newmarked)
4968         ;; Add the marks lists to the end of the info.
4969         (when newmarked
4970           (setcdr (nthcdr 2 info) (list newmarked))))
4971
4972       ;; Cut off the end of the info if there's nothing else there.
4973       (let ((i 5))
4974         (while (and (> i 2)
4975                     (not (nth i info)))
4976           (when (nthcdr (decf i) info)
4977             (setcdr (nthcdr i info) nil)))))))
4978
4979 (defun gnus-set-mode-line (where)
4980   "Set the mode line of the article or summary buffers.
4981 If WHERE is `summary', the summary mode line format will be used."
4982   ;; Is this mode line one we keep updated?
4983   (when (and (memq where gnus-updated-mode-lines)
4984              (symbol-value
4985               (intern (format "gnus-%s-mode-line-format-spec" where))))
4986     (let (mode-string)
4987       (save-excursion
4988         ;; We evaluate this in the summary buffer since these
4989         ;; variables are buffer-local to that buffer.
4990         (set-buffer gnus-summary-buffer)
4991         ;; We bind all these variables that are used in the `eval' form
4992         ;; below.
4993         (let* ((mformat (symbol-value
4994                          (intern
4995                           (format "gnus-%s-mode-line-format-spec" where))))
4996                (gnus-tmp-group-name (gnus-group-decoded-name
4997                                      gnus-newsgroup-name))
4998                (gnus-tmp-article-number (or gnus-current-article 0))
4999                (gnus-tmp-unread gnus-newsgroup-unreads)
5000                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5001                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5002                (gnus-tmp-unread-and-unselected
5003                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5004                             (zerop gnus-tmp-unselected))
5005                        "")
5006                       ((zerop gnus-tmp-unselected)
5007                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5008                       (t (format "{%d(+%d) more}"
5009                                  gnus-tmp-unread-and-unticked
5010                                  gnus-tmp-unselected))))
5011                (gnus-tmp-subject
5012                 (if (and gnus-current-headers
5013                          (vectorp gnus-current-headers))
5014                     (gnus-mode-string-quote
5015                      (mail-header-subject gnus-current-headers))
5016                   ""))
5017                bufname-length max-len
5018                gnus-tmp-header);; passed as argument to any user-format-funcs
5019           (setq mode-string (eval mformat))
5020           (setq bufname-length (if (string-match "%b" mode-string)
5021                                    (- (length
5022                                        (buffer-name
5023                                         (if (eq where 'summary)
5024                                             nil
5025                                           (get-buffer gnus-article-buffer))))
5026                                       2)
5027                                  0))
5028           (setq max-len (max 4 (if gnus-mode-non-string-length
5029                                    (- (window-width)
5030                                       gnus-mode-non-string-length
5031                                       bufname-length)
5032                                  (length mode-string))))
5033           ;; We might have to chop a bit of the string off...
5034           (when (> (length mode-string) max-len)
5035             (setq mode-string
5036                   (concat (gnus-truncate-string mode-string (- max-len 3))
5037                           "...")))
5038           ;; Pad the mode string a bit.
5039           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5040       ;; Update the mode line.
5041       (setq mode-line-buffer-identification
5042             (gnus-mode-line-buffer-identification (list mode-string)))
5043       (set-buffer-modified-p t))))
5044
5045 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5046   "Go through the HEADERS list and add all Xrefs to a hash table.
5047 The resulting hash table is returned, or nil if no Xrefs were found."
5048   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5049          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5050          (xref-hashtb (gnus-make-hashtable))
5051          start group entry number xrefs header)
5052     (while headers
5053       (setq header (pop headers))
5054       (when (and (setq xrefs (mail-header-xref header))
5055                  (not (memq (setq number (mail-header-number header))
5056                             unreads)))
5057         (setq start 0)
5058         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5059           (setq start (match-end 0))
5060           (setq group (if prefix
5061                           (concat prefix (substring xrefs (match-beginning 1)
5062                                                     (match-end 1)))
5063                         (substring xrefs (match-beginning 1) (match-end 1))))
5064           (setq number
5065                 (string-to-int (substring xrefs (match-beginning 2)
5066                                           (match-end 2))))
5067           (if (setq entry (gnus-gethash group xref-hashtb))
5068               (setcdr entry (cons number (cdr entry)))
5069             (gnus-sethash group (cons number nil) xref-hashtb)))))
5070     (and start xref-hashtb)))
5071
5072 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5073   "Look through all the headers and mark the Xrefs as read."
5074   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5075         name entry info xref-hashtb idlist method nth4)
5076     (save-excursion
5077       (set-buffer gnus-group-buffer)
5078       (when (setq xref-hashtb
5079                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5080         (mapatoms
5081          (lambda (group)
5082            (unless (string= from-newsgroup (setq name (symbol-name group)))
5083              (setq idlist (symbol-value group))
5084              ;; Dead groups are not updated.
5085              (and (prog1
5086                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5087                             info (nth 2 entry))
5088                     (when (stringp (setq nth4 (gnus-info-method info)))
5089                       (setq nth4 (gnus-server-to-method nth4))))
5090                   ;; Only do the xrefs if the group has the same
5091                   ;; select method as the group we have just read.
5092                   (or (gnus-methods-equal-p
5093                        nth4 (gnus-find-method-for-group from-newsgroup))
5094                       virtual
5095                       (equal nth4 (setq method (gnus-find-method-for-group
5096                                                 from-newsgroup)))
5097                       (and (equal (car nth4) (car method))
5098                            (equal (nth 1 nth4) (nth 1 method))))
5099                   gnus-use-cross-reference
5100                   (or (not (eq gnus-use-cross-reference t))
5101                       virtual
5102                       ;; Only do cross-references on subscribed
5103                       ;; groups, if that is what is wanted.
5104                       (<= (gnus-info-level info) gnus-level-subscribed))
5105                   (gnus-group-make-articles-read name idlist))))
5106          xref-hashtb)))))
5107
5108 (defun gnus-compute-read-articles (group articles)
5109   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5110          (info (nth 2 entry))
5111          (active (gnus-active group))
5112          ninfo)
5113     (when entry
5114       ;; First peel off all invalid article numbers.
5115       (when active
5116         (let ((ids articles)
5117               id first)
5118           (while (setq id (pop ids))
5119             (when (and first (> id (cdr active)))
5120               ;; We'll end up in this situation in one particular
5121               ;; obscure situation.  If you re-scan a group and get
5122               ;; a new article that is cross-posted to a different
5123               ;; group that has not been re-scanned, you might get
5124               ;; crossposted article that has a higher number than
5125               ;; Gnus believes possible.  So we re-activate this
5126               ;; group as well.  This might mean doing the
5127               ;; crossposting thingy will *increase* the number
5128               ;; of articles in some groups.  Tsk, tsk.
5129               (setq active (or (gnus-activate-group group) active)))
5130             (when (or (> id (cdr active))
5131                       (< id (car active)))
5132               (setq articles (delq id articles))))))
5133       ;; If the read list is nil, we init it.
5134       (if (and active
5135                (null (gnus-info-read info))
5136                (> (car active) 1))
5137           (setq ninfo (cons 1 (1- (car active))))
5138         (setq ninfo (gnus-info-read info)))
5139       ;; Then we add the read articles to the range.
5140       (gnus-add-to-range
5141        ninfo (setq articles (sort articles '<))))))
5142
5143 (defun gnus-group-make-articles-read (group articles)
5144   "Update the info of GROUP to say that ARTICLES are read."
5145   (let* ((num 0)
5146          (entry (gnus-gethash group gnus-newsrc-hashtb))
5147          (info (nth 2 entry))
5148          (active (gnus-active group))
5149          range)
5150     (when entry
5151       (setq range (gnus-compute-read-articles group articles))
5152       (save-excursion
5153         (set-buffer gnus-group-buffer)
5154         (gnus-undo-register
5155           `(progn
5156              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5157              (gnus-info-set-read ',info ',(gnus-info-read info))
5158              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5159              (gnus-group-update-group ,group t))))
5160       ;; Add the read articles to the range.
5161       (gnus-info-set-read info range)
5162       (gnus-request-set-mark group (list (list range 'add '(read))))
5163       ;; Then we have to re-compute how many unread
5164       ;; articles there are in this group.
5165       (when active
5166         (cond
5167          ((not range)
5168           (setq num (- (1+ (cdr active)) (car active))))
5169          ((not (listp (cdr range)))
5170           (setq num (- (cdr active) (- (1+ (cdr range))
5171                                        (car range)))))
5172          (t
5173           (while range
5174             (if (numberp (car range))
5175                 (setq num (1+ num))
5176               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5177             (setq range (cdr range)))
5178           (setq num (- (cdr active) num))))
5179         ;; Update the number of unread articles.
5180         (setcar entry num)
5181         ;; Update the group buffer.
5182         (gnus-group-update-group group t)))))
5183
5184 (defvar gnus-newsgroup-none-id 0)
5185
5186 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5187   (let ((cur nntp-server-buffer)
5188         (dependencies
5189          (or dependencies
5190              (save-excursion (set-buffer gnus-summary-buffer)
5191                              gnus-newsgroup-dependencies)))
5192         headers id end ref
5193         (mail-parse-charset gnus-newsgroup-charset)
5194         (mail-parse-ignored-charsets
5195          (save-excursion (condition-case nil
5196                              (set-buffer gnus-summary-buffer)
5197                            (error))
5198                          gnus-newsgroup-ignored-charsets)))
5199     (save-excursion
5200       (set-buffer nntp-server-buffer)
5201       ;; Translate all TAB characters into SPACE characters.
5202       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5203       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5204       (gnus-run-hooks 'gnus-parse-headers-hook)
5205       (let ((case-fold-search t)
5206             in-reply-to header p lines chars ctype)
5207         (goto-char (point-min))
5208         ;; Search to the beginning of the next header.  Error messages
5209         ;; do not begin with 2 or 3.
5210         (while (re-search-forward "^[23][0-9]+ " nil t)
5211           (setq id nil
5212                 ref nil)
5213           ;; This implementation of this function, with nine
5214           ;; search-forwards instead of the one re-search-forward and
5215           ;; a case (which basically was the old function) is actually
5216           ;; about twice as fast, even though it looks messier.  You
5217           ;; can't have everything, I guess.  Speed and elegance
5218           ;; doesn't always go hand in hand.
5219           (setq
5220            header
5221            (make-full-mail-header
5222             ;; Number.
5223             (prog1
5224                 (read cur)
5225               (end-of-line)
5226               (setq p (point))
5227               (narrow-to-region (point)
5228                                 (or (and (search-forward "\n.\n" nil t)
5229                                          (- (point) 2))
5230                                     (point))))
5231             ;; Subject.
5232             (progn
5233               (goto-char p)
5234               (if (search-forward "\nsubject:" nil t)
5235                   (nnheader-header-value)
5236                 "(none)"))
5237             ;; From.
5238             (progn
5239               (goto-char p)
5240               (if (search-forward "\nfrom:" nil t)
5241                   (nnheader-header-value)
5242                 "(nobody)"))
5243             ;; Date.
5244             (progn
5245               (goto-char p)
5246               (if (search-forward "\ndate:" nil t)
5247                   (nnheader-header-value) ""))
5248             ;; Message-ID.
5249             (progn
5250               (goto-char p)
5251               (setq id (if (re-search-forward
5252                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5253                            ;; We do it this way to make sure the Message-ID
5254                            ;; is (somewhat) syntactically valid.
5255                            (buffer-substring (match-beginning 1)
5256                                              (match-end 1))
5257                          ;; If there was no message-id, we just fake one
5258                          ;; to make subsequent routines simpler.
5259                          (nnheader-generate-fake-message-id))))
5260             ;; References.
5261             (progn
5262               (goto-char p)
5263               (if (search-forward "\nreferences:" nil t)
5264                   (progn
5265                     (setq end (point))
5266                     (prog1
5267                         (nnheader-header-value)
5268                       (setq ref
5269                             (buffer-substring
5270                              (progn
5271                                ;; (end-of-line)
5272                                (search-backward ">" end t)
5273                                (1+ (point)))
5274                              (progn
5275                                (search-backward "<" end t)
5276                                (point))))))
5277                 ;; Get the references from the in-reply-to header if there
5278                 ;; were no references and the in-reply-to header looks
5279                 ;; promising.
5280                 (if (and (search-forward "\nin-reply-to:" nil t)
5281                          (setq in-reply-to (nnheader-header-value))
5282                          (string-match "<[^>]+>" in-reply-to))
5283                     (let (ref2)
5284                       (setq ref (substring in-reply-to (match-beginning 0)
5285                                            (match-end 0)))
5286                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5287                         (setq ref2 (substring in-reply-to (match-beginning 0)
5288                                               (match-end 0)))
5289                         (when (> (length ref2) (length ref))
5290                           (setq ref ref2)))
5291                       ref)
5292                   (setq ref nil))))
5293             ;; Chars.
5294             (progn
5295               (goto-char p)
5296               (if (search-forward "\nchars: " nil t)
5297                   (if (numberp (setq chars (ignore-errors (read cur))))
5298                       chars -1)
5299                 -1))
5300             ;; Lines.
5301             (progn
5302               (goto-char p)
5303               (if (search-forward "\nlines: " nil t)
5304                   (if (numberp (setq lines (ignore-errors (read cur))))
5305                       lines -1)
5306                 -1))
5307             ;; Xref.
5308             (progn
5309               (goto-char p)
5310               (and (search-forward "\nxref:" nil t)
5311                    (nnheader-header-value)))
5312             ;; Extra.
5313             (when gnus-extra-headers
5314               (let ((extra gnus-extra-headers)
5315                     out)
5316                 (while extra
5317                   (goto-char p)
5318                   (when (search-forward
5319                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5320                     (push (cons (car extra) (nnheader-header-value)) out))
5321                   (pop extra))
5322                 out))))
5323           (goto-char p)
5324           (if (and (search-forward "\ncontent-type: " nil t)
5325                    (setq ctype (nnheader-header-value)))
5326               (mime-entity-set-content-type-internal
5327                header (mime-parse-Content-Type ctype)))
5328           (when (equal id ref)
5329             (setq ref nil))
5330
5331           (when gnus-alter-header-function
5332             (funcall gnus-alter-header-function header)
5333             (setq id (mail-header-id header)
5334                   ref (gnus-parent-id (mail-header-references header))))
5335
5336           (when (setq header
5337                       (gnus-dependencies-add-header
5338                        header dependencies force-new))
5339             (push header headers))
5340           (goto-char (point-max))
5341           (widen))
5342         (nreverse headers)))))
5343
5344 ;; Goes through the xover lines and returns a list of vectors
5345 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5346                                                   force-new dependencies
5347                                                   group also-fetch-heads)
5348   "Parse the news overview data in the server buffer.
5349 Return a list of headers that match SEQUENCE (see
5350 `nntp-retrieve-headers')."
5351   ;; Get the Xref when the users reads the articles since most/some
5352   ;; NNTP servers do not include Xrefs when using XOVER.
5353   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5354   (let ((mail-parse-charset gnus-newsgroup-charset)
5355         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5356         (cur nntp-server-buffer)
5357         (dependencies (or dependencies gnus-newsgroup-dependencies))
5358         (allp (cond
5359                ((eq gnus-read-all-available-headers t)
5360                 t)
5361                ((stringp gnus-read-all-available-headers)
5362                 (string-match gnus-read-all-available-headers group))
5363                (t
5364                 nil)))
5365         number headers header)
5366     (save-excursion
5367       (set-buffer nntp-server-buffer)
5368       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5369       ;; Allow the user to mangle the headers before parsing them.
5370       (gnus-run-hooks 'gnus-parse-headers-hook)
5371       (goto-char (point-min))
5372       (while (not (eobp))
5373         (condition-case ()
5374             (while (and (or sequence allp)
5375                         (not (eobp)))
5376               (setq number (read cur))
5377               (when (not allp)
5378                 (while (and sequence
5379                             (< (car sequence) number))
5380                   (setq sequence (cdr sequence))))
5381               (when (and (or allp
5382                              (and sequence
5383                                   (eq number (car sequence))))
5384                          (progn
5385                            (setq sequence (cdr sequence))
5386                            (setq header (inline
5387                                           (gnus-nov-parse-line
5388                                            number dependencies force-new)))))
5389                 (push header headers))
5390               (forward-line 1))
5391           (error
5392            (gnus-error 4 "Strange nov line (%d)"
5393                        (count-lines (point-min) (point)))))
5394         (forward-line 1))
5395       ;; A common bug in inn is that if you have posted an article and
5396       ;; then retrieves the active file, it will answer correctly --
5397       ;; the new article is included.  However, a NOV entry for the
5398       ;; article may not have been generated yet, so this may fail.
5399       ;; We work around this problem by retrieving the last few
5400       ;; headers using HEAD.
5401       (if (or (not also-fetch-heads)
5402               (not sequence))
5403           ;; We (probably) got all the headers.
5404           (nreverse headers)
5405         (let ((gnus-nov-is-evil t))
5406           (nconc
5407            (nreverse headers)
5408            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5409              (gnus-get-newsgroup-headers))))))))
5410
5411 (defun gnus-article-get-xrefs ()
5412   "Fill in the Xref value in `gnus-current-headers', if necessary.
5413 This is meant to be called in `gnus-article-internal-prepare-hook'."
5414   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5415                                  gnus-current-headers)))
5416     (or (not gnus-use-cross-reference)
5417         (not headers)
5418         (and (mail-header-xref headers)
5419              (not (string= (mail-header-xref headers) "")))
5420         (let ((case-fold-search t)
5421               xref)
5422           (save-restriction
5423             (nnheader-narrow-to-headers)
5424             (goto-char (point-min))
5425             (when (or (and (not (eobp))
5426                            (eq (downcase (char-after)) ?x)
5427                            (looking-at "Xref:"))
5428                       (search-forward "\nXref:" nil t))
5429               (goto-char (1+ (match-end 0)))
5430               (setq xref (buffer-substring (point)
5431                                            (progn (end-of-line) (point))))
5432               (mail-header-set-xref headers xref)))))))
5433
5434 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5435   "Find article ID and insert the summary line for that article.
5436 OLD-HEADER can either be a header or a line number to insert
5437 the subject line on."
5438   (let* ((line (and (numberp old-header) old-header))
5439          (old-header (and (vectorp old-header) old-header))
5440          (header (cond ((and old-header use-old-header)
5441                         old-header)
5442                        ((and (numberp id)
5443                              (gnus-number-to-header id))
5444                         (gnus-number-to-header id))
5445                        (t
5446                         (gnus-read-header id))))
5447          (number (and (numberp id) id))
5448          d)
5449     (when header
5450       ;; Rebuild the thread that this article is part of and go to the
5451       ;; article we have fetched.
5452       (when (and (not gnus-show-threads)
5453                  old-header)
5454         (when (and number
5455                    (setq d (gnus-data-find (mail-header-number old-header))))
5456           (goto-char (gnus-data-pos d))
5457           (gnus-data-remove
5458            number
5459            (- (gnus-point-at-bol)
5460               (prog1
5461                   (1+ (gnus-point-at-eol))
5462                 (gnus-delete-line))))))
5463       (when old-header
5464         (mail-header-set-number header (mail-header-number old-header)))
5465       (setq gnus-newsgroup-sparse
5466             (delq (setq number (mail-header-number header))
5467                   gnus-newsgroup-sparse))
5468       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5469       (push number gnus-newsgroup-limit)
5470       (gnus-rebuild-thread (mail-header-id header) line)
5471       (gnus-summary-goto-subject number nil t))
5472     (when (and (numberp number)
5473                (> number 0))
5474       ;; We have to update the boundaries even if we can't fetch the
5475       ;; article if ID is a number -- so that the next `P' or `N'
5476       ;; command will fetch the previous (or next) article even
5477       ;; if the one we tried to fetch this time has been canceled.
5478       (when (> number gnus-newsgroup-end)
5479         (setq gnus-newsgroup-end number))
5480       (when (< number gnus-newsgroup-begin)
5481         (setq gnus-newsgroup-begin number))
5482       (setq gnus-newsgroup-unselected
5483             (delq number gnus-newsgroup-unselected)))
5484     ;; Report back a success?
5485     (and header (mail-header-number header))))
5486
5487 ;;; Process/prefix in the summary buffer
5488
5489 (defun gnus-summary-work-articles (n)
5490   "Return a list of articles to be worked upon.
5491 The prefix argument, the list of process marked articles, and the
5492 current article will be taken into consideration."
5493   (save-excursion
5494     (set-buffer gnus-summary-buffer)
5495     (cond
5496      (n
5497       ;; A numerical prefix has been given.
5498       (setq n (prefix-numeric-value n))
5499       (let ((backward (< n 0))
5500             (n (abs (prefix-numeric-value n)))
5501             articles article)
5502         (save-excursion
5503           (while
5504               (and (> n 0)
5505                    (push (setq article (gnus-summary-article-number))
5506                          articles)
5507                    (if backward
5508                        (gnus-summary-find-prev nil article)
5509                      (gnus-summary-find-next nil article)))
5510             (decf n)))
5511         (nreverse articles)))
5512      ((and (gnus-region-active-p) (mark))
5513       (message "region active")
5514       ;; Work on the region between point and mark.
5515       (let ((max (max (point) (mark)))
5516             articles article)
5517         (save-excursion
5518           (goto-char (min (point) (mark)))
5519           (while
5520               (and
5521                (push (setq article (gnus-summary-article-number)) articles)
5522                (gnus-summary-find-next nil article)
5523                (< (point) max)))
5524           (nreverse articles))))
5525      (gnus-newsgroup-processable
5526       ;; There are process-marked articles present.
5527       ;; Save current state.
5528       (gnus-summary-save-process-mark)
5529       ;; Return the list.
5530       (reverse gnus-newsgroup-processable))
5531      (t
5532       ;; Just return the current article.
5533       (list (gnus-summary-article-number))))))
5534
5535 (defmacro gnus-summary-iterate (arg &rest forms)
5536   "Iterate over the process/prefixed articles and do FORMS.
5537 ARG is the interactive prefix given to the command.  FORMS will be
5538 executed with point over the summary line of the articles."
5539   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5540     `(let ((,articles (gnus-summary-work-articles ,arg)))
5541        (while ,articles
5542          (gnus-summary-goto-subject (car ,articles))
5543          ,@forms
5544          (pop ,articles)))))
5545
5546 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5547 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5548
5549 (defun gnus-summary-save-process-mark ()
5550   "Push the current set of process marked articles on the stack."
5551   (interactive)
5552   (push (copy-sequence gnus-newsgroup-processable)
5553         gnus-newsgroup-process-stack))
5554
5555 (defun gnus-summary-kill-process-mark ()
5556   "Push the current set of process marked articles on the stack and unmark."
5557   (interactive)
5558   (gnus-summary-save-process-mark)
5559   (gnus-summary-unmark-all-processable))
5560
5561 (defun gnus-summary-yank-process-mark ()
5562   "Pop the last process mark state off the stack and restore it."
5563   (interactive)
5564   (unless gnus-newsgroup-process-stack
5565     (error "Empty mark stack"))
5566   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5567
5568 (defun gnus-summary-process-mark-set (set)
5569   "Make SET into the current process marked articles."
5570   (gnus-summary-unmark-all-processable)
5571   (while set
5572     (gnus-summary-set-process-mark (pop set))))
5573
5574 ;;; Searching and stuff
5575
5576 (defun gnus-summary-search-group (&optional backward use-level)
5577   "Search for next unread newsgroup.
5578 If optional argument BACKWARD is non-nil, search backward instead."
5579   (save-excursion
5580     (set-buffer gnus-group-buffer)
5581     (when (gnus-group-search-forward
5582            backward nil (if use-level (gnus-group-group-level) nil))
5583       (gnus-group-group-name))))
5584
5585 (defun gnus-summary-best-group (&optional exclude-group)
5586   "Find the name of the best unread group.
5587 If EXCLUDE-GROUP, do not go to this group."
5588   (save-excursion
5589     (set-buffer gnus-group-buffer)
5590     (save-excursion
5591       (gnus-group-best-unread-group exclude-group))))
5592
5593 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5594   (if backward (gnus-summary-find-prev)
5595     (let* ((dummy (gnus-summary-article-intangible-p))
5596            (article (or article (gnus-summary-article-number)))
5597            (arts (gnus-data-find-list article))
5598            result)
5599       (when (and (not dummy)
5600                  (or (not gnus-summary-check-current)
5601                      (not unread)
5602                      (not (gnus-data-unread-p (car arts)))))
5603         (setq arts (cdr arts)))
5604       (when (setq result
5605                   (if unread
5606                       (progn
5607                         (while arts
5608                           (when (or (and undownloaded
5609                                          (eq gnus-undownloaded-mark
5610                                              (gnus-data-mark (car arts))))
5611                                     (gnus-data-unread-p (car arts)))
5612                             (setq result (car arts)
5613                                   arts nil))
5614                           (setq arts (cdr arts)))
5615                         result)
5616                     (car arts)))
5617         (goto-char (gnus-data-pos result))
5618         (gnus-data-number result)))))
5619
5620 (defun gnus-summary-find-prev (&optional unread article)
5621   (let* ((eobp (eobp))
5622          (article (or article (gnus-summary-article-number)))
5623          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5624          result)
5625     (when (and (not eobp)
5626                (or (not gnus-summary-check-current)
5627                    (not unread)
5628                    (not (gnus-data-unread-p (car arts)))))
5629       (setq arts (cdr arts)))
5630     (when (setq result
5631                 (if unread
5632                     (progn
5633                       (while arts
5634                         (when (gnus-data-unread-p (car arts))
5635                           (setq result (car arts)
5636                                 arts nil))
5637                         (setq arts (cdr arts)))
5638                       result)
5639                   (car arts)))
5640       (goto-char (gnus-data-pos result))
5641       (gnus-data-number result))))
5642
5643 (defun gnus-summary-find-subject (subject &optional unread backward article)
5644   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5645          (article (or article (gnus-summary-article-number)))
5646          (articles (gnus-data-list backward))
5647          (arts (gnus-data-find-list article articles))
5648          result)
5649     (when (or (not gnus-summary-check-current)
5650               (not unread)
5651               (not (gnus-data-unread-p (car arts))))
5652       (setq arts (cdr arts)))
5653     (while arts
5654       (and (or (not unread)
5655                (gnus-data-unread-p (car arts)))
5656            (vectorp (gnus-data-header (car arts)))
5657            (gnus-subject-equal
5658             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5659            (setq result (car arts)
5660                  arts nil))
5661       (setq arts (cdr arts)))
5662     (and result
5663          (goto-char (gnus-data-pos result))
5664          (gnus-data-number result))))
5665
5666 (defun gnus-summary-search-forward (&optional unread subject backward)
5667   "Search forward for an article.
5668 If UNREAD, look for unread articles.  If SUBJECT, look for
5669 articles with that subject.  If BACKWARD, search backward instead."
5670   (cond (subject (gnus-summary-find-subject subject unread backward))
5671         (backward (gnus-summary-find-prev unread))
5672         (t (gnus-summary-find-next unread))))
5673
5674 (defun gnus-recenter (&optional n)
5675   "Center point in window and redisplay frame.
5676 Also do horizontal recentering."
5677   (interactive "P")
5678   (when (and gnus-auto-center-summary
5679              (not (eq gnus-auto-center-summary 'vertical)))
5680     (gnus-horizontal-recenter))
5681   (recenter n))
5682
5683 (defun gnus-summary-recenter ()
5684   "Center point in the summary window.
5685 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5686 displayed, no centering will be performed."
5687   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5688   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5689   (interactive)
5690   (let* ((top (cond ((< (window-height) 4) 0)
5691                     ((< (window-height) 7) 1)
5692                     (t (if (numberp gnus-auto-center-summary)
5693                            gnus-auto-center-summary
5694                          2))))
5695          (height (1- (window-height)))
5696          (bottom (save-excursion (goto-char (point-max))
5697                                  (forward-line (- height))
5698                                  (point)))
5699          (window (get-buffer-window (current-buffer))))
5700     ;; The user has to want it.
5701     (when gnus-auto-center-summary
5702       (when (get-buffer-window gnus-article-buffer)
5703         ;; Only do recentering when the article buffer is displayed,
5704         ;; Set the window start to either `bottom', which is the biggest
5705         ;; possible valid number, or the second line from the top,
5706         ;; whichever is the least.
5707         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5708           (if (> bottom top-pos)
5709               ;; Keep the second line from the top visible
5710               (set-window-start window top-pos t)
5711             ;; Try to keep the bottom line visible; if it's partially
5712             ;; obscured, either scroll one more line to make it fully
5713             ;; visible, or revert to using TOP-POS.
5714             (save-excursion
5715               (goto-char (point-max))
5716               (forward-line -1)
5717               (let ((last-line-start (point)))
5718                 (goto-char bottom)
5719                 (set-window-start window (point) t)
5720                 (when (not (pos-visible-in-window-p last-line-start window))
5721                   (forward-line 1)
5722                   (set-window-start window (min (point) top-pos) t)))))))
5723       ;; Do horizontal recentering while we're at it.
5724       (when (and (get-buffer-window (current-buffer) t)
5725                  (not (eq gnus-auto-center-summary 'vertical)))
5726         (let ((selected (selected-window)))
5727           (select-window (get-buffer-window (current-buffer) t))
5728           (gnus-summary-position-point)
5729           (gnus-horizontal-recenter)
5730           (select-window selected))))))
5731
5732 (defun gnus-summary-jump-to-group (newsgroup)
5733   "Move point to NEWSGROUP in group mode buffer."
5734   ;; Keep update point of group mode buffer if visible.
5735   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5736       (save-window-excursion
5737         ;; Take care of tree window mode.
5738         (when (get-buffer-window gnus-group-buffer)
5739           (pop-to-buffer gnus-group-buffer))
5740         (gnus-group-jump-to-group newsgroup))
5741     (save-excursion
5742       ;; Take care of tree window mode.
5743       (if (get-buffer-window gnus-group-buffer)
5744           (pop-to-buffer gnus-group-buffer)
5745         (set-buffer gnus-group-buffer))
5746       (gnus-group-jump-to-group newsgroup))))
5747
5748 ;; This function returns a list of article numbers based on the
5749 ;; difference between the ranges of read articles in this group and
5750 ;; the range of active articles.
5751 (defun gnus-list-of-unread-articles (group)
5752   (let* ((read (gnus-info-read (gnus-get-info group)))
5753          (active (or (gnus-active group) (gnus-activate-group group)))
5754          (last (cdr active))
5755          first nlast unread)
5756     ;; If none are read, then all are unread.
5757     (if (not read)
5758         (setq first (car active))
5759       ;; If the range of read articles is a single range, then the
5760       ;; first unread article is the article after the last read
5761       ;; article.  Sounds logical, doesn't it?
5762       (if (and (not (listp (cdr read)))
5763                (or (< (car read) (car active))
5764                    (progn (setq read (list read))
5765                           nil)))
5766           (setq first (max (car active) (1+ (cdr read))))
5767         ;; `read' is a list of ranges.
5768         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5769                                   (caar read)))
5770                   1)
5771           (setq first (car active)))
5772         (while read
5773           (when first
5774             (while (< first nlast)
5775               (push first unread)
5776               (setq first (1+ first))))
5777           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5778           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5779           (setq read (cdr read)))))
5780     ;; And add the last unread articles.
5781     (while (<= first last)
5782       (push first unread)
5783       (setq first (1+ first)))
5784     ;; Return the list of unread articles.
5785     (delq 0 (nreverse unread))))
5786
5787 (defun gnus-list-of-read-articles (group)
5788   "Return a list of unread, unticked and non-dormant articles."
5789   (let* ((info (gnus-get-info group))
5790          (marked (gnus-info-marks info))
5791          (active (gnus-active group)))
5792     (and info active
5793          (gnus-set-difference
5794           (gnus-sorted-complement
5795            (gnus-uncompress-range active)
5796            (gnus-list-of-unread-articles group))
5797           (append
5798            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5799            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5800
5801 ;; Various summary commands
5802
5803 (defun gnus-summary-select-article-buffer ()
5804   "Reconfigure windows to show article buffer."
5805   (interactive)
5806   (if (not (gnus-buffer-live-p gnus-article-buffer))
5807       (error "There is no article buffer for this summary buffer")
5808     (gnus-configure-windows 'article)
5809     (select-window (get-buffer-window gnus-article-buffer))))
5810
5811 (defun gnus-summary-universal-argument (arg)
5812   "Perform any operation on all articles that are process/prefixed."
5813   (interactive "P")
5814   (let ((articles (gnus-summary-work-articles arg))
5815         func article)
5816     (if (eq
5817          (setq
5818           func
5819           (key-binding
5820            (read-key-sequence
5821             (substitute-command-keys
5822              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5823          'undefined)
5824         (gnus-error 1 "Undefined key")
5825       (save-excursion
5826         (while articles
5827           (gnus-summary-goto-subject (setq article (pop articles)))
5828           (let (gnus-newsgroup-processable)
5829             (command-execute func))
5830           (gnus-summary-remove-process-mark article)))))
5831   (gnus-summary-position-point))
5832
5833 (defun gnus-summary-toggle-truncation (&optional arg)
5834   "Toggle truncation of summary lines.
5835 With arg, turn line truncation on iff arg is positive."
5836   (interactive "P")
5837   (setq truncate-lines
5838         (if (null arg) (not truncate-lines)
5839           (> (prefix-numeric-value arg) 0)))
5840   (redraw-display))
5841
5842 (defun gnus-summary-reselect-current-group (&optional all rescan)
5843   "Exit and then reselect the current newsgroup.
5844 The prefix argument ALL means to select all articles."
5845   (interactive "P")
5846   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5847     (error "Ephemeral groups can't be reselected"))
5848   (let ((current-subject (gnus-summary-article-number))
5849         (group gnus-newsgroup-name))
5850     (setq gnus-newsgroup-begin nil)
5851     (gnus-summary-exit)
5852     ;; We have to adjust the point of group mode buffer because
5853     ;; point was moved to the next unread newsgroup by exiting.
5854     (gnus-summary-jump-to-group group)
5855     (when rescan
5856       (save-excursion
5857         (save-window-excursion
5858           ;; Don't show group contents.
5859           (set-window-start (selected-window) (point-max))
5860           (gnus-group-get-new-news-this-group 1))))
5861     (gnus-group-read-group all t)
5862     (gnus-summary-goto-subject current-subject nil t)))
5863
5864 (defun gnus-summary-rescan-group (&optional all)
5865   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5866   (interactive "P")
5867   (gnus-summary-reselect-current-group all t))
5868
5869 (defun gnus-summary-update-info (&optional non-destructive)
5870   (save-excursion
5871     (let ((group gnus-newsgroup-name))
5872       (when group
5873         (when gnus-newsgroup-kill-headers
5874           (setq gnus-newsgroup-killed
5875                 (gnus-compress-sequence
5876                  (nconc
5877                   (gnus-set-sorted-intersection
5878                    (gnus-uncompress-range gnus-newsgroup-killed)
5879                    (setq gnus-newsgroup-unselected
5880                          (sort gnus-newsgroup-unselected '<)))
5881                   (setq gnus-newsgroup-unreads
5882                         (sort gnus-newsgroup-unreads '<)))
5883                  t)))
5884         (unless (listp (cdr gnus-newsgroup-killed))
5885           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5886         (let ((headers gnus-newsgroup-headers))
5887           ;; Set the new ranges of read articles.
5888           (save-excursion
5889             (set-buffer gnus-group-buffer)
5890             (gnus-undo-force-boundary))
5891           (gnus-update-read-articles
5892            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5893           ;; Set the current article marks.
5894           (let ((gnus-newsgroup-scored
5895                  (if (and (not gnus-save-score)
5896                           (not non-destructive))
5897                      nil
5898                    gnus-newsgroup-scored)))
5899             (save-excursion
5900               (gnus-update-marks)))
5901           ;; Do the cross-ref thing.
5902           (when gnus-use-cross-reference
5903             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5904           ;; Do not switch windows but change the buffer to work.
5905           (set-buffer gnus-group-buffer)
5906           (unless (gnus-ephemeral-group-p group)
5907             (gnus-group-update-group group)))))))
5908
5909 (defun gnus-summary-save-newsrc (&optional force)
5910   "Save the current number of read/marked articles in the dribble buffer.
5911 The dribble buffer will then be saved.
5912 If FORCE (the prefix), also save the .newsrc file(s)."
5913   (interactive "P")
5914   (gnus-summary-update-info t)
5915   (if force
5916       (gnus-save-newsrc-file)
5917     (gnus-dribble-save)))
5918
5919 (defun gnus-summary-exit (&optional temporary)
5920   "Exit reading current newsgroup, and then return to group selection mode.
5921 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5922   (interactive)
5923   (gnus-set-global-variables)
5924   (gnus-kill-save-kill-buffer)
5925   (gnus-async-halt-prefetch)
5926   (let* ((group gnus-newsgroup-name)
5927          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5928          (mode major-mode)
5929          (group-point nil)
5930          (buf (current-buffer)))
5931     (unless quit-config
5932       ;; Do adaptive scoring, and possibly save score files.
5933       (when gnus-newsgroup-adaptive
5934         (gnus-score-adaptive))
5935       (when gnus-use-scoring
5936         (gnus-score-save)))
5937     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5938     ;; If we have several article buffers, we kill them at exit.
5939     (unless gnus-single-article-buffer
5940       (gnus-kill-buffer gnus-original-article-buffer)
5941       (setq gnus-article-current nil))
5942     (when gnus-use-cache
5943       (gnus-cache-possibly-remove-articles)
5944       (gnus-cache-save-buffers))
5945     (gnus-async-prefetch-remove-group group)
5946     (when gnus-suppress-duplicates
5947       (gnus-dup-enter-articles))
5948     (when gnus-use-trees
5949       (gnus-tree-close group))
5950     (when gnus-use-cache
5951       (gnus-cache-write-active))
5952     ;; Remove entries for this group.
5953     (nnmail-purge-split-history (gnus-group-real-name group))
5954     ;; Make all changes in this group permanent.
5955     (unless quit-config
5956       (gnus-run-hooks 'gnus-exit-group-hook)
5957       (gnus-summary-update-info))
5958     (gnus-close-group group)
5959     ;; Make sure where we were, and go to next newsgroup.
5960     (set-buffer gnus-group-buffer)
5961     (unless quit-config
5962       (gnus-group-jump-to-group group))
5963     (gnus-run-hooks 'gnus-summary-exit-hook)
5964     (unless (or quit-config
5965                 ;; If this group has disappeared from the summary
5966                 ;; buffer, don't skip forwards.
5967                 (not (string= group (gnus-group-group-name))))
5968       (gnus-group-next-unread-group 1))
5969     (setq group-point (point))
5970     (if temporary
5971         nil                             ;Nothing to do.
5972       ;; If we have several article buffers, we kill them at exit.
5973       (unless gnus-single-article-buffer
5974         (gnus-kill-buffer gnus-article-buffer)
5975         (gnus-kill-buffer gnus-original-article-buffer)
5976         (setq gnus-article-current nil))
5977       (set-buffer buf)
5978       (if (not gnus-kill-summary-on-exit)
5979           (progn
5980             (gnus-deaden-summary)
5981             (setq mode nil))
5982         ;; We set all buffer-local variables to nil.  It is unclear why
5983         ;; this is needed, but if we don't, buffer-local variables are
5984         ;; not garbage-collected, it seems.  This would the lead to en
5985         ;; ever-growing Emacs.
5986         (gnus-summary-clear-local-variables)
5987         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5988           (gnus-summary-clear-local-variables))
5989         (when (get-buffer gnus-article-buffer)
5990           (bury-buffer gnus-article-buffer))
5991         ;; We clear the global counterparts of the buffer-local
5992         ;; variables as well, just to be on the safe side.
5993         (set-buffer gnus-group-buffer)
5994         (gnus-summary-clear-local-variables)
5995         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5996           (gnus-summary-clear-local-variables)))
5997       (setq gnus-current-select-method gnus-select-method)
5998       (pop-to-buffer gnus-group-buffer)
5999       (if (not quit-config)
6000           (progn
6001             (goto-char group-point)
6002             (gnus-configure-windows 'group 'force)
6003             (unless (pos-visible-in-window-p)
6004               (forward-line (/ (static-if (featurep 'xemacs)
6005                                    (window-displayed-height)
6006                                  (1- (window-height)))
6007                                -2))
6008               (set-window-start (selected-window) (point))
6009               (goto-char group-point)))
6010         (gnus-handle-ephemeral-exit quit-config))
6011       ;; Return to group mode buffer.
6012       (when (eq mode 'gnus-summary-mode)
6013         (gnus-kill-buffer buf))
6014       ;; Clear the current group name.
6015       (unless quit-config
6016         (setq gnus-newsgroup-name nil)))))
6017
6018 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6019 (defun gnus-summary-exit-no-update (&optional no-questions)
6020   "Quit reading current newsgroup without updating read article info."
6021   (interactive)
6022   (let* ((group gnus-newsgroup-name)
6023          (quit-config (gnus-group-quit-config group)))
6024     (when (or no-questions
6025               gnus-expert-user
6026               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6027       (gnus-async-halt-prefetch)
6028       (mapcar 'funcall
6029               (delq 'gnus-summary-expire-articles
6030                     (copy-sequence gnus-summary-prepare-exit-hook)))
6031       ;; If we have several article buffers, we kill them at exit.
6032       (unless gnus-single-article-buffer
6033         (gnus-kill-buffer gnus-article-buffer)
6034         (gnus-kill-buffer gnus-original-article-buffer)
6035         (setq gnus-article-current nil))
6036       (if (not gnus-kill-summary-on-exit)
6037           (gnus-deaden-summary)
6038         (gnus-close-group group)
6039         (gnus-summary-clear-local-variables)
6040         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6041           (gnus-summary-clear-local-variables))
6042         (set-buffer gnus-group-buffer)
6043         (gnus-summary-clear-local-variables)
6044         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6045           (gnus-summary-clear-local-variables))
6046         (when (get-buffer gnus-summary-buffer)
6047           (kill-buffer gnus-summary-buffer)))
6048       (unless gnus-single-article-buffer
6049         (setq gnus-article-current nil))
6050       (when gnus-use-trees
6051         (gnus-tree-close group))
6052       (gnus-async-prefetch-remove-group group)
6053       (when (get-buffer gnus-article-buffer)
6054         (bury-buffer gnus-article-buffer))
6055       ;; Return to the group buffer.
6056       (gnus-configure-windows 'group 'force)
6057       ;; Clear the current group name.
6058       (setq gnus-newsgroup-name nil)
6059       (when (equal (gnus-group-group-name) group)
6060         (gnus-group-next-unread-group 1))
6061       (when quit-config
6062         (gnus-handle-ephemeral-exit quit-config)))))
6063
6064 (defun gnus-handle-ephemeral-exit (quit-config)
6065   "Handle movement when leaving an ephemeral group.
6066 The state which existed when entering the ephemeral is reset."
6067   (if (not (buffer-name (car quit-config)))
6068       (gnus-configure-windows 'group 'force)
6069     (set-buffer (car quit-config))
6070     (cond ((eq major-mode 'gnus-summary-mode)
6071            (gnus-set-global-variables))
6072           ((eq major-mode 'gnus-article-mode)
6073            (save-excursion
6074              ;; The `gnus-summary-buffer' variable may point
6075              ;; to the old summary buffer when using a single
6076              ;; article buffer.
6077              (unless (gnus-buffer-live-p gnus-summary-buffer)
6078                (set-buffer gnus-group-buffer))
6079              (set-buffer gnus-summary-buffer)
6080              (gnus-set-global-variables))))
6081     (if (or (eq (cdr quit-config) 'article)
6082             (eq (cdr quit-config) 'pick))
6083         (progn
6084           ;; The current article may be from the ephemeral group
6085           ;; thus it is best that we reload this article
6086           (gnus-summary-show-article)
6087           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6088               (gnus-configure-windows 'pick 'force)
6089             (gnus-configure-windows (cdr quit-config) 'force)))
6090       (gnus-configure-windows (cdr quit-config) 'force))
6091     (when (eq major-mode 'gnus-summary-mode)
6092       (gnus-summary-next-subject 1 nil t)
6093       (gnus-summary-recenter)
6094       (gnus-summary-position-point))))
6095
6096 (defun gnus-summary-preview-mime-message ()
6097   "MIME decode and play this message."
6098   (interactive)
6099   (let ((gnus-break-pages nil)
6100         (gnus-show-mime t))
6101     (gnus-summary-select-article gnus-show-all-headers t))
6102   (select-window (get-buffer-window gnus-article-buffer)))
6103
6104 ;;; Dead summaries.
6105
6106 (defvar gnus-dead-summary-mode-map nil)
6107
6108 (unless gnus-dead-summary-mode-map
6109   (setq gnus-dead-summary-mode-map (make-keymap))
6110   (suppress-keymap gnus-dead-summary-mode-map)
6111   (substitute-key-definition
6112    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6113   (let ((keys '("\C-d" "\r" "\177" [delete])))
6114     (while keys
6115       (define-key gnus-dead-summary-mode-map
6116         (pop keys) 'gnus-summary-wake-up-the-dead))))
6117
6118 (defvar gnus-dead-summary-mode nil
6119   "Minor mode for Gnus summary buffers.")
6120
6121 (defun gnus-dead-summary-mode (&optional arg)
6122   "Minor mode for Gnus summary buffers."
6123   (interactive "P")
6124   (when (eq major-mode 'gnus-summary-mode)
6125     (make-local-variable 'gnus-dead-summary-mode)
6126     (setq gnus-dead-summary-mode
6127           (if (null arg) (not gnus-dead-summary-mode)
6128             (> (prefix-numeric-value arg) 0)))
6129     (when gnus-dead-summary-mode
6130       (gnus-add-minor-mode
6131        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6132
6133 (defun gnus-deaden-summary ()
6134   "Make the current summary buffer into a dead summary buffer."
6135   ;; Kill any previous dead summary buffer.
6136   (when (and gnus-dead-summary
6137              (buffer-name gnus-dead-summary))
6138     (save-excursion
6139       (set-buffer gnus-dead-summary)
6140       (when gnus-dead-summary-mode
6141         (kill-buffer (current-buffer)))))
6142   ;; Make this the current dead summary.
6143   (setq gnus-dead-summary (current-buffer))
6144   (gnus-dead-summary-mode 1)
6145   (let ((name (buffer-name)))
6146     (when (string-match "Summary" name)
6147       (rename-buffer
6148        (concat (substring name 0 (match-beginning 0)) "Dead "
6149                (substring name (match-beginning 0)))
6150        t)
6151       (bury-buffer))))
6152
6153 (defun gnus-kill-or-deaden-summary (buffer)
6154   "Kill or deaden the summary BUFFER."
6155   (save-excursion
6156     (when (and (buffer-name buffer)
6157                (not gnus-single-article-buffer))
6158       (save-excursion
6159         (set-buffer buffer)
6160         (gnus-kill-buffer gnus-article-buffer)
6161         (gnus-kill-buffer gnus-original-article-buffer)))
6162     (cond (gnus-kill-summary-on-exit
6163            (when (and gnus-use-trees
6164                       (gnus-buffer-exists-p buffer))
6165              (save-excursion
6166                (set-buffer buffer)
6167                (gnus-tree-close gnus-newsgroup-name)))
6168            (gnus-kill-buffer buffer))
6169           ((gnus-buffer-exists-p buffer)
6170            (save-excursion
6171              (set-buffer buffer)
6172              (gnus-deaden-summary))))))
6173
6174 (defun gnus-summary-wake-up-the-dead (&rest args)
6175   "Wake up the dead summary buffer."
6176   (interactive)
6177   (gnus-dead-summary-mode -1)
6178   (let ((name (buffer-name)))
6179     (when (string-match "Dead " name)
6180       (rename-buffer
6181        (concat (substring name 0 (match-beginning 0))
6182                (substring name (match-end 0)))
6183        t)))
6184   (gnus-message 3 "This dead summary is now alive again"))
6185
6186 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6187 (defun gnus-summary-fetch-faq (&optional faq-dir)
6188   "Fetch the FAQ for the current group.
6189 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6190 in."
6191   (interactive
6192    (list
6193     (when current-prefix-arg
6194       (completing-read
6195        "Faq dir: " (and (listp gnus-group-faq-directory)
6196                         (mapcar (lambda (file) (list file))
6197                                 gnus-group-faq-directory))))))
6198   (let (gnus-faq-buffer)
6199     (when (setq gnus-faq-buffer
6200                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6201       (gnus-configure-windows 'summary-faq))))
6202
6203 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6204 (defun gnus-summary-describe-group (&optional force)
6205   "Describe the current newsgroup."
6206   (interactive "P")
6207   (gnus-group-describe-group force gnus-newsgroup-name))
6208
6209 (defun gnus-summary-describe-briefly ()
6210   "Describe summary mode commands briefly."
6211   (interactive)
6212   (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")))
6213
6214 ;; Walking around group mode buffer from summary mode.
6215
6216 (defun gnus-summary-next-group (&optional no-article target-group backward)
6217   "Exit current newsgroup and then select next unread newsgroup.
6218 If prefix argument NO-ARTICLE is non-nil, no article is selected
6219 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6220 previous group instead."
6221   (interactive "P")
6222   ;; Stop pre-fetching.
6223   (gnus-async-halt-prefetch)
6224   (let ((current-group gnus-newsgroup-name)
6225         (current-buffer (current-buffer))
6226         entered)
6227     ;; First we semi-exit this group to update Xrefs and all variables.
6228     ;; We can't do a real exit, because the window conf must remain
6229     ;; the same in case the user is prompted for info, and we don't
6230     ;; want the window conf to change before that...
6231     (gnus-summary-exit t)
6232     (while (not entered)
6233       ;; Then we find what group we are supposed to enter.
6234       (set-buffer gnus-group-buffer)
6235       (gnus-group-jump-to-group current-group)
6236       (setq target-group
6237             (or target-group
6238                 (if (eq gnus-keep-same-level 'best)
6239                     (gnus-summary-best-group gnus-newsgroup-name)
6240                   (gnus-summary-search-group backward gnus-keep-same-level))))
6241       (if (not target-group)
6242           ;; There are no further groups, so we return to the group
6243           ;; buffer.
6244           (progn
6245             (gnus-message 5 "Returning to the group buffer")
6246             (setq entered t)
6247             (when (gnus-buffer-live-p current-buffer)
6248               (set-buffer current-buffer)
6249               (gnus-summary-exit))
6250             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6251         ;; We try to enter the target group.
6252         (gnus-group-jump-to-group target-group)
6253         (let ((unreads (gnus-group-group-unread)))
6254           (if (and (or (eq t unreads)
6255                        (and unreads (not (zerop unreads))))
6256                    (gnus-summary-read-group
6257                     target-group nil no-article
6258                     (and (buffer-name current-buffer) current-buffer)
6259                     nil backward))
6260               (setq entered t)
6261             (setq current-group target-group
6262                   target-group nil)))))))
6263
6264 (defun gnus-summary-prev-group (&optional no-article)
6265   "Exit current newsgroup and then select previous unread newsgroup.
6266 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6267   (interactive "P")
6268   (gnus-summary-next-group no-article nil t))
6269
6270 ;; Walking around summary lines.
6271
6272 (defun gnus-summary-first-subject (&optional unread undownloaded)
6273   "Go to the first unread subject.
6274 If UNREAD is non-nil, go to the first unread article.
6275 Returns the article selected or nil if there are no unread articles."
6276   (interactive "P")
6277   (prog1
6278       (cond
6279        ;; Empty summary.
6280        ((null gnus-newsgroup-data)
6281         (gnus-message 3 "No articles in the group")
6282         nil)
6283        ;; Pick the first article.
6284        ((not unread)
6285         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6286         (gnus-data-number (car gnus-newsgroup-data)))
6287        ;; No unread articles.
6288        ((null gnus-newsgroup-unreads)
6289         (gnus-message 3 "No more unread articles")
6290         nil)
6291        ;; Find the first unread article.
6292        (t
6293         (let ((data gnus-newsgroup-data))
6294           (while (and data
6295                       (and (not (and undownloaded
6296                                      (eq gnus-undownloaded-mark
6297                                          (gnus-data-mark (car data)))))
6298                            (not (gnus-data-unread-p (car data)))))
6299             (setq data (cdr data)))
6300           (when data
6301             (goto-char (gnus-data-pos (car data)))
6302             (gnus-data-number (car data))))))
6303     (gnus-summary-position-point)))
6304
6305 (defun gnus-summary-next-subject (n &optional unread dont-display)
6306   "Go to next N'th summary line.
6307 If N is negative, go to the previous N'th subject line.
6308 If UNREAD is non-nil, only unread articles are selected.
6309 The difference between N and the actual number of steps taken is
6310 returned."
6311   (interactive "p")
6312   (let ((backward (< n 0))
6313         (n (abs n)))
6314     (while (and (> n 0)
6315                 (if backward
6316                     (gnus-summary-find-prev unread)
6317                   (gnus-summary-find-next unread)))
6318       (unless (zerop (setq n (1- n)))
6319         (gnus-summary-show-thread)))
6320     (when (/= 0 n)
6321       (gnus-message 7 "No more%s articles"
6322                     (if unread " unread" "")))
6323     (unless dont-display
6324       (gnus-summary-recenter)
6325       (gnus-summary-position-point))
6326     n))
6327
6328 (defun gnus-summary-next-unread-subject (n)
6329   "Go to next N'th unread summary line."
6330   (interactive "p")
6331   (gnus-summary-next-subject n t))
6332
6333 (defun gnus-summary-prev-subject (n &optional unread)
6334   "Go to previous N'th summary line.
6335 If optional argument UNREAD is non-nil, only unread article is selected."
6336   (interactive "p")
6337   (gnus-summary-next-subject (- n) unread))
6338
6339 (defun gnus-summary-prev-unread-subject (n)
6340   "Go to previous N'th unread summary line."
6341   (interactive "p")
6342   (gnus-summary-next-subject (- n) t))
6343
6344 (defun gnus-summary-goto-subject (article &optional force silent)
6345   "Go the subject line of ARTICLE.
6346 If FORCE, also allow jumping to articles not currently shown."
6347   (interactive "nArticle number: ")
6348   (let ((b (point))
6349         (data (gnus-data-find article)))
6350     ;; We read in the article if we have to.
6351     (and (not data)
6352          force
6353          (gnus-summary-insert-subject
6354           article
6355           (if (or (numberp force) (vectorp force)) force)
6356           t)
6357          (setq data (gnus-data-find article)))
6358     (goto-char b)
6359     (if (not data)
6360         (progn
6361           (unless silent
6362             (gnus-message 3 "Can't find article %d" article))
6363           nil)
6364       (let ((pt (gnus-data-pos data)))
6365         (goto-char pt)
6366         (gnus-summary-set-article-display-arrow pt))
6367       (gnus-summary-position-point)
6368       article)))
6369
6370 ;; Walking around summary lines with displaying articles.
6371
6372 (defun gnus-summary-expand-window (&optional arg)
6373   "Make the summary buffer take up the entire Emacs frame.
6374 Given a prefix, will force an `article' buffer configuration."
6375   (interactive "P")
6376   (if arg
6377       (gnus-configure-windows 'article 'force)
6378     (gnus-configure-windows 'summary 'force)))
6379
6380 (defun gnus-summary-display-article (article &optional all-header)
6381   "Display ARTICLE in article buffer."
6382   (when (gnus-buffer-live-p gnus-article-buffer)
6383     (with-current-buffer gnus-article-buffer
6384       (set-buffer-multibyte t)))
6385   (gnus-set-global-variables)
6386   (when (gnus-buffer-live-p gnus-article-buffer)
6387     (with-current-buffer gnus-article-buffer
6388       (setq gnus-article-charset gnus-newsgroup-charset)
6389       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6390   (if (null article)
6391       nil
6392     (prog1
6393         (if gnus-summary-display-article-function
6394             (funcall gnus-summary-display-article-function article all-header)
6395           (gnus-article-prepare article all-header))
6396       (with-current-buffer gnus-article-buffer
6397         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6398              nil))
6399       (gnus-run-hooks 'gnus-select-article-hook)
6400       (when (and gnus-current-article
6401                  (not (zerop gnus-current-article)))
6402         (gnus-summary-goto-subject gnus-current-article))
6403       (gnus-summary-recenter)
6404       (when (and gnus-use-trees gnus-show-threads)
6405         (gnus-possibly-generate-tree article)
6406         (gnus-highlight-selected-tree article))
6407       ;; Successfully display article.
6408       (gnus-article-set-window-start
6409        (cdr (assq article gnus-newsgroup-bookmarks))))))
6410
6411 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6412   "Select the current article.
6413 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6414 non-nil, the article will be re-fetched even if it already present in
6415 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6416 be displayed."
6417   ;; Make sure we are in the summary buffer to work around bbdb bug.
6418   (unless (eq major-mode 'gnus-summary-mode)
6419     (set-buffer gnus-summary-buffer))
6420   (let ((article (or article (gnus-summary-article-number)))
6421         (all-headers (not (not all-headers))) ;Must be T or NIL.
6422         gnus-summary-display-article-function)
6423     (and (not pseudo)
6424          (gnus-summary-article-pseudo-p article)
6425          (error "This is a pseudo-article"))
6426     (save-excursion
6427       (set-buffer gnus-summary-buffer)
6428       (if (or (and gnus-single-article-buffer
6429                    (or (null gnus-current-article)
6430                        (null gnus-article-current)
6431                        (null (get-buffer gnus-article-buffer))
6432                        (not (eq article (cdr gnus-article-current)))
6433                        (not (equal (car gnus-article-current)
6434                                    gnus-newsgroup-name))))
6435               (and (not gnus-single-article-buffer)
6436                    (or (null gnus-current-article)
6437                        (not (eq gnus-current-article article))))
6438               force)
6439           ;; The requested article is different from the current article.
6440           (progn
6441             (gnus-summary-display-article article all-headers)
6442             (when (or all-headers gnus-show-all-headers)
6443               (gnus-article-show-all-headers))
6444             (gnus-article-set-window-start
6445              (cdr (assq article gnus-newsgroup-bookmarks)))
6446             article)
6447         (when (or all-headers gnus-show-all-headers)
6448           (gnus-article-show-all-headers))
6449         'old))))
6450
6451 (defun gnus-summary-force-verify-and-decrypt ()
6452   (interactive)
6453   (let ((mm-verify-option 'known)
6454         (mm-decrypt-option 'known))
6455     (gnus-summary-select-article nil 'force)))
6456
6457 (defun gnus-summary-set-current-mark (&optional current-mark)
6458   "Obsolete function."
6459   nil)
6460
6461 (defun gnus-summary-next-article (&optional unread subject backward push)
6462   "Select the next article.
6463 If UNREAD, only unread articles are selected.
6464 If SUBJECT, only articles with SUBJECT are selected.
6465 If BACKWARD, the previous article is selected instead of the next."
6466   (interactive "P")
6467   (cond
6468    ;; Is there such an article?
6469    ((and (gnus-summary-search-forward unread subject backward)
6470          (or (gnus-summary-display-article (gnus-summary-article-number))
6471              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6472     (gnus-summary-position-point))
6473    ;; If not, we try the first unread, if that is wanted.
6474    ((and subject
6475          gnus-auto-select-same
6476          (gnus-summary-first-unread-article))
6477     (gnus-summary-position-point)
6478     (gnus-message 6 "Wrapped"))
6479    ;; Try to get next/previous article not displayed in this group.
6480    ((and gnus-auto-extend-newsgroup
6481          (not unread) (not subject))
6482     (gnus-summary-goto-article
6483      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6484      nil (count-lines (point-min) (point))))
6485    ;; Go to next/previous group.
6486    (t
6487     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6488       (gnus-summary-jump-to-group gnus-newsgroup-name))
6489     (let ((cmd last-command-char)
6490           (point
6491            (save-excursion
6492              (set-buffer gnus-group-buffer)
6493              (point)))
6494           (group
6495            (if (eq gnus-keep-same-level 'best)
6496                (gnus-summary-best-group gnus-newsgroup-name)
6497              (gnus-summary-search-group backward gnus-keep-same-level))))
6498       ;; For some reason, the group window gets selected.  We change
6499       ;; it back.
6500       (select-window (get-buffer-window (current-buffer)))
6501       ;; Select next unread newsgroup automagically.
6502       (cond
6503        ((or (not gnus-auto-select-next)
6504             (not cmd))
6505         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6506        ((or (eq gnus-auto-select-next 'quietly)
6507             (and (eq gnus-auto-select-next 'slightly-quietly)
6508                  push)
6509             (and (eq gnus-auto-select-next 'almost-quietly)
6510                  (gnus-summary-last-article-p)))
6511         ;; Select quietly.
6512         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6513             (gnus-summary-exit)
6514           (gnus-message 7 "No more%s articles (%s)..."
6515                         (if unread " unread" "")
6516                         (if group (concat "selecting " group)
6517                           "exiting"))
6518           (gnus-summary-next-group nil group backward)))
6519        (t
6520         (when (gnus-key-press-event-p last-input-event)
6521           (gnus-summary-walk-group-buffer
6522            gnus-newsgroup-name cmd unread backward point))))))))
6523
6524 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6525   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6526                       (?\C-p (gnus-group-prev-unread-group 1))))
6527         (cursor-in-echo-area t)
6528         keve key group ended)
6529     (save-excursion
6530       (set-buffer gnus-group-buffer)
6531       (goto-char start)
6532       (setq group
6533             (if (eq gnus-keep-same-level 'best)
6534                 (gnus-summary-best-group gnus-newsgroup-name)
6535               (gnus-summary-search-group backward gnus-keep-same-level))))
6536     (while (not ended)
6537       (gnus-message
6538        5 "No more%s articles%s" (if unread " unread" "")
6539        (if (and group
6540                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6541            (format " (Type %s for %s [%s])"
6542                    (single-key-description cmd) group
6543                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6544          (format " (Type %s to exit %s)"
6545                  (single-key-description cmd)
6546                  gnus-newsgroup-name)))
6547       ;; Confirm auto selection.
6548       (setq key (car (setq keve (gnus-read-event-char))))
6549       (setq ended t)
6550       (cond
6551        ((assq key keystrokes)
6552         (let ((obuf (current-buffer)))
6553           (switch-to-buffer gnus-group-buffer)
6554           (when group
6555             (gnus-group-jump-to-group group))
6556           (eval (cadr (assq key keystrokes)))
6557           (setq group (gnus-group-group-name))
6558           (switch-to-buffer obuf))
6559         (setq ended nil))
6560        ((equal key cmd)
6561         (if (or (not group)
6562                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6563             (gnus-summary-exit)
6564           (gnus-summary-next-group nil group backward)))
6565        (t
6566         (push (cdr keve) unread-command-events))))))
6567
6568 (defun gnus-summary-next-unread-article ()
6569   "Select unread article after current one."
6570   (interactive)
6571   (gnus-summary-next-article
6572    (or (not (eq gnus-summary-goto-unread 'never))
6573        (gnus-summary-last-article-p (gnus-summary-article-number)))
6574    (and gnus-auto-select-same
6575         (gnus-summary-article-subject))))
6576
6577 (defun gnus-summary-prev-article (&optional unread subject)
6578   "Select the article after the current one.
6579 If UNREAD is non-nil, only unread articles are selected."
6580   (interactive "P")
6581   (gnus-summary-next-article unread subject t))
6582
6583 (defun gnus-summary-prev-unread-article ()
6584   "Select unread article before current one."
6585   (interactive)
6586   (gnus-summary-prev-article
6587    (or (not (eq gnus-summary-goto-unread 'never))
6588        (gnus-summary-first-article-p (gnus-summary-article-number)))
6589    (and gnus-auto-select-same
6590         (gnus-summary-article-subject))))
6591
6592 (defun gnus-summary-next-page (&optional lines circular)
6593   "Show next page of the selected article.
6594 If at the end of the current article, select the next article.
6595 LINES says how many lines should be scrolled up.
6596
6597 If CIRCULAR is non-nil, go to the start of the article instead of
6598 selecting the next article when reaching the end of the current
6599 article."
6600   (interactive "P")
6601   (setq gnus-summary-buffer (current-buffer))
6602   (gnus-set-global-variables)
6603   (let ((article (gnus-summary-article-number))
6604         (article-window (get-buffer-window gnus-article-buffer t))
6605         endp)
6606     ;; If the buffer is empty, we have no article.
6607     (unless article
6608       (error "No article to select"))
6609     (gnus-configure-windows 'article)
6610     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6611         (if (and (eq gnus-summary-goto-unread 'never)
6612                  (not (gnus-summary-last-article-p article)))
6613             (gnus-summary-next-article)
6614           (gnus-summary-next-unread-article))
6615       (if (or (null gnus-current-article)
6616               (null gnus-article-current)
6617               (/= article (cdr gnus-article-current))
6618               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6619           ;; Selected subject is different from current article's.
6620           (gnus-summary-display-article article)
6621         (when article-window
6622           (gnus-eval-in-buffer-window gnus-article-buffer
6623             (setq endp (gnus-article-next-page lines)))
6624           (when endp
6625             (cond (circular
6626                    (gnus-summary-beginning-of-article))
6627                   (lines
6628                    (gnus-message 3 "End of message"))
6629                   ((null lines)
6630                    (if (and (eq gnus-summary-goto-unread 'never)
6631                             (not (gnus-summary-last-article-p article)))
6632                        (gnus-summary-next-article)
6633                      (gnus-summary-next-unread-article))))))))
6634     (gnus-summary-recenter)
6635     (gnus-summary-position-point)))
6636
6637 (defun gnus-summary-prev-page (&optional lines move)
6638   "Show previous page of selected article.
6639 Argument LINES specifies lines to be scrolled down.
6640 If MOVE, move to the previous unread article if point is at
6641 the beginning of the buffer."
6642   (interactive "P")
6643   (let ((article (gnus-summary-article-number))
6644         (article-window (get-buffer-window gnus-article-buffer t))
6645         endp)
6646     (gnus-configure-windows 'article)
6647     (if (or (null gnus-current-article)
6648             (null gnus-article-current)
6649             (/= article (cdr gnus-article-current))
6650             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6651         ;; Selected subject is different from current article's.
6652         (gnus-summary-display-article article)
6653       (gnus-summary-recenter)
6654       (when article-window
6655         (gnus-eval-in-buffer-window gnus-article-buffer
6656           (setq endp (gnus-article-prev-page lines)))
6657         (when (and move endp)
6658           (cond (lines
6659                  (gnus-message 3 "Beginning of message"))
6660                 ((null lines)
6661                  (if (and (eq gnus-summary-goto-unread 'never)
6662                           (not (gnus-summary-first-article-p article)))
6663                      (gnus-summary-prev-article)
6664                    (gnus-summary-prev-unread-article))))))))
6665   (gnus-summary-position-point))
6666
6667 (defun gnus-summary-prev-page-or-article (&optional lines)
6668   "Show previous page of selected article.
6669 Argument LINES specifies lines to be scrolled down.
6670 If at the beginning of the article, go to the next article."
6671   (interactive "P")
6672   (gnus-summary-prev-page lines t))
6673
6674 (defun gnus-summary-scroll-up (lines)
6675   "Scroll up (or down) one line current article.
6676 Argument LINES specifies lines to be scrolled up (or down if negative)."
6677   (interactive "p")
6678   (gnus-configure-windows 'article)
6679   (gnus-summary-show-thread)
6680   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6681     (gnus-eval-in-buffer-window gnus-article-buffer
6682       (cond ((> lines 0)
6683              (when (gnus-article-next-page lines)
6684                (gnus-message 3 "End of message")))
6685             ((< lines 0)
6686              (gnus-article-prev-page (- lines))))))
6687   (gnus-summary-recenter)
6688   (gnus-summary-position-point))
6689
6690 (defun gnus-summary-scroll-down (lines)
6691   "Scroll down (or up) one line current article.
6692 Argument LINES specifies lines to be scrolled down (or up if negative)."
6693   (interactive "p")
6694   (gnus-summary-scroll-up (- lines)))
6695
6696 (defun gnus-summary-next-same-subject ()
6697   "Select next article which has the same subject as current one."
6698   (interactive)
6699   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6700
6701 (defun gnus-summary-prev-same-subject ()
6702   "Select previous article which has the same subject as current one."
6703   (interactive)
6704   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6705
6706 (defun gnus-summary-next-unread-same-subject ()
6707   "Select next unread article which has the same subject as current one."
6708   (interactive)
6709   (gnus-summary-next-article t (gnus-summary-article-subject)))
6710
6711 (defun gnus-summary-prev-unread-same-subject ()
6712   "Select previous unread article which has the same subject as current one."
6713   (interactive)
6714   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6715
6716 (defun gnus-summary-first-unread-article ()
6717   "Select the first unread article.
6718 Return nil if there are no unread articles."
6719   (interactive)
6720   (prog1
6721       (when (gnus-summary-first-subject t)
6722         (gnus-summary-show-thread)
6723         (gnus-summary-first-subject t)
6724         (gnus-summary-display-article (gnus-summary-article-number)))
6725     (gnus-summary-position-point)))
6726
6727 (defun gnus-summary-first-unread-subject ()
6728   "Place the point on the subject line of the first unread article.
6729 Return nil if there are no unread articles."
6730   (interactive)
6731   (prog1
6732       (when (gnus-summary-first-subject t)
6733         (gnus-summary-show-thread)
6734         (gnus-summary-first-subject t))
6735     (gnus-summary-position-point)))
6736
6737 (defun gnus-summary-first-article ()
6738   "Select the first article.
6739 Return nil if there are no articles."
6740   (interactive)
6741   (prog1
6742       (when (gnus-summary-first-subject)
6743         (gnus-summary-show-thread)
6744         (gnus-summary-first-subject)
6745         (gnus-summary-display-article (gnus-summary-article-number)))
6746     (gnus-summary-position-point)))
6747
6748 (defun gnus-summary-best-unread-article ()
6749   "Select the unread article with the highest score."
6750   (interactive)
6751   (let ((best -1000000)
6752         (data gnus-newsgroup-data)
6753         article score)
6754     (while data
6755       (and (gnus-data-unread-p (car data))
6756            (> (setq score
6757                     (gnus-summary-article-score (gnus-data-number (car data))))
6758               best)
6759            (setq best score
6760                  article (gnus-data-number (car data))))
6761       (setq data (cdr data)))
6762     (prog1
6763         (if article
6764             (gnus-summary-goto-article article)
6765           (error "No unread articles"))
6766       (gnus-summary-position-point))))
6767
6768 (defun gnus-summary-last-subject ()
6769   "Go to the last displayed subject line in the group."
6770   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6771     (when article
6772       (gnus-summary-goto-subject article))))
6773
6774 (defun gnus-summary-goto-article (article &optional all-headers force)
6775   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6776 If ALL-HEADERS is non-nil, no header lines are hidden.
6777 If FORCE, go to the article even if it isn't displayed.  If FORCE
6778 is a number, it is the line the article is to be displayed on."
6779   (interactive
6780    (list
6781     (completing-read
6782      "Article number or Message-ID: "
6783      (mapcar (lambda (number) (list (int-to-string number)))
6784              gnus-newsgroup-limit))
6785     current-prefix-arg
6786     t))
6787   (prog1
6788       (if (and (stringp article)
6789                (string-match "@" article))
6790           (gnus-summary-refer-article article)
6791         (when (stringp article)
6792           (setq article (string-to-number article)))
6793         (if (gnus-summary-goto-subject article force)
6794             (gnus-summary-display-article article all-headers)
6795           (gnus-message 4 "Couldn't go to article %s" article) nil))
6796     (gnus-summary-position-point)))
6797
6798 (defun gnus-summary-goto-last-article ()
6799   "Go to the previously read article."
6800   (interactive)
6801   (prog1
6802       (when gnus-last-article
6803         (gnus-summary-goto-article gnus-last-article nil t))
6804     (gnus-summary-position-point)))
6805
6806 (defun gnus-summary-pop-article (number)
6807   "Pop one article off the history and go to the previous.
6808 NUMBER articles will be popped off."
6809   (interactive "p")
6810   (let (to)
6811     (setq gnus-newsgroup-history
6812           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6813     (if to
6814         (gnus-summary-goto-article (car to) nil t)
6815       (error "Article history empty")))
6816   (gnus-summary-position-point))
6817
6818 ;; Summary commands and functions for limiting the summary buffer.
6819
6820 (defun gnus-summary-limit-to-articles (n)
6821   "Limit the summary buffer to the next N articles.
6822 If not given a prefix, use the process marked articles instead."
6823   (interactive "P")
6824   (prog1
6825       (let ((articles (gnus-summary-work-articles n)))
6826         (setq gnus-newsgroup-processable nil)
6827         (gnus-summary-limit articles))
6828     (gnus-summary-position-point)))
6829
6830 (defun gnus-summary-pop-limit (&optional total)
6831   "Restore the previous limit.
6832 If given a prefix, remove all limits."
6833   (interactive "P")
6834   (when total
6835     (setq gnus-newsgroup-limits
6836           (list (mapcar (lambda (h) (mail-header-number h))
6837                         gnus-newsgroup-headers))))
6838   (unless gnus-newsgroup-limits
6839     (error "No limit to pop"))
6840   (prog1
6841       (gnus-summary-limit nil 'pop)
6842     (gnus-summary-position-point)))
6843
6844 (defun gnus-summary-limit-to-subject (subject &optional header)
6845   "Limit the summary buffer to articles that have subjects that match a regexp."
6846   (interactive "sLimit to subject (regexp): ")
6847   (unless header
6848     (setq header "subject"))
6849   (when (not (equal "" subject))
6850     (prog1
6851         (let ((articles (gnus-summary-find-matching
6852                          (or header "subject") subject 'all)))
6853           (unless articles
6854             (error "Found no matches for \"%s\"" subject))
6855           (gnus-summary-limit articles))
6856       (gnus-summary-position-point))))
6857
6858 (defun gnus-summary-limit-to-author (from)
6859   "Limit the summary buffer to articles that have authors that match a regexp."
6860   (interactive "sLimit to author (regexp): ")
6861   (gnus-summary-limit-to-subject from "from"))
6862
6863 (defun gnus-summary-limit-to-age (age &optional younger-p)
6864   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6865 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6866 articles that are younger than AGE days."
6867   (interactive
6868    (let ((younger current-prefix-arg)
6869          (days-got nil)
6870          days)
6871      (while (not days-got)
6872        (setq days (if younger
6873                       (read-string "Limit to articles within (in days): ")
6874                     (read-string "Limit to articles older than (in days): ")))
6875        (when (> (length days) 0)
6876          (setq days (read days)))
6877        (if (numberp days)
6878            (setq days-got t)
6879          (message "Please enter a number.")
6880          (sleep-for 1)))
6881      (list days younger)))
6882   (prog1
6883       (let ((data gnus-newsgroup-data)
6884             (cutoff (days-to-time age))
6885             articles d date is-younger)
6886         (while (setq d (pop data))
6887           (when (and (vectorp (gnus-data-header d))
6888                      (setq date (mail-header-date (gnus-data-header d))))
6889             (setq is-younger (time-less-p
6890                               (time-since (condition-case ()
6891                                               (date-to-time date)
6892                                             (error '(0 0))))
6893                               cutoff))
6894             (when (if younger-p
6895                       is-younger
6896                     (not is-younger))
6897               (push (gnus-data-number d) articles))))
6898         (gnus-summary-limit (nreverse articles)))
6899     (gnus-summary-position-point)))
6900
6901 (defun gnus-summary-limit-to-extra (header regexp)
6902   "Limit the summary buffer to articles that match an 'extra' header."
6903   (interactive
6904    (let ((header
6905           (intern
6906            (gnus-completing-read
6907             (symbol-name (car gnus-extra-headers))
6908             "Limit extra header:"
6909             (mapcar (lambda (x)
6910                       (cons (symbol-name x) x))
6911                     gnus-extra-headers)
6912             nil
6913             t))))
6914      (list header
6915            (read-string (format "Limit to header %s (regexp): " header)))))
6916   (when (not (equal "" regexp))
6917     (prog1
6918         (let ((articles (gnus-summary-find-matching
6919                          (cons 'extra header) regexp 'all)))
6920           (unless articles
6921             (error "Found no matches for \"%s\"" regexp))
6922           (gnus-summary-limit articles))
6923       (gnus-summary-position-point))))
6924
6925 (defun gnus-summary-limit-to-display-predicate ()
6926   "Limit the summary buffer to the predicated in the `display' group parameter."
6927   (interactive)
6928   (unless gnus-newsgroup-display
6929     (error "There is no `diplay' group parameter"))
6930   (let (articles)
6931     (dolist (number gnus-newsgroup-articles)
6932       (when (funcall gnus-newsgroup-display)
6933         (push number articles)))
6934     (gnus-summary-limit articles))
6935   (gnus-summary-position-point))
6936
6937 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6938 (make-obsolete
6939  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6940
6941 (defun gnus-summary-limit-to-unread (&optional all)
6942   "Limit the summary buffer to articles that are not marked as read.
6943 If ALL is non-nil, limit strictly to unread articles."
6944   (interactive "P")
6945   (if all
6946       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6947     (gnus-summary-limit-to-marks
6948      ;; Concat all the marks that say that an article is read and have
6949      ;; those removed.
6950      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6951            gnus-killed-mark gnus-kill-file-mark
6952            gnus-low-score-mark gnus-expirable-mark
6953            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6954            gnus-duplicate-mark gnus-souped-mark)
6955      'reverse)))
6956
6957 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6958 (make-obsolete 'gnus-summary-delete-marked-with
6959                'gnus-summary-limit-exlude-marks)
6960
6961 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6962   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6963 If REVERSE, limit the summary buffer to articles that are marked
6964 with MARKS.  MARKS can either be a string of marks or a list of marks.
6965 Returns how many articles were removed."
6966   (interactive "sMarks: ")
6967   (gnus-summary-limit-to-marks marks t))
6968
6969 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6970   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6971 If REVERSE (the prefix), limit the summary buffer to articles that are
6972 not marked with MARKS.  MARKS can either be a string of marks or a
6973 list of marks.
6974 Returns how many articles were removed."
6975   (interactive "sMarks: \nP")
6976   (prog1
6977       (let ((data gnus-newsgroup-data)
6978             (marks (if (listp marks) marks
6979                      (append marks nil))) ; Transform to list.
6980             articles)
6981         (while data
6982           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6983                   (memq (gnus-data-mark (car data)) marks))
6984             (push (gnus-data-number (car data)) articles))
6985           (setq data (cdr data)))
6986         (gnus-summary-limit articles))
6987     (gnus-summary-position-point)))
6988
6989 (defun gnus-summary-limit-to-score (&optional score)
6990   "Limit to articles with score at or above SCORE."
6991   (interactive "P")
6992   (setq score (if score
6993                   (prefix-numeric-value score)
6994                 (or gnus-summary-default-score 0)))
6995   (let ((data gnus-newsgroup-data)
6996         articles)
6997     (while data
6998       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6999                 score)
7000         (push (gnus-data-number (car data)) articles))
7001       (setq data (cdr data)))
7002     (prog1
7003         (gnus-summary-limit articles)
7004       (gnus-summary-position-point))))
7005
7006 (defun gnus-summary-limit-include-thread (id)
7007   "Display all the hidden articles that is in the thread with ID in it.
7008 When called interactively, ID is the Message-ID of the current
7009 article."
7010   (interactive (list (mail-header-id (gnus-summary-article-header))))
7011   (let ((articles (gnus-articles-in-thread
7012                    (gnus-id-to-thread (gnus-root-id id)))))
7013     (prog1
7014         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7015       (gnus-summary-limit-include-matching-articles
7016        "subject"
7017        (regexp-quote (gnus-simplify-subject-re
7018                       (mail-header-subject (gnus-id-to-header id)))))
7019       (gnus-summary-position-point))))
7020
7021 (defun gnus-summary-limit-include-matching-articles (header regexp)
7022   "Display all the hidden articles that have HEADERs that match REGEXP."
7023   (interactive (list (read-string "Match on header: ")
7024                      (read-string "Regexp: ")))
7025   (let ((articles (gnus-find-matching-articles header regexp)))
7026     (prog1
7027         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7028       (gnus-summary-position-point))))
7029
7030 (defun gnus-summary-limit-include-dormant ()
7031   "Display all the hidden articles that are marked as dormant.
7032 Note that this command only works on a subset of the articles currently
7033 fetched for this group."
7034   (interactive)
7035   (unless gnus-newsgroup-dormant
7036     (error "There are no dormant articles in this group"))
7037   (prog1
7038       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7039     (gnus-summary-position-point)))
7040
7041 (defun gnus-summary-limit-exclude-dormant ()
7042   "Hide all dormant articles."
7043   (interactive)
7044   (prog1
7045       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7046     (gnus-summary-position-point)))
7047
7048 (defun gnus-summary-limit-exclude-childless-dormant ()
7049   "Hide all dormant articles that have no children."
7050   (interactive)
7051   (let ((data (gnus-data-list t))
7052         articles d children)
7053     ;; Find all articles that are either not dormant or have
7054     ;; children.
7055     (while (setq d (pop data))
7056       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7057                 (and (setq children
7058                            (gnus-article-children (gnus-data-number d)))
7059                      (let (found)
7060                        (while children
7061                          (when (memq (car children) articles)
7062                            (setq children nil
7063                                  found t))
7064                          (pop children))
7065                        found)))
7066         (push (gnus-data-number d) articles)))
7067     ;; Do the limiting.
7068     (prog1
7069         (gnus-summary-limit articles)
7070       (gnus-summary-position-point))))
7071
7072 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7073   "Mark all unread excluded articles as read.
7074 If ALL, mark even excluded ticked and dormants as read."
7075   (interactive "P")
7076   (let ((articles (gnus-sorted-complement
7077                    (sort
7078                     (mapcar (lambda (h) (mail-header-number h))
7079                             gnus-newsgroup-headers)
7080                     '<)
7081                    (sort gnus-newsgroup-limit '<)))
7082         article)
7083     (setq gnus-newsgroup-unreads
7084           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7085     (if all
7086         (setq gnus-newsgroup-dormant nil
7087               gnus-newsgroup-marked nil
7088               gnus-newsgroup-reads
7089               (nconc
7090                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7091                gnus-newsgroup-reads))
7092       (while (setq article (pop articles))
7093         (unless (or (memq article gnus-newsgroup-dormant)
7094                     (memq article gnus-newsgroup-marked))
7095           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7096
7097 (defun gnus-summary-limit (articles &optional pop)
7098   (if pop
7099       ;; We pop the previous limit off the stack and use that.
7100       (setq articles (car gnus-newsgroup-limits)
7101             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7102     ;; We use the new limit, so we push the old limit on the stack.
7103     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7104   ;; Set the limit.
7105   (setq gnus-newsgroup-limit articles)
7106   (let ((total (length gnus-newsgroup-data))
7107         (data (gnus-data-find-list (gnus-summary-article-number)))
7108         (gnus-summary-mark-below nil)   ; Inhibit this.
7109         found)
7110     ;; This will do all the work of generating the new summary buffer
7111     ;; according to the new limit.
7112     (gnus-summary-prepare)
7113     ;; Hide any threads, possibly.
7114     (and gnus-show-threads
7115          gnus-thread-hide-subtree
7116          (gnus-summary-hide-all-threads))
7117     ;; Try to return to the article you were at, or one in the
7118     ;; neighborhood.
7119     (when data
7120       ;; We try to find some article after the current one.
7121       (while data
7122         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7123           (setq data nil
7124                 found t))
7125         (setq data (cdr data))))
7126     (unless found
7127       ;; If there is no data, that means that we were after the last
7128       ;; article.  The same goes when we can't find any articles
7129       ;; after the current one.
7130       (goto-char (point-max))
7131       (gnus-summary-find-prev))
7132     (gnus-set-mode-line 'summary)
7133     ;; We return how many articles were removed from the summary
7134     ;; buffer as a result of the new limit.
7135     (- total (length gnus-newsgroup-data))))
7136
7137 (defsubst gnus-invisible-cut-children (threads)
7138   (let ((num 0))
7139     (while threads
7140       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7141         (incf num))
7142       (pop threads))
7143     (< num 2)))
7144
7145 (defsubst gnus-cut-thread (thread)
7146   "Go forwards in the thread until we find an article that we want to display."
7147   (when (or (eq gnus-fetch-old-headers 'some)
7148             (eq gnus-fetch-old-headers 'invisible)
7149             (numberp gnus-fetch-old-headers)
7150             (eq gnus-build-sparse-threads 'some)
7151             (eq gnus-build-sparse-threads 'more))
7152     ;; Deal with old-fetched headers and sparse threads.
7153     (while (and
7154             thread
7155             (or
7156              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7157              (gnus-summary-article-ancient-p
7158               (mail-header-number (car thread))))
7159             (if (or (<= (length (cdr thread)) 1)
7160                     (eq gnus-fetch-old-headers 'invisible))
7161                 (setq gnus-newsgroup-limit
7162                       (delq (mail-header-number (car thread))
7163                             gnus-newsgroup-limit)
7164                       thread (cadr thread))
7165               (when (gnus-invisible-cut-children (cdr thread))
7166                 (let ((th (cdr thread)))
7167                   (while th
7168                     (if (memq (mail-header-number (caar th))
7169                               gnus-newsgroup-limit)
7170                         (setq thread (car th)
7171                               th nil)
7172                       (setq th (cdr th))))))))))
7173   thread)
7174
7175 (defun gnus-cut-threads (threads)
7176   "Cut off all uninteresting articles from the beginning of threads."
7177   (when (or (eq gnus-fetch-old-headers 'some)
7178             (eq gnus-fetch-old-headers 'invisible)
7179             (numberp gnus-fetch-old-headers)
7180             (eq gnus-build-sparse-threads 'some)
7181             (eq gnus-build-sparse-threads 'more))
7182     (let ((th threads))
7183       (while th
7184         (setcar th (gnus-cut-thread (car th)))
7185         (setq th (cdr th)))))
7186   ;; Remove nixed out threads.
7187   (delq nil threads))
7188
7189 (defun gnus-summary-initial-limit (&optional show-if-empty)
7190   "Figure out what the initial limit is supposed to be on group entry.
7191 This entails weeding out unwanted dormants, low-scored articles,
7192 fetch-old-headers verbiage, and so on."
7193   ;; Most groups have nothing to remove.
7194   (if (or gnus-inhibit-limiting
7195           (and (null gnus-newsgroup-dormant)
7196                (eq gnus-newsgroup-display 'gnus-not-ignore)
7197                (not (eq gnus-fetch-old-headers 'some))
7198                (not (numberp gnus-fetch-old-headers))
7199                (not (eq gnus-fetch-old-headers 'invisible))
7200                (null gnus-summary-expunge-below)
7201                (not (eq gnus-build-sparse-threads 'some))
7202                (not (eq gnus-build-sparse-threads 'more))
7203                (null gnus-thread-expunge-below)
7204                (not gnus-use-nocem)))
7205       ()                                ; Do nothing.
7206     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7207     (setq gnus-newsgroup-limit nil)
7208     (mapatoms
7209      (lambda (node)
7210        (unless (car (symbol-value node))
7211          ;; These threads have no parents -- they are roots.
7212          (let ((nodes (cdr (symbol-value node)))
7213                thread)
7214            (while nodes
7215              (if (and gnus-thread-expunge-below
7216                       (< (gnus-thread-total-score (car nodes))
7217                          gnus-thread-expunge-below))
7218                  (gnus-expunge-thread (pop nodes))
7219                (setq thread (pop nodes))
7220                (gnus-summary-limit-children thread))))))
7221      gnus-newsgroup-dependencies)
7222     ;; If this limitation resulted in an empty group, we might
7223     ;; pop the previous limit and use it instead.
7224     (when (and (not gnus-newsgroup-limit)
7225                show-if-empty)
7226       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7227     gnus-newsgroup-limit))
7228
7229 (defun gnus-summary-limit-children (thread)
7230   "Return 1 if this subthread is visible and 0 if it is not."
7231   ;; First we get the number of visible children to this thread.  This
7232   ;; is done by recursing down the thread using this function, so this
7233   ;; will really go down to a leaf article first, before slowly
7234   ;; working its way up towards the root.
7235   (when thread
7236     (let ((children
7237            (if (cdr thread)
7238                (apply '+ (mapcar 'gnus-summary-limit-children
7239                                  (cdr thread)))
7240              0))
7241           (number (mail-header-number (car thread)))
7242           score)
7243       (if (and
7244            (not (memq number gnus-newsgroup-marked))
7245            (or
7246             ;; If this article is dormant and has absolutely no visible
7247             ;; children, then this article isn't visible.
7248             (and (memq number gnus-newsgroup-dormant)
7249                  (zerop children))
7250             ;; If this is "fetch-old-headered" and there is no
7251             ;; visible children, then we don't want this article.
7252             (and (or (eq gnus-fetch-old-headers 'some)
7253                      (numberp gnus-fetch-old-headers))
7254                  (gnus-summary-article-ancient-p number)
7255                  (zerop children))
7256             ;; If this is "fetch-old-headered" and `invisible', then
7257             ;; we don't want this article.
7258             (and (eq gnus-fetch-old-headers 'invisible)
7259                  (gnus-summary-article-ancient-p number))
7260             ;; If this is a sparsely inserted article with no children,
7261             ;; we don't want it.
7262             (and (eq gnus-build-sparse-threads 'some)
7263                  (gnus-summary-article-sparse-p number)
7264                  (zerop children))
7265             ;; If we use expunging, and this article is really
7266             ;; low-scored, then we don't want this article.
7267             (when (and gnus-summary-expunge-below
7268                        (< (setq score
7269                                 (or (cdr (assq number gnus-newsgroup-scored))
7270                                     gnus-summary-default-score))
7271                           gnus-summary-expunge-below))
7272               ;; We increase the expunge-tally here, but that has
7273               ;; nothing to do with the limits, really.
7274               (incf gnus-newsgroup-expunged-tally)
7275               ;; We also mark as read here, if that's wanted.
7276               (when (and gnus-summary-mark-below
7277                          (< score gnus-summary-mark-below))
7278                 (setq gnus-newsgroup-unreads
7279                       (delq number gnus-newsgroup-unreads))
7280                 (if gnus-newsgroup-auto-expire
7281                     (push number gnus-newsgroup-expirable)
7282                   (push (cons number gnus-low-score-mark)
7283                         gnus-newsgroup-reads)))
7284               t)
7285             ;; Do the `display' group parameter.
7286             (and gnus-newsgroup-display
7287                  (not (funcall gnus-newsgroup-display)))
7288             ;; Check NoCeM things.
7289             (if (and gnus-use-nocem
7290                      (gnus-nocem-unwanted-article-p
7291                       (mail-header-id (car thread))))
7292                 (progn
7293                   (setq gnus-newsgroup-unreads
7294                         (delq number gnus-newsgroup-unreads))
7295                   t))))
7296           ;; Nope, invisible article.
7297           0
7298         ;; Ok, this article is to be visible, so we add it to the limit
7299         ;; and return 1.
7300         (push number gnus-newsgroup-limit)
7301         1))))
7302
7303 (defun gnus-expunge-thread (thread)
7304   "Mark all articles in THREAD as read."
7305   (let* ((number (mail-header-number (car thread))))
7306     (incf gnus-newsgroup-expunged-tally)
7307     ;; We also mark as read here, if that's wanted.
7308     (setq gnus-newsgroup-unreads
7309           (delq number gnus-newsgroup-unreads))
7310     (if gnus-newsgroup-auto-expire
7311         (push number gnus-newsgroup-expirable)
7312       (push (cons number gnus-low-score-mark)
7313             gnus-newsgroup-reads)))
7314   ;; Go recursively through all subthreads.
7315   (mapcar 'gnus-expunge-thread (cdr thread)))
7316
7317 ;; Summary article oriented commands
7318
7319 (defun gnus-summary-refer-parent-article (n)
7320   "Refer parent article N times.
7321 If N is negative, go to ancestor -N instead.
7322 The difference between N and the number of articles fetched is returned."
7323   (interactive "p")
7324   (let ((skip 1)
7325         error header ref)
7326     (when (not (natnump n))
7327       (setq skip (abs n)
7328             n 1))
7329     (while (and (> n 0)
7330                 (not error))
7331       (setq header (gnus-summary-article-header))
7332       (if (and (eq (mail-header-number header)
7333                    (cdr gnus-article-current))
7334                (equal gnus-newsgroup-name
7335                       (car gnus-article-current)))
7336           ;; If we try to find the parent of the currently
7337           ;; displayed article, then we take a look at the actual
7338           ;; References header, since this is slightly more
7339           ;; reliable than the References field we got from the
7340           ;; server.
7341           (save-excursion
7342             (set-buffer gnus-original-article-buffer)
7343             (nnheader-narrow-to-headers)
7344             (unless (setq ref (message-fetch-field "references"))
7345               (setq ref (message-fetch-field "in-reply-to")))
7346             (widen))
7347         (setq ref
7348               ;; It's not the current article, so we take a bet on
7349               ;; the value we got from the server.
7350               (mail-header-references header)))
7351       (if (and ref
7352                (not (equal ref "")))
7353           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7354             (gnus-message 1 "Couldn't find parent"))
7355         (gnus-message 1 "No references in article %d"
7356                       (gnus-summary-article-number))
7357         (setq error t))
7358       (decf n))
7359     (gnus-summary-position-point)
7360     n))
7361
7362 (defun gnus-summary-refer-references ()
7363   "Fetch all articles mentioned in the References header.
7364 Return the number of articles fetched."
7365   (interactive)
7366   (let ((ref (mail-header-references (gnus-summary-article-header)))
7367         (current (gnus-summary-article-number))
7368         (n 0))
7369     (if (or (not ref)
7370             (equal ref ""))
7371         (error "No References in the current article")
7372       ;; For each Message-ID in the References header...
7373       (while (string-match "<[^>]*>" ref)
7374         (incf n)
7375         ;; ... fetch that article.
7376         (gnus-summary-refer-article
7377          (prog1 (match-string 0 ref)
7378            (setq ref (substring ref (match-end 0))))))
7379       (gnus-summary-goto-subject current)
7380       (gnus-summary-position-point)
7381       n)))
7382
7383 (defun gnus-summary-refer-thread (&optional limit)
7384   "Fetch all articles in the current thread.
7385 If LIMIT (the numerical prefix), fetch that many old headers instead
7386 of what's specified by the `gnus-refer-thread-limit' variable."
7387   (interactive "P")
7388   (let ((id (mail-header-id (gnus-summary-article-header)))
7389         (limit (if limit (prefix-numeric-value limit)
7390                  gnus-refer-thread-limit)))
7391     ;; We want to fetch LIMIT *old* headers, but we also have to
7392     ;; re-fetch all the headers in the current buffer, because many of
7393     ;; them may be undisplayed.  So we adjust LIMIT.
7394     (when (numberp limit)
7395       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7396     (unless (eq gnus-fetch-old-headers 'invisible)
7397       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7398       ;; Retrieve the headers and read them in.
7399       (if (eq (gnus-retrieve-headers
7400                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7401               'nov)
7402           (gnus-build-all-threads)
7403         (error "Can't fetch thread from backends that don't support NOV"))
7404       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7405     (gnus-summary-limit-include-thread id)))
7406
7407 (defun gnus-summary-refer-article (message-id)
7408   "Fetch an article specified by MESSAGE-ID."
7409   (interactive "sMessage-ID: ")
7410   (when (and (stringp message-id)
7411              (not (zerop (length message-id))))
7412     ;; Construct the correct Message-ID if necessary.
7413     ;; Suggested by tale@pawl.rpi.edu.
7414     (unless (string-match "^<" message-id)
7415       (setq message-id (concat "<" message-id)))
7416     (unless (string-match ">$" message-id)
7417       (setq message-id (concat message-id ">")))
7418     (let* ((header (gnus-id-to-header message-id))
7419            (sparse (and header
7420                         (gnus-summary-article-sparse-p
7421                          (mail-header-number header))
7422                         (memq (mail-header-number header)
7423                               gnus-newsgroup-limit)))
7424            number)
7425       (cond
7426        ;; If the article is present in the buffer we just go to it.
7427        ((and header
7428              (or (not (gnus-summary-article-sparse-p
7429                        (mail-header-number header)))
7430                  sparse))
7431         (prog1
7432             (gnus-summary-goto-article
7433              (mail-header-number header) nil t)
7434           (when sparse
7435             (gnus-summary-update-article (mail-header-number header)))))
7436        (t
7437         ;; We fetch the article.
7438         (catch 'found
7439           (dolist (gnus-override-method (gnus-refer-article-methods))
7440             (gnus-check-server gnus-override-method)
7441             ;; Fetch the header, and display the article.
7442             (when (setq number (gnus-summary-insert-subject message-id))
7443               (gnus-summary-select-article nil nil nil number)
7444               (throw 'found t)))
7445           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7446
7447 (defun gnus-refer-article-methods ()
7448   "Return a list of referrable methods."
7449   (cond
7450    ;; No method, so we default to current and native.
7451    ((null gnus-refer-article-method)
7452     (list gnus-current-select-method gnus-select-method))
7453    ;; Current.
7454    ((eq 'current gnus-refer-article-method)
7455     (list gnus-current-select-method))
7456    ;; List of select methods.
7457    ((not (and (symbolp (car gnus-refer-article-method))
7458               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7459     (let (out)
7460       (dolist (method gnus-refer-article-method)
7461         (push (if (eq 'current method)
7462                   gnus-current-select-method
7463                 method)
7464               out))
7465       (nreverse out)))
7466    ;; One single select method.
7467    (t
7468     (list gnus-refer-article-method))))
7469
7470 (defun gnus-summary-edit-parameters ()
7471   "Edit the group parameters of the current group."
7472   (interactive)
7473   (gnus-group-edit-group gnus-newsgroup-name 'params))
7474
7475 (defun gnus-summary-customize-parameters ()
7476   "Customize the group parameters of the current group."
7477   (interactive)
7478   (gnus-group-customize gnus-newsgroup-name))
7479
7480 (defun gnus-summary-enter-digest-group (&optional force)
7481   "Enter an nndoc group based on the current article.
7482 If FORCE, force a digest interpretation.  If not, try
7483 to guess what the document format is."
7484   (interactive "P")
7485   (let ((conf gnus-current-window-configuration))
7486     (save-excursion
7487       (gnus-summary-select-article))
7488     (setq gnus-current-window-configuration conf)
7489     (let* ((name (format "%s-%d"
7490                          (gnus-group-prefixed-name
7491                           gnus-newsgroup-name (list 'nndoc ""))
7492                          (save-excursion
7493                            (set-buffer gnus-summary-buffer)
7494                            gnus-current-article)))
7495            (ogroup gnus-newsgroup-name)
7496            (params (append (gnus-info-params (gnus-get-info ogroup))
7497                            (list (cons 'to-group ogroup))
7498                            (list (cons 'save-article-group ogroup))))
7499            (case-fold-search t)
7500            (buf (current-buffer))
7501            dig to-address)
7502       (save-excursion
7503         (set-buffer gnus-original-article-buffer)
7504         ;; Have the digest group inherit the main mail address of
7505         ;; the parent article.
7506         (when (setq to-address (or (message-fetch-field "reply-to")
7507                                    (message-fetch-field "from")))
7508           (setq params (append
7509                         (list (cons 'to-address
7510                                     (funcall gnus-decode-encoded-word-function
7511                                              to-address))))))
7512         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7513         (insert-buffer-substring gnus-original-article-buffer)
7514         ;; Remove lines that may lead nndoc to misinterpret the
7515         ;; document type.
7516         (narrow-to-region
7517          (goto-char (point-min))
7518          (or (search-forward "\n\n" nil t) (point)))
7519         (goto-char (point-min))
7520         (delete-matching-lines "^Path:\\|^From ")
7521         (widen))
7522       (unwind-protect
7523           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7524                     (gnus-newsgroup-ephemeral-ignored-charsets
7525                      gnus-newsgroup-ignored-charsets))
7526                 (gnus-group-read-ephemeral-group
7527                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7528                               (nndoc-article-type
7529                                ,(if force 'mbox 'guess))) t))
7530               ;; Make all postings to this group go to the parent group.
7531               (nconc (gnus-info-params (gnus-get-info name))
7532                      params)
7533             ;; Couldn't select this doc group.
7534             (switch-to-buffer buf)
7535             (gnus-set-global-variables)
7536             (gnus-configure-windows 'summary)
7537             (gnus-message 3 "Article couldn't be entered?"))
7538         (kill-buffer dig)))))
7539
7540 (defun gnus-summary-read-document (n)
7541   "Open a new group based on the current article(s).
7542 This will allow you to read digests and other similar
7543 documents as newsgroups.
7544 Obeys the standard process/prefix convention."
7545   (interactive "P")
7546   (let* ((articles (gnus-summary-work-articles n))
7547          (ogroup gnus-newsgroup-name)
7548          (params (append (gnus-info-params (gnus-get-info ogroup))
7549                          (list (cons 'to-group ogroup))))
7550          article group egroup groups vgroup)
7551     (while (setq article (pop articles))
7552       (setq group (format "%s-%d" gnus-newsgroup-name article))
7553       (gnus-summary-remove-process-mark article)
7554       (when (gnus-summary-display-article article)
7555         (save-excursion
7556           (with-temp-buffer
7557             (insert-buffer-substring gnus-original-article-buffer)
7558             ;; Remove some headers that may lead nndoc to make
7559             ;; the wrong guess.
7560             (message-narrow-to-head)
7561             (goto-char (point-min))
7562             (delete-matching-lines "^\\(Path\\):\\|^From ")
7563             (widen)
7564             (if (setq egroup
7565                       (gnus-group-read-ephemeral-group
7566                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7567                                      (nndoc-article-type guess))
7568                        t nil t))
7569                 (progn
7570                   ;; Make all postings to this group go to the parent group.
7571                   (nconc (gnus-info-params (gnus-get-info egroup))
7572                          params)
7573                   (push egroup groups))
7574               ;; Couldn't select this doc group.
7575               (gnus-error 3 "Article couldn't be entered"))))))
7576     ;; Now we have selected all the documents.
7577     (cond
7578      ((not groups)
7579       (error "None of the articles could be interpreted as documents"))
7580      ((gnus-group-read-ephemeral-group
7581        (setq vgroup (format
7582                      "nnvirtual:%s-%s" gnus-newsgroup-name
7583                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7584        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7585        t
7586        (cons (current-buffer) 'summary)))
7587      (t
7588       (error "Couldn't select virtual nndoc group")))))
7589
7590 (defun gnus-summary-isearch-article (&optional regexp-p)
7591   "Do incremental search forward on the current article.
7592 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7593   (interactive "P")
7594   (let* ((gnus-inhibit-treatment t)
7595          (old (gnus-summary-select-article)))
7596     (gnus-configure-windows 'article)
7597     (gnus-eval-in-buffer-window gnus-article-buffer
7598       (save-restriction
7599         (widen)
7600         (when (eq 'old old)
7601           (gnus-article-show-all-headers))
7602         (goto-char (point-min))
7603         (isearch-forward regexp-p)))))
7604
7605 (defun gnus-summary-search-article-forward (regexp &optional backward)
7606   "Search for an article containing REGEXP forward.
7607 If BACKWARD, search backward instead."
7608   (interactive
7609    (list (read-string
7610           (format "Search article %s (regexp%s): "
7611                   (if current-prefix-arg "backward" "forward")
7612                   (if gnus-last-search-regexp
7613                       (concat ", default " gnus-last-search-regexp)
7614                     "")))
7615          current-prefix-arg))
7616   (if (string-equal regexp "")
7617       (setq regexp (or gnus-last-search-regexp ""))
7618     (setq gnus-last-search-regexp regexp)
7619     (setq gnus-article-before-search gnus-current-article))
7620   ;; Intentionally set gnus-last-article.
7621   (setq gnus-last-article gnus-article-before-search)
7622   (let ((gnus-last-article gnus-last-article))
7623     (if (gnus-summary-search-article regexp backward)
7624         (gnus-summary-show-thread)
7625       (error "Search failed: \"%s\"" regexp))))
7626
7627 (defun gnus-summary-search-article-backward (regexp)
7628   "Search for an article containing REGEXP backward."
7629   (interactive
7630    (list (read-string
7631           (format "Search article backward (regexp%s): "
7632                   (if gnus-last-search-regexp
7633                       (concat ", default " gnus-last-search-regexp)
7634                     "")))))
7635   (gnus-summary-search-article-forward regexp 'backward))
7636
7637 (eval-when-compile
7638   (defmacro gnus-summary-search-article-position-point (regexp backward)
7639     "Dehighlight the last matched text and goto the beginning position."
7640     (` (if (and gnus-summary-search-article-matched-data
7641                 (let ((text (caddr gnus-summary-search-article-matched-data))
7642                       (inhibit-read-only t)
7643                       buffer-read-only)
7644                   (delete-region
7645                    (goto-char (car gnus-summary-search-article-matched-data))
7646                    (cadr gnus-summary-search-article-matched-data))
7647                   (insert text)
7648                   (string-match (, regexp) text)))
7649            (if (, backward) (beginning-of-line) (end-of-line))
7650          (goto-char (if (, backward) (point-max) (point-min))))))
7651
7652   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7653     "Place point where X-Face image is displayed."
7654     (if (featurep 'xemacs)
7655         (` (let ((end (if (search-forward "\n\n" nil t)
7656                           (goto-char (1- (point)))
7657                         (point-min)))
7658                  extent)
7659              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7660              (unless (and (re-search-forward "^From:" end t)
7661                           (setq extent (extent-at (point)))
7662                           (extent-begin-glyph extent))
7663                (goto-char (, opoint)))))
7664       (` (let ((end (if (search-forward "\n\n" nil t)
7665                         (goto-char (1- (point)))
7666                       (point-min)))
7667                (start (or (search-backward "\n\n" nil t) (point-min))))
7668            (goto-char
7669             (or (text-property-any start end 'x-face-image t);; x-face-e21
7670                 (text-property-any start end 'x-face-mule-bitmap-image t)
7671                 (, opoint)))))))
7672
7673   (defmacro gnus-summary-search-article-highlight-matched-text
7674     (backward treated x-face)
7675     "Highlight matched text in the function `gnus-summary-search-article'."
7676     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7677              (end (set-marker (make-marker) (match-end 0)))
7678              (inhibit-read-only t)
7679              buffer-read-only)
7680          (unless treated
7681            (let ((,@
7682                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7683                     (mapcar
7684                      (lambda (item) (setq items (delq item items)))
7685                      '(gnus-treat-buttonize
7686                        gnus-treat-fill-article
7687                        gnus-treat-fill-long-lines
7688                        gnus-treat-emphasize
7689                        gnus-treat-highlight-headers
7690                        gnus-treat-highlight-citation
7691                        gnus-treat-highlight-signature
7692                        gnus-treat-overstrike
7693                        gnus-treat-display-xface
7694                        gnus-treat-buttonize-head
7695                        gnus-treat-decode-article-as-default-mime-charset))
7696                     (static-if (featurep 'xemacs)
7697                         items
7698                       (cons '(x-face-mule-delete-x-face-field
7699                               (quote never))
7700                             items))))
7701                  (gnus-treat-display-xface
7702                   (when (, x-face) gnus-treat-display-xface)))
7703              (gnus-article-prepare-mime-display)))
7704          (goto-char (if (, backward) start end))
7705          (when (, x-face)
7706            (gnus-summary-search-article-highlight-goto-x-face (point)))
7707          (setq gnus-summary-search-article-matched-data
7708                (list start end (buffer-substring start end)))
7709          (unless (eq start end);; matched text has been deleted. :-<
7710            (put-text-property start end 'face
7711                               (or (find-face 'isearch)
7712                                   'secondary-selection))))))
7713   )
7714
7715 (defun gnus-summary-search-article (regexp &optional backward)
7716   "Search for an article containing REGEXP.
7717 Optional argument BACKWARD means do search for backward.
7718 `gnus-select-article-hook' is not called during the search."
7719   ;; We have to require this here to make sure that the following
7720   ;; dynamic binding isn't shadowed by autoloading.
7721   (require 'gnus-async)
7722   (require 'gnus-art)
7723   (let ((gnus-select-article-hook nil)  ;Disable hook.
7724         (gnus-article-prepare-hook nil)
7725         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7726         (gnus-use-article-prefetch nil)
7727         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7728         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7729         (sum (current-buffer))
7730         (found nil)
7731         point treated)
7732     (gnus-save-hidden-threads
7733       (static-if (featurep 'xemacs)
7734           (let ((gnus-inhibit-treatment t))
7735             (setq treated (eq 'old (gnus-summary-select-article)))
7736             (when (and treated
7737                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7738                                  (window-live-p (get-buffer-window
7739                                                  gnus-article-buffer t)))))
7740               (gnus-summary-select-article nil t)
7741               (setq treated nil)))
7742         (let ((gnus-inhibit-treatment t)
7743               (x-face-mule-delete-x-face-field 'never))
7744           (setq treated (eq 'old (gnus-summary-select-article)))
7745           (when (and treated
7746                      (not
7747                       (and (gnus-buffer-live-p gnus-article-buffer)
7748                            (window-live-p (get-buffer-window
7749                                            gnus-article-buffer t))
7750                            (or (not (string-match "^\\^X-Face:" regexp))
7751                                (with-current-buffer gnus-article-buffer
7752                                  gnus-summary-search-article-matched-data)))))
7753             (gnus-summary-select-article nil t)
7754             (setq treated nil))))
7755       (set-buffer gnus-article-buffer)
7756       (widen)
7757       (if treated
7758           (progn
7759             (gnus-article-show-all-headers)
7760             (gnus-summary-search-article-position-point regexp backward))
7761         (goto-char (if backward (point-max) (point-min))))
7762       (while (not found)
7763         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7764         (if (if backward
7765                 (re-search-backward regexp nil t)
7766               (re-search-forward regexp nil t))
7767             ;; We found the regexp.
7768             (progn
7769               (gnus-summary-search-article-highlight-matched-text
7770                backward treated (string-match "^\\^X-Face:" regexp))
7771               (setq found 'found)
7772               (forward-line
7773                (/ (- 2 (window-height
7774                         (get-buffer-window gnus-article-buffer t)))
7775                   2))
7776               (set-window-start
7777                (get-buffer-window (current-buffer))
7778                (point))
7779               (set-buffer sum)
7780               (setq point (point)))
7781           ;; We didn't find it, so we go to the next article.
7782           (set-buffer sum)
7783           (setq found 'not)
7784           (while (eq found 'not)
7785             (if (not (if backward (gnus-summary-find-prev)
7786                        (gnus-summary-find-next)))
7787                 ;; No more articles.
7788                 (setq found t)
7789               ;; Select the next article and adjust point.
7790               (unless (gnus-summary-article-sparse-p
7791                        (gnus-summary-article-number))
7792                 (setq found nil)
7793                 (let ((gnus-inhibit-treatment t))
7794                   (gnus-summary-select-article))
7795                 (setq treated nil)
7796                 (set-buffer gnus-article-buffer)
7797                 (widen)
7798                 (goto-char (if backward (point-max) (point-min))))))))
7799       (gnus-message 7 ""))
7800     ;; Return whether we found the regexp.
7801     (when (eq found 'found)
7802       (goto-char point)
7803       (gnus-summary-show-thread)
7804       (gnus-summary-goto-subject gnus-current-article)
7805       (gnus-summary-position-point)
7806       t)))
7807
7808 (defun gnus-find-matching-articles (header regexp)
7809   "Return a list of all articles that match REGEXP on HEADER.
7810 This search includes all articles in the current group that Gnus has
7811 fetched headers for, whether they are displayed or not."
7812   (let ((articles nil)
7813         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7814         (case-fold-search t))
7815     (dolist (header gnus-newsgroup-headers)
7816       (when (string-match regexp (funcall func header))
7817         (push (mail-header-number header) articles)))
7818     (nreverse articles)))
7819
7820 (defun gnus-summary-find-matching (header regexp &optional backward unread
7821                                           not-case-fold)
7822   "Return a list of all articles that match REGEXP on HEADER.
7823 The search stars on the current article and goes forwards unless
7824 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7825 If UNREAD is non-nil, only unread articles will
7826 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7827 in the comparisons."
7828   (let ((case-fold-search (not not-case-fold))
7829         articles d func)
7830     (if (consp header)
7831         (if (eq (car header) 'extra)
7832             (setq func
7833                   `(lambda (h)
7834                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7835                          "")))
7836           (error "%s is an invalid header" header))
7837       (unless (fboundp (intern (concat "mail-header-" header)))
7838         (error "%s is not a valid header" header))
7839       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7840     (dolist (d (if (eq backward 'all)
7841                    gnus-newsgroup-data
7842                  (gnus-data-find-list
7843                   (gnus-summary-article-number)
7844                   (gnus-data-list backward))))
7845       (when (and (or (not unread)       ; We want all articles...
7846                      (gnus-data-unread-p d)) ; Or just unreads.
7847                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7848                  (string-match regexp
7849                                (funcall func (gnus-data-header d)))) ; Match.
7850         (push (gnus-data-number d) articles))) ; Success!
7851     (nreverse articles)))
7852
7853 (defun gnus-summary-execute-command (header regexp command &optional backward)
7854   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7855 If HEADER is an empty string (or nil), the match is done on the entire
7856 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7857   (interactive
7858    (list (let ((completion-ignore-case t))
7859            (completing-read
7860             "Header name: "
7861             (mapcar (lambda (header) (list (format "%s" header)))
7862                     (append
7863                      '("Number" "Subject" "From" "Lines" "Date"
7864                        "Message-ID" "Xref" "References" "Body")
7865                      gnus-extra-headers))
7866             nil 'require-match))
7867          (read-string "Regexp: ")
7868          (read-key-sequence "Command: ")
7869          current-prefix-arg))
7870   (when (equal header "Body")
7871     (setq header ""))
7872   ;; Hidden thread subtrees must be searched as well.
7873   (gnus-summary-show-all-threads)
7874   ;; We don't want to change current point nor window configuration.
7875   (save-excursion
7876     (save-window-excursion
7877       (gnus-message 6 "Executing %s..." (key-description command))
7878       ;; We'd like to execute COMMAND interactively so as to give arguments.
7879       (gnus-execute header regexp
7880                     `(call-interactively ',(key-binding command))
7881                     backward)
7882       (gnus-message 6 "Executing %s...done" (key-description command)))))
7883
7884 (defun gnus-summary-beginning-of-article ()
7885   "Scroll the article back to the beginning."
7886   (interactive)
7887   (gnus-summary-select-article)
7888   (gnus-configure-windows 'article)
7889   (gnus-eval-in-buffer-window gnus-article-buffer
7890     (widen)
7891     (goto-char (point-min))
7892     (when gnus-page-broken
7893       (gnus-narrow-to-page))))
7894
7895 (defun gnus-summary-end-of-article ()
7896   "Scroll to the end of the article."
7897   (interactive)
7898   (gnus-summary-select-article)
7899   (gnus-configure-windows 'article)
7900   (gnus-eval-in-buffer-window gnus-article-buffer
7901     (widen)
7902     (goto-char (point-max))
7903     (recenter -3)
7904     (when gnus-page-broken
7905       (gnus-narrow-to-page))))
7906
7907 (defun gnus-summary-print-article (&optional filename n)
7908   "Generate and print a PostScript image of the N next (mail) articles.
7909
7910 If N is negative, print the N previous articles.  If N is nil and articles
7911 have been marked with the process mark, print these instead.
7912
7913 If the optional first argument FILENAME is nil, send the image to the
7914 printer.  If FILENAME is a string, save the PostScript image in a file with
7915 that name.  If FILENAME is a number, prompt the user for the name of the file
7916 to save in."
7917   (interactive (list (ps-print-preprint current-prefix-arg)))
7918   (dolist (article (gnus-summary-work-articles n))
7919     (gnus-summary-select-article nil nil 'pseudo article)
7920     (gnus-eval-in-buffer-window gnus-article-buffer
7921       (let ((buffer (generate-new-buffer " *print*")))
7922         (unwind-protect
7923             (progn
7924               (copy-to-buffer buffer (point-min) (point-max))
7925               (set-buffer buffer)
7926               (gnus-article-delete-invisible-text)
7927               (when (gnus-visual-p 'article-highlight 'highlight)
7928                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7929                 ;; highlight.
7930                 (let ((gnus-article-buffer buffer))
7931                   (gnus-article-highlight-citation t)
7932                   (gnus-article-highlight-signature)))
7933               (let ((ps-left-header
7934                      (list
7935                       (concat "("
7936                               (mail-header-subject gnus-current-headers) ")")
7937                       (concat "("
7938                               (mail-header-from gnus-current-headers) ")")))
7939                     (ps-right-header
7940                      (list
7941                       "/pagenumberstring load"
7942                       (concat "("
7943                               (mail-header-date gnus-current-headers) ")"))))
7944                 (gnus-run-hooks 'gnus-ps-print-hook)
7945                 (save-excursion
7946                   (if window-system
7947                       (ps-spool-buffer-with-faces)
7948                     (ps-spool-buffer)))))
7949           (kill-buffer buffer))))
7950     (gnus-summary-remove-process-mark article))
7951   (ps-despool filename))
7952
7953 (defun gnus-summary-show-article (&optional arg)
7954   "Force re-fetching of the current article.
7955 If ARG (the prefix) is a number, show the article with the charset
7956 defined in `gnus-summary-show-article-charset-alist', or the charset
7957 input.
7958 If ARG (the prefix) is non-nil and not a number, show the raw article
7959 without any article massaging functions being run."
7960   (interactive "P")
7961   (cond
7962    ((numberp arg)
7963     (gnus-summary-show-article t)
7964     (let ((gnus-newsgroup-charset
7965            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7966                (mm-read-coding-system
7967                 "View as charset: "
7968                 (save-excursion
7969                   (set-buffer gnus-article-buffer)
7970                   (detect-coding-region (point) (point-max) t)))))
7971           (gnus-newsgroup-ignored-charsets 'gnus-all))
7972       (gnus-summary-select-article nil 'force)
7973       (let ((deps gnus-newsgroup-dependencies)
7974             head header)
7975         (save-excursion
7976           (set-buffer gnus-original-article-buffer)
7977           (save-restriction
7978             (message-narrow-to-head)
7979             (setq head (buffer-string)))
7980           (with-temp-buffer
7981             (insert (format "211 %d Article retrieved.\n"
7982                             (cdr gnus-article-current)))
7983             (insert head)
7984             (insert ".\n")
7985             (let ((nntp-server-buffer (current-buffer)))
7986               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7987         (gnus-data-set-header
7988          (gnus-data-find (cdr gnus-article-current))
7989          header)
7990         (gnus-summary-update-article-line
7991          (cdr gnus-article-current) header)
7992         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7993           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7994    ((not arg)
7995     ;; Select the article the normal way.
7996     (gnus-summary-select-article nil 'force))
7997    (t
7998     ;; We have to require this here to make sure that the following
7999     ;; dynamic binding isn't shadowed by autoloading.
8000     (require 'gnus-async)
8001     (require 'gnus-art)
8002     ;; Bind the article treatment functions to nil.
8003     (let ((gnus-have-all-headers t)
8004           gnus-article-prepare-hook
8005           gnus-article-decode-hook
8006           gnus-break-pages
8007           gnus-show-mime
8008           (gnus-inhibit-treatment t))
8009       (gnus-summary-select-article nil 'force))))
8010   (gnus-summary-goto-subject gnus-current-article)
8011   (gnus-summary-position-point))
8012
8013 (defun gnus-summary-show-raw-article ()
8014   "Show the raw article without any article massaging functions being run."
8015   (interactive)
8016   (gnus-summary-show-article t))
8017
8018 (defun gnus-summary-verbose-headers (&optional arg)
8019   "Toggle permanent full header display.
8020 If ARG is a positive number, turn header display on.
8021 If ARG is a negative number, turn header display off."
8022   (interactive "P")
8023   (setq gnus-show-all-headers
8024         (cond ((or (not (numberp arg))
8025                    (zerop arg))
8026                (not gnus-show-all-headers))
8027               ((natnump arg)
8028                t)))
8029   (gnus-summary-show-article))
8030
8031 (defun gnus-summary-toggle-header (&optional arg)
8032   "Show the headers if they are hidden, or hide them if they are shown.
8033 If ARG is a positive number, show the entire header.
8034 If ARG is a negative number, hide the unwanted header lines."
8035   (interactive "P")
8036   (save-excursion
8037     (set-buffer gnus-article-buffer)
8038     (save-restriction
8039       (let* ((buffer-read-only nil)
8040              (inhibit-point-motion-hooks t)
8041              hidden e)
8042         (setq hidden
8043               (if (numberp arg)
8044                   (>= arg 0)
8045                 (save-restriction
8046                   (article-narrow-to-head)
8047                   (gnus-article-hidden-text-p 'headers))))
8048         (goto-char (point-min))
8049         (when (search-forward "\n\n" nil t)
8050           (delete-region (point-min) (1- (point))))
8051         (goto-char (point-min))
8052         (save-excursion
8053           (set-buffer gnus-original-article-buffer)
8054           (goto-char (point-min))
8055           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8056         (insert-buffer-substring gnus-original-article-buffer 1 e)
8057         (save-restriction
8058           (narrow-to-region (point-min) (point))
8059           (article-decode-encoded-words)
8060           (if  hidden
8061               (let ((gnus-treat-hide-headers nil)
8062                     (gnus-treat-hide-boring-headers nil))
8063                 (setq gnus-article-wash-types
8064                       (delq 'headers gnus-article-wash-types))
8065                 (gnus-treat-article 'head))
8066             (gnus-treat-article 'head)))
8067         (gnus-set-mode-line 'article)))))
8068
8069 (defun gnus-summary-show-all-headers ()
8070   "Make all header lines visible."
8071   (interactive)
8072   (gnus-summary-toggle-header 1))
8073
8074 (defun gnus-summary-toggle-mime (&optional arg)
8075   "Toggle MIME processing.
8076 If ARG is a positive number, turn MIME processing on."
8077   (interactive "P")
8078   (setq gnus-show-mime
8079         (if (null arg)
8080             (not gnus-show-mime)
8081           (> (prefix-numeric-value arg) 0)))
8082   (gnus-summary-select-article t 'force))
8083
8084 (defun gnus-summary-caesar-message (&optional arg)
8085   "Caesar rotate the current article by 13.
8086 The numerical prefix specifies how many places to rotate each letter
8087 forward."
8088   (interactive "P")
8089   (gnus-summary-select-article)
8090   (let ((mail-header-separator ""))
8091     (gnus-eval-in-buffer-window gnus-article-buffer
8092       (save-restriction
8093         (widen)
8094         (let ((start (window-start))
8095               buffer-read-only)
8096           (message-caesar-buffer-body arg)
8097           (set-window-start (get-buffer-window (current-buffer)) start))))))
8098
8099 (defun gnus-summary-stop-page-breaking ()
8100   "Stop page breaking in the current article."
8101   (interactive)
8102   (gnus-summary-select-article)
8103   (gnus-eval-in-buffer-window gnus-article-buffer
8104     (widen)
8105     (when (gnus-visual-p 'page-marker)
8106       (let ((buffer-read-only nil))
8107         (gnus-remove-text-with-property 'gnus-prev)
8108         (gnus-remove-text-with-property 'gnus-next))
8109       (setq gnus-page-broken nil))))
8110
8111 (defun gnus-summary-move-article (&optional n to-newsgroup
8112                                             select-method action)
8113   "Move the current article to a different newsgroup.
8114 If N is a positive number, move the N next articles.
8115 If N is a negative number, move the N previous articles.
8116 If N is nil and any articles have been marked with the process mark,
8117 move those articles instead.
8118 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8119 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8120 re-spool using this method.
8121
8122 For this function to work, both the current newsgroup and the
8123 newsgroup that you want to move to have to support the `request-move'
8124 and `request-accept' functions.
8125
8126 ACTION can be either `move' (the default), `crosspost' or `copy'."
8127   (interactive "P")
8128   (unless action
8129     (setq action 'move))
8130   ;; Check whether the source group supports the required functions.
8131   (cond ((and (eq action 'move)
8132               (not (gnus-check-backend-function
8133                     'request-move-article gnus-newsgroup-name)))
8134          (error "The current group does not support article moving"))
8135         ((and (eq action 'crosspost)
8136               (not (gnus-check-backend-function
8137                     'request-replace-article gnus-newsgroup-name)))
8138          (error "The current group does not support article editing")))
8139   (let ((articles (gnus-summary-work-articles n))
8140         (prefix (if (gnus-check-backend-function
8141                      'request-move-article gnus-newsgroup-name)
8142                     (gnus-group-real-prefix gnus-newsgroup-name)
8143                   ""))
8144         (names '((move "Move" "Moving")
8145                  (copy "Copy" "Copying")
8146                  (crosspost "Crosspost" "Crossposting")))
8147         (copy-buf (save-excursion
8148                     (nnheader-set-temp-buffer " *copy article*")))
8149         (default-marks gnus-article-mark-lists)
8150         (no-expire-marks (delete '(expirable . expire)
8151                                  (copy-sequence gnus-article-mark-lists)))
8152         art-group to-method new-xref article to-groups)
8153     (unless (assq action names)
8154       (error "Unknown action %s" action))
8155     ;; Read the newsgroup name.
8156     (when (and (not to-newsgroup)
8157                (not select-method))
8158       (setq to-newsgroup
8159             (gnus-read-move-group-name
8160              (cadr (assq action names))
8161              (symbol-value (intern (format "gnus-current-%s-group" action)))
8162              articles prefix))
8163       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8164     (setq to-method (or select-method
8165                         (gnus-server-to-method
8166                          (gnus-group-method to-newsgroup))))
8167     ;; Check the method we are to move this article to...
8168     (unless (gnus-check-backend-function
8169              'request-accept-article (car to-method))
8170       (error "%s does not support article copying" (car to-method)))
8171     (unless (gnus-check-server to-method)
8172       (error "Can't open server %s" (car to-method)))
8173     (gnus-message 6 "%s to %s: %s..."
8174                   (caddr (assq action names))
8175                   (or (car select-method) to-newsgroup) articles)
8176     (while articles
8177       (setq article (pop articles))
8178       (setq
8179        art-group
8180        (cond
8181         ;; Move the article.
8182         ((eq action 'move)
8183          ;; Remove this article from future suppression.
8184          (gnus-dup-unsuppress-article article)
8185          (gnus-request-move-article
8186           article                       ; Article to move
8187           gnus-newsgroup-name           ; From newsgroup
8188           (nth 1 (gnus-find-method-for-group
8189                   gnus-newsgroup-name)) ; Server
8190           (list 'gnus-request-accept-article
8191                 to-newsgroup (list 'quote select-method)
8192                 (not articles) t)       ; Accept form
8193           (not articles)))              ; Only save nov last time
8194         ;; Copy the article.
8195         ((eq action 'copy)
8196          (save-excursion
8197            (set-buffer copy-buf)
8198            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8199              (gnus-request-accept-article
8200               to-newsgroup select-method (not articles) t))))
8201         ;; Crosspost the article.
8202         ((eq action 'crosspost)
8203          (let ((xref (message-tokenize-header
8204                       (mail-header-xref (gnus-summary-article-header article))
8205                       " ")))
8206            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8207                                   ":" article))
8208            (unless xref
8209              (setq xref (list (system-name))))
8210            (setq new-xref
8211                  (concat
8212                   (mapconcat 'identity
8213                              (delete "Xref:" (delete new-xref xref))
8214                              " ")
8215                   " " new-xref))
8216            (save-excursion
8217              (set-buffer copy-buf)
8218              ;; First put the article in the destination group.
8219              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8220              (when (consp (setq art-group
8221                                 (gnus-request-accept-article
8222                                  to-newsgroup select-method (not articles))))
8223                (setq new-xref (concat new-xref " " (car art-group)
8224                                       ":" (cdr art-group)))
8225                ;; Now we have the new Xrefs header, so we insert
8226                ;; it and replace the new article.
8227                (nnheader-replace-header "Xref" new-xref)
8228                (gnus-request-replace-article
8229                 (cdr art-group) to-newsgroup (current-buffer))
8230                art-group))))))
8231       (cond
8232        ((not art-group)
8233         (gnus-message 1 "Couldn't %s article %s: %s"
8234                       (cadr (assq action names)) article
8235                       (nnheader-get-report (car to-method))))
8236        ((eq art-group 'junk)
8237         (when (eq action 'move)
8238           (gnus-summary-mark-article article gnus-canceled-mark)
8239           (gnus-message 4 "Deleted article %s" article)))
8240        (t
8241         (let* ((pto-group (gnus-group-prefixed-name
8242                            (car art-group) to-method))
8243                (entry
8244                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8245                (info (nth 2 entry))
8246                (to-group (gnus-info-group info))
8247                to-marks)
8248           ;; Update the group that has been moved to.
8249           (when (and info
8250                      (memq action '(move copy)))
8251             (unless (member to-group to-groups)
8252               (push to-group to-groups))
8253
8254             (unless (memq article gnus-newsgroup-unreads)
8255               (push 'read to-marks)
8256               (gnus-info-set-read
8257                info (gnus-add-to-range (gnus-info-read info)
8258                                        (list (cdr art-group)))))
8259
8260             ;; See whether the article is to be put in the cache.
8261             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8262                              default-marks
8263                            no-expire-marks))
8264                   (to-article (cdr art-group)))
8265
8266               ;; Enter the article into the cache in the new group,
8267               ;; if that is required.
8268               (when gnus-use-cache
8269                 (gnus-cache-possibly-enter-article
8270                  to-group to-article
8271                  (let ((header (copy-sequence
8272                                 (gnus-summary-article-header article))))
8273                    (mail-header-set-number header to-article)
8274                    header)
8275                  (memq article gnus-newsgroup-marked)
8276                  (memq article gnus-newsgroup-dormant)
8277                  (memq article gnus-newsgroup-unreads)))
8278
8279               (when gnus-preserve-marks
8280                 ;; Copy any marks over to the new group.
8281                 (when (and (equal to-group gnus-newsgroup-name)
8282                            (not (memq article gnus-newsgroup-unreads)))
8283                   ;; Mark this article as read in this group.
8284                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8285                   (setcdr (gnus-active to-group) to-article)
8286                   (setcdr gnus-newsgroup-active to-article))
8287
8288                 (while marks
8289                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8290                     (when (memq article (symbol-value
8291                                          (intern (format "gnus-newsgroup-%s"
8292                                                          (caar marks)))))
8293                       (push (cdar marks) to-marks)
8294                       ;; If the other group is the same as this group,
8295                       ;; then we have to add the mark to the list.
8296                       (when (equal to-group gnus-newsgroup-name)
8297                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8298                              (cons to-article
8299                                    (symbol-value
8300                                     (intern (format "gnus-newsgroup-%s"
8301                                                     (caar marks)))))))
8302                       ;; Copy the marks to other group.
8303                       (gnus-add-marked-articles
8304                        to-group (cdar marks) (list to-article) info)))
8305                   (setq marks (cdr marks)))
8306
8307                 (gnus-request-set-mark to-group (list (list (list to-article)
8308                                                             'add
8309                                                             to-marks))))
8310
8311               (gnus-dribble-enter
8312                (concat "(gnus-group-set-info '"
8313                        (gnus-prin1-to-string (gnus-get-info to-group))
8314                        ")"))))
8315
8316           ;; Update the Xref header in this article to point to
8317           ;; the new crossposted article we have just created.
8318           (when (eq action 'crosspost)
8319             (save-excursion
8320               (set-buffer copy-buf)
8321               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8322               (nnheader-replace-header "Xref" new-xref)
8323               (gnus-request-replace-article
8324                article gnus-newsgroup-name (current-buffer)))))
8325
8326         ;;;!!!Why is this necessary?
8327         (set-buffer gnus-summary-buffer)
8328
8329         (gnus-summary-goto-subject article)
8330         (when (eq action 'move)
8331           (gnus-summary-mark-article article gnus-canceled-mark))))
8332       (gnus-summary-remove-process-mark article))
8333     ;; Re-activate all groups that have been moved to.
8334     (while to-groups
8335       (save-excursion
8336         (set-buffer gnus-group-buffer)
8337         (when (gnus-group-goto-group (car to-groups) t)
8338           (gnus-group-get-new-news-this-group 1 t))
8339         (pop to-groups)))
8340
8341     (gnus-kill-buffer copy-buf)
8342     (gnus-summary-position-point)
8343     (gnus-set-mode-line 'summary)))
8344
8345 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8346   "Move the current article to a different newsgroup.
8347 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8348 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8349 re-spool using this method."
8350   (interactive "P")
8351   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8352
8353 (defun gnus-summary-crosspost-article (&optional n)
8354   "Crosspost the current article to some other group."
8355   (interactive "P")
8356   (gnus-summary-move-article n nil nil 'crosspost))
8357
8358 (defcustom gnus-summary-respool-default-method nil
8359   "Default method for respooling an article.
8360 If nil, use to the current newsgroup method."
8361   :type '(choice (gnus-select-method :value (nnml ""))
8362                  (const nil))
8363   :group 'gnus-summary-mail)
8364
8365 (defun gnus-summary-respool-article (&optional n method)
8366   "Respool the current article.
8367 The article will be squeezed through the mail spooling process again,
8368 which means that it will be put in some mail newsgroup or other
8369 depending on `nnmail-split-methods'.
8370 If N is a positive number, respool the N next articles.
8371 If N is a negative number, respool the N previous articles.
8372 If N is nil and any articles have been marked with the process mark,
8373 respool those articles instead.
8374
8375 Respooling can be done both from mail groups and \"real\" newsgroups.
8376 In the former case, the articles in question will be moved from the
8377 current group into whatever groups they are destined to.  In the
8378 latter case, they will be copied into the relevant groups."
8379   (interactive
8380    (list current-prefix-arg
8381          (let* ((methods (gnus-methods-using 'respool))
8382                 (methname
8383                  (symbol-name (or gnus-summary-respool-default-method
8384                                   (car (gnus-find-method-for-group
8385                                         gnus-newsgroup-name)))))
8386                 (method
8387                  (gnus-completing-read
8388                   methname "What backend do you want to use when respooling?"
8389                   methods nil t nil 'gnus-mail-method-history))
8390                 ms)
8391            (cond
8392             ((zerop (length (setq ms (gnus-servers-using-backend
8393                                       (intern method)))))
8394              (list (intern method) ""))
8395             ((= 1 (length ms))
8396              (car ms))
8397             (t
8398              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8399                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8400                            ms-alist))))))))
8401   (unless method
8402     (error "No method given for respooling"))
8403   (if (assoc (symbol-name
8404               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8405              (gnus-methods-using 'respool))
8406       (gnus-summary-move-article n nil method)
8407     (gnus-summary-copy-article n nil method)))
8408
8409 (defun gnus-summary-import-article (file &optional edit)
8410   "Import an arbitrary file into a mail newsgroup."
8411   (interactive "fImport file: \nP")
8412   (let ((group gnus-newsgroup-name)
8413         (now (current-time))
8414         atts lines group-art)
8415     (unless (gnus-check-backend-function 'request-accept-article group)
8416       (error "%s does not support article importing" group))
8417     (or (file-readable-p file)
8418         (not (file-regular-p file))
8419         (error "Can't read %s" file))
8420     (save-excursion
8421       (set-buffer (gnus-get-buffer-create " *import file*"))
8422       (erase-buffer)
8423       (nnheader-insert-file-contents file)
8424       (goto-char (point-min))
8425       (if (nnheader-article-p)
8426           (save-restriction
8427             (goto-char (point-min))
8428             (search-forward "\n\n" nil t)
8429             (narrow-to-region (point-min) (1- (point)))
8430             (goto-char (point-min))
8431             (unless (re-search-forward "^date:" nil t)
8432               (goto-char (point-max))
8433               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8434         ;; This doesn't look like an article, so we fudge some headers.
8435         (setq atts (file-attributes file)
8436               lines (count-lines (point-min) (point-max)))
8437         (insert "From: " (read-string "From: ") "\n"
8438                 "Subject: " (read-string "Subject: ") "\n"
8439                 "Date: " (message-make-date (nth 5 atts)) "\n"
8440                 "Message-ID: " (message-make-message-id) "\n"
8441                 "Lines: " (int-to-string lines) "\n"
8442                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8443       (setq group-art (gnus-request-accept-article group nil t))
8444       (kill-buffer (current-buffer)))
8445     (setq gnus-newsgroup-active (gnus-activate-group group))
8446     (forward-line 1)
8447     (gnus-summary-goto-article (cdr group-art) nil t)
8448     (when edit
8449       (gnus-summary-edit-article))))
8450
8451 (defun gnus-summary-create-article ()
8452   "Create an article in a mail newsgroup."
8453   (interactive)
8454   (let ((group gnus-newsgroup-name)
8455         (now (current-time))
8456         group-art)
8457     (unless (gnus-check-backend-function 'request-accept-article group)
8458       (error "%s does not support article importing" group))
8459     (save-excursion
8460       (set-buffer (gnus-get-buffer-create " *import file*"))
8461       (erase-buffer)
8462       (goto-char (point-min))
8463       ;; This doesn't look like an article, so we fudge some headers.
8464       (insert "From: " (read-string "From: ") "\n"
8465               "Subject: " (read-string "Subject: ") "\n"
8466               "Date: " (message-make-date now) "\n"
8467               "Message-ID: " (message-make-message-id) "\n")
8468       (setq group-art (gnus-request-accept-article group nil t))
8469       (kill-buffer (current-buffer)))
8470     (setq gnus-newsgroup-active (gnus-activate-group group))
8471     (forward-line 1)
8472     (gnus-summary-goto-article (cdr group-art) nil t)
8473     (gnus-summary-edit-article)))
8474
8475 (defun gnus-summary-article-posted-p ()
8476   "Say whether the current (mail) article is available from news as well.
8477 This will be the case if the article has both been mailed and posted."
8478   (interactive)
8479   (let ((id (mail-header-references (gnus-summary-article-header)))
8480         (gnus-override-method (car (gnus-refer-article-methods))))
8481     (if (gnus-request-head id "")
8482         (gnus-message 2 "The current message was found on %s"
8483                       gnus-override-method)
8484       (gnus-message 2 "The current message couldn't be found on %s"
8485                     gnus-override-method)
8486       nil)))
8487
8488 (defun gnus-summary-expire-articles (&optional now)
8489   "Expire all articles that are marked as expirable in the current group."
8490   (interactive)
8491   (when (gnus-check-backend-function
8492          'request-expire-articles gnus-newsgroup-name)
8493     ;; This backend supports expiry.
8494     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8495            (expirable (if total
8496                           (progn
8497                             ;; We need to update the info for
8498                             ;; this group for `gnus-list-of-read-articles'
8499                             ;; to give us the right answer.
8500                             (gnus-run-hooks 'gnus-exit-group-hook)
8501                             (gnus-summary-update-info)
8502                             (gnus-list-of-read-articles gnus-newsgroup-name))
8503                         (setq gnus-newsgroup-expirable
8504                               (sort gnus-newsgroup-expirable '<))))
8505            (expiry-wait (if now 'immediate
8506                           (gnus-group-find-parameter
8507                            gnus-newsgroup-name 'expiry-wait)))
8508            (nnmail-expiry-target
8509             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8510                 nnmail-expiry-target))
8511            es)
8512       (when expirable
8513         ;; There are expirable articles in this group, so we run them
8514         ;; through the expiry process.
8515         (gnus-message 6 "Expiring articles...")
8516         (unless (gnus-check-group gnus-newsgroup-name)
8517           (error "Can't open server for %s" gnus-newsgroup-name))
8518         ;; The list of articles that weren't expired is returned.
8519         (save-excursion
8520           (if expiry-wait
8521               (let ((nnmail-expiry-wait-function nil)
8522                     (nnmail-expiry-wait expiry-wait))
8523                 (setq es (gnus-request-expire-articles
8524                           expirable gnus-newsgroup-name)))
8525             (setq es (gnus-request-expire-articles
8526                       expirable gnus-newsgroup-name)))
8527           (unless total
8528             (setq gnus-newsgroup-expirable es))
8529           ;; We go through the old list of expirable, and mark all
8530           ;; really expired articles as nonexistent.
8531           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8532             (let ((gnus-use-cache nil))
8533               (while expirable
8534                 (unless (memq (car expirable) es)
8535                   (when (gnus-data-find (car expirable))
8536                     (gnus-summary-mark-article
8537                      (car expirable) gnus-canceled-mark)))
8538                 (setq expirable (cdr expirable))))))
8539         (gnus-message 6 "Expiring articles...done")))))
8540
8541 (defun gnus-summary-expire-articles-now ()
8542   "Expunge all expirable articles in the current group.
8543 This means that *all* articles that are marked as expirable will be
8544 deleted forever, right now."
8545   (interactive)
8546   (or gnus-expert-user
8547       (gnus-yes-or-no-p
8548        "Are you really, really, really sure you want to delete all these messages? ")
8549       (error "Phew!"))
8550   (gnus-summary-expire-articles t))
8551
8552 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8553 (defun gnus-summary-delete-article (&optional n)
8554   "Delete the N next (mail) articles.
8555 This command actually deletes articles.  This is not a marking
8556 command.  The article will disappear forever from your life, never to
8557 return.
8558 If N is negative, delete backwards.
8559 If N is nil and articles have been marked with the process mark,
8560 delete these instead."
8561   (interactive "P")
8562   (unless (gnus-check-backend-function 'request-expire-articles
8563                                        gnus-newsgroup-name)
8564     (error "The current newsgroup does not support article deletion"))
8565   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8566     (error "Couldn't open server"))
8567   ;; Compute the list of articles to delete.
8568   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8569         not-deleted)
8570     (if (and gnus-novice-user
8571              (not (gnus-yes-or-no-p
8572                    (format "Do you really want to delete %s forever? "
8573                            (if (> (length articles) 1)
8574                                (format "these %s articles" (length articles))
8575                              "this article")))))
8576         ()
8577       ;; Delete the articles.
8578       (setq not-deleted (gnus-request-expire-articles
8579                          articles gnus-newsgroup-name 'force))
8580       (while articles
8581         (gnus-summary-remove-process-mark (car articles))
8582         ;; The backend might not have been able to delete the article
8583         ;; after all.
8584         (unless (memq (car articles) not-deleted)
8585           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8586         (setq articles (cdr articles)))
8587       (when not-deleted
8588         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8589     (gnus-summary-position-point)
8590     (gnus-set-mode-line 'summary)
8591     not-deleted))
8592
8593 (defun gnus-summary-edit-article (&optional force)
8594   "Edit the current article.
8595 This will have permanent effect only in mail groups.
8596 If FORCE is non-nil, allow editing of articles even in read-only
8597 groups."
8598   (interactive "P")
8599   (save-excursion
8600     (set-buffer gnus-summary-buffer)
8601     (let ((mail-parse-charset gnus-newsgroup-charset)
8602           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8603       (gnus-set-global-variables)
8604       (when (and (not force)
8605                  (gnus-group-read-only-p))
8606         (error "The current newsgroup does not support article editing"))
8607       (gnus-summary-show-article t)
8608       (gnus-article-edit-article
8609        'ignore
8610        `(lambda (no-highlight)
8611           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8612                 (message-options message-options)
8613                 (message-options-set-recipient)
8614                 (mail-parse-ignored-charsets
8615                  ',gnus-newsgroup-ignored-charsets))
8616             (gnus-summary-edit-article-done
8617              ,(or (mail-header-references gnus-current-headers) "")
8618              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8619
8620 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8621
8622 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8623                                                  no-highlight)
8624   "Make edits to the current article permanent."
8625   (interactive)
8626   (save-excursion
8627     ;; The buffer restriction contains the entire article if it exists.
8628     (when (article-goto-body)
8629       (let ((lines (count-lines (point) (point-max)))
8630             (length (- (point-max) (point)))
8631             (case-fold-search t)
8632             (body (copy-marker (point))))
8633         (goto-char (point-min))
8634         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8635           (delete-region (match-beginning 1) (match-end 1))
8636           (insert (number-to-string length)))
8637         (goto-char (point-min))
8638         (when (re-search-forward
8639                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8640           (delete-region (match-beginning 1) (match-end 1))
8641           (insert (number-to-string length)))
8642         (goto-char (point-min))
8643         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8644           (delete-region (match-beginning 1) (match-end 1))
8645           (insert (number-to-string lines))))))
8646   ;; Replace the article.
8647   (let ((buf (current-buffer)))
8648     (with-temp-buffer
8649       (insert-buffer-substring buf)
8650
8651       (if (and (not read-only)
8652                (not (gnus-request-replace-article
8653                      (cdr gnus-article-current) (car gnus-article-current)
8654                      (current-buffer) t)))
8655           (error "Couldn't replace article")
8656         ;; Update the summary buffer.
8657         (if (and references
8658                  (equal (message-tokenize-header references " ")
8659                         (message-tokenize-header
8660                          (or (message-fetch-field "references") "") " ")))
8661             ;; We only have to update this line.
8662             (save-excursion
8663               (save-restriction
8664                 (message-narrow-to-head)
8665                 (let ((head (buffer-string))
8666                       header)
8667                   (with-temp-buffer
8668                     (insert (format "211 %d Article retrieved.\n"
8669                                     (cdr gnus-article-current)))
8670                     (insert head)
8671                     (insert ".\n")
8672                     (let ((nntp-server-buffer (current-buffer)))
8673                       (setq header (car (gnus-get-newsgroup-headers
8674                                          (save-excursion
8675                                            (set-buffer gnus-summary-buffer)
8676                                            gnus-newsgroup-dependencies)
8677                                          t))))
8678                     (save-excursion
8679                       (set-buffer gnus-summary-buffer)
8680                       (gnus-data-set-header
8681                        (gnus-data-find (cdr gnus-article-current))
8682                        header)
8683                       (gnus-summary-update-article-line
8684                        (cdr gnus-article-current) header)
8685                       (if (gnus-summary-goto-subject
8686                            (cdr gnus-article-current) nil t)
8687                           (gnus-summary-update-secondary-mark
8688                            (cdr gnus-article-current))))))))
8689           ;; Update threads.
8690           (set-buffer (or buffer gnus-summary-buffer))
8691           (gnus-summary-update-article (cdr gnus-article-current))
8692           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8693               (gnus-summary-update-secondary-mark
8694                (cdr gnus-article-current))))
8695         ;; Prettify the article buffer again.
8696         (unless no-highlight
8697           (save-excursion
8698             (set-buffer gnus-article-buffer)
8699             ;;;!!! Fix this -- article should be rehighlighted.
8700             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8701             (set-buffer gnus-original-article-buffer)
8702             (gnus-request-article
8703              (cdr gnus-article-current)
8704              (car gnus-article-current) (current-buffer))))
8705         ;; Prettify the summary buffer line.
8706         (when (gnus-visual-p 'summary-highlight 'highlight)
8707           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8708
8709 (defun gnus-summary-edit-wash (key)
8710   "Perform editing command KEY in the article buffer."
8711   (interactive
8712    (list
8713     (progn
8714       (message "%s" (concat (this-command-keys) "- "))
8715       (read-char))))
8716   (message "")
8717   (gnus-summary-edit-article)
8718   (execute-kbd-macro (concat (this-command-keys) key))
8719   (gnus-article-edit-done))
8720
8721 (defun gnus-summary-toggle-smiley (&optional arg)
8722   "Toggle the display of smilies as small graphical icons."
8723   (interactive "P")
8724   (save-excursion
8725     (set-buffer gnus-article-buffer)
8726     (gnus-smiley-display arg)))
8727
8728 ;;; Respooling
8729
8730 (defun gnus-summary-respool-query (&optional silent trace)
8731   "Query where the respool algorithm would put this article."
8732   (interactive)
8733   (let (gnus-mark-article-hook)
8734     (gnus-summary-select-article)
8735     (save-excursion
8736       (set-buffer gnus-original-article-buffer)
8737       (save-restriction
8738         (message-narrow-to-head)
8739         (let ((groups (nnmail-article-group 'identity trace)))
8740           (unless silent
8741             (if groups
8742                 (message "This message would go to %s"
8743                          (mapconcat 'car groups ", "))
8744               (message "This message would go to no groups"))
8745             groups))))))
8746
8747 (defun gnus-summary-respool-trace ()
8748   "Trace where the respool algorithm would put this article.
8749 Display a buffer showing all fancy splitting patterns which matched."
8750   (interactive)
8751   (gnus-summary-respool-query nil t))
8752
8753 ;; Summary marking commands.
8754
8755 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8756   "Mark articles which has the same subject as read, and then select the next.
8757 If UNMARK is positive, remove any kind of mark.
8758 If UNMARK is negative, tick articles."
8759   (interactive "P")
8760   (when unmark
8761     (setq unmark (prefix-numeric-value unmark)))
8762   (let ((count
8763          (gnus-summary-mark-same-subject
8764           (gnus-summary-article-subject) unmark)))
8765     ;; Select next unread article.  If auto-select-same mode, should
8766     ;; select the first unread article.
8767     (gnus-summary-next-article t (and gnus-auto-select-same
8768                                       (gnus-summary-article-subject)))
8769     (gnus-message 7 "%d article%s marked as %s"
8770                   count (if (= count 1) " is" "s are")
8771                   (if unmark "unread" "read"))))
8772
8773 (defun gnus-summary-kill-same-subject (&optional unmark)
8774   "Mark articles which has the same subject as read.
8775 If UNMARK is positive, remove any kind of mark.
8776 If UNMARK is negative, tick articles."
8777   (interactive "P")
8778   (when unmark
8779     (setq unmark (prefix-numeric-value unmark)))
8780   (let ((count
8781          (gnus-summary-mark-same-subject
8782           (gnus-summary-article-subject) unmark)))
8783     ;; If marked as read, go to next unread subject.
8784     (when (null unmark)
8785       ;; Go to next unread subject.
8786       (gnus-summary-next-subject 1 t))
8787     (gnus-message 7 "%d articles are marked as %s"
8788                   count (if unmark "unread" "read"))))
8789
8790 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8791   "Mark articles with same SUBJECT as read, and return marked number.
8792 If optional argument UNMARK is positive, remove any kinds of marks.
8793 If optional argument UNMARK is negative, mark articles as unread instead."
8794   (let ((count 1))
8795     (save-excursion
8796       (cond
8797        ((null unmark)                   ; Mark as read.
8798         (while (and
8799                 (progn
8800                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8801                   (gnus-summary-show-thread) t)
8802                 (gnus-summary-find-subject subject))
8803           (setq count (1+ count))))
8804        ((> unmark 0)                    ; Tick.
8805         (while (and
8806                 (progn
8807                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8808                   (gnus-summary-show-thread) t)
8809                 (gnus-summary-find-subject subject))
8810           (setq count (1+ count))))
8811        (t                               ; Mark as unread.
8812         (while (and
8813                 (progn
8814                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8815                   (gnus-summary-show-thread) t)
8816                 (gnus-summary-find-subject subject))
8817           (setq count (1+ count)))))
8818       (gnus-set-mode-line 'summary)
8819       ;; Return the number of marked articles.
8820       count)))
8821
8822 (defun gnus-summary-mark-as-processable (n &optional unmark)
8823   "Set the process mark on the next N articles.
8824 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8825 the process mark instead.  The difference between N and the actual
8826 number of articles marked is returned."
8827   (interactive "P")
8828   (if (and (null n) (gnus-region-active-p))
8829       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8830     (setq n (prefix-numeric-value n))
8831     (let ((backward (< n 0))
8832           (n (abs n)))
8833       (while (and
8834               (> n 0)
8835               (if unmark
8836                   (gnus-summary-remove-process-mark
8837                    (gnus-summary-article-number))
8838                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8839               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8840         (setq n (1- n)))
8841       (when (/= 0 n)
8842         (gnus-message 7 "No more articles"))
8843       (gnus-summary-recenter)
8844       (gnus-summary-position-point)
8845       n)))
8846
8847 (defun gnus-summary-unmark-as-processable (n)
8848   "Remove the process mark from the next N articles.
8849 If N is negative, unmark backward instead.  The difference between N and
8850 the actual number of articles unmarked is returned."
8851   (interactive "P")
8852   (gnus-summary-mark-as-processable n t))
8853
8854 (defun gnus-summary-unmark-all-processable ()
8855   "Remove the process mark from all articles."
8856   (interactive)
8857   (save-excursion
8858     (while gnus-newsgroup-processable
8859       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8860   (gnus-summary-position-point))
8861
8862 (defun gnus-summary-add-mark (article type)
8863   "Mark ARTICLE with a mark of TYPE."
8864   (let ((vtype (car (assq type gnus-article-mark-lists)))
8865         var)
8866     (if (not vtype)
8867         (error "No such mark type: %s" type)
8868       (setq var (intern (format "gnus-newsgroup-%s" type)))
8869       (set var (cons article (symbol-value var)))
8870       (if (memq type '(processable cached replied forwarded recent saved))
8871           (gnus-summary-update-secondary-mark article)
8872         ;;; !!! This is bogus.  We should find out what primary
8873         ;;; !!! mark we want to set.
8874         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8875
8876 (defun gnus-summary-mark-as-expirable (n)
8877   "Mark N articles forward as expirable.
8878 If N is negative, mark backward instead.  The difference between N and
8879 the actual number of articles marked is returned."
8880   (interactive "p")
8881   (gnus-summary-mark-forward n gnus-expirable-mark))
8882
8883 (defun gnus-summary-mark-article-as-replied (article)
8884   "Mark ARTICLE as replied to and update the summary line.
8885 ARTICLE can also be a list of articles."
8886   (interactive (list (gnus-summary-article-number)))
8887   (let ((articles (if (listp article) article (list article))))
8888     (dolist (article articles)
8889       (push article gnus-newsgroup-replied)
8890       (let ((buffer-read-only nil))
8891         (when (gnus-summary-goto-subject article nil t)
8892           (gnus-summary-update-secondary-mark article))))))
8893
8894 (defun gnus-summary-mark-article-as-forwarded (article)
8895   "Mark ARTICLE as forwarded and update the summary line.
8896 ARTICLE can also be a list of articles."
8897   (let ((articles (if (listp article) article (list article))))
8898     (dolist (article articles)
8899       (push article gnus-newsgroup-forwarded)
8900       (let ((buffer-read-only nil))
8901         (when (gnus-summary-goto-subject article nil t)
8902           (gnus-summary-update-secondary-mark article))))))
8903
8904 (defun gnus-summary-set-bookmark (article)
8905   "Set a bookmark in current article."
8906   (interactive (list (gnus-summary-article-number)))
8907   (when (or (not (get-buffer gnus-article-buffer))
8908             (not gnus-current-article)
8909             (not gnus-article-current)
8910             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8911     (error "No current article selected"))
8912   ;; Remove old bookmark, if one exists.
8913   (let ((old (assq article gnus-newsgroup-bookmarks)))
8914     (when old
8915       (setq gnus-newsgroup-bookmarks
8916             (delq old gnus-newsgroup-bookmarks))))
8917   ;; Set the new bookmark, which is on the form
8918   ;; (article-number . line-number-in-body).
8919   (push
8920    (cons article
8921          (save-excursion
8922            (set-buffer gnus-article-buffer)
8923            (count-lines
8924             (min (point)
8925                  (save-excursion
8926                    (goto-char (point-min))
8927                    (search-forward "\n\n" nil t)
8928                    (point)))
8929             (point))))
8930    gnus-newsgroup-bookmarks)
8931   (gnus-message 6 "A bookmark has been added to the current article."))
8932
8933 (defun gnus-summary-remove-bookmark (article)
8934   "Remove the bookmark from the current article."
8935   (interactive (list (gnus-summary-article-number)))
8936   ;; Remove old bookmark, if one exists.
8937   (let ((old (assq article gnus-newsgroup-bookmarks)))
8938     (if old
8939         (progn
8940           (setq gnus-newsgroup-bookmarks
8941                 (delq old gnus-newsgroup-bookmarks))
8942           (gnus-message 6 "Removed bookmark."))
8943       (gnus-message 6 "No bookmark in current article."))))
8944
8945 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8946 (defun gnus-summary-mark-as-dormant (n)
8947   "Mark N articles forward as dormant.
8948 If N is negative, mark backward instead.  The difference between N and
8949 the actual number of articles marked is returned."
8950   (interactive "p")
8951   (gnus-summary-mark-forward n gnus-dormant-mark))
8952
8953 (defun gnus-summary-set-process-mark (article)
8954   "Set the process mark on ARTICLE and update the summary line."
8955   (setq gnus-newsgroup-processable
8956         (cons article
8957               (delq article gnus-newsgroup-processable)))
8958   (when (gnus-summary-goto-subject article)
8959     (gnus-summary-show-thread)
8960     (gnus-summary-goto-subject article)
8961     (gnus-summary-update-secondary-mark article)))
8962
8963 (defun gnus-summary-remove-process-mark (article)
8964   "Remove the process mark from ARTICLE and update the summary line."
8965   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8966   (when (gnus-summary-goto-subject article)
8967     (gnus-summary-show-thread)
8968     (gnus-summary-goto-subject article)
8969     (gnus-summary-update-secondary-mark article)))
8970
8971 (defun gnus-summary-set-saved-mark (article)
8972   "Set the process mark on ARTICLE and update the summary line."
8973   (push article gnus-newsgroup-saved)
8974   (when (gnus-summary-goto-subject article)
8975     (gnus-summary-update-secondary-mark article)))
8976
8977 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8978   "Mark N articles as read forwards.
8979 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8980 The difference between N and the actual number of articles marked is
8981 returned.
8982 Iff NO-EXPIRE, auto-expiry will be inhibited."
8983   (interactive "p")
8984   (gnus-summary-show-thread)
8985   (let ((backward (< n 0))
8986         (gnus-summary-goto-unread
8987          (and gnus-summary-goto-unread
8988               (not (eq gnus-summary-goto-unread 'never))
8989               (not (memq mark (list gnus-unread-mark
8990                                     gnus-ticked-mark gnus-dormant-mark)))))
8991         (n (abs n))
8992         (mark (or mark gnus-del-mark)))
8993     (while (and (> n 0)
8994                 (gnus-summary-mark-article nil mark no-expire)
8995                 (zerop (gnus-summary-next-subject
8996                         (if backward -1 1)
8997                         (and gnus-summary-goto-unread
8998                              (not (eq gnus-summary-goto-unread 'never)))
8999                         t)))
9000       (setq n (1- n)))
9001     (when (/= 0 n)
9002       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9003     (gnus-summary-recenter)
9004     (gnus-summary-position-point)
9005     (gnus-set-mode-line 'summary)
9006     n))
9007
9008 (defun gnus-summary-mark-article-as-read (mark)
9009   "Mark the current article quickly as read with MARK."
9010   (let ((article (gnus-summary-article-number)))
9011     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9012     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9013     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9014     (push (cons article mark) gnus-newsgroup-reads)
9015     ;; Possibly remove from cache, if that is used.
9016     (when gnus-use-cache
9017       (gnus-cache-enter-remove-article article))
9018     ;; Allow the backend to change the mark.
9019     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9020     ;; Check for auto-expiry.
9021     (when (and gnus-newsgroup-auto-expire
9022                (memq mark gnus-auto-expirable-marks))
9023       (setq mark gnus-expirable-mark)
9024       ;; Let the backend know about the mark change.
9025       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9026       (push article gnus-newsgroup-expirable))
9027     ;; Set the mark in the buffer.
9028     (gnus-summary-update-mark mark 'unread)
9029     t))
9030
9031 (defun gnus-summary-mark-article-as-unread (mark)
9032   "Mark the current article quickly as unread with MARK."
9033   (let* ((article (gnus-summary-article-number))
9034          (old-mark (gnus-summary-article-mark article)))
9035     ;; Allow the backend to change the mark.
9036     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9037     (if (eq mark old-mark)
9038         t
9039       (if (<= article 0)
9040           (progn
9041             (gnus-error 1 "Can't mark negative article numbers")
9042             nil)
9043         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9044         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9045         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9046         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9047         (cond ((= mark gnus-ticked-mark)
9048                (push article gnus-newsgroup-marked))
9049               ((= mark gnus-dormant-mark)
9050                (push article gnus-newsgroup-dormant))
9051               (t
9052                (push article gnus-newsgroup-unreads)))
9053         (gnus-pull article gnus-newsgroup-reads)
9054
9055         ;; See whether the article is to be put in the cache.
9056         (and gnus-use-cache
9057              (vectorp (gnus-summary-article-header article))
9058              (save-excursion
9059                (gnus-cache-possibly-enter-article
9060                 gnus-newsgroup-name article
9061                 (gnus-summary-article-header article)
9062                 (= mark gnus-ticked-mark)
9063                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9064
9065         ;; Fix the mark.
9066         (gnus-summary-update-mark mark 'unread)
9067         t))))
9068
9069 (defun gnus-summary-mark-article (&optional article mark no-expire)
9070   "Mark ARTICLE with MARK.  MARK can be any character.
9071 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9072 `??' (dormant) and `?E' (expirable).
9073 If MARK is nil, then the default character `?r' is used.
9074 If ARTICLE is nil, then the article on the current line will be
9075 marked.
9076 Iff NO-EXPIRE, auto-expiry will be inhibited."
9077   ;; The mark might be a string.
9078   (when (stringp mark)
9079     (setq mark (aref mark 0)))
9080   ;; If no mark is given, then we check auto-expiring.
9081   (when (null mark)
9082     (setq mark gnus-del-mark))
9083   (when (and (not no-expire)
9084              gnus-newsgroup-auto-expire
9085              (memq mark gnus-auto-expirable-marks))
9086     (setq mark gnus-expirable-mark))
9087   (let ((article (or article (gnus-summary-article-number)))
9088         (old-mark (gnus-summary-article-mark article)))
9089     ;; Allow the backend to change the mark.
9090     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9091     (if (eq mark old-mark)
9092         t
9093       (unless article
9094         (error "No article on current line"))
9095       (if (not (if (or (= mark gnus-unread-mark)
9096                        (= mark gnus-ticked-mark)
9097                        (= mark gnus-dormant-mark))
9098                    (gnus-mark-article-as-unread article mark)
9099                  (gnus-mark-article-as-read article mark)))
9100           t
9101         ;; See whether the article is to be put in the cache.
9102         (and gnus-use-cache
9103              (not (= mark gnus-canceled-mark))
9104              (vectorp (gnus-summary-article-header article))
9105              (save-excursion
9106                (gnus-cache-possibly-enter-article
9107                 gnus-newsgroup-name article
9108                 (gnus-summary-article-header article)
9109                 (= mark gnus-ticked-mark)
9110                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9111
9112         (when (gnus-summary-goto-subject article nil t)
9113           (let ((buffer-read-only nil))
9114             (gnus-summary-show-thread)
9115             ;; Fix the mark.
9116             (gnus-summary-update-mark mark 'unread)
9117             t))))))
9118
9119 (defun gnus-summary-update-secondary-mark (article)
9120   "Update the secondary (read, process, cache) mark."
9121   (gnus-summary-update-mark
9122    (cond ((memq article gnus-newsgroup-processable)
9123           gnus-process-mark)
9124          ((memq article gnus-newsgroup-cached)
9125           gnus-cached-mark)
9126          ((memq article gnus-newsgroup-replied)
9127           gnus-replied-mark)
9128          ((memq article gnus-newsgroup-forwarded)
9129           gnus-forwarded-mark)
9130          ((memq article gnus-newsgroup-saved)
9131           gnus-saved-mark)
9132          ((memq article gnus-newsgroup-recent)
9133           gnus-recent-mark)
9134          ((memq article gnus-newsgroup-unseen)
9135           gnus-unseen-mark)
9136          (t gnus-no-mark))
9137    'replied)
9138   (when (gnus-visual-p 'summary-highlight 'highlight)
9139     (gnus-run-hooks 'gnus-summary-update-hook))
9140   t)
9141
9142 (defun gnus-summary-update-mark (mark type)
9143   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9144         (buffer-read-only nil))
9145     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9146     (when forward
9147       (when (looking-at "\r")
9148         (incf forward))
9149       (when (<= (+ forward (point)) (point-max))
9150         ;; Go to the right position on the line.
9151         (goto-char (+ forward (point)))
9152         ;; Replace the old mark with the new mark.
9153         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9154         ;; Optionally update the marks by some user rule.
9155         (when (eq type 'unread)
9156           (gnus-data-set-mark
9157            (gnus-data-find (gnus-summary-article-number)) mark)
9158           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9159
9160 (defun gnus-mark-article-as-read (article &optional mark)
9161   "Enter ARTICLE in the pertinent lists and remove it from others."
9162   ;; Make the article expirable.
9163   (let ((mark (or mark gnus-del-mark)))
9164     (if (= mark gnus-expirable-mark)
9165         (push article gnus-newsgroup-expirable)
9166       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9167     ;; Remove from unread and marked lists.
9168     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9169     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9170     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9171     (push (cons article mark) gnus-newsgroup-reads)
9172     ;; Possibly remove from cache, if that is used.
9173     (when gnus-use-cache
9174       (gnus-cache-enter-remove-article article))
9175     t))
9176
9177 (defun gnus-mark-article-as-unread (article &optional mark)
9178   "Enter ARTICLE in the pertinent lists and remove it from others."
9179   (let ((mark (or mark gnus-ticked-mark)))
9180     (if (<= article 0)
9181         (progn
9182           (gnus-error 1 "Can't mark negative article numbers")
9183           nil)
9184       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9185             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9186             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9187             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9188
9189       ;; Unsuppress duplicates?
9190       (when gnus-suppress-duplicates
9191         (gnus-dup-unsuppress-article article))
9192
9193       (cond ((= mark gnus-ticked-mark)
9194              (push article gnus-newsgroup-marked))
9195             ((= mark gnus-dormant-mark)
9196              (push article gnus-newsgroup-dormant))
9197             (t
9198              (push article gnus-newsgroup-unreads)))
9199       (gnus-pull article gnus-newsgroup-reads)
9200       t)))
9201
9202 (defalias 'gnus-summary-mark-as-unread-forward
9203   'gnus-summary-tick-article-forward)
9204 (make-obsolete 'gnus-summary-mark-as-unread-forward
9205                'gnus-summary-tick-article-forward)
9206 (defun gnus-summary-tick-article-forward (n)
9207   "Tick N articles forwards.
9208 If N is negative, tick backwards instead.
9209 The difference between N and the number of articles ticked is returned."
9210   (interactive "p")
9211   (gnus-summary-mark-forward n gnus-ticked-mark))
9212
9213 (defalias 'gnus-summary-mark-as-unread-backward
9214   'gnus-summary-tick-article-backward)
9215 (make-obsolete 'gnus-summary-mark-as-unread-backward
9216                'gnus-summary-tick-article-backward)
9217 (defun gnus-summary-tick-article-backward (n)
9218   "Tick N articles backwards.
9219 The difference between N and the number of articles ticked is returned."
9220   (interactive "p")
9221   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9222
9223 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9224 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9225 (defun gnus-summary-tick-article (&optional article clear-mark)
9226   "Mark current article as unread.
9227 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9228 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9229   (interactive)
9230   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9231                                        gnus-ticked-mark)))
9232
9233 (defun gnus-summary-mark-as-read-forward (n)
9234   "Mark N articles as read forwards.
9235 If N is negative, mark backwards instead.
9236 The difference between N and the actual number of articles marked is
9237 returned."
9238   (interactive "p")
9239   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9240
9241 (defun gnus-summary-mark-as-read-backward (n)
9242   "Mark the N articles as read backwards.
9243 The difference between N and the actual number of articles marked is
9244 returned."
9245   (interactive "p")
9246   (gnus-summary-mark-forward
9247    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9248
9249 (defun gnus-summary-mark-as-read (&optional article mark)
9250   "Mark current article as read.
9251 ARTICLE specifies the article to be marked as read.
9252 MARK specifies a string to be inserted at the beginning of the line."
9253   (gnus-summary-mark-article article mark))
9254
9255 (defun gnus-summary-clear-mark-forward (n)
9256   "Clear marks from N articles forward.
9257 If N is negative, clear backward instead.
9258 The difference between N and the number of marks cleared is returned."
9259   (interactive "p")
9260   (gnus-summary-mark-forward n gnus-unread-mark))
9261
9262 (defun gnus-summary-clear-mark-backward (n)
9263   "Clear marks from N articles backward.
9264 The difference between N and the number of marks cleared is returned."
9265   (interactive "p")
9266   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9267
9268 (defun gnus-summary-mark-unread-as-read ()
9269   "Intended to be used by `gnus-summary-mark-article-hook'."
9270   (when (memq gnus-current-article gnus-newsgroup-unreads)
9271     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9272
9273 (defun gnus-summary-mark-read-and-unread-as-read ()
9274   "Intended to be used by `gnus-summary-mark-article-hook'."
9275   (let ((mark (gnus-summary-article-mark)))
9276     (when (or (gnus-unread-mark-p mark)
9277               (gnus-read-mark-p mark))
9278       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9279
9280 (defun gnus-summary-mark-unread-as-ticked ()
9281   "Intended to be used by `gnus-summary-mark-article-hook'."
9282   (when (memq gnus-current-article gnus-newsgroup-unreads)
9283     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9284
9285 (defun gnus-summary-mark-region-as-read (point mark all)
9286   "Mark all unread articles between point and mark as read.
9287 If given a prefix, mark all articles between point and mark as read,
9288 even ticked and dormant ones."
9289   (interactive "r\nP")
9290   (save-excursion
9291     (let (article)
9292       (goto-char point)
9293       (beginning-of-line)
9294       (while (and
9295               (< (point) mark)
9296               (progn
9297                 (when (or all
9298                           (memq (setq article (gnus-summary-article-number))
9299                                 gnus-newsgroup-unreads))
9300                   (gnus-summary-mark-article article gnus-del-mark))
9301                 t)
9302               (gnus-summary-find-next))))))
9303
9304 (defun gnus-summary-mark-below (score mark)
9305   "Mark articles with score less than SCORE with MARK."
9306   (interactive "P\ncMark: ")
9307   (setq score (if score
9308                   (prefix-numeric-value score)
9309                 (or gnus-summary-default-score 0)))
9310   (save-excursion
9311     (set-buffer gnus-summary-buffer)
9312     (goto-char (point-min))
9313     (while
9314         (progn
9315           (and (< (gnus-summary-article-score) score)
9316                (gnus-summary-mark-article nil mark))
9317           (gnus-summary-find-next)))))
9318
9319 (defun gnus-summary-kill-below (&optional score)
9320   "Mark articles with score below SCORE as read."
9321   (interactive "P")
9322   (gnus-summary-mark-below score gnus-killed-mark))
9323
9324 (defun gnus-summary-clear-above (&optional score)
9325   "Clear all marks from articles with score above SCORE."
9326   (interactive "P")
9327   (gnus-summary-mark-above score gnus-unread-mark))
9328
9329 (defun gnus-summary-tick-above (&optional score)
9330   "Tick all articles with score above SCORE."
9331   (interactive "P")
9332   (gnus-summary-mark-above score gnus-ticked-mark))
9333
9334 (defun gnus-summary-mark-above (score mark)
9335   "Mark articles with score over SCORE with MARK."
9336   (interactive "P\ncMark: ")
9337   (setq score (if score
9338                   (prefix-numeric-value score)
9339                 (or gnus-summary-default-score 0)))
9340   (save-excursion
9341     (set-buffer gnus-summary-buffer)
9342     (goto-char (point-min))
9343     (while (and (progn
9344                   (when (> (gnus-summary-article-score) score)
9345                     (gnus-summary-mark-article nil mark))
9346                   t)
9347                 (gnus-summary-find-next)))))
9348
9349 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9350 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9351 (defun gnus-summary-limit-include-expunged (&optional no-error)
9352   "Display all the hidden articles that were expunged for low scores."
9353   (interactive)
9354   (let ((buffer-read-only nil))
9355     (let ((scored gnus-newsgroup-scored)
9356           headers h)
9357       (while scored
9358         (unless (gnus-summary-article-header (caar scored))
9359           (and (setq h (gnus-number-to-header (caar scored)))
9360                (< (cdar scored) gnus-summary-expunge-below)
9361                (push h headers)))
9362         (setq scored (cdr scored)))
9363       (if (not headers)
9364           (when (not no-error)
9365             (error "No expunged articles hidden"))
9366         (goto-char (point-min))
9367         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9368         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9369         (mapcar (lambda (x) (push (mail-header-number x)
9370                                   gnus-newsgroup-limit))
9371                 headers)
9372         (gnus-summary-prepare-unthreaded (nreverse headers))
9373         (goto-char (point-min))
9374         (gnus-summary-position-point)
9375         t))))
9376
9377 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9378   "Mark all unread articles in this newsgroup as read.
9379 If prefix argument ALL is non-nil, ticked and dormant articles will
9380 also be marked as read.
9381 If QUIETLY is non-nil, no questions will be asked.
9382 If TO-HERE is non-nil, it should be a point in the buffer.  All
9383 articles before (after, if REVERSE is set) this point will be marked as read.
9384 Note that this function will only catch up the unread article
9385 in the current summary buffer limitation.
9386 The number of articles marked as read is returned."
9387   (interactive "P")
9388   (prog1
9389       (save-excursion
9390         (when (or quietly
9391                   (not gnus-interactive-catchup) ;Without confirmation?
9392                   gnus-expert-user
9393                   (gnus-y-or-n-p
9394                    (if all
9395                        "Mark absolutely all articles as read? "
9396                      "Mark all unread articles as read? ")))
9397           (if (and not-mark
9398                    (not gnus-newsgroup-adaptive)
9399                    (not gnus-newsgroup-auto-expire)
9400                    (not gnus-suppress-duplicates)
9401                    (or (not gnus-use-cache)
9402                        (eq gnus-use-cache 'passive)))
9403               (progn
9404                 (when all
9405                   (setq gnus-newsgroup-marked nil
9406                         gnus-newsgroup-dormant nil))
9407                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9408             ;; We actually mark all articles as canceled, which we
9409             ;; have to do when using auto-expiry or adaptive scoring.
9410             (gnus-summary-show-all-threads)
9411             (if (and to-here reverse)
9412                 (progn
9413                   (goto-char to-here)
9414                   (while (and
9415                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9416                           (gnus-summary-find-next (not all) nil nil t))))
9417               (when (gnus-summary-first-subject (not all) t)
9418                 (while (and
9419                         (if to-here (< (point) to-here) t)
9420                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9421                         (gnus-summary-find-next (not all) nil nil t)))))
9422             (gnus-set-mode-line 'summary))
9423           t))
9424     (gnus-summary-position-point)))
9425
9426 (defun gnus-summary-catchup-to-here (&optional all)
9427   "Mark all unticked articles before the current one as read.
9428 If ALL is non-nil, also mark ticked and dormant articles as read."
9429   (interactive "P")
9430   (save-excursion
9431     (gnus-save-hidden-threads
9432       (let ((beg (point)))
9433         ;; We check that there are unread articles.
9434         (when (or all (gnus-summary-find-prev))
9435           (gnus-summary-catchup all t beg)))))
9436   (gnus-summary-position-point))
9437
9438 (defun gnus-summary-catchup-from-here (&optional all)
9439   "Mark all unticked articles after the current one as read.
9440 If ALL is non-nil, also mark ticked and dormant articles as read."
9441   (interactive "P")
9442   (save-excursion
9443     (gnus-save-hidden-threads
9444       (let ((beg (point)))
9445         ;; We check that there are unread articles.
9446         (when (or all (gnus-summary-find-next))
9447           (gnus-summary-catchup all t beg nil t)))))
9448   (gnus-summary-position-point))
9449
9450 (defun gnus-summary-catchup-all (&optional quietly)
9451   "Mark all articles in this newsgroup as read."
9452   (interactive "P")
9453   (gnus-summary-catchup t quietly))
9454
9455 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9456   "Mark all unread articles in this group as read, then exit.
9457 If prefix argument ALL is non-nil, all articles are marked as read.
9458 If QUIETLY is non-nil, no questions will be asked."
9459   (interactive "P")
9460   (when (gnus-summary-catchup all quietly nil 'fast)
9461     ;; Select next newsgroup or exit.
9462     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9463              (eq gnus-auto-select-next 'quietly))
9464         (gnus-summary-next-group nil)
9465       (gnus-summary-exit))))
9466
9467 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9468   "Mark all articles in this newsgroup as read, and then exit."
9469   (interactive "P")
9470   (gnus-summary-catchup-and-exit t quietly))
9471
9472 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9473   "Mark all articles in this group as read and select the next group.
9474 If given a prefix, mark all articles, unread as well as ticked, as
9475 read."
9476   (interactive "P")
9477   (save-excursion
9478     (gnus-summary-catchup all))
9479   (gnus-summary-next-group))
9480
9481 ;;;
9482 ;;; with article
9483 ;;;
9484
9485 (defmacro gnus-with-article (article &rest forms)
9486   "Select ARTICLE and perform FORMS in the original article buffer.
9487 Then replace the article with the result."
9488   `(progn
9489      ;; We don't want the article to be marked as read.
9490      (let (gnus-mark-article-hook)
9491        (gnus-summary-select-article t t nil ,article))
9492      (set-buffer gnus-original-article-buffer)
9493      ,@forms
9494      (if (not (gnus-check-backend-function
9495                'request-replace-article (car gnus-article-current)))
9496          (gnus-message 5 "Read-only group; not replacing")
9497        (unless (gnus-request-replace-article
9498                 ,article (car gnus-article-current)
9499                 (current-buffer) t)
9500          (error "Couldn't replace article")))
9501      ;; The cache and backlog have to be flushed somewhat.
9502      (when gnus-keep-backlog
9503        (gnus-backlog-remove-article
9504         (car gnus-article-current) (cdr gnus-article-current)))
9505      (when gnus-use-cache
9506        (gnus-cache-update-article
9507         (car gnus-article-current) (cdr gnus-article-current)))))
9508
9509 (put 'gnus-with-article 'lisp-indent-function 1)
9510 (put 'gnus-with-article 'edebug-form-spec '(form body))
9511
9512 ;; Thread-based commands.
9513
9514 (defun gnus-summary-articles-in-thread (&optional article)
9515   "Return a list of all articles in the current thread.
9516 If ARTICLE is non-nil, return all articles in the thread that starts
9517 with that article."
9518   (let* ((article (or article (gnus-summary-article-number)))
9519          (data (gnus-data-find-list article))
9520          (top-level (gnus-data-level (car data)))
9521          (top-subject
9522           (cond ((null gnus-thread-operation-ignore-subject)
9523                  (gnus-simplify-subject-re
9524                   (mail-header-subject (gnus-data-header (car data)))))
9525                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9526                  (gnus-simplify-subject-fuzzy
9527                   (mail-header-subject (gnus-data-header (car data)))))
9528                 (t nil)))
9529          (end-point (save-excursion
9530                       (if (gnus-summary-go-to-next-thread)
9531                           (point) (point-max))))
9532          articles)
9533     (while (and data
9534                 (< (gnus-data-pos (car data)) end-point))
9535       (when (or (not top-subject)
9536                 (string= top-subject
9537                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9538                              (gnus-simplify-subject-fuzzy
9539                               (mail-header-subject
9540                                (gnus-data-header (car data))))
9541                            (gnus-simplify-subject-re
9542                             (mail-header-subject
9543                              (gnus-data-header (car data)))))))
9544         (push (gnus-data-number (car data)) articles))
9545       (unless (and (setq data (cdr data))
9546                    (> (gnus-data-level (car data)) top-level))
9547         (setq data nil)))
9548     ;; Return the list of articles.
9549     (nreverse articles)))
9550
9551 (defun gnus-summary-rethread-current ()
9552   "Rethread the thread the current article is part of."
9553   (interactive)
9554   (let* ((gnus-show-threads t)
9555          (article (gnus-summary-article-number))
9556          (id (mail-header-id (gnus-summary-article-header)))
9557          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9558     (unless id
9559       (error "No article on the current line"))
9560     (gnus-rebuild-thread id)
9561     (gnus-summary-goto-subject article)))
9562
9563 (defun gnus-summary-reparent-thread ()
9564   "Make the current article child of the marked (or previous) article.
9565
9566 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9567 is non-nil or the Subject: of both articles are the same."
9568   (interactive)
9569   (unless (not (gnus-group-read-only-p))
9570     (error "The current newsgroup does not support article editing"))
9571   (unless (<= (length gnus-newsgroup-processable) 1)
9572     (error "No more than one article may be marked"))
9573   (save-window-excursion
9574     (let ((gnus-article-buffer " *reparent*")
9575           (current-article (gnus-summary-article-number))
9576           ;; First grab the marked article, otherwise one line up.
9577           (parent-article (if (not (null gnus-newsgroup-processable))
9578                               (car gnus-newsgroup-processable)
9579                             (save-excursion
9580                               (if (eq (forward-line -1) 0)
9581                                   (gnus-summary-article-number)
9582                                 (error "Beginning of summary buffer"))))))
9583       (unless (not (eq current-article parent-article))
9584         (error "An article may not be self-referential"))
9585       (let ((message-id (mail-header-id
9586                          (gnus-summary-article-header parent-article))))
9587         (unless (and message-id (not (equal message-id "")))
9588           (error "No message-id in desired parent"))
9589         (gnus-with-article current-article
9590           (save-restriction
9591             (goto-char (point-min))
9592             (message-narrow-to-head)
9593             (if (re-search-forward "^References: " nil t)
9594                 (progn
9595                   (re-search-forward "^[^ \t]" nil t)
9596                   (forward-line -1)
9597                   (end-of-line)
9598                   (insert " " message-id))
9599               (insert "References: " message-id "\n"))))
9600         (set-buffer gnus-summary-buffer)
9601         (gnus-summary-unmark-all-processable)
9602         (gnus-summary-update-article current-article)
9603         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9604             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9605         (gnus-summary-rethread-current)
9606         (gnus-message 3 "Article %d is now the child of article %d"
9607                       current-article parent-article)))))
9608
9609 (defun gnus-summary-toggle-threads (&optional arg)
9610   "Toggle showing conversation threads.
9611 If ARG is positive number, turn showing conversation threads on."
9612   (interactive "P")
9613   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9614     (setq gnus-show-threads
9615           (if (null arg) (not gnus-show-threads)
9616             (> (prefix-numeric-value arg) 0)))
9617     (gnus-summary-prepare)
9618     (gnus-summary-goto-subject current)
9619     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9620     (gnus-summary-position-point)))
9621
9622 (defun gnus-summary-show-all-threads ()
9623   "Show all threads."
9624   (interactive)
9625   (save-excursion
9626     (let ((buffer-read-only nil))
9627       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9628   (gnus-summary-position-point))
9629
9630 (defun gnus-summary-show-thread ()
9631   "Show thread subtrees.
9632 Returns nil if no thread was there to be shown."
9633   (interactive)
9634   (let ((buffer-read-only nil)
9635         (orig (point))
9636         ;; first goto end then to beg, to have point at beg after let
9637         (end (progn (end-of-line) (point)))
9638         (beg (progn (beginning-of-line) (point))))
9639     (prog1
9640         ;; Any hidden lines here?
9641         (search-forward "\r" end t)
9642       (subst-char-in-region beg end ?\^M ?\n t)
9643       (goto-char orig)
9644       (gnus-summary-position-point))))
9645
9646 (defun gnus-summary-hide-all-threads ()
9647   "Hide all thread subtrees."
9648   (interactive)
9649   (save-excursion
9650     (goto-char (point-min))
9651     (gnus-summary-hide-thread)
9652     (while (zerop (gnus-summary-next-thread 1 t))
9653       (gnus-summary-hide-thread)))
9654   (gnus-summary-position-point))
9655
9656 (defun gnus-summary-hide-thread ()
9657   "Hide thread subtrees.
9658 Returns nil if no threads were there to be hidden."
9659   (interactive)
9660   (let ((buffer-read-only nil)
9661         (start (point))
9662         (article (gnus-summary-article-number)))
9663     (goto-char start)
9664     ;; Go forward until either the buffer ends or the subthread
9665     ;; ends.
9666     (when (and (not (eobp))
9667                (or (zerop (gnus-summary-next-thread 1 t))
9668                    (goto-char (point-max))))
9669       (prog1
9670           (if (and (> (point) start)
9671                    (search-backward "\n" start t))
9672               (progn
9673                 (subst-char-in-region start (point) ?\n ?\^M)
9674                 (gnus-summary-goto-subject article))
9675             (goto-char start)
9676             nil)))))
9677
9678 (defun gnus-summary-go-to-next-thread (&optional previous)
9679   "Go to the same level (or less) next thread.
9680 If PREVIOUS is non-nil, go to previous thread instead.
9681 Return the article number moved to, or nil if moving was impossible."
9682   (let ((level (gnus-summary-thread-level))
9683         (way (if previous -1 1))
9684         (beg (point)))
9685     (forward-line way)
9686     (while (and (not (eobp))
9687                 (< level (gnus-summary-thread-level)))
9688       (forward-line way))
9689     (if (eobp)
9690         (progn
9691           (goto-char beg)
9692           nil)
9693       (setq beg (point))
9694       (prog1
9695           (gnus-summary-article-number)
9696         (goto-char beg)))))
9697
9698 (defun gnus-summary-next-thread (n &optional silent)
9699   "Go to the same level next N'th thread.
9700 If N is negative, search backward instead.
9701 Returns the difference between N and the number of skips actually
9702 done.
9703
9704 If SILENT, don't output messages."
9705   (interactive "p")
9706   (let ((backward (< n 0))
9707         (n (abs n)))
9708     (while (and (> n 0)
9709                 (gnus-summary-go-to-next-thread backward))
9710       (decf n))
9711     (unless silent
9712       (gnus-summary-position-point))
9713     (when (and (not silent) (/= 0 n))
9714       (gnus-message 7 "No more threads"))
9715     n))
9716
9717 (defun gnus-summary-prev-thread (n)
9718   "Go to the same level previous N'th thread.
9719 Returns the difference between N and the number of skips actually
9720 done."
9721   (interactive "p")
9722   (gnus-summary-next-thread (- n)))
9723
9724 (defun gnus-summary-go-down-thread ()
9725   "Go down one level in the current thread."
9726   (let ((children (gnus-summary-article-children)))
9727     (when children
9728       (gnus-summary-goto-subject (car children)))))
9729
9730 (defun gnus-summary-go-up-thread ()
9731   "Go up one level in the current thread."
9732   (let ((parent (gnus-summary-article-parent)))
9733     (when parent
9734       (gnus-summary-goto-subject parent))))
9735
9736 (defun gnus-summary-down-thread (n)
9737   "Go down thread N steps.
9738 If N is negative, go up instead.
9739 Returns the difference between N and how many steps down that were
9740 taken."
9741   (interactive "p")
9742   (let ((up (< n 0))
9743         (n (abs n)))
9744     (while (and (> n 0)
9745                 (if up (gnus-summary-go-up-thread)
9746                   (gnus-summary-go-down-thread)))
9747       (setq n (1- n)))
9748     (gnus-summary-position-point)
9749     (when (/= 0 n)
9750       (gnus-message 7 "Can't go further"))
9751     n))
9752
9753 (defun gnus-summary-up-thread (n)
9754   "Go up thread N steps.
9755 If N is negative, go down instead.
9756 Returns the difference between N and how many steps down that were
9757 taken."
9758   (interactive "p")
9759   (gnus-summary-down-thread (- n)))
9760
9761 (defun gnus-summary-top-thread ()
9762   "Go to the top of the thread."
9763   (interactive)
9764   (while (gnus-summary-go-up-thread))
9765   (gnus-summary-article-number))
9766
9767 (defun gnus-summary-kill-thread (&optional unmark)
9768   "Mark articles under current thread as read.
9769 If the prefix argument is positive, remove any kinds of marks.
9770 If the prefix argument is negative, tick articles instead."
9771   (interactive "P")
9772   (when unmark
9773     (setq unmark (prefix-numeric-value unmark)))
9774   (let ((articles (gnus-summary-articles-in-thread)))
9775     (save-excursion
9776       ;; Expand the thread.
9777       (gnus-summary-show-thread)
9778       ;; Mark all the articles.
9779       (while articles
9780         (gnus-summary-goto-subject (car articles))
9781         (cond ((null unmark)
9782                (gnus-summary-mark-article-as-read gnus-killed-mark))
9783               ((> unmark 0)
9784                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9785               (t
9786                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9787         (setq articles (cdr articles))))
9788     ;; Hide killed subtrees.
9789     (and (null unmark)
9790          gnus-thread-hide-killed
9791          (gnus-summary-hide-thread))
9792     ;; If marked as read, go to next unread subject.
9793     (when (null unmark)
9794       ;; Go to next unread subject.
9795       (gnus-summary-next-subject 1 t)))
9796   (gnus-set-mode-line 'summary))
9797
9798 ;; Summary sorting commands
9799
9800 (defun gnus-summary-sort-by-number (&optional reverse)
9801   "Sort the summary buffer by article number.
9802 Argument REVERSE means reverse order."
9803   (interactive "P")
9804   (gnus-summary-sort 'number reverse))
9805
9806 (defun gnus-summary-sort-by-author (&optional reverse)
9807   "Sort the summary buffer by author name alphabetically.
9808 If `case-fold-search' is non-nil, case of letters is ignored.
9809 Argument REVERSE means reverse order."
9810   (interactive "P")
9811   (gnus-summary-sort 'author reverse))
9812
9813 (defun gnus-summary-sort-by-subject (&optional reverse)
9814   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9815 If `case-fold-search' is non-nil, case of letters is ignored.
9816 Argument REVERSE means reverse order."
9817   (interactive "P")
9818   (gnus-summary-sort 'subject reverse))
9819
9820 (defun gnus-summary-sort-by-date (&optional reverse)
9821   "Sort the summary buffer by date.
9822 Argument REVERSE means reverse order."
9823   (interactive "P")
9824   (gnus-summary-sort 'date reverse))
9825
9826 (defun gnus-summary-sort-by-score (&optional reverse)
9827   "Sort the summary buffer by score.
9828 Argument REVERSE means reverse order."
9829   (interactive "P")
9830   (gnus-summary-sort 'score reverse))
9831
9832 (defun gnus-summary-sort-by-lines (&optional reverse)
9833   "Sort the summary buffer by the number of lines.
9834 Argument REVERSE means reverse order."
9835   (interactive "P")
9836   (gnus-summary-sort 'lines reverse))
9837
9838 (defun gnus-summary-sort-by-chars (&optional reverse)
9839   "Sort the summary buffer by article length.
9840 Argument REVERSE means reverse order."
9841   (interactive "P")
9842   (gnus-summary-sort 'chars reverse))
9843
9844 (defun gnus-summary-sort-by-original (&optional reverse)
9845   "Sort the summary buffer using the default sorting method.
9846 Argument REVERSE means reverse order."
9847   (interactive "P")
9848   (let* ((buffer-read-only)
9849          (gnus-summary-prepare-hook nil))
9850     ;; We do the sorting by regenerating the threads.
9851     (gnus-summary-prepare)
9852     ;; Hide subthreads if needed.
9853     (when (and gnus-show-threads gnus-thread-hide-subtree)
9854       (gnus-summary-hide-all-threads))))
9855
9856 (defun gnus-summary-sort (predicate reverse)
9857   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9858   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9859          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9860          (gnus-thread-sort-functions
9861           (if (not reverse)
9862               thread
9863             `(lambda (t1 t2)
9864                (,thread t2 t1))))
9865          (gnus-sort-gathered-threads-function
9866           gnus-thread-sort-functions)
9867          (gnus-article-sort-functions
9868           (if (not reverse)
9869               article
9870             `(lambda (t1 t2)
9871                (,article t2 t1))))
9872          (buffer-read-only)
9873          (gnus-summary-prepare-hook nil))
9874     ;; We do the sorting by regenerating the threads.
9875     (gnus-summary-prepare)
9876     ;; Hide subthreads if needed.
9877     (when (and gnus-show-threads gnus-thread-hide-subtree)
9878       (gnus-summary-hide-all-threads))))
9879
9880 ;; Summary saving commands.
9881
9882 (defun gnus-summary-save-article (&optional n not-saved)
9883   "Save the current article using the default saver function.
9884 If N is a positive number, save the N next articles.
9885 If N is a negative number, save the N previous articles.
9886 If N is nil and any articles have been marked with the process mark,
9887 save those articles instead.
9888 The variable `gnus-default-article-saver' specifies the saver function."
9889   (interactive "P")
9890   (let* ((articles (gnus-summary-work-articles n))
9891          (save-buffer (save-excursion
9892                         (nnheader-set-temp-buffer " *Gnus Save*")))
9893          (num (length articles))
9894          header file)
9895     (dolist (article articles)
9896       (setq header (gnus-summary-article-header article))
9897       (if (not (vectorp header))
9898           ;; This is a pseudo-article.
9899           (if (assq 'name header)
9900               (gnus-copy-file (cdr (assq 'name header)))
9901             (gnus-message 1 "Article %d is unsaveable" article))
9902         ;; This is a real article.
9903         (save-window-excursion
9904           (gnus-summary-select-article t nil nil article))
9905         (save-excursion
9906           (set-buffer save-buffer)
9907           (erase-buffer)
9908           (insert-buffer-substring gnus-original-article-buffer))
9909         (setq file (gnus-article-save save-buffer file num))
9910         (gnus-summary-remove-process-mark article)
9911         (unless not-saved
9912           (gnus-summary-set-saved-mark article))))
9913     (gnus-kill-buffer save-buffer)
9914     (gnus-summary-position-point)
9915     (gnus-set-mode-line 'summary)
9916     n))
9917
9918 (defun gnus-summary-pipe-output (&optional arg)
9919   "Pipe the current article to a subprocess.
9920 If N is a positive number, pipe the N next articles.
9921 If N is a negative number, pipe the N previous articles.
9922 If N is nil and any articles have been marked with the process mark,
9923 pipe those articles instead."
9924   (interactive "P")
9925   (require 'gnus-art)
9926   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9927     (gnus-summary-save-article arg t))
9928   (let ((buffer (get-buffer "*Shell Command Output*")))
9929     (if (and buffer
9930              (with-current-buffer buffer (> (point-max) (point-min))))
9931         (gnus-configure-windows 'pipe))))
9932
9933 (defun gnus-summary-save-article-mail (&optional arg)
9934   "Append the current article to an mail file.
9935 If N is a positive number, save the N next articles.
9936 If N is a negative number, save the N previous articles.
9937 If N is nil and any articles have been marked with the process mark,
9938 save those articles instead."
9939   (interactive "P")
9940   (require 'gnus-art)
9941   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9942     (gnus-summary-save-article arg)))
9943
9944 (defun gnus-summary-save-article-rmail (&optional arg)
9945   "Append the current article to an rmail file.
9946 If N is a positive number, save the N next articles.
9947 If N is a negative number, save the N previous articles.
9948 If N is nil and any articles have been marked with the process mark,
9949 save those articles instead."
9950   (interactive "P")
9951   (require 'gnus-art)
9952   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9953     (gnus-summary-save-article arg)))
9954
9955 (defun gnus-summary-save-article-file (&optional arg)
9956   "Append the current article to a file.
9957 If N is a positive number, save the N next articles.
9958 If N is a negative number, save the N previous articles.
9959 If N is nil and any articles have been marked with the process mark,
9960 save those articles instead."
9961   (interactive "P")
9962   (require 'gnus-art)
9963   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9964     (gnus-summary-save-article arg)))
9965
9966 (defun gnus-summary-write-article-file (&optional arg)
9967   "Write the current article to a file, deleting the previous file.
9968 If N is a positive number, save the N next articles.
9969 If N is a negative number, save the N previous articles.
9970 If N is nil and any articles have been marked with the process mark,
9971 save those articles instead."
9972   (interactive "P")
9973   (require 'gnus-art)
9974   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9975     (gnus-summary-save-article arg)))
9976
9977 (defun gnus-summary-save-article-body-file (&optional arg)
9978   "Append the current article body to a file.
9979 If N is a positive number, save the N next articles.
9980 If N is a negative number, save the N previous articles.
9981 If N is nil and any articles have been marked with the process mark,
9982 save those articles instead."
9983   (interactive "P")
9984   (require 'gnus-art)
9985   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9986     (gnus-summary-save-article arg)))
9987
9988 (defun gnus-summary-pipe-message (program)
9989   "Pipe the current article through PROGRAM."
9990   (interactive "sProgram: ")
9991   (gnus-summary-select-article)
9992   (let ((mail-header-separator ""))
9993     (gnus-eval-in-buffer-window gnus-article-buffer
9994       (save-restriction
9995         (widen)
9996         (let ((start (window-start))
9997               buffer-read-only)
9998           (message-pipe-buffer-body program)
9999           (set-window-start (get-buffer-window (current-buffer)) start))))))
10000
10001 (defun gnus-get-split-value (methods)
10002   "Return a value based on the split METHODS."
10003   (let (split-name method result match)
10004     (when methods
10005       (save-excursion
10006         (set-buffer gnus-original-article-buffer)
10007         (save-restriction
10008           (nnheader-narrow-to-headers)
10009           (while (and methods (not split-name))
10010             (goto-char (point-min))
10011             (setq method (pop methods))
10012             (setq match (car method))
10013             (when (cond
10014                    ((stringp match)
10015                     ;; Regular expression.
10016                     (ignore-errors
10017                       (re-search-forward match nil t)))
10018                    ((gnus-functionp match)
10019                     ;; Function.
10020                     (save-restriction
10021                       (widen)
10022                       (setq result (funcall match gnus-newsgroup-name))))
10023                    ((consp match)
10024                     ;; Form.
10025                     (save-restriction
10026                       (widen)
10027                       (setq result (eval match)))))
10028               (setq split-name (cdr method))
10029               (cond ((stringp result)
10030                      (push (expand-file-name
10031                             result gnus-article-save-directory)
10032                            split-name))
10033                     ((consp result)
10034                      (setq split-name (append result split-name)))))))))
10035     (nreverse split-name)))
10036
10037 (defun gnus-valid-move-group-p (group)
10038   (and (boundp group)
10039        (symbol-name group)
10040        (symbol-value group)
10041        (gnus-get-function (gnus-find-method-for-group
10042                            (symbol-name group)) 'request-accept-article t)))
10043
10044 (defun gnus-read-move-group-name (prompt default articles prefix)
10045   "Read a group name."
10046   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10047          (minibuffer-confirm-incomplete nil) ; XEmacs
10048          (prom
10049           (format "%s %s to:"
10050                   prompt
10051                   (if (> (length articles) 1)
10052                       (format "these %d articles" (length articles))
10053                     "this article")))
10054          (to-newsgroup
10055           (cond
10056            ((null split-name)
10057             (gnus-completing-read default prom
10058                                   gnus-active-hashtb
10059                                   'gnus-valid-move-group-p
10060                                   nil prefix
10061                                   'gnus-group-history))
10062            ((= 1 (length split-name))
10063             (gnus-completing-read (car split-name) prom
10064                                   gnus-active-hashtb
10065                                   'gnus-valid-move-group-p
10066                                   nil nil
10067                                   'gnus-group-history))
10068            (t
10069             (gnus-completing-read nil prom
10070                                   (mapcar (lambda (el) (list el))
10071                                           (nreverse split-name))
10072                                   nil nil nil
10073                                   'gnus-group-history))))
10074          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10075     (when to-newsgroup
10076       (if (or (string= to-newsgroup "")
10077               (string= to-newsgroup prefix))
10078           (setq to-newsgroup default))
10079       (unless to-newsgroup
10080         (error "No group name entered"))
10081       (or (gnus-active to-newsgroup)
10082           (gnus-activate-group to-newsgroup nil nil to-method)
10083           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10084                                      to-newsgroup))
10085               (or (and (gnus-request-create-group to-newsgroup to-method)
10086                        (gnus-activate-group
10087                         to-newsgroup nil nil to-method)
10088                        (gnus-subscribe-group to-newsgroup))
10089                   (error "Couldn't create group %s" to-newsgroup)))
10090           (error "No such group: %s" to-newsgroup)))
10091     to-newsgroup))
10092
10093 (defun gnus-summary-save-parts (type dir n &optional reverse)
10094   "Save parts matching TYPE to DIR.
10095 If REVERSE, save parts that do not match TYPE."
10096   (interactive
10097    (list (read-string "Save parts of type: "
10098                       (or (car gnus-summary-save-parts-type-history)
10099                           gnus-summary-save-parts-default-mime)
10100                       'gnus-summary-save-parts-type-history)
10101          (setq gnus-summary-save-parts-last-directory
10102                (read-file-name "Save to directory: "
10103                                gnus-summary-save-parts-last-directory
10104                                nil t))
10105          current-prefix-arg))
10106   (gnus-summary-iterate n
10107     (let ((gnus-display-mime-function nil)
10108           (gnus-inhibit-treatment t))
10109       (gnus-summary-select-article))
10110     (save-excursion
10111       (set-buffer gnus-article-buffer)
10112       (let ((handles (or gnus-article-mime-handles
10113                          (mm-dissect-buffer) (mm-uu-dissect))))
10114         (when handles
10115           (gnus-summary-save-parts-1 type dir handles reverse)
10116           (unless gnus-article-mime-handles ;; Don't destroy this case.
10117             (mm-destroy-parts handles)))))))
10118
10119 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10120   (if (stringp (car handle))
10121       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10122               (cdr handle))
10123     (when (if reverse
10124               (not (string-match type (mm-handle-media-type handle)))
10125             (string-match type (mm-handle-media-type handle)))
10126       (let ((file (expand-file-name
10127                    (file-name-nondirectory
10128                     (or
10129                      (mail-content-type-get
10130                       (mm-handle-disposition handle) 'filename)
10131                      (concat gnus-newsgroup-name
10132                              "." (number-to-string
10133                                   (cdr gnus-article-current)))))
10134                    dir)))
10135         (unless (file-exists-p file)
10136           (mm-save-part-to-file handle file))))))
10137
10138 ;; Summary extract commands
10139
10140 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10141   (let ((buffer-read-only nil)
10142         (article (gnus-summary-article-number))
10143         after-article b e)
10144     (unless (gnus-summary-goto-subject article)
10145       (error "No such article: %d" article))
10146     (gnus-summary-position-point)
10147     ;; If all commands are to be bunched up on one line, we collect
10148     ;; them here.
10149     (unless gnus-view-pseudos-separately
10150       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10151             files action)
10152         (while ps
10153           (setq action (cdr (assq 'action (car ps))))
10154           (setq files (list (cdr (assq 'name (car ps)))))
10155           (while (and ps (cdr ps)
10156                       (string= (or action "1")
10157                                (or (cdr (assq 'action (cadr ps))) "2")))
10158             (push (cdr (assq 'name (cadr ps))) files)
10159             (setcdr ps (cddr ps)))
10160           (when files
10161             (when (not (string-match "%s" action))
10162               (push " " files))
10163             (push " " files)
10164             (when (assq 'execute (car ps))
10165               (setcdr (assq 'execute (car ps))
10166                       (funcall (if (string-match "%s" action)
10167                                    'format 'concat)
10168                                action
10169                                (mapconcat
10170                                 (lambda (f)
10171                                   (if (equal f " ")
10172                                       f
10173                                     (gnus-quote-arg-for-sh-or-csh f)))
10174                                 files " ")))))
10175           (setq ps (cdr ps)))))
10176     (if (and gnus-view-pseudos (not not-view))
10177         (while pslist
10178           (when (assq 'execute (car pslist))
10179             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10180                                   (eq gnus-view-pseudos 'not-confirm)))
10181           (setq pslist (cdr pslist)))
10182       (save-excursion
10183         (while pslist
10184           (setq after-article (or (cdr (assq 'article (car pslist)))
10185                                   (gnus-summary-article-number)))
10186           (gnus-summary-goto-subject after-article)
10187           (forward-line 1)
10188           (setq b (point))
10189           (insert "    " (file-name-nondirectory
10190                           (cdr (assq 'name (car pslist))))
10191                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10192           (setq e (point))
10193           (forward-line -1)             ; back to `b'
10194           (gnus-add-text-properties
10195            b (1- e) (list 'gnus-number gnus-reffed-article-number
10196                           gnus-mouse-face-prop gnus-mouse-face))
10197           (gnus-data-enter
10198            after-article gnus-reffed-article-number
10199            gnus-unread-mark b (car pslist) 0 (- e b))
10200           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10201           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10202           (setq pslist (cdr pslist)))))))
10203
10204 (defun gnus-pseudos< (p1 p2)
10205   (let ((c1 (cdr (assq 'action p1)))
10206         (c2 (cdr (assq 'action p2))))
10207     (and c1 c2 (string< c1 c2))))
10208
10209 (defun gnus-request-pseudo-article (props)
10210   (cond ((assq 'execute props)
10211          (gnus-execute-command (cdr (assq 'execute props)))))
10212   (let ((gnus-current-article (gnus-summary-article-number)))
10213     (gnus-run-hooks 'gnus-mark-article-hook)))
10214
10215 (defun gnus-execute-command (command &optional automatic)
10216   (save-excursion
10217     (gnus-article-setup-buffer)
10218     (set-buffer gnus-article-buffer)
10219     (setq buffer-read-only nil)
10220     (let ((command (if automatic command
10221                      (read-string "Command: " (cons command 0)))))
10222       (erase-buffer)
10223       (insert "$ " command "\n\n")
10224       (if gnus-view-pseudo-asynchronously
10225           (start-process "gnus-execute" (current-buffer) shell-file-name
10226                          shell-command-switch command)
10227         (call-process shell-file-name nil t nil
10228                       shell-command-switch command)))))
10229
10230 ;; Summary kill commands.
10231
10232 (defun gnus-summary-edit-global-kill (article)
10233   "Edit the \"global\" kill file."
10234   (interactive (list (gnus-summary-article-number)))
10235   (gnus-group-edit-global-kill article))
10236
10237 (defun gnus-summary-edit-local-kill ()
10238   "Edit a local kill file applied to the current newsgroup."
10239   (interactive)
10240   (setq gnus-current-headers (gnus-summary-article-header))
10241   (gnus-group-edit-local-kill
10242    (gnus-summary-article-number) gnus-newsgroup-name))
10243
10244 ;;; Header reading.
10245
10246 (defun gnus-read-header (id &optional header)
10247   "Read the headers of article ID and enter them into the Gnus system."
10248   (let ((group gnus-newsgroup-name)
10249         (gnus-override-method
10250          (or
10251           gnus-override-method
10252           (and (gnus-news-group-p gnus-newsgroup-name)
10253                (car (gnus-refer-article-methods)))))
10254         where)
10255     ;; First we check to see whether the header in question is already
10256     ;; fetched.
10257     (if (stringp id)
10258         ;; This is a Message-ID.
10259         (setq header (or header (gnus-id-to-header id)))
10260       ;; This is an article number.
10261       (setq header (or header (gnus-summary-article-header id))))
10262     (if (and header
10263              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10264         ;; We have found the header.
10265         header
10266       ;; If this is a sparse article, we have to nix out its
10267       ;; previous entry in the thread hashtb.
10268       (when (and header
10269                  (gnus-summary-article-sparse-p (mail-header-number header)))
10270         (let* ((parent (gnus-parent-id (mail-header-references header)))
10271                (thread (and parent (gnus-id-to-thread parent))))
10272           (when thread
10273             (delq (assq header thread) thread))))
10274       ;; We have to really fetch the header to this article.
10275       (save-excursion
10276         (set-buffer nntp-server-buffer)
10277         (when (setq where (gnus-request-head id group))
10278           (nnheader-fold-continuation-lines)
10279           (goto-char (point-max))
10280           (insert ".\n")
10281           (goto-char (point-min))
10282           (insert "211 ")
10283           (princ (cond
10284                   ((numberp id) id)
10285                   ((cdr where) (cdr where))
10286                   (header (mail-header-number header))
10287                   (t gnus-reffed-article-number))
10288                  (current-buffer))
10289           (insert " Article retrieved.\n"))
10290         (if (or (not where)
10291                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10292             ()                          ; Malformed head.
10293           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10294             (when (and (stringp id)
10295                        (not (string= (gnus-group-real-name group)
10296                                      (car where))))
10297               ;; If we fetched by Message-ID and the article came
10298               ;; from a different group, we fudge some bogus article
10299               ;; numbers for this article.
10300               (mail-header-set-number header gnus-reffed-article-number))
10301             (save-excursion
10302               (set-buffer gnus-summary-buffer)
10303               (decf gnus-reffed-article-number)
10304               (gnus-remove-header (mail-header-number header))
10305               (push header gnus-newsgroup-headers)
10306               (setq gnus-current-headers header)
10307               (push (mail-header-number header) gnus-newsgroup-limit)))
10308           header)))))
10309
10310 (defun gnus-remove-header (number)
10311   "Remove header NUMBER from `gnus-newsgroup-headers'."
10312   (if (and gnus-newsgroup-headers
10313            (= number (mail-header-number (car gnus-newsgroup-headers))))
10314       (pop gnus-newsgroup-headers)
10315     (let ((headers gnus-newsgroup-headers))
10316       (while (and (cdr headers)
10317                   (not (= number (mail-header-number (cadr headers)))))
10318         (pop headers))
10319       (when (cdr headers)
10320         (setcdr headers (cddr headers))))))
10321
10322 ;;;
10323 ;;; summary highlights
10324 ;;;
10325
10326 (defun gnus-highlight-selected-summary ()
10327   "Highlight selected article in summary buffer."
10328   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10329   (when gnus-summary-selected-face
10330     (save-excursion
10331       (let* ((beg (progn (beginning-of-line) (point)))
10332              (end (progn (end-of-line) (point)))
10333              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10334              (from (if (get-text-property beg gnus-mouse-face-prop)
10335                        beg
10336                      (or (next-single-property-change
10337                           beg gnus-mouse-face-prop nil end)
10338                          beg)))
10339              (to
10340               (if (= from end)
10341                   (- from 2)
10342                 (or (next-single-property-change
10343                      from gnus-mouse-face-prop nil end)
10344                     end))))
10345         ;; If no mouse-face prop on line we will have to = from = end,
10346         ;; so we highlight the entire line instead.
10347         (when (= (+ to 2) from)
10348           (setq from beg)
10349           (setq to end))
10350         (if gnus-newsgroup-selected-overlay
10351             ;; Move old overlay.
10352             (gnus-move-overlay
10353              gnus-newsgroup-selected-overlay from to (current-buffer))
10354           ;; Create new overlay.
10355           (gnus-overlay-put
10356            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10357            'face gnus-summary-selected-face))))))
10358
10359 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10360 (defun gnus-summary-highlight-line ()
10361   "Highlight current line according to `gnus-summary-highlight'."
10362   (let* ((list gnus-summary-highlight)
10363          (p (point))
10364          (end (progn (end-of-line) (point)))
10365          ;; now find out where the line starts and leave point there.
10366          (beg (progn (beginning-of-line) (point)))
10367          (article (gnus-summary-article-number))
10368          (score (or (cdr (assq (or article gnus-current-article)
10369                                gnus-newsgroup-scored))
10370                     gnus-summary-default-score 0))
10371          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10372          (inhibit-read-only t))
10373     ;; Eval the cars of the lists until we find a match.
10374     (let ((default gnus-summary-default-score)
10375           (default-high gnus-summary-default-high-score)
10376           (default-low gnus-summary-default-low-score))
10377       (while (and list
10378                   (not (eval (caar list))))
10379         (setq list (cdr list))))
10380     (let ((face (cdar list)))
10381       (unless (eq face (get-text-property beg 'face))
10382         (gnus-put-text-property-excluding-characters-with-faces
10383          beg end 'face
10384          (setq face (if (boundp face) (symbol-value face) face)))
10385         (when gnus-summary-highlight-line-function
10386           (funcall gnus-summary-highlight-line-function article face))))
10387     (goto-char p)))
10388
10389 (defun gnus-update-read-articles (group unread &optional compute)
10390   "Update the list of read articles in GROUP."
10391   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10392          (entry (gnus-gethash group gnus-newsrc-hashtb))
10393          (info (nth 2 entry))
10394          (prev 1)
10395          (unread (sort (copy-sequence unread) '<))
10396          read)
10397     (if (or (not info) (not active))
10398         ;; There is no info on this group if it was, in fact,
10399         ;; killed.  Gnus stores no information on killed groups, so
10400         ;; there's nothing to be done.
10401         ;; One could store the information somewhere temporarily,
10402         ;; perhaps...  Hmmm...
10403         ()
10404       ;; Remove any negative articles numbers.
10405       (while (and unread (< (car unread) 0))
10406         (setq unread (cdr unread)))
10407       ;; Remove any expired article numbers
10408       (while (and unread (< (car unread) (car active)))
10409         (setq unread (cdr unread)))
10410       ;; Compute the ranges of read articles by looking at the list of
10411       ;; unread articles.
10412       (while unread
10413         (when (/= (car unread) prev)
10414           (push (if (= prev (1- (car unread))) prev
10415                   (cons prev (1- (car unread))))
10416                 read))
10417         (setq prev (1+ (car unread)))
10418         (setq unread (cdr unread)))
10419       (when (<= prev (cdr active))
10420         (push (cons prev (cdr active)) read))
10421       (setq read (if (> (length read) 1) (nreverse read) read))
10422       (if compute
10423           read
10424         (save-excursion
10425           (let (setmarkundo)
10426             ;; Propagate the read marks to the backend.
10427             (when (gnus-check-backend-function 'request-set-mark group)
10428               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10429                     (add (gnus-remove-from-range read (gnus-info-read info))))
10430                 (when (or add del)
10431                   (unless (gnus-check-group group)
10432                     (error "Can't open server for %s" group))
10433                   (gnus-request-set-mark
10434                    group (delq nil (list (if add (list add 'add '(read)))
10435                                          (if del (list del 'del '(read))))))
10436                   (setq setmarkundo
10437                         `(gnus-request-set-mark
10438                           ,group
10439                           ',(delq nil (list
10440                                        (if del (list del 'add '(read)))
10441                                        (if add (list add 'del '(read))))))))))
10442             (set-buffer gnus-group-buffer)
10443             (gnus-undo-register
10444               `(progn
10445                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10446                  (gnus-info-set-read ',info ',(gnus-info-read info))
10447                  (gnus-get-unread-articles-in-group ',info
10448                                                     (gnus-active ,group))
10449                  (gnus-group-update-group ,group t)
10450                  ,setmarkundo))))
10451         ;; Enter this list into the group info.
10452         (gnus-info-set-read info read)
10453         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10454         (gnus-get-unread-articles-in-group info (gnus-active group))
10455         t))))
10456
10457 (defun gnus-offer-save-summaries ()
10458   "Offer to save all active summary buffers."
10459   (let (buffers)
10460     ;; Go through all buffers and find all summaries.
10461     (dolist (buffer (buffer-list))
10462       (when (and (setq buffer (buffer-name buffer))
10463                  (string-match "Summary" buffer)
10464                  (save-excursion
10465                    (set-buffer buffer)
10466                    ;; We check that this is, indeed, a summary buffer.
10467                    (and (eq major-mode 'gnus-summary-mode)
10468                         ;; Also make sure this isn't bogus.
10469                         gnus-newsgroup-prepared
10470                         ;; Also make sure that this isn't a
10471                         ;; dead summary buffer.
10472                         (not gnus-dead-summary-mode))))
10473         (push buffer buffers)))
10474     ;; Go through all these summary buffers and offer to save them.
10475     (when buffers
10476       (save-excursion
10477         (map-y-or-n-p
10478          "Update summary buffer %s? "
10479          (lambda (buf)
10480            (switch-to-buffer buf)
10481            (gnus-summary-exit))
10482          buffers)))))
10483
10484
10485 ;;; @ for mime-partial
10486 ;;;
10487
10488 (defun gnus-request-partial-message ()
10489   (save-excursion
10490     (let ((number (gnus-summary-article-number))
10491           (group gnus-newsgroup-name)
10492           (mother gnus-article-buffer))
10493       (set-buffer (get-buffer-create " *Partial Article*"))
10494       (erase-buffer)
10495       (setq mime-preview-buffer mother)
10496       (gnus-request-article-this-buffer number group)
10497       (mime-parse-buffer)
10498       )))
10499
10500 (autoload 'mime-combine-message/partial-pieces-automatically
10501   "mime-partial"
10502   "Internal method to combine message/partial messages automatically.")
10503
10504 (mime-add-condition
10505  'action '((type . message)(subtype . partial)
10506            (major-mode . gnus-original-article-mode)
10507            (method . mime-combine-message/partial-pieces-automatically)
10508            (summary-buffer-exp . gnus-summary-buffer)
10509            (request-partial-message-method . gnus-request-partial-message)
10510            ))
10511
10512
10513 ;;; @ for message/rfc822
10514 ;;;
10515
10516 (defun gnus-mime-extract-message/rfc822 (entity situation)
10517   (let (group article num cwin swin cur)
10518     (with-temp-buffer
10519       (mime-insert-entity-content entity)
10520       (setq group (or (cdr (assq 'group situation))
10521                       (completing-read "Group: "
10522                                        gnus-active-hashtb
10523                                        nil
10524                                        (gnus-read-active-file-p)
10525                                        gnus-newsgroup-name))
10526             article (gnus-request-accept-article group)))
10527     (when (and (consp article)
10528                (numberp (setq article (cdr article))))
10529       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10530             cwin (get-buffer-window (current-buffer) t))
10531       (save-window-excursion
10532         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10533             (select-window swin)
10534           (set-buffer gnus-summary-buffer))
10535         (setq cur gnus-current-article)
10536         (forward-line num)
10537         (let (gnus-show-threads)
10538           (gnus-summary-goto-subject article t))
10539         (gnus-summary-clear-mark-forward 1)
10540         (gnus-summary-goto-subject cur))
10541       (when (and cwin (window-frame cwin))
10542         (select-frame (window-frame cwin)))
10543       (when (boundp 'mime-acting-situation-to-override)
10544         (set-alist 'mime-acting-situation-to-override
10545                    'group
10546                    group)
10547         (set-alist 'mime-acting-situation-to-override
10548                    'after-method
10549                    `(progn
10550                       (save-current-buffer
10551                         (set-buffer gnus-group-buffer)
10552                         (gnus-activate-group ,group))
10553                       (gnus-summary-goto-article ,cur
10554                                                  gnus-show-all-headers)))
10555         (set-alist 'mime-acting-situation-to-override
10556                    'number num)))))
10557
10558 (mime-add-condition
10559  'action '((type . message)(subtype . rfc822)
10560            (major-mode . gnus-original-article-mode)
10561            (method . gnus-mime-extract-message/rfc822)
10562            (mode . "extract")
10563            ))
10564
10565 (mime-add-condition
10566  'action '((type . message)(subtype . news)
10567            (major-mode . gnus-original-article-mode)
10568            (method . gnus-mime-extract-message/rfc822)
10569            (mode . "extract")
10570            ))
10571
10572 (defun gnus-mime-extract-multipart (entity situation)
10573   (let ((children (mime-entity-children entity))
10574         mime-acting-situation-to-override
10575         f)
10576     (while children
10577       (mime-play-entity (car children)
10578                         (cons (assq 'mode situation)
10579                               mime-acting-situation-to-override))
10580       (setq children (cdr children)))
10581     (if (setq f (cdr (assq 'after-method
10582                            mime-acting-situation-to-override)))
10583         (eval f)
10584       )))
10585
10586 (mime-add-condition
10587  'action '((type . multipart)
10588            (method . gnus-mime-extract-multipart)
10589            (mode . "extract")
10590            )
10591  'with-default)
10592
10593
10594 ;;; @ end
10595 ;;;
10596
10597 (defun gnus-summary-setup-default-charset ()
10598   "Setup newsgroup default charset."
10599   (if (equal gnus-newsgroup-name "nndraft:drafts")
10600       (setq gnus-newsgroup-charset nil)
10601     (let* ((ignored-charsets
10602             (or gnus-newsgroup-ephemeral-ignored-charsets
10603                 (append
10604                  (and gnus-newsgroup-name
10605                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10606                  gnus-newsgroup-ignored-charsets))))
10607       (setq gnus-newsgroup-charset
10608             (or gnus-newsgroup-ephemeral-charset
10609                 (and gnus-newsgroup-name
10610                      (gnus-parameter-charset gnus-newsgroup-name))
10611                 gnus-default-charset))
10612       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10613            ignored-charsets))))
10614
10615 ;;;
10616 ;;; Mime Commands
10617 ;;;
10618
10619 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10620   "Display the current article buffer fully MIME-buttonized.
10621 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10622 treated as multipart/mixed."
10623   (interactive "P")
10624   (require 'gnus-art)
10625   (let ((gnus-unbuttonized-mime-types nil)
10626         (gnus-mime-display-multipart-as-mixed show-all-parts))
10627     (gnus-summary-show-article)))
10628
10629 (defun gnus-summary-repair-multipart (article)
10630   "Add a Content-Type header to a multipart article without one."
10631   (interactive (list (gnus-summary-article-number)))
10632   (gnus-with-article article
10633     (message-narrow-to-head)
10634     (message-remove-header "Mime-Version")
10635     (goto-char (point-max))
10636     (insert "Mime-Version: 1.0\n")
10637     (widen)
10638     (when (search-forward "\n--" nil t)
10639       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10640         (message-narrow-to-head)
10641         (message-remove-header "Content-Type")
10642         (goto-char (point-max))
10643         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10644                         separator))
10645         (widen))))
10646   (let (gnus-mark-article-hook)
10647     (gnus-summary-select-article t t nil article)))
10648
10649 (defun gnus-summary-toggle-display-buttonized ()
10650   "Toggle the buttonizing of the article buffer."
10651   (interactive)
10652   (require 'gnus-art)
10653   (if (setq gnus-inhibit-mime-unbuttonizing
10654             (not gnus-inhibit-mime-unbuttonizing))
10655       (let ((gnus-unbuttonized-mime-types nil))
10656         (gnus-summary-show-article))
10657     (gnus-summary-show-article)))
10658
10659 ;;;
10660 ;;; Intelli-mouse commmands
10661 ;;;
10662
10663 (defun gnus-wheel-summary-scroll (event)
10664   (interactive "e")
10665   (let ((amount (if (memq 'shift (event-modifiers event))
10666                     (car gnus-wheel-scroll-amount)
10667                   (cdr gnus-wheel-scroll-amount)))
10668         (direction (- (* (static-if (featurep 'xemacs)
10669                              (event-button event)
10670                            (cond ((eq 'mouse-4 (event-basic-type event))
10671                                   4)
10672                                  ((eq 'mouse-5 (event-basic-type event))
10673                                   5)))
10674                          2) 9))
10675         edge)
10676     (gnus-summary-scroll-up (* amount direction))
10677     (when (gnus-eval-in-buffer-window gnus-article-buffer
10678             (save-restriction
10679               (widen)
10680               (and (if (< 0 direction)
10681                        (gnus-article-next-page 0)
10682                      (gnus-article-prev-page 0)
10683                      (bobp))
10684                    (if (setq edge (get-text-property
10685                                    (point-min) 'gnus-wheel-edge))
10686                        (setq edge (* edge direction))
10687                      (setq edge -1))
10688                    (or (plusp edge)
10689                        (let ((buffer-read-only nil)
10690                              (inhibit-read-only t))
10691                          (put-text-property (point-min) (point-max)
10692                                             'gnus-wheel-edge direction)
10693                          nil))
10694                    (or (> edge gnus-wheel-edge-resistance)
10695                        (let ((buffer-read-only nil)
10696                              (inhibit-read-only t))
10697                          (put-text-property (point-min) (point-max)
10698                                             'gnus-wheel-edge
10699                                             (* (1+ edge) direction))
10700                          nil))
10701                    (eq last-command 'gnus-wheel-summary-scroll))))
10702       (gnus-summary-next-article nil nil (minusp direction)))))
10703
10704 (defun gnus-wheel-install ()
10705   "Enable mouse wheel support on summary window."
10706   (when gnus-use-wheel
10707     (let ((keys
10708            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10709       (dolist (key keys)
10710         (define-key gnus-summary-mode-map key
10711           'gnus-wheel-summary-scroll)))))
10712
10713 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10714
10715 ;;;
10716 ;;; Traditional PGP commmands
10717 ;;;
10718
10719 (defun gnus-summary-decrypt-article (&optional force)
10720   "Decrypt the current article in traditional PGP way.
10721 This will have permanent effect only in mail groups.
10722 If FORCE is non-nil, allow editing of articles even in read-only
10723 groups."
10724   (interactive "P")
10725   (gnus-summary-select-article t)
10726   (gnus-eval-in-buffer-window gnus-article-buffer
10727     (save-excursion
10728       (save-restriction
10729         (widen)
10730         (goto-char (point-min))
10731         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10732           (error "Not a traditional PGP message!"))
10733         (let ((armor-start (match-beginning 0)))
10734           (if (and (pgg-decrypt-region armor-start (point-max))
10735                    (or force (not (gnus-group-read-only-p))))
10736               (let ((inhibit-read-only t)
10737                     buffer-read-only)
10738                 (delete-region armor-start
10739                                (progn
10740                                  (re-search-forward "^-+END PGP" nil t)
10741                                  (beginning-of-line 2)
10742                                  (point)))
10743                 (insert-buffer-substring pgg-output-buffer))))))))
10744
10745 (defun gnus-summary-verify-article ()
10746   "Verify the current article in traditional PGP way."
10747   (interactive)
10748   (save-excursion
10749     (set-buffer gnus-original-article-buffer)
10750     (goto-char (point-min))
10751     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10752       (error "Not a traditional PGP message!"))
10753     (re-search-forward "^-+END PGP" nil t)
10754     (beginning-of-line 2)
10755     (call-interactively (function pgg-verify-region))))
10756
10757 ;;;
10758 ;;; Generic summary marking commands
10759 ;;;
10760
10761 (defvar gnus-summary-marking-alist
10762   '((read gnus-del-mark "d")
10763     (unread gnus-unread-mark "u")
10764     (ticked gnus-ticked-mark "!")
10765     (dormant gnus-dormant-mark "?")
10766     (expirable gnus-expirable-mark "e"))
10767   "An alist of names/marks/keystrokes.")
10768
10769 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10770 (defvar gnus-summary-mark-map)
10771
10772 (defun gnus-summary-make-all-marking-commands ()
10773   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10774   (dolist (elem gnus-summary-marking-alist)
10775     (apply 'gnus-summary-make-marking-command elem)))
10776
10777 (defun gnus-summary-make-marking-command (name mark keystroke)
10778   (let ((map (make-sparse-keymap)))
10779     (define-key gnus-summary-generic-mark-map keystroke map)
10780     (dolist (lway `((next "next" next nil "n")
10781                     (next-unread "next unread" next t "N")
10782                     (prev "previous" prev nil "p")
10783                     (prev-unread "previous unread" prev t "P")
10784                     (nomove "" nil nil ,keystroke)))
10785       (let ((func (gnus-summary-make-marking-command-1
10786                    mark (car lway) lway name)))
10787         (setq func (eval func))
10788         (define-key map (nth 4 lway) func)))))
10789
10790 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10791   `(defun ,(intern
10792             (format "gnus-summary-put-mark-as-%s%s"
10793                     name (if (eq way 'nomove)
10794                              ""
10795                            (concat "-" (symbol-name way)))))
10796      (n)
10797      ,(format
10798        "Mark the current article as %s%s.
10799 If N, the prefix, then repeat N times.
10800 If N is negative, move in reverse order.
10801 The difference between N and the actual number of articles marked is
10802 returned."
10803        name (car (cdr lway)))
10804      (interactive "p")
10805      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10806
10807 (defun gnus-summary-generic-mark (n mark move unread)
10808   "Mark N articles with MARK."
10809   (unless (eq major-mode 'gnus-summary-mode)
10810     (error "This command can only be used in the summary buffer"))
10811   (gnus-summary-show-thread)
10812   (let ((nummove
10813          (cond
10814           ((eq move 'next) 1)
10815           ((eq move 'prev) -1)
10816           (t 0))))
10817     (if (zerop nummove)
10818         (setq n 1)
10819       (when (< n 0)
10820         (setq n (abs n)
10821               nummove (* -1 nummove))))
10822     (while (and (> n 0)
10823                 (gnus-summary-mark-article nil mark)
10824                 (zerop (gnus-summary-next-subject nummove unread t)))
10825       (setq n (1- n)))
10826     (when (/= 0 n)
10827       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10828     (gnus-summary-recenter)
10829     (gnus-summary-position-point)
10830     (gnus-set-mode-line 'summary)
10831     n))
10832
10833 (defun gnus-summary-insert-articles (articles)
10834   (when (setq articles
10835               (gnus-set-difference articles
10836                                    (mapcar (lambda (h) (mail-header-number h))
10837                                            gnus-newsgroup-headers)))
10838     (setq gnus-newsgroup-headers
10839           (merge 'list
10840                  gnus-newsgroup-headers
10841                  (gnus-fetch-headers articles)
10842                  'gnus-article-sort-by-number))
10843     ;; Suppress duplicates?
10844     (when gnus-suppress-duplicates
10845       (gnus-dup-suppress-articles))
10846
10847     ;; We might want to build some more threads first.
10848     (when (and gnus-fetch-old-headers
10849                (eq gnus-headers-retrieved-by 'nov))
10850       (if (eq gnus-fetch-old-headers 'invisible)
10851           (gnus-build-all-threads)
10852         (gnus-build-old-threads)))
10853     ;; Let the Gnus agent mark articles as read.
10854     (when gnus-agent
10855       (gnus-agent-get-undownloaded-list))
10856     ;; Remove list identifiers from subject
10857     (when gnus-list-identifiers
10858       (gnus-summary-remove-list-identifiers))
10859     ;; First and last article in this newsgroup.
10860     (when gnus-newsgroup-headers
10861       (setq gnus-newsgroup-begin
10862             (mail-header-number (car gnus-newsgroup-headers))
10863             gnus-newsgroup-end
10864             (mail-header-number
10865              (gnus-last-element gnus-newsgroup-headers))))
10866     (when gnus-use-scoring
10867       (gnus-possibly-score-headers))))
10868
10869 (defun gnus-summary-insert-old-articles (&optional all)
10870   "Insert all old articles in this group.
10871 If ALL is non-nil, already read articles become readable.
10872 If ALL is a number, fetch this number of articles."
10873   (interactive "P")
10874   (prog1
10875       (let ((old (mapcar 'car gnus-newsgroup-data))
10876             (i (car gnus-newsgroup-active))
10877             older len)
10878         (while (<= i (cdr gnus-newsgroup-active))
10879           (or (memq i old) (push i older))
10880           (incf i))
10881         (setq len (length older))
10882         (cond
10883          ((null older) nil)
10884          ((numberp all)
10885           (if (< all len)
10886               (setq older (subseq older 0 all))))
10887          (all nil)
10888          (t
10889           (if (and (numberp gnus-large-newsgroup)
10890                    (> len gnus-large-newsgroup))
10891               (let ((input
10892                      (read-string
10893                       (format
10894                        "How many articles from %s (default %d): "
10895                        (gnus-limit-string
10896                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10897                        len))))
10898                 (unless (string-match "^[ \t]*$" input)
10899                   (setq all (string-to-number input))
10900                   (if (< all len)
10901                       (setq older (subseq older 0 all))))))))
10902         (if (not older)
10903             (message "No old news.")
10904           (gnus-summary-insert-articles older)
10905           (gnus-summary-limit (gnus-union older old))))
10906     (gnus-summary-position-point)))
10907
10908 (defun gnus-summary-insert-new-articles ()
10909   "Insert all new articles in this group."
10910   (interactive)
10911   (prog1
10912       (let ((old (mapcar 'car gnus-newsgroup-data))
10913             (old-active gnus-newsgroup-active)
10914             (nnmail-fetched-sources (list t))
10915             i new)
10916         (setq gnus-newsgroup-active
10917               (gnus-activate-group gnus-newsgroup-name 'scan))
10918         (setq i (1+ (cdr old-active)))
10919         (while (<= i (cdr gnus-newsgroup-active))
10920           (push i new)
10921           (incf i))
10922         (if (not new)
10923             (message "No gnus is bad news.")
10924           (setq new (nreverse new))
10925           (gnus-summary-insert-articles new)
10926           (setq gnus-newsgroup-unreads
10927                 (append gnus-newsgroup-unreads new))
10928           (gnus-summary-limit (gnus-union old new))))
10929     (gnus-summary-position-point)))
10930
10931 (gnus-summary-make-all-marking-commands)
10932
10933 (gnus-ems-redefine)
10934
10935 (provide 'gnus-sum)
10936
10937 (run-hooks 'gnus-sum-load-hook)
10938
10939 ;;; gnus-sum.el ends here