Synch with Oort Gnus.
[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 gnus-pick-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 gnus-tmp-from)
1124         ?s)
1125     (?t (gnus-summary-number-of-articles-in-thread
1126          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1127         ?d)
1128     (?e (gnus-summary-number-of-articles-in-thread
1129          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1130         ?c)
1131     (?u gnus-tmp-user-defined ?s)
1132     (?P (gnus-pick-line-number) ?d)
1133     (?B gnus-tmp-thread-tree-header-string ?s)
1134     (user-date (gnus-user-date
1135                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1136   "An alist of format specifications that can appear in summary lines.
1137 These are paired with what variables they correspond with, along with
1138 the type of the variable (string, integer, character, etc).")
1139
1140 (defvar gnus-summary-dummy-line-format-alist
1141   `((?S gnus-tmp-subject ?s)
1142     (?N gnus-tmp-number ?d)
1143     (?u gnus-tmp-user-defined ?s)))
1144
1145 (defvar gnus-summary-mode-line-format-alist
1146   `((?G gnus-tmp-group-name ?s)
1147     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1148     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1149     (?A gnus-tmp-article-number ?d)
1150     (?Z gnus-tmp-unread-and-unselected ?s)
1151     (?V gnus-version ?s)
1152     (?U gnus-tmp-unread-and-unticked ?d)
1153     (?S gnus-tmp-subject ?s)
1154     (?e gnus-tmp-unselected ?d)
1155     (?u gnus-tmp-user-defined ?s)
1156     (?d (length gnus-newsgroup-dormant) ?d)
1157     (?t (length gnus-newsgroup-marked) ?d)
1158     (?r (length gnus-newsgroup-reads) ?d)
1159     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1160     (?E gnus-newsgroup-expunged-tally ?d)
1161     (?s (gnus-current-score-file-nondirectory) ?s)))
1162
1163 (defvar gnus-last-search-regexp nil
1164   "Default regexp for article search command.")
1165
1166 (defvar gnus-summary-search-article-matched-data nil
1167   "Last matched data of article search command.  It is the local variable
1168 in `gnus-article-buffer' which consists of the list of start position,
1169 end position and text.")
1170
1171 (defvar gnus-last-shell-command nil
1172   "Default shell command on article.")
1173
1174 (defvar gnus-newsgroup-begin nil)
1175 (defvar gnus-newsgroup-end nil)
1176 (defvar gnus-newsgroup-last-rmail nil)
1177 (defvar gnus-newsgroup-last-mail nil)
1178 (defvar gnus-newsgroup-last-folder nil)
1179 (defvar gnus-newsgroup-last-file nil)
1180 (defvar gnus-newsgroup-auto-expire nil)
1181 (defvar gnus-newsgroup-active nil)
1182
1183 (defvar gnus-newsgroup-data nil)
1184 (defvar gnus-newsgroup-data-reverse nil)
1185 (defvar gnus-newsgroup-limit nil)
1186 (defvar gnus-newsgroup-limits nil)
1187
1188 (defvar gnus-newsgroup-unreads nil
1189   "List of unread articles in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-unselected nil
1192   "List of unselected unread articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-reads nil
1195   "Alist of read articles and article marks in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-expunged-tally nil)
1198
1199 (defvar gnus-newsgroup-marked nil
1200   "List of ticked articles in the current newsgroup (a subset of unread art).")
1201
1202 (defvar gnus-newsgroup-killed nil
1203   "List of ranges of articles that have been through the scoring process.")
1204
1205 (defvar gnus-newsgroup-cached nil
1206   "List of articles that come from the article cache.")
1207
1208 (defvar gnus-newsgroup-saved nil
1209   "List of articles that have been saved.")
1210
1211 (defvar gnus-newsgroup-kill-headers nil)
1212
1213 (defvar gnus-newsgroup-replied nil
1214   "List of articles that have been replied to in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-forwarded nil
1217   "List of articles that have been forwarded in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-recent nil
1220   "List of articles that have are recent in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-expirable nil
1223   "List of articles in the current newsgroup that can be expired.")
1224
1225 (defvar gnus-newsgroup-processable nil
1226   "List of articles in the current newsgroup that can be processed.")
1227
1228 (defvar gnus-newsgroup-downloadable nil
1229   "List of articles in the current newsgroup that can be processed.")
1230
1231 (defvar gnus-newsgroup-undownloaded nil
1232   "List of articles in the current newsgroup that haven't been downloaded..")
1233
1234 (defvar gnus-newsgroup-unsendable nil
1235   "List of articles in the current newsgroup that won't be sent.")
1236
1237 (defvar gnus-newsgroup-bookmarks nil
1238   "List of articles in the current newsgroup that have bookmarks.")
1239
1240 (defvar gnus-newsgroup-dormant nil
1241   "List of dormant articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-unseen nil
1244   "List of unseen articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-seen nil
1247   "Range of seen articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-articles nil
1250   "List of articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-scored nil
1253   "List of scored articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-incorporated nil
1256   "List of incorporated articles in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-headers nil
1259   "List of article headers in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-threads nil)
1262
1263 (defvar gnus-newsgroup-prepared nil
1264   "Whether the current group has been prepared properly.")
1265
1266 (defvar gnus-newsgroup-ancient nil
1267   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1268
1269 (defvar gnus-newsgroup-sparse nil)
1270
1271 (defvar gnus-current-article nil)
1272 (defvar gnus-article-current nil)
1273 (defvar gnus-current-headers nil)
1274 (defvar gnus-have-all-headers nil)
1275 (defvar gnus-last-article nil)
1276 (defvar gnus-newsgroup-history nil)
1277 (defvar gnus-newsgroup-charset nil)
1278 (defvar gnus-newsgroup-ephemeral-charset nil)
1279 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1280
1281 (defvar gnus-article-before-search nil)
1282
1283 (defconst gnus-summary-local-variables
1284   '(gnus-newsgroup-name
1285     gnus-newsgroup-begin gnus-newsgroup-end
1286     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1287     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1288     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1289     gnus-newsgroup-unselected gnus-newsgroup-marked
1290     gnus-newsgroup-reads gnus-newsgroup-saved
1291     gnus-newsgroup-replied gnus-newsgroup-forwarded
1292     gnus-newsgroup-recent
1293     gnus-newsgroup-expirable
1294     gnus-newsgroup-processable gnus-newsgroup-killed
1295     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1296     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1297     gnus-newsgroup-seen gnus-newsgroup-articles
1298     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1299     gnus-newsgroup-headers gnus-newsgroup-threads
1300     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1301     gnus-current-article gnus-current-headers gnus-have-all-headers
1302     gnus-last-article gnus-article-internal-prepare-hook
1303     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1304     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1305     gnus-thread-expunge-below
1306     gnus-score-alist gnus-current-score-file
1307     (gnus-summary-expunge-below . global)
1308     (gnus-summary-mark-below . global)
1309     (gnus-orphan-score . global)
1310     gnus-newsgroup-active gnus-scores-exclude-files
1311     gnus-newsgroup-history gnus-newsgroup-ancient
1312     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1313     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1314     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1315     (gnus-newsgroup-expunged-tally . 0)
1316     gnus-cache-removable-articles gnus-newsgroup-cached
1317     gnus-newsgroup-data gnus-newsgroup-data-reverse
1318     gnus-newsgroup-limit gnus-newsgroup-limits
1319     gnus-newsgroup-charset gnus-newsgroup-display
1320     gnus-newsgroup-incorporated)
1321   "Variables that are buffer-local to the summary buffers.")
1322
1323 (defvar gnus-newsgroup-variables nil
1324   "A list of variables that have separate values in different newsgroups.
1325 A list of newsgroup (summary buffer) local variables, or cons of
1326 variables and their default values (when the default values are not
1327 nil), that should be made global while the summary buffer is active.
1328 These variables can be used to set variables in the group parameters
1329 while still allowing them to affect operations done in other
1330 buffers. For example:
1331
1332 (setq gnus-newsgroup-variables
1333      '(message-use-followup-to
1334        (gnus-visible-headers .
1335          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1336 ")
1337
1338 ;; Byte-compiler warning.
1339 (eval-when-compile (defvar gnus-article-mode-map))
1340
1341 ;; Subject simplification.
1342
1343 (defun gnus-simplify-whitespace (str)
1344   "Remove excessive whitespace from STR."
1345   (let ((mystr str))
1346     ;; Multiple spaces.
1347     (while (string-match "[ \t][ \t]+" mystr)
1348       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1349                           " "
1350                           (substring mystr (match-end 0)))))
1351     ;; Leading spaces.
1352     (when (string-match "^[ \t]+" mystr)
1353       (setq mystr (substring mystr (match-end 0))))
1354     ;; Trailing spaces.
1355     (when (string-match "[ \t]+$" mystr)
1356       (setq mystr (substring mystr 0 (match-beginning 0))))
1357     mystr))
1358
1359 (defsubst gnus-simplify-subject-re (subject)
1360   "Remove \"Re:\" from subject lines."
1361   (if (string-match message-subject-re-regexp subject)
1362       (substring subject (match-end 0))
1363     subject))
1364
1365 (defun gnus-simplify-subject (subject &optional re-only)
1366   "Remove `Re:' and words in parentheses.
1367 If RE-ONLY is non-nil, strip leading `Re:'s only."
1368   (let ((case-fold-search t))           ;Ignore case.
1369     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1370     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1371       (setq subject (substring subject (match-end 0))))
1372     ;; Remove uninteresting prefixes.
1373     (when (and (not re-only)
1374                gnus-simplify-ignored-prefixes
1375                (string-match gnus-simplify-ignored-prefixes subject))
1376       (setq subject (substring subject (match-end 0))))
1377     ;; Remove words in parentheses from end.
1378     (unless re-only
1379       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1380         (setq subject (substring subject 0 (match-beginning 0)))))
1381     ;; Return subject string.
1382     subject))
1383
1384 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1385 ;; all whitespace.
1386 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1387   (goto-char (point-min))
1388   (while (re-search-forward regexp nil t)
1389     (replace-match (or newtext ""))))
1390
1391 (defun gnus-simplify-buffer-fuzzy ()
1392   "Simplify string in the buffer fuzzily.
1393 The string in the accessible portion of the current buffer is simplified.
1394 It is assumed to be a single-line subject.
1395 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1396 matter is removed.  Additional things can be deleted by setting
1397 `gnus-simplify-subject-fuzzy-regexp'."
1398   (let ((case-fold-search t)
1399         (modified-tick))
1400     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1401
1402     (while (not (eq modified-tick (buffer-modified-tick)))
1403       (setq modified-tick (buffer-modified-tick))
1404       (cond
1405        ((listp gnus-simplify-subject-fuzzy-regexp)
1406         (mapcar 'gnus-simplify-buffer-fuzzy-step
1407                 gnus-simplify-subject-fuzzy-regexp))
1408        (gnus-simplify-subject-fuzzy-regexp
1409         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1410       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1411       (gnus-simplify-buffer-fuzzy-step
1412        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1413       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1414
1415     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1416     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1417     (gnus-simplify-buffer-fuzzy-step " $")
1418     (gnus-simplify-buffer-fuzzy-step "^ +")))
1419
1420 (defun gnus-simplify-subject-fuzzy (subject)
1421   "Simplify a subject string fuzzily.
1422 See `gnus-simplify-buffer-fuzzy' for details."
1423   (save-excursion
1424     (gnus-set-work-buffer)
1425     (let ((case-fold-search t))
1426       ;; Remove uninteresting prefixes.
1427       (when (and gnus-simplify-ignored-prefixes
1428                  (string-match gnus-simplify-ignored-prefixes subject))
1429         (setq subject (substring subject (match-end 0))))
1430       (insert subject)
1431       (inline (gnus-simplify-buffer-fuzzy))
1432       (buffer-string))))
1433
1434 (defsubst gnus-simplify-subject-fully (subject)
1435   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1436   (cond
1437    (gnus-simplify-subject-functions
1438     (gnus-map-function gnus-simplify-subject-functions subject))
1439    ((null gnus-summary-gather-subject-limit)
1440     (gnus-simplify-subject-re subject))
1441    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1442     (gnus-simplify-subject-fuzzy subject))
1443    ((numberp gnus-summary-gather-subject-limit)
1444     (gnus-limit-string (gnus-simplify-subject-re subject)
1445                        gnus-summary-gather-subject-limit))
1446    (t
1447     subject)))
1448
1449 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1450   "Check whether two subjects are equal.
1451 If optional argument simple-first is t, first argument is already
1452 simplified."
1453   (cond
1454    ((null simple-first)
1455     (equal (gnus-simplify-subject-fully s1)
1456            (gnus-simplify-subject-fully s2)))
1457    (t
1458     (equal s1
1459            (gnus-simplify-subject-fully s2)))))
1460
1461 (defun gnus-summary-bubble-group ()
1462   "Increase the score of the current group.
1463 This is a handy function to add to `gnus-summary-exit-hook' to
1464 increase the score of each group you read."
1465   (gnus-group-add-score gnus-newsgroup-name))
1466
1467 \f
1468 ;;;
1469 ;;; Gnus summary mode
1470 ;;;
1471
1472 (put 'gnus-summary-mode 'mode-class 'special)
1473
1474 (defvar gnus-article-commands-menu)
1475
1476 (when t
1477   ;; Non-orthogonal keys
1478
1479   (gnus-define-keys gnus-summary-mode-map
1480     " " gnus-summary-next-page
1481     "\177" gnus-summary-prev-page
1482     [delete] gnus-summary-prev-page
1483     [backspace] gnus-summary-prev-page
1484     "\r" gnus-summary-scroll-up
1485     "\M-\r" gnus-summary-scroll-down
1486     "n" gnus-summary-next-unread-article
1487     "p" gnus-summary-prev-unread-article
1488     "N" gnus-summary-next-article
1489     "P" gnus-summary-prev-article
1490     "\M-\C-n" gnus-summary-next-same-subject
1491     "\M-\C-p" gnus-summary-prev-same-subject
1492     "\M-n" gnus-summary-next-unread-subject
1493     "\M-p" gnus-summary-prev-unread-subject
1494     "." gnus-summary-first-unread-article
1495     "," gnus-summary-best-unread-article
1496     "\M-s" gnus-summary-search-article-forward
1497     "\M-r" gnus-summary-search-article-backward
1498     "<" gnus-summary-beginning-of-article
1499     ">" gnus-summary-end-of-article
1500     "j" gnus-summary-goto-article
1501     "^" gnus-summary-refer-parent-article
1502     "\M-^" gnus-summary-refer-article
1503     "u" gnus-summary-tick-article-forward
1504     "!" gnus-summary-tick-article-forward
1505     "U" gnus-summary-tick-article-backward
1506     "d" gnus-summary-mark-as-read-forward
1507     "D" gnus-summary-mark-as-read-backward
1508     "E" gnus-summary-mark-as-expirable
1509     "\M-u" gnus-summary-clear-mark-forward
1510     "\M-U" gnus-summary-clear-mark-backward
1511     "k" gnus-summary-kill-same-subject-and-select
1512     "\C-k" gnus-summary-kill-same-subject
1513     "\M-\C-k" gnus-summary-kill-thread
1514     "\M-\C-l" gnus-summary-lower-thread
1515     "e" gnus-summary-edit-article
1516     "#" gnus-summary-mark-as-processable
1517     "\M-#" gnus-summary-unmark-as-processable
1518     "\M-\C-t" gnus-summary-toggle-threads
1519     "\M-\C-s" gnus-summary-show-thread
1520     "\M-\C-h" gnus-summary-hide-thread
1521     "\M-\C-f" gnus-summary-next-thread
1522     "\M-\C-b" gnus-summary-prev-thread
1523     [(meta down)] gnus-summary-next-thread
1524     [(meta up)] gnus-summary-prev-thread
1525     "\M-\C-u" gnus-summary-up-thread
1526     "\M-\C-d" gnus-summary-down-thread
1527     "&" gnus-summary-execute-command
1528     "c" gnus-summary-catchup-and-exit
1529     "\C-w" gnus-summary-mark-region-as-read
1530     "\C-t" gnus-summary-toggle-truncation
1531     "?" gnus-summary-mark-as-dormant
1532     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1533     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1534     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1535     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1536     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1537     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1538     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1539     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1540     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1541     "=" gnus-summary-expand-window
1542     "\C-x\C-s" gnus-summary-reselect-current-group
1543     "\M-g" gnus-summary-rescan-group
1544     "w" gnus-summary-stop-page-breaking
1545     "\C-c\C-r" gnus-summary-caesar-message
1546     "\M-t" gnus-summary-toggle-mime
1547     "f" gnus-summary-followup
1548     "F" gnus-summary-followup-with-original
1549     "C" gnus-summary-cancel-article
1550     "r" gnus-summary-reply
1551     "R" gnus-summary-reply-with-original
1552     "\C-c\C-f" gnus-summary-mail-forward
1553     "o" gnus-summary-save-article
1554     "\C-o" gnus-summary-save-article-mail
1555     "|" gnus-summary-pipe-output
1556     "\M-k" gnus-summary-edit-local-kill
1557     "\M-K" gnus-summary-edit-global-kill
1558     ;; "V" gnus-version
1559     "\C-c\C-d" gnus-summary-describe-group
1560     "q" gnus-summary-exit
1561     "Q" gnus-summary-exit-no-update
1562     "\C-c\C-i" gnus-info-find-node
1563     gnus-mouse-2 gnus-mouse-pick-article
1564     "m" gnus-summary-mail-other-window
1565     "a" gnus-summary-post-news
1566     "x" gnus-summary-limit-to-unread
1567     "s" gnus-summary-isearch-article
1568     "t" gnus-article-toggle-headers
1569     "g" gnus-summary-show-article
1570     "l" gnus-summary-goto-last-article
1571     "v" gnus-summary-preview-mime-message
1572     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1573     "\C-d" gnus-summary-enter-digest-group
1574     "\M-\C-d" gnus-summary-read-document
1575     "\M-\C-e" gnus-summary-edit-parameters
1576     "\M-\C-a" gnus-summary-customize-parameters
1577     "\C-c\C-b" gnus-bug
1578     "\C-c\C-n" gnus-namazu-search
1579     "*" gnus-cache-enter-article
1580     "\M-*" gnus-cache-remove-article
1581     "\M-&" gnus-summary-universal-argument
1582     "\C-l" gnus-recenter
1583     "I" gnus-summary-increase-score
1584     "L" gnus-summary-lower-score
1585     "\M-i" gnus-symbolic-argument
1586     "h" gnus-summary-select-article-buffer
1587
1588     "V" gnus-summary-score-map
1589     "X" gnus-uu-extract-map
1590     "S" gnus-summary-send-map)
1591
1592   ;; Sort of orthogonal keymap
1593   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1594     "t" gnus-summary-tick-article-forward
1595     "!" gnus-summary-tick-article-forward
1596     "d" gnus-summary-mark-as-read-forward
1597     "r" gnus-summary-mark-as-read-forward
1598     "c" gnus-summary-clear-mark-forward
1599     " " gnus-summary-clear-mark-forward
1600     "e" gnus-summary-mark-as-expirable
1601     "x" gnus-summary-mark-as-expirable
1602     "?" gnus-summary-mark-as-dormant
1603     "b" gnus-summary-set-bookmark
1604     "B" gnus-summary-remove-bookmark
1605     "#" gnus-summary-mark-as-processable
1606     "\M-#" gnus-summary-unmark-as-processable
1607     "S" gnus-summary-limit-include-expunged
1608     "C" gnus-summary-catchup
1609     "H" gnus-summary-catchup-to-here
1610     "h" gnus-summary-catchup-from-here
1611     "\C-c" gnus-summary-catchup-all
1612     "k" gnus-summary-kill-same-subject-and-select
1613     "K" gnus-summary-kill-same-subject
1614     "P" gnus-uu-mark-map)
1615
1616   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1617     "c" gnus-summary-clear-above
1618     "u" gnus-summary-tick-above
1619     "m" gnus-summary-mark-above
1620     "k" gnus-summary-kill-below)
1621
1622   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1623     "/" gnus-summary-limit-to-subject
1624     "n" gnus-summary-limit-to-articles
1625     "w" gnus-summary-pop-limit
1626     "s" gnus-summary-limit-to-subject
1627     "a" gnus-summary-limit-to-author
1628     "u" gnus-summary-limit-to-unread
1629     "m" gnus-summary-limit-to-marks
1630     "M" gnus-summary-limit-exclude-marks
1631     "v" gnus-summary-limit-to-score
1632     "*" gnus-summary-limit-include-cached
1633     "D" gnus-summary-limit-include-dormant
1634     "T" gnus-summary-limit-include-thread
1635     "d" gnus-summary-limit-exclude-dormant
1636     "t" gnus-summary-limit-to-age
1637     "x" gnus-summary-limit-to-extra
1638     "p" gnus-summary-limit-to-display-predicate
1639     "E" gnus-summary-limit-include-expunged
1640     "c" gnus-summary-limit-exclude-childless-dormant
1641     "C" gnus-summary-limit-mark-excluded-as-read
1642     "o" gnus-summary-insert-old-articles
1643     "N" gnus-summary-insert-new-articles)
1644
1645   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1646     "n" gnus-summary-next-unread-article
1647     "p" gnus-summary-prev-unread-article
1648     "N" gnus-summary-next-article
1649     "P" gnus-summary-prev-article
1650     "\C-n" gnus-summary-next-same-subject
1651     "\C-p" gnus-summary-prev-same-subject
1652     "\M-n" gnus-summary-next-unread-subject
1653     "\M-p" gnus-summary-prev-unread-subject
1654     "f" gnus-summary-first-unread-article
1655     "b" gnus-summary-best-unread-article
1656     "j" gnus-summary-goto-article
1657     "g" gnus-summary-goto-subject
1658     "l" gnus-summary-goto-last-article
1659     "o" gnus-summary-pop-article)
1660
1661   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1662     "k" gnus-summary-kill-thread
1663     "l" gnus-summary-lower-thread
1664     "i" gnus-summary-raise-thread
1665     "T" gnus-summary-toggle-threads
1666     "t" gnus-summary-rethread-current
1667     "^" gnus-summary-reparent-thread
1668     "s" gnus-summary-show-thread
1669     "S" gnus-summary-show-all-threads
1670     "h" gnus-summary-hide-thread
1671     "H" gnus-summary-hide-all-threads
1672     "n" gnus-summary-next-thread
1673     "p" gnus-summary-prev-thread
1674     "u" gnus-summary-up-thread
1675     "o" gnus-summary-top-thread
1676     "d" gnus-summary-down-thread
1677     "#" gnus-uu-mark-thread
1678     "\M-#" gnus-uu-unmark-thread)
1679
1680   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1681     "g" gnus-summary-prepare
1682     "c" gnus-summary-insert-cached-articles)
1683
1684   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1685     "c" gnus-summary-catchup-and-exit
1686     "C" gnus-summary-catchup-all-and-exit
1687     "E" gnus-summary-exit-no-update
1688     "J" gnus-summary-jump-to-other-group
1689     "Q" gnus-summary-exit
1690     "Z" gnus-summary-exit
1691     "n" gnus-summary-catchup-and-goto-next-group
1692     "R" gnus-summary-reselect-current-group
1693     "G" gnus-summary-rescan-group
1694     "N" gnus-summary-next-group
1695     "s" gnus-summary-save-newsrc
1696     "P" gnus-summary-prev-group)
1697
1698   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1699     " " gnus-summary-next-page
1700     "n" gnus-summary-next-page
1701     "\177" gnus-summary-prev-page
1702     [delete] gnus-summary-prev-page
1703     "p" gnus-summary-prev-page
1704     "\r" gnus-summary-scroll-up
1705     "\M-\r" gnus-summary-scroll-down
1706     "<" gnus-summary-beginning-of-article
1707     ">" gnus-summary-end-of-article
1708     "b" gnus-summary-beginning-of-article
1709     "e" gnus-summary-end-of-article
1710     "^" gnus-summary-refer-parent-article
1711     "r" gnus-summary-refer-parent-article
1712     "D" gnus-summary-enter-digest-group
1713     "R" gnus-summary-refer-references
1714     "T" gnus-summary-refer-thread
1715     "g" gnus-summary-show-article
1716     "s" gnus-summary-isearch-article
1717     "P" gnus-summary-print-article
1718     "M" gnus-mailing-list-insinuate
1719     "t" gnus-article-babel)
1720
1721   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1722     "b" gnus-article-add-buttons
1723     "B" gnus-article-add-buttons-to-head
1724     "o" gnus-article-treat-overstrike
1725     "e" gnus-article-emphasize
1726     "w" gnus-article-fill-cited-article
1727     "Q" gnus-article-fill-long-lines
1728     "C" gnus-article-capitalize-sentences
1729     "c" gnus-article-remove-cr
1730     "Z" gnus-article-decode-HZ
1731     "f" gnus-article-display-x-face
1732     "l" gnus-summary-stop-page-breaking
1733     "r" gnus-summary-caesar-message
1734     "t" gnus-article-toggle-headers
1735     "g" gnus-summary-toggle-smiley
1736     "v" gnus-summary-verbose-headers
1737     "m" gnus-summary-toggle-mime
1738     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1739     "p" gnus-article-verify-x-pgp-sig
1740     "d" gnus-article-treat-dumbquotes)
1741
1742   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1743     "a" gnus-article-hide
1744     "h" gnus-article-toggle-headers
1745     "b" gnus-article-hide-boring-headers
1746     "s" gnus-article-hide-signature
1747     "c" gnus-article-hide-citation
1748     "C" gnus-article-hide-citation-in-followups
1749     "l" gnus-article-hide-list-identifiers
1750     "p" gnus-article-hide-pgp
1751     "B" gnus-article-strip-banner
1752     "P" gnus-article-hide-pem
1753     "\C-c" gnus-article-hide-citation-maybe)
1754
1755   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1756     "a" gnus-article-highlight
1757     "h" gnus-article-highlight-headers
1758     "c" gnus-article-highlight-citation
1759     "s" gnus-article-highlight-signature)
1760
1761   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1762     "z" gnus-article-date-ut
1763     "u" gnus-article-date-ut
1764     "l" gnus-article-date-local
1765     "p" gnus-article-date-english
1766     "e" gnus-article-date-lapsed
1767     "o" gnus-article-date-original
1768     "i" gnus-article-date-iso8601
1769     "s" gnus-article-date-user)
1770
1771   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1772     "t" gnus-article-remove-trailing-blank-lines
1773     "l" gnus-article-strip-leading-blank-lines
1774     "m" gnus-article-strip-multiple-blank-lines
1775     "a" gnus-article-strip-blank-lines
1776     "A" gnus-article-strip-all-blank-lines
1777     "s" gnus-article-strip-leading-space
1778     "e" gnus-article-strip-trailing-space
1779     "w" gnus-article-remove-leading-whitespace)
1780
1781   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1782     "v" gnus-version
1783     "f" gnus-summary-fetch-faq
1784     "d" gnus-summary-describe-group
1785     "h" gnus-summary-describe-briefly
1786     "i" gnus-info-find-node)
1787
1788   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1789     "e" gnus-summary-expire-articles
1790     "\M-\C-e" gnus-summary-expire-articles-now
1791     "\177" gnus-summary-delete-article
1792     [delete] gnus-summary-delete-article
1793     [backspace] gnus-summary-delete-article
1794     "m" gnus-summary-move-article
1795     "r" gnus-summary-respool-article
1796     "w" gnus-summary-edit-article
1797     "c" gnus-summary-copy-article
1798     "B" gnus-summary-crosspost-article
1799     "q" gnus-summary-respool-query
1800     "t" gnus-summary-respool-trace
1801     "i" gnus-summary-import-article
1802     "I" gnus-summary-create-article
1803     "p" gnus-summary-article-posted-p)
1804
1805   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1806     "o" gnus-summary-save-article
1807     "m" gnus-summary-save-article-mail
1808     "F" gnus-summary-write-article-file
1809     "r" gnus-summary-save-article-rmail
1810     "f" gnus-summary-save-article-file
1811     "b" gnus-summary-save-article-body-file
1812     "h" gnus-summary-save-article-folder
1813     "v" gnus-summary-save-article-vm
1814     "p" gnus-summary-pipe-output
1815     "s" gnus-soup-add-article)
1816
1817   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1818     "b" gnus-summary-display-buttonized
1819     "m" gnus-summary-repair-multipart
1820     "v" gnus-article-view-part
1821     "o" gnus-article-save-part
1822     "c" gnus-article-copy-part
1823     "C" gnus-article-view-part-as-charset
1824     "e" gnus-article-externalize-part
1825     "E" gnus-article-encrypt-body
1826     "i" gnus-article-inline-part
1827     "|" gnus-article-pipe-part))
1828
1829 (defvar gnus-article-post-menu nil)
1830
1831 (defun gnus-summary-make-menu-bar ()
1832   (gnus-turn-off-edit-menu 'summary)
1833
1834   (unless (boundp 'gnus-summary-misc-menu)
1835
1836     (easy-menu-define
1837      gnus-summary-kill-menu gnus-summary-mode-map ""
1838      (cons
1839       "Score"
1840       (nconc
1841        (list
1842         ["Customize" gnus-score-customize t])
1843        (gnus-make-score-map 'increase)
1844        (gnus-make-score-map 'lower)
1845        '(("Mark"
1846           ["Kill below" gnus-summary-kill-below t]
1847           ["Mark above" gnus-summary-mark-above t]
1848           ["Tick above" gnus-summary-tick-above t]
1849           ["Clear above" gnus-summary-clear-above t])
1850          ["Current score" gnus-summary-current-score t]
1851          ["Set score" gnus-summary-set-score t]
1852          ["Switch current score file..." gnus-score-change-score-file t]
1853          ["Set mark below..." gnus-score-set-mark-below t]
1854          ["Set expunge below..." gnus-score-set-expunge-below t]
1855          ["Edit current score file" gnus-score-edit-current-scores t]
1856          ["Edit score file" gnus-score-edit-file t]
1857          ["Trace score" gnus-score-find-trace t]
1858          ["Find words" gnus-score-find-favourite-words t]
1859          ["Rescore buffer" gnus-summary-rescore t]
1860          ["Increase score..." gnus-summary-increase-score t]
1861          ["Lower score..." gnus-summary-lower-score t]))))
1862
1863     ;; Define both the Article menu in the summary buffer and the equivalent
1864     ;; Commands menu in the article buffer here for consistency.
1865     (let ((innards
1866            `(("Hide"
1867               ["All" gnus-article-hide t]
1868               ["Headers" gnus-article-toggle-headers t]
1869               ["Signature" gnus-article-hide-signature t]
1870               ["Citation" gnus-article-hide-citation t]
1871               ["List identifiers" gnus-article-hide-list-identifiers t]
1872               ["PGP" gnus-article-hide-pgp t]
1873               ["Banner" gnus-article-strip-banner t]
1874               ["Boring headers" gnus-article-hide-boring-headers t])
1875              ("Highlight"
1876               ["All" gnus-article-highlight t]
1877               ["Headers" gnus-article-highlight-headers t]
1878               ["Signature" gnus-article-highlight-signature t]
1879               ["Citation" gnus-article-highlight-citation t])
1880              ("Date"
1881               ["Local" gnus-article-date-local t]
1882               ["ISO8601" gnus-article-date-iso8601 t]
1883               ["UT" gnus-article-date-ut t]
1884               ["Original" gnus-article-date-original t]
1885               ["Lapsed" gnus-article-date-lapsed t]
1886               ["User-defined" gnus-article-date-user t])
1887              ("Washing"
1888               ("Remove Blanks"
1889                ["Leading" gnus-article-strip-leading-blank-lines t]
1890                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1891                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1892                ["All of the above" gnus-article-strip-blank-lines t]
1893                ["All" gnus-article-strip-all-blank-lines t]
1894                ["Leading space" gnus-article-strip-leading-space t]
1895                ["Trailing space" gnus-article-strip-trailing-space t]
1896                ["Leading space in headers"
1897                 gnus-article-remove-leading-whitespace t])
1898               ["Overstrike" gnus-article-treat-overstrike t]
1899               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1900               ["Emphasis" gnus-article-emphasize t]
1901               ["Word wrap" gnus-article-fill-cited-article t]
1902               ["Fill long lines" gnus-article-fill-long-lines t]
1903               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1904               ["CR" gnus-article-remove-cr t]
1905               ["Show X-Face" gnus-article-display-x-face t]
1906               ["Rot 13" gnus-summary-caesar-message
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "\"Caesar rotate\" article by 13"))]
1909               ["Unix pipe" gnus-summary-pipe-message t]
1910               ["Add buttons" gnus-article-add-buttons t]
1911               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1912               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1913               ["Toggle MIME" gnus-summary-toggle-mime t]
1914               ["Verbose header" gnus-summary-verbose-headers t]
1915               ["Toggle header" gnus-summary-toggle-header t]
1916               ["Toggle smiley" gnus-summary-toggle-smiley t]
1917               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1918               ["HZ" gnus-article-decode-HZ t])
1919              ("Output"
1920               ["Save in default format" gnus-summary-save-article
1921                ,@(if (featurep 'xemacs) '(t)
1922                    '(:help "Save article using default method"))]
1923               ["Save in file" gnus-summary-save-article-file
1924                ,@(if (featurep 'xemacs) '(t)
1925                    '(:help "Save article in file"))]
1926               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1927               ["Save in MH folder" gnus-summary-save-article-folder t]
1928               ["Save in VM folder" gnus-summary-save-article-vm t]
1929               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1930               ["Save body in file" gnus-summary-save-article-body-file t]
1931               ["Pipe through a filter" gnus-summary-pipe-output t]
1932               ["Add to SOUP packet" gnus-soup-add-article t]
1933               ["Print" gnus-summary-print-article t])
1934              ("Backend"
1935               ["Respool article..." gnus-summary-respool-article t]
1936               ["Move article..." gnus-summary-move-article
1937                (gnus-check-backend-function
1938                 'request-move-article gnus-newsgroup-name)]
1939               ["Copy article..." gnus-summary-copy-article t]
1940               ["Crosspost article..." gnus-summary-crosspost-article
1941                (gnus-check-backend-function
1942                 'request-replace-article gnus-newsgroup-name)]
1943               ["Import file..." gnus-summary-import-article t]
1944               ["Create article..." gnus-summary-create-article t]
1945               ["Check if posted" gnus-summary-article-posted-p t]
1946               ["Edit article" gnus-summary-edit-article
1947                (not (gnus-group-read-only-p))]
1948               ["Delete article" gnus-summary-delete-article
1949                (gnus-check-backend-function
1950                 'request-expire-articles gnus-newsgroup-name)]
1951               ["Query respool" gnus-summary-respool-query t]
1952               ["Trace respool" gnus-summary-respool-trace t]
1953               ["Delete expirable articles" gnus-summary-expire-articles-now
1954                (gnus-check-backend-function
1955                 'request-expire-articles gnus-newsgroup-name)])
1956              ("Extract"
1957               ["Uudecode" gnus-uu-decode-uu
1958                ,@(if (featurep 'xemacs) '(t)
1959                    '(:help "Decode uuencoded article(s)"))]
1960               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1961               ["Unshar" gnus-uu-decode-unshar t]
1962               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1963               ["Save" gnus-uu-decode-save t]
1964               ["Binhex" gnus-uu-decode-binhex t]
1965               ["Postscript" gnus-uu-decode-postscript t])
1966              ("Cache"
1967               ["Enter article" gnus-cache-enter-article t]
1968               ["Remove article" gnus-cache-remove-article t])
1969              ["Translate" gnus-article-babel t]
1970              ["Select article buffer" gnus-summary-select-article-buffer t]
1971              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1972              ["Isearch article..." gnus-summary-isearch-article t]
1973              ["Beginning of the article" gnus-summary-beginning-of-article t]
1974              ["End of the article" gnus-summary-end-of-article t]
1975              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1976              ["Fetch referenced articles" gnus-summary-refer-references t]
1977              ["Fetch current thread" gnus-summary-refer-thread t]
1978              ["Fetch article with id..." gnus-summary-refer-article t]
1979              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1980              ["Redisplay" gnus-summary-show-article t]
1981              ["Raw article" gnus-summary-show-raw-article t])))
1982       (easy-menu-define
1983        gnus-summary-article-menu gnus-summary-mode-map ""
1984        (cons "Article" innards))
1985
1986       (if (not (keymapp gnus-summary-article-menu))
1987           (easy-menu-define
1988            gnus-article-commands-menu gnus-article-mode-map ""
1989            (cons "Commands" innards))
1990         ;; in Emacs, don't share menu.
1991         (setq gnus-article-commands-menu
1992               (copy-keymap gnus-summary-article-menu))
1993         (define-key gnus-article-mode-map [menu-bar commands]
1994           (cons "Commands" gnus-article-commands-menu))))
1995
1996     (easy-menu-define
1997      gnus-summary-thread-menu gnus-summary-mode-map ""
1998      '("Threads"
1999        ["Toggle threading" gnus-summary-toggle-threads t]
2000        ["Hide threads" gnus-summary-hide-all-threads t]
2001        ["Show threads" gnus-summary-show-all-threads t]
2002        ["Hide thread" gnus-summary-hide-thread t]
2003        ["Show thread" gnus-summary-show-thread t]
2004        ["Go to next thread" gnus-summary-next-thread t]
2005        ["Go to previous thread" gnus-summary-prev-thread t]
2006        ["Go down thread" gnus-summary-down-thread t]
2007        ["Go up thread" gnus-summary-up-thread t]
2008        ["Top of thread" gnus-summary-top-thread t]
2009        ["Mark thread as read" gnus-summary-kill-thread t]
2010        ["Lower thread score" gnus-summary-lower-thread t]
2011        ["Raise thread score" gnus-summary-raise-thread t]
2012        ["Rethread current" gnus-summary-rethread-current t]))
2013
2014     (easy-menu-define
2015      gnus-summary-post-menu gnus-summary-mode-map ""
2016      `("Post"
2017        ["Post an article" gnus-summary-post-news
2018         ,@(if (featurep 'xemacs) '(t)
2019             '(:help "Post an article"))]
2020        ["Followup" gnus-summary-followup
2021         ,@(if (featurep 'xemacs) '(t)
2022             '(:help "Post followup to this article"))]
2023        ["Followup and yank" gnus-summary-followup-with-original
2024         ,@(if (featurep 'xemacs) '(t)
2025             '(:help "Post followup to this article, quoting its contents"))]
2026        ["Supersede article" gnus-summary-supersede-article t]
2027        ["Cancel article" gnus-summary-cancel-article
2028         ,@(if (featurep 'xemacs) '(t)
2029             '(:help "Cancel an article you posted"))]
2030        ["Reply" gnus-summary-reply t]
2031        ["Reply and yank" gnus-summary-reply-with-original t]
2032        ["Wide reply" gnus-summary-wide-reply t]
2033        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2034         ,@(if (featurep 'xemacs) '(t)
2035             '(:help "Mail a reply, quoting this article"))]
2036        ["Mail forward" gnus-summary-mail-forward t]
2037        ["Post forward" gnus-summary-post-forward t]
2038        ["Digest and mail" gnus-summary-digest-mail-forward t]
2039        ["Digest and post" gnus-summary-digest-post-forward t]
2040        ["Resend message" gnus-summary-resend-message t]
2041        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2042        ["Send a mail" gnus-summary-mail-other-window t]
2043        ["Uuencode and post" gnus-uu-post-news
2044         ,@(if (featurep 'xemacs) '(t)
2045             '(:help "Post a uuencoded article"))]
2046        ["Followup via news" gnus-summary-followup-to-mail t]
2047        ["Followup via news and yank"
2048         gnus-summary-followup-to-mail-with-original t]
2049        ;;("Draft"
2050        ;;["Send" gnus-summary-send-draft t]
2051        ;;["Send bounced" gnus-resend-bounced-mail t])
2052        ))
2053
2054     (cond
2055      ((not (keymapp gnus-summary-post-menu))
2056       (setq gnus-article-post-menu gnus-summary-post-menu))
2057      ((not gnus-article-post-menu)
2058       ;; Don't share post menu.
2059       (setq gnus-article-post-menu
2060             (copy-keymap gnus-summary-post-menu))))
2061     (define-key gnus-article-mode-map [menu-bar post]
2062       (cons "Post" gnus-article-post-menu))
2063
2064     (easy-menu-define
2065      gnus-summary-misc-menu gnus-summary-mode-map ""
2066      `("Gnus"
2067        ("Mark Read"
2068         ["Mark as read" gnus-summary-mark-as-read-forward t]
2069         ["Mark same subject and select"
2070          gnus-summary-kill-same-subject-and-select t]
2071         ["Mark same subject" gnus-summary-kill-same-subject t]
2072         ["Catchup" gnus-summary-catchup
2073          ,@(if (featurep 'xemacs) '(t)
2074              '(:help "Mark unread articles in this group as read"))]
2075         ["Catchup all" gnus-summary-catchup-all t]
2076         ["Catchup to here" gnus-summary-catchup-to-here t]
2077         ["Catchup from here" gnus-summary-catchup-from-here t]
2078         ["Catchup region" gnus-summary-mark-region-as-read t]
2079         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2080        ("Mark Various"
2081         ["Tick" gnus-summary-tick-article-forward t]
2082         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2083         ["Remove marks" gnus-summary-clear-mark-forward t]
2084         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2085         ["Set bookmark" gnus-summary-set-bookmark t]
2086         ["Remove bookmark" gnus-summary-remove-bookmark t])
2087        ("Mark Limit"
2088         ["Marks..." gnus-summary-limit-to-marks t]
2089         ["Subject..." gnus-summary-limit-to-subject t]
2090         ["Author..." gnus-summary-limit-to-author t]
2091         ["Age..." gnus-summary-limit-to-age t]
2092         ["Extra..." gnus-summary-limit-to-extra t]
2093         ["Score" gnus-summary-limit-to-score t]
2094         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2095         ["Unread" gnus-summary-limit-to-unread t]
2096         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2097         ["Articles" gnus-summary-limit-to-articles t]
2098         ["Pop limit" gnus-summary-pop-limit t]
2099         ["Show dormant" gnus-summary-limit-include-dormant t]
2100         ["Hide childless dormant"
2101          gnus-summary-limit-exclude-childless-dormant t]
2102         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2103         ["Hide marked" gnus-summary-limit-exclude-marks t]
2104         ["Show expunged" gnus-summary-limit-include-expunged t])
2105        ("Process Mark"
2106         ["Set mark" gnus-summary-mark-as-processable t]
2107         ["Remove mark" gnus-summary-unmark-as-processable t]
2108         ["Remove all marks" gnus-summary-unmark-all-processable t]
2109         ["Mark above" gnus-uu-mark-over t]
2110         ["Mark series" gnus-uu-mark-series t]
2111         ["Mark region" gnus-uu-mark-region t]
2112         ["Unmark region" gnus-uu-unmark-region t]
2113         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2114         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2115         ["Mark all" gnus-uu-mark-all t]
2116         ["Mark buffer" gnus-uu-mark-buffer t]
2117         ["Mark sparse" gnus-uu-mark-sparse t]
2118         ["Mark thread" gnus-uu-mark-thread t]
2119         ["Unmark thread" gnus-uu-unmark-thread t]
2120         ("Process Mark Sets"
2121          ["Kill" gnus-summary-kill-process-mark t]
2122          ["Yank" gnus-summary-yank-process-mark
2123           gnus-newsgroup-process-stack]
2124          ["Save" gnus-summary-save-process-mark t]))
2125        ("Scroll article"
2126         ["Page forward" gnus-summary-next-page
2127          ,@(if (featurep 'xemacs) '(t)
2128              '(:help "Show next page of article"))]
2129         ["Page backward" gnus-summary-prev-page
2130          ,@(if (featurep 'xemacs) '(t)
2131              '(:help "Show previous page of article"))]
2132         ["Line forward" gnus-summary-scroll-up t])
2133        ("Move"
2134         ["Next unread article" gnus-summary-next-unread-article t]
2135         ["Previous unread article" gnus-summary-prev-unread-article t]
2136         ["Next article" gnus-summary-next-article t]
2137         ["Previous article" gnus-summary-prev-article t]
2138         ["Next unread subject" gnus-summary-next-unread-subject t]
2139         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2140         ["Next article same subject" gnus-summary-next-same-subject t]
2141         ["Previous article same subject" gnus-summary-prev-same-subject t]
2142         ["First unread article" gnus-summary-first-unread-article t]
2143         ["Best unread article" gnus-summary-best-unread-article t]
2144         ["Go to subject number..." gnus-summary-goto-subject t]
2145         ["Go to article number..." gnus-summary-goto-article t]
2146         ["Go to the last article" gnus-summary-goto-last-article t]
2147         ["Pop article off history" gnus-summary-pop-article t])
2148        ("Sort"
2149         ["Sort by number" gnus-summary-sort-by-number t]
2150         ["Sort by author" gnus-summary-sort-by-author t]
2151         ["Sort by subject" gnus-summary-sort-by-subject t]
2152         ["Sort by date" gnus-summary-sort-by-date t]
2153         ["Sort by score" gnus-summary-sort-by-score t]
2154         ["Sort by lines" gnus-summary-sort-by-lines t]
2155         ["Sort by characters" gnus-summary-sort-by-chars t]
2156         ["Original sort" gnus-summary-sort-by-original t])
2157        ("Help"
2158         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2159         ["Describe group" gnus-summary-describe-group t]
2160         ["Read manual" gnus-info-find-node t])
2161        ("Modes"
2162         ["Pick and read" gnus-pick-mode t]
2163         ["Binary" gnus-binary-mode t])
2164        ("Regeneration"
2165         ["Regenerate" gnus-summary-prepare t]
2166         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2167         ["Toggle threading" gnus-summary-toggle-threads t])
2168        ["See old articles" gnus-summary-insert-old-articles t]
2169        ["See new articles" gnus-summary-insert-new-articles t]
2170        ["Filter articles..." gnus-summary-execute-command t]
2171        ["Run command on subjects..." gnus-summary-universal-argument t]
2172        ["Search articles forward..." gnus-summary-search-article-forward t]
2173        ["Search articles backward..." gnus-summary-search-article-backward t]
2174        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2175        ["Expand window" gnus-summary-expand-window t]
2176        ["Expire expirable articles" gnus-summary-expire-articles
2177         (gnus-check-backend-function
2178          'request-expire-articles gnus-newsgroup-name)]
2179        ["Edit local kill file" gnus-summary-edit-local-kill t]
2180        ["Edit main kill file" gnus-summary-edit-global-kill t]
2181        ["Edit group parameters" gnus-summary-edit-parameters t]
2182        ["Customize group parameters" gnus-summary-customize-parameters t]
2183        ["Send a bug report" gnus-bug t]
2184        ("Exit"
2185         ["Catchup and exit" gnus-summary-catchup-and-exit
2186          ,@(if (featurep 'xemacs) '(t)
2187              '(:help "Mark unread articles in this group as read, then exit"))]
2188         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2189         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2190         ["Exit group" gnus-summary-exit
2191          ,@(if (featurep 'xemacs) '(t)
2192              '(:help "Exit current group, return to group selection mode"))]
2193         ["Exit group without updating" gnus-summary-exit-no-update t]
2194         ["Exit and goto next group" gnus-summary-next-group t]
2195         ["Exit and goto prev group" gnus-summary-prev-group t]
2196         ["Reselect group" gnus-summary-reselect-current-group t]
2197         ["Rescan group" gnus-summary-rescan-group t]
2198         ["Update dribble" gnus-summary-save-newsrc t])))
2199
2200     (gnus-run-hooks 'gnus-summary-menu-hook)))
2201
2202 (defvar gnus-summary-tool-bar-map nil)
2203
2204 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2205 (defun gnus-summary-make-tool-bar ()
2206   (if (and (fboundp 'tool-bar-add-item-from-menu)
2207            (default-value 'tool-bar-mode)
2208            (not gnus-summary-tool-bar-map))
2209       (setq gnus-summary-tool-bar-map
2210             (let ((tool-bar-map (make-sparse-keymap))
2211                   (load-path (mm-image-load-path)))
2212               (tool-bar-add-item-from-menu
2213                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-post-news "post" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-summary-followup "followup" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-reply "reply" gnus-summary-mode-map)
2226               (tool-bar-add-item-from-menu
2227                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2228               (tool-bar-add-item-from-menu
2229                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2230               (tool-bar-add-item-from-menu
2231                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2232               (tool-bar-add-item-from-menu
2233                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2234               (tool-bar-add-item-from-menu
2235                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2236               (tool-bar-add-item-from-menu
2237                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2238               (tool-bar-add-item-from-menu
2239                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2240               (tool-bar-add-item-from-menu
2241                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2242               tool-bar-map)))
2243   (if gnus-summary-tool-bar-map
2244       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2245
2246 (defun gnus-score-set-default (var value)
2247   "A version of set that updates the GNU Emacs menu-bar."
2248   (set var value)
2249   ;; It is the message that forces the active status to be updated.
2250   (message ""))
2251
2252 (defun gnus-make-score-map (type)
2253   "Make a summary score map of type TYPE."
2254   (if t
2255       nil
2256     (let ((headers '(("author" "from" string)
2257                      ("subject" "subject" string)
2258                      ("article body" "body" string)
2259                      ("article head" "head" string)
2260                      ("xref" "xref" string)
2261                      ("extra header" "extra" string)
2262                      ("lines" "lines" number)
2263                      ("followups to author" "followup" string)))
2264           (types '((number ("less than" <)
2265                            ("greater than" >)
2266                            ("equal" =))
2267                    (string ("substring" s)
2268                            ("exact string" e)
2269                            ("fuzzy string" f)
2270                            ("regexp" r))))
2271           (perms '(("temporary" (current-time-string))
2272                    ("permanent" nil)
2273                    ("immediate" now)))
2274           header)
2275       (list
2276        (apply
2277         'nconc
2278         (list
2279          (if (eq type 'lower)
2280              "Lower score"
2281            "Increase score"))
2282         (let (outh)
2283           (while headers
2284             (setq header (car headers))
2285             (setq outh
2286                   (cons
2287                    (apply
2288                     'nconc
2289                     (list (car header))
2290                     (let ((ts (cdr (assoc (nth 2 header) types)))
2291                           outt)
2292                       (while ts
2293                         (setq outt
2294                               (cons
2295                                (apply
2296                                 'nconc
2297                                 (list (caar ts))
2298                                 (let ((ps perms)
2299                                       outp)
2300                                   (while ps
2301                                     (setq outp
2302                                           (cons
2303                                            (vector
2304                                             (caar ps)
2305                                             (list
2306                                              'gnus-summary-score-entry
2307                                              (nth 1 header)
2308                                              (if (or (string= (nth 1 header)
2309                                                               "head")
2310                                                      (string= (nth 1 header)
2311                                                               "body"))
2312                                                  ""
2313                                                (list 'gnus-summary-header
2314                                                      (nth 1 header)))
2315                                              (list 'quote (nth 1 (car ts)))
2316                                              (list 'gnus-score-delta-default
2317                                                    nil)
2318                                              (nth 1 (car ps))
2319                                              t)
2320                                             t)
2321                                            outp))
2322                                     (setq ps (cdr ps)))
2323                                   (list (nreverse outp))))
2324                                outt))
2325                         (setq ts (cdr ts)))
2326                       (list (nreverse outt))))
2327                    outh))
2328             (setq headers (cdr headers)))
2329           (list (nreverse outh))))))))
2330
2331 \f
2332
2333 (defun gnus-summary-mode (&optional group)
2334   "Major mode for reading articles.
2335
2336 All normal editing commands are switched off.
2337 \\<gnus-summary-mode-map>
2338 Each line in this buffer represents one article.  To read an
2339 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2340 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2341 respectively.
2342
2343 You can also post articles and send mail from this buffer.  To
2344 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2345 of an article, type `\\[gnus-summary-reply]'.
2346
2347 There are approx. one gazillion commands you can execute in this
2348 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2349
2350 The following commands are available:
2351
2352 \\{gnus-summary-mode-map}"
2353   (interactive)
2354   (kill-all-local-variables)
2355   (when (gnus-visual-p 'summary-menu 'menu)
2356     (gnus-summary-make-menu-bar)
2357     (gnus-summary-make-tool-bar))
2358   (gnus-summary-make-local-variables)
2359   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2360     (gnus-summary-make-local-variables))
2361   (gnus-make-thread-indent-array)
2362   (gnus-simplify-mode-line)
2363   (setq major-mode 'gnus-summary-mode)
2364   (setq mode-name "Summary")
2365   (make-local-variable 'minor-mode-alist)
2366   (use-local-map gnus-summary-mode-map)
2367   (buffer-disable-undo)
2368   (setq buffer-read-only t)             ;Disable modification
2369   (setq truncate-lines t)
2370   (setq selective-display t)
2371   (setq selective-display-ellipses t)   ;Display `...'
2372   (gnus-summary-set-display-table)
2373   (gnus-set-default-directory)
2374   (setq gnus-newsgroup-name group)
2375   (unless (gnus-news-group-p group)
2376     (setq gnus-newsgroup-incorporated
2377           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2378   (make-local-variable 'gnus-summary-line-format)
2379   (make-local-variable 'gnus-summary-line-format-spec)
2380   (make-local-variable 'gnus-summary-dummy-line-format)
2381   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2382   (make-local-variable 'gnus-summary-mark-positions)
2383   (make-local-hook 'pre-command-hook)
2384   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2385   (gnus-run-hooks 'gnus-summary-mode-hook)
2386   (turn-on-gnus-mailing-list-mode)
2387   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2388   (gnus-update-summary-mark-positions))
2389
2390 (defun gnus-summary-make-local-variables ()
2391   "Make all the local summary buffer variables."
2392   (let (global)
2393     (dolist (local gnus-summary-local-variables)
2394       (if (consp local)
2395           (progn
2396             (if (eq (cdr local) 'global)
2397                 ;; Copy the global value of the variable.
2398                 (setq global (symbol-value (car local)))
2399               ;; Use the value from the list.
2400               (setq global (eval (cdr local))))
2401             (set (make-local-variable (car local)) global))
2402         ;; Simple nil-valued local variable.
2403         (set (make-local-variable local) nil)))))
2404
2405 (defun gnus-summary-clear-local-variables ()
2406   (let ((locals gnus-summary-local-variables))
2407     (while locals
2408       (if (consp (car locals))
2409           (and (vectorp (caar locals))
2410                (set (caar locals) nil))
2411         (and (vectorp (car locals))
2412              (set (car locals) nil)))
2413       (setq locals (cdr locals)))))
2414
2415 ;; Summary data functions.
2416
2417 (defmacro gnus-data-number (data)
2418   `(car ,data))
2419
2420 (defmacro gnus-data-set-number (data number)
2421   `(setcar ,data ,number))
2422
2423 (defmacro gnus-data-mark (data)
2424   `(nth 1 ,data))
2425
2426 (defmacro gnus-data-set-mark (data mark)
2427   `(setcar (nthcdr 1 ,data) ,mark))
2428
2429 (defmacro gnus-data-pos (data)
2430   `(nth 2 ,data))
2431
2432 (defmacro gnus-data-set-pos (data pos)
2433   `(setcar (nthcdr 2 ,data) ,pos))
2434
2435 (defmacro gnus-data-header (data)
2436   `(nth 3 ,data))
2437
2438 (defmacro gnus-data-set-header (data header)
2439   `(setcar (nthcdr 3 ,data) ,header))
2440
2441 (defmacro gnus-data-level (data)
2442   `(nth 4 ,data))
2443
2444 (defmacro gnus-data-unread-p (data)
2445   `(= (nth 1 ,data) gnus-unread-mark))
2446
2447 (defmacro gnus-data-read-p (data)
2448   `(/= (nth 1 ,data) gnus-unread-mark))
2449
2450 (defmacro gnus-data-pseudo-p (data)
2451   `(consp (nth 3 ,data)))
2452
2453 (defmacro gnus-data-find (number)
2454   `(assq ,number gnus-newsgroup-data))
2455
2456 (defmacro gnus-data-find-list (number &optional data)
2457   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2458      (memq (assq ,number bdata)
2459            bdata)))
2460
2461 (defmacro gnus-data-make (number mark pos header level)
2462   `(list ,number ,mark ,pos ,header ,level))
2463
2464 (defun gnus-data-enter (after-article number mark pos header level offset)
2465   (let ((data (gnus-data-find-list after-article)))
2466     (unless data
2467       (error "No such article: %d" after-article))
2468     (setcdr data (cons (gnus-data-make number mark pos header level)
2469                        (cdr data)))
2470     (setq gnus-newsgroup-data-reverse nil)
2471     (gnus-data-update-list (cddr data) offset)))
2472
2473 (defun gnus-data-enter-list (after-article list &optional offset)
2474   (when list
2475     (let ((data (and after-article (gnus-data-find-list after-article)))
2476           (ilist list))
2477       (if (not (or data
2478                    after-article))
2479           (let ((odata gnus-newsgroup-data))
2480             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2481             (when offset
2482               (gnus-data-update-list odata offset)))
2483         ;; Find the last element in the list to be spliced into the main
2484         ;; list.
2485         (while (cdr list)
2486           (setq list (cdr list)))
2487         (if (not data)
2488             (progn
2489               (setcdr list gnus-newsgroup-data)
2490               (setq gnus-newsgroup-data ilist)
2491               (when offset
2492                 (gnus-data-update-list (cdr list) offset)))
2493           (setcdr list (cdr data))
2494           (setcdr data ilist)
2495           (when offset
2496             (gnus-data-update-list (cdr list) offset))))
2497       (setq gnus-newsgroup-data-reverse nil))))
2498
2499 (defun gnus-data-remove (article &optional offset)
2500   (let ((data gnus-newsgroup-data))
2501     (if (= (gnus-data-number (car data)) article)
2502         (progn
2503           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2504                 gnus-newsgroup-data-reverse nil)
2505           (when offset
2506             (gnus-data-update-list gnus-newsgroup-data offset)))
2507       (while (cdr data)
2508         (when (= (gnus-data-number (cadr data)) article)
2509           (setcdr data (cddr data))
2510           (when offset
2511             (gnus-data-update-list (cdr data) offset))
2512           (setq data nil
2513                 gnus-newsgroup-data-reverse nil))
2514         (setq data (cdr data))))))
2515
2516 (defmacro gnus-data-list (backward)
2517   `(if ,backward
2518        (or gnus-newsgroup-data-reverse
2519            (setq gnus-newsgroup-data-reverse
2520                  (reverse gnus-newsgroup-data)))
2521      gnus-newsgroup-data))
2522
2523 (defun gnus-data-update-list (data offset)
2524   "Add OFFSET to the POS of all data entries in DATA."
2525   (setq gnus-newsgroup-data-reverse nil)
2526   (while data
2527     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2528     (setq data (cdr data))))
2529
2530 (defun gnus-summary-article-pseudo-p (article)
2531   "Say whether this article is a pseudo article or not."
2532   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2533
2534 (defmacro gnus-summary-article-sparse-p (article)
2535   "Say whether this article is a sparse article or not."
2536   `(memq ,article gnus-newsgroup-sparse))
2537
2538 (defmacro gnus-summary-article-ancient-p (article)
2539   "Say whether this article is a sparse article or not."
2540   `(memq ,article gnus-newsgroup-ancient))
2541
2542 (defun gnus-article-parent-p (number)
2543   "Say whether this article is a parent or not."
2544   (let ((data (gnus-data-find-list number)))
2545     (and (cdr data)                     ; There has to be an article after...
2546          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2547             (gnus-data-level (nth 1 data))))))
2548
2549 (defun gnus-article-children (number)
2550   "Return a list of all children to NUMBER."
2551   (let* ((data (gnus-data-find-list number))
2552          (level (gnus-data-level (car data)))
2553          children)
2554     (setq data (cdr data))
2555     (while (and data
2556                 (= (gnus-data-level (car data)) (1+ level)))
2557       (push (gnus-data-number (car data)) children)
2558       (setq data (cdr data)))
2559     children))
2560
2561 (defmacro gnus-summary-skip-intangible ()
2562   "If the current article is intangible, then jump to a different article."
2563   '(let ((to (get-text-property (point) 'gnus-intangible)))
2564      (and to (gnus-summary-goto-subject to))))
2565
2566 (defmacro gnus-summary-article-intangible-p ()
2567   "Say whether this article is intangible or not."
2568   '(get-text-property (point) 'gnus-intangible))
2569
2570 (defun gnus-article-read-p (article)
2571   "Say whether ARTICLE is read or not."
2572   (not (or (memq article gnus-newsgroup-marked)
2573            (memq article gnus-newsgroup-unreads)
2574            (memq article gnus-newsgroup-unselected)
2575            (memq article gnus-newsgroup-dormant))))
2576
2577 ;; Some summary mode macros.
2578
2579 (defmacro gnus-summary-article-number ()
2580   "The article number of the article on the current line.
2581 If there isn's an article number here, then we return the current
2582 article number."
2583   '(progn
2584      (gnus-summary-skip-intangible)
2585      (or (get-text-property (point) 'gnus-number)
2586          (gnus-summary-last-subject))))
2587
2588 (defmacro gnus-summary-article-header (&optional number)
2589   "Return the header of article NUMBER."
2590   `(gnus-data-header (gnus-data-find
2591                       ,(or number '(gnus-summary-article-number)))))
2592
2593 (defmacro gnus-summary-thread-level (&optional number)
2594   "Return the level of thread that starts with article NUMBER."
2595   `(if (and (eq gnus-summary-make-false-root 'dummy)
2596             (get-text-property (point) 'gnus-intangible))
2597        0
2598      (gnus-data-level (gnus-data-find
2599                        ,(or number '(gnus-summary-article-number))))))
2600
2601 (defmacro gnus-summary-article-mark (&optional number)
2602   "Return the mark of article NUMBER."
2603   `(gnus-data-mark (gnus-data-find
2604                     ,(or number '(gnus-summary-article-number)))))
2605
2606 (defmacro gnus-summary-article-pos (&optional number)
2607   "Return the position of the line of article NUMBER."
2608   `(gnus-data-pos (gnus-data-find
2609                    ,(or number '(gnus-summary-article-number)))))
2610
2611 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2612 (defmacro gnus-summary-article-subject (&optional number)
2613   "Return current subject string or nil if nothing."
2614   `(let ((headers
2615           ,(if number
2616                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2617              '(gnus-data-header (assq (gnus-summary-article-number)
2618                                       gnus-newsgroup-data)))))
2619      (and headers
2620           (vectorp headers)
2621           (mail-header-subject headers))))
2622
2623 (defmacro gnus-summary-article-score (&optional number)
2624   "Return current article score."
2625   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2626                   gnus-newsgroup-scored))
2627        gnus-summary-default-score 0))
2628
2629 (defun gnus-summary-article-children (&optional number)
2630   "Return a list of article numbers that are children of article NUMBER."
2631   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2632          (level (gnus-data-level (car data)))
2633          l children)
2634     (while (and (setq data (cdr data))
2635                 (> (setq l (gnus-data-level (car data))) level))
2636       (and (= (1+ level) l)
2637            (push (gnus-data-number (car data))
2638                  children)))
2639     (nreverse children)))
2640
2641 (defun gnus-summary-article-parent (&optional number)
2642   "Return the article number of the parent of article NUMBER."
2643   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2644                                     (gnus-data-list t)))
2645          (level (gnus-data-level (car data))))
2646     (if (zerop level)
2647         ()                              ; This is a root.
2648       ;; We search until we find an article with a level less than
2649       ;; this one.  That function has to be the parent.
2650       (while (and (setq data (cdr data))
2651                   (not (< (gnus-data-level (car data)) level))))
2652       (and data (gnus-data-number (car data))))))
2653
2654 (defun gnus-unread-mark-p (mark)
2655   "Say whether MARK is the unread mark."
2656   (= mark gnus-unread-mark))
2657
2658 (defun gnus-read-mark-p (mark)
2659   "Say whether MARK is one of the marks that mark as read.
2660 This is all marks except unread, ticked, dormant, and expirable."
2661   (not (or (= mark gnus-unread-mark)
2662            (= mark gnus-ticked-mark)
2663            (= mark gnus-dormant-mark)
2664            (= mark gnus-expirable-mark))))
2665
2666 (defmacro gnus-article-mark (number)
2667   "Return the MARK of article NUMBER.
2668 This macro should only be used when computing the mark the \"first\"
2669 time; i.e., when generating the summary lines.  After that,
2670 `gnus-summary-article-mark' should be used to examine the
2671 marks of articles."
2672   `(cond
2673     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2674     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2675     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2676     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2677     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2678     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2679     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2680     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2681            gnus-ancient-mark))))
2682
2683 ;; Saving hidden threads.
2684
2685 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2686 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2687
2688 (defmacro gnus-save-hidden-threads (&rest forms)
2689   "Save hidden threads, eval FORMS, and restore the hidden threads."
2690   (let ((config (make-symbol "config")))
2691     `(let ((,config (gnus-hidden-threads-configuration)))
2692        (unwind-protect
2693            (save-excursion
2694              ,@forms)
2695          (gnus-restore-hidden-threads-configuration ,config)))))
2696
2697 (defun gnus-data-compute-positions ()
2698   "Compute the positions of all articles."
2699   (setq gnus-newsgroup-data-reverse nil)
2700   (let ((data gnus-newsgroup-data))
2701     (save-excursion
2702       (gnus-save-hidden-threads
2703         (gnus-summary-show-all-threads)
2704         (goto-char (point-min))
2705         (while data
2706           (while (get-text-property (point) 'gnus-intangible)
2707             (forward-line 1))
2708           (gnus-data-set-pos (car data) (+ (point) 3))
2709           (setq data (cdr data))
2710           (forward-line 1))))))
2711
2712 (defun gnus-hidden-threads-configuration ()
2713   "Return the current hidden threads configuration."
2714   (save-excursion
2715     (let (config)
2716       (goto-char (point-min))
2717       (while (search-forward "\r" nil t)
2718         (push (1- (point)) config))
2719       config)))
2720
2721 (defun gnus-restore-hidden-threads-configuration (config)
2722   "Restore hidden threads configuration from CONFIG."
2723   (save-excursion
2724     (let (point buffer-read-only)
2725       (while (setq point (pop config))
2726         (when (and (< point (point-max))
2727                    (goto-char point)
2728                    (eq (char-after) ?\n))
2729           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2730
2731 ;; Various summary mode internalish functions.
2732
2733 (defun gnus-mouse-pick-article (e)
2734   (interactive "e")
2735   (mouse-set-point e)
2736   (gnus-summary-next-page nil t))
2737
2738 (defun gnus-summary-set-display-table ()
2739   "Change the display table.
2740 Odd characters have a tendency to mess
2741 up nicely formatted displays - we make all possible glyphs
2742 display only a single character."
2743
2744   ;; We start from the standard display table, if any.
2745   (let ((table (or (copy-sequence standard-display-table)
2746                    (make-display-table)))
2747         (i 32))
2748     ;; Nix out all the control chars...
2749     (while (>= (setq i (1- i)) 0)
2750       (aset table i [??]))
2751     ;; ... but not newline and cr, of course.  (cr is necessary for the
2752     ;; selective display).
2753     (aset table ?\n nil)
2754     (aset table ?\r nil)
2755     ;; We keep TAB as well.
2756     (aset table ?\t nil)
2757     ;; We nix out any glyphs over 126 that are not set already.
2758     (let ((i 256))
2759       (while (>= (setq i (1- i)) 127)
2760         ;; Only modify if the entry is nil.
2761         (unless (aref table i)
2762           (aset table i [??]))))
2763     (setq buffer-display-table table)))
2764
2765 (defun gnus-summary-set-article-display-arrow (pos)
2766   "Update the overlay arrow to point to line at position POS."
2767   (when (and gnus-summary-display-arrow
2768              (boundp 'overlay-arrow-position)
2769              (boundp 'overlay-arrow-string))
2770     (save-excursion
2771       (goto-char pos)
2772       (beginning-of-line)
2773       (unless overlay-arrow-position
2774         (setq overlay-arrow-position (make-marker)))
2775       (setq overlay-arrow-string "=>"
2776             overlay-arrow-position (set-marker overlay-arrow-position
2777                                                (point)
2778                                                (current-buffer))))))
2779
2780 (defun gnus-summary-buffer-name (group)
2781   "Return the summary buffer name of GROUP."
2782   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2783
2784 (defun gnus-summary-setup-buffer (group)
2785   "Initialize summary buffer."
2786   (let ((buffer (gnus-summary-buffer-name group)))
2787     (if (get-buffer buffer)
2788         (progn
2789           (set-buffer buffer)
2790           (setq gnus-summary-buffer (current-buffer))
2791           (not gnus-newsgroup-prepared))
2792       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2793       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2794       (gnus-summary-mode group)
2795       (when gnus-carpal
2796         (gnus-carpal-setup-buffer 'summary))
2797       (unless gnus-single-article-buffer
2798         (make-local-variable 'gnus-article-buffer)
2799         (make-local-variable 'gnus-article-current)
2800         (make-local-variable 'gnus-original-article-buffer))
2801       (setq gnus-newsgroup-name group)
2802       ;; Set any local variables in the group parameters.
2803       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2804       t)))
2805
2806 (defun gnus-set-global-variables ()
2807   "Set the global equivalents of the buffer-local variables.
2808 They are set to the latest values they had.  These reflect the summary
2809 buffer that was in action when the last article was fetched."
2810   (when (eq major-mode 'gnus-summary-mode)
2811     (setq gnus-summary-buffer (current-buffer))
2812     (let ((name gnus-newsgroup-name)
2813           (marked gnus-newsgroup-marked)
2814           (unread gnus-newsgroup-unreads)
2815           (headers gnus-current-headers)
2816           (data gnus-newsgroup-data)
2817           (summary gnus-summary-buffer)
2818           (article-buffer gnus-article-buffer)
2819           (original gnus-original-article-buffer)
2820           (gac gnus-article-current)
2821           (reffed gnus-reffed-article-number)
2822           (score-file gnus-current-score-file)
2823           (default-charset gnus-newsgroup-charset)
2824           vlist)
2825       (let ((locals gnus-newsgroup-variables))
2826         (while locals
2827           (if (consp (car locals))
2828               (push (eval (caar locals)) vlist)
2829             (push (eval (car locals)) vlist))
2830           (setq locals (cdr locals)))
2831         (setq vlist (nreverse vlist)))
2832       (save-excursion
2833         (set-buffer gnus-group-buffer)
2834         (setq gnus-newsgroup-name name
2835               gnus-newsgroup-marked marked
2836               gnus-newsgroup-unreads unread
2837               gnus-current-headers headers
2838               gnus-newsgroup-data data
2839               gnus-article-current gac
2840               gnus-summary-buffer summary
2841               gnus-article-buffer article-buffer
2842               gnus-original-article-buffer original
2843               gnus-reffed-article-number reffed
2844               gnus-current-score-file score-file
2845               gnus-newsgroup-charset default-charset)
2846         (let ((locals gnus-newsgroup-variables))
2847           (while locals
2848             (if (consp (car locals))
2849                 (set (caar locals) (pop vlist))
2850               (set (car locals) (pop vlist)))
2851             (setq locals (cdr locals))))
2852         ;; The article buffer also has local variables.
2853         (when (gnus-buffer-live-p gnus-article-buffer)
2854           (set-buffer gnus-article-buffer)
2855           (setq gnus-summary-buffer summary))))))
2856
2857 (defun gnus-summary-article-unread-p (article)
2858   "Say whether ARTICLE is unread or not."
2859   (memq article gnus-newsgroup-unreads))
2860
2861 (defun gnus-summary-first-article-p (&optional article)
2862   "Return whether ARTICLE is the first article in the buffer."
2863   (if (not (setq article (or article (gnus-summary-article-number))))
2864       nil
2865     (eq article (caar gnus-newsgroup-data))))
2866
2867 (defun gnus-summary-last-article-p (&optional article)
2868   "Return whether ARTICLE is the last article in the buffer."
2869   (if (not (setq article (or article (gnus-summary-article-number))))
2870       ;; All non-existent numbers are the last article.  :-)
2871       t
2872     (not (cdr (gnus-data-find-list article)))))
2873
2874 (defun gnus-make-thread-indent-array ()
2875   (let ((n 200))
2876     (unless (and gnus-thread-indent-array
2877                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2878       (setq gnus-thread-indent-array (make-vector 201 "")
2879             gnus-thread-indent-array-level gnus-thread-indent-level)
2880       (while (>= n 0)
2881         (aset gnus-thread-indent-array n
2882               (make-string (* n gnus-thread-indent-level) ? ))
2883         (setq n (1- n))))))
2884
2885 (defun gnus-update-summary-mark-positions ()
2886   "Compute where the summary marks are to go."
2887   (save-excursion
2888     (when (gnus-buffer-exists-p gnus-summary-buffer)
2889       (set-buffer gnus-summary-buffer))
2890     (let ((gnus-replied-mark 129)
2891           (gnus-score-below-mark 130)
2892           (gnus-score-over-mark 130)
2893           (gnus-download-mark 131)
2894           (spec gnus-summary-line-format-spec)
2895           gnus-visual pos)
2896       (save-excursion
2897         (gnus-set-work-buffer)
2898         (let ((gnus-summary-line-format-spec spec)
2899               (gnus-newsgroup-downloadable '((0 . t))))
2900           (gnus-summary-insert-line
2901            (make-full-mail-header 0 "" "nobody"
2902                                   "05 Apr 2001 23:33:09 +0400"
2903                                   "" "" 0 0 "" nil)
2904            0 nil 128 t nil "" nil 1)
2905           (goto-char (point-min))
2906           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2907                                              (- (point) 2)))))
2908           (goto-char (point-min))
2909           (push (cons 'replied (and (search-forward "\201" nil t)
2910                                     (- (point) 2)))
2911                 pos)
2912           (goto-char (point-min))
2913           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2914                 pos)
2915           (goto-char (point-min))
2916           (push (cons 'download
2917                       (and (search-forward "\203" nil t) (- (point) 2)))
2918                 pos)))
2919       (setq gnus-summary-mark-positions pos))))
2920
2921 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2922   "Insert a dummy root in the summary buffer."
2923   (beginning-of-line)
2924   (gnus-add-text-properties
2925    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2926    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2927
2928 (defun gnus-summary-extract-address-component (from)
2929   (or (car (funcall gnus-extract-address-components from))
2930       from))
2931
2932 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2933   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
2934                                 default-mime-charset)))
2935     ;; Is it really necessary to do this next part for each summary line?
2936     ;; Luckily, doesn't seem to slow things down much.
2937     (or
2938      (and gnus-ignored-from-addresses
2939           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2940           (let ((extra-headers (mail-header-extra header))
2941                 to
2942                 newsgroups)
2943             (cond
2944              ((setq to (cdr (assq 'To extra-headers)))
2945               (concat "-> "
2946                       (gnus-summary-extract-address-component
2947                        (funcall gnus-decode-encoded-word-function to))))
2948              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2949               (concat "=> " newsgroups)))))
2950      (gnus-summary-extract-address-component gnus-tmp-from))))
2951
2952 (defun gnus-summary-insert-line (gnus-tmp-header
2953                                  gnus-tmp-level gnus-tmp-current
2954                                  gnus-tmp-unread gnus-tmp-replied
2955                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2956                                  &optional gnus-tmp-dummy gnus-tmp-score
2957                                  gnus-tmp-process)
2958   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2959          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2960          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2961          (gnus-tmp-score-char
2962           (if (or (null gnus-summary-default-score)
2963                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2964                       gnus-summary-zcore-fuzz))
2965               ?\ ;;;Whitespace
2966             (if (< gnus-tmp-score gnus-summary-default-score)
2967                 gnus-score-below-mark gnus-score-over-mark)))
2968          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2969          (gnus-tmp-replied
2970           (cond (gnus-tmp-process gnus-process-mark)
2971                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2972                  gnus-cached-mark)
2973                 (gnus-tmp-replied gnus-replied-mark)
2974                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2975                  gnus-forwarded-mark)
2976                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2977                  gnus-saved-mark)
2978                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2979                  gnus-recent-mark)
2980                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2981                  gnus-unseen-mark)
2982                 (t gnus-no-mark)))
2983          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2984          (gnus-tmp-name
2985           (cond
2986            ((string-match "<[^>]+> *$" gnus-tmp-from)
2987             (let ((beg (match-beginning 0)))
2988               (or (and (string-match "^\".+\"" gnus-tmp-from)
2989                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2990                   (substring gnus-tmp-from 0 beg))))
2991            ((string-match "(.+)" gnus-tmp-from)
2992             (substring gnus-tmp-from
2993                        (1+ (match-beginning 0)) (1- (match-end 0))))
2994            (t gnus-tmp-from)))
2995          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2996          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2997          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2998          (buffer-read-only nil))
2999     (when (string= gnus-tmp-name "")
3000       (setq gnus-tmp-name gnus-tmp-from))
3001     (unless (numberp gnus-tmp-lines)
3002       (setq gnus-tmp-lines -1))
3003     (if (= gnus-tmp-lines -1)
3004         (setq gnus-tmp-lines "?")
3005       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3006     (gnus-put-text-property-excluding-characters-with-faces
3007      (point)
3008      (progn (eval gnus-summary-line-format-spec) (point))
3009      'gnus-number gnus-tmp-number)
3010     (when (gnus-visual-p 'summary-highlight 'highlight)
3011       (forward-line -1)
3012       (gnus-run-hooks 'gnus-summary-update-hook)
3013       (forward-line 1))))
3014
3015 (defun gnus-summary-update-line (&optional dont-update)
3016   "Update summary line after change."
3017   (when (and gnus-summary-default-score
3018              (not gnus-summary-inhibit-highlight))
3019     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3020            (article (gnus-summary-article-number))
3021            (score (gnus-summary-article-score article)))
3022       (unless dont-update
3023         (if (and gnus-summary-mark-below
3024                  (< (gnus-summary-article-score)
3025                     gnus-summary-mark-below))
3026             ;; This article has a low score, so we mark it as read.
3027             (when (memq article gnus-newsgroup-unreads)
3028               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3029           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3030             ;; This article was previously marked as read on account
3031             ;; of a low score, but now it has risen, so we mark it as
3032             ;; unread.
3033             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3034         (gnus-summary-update-mark
3035          (if (or (null gnus-summary-default-score)
3036                  (<= (abs (- score gnus-summary-default-score))
3037                      gnus-summary-zcore-fuzz))
3038              ?\ ;;;Whitespace
3039            (if (< score gnus-summary-default-score)
3040                gnus-score-below-mark gnus-score-over-mark))
3041          'score))
3042       ;; Do visual highlighting.
3043       (when (gnus-visual-p 'summary-highlight 'highlight)
3044         (gnus-run-hooks 'gnus-summary-update-hook)))))
3045
3046 (defvar gnus-tmp-new-adopts nil)
3047
3048 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3049   "Return the number of articles in THREAD.
3050 This may be 0 in some cases -- if none of the articles in
3051 the thread are to be displayed."
3052   (let* ((number
3053           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3054           (cond
3055            ((not (listp thread))
3056             1)
3057            ((and (consp thread) (cdr thread))
3058             (apply
3059              '+ 1 (mapcar
3060                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3061            ((null thread)
3062             1)
3063            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3064             1)
3065            (t 0))))
3066     (when (and level (zerop level) gnus-tmp-new-adopts)
3067       (incf number
3068             (apply '+ (mapcar
3069                        'gnus-summary-number-of-articles-in-thread
3070                        gnus-tmp-new-adopts))))
3071     (if char
3072         (if (> number 1) gnus-not-empty-thread-mark
3073           gnus-empty-thread-mark)
3074       number)))
3075
3076 (defun gnus-summary-set-local-parameters (group)
3077   "Go through the local params of GROUP and set all variable specs in that list."
3078   (let ((params (gnus-group-find-parameter group))
3079         (vars '(quit-config))           ; Ignore quit-config.
3080         elem)
3081     (while params
3082       (setq elem (car params)
3083             params (cdr params))
3084       (and (consp elem)                 ; Has to be a cons.
3085            (consp (cdr elem))           ; The cdr has to be a list.
3086            (symbolp (car elem))         ; Has to be a symbol in there.
3087            (not (memq (car elem) vars))
3088            (ignore-errors               ; So we set it.
3089              (push (car elem) vars)
3090              (make-local-variable (car elem))
3091              (set (car elem) (eval (nth 1 elem))))))))
3092
3093 (defun gnus-summary-read-group (group &optional show-all no-article
3094                                       kill-buffer no-display backward
3095                                       select-articles)
3096   "Start reading news in newsgroup GROUP.
3097 If SHOW-ALL is non-nil, already read articles are also listed.
3098 If NO-ARTICLE is non-nil, no article is selected initially.
3099 If NO-DISPLAY, don't generate a summary buffer."
3100   (let (result)
3101     (while (and group
3102                 (null (setq result
3103                             (let ((gnus-auto-select-next nil))
3104                               (or (gnus-summary-read-group-1
3105                                    group show-all no-article
3106                                    kill-buffer no-display
3107                                    select-articles)
3108                                   (setq show-all nil
3109                                         select-articles nil)))))
3110                 (eq gnus-auto-select-next 'quietly))
3111       (set-buffer gnus-group-buffer)
3112       ;; The entry function called above goes to the next
3113       ;; group automatically, so we go two groups back
3114       ;; if we are searching for the previous group.
3115       (when backward
3116         (gnus-group-prev-unread-group 2))
3117       (if (not (equal group (gnus-group-group-name)))
3118           (setq group (gnus-group-group-name))
3119         (setq group nil)))
3120     result))
3121
3122 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3123   "Directly jump to the other GROUP from summary buffer.
3124 If SHOW-ALL is non-nil, already read articles are also listed."
3125   (interactive
3126    (if (eq gnus-summary-buffer (current-buffer))
3127        (list (completing-read
3128               "Group: " gnus-active-hashtb nil t
3129               (when (and gnus-newsgroup-name
3130                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3131                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3132               'gnus-group-history)
3133              current-prefix-arg)
3134      (error "%s must be invoked from a gnus summary buffer." this-command)))
3135   (unless (or (zerop (length group))
3136               (and gnus-newsgroup-name
3137                    (string-equal gnus-newsgroup-name group)))
3138     (gnus-summary-exit)
3139     (gnus-summary-read-group group show-all
3140                              gnus-dont-select-after-jump-to-other-group)))
3141
3142 (defun gnus-summary-read-group-1 (group show-all no-article
3143                                         kill-buffer no-display
3144                                         &optional select-articles)
3145   ;; Killed foreign groups can't be entered.
3146   ;;  (when (and (not (gnus-group-native-p group))
3147   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3148   ;;    (error "Dead non-native groups can't be entered"))
3149   (gnus-message 5 "Retrieving newsgroup: %s..."
3150                 (gnus-group-decoded-name group))
3151   (let* ((new-group (gnus-summary-setup-buffer group))
3152          (quit-config (gnus-group-quit-config group))
3153          (did-select (and new-group (gnus-select-newsgroup
3154                                      group show-all select-articles))))
3155     (cond
3156      ;; This summary buffer exists already, so we just select it.
3157      ((not new-group)
3158       (gnus-set-global-variables)
3159       (when kill-buffer
3160         (gnus-kill-or-deaden-summary kill-buffer))
3161       (gnus-configure-windows 'summary 'force)
3162       (gnus-set-mode-line 'summary)
3163       (gnus-summary-position-point)
3164       (message "")
3165       t)
3166      ;; We couldn't select this group.
3167      ((null did-select)
3168       (when (and (eq major-mode 'gnus-summary-mode)
3169                  (not (equal (current-buffer) kill-buffer)))
3170         (kill-buffer (current-buffer))
3171         (if (not quit-config)
3172             (progn
3173               ;; Update the info -- marks might need to be removed,
3174               ;; for instance.
3175               (gnus-summary-update-info)
3176               (set-buffer gnus-group-buffer)
3177               (gnus-group-jump-to-group group)
3178               (gnus-group-next-unread-group 1))
3179           (gnus-handle-ephemeral-exit quit-config)))
3180       (let ((grpinfo (gnus-get-info group)))
3181         (if (null (gnus-info-read grpinfo))
3182             (gnus-message 3 "Group %s contains no messages"
3183                           (gnus-group-decoded-name group))
3184           (gnus-message 3 "Can't select group")))
3185       nil)
3186      ;; The user did a `C-g' while prompting for number of articles,
3187      ;; so we exit this group.
3188      ((eq did-select 'quit)
3189       (and (eq major-mode 'gnus-summary-mode)
3190            (not (equal (current-buffer) kill-buffer))
3191            (kill-buffer (current-buffer)))
3192       (when kill-buffer
3193         (gnus-kill-or-deaden-summary kill-buffer))
3194       (if (not quit-config)
3195           (progn
3196             (set-buffer gnus-group-buffer)
3197             (gnus-group-jump-to-group group)
3198             (gnus-group-next-unread-group 1)
3199             (gnus-configure-windows 'group 'force))
3200         (gnus-handle-ephemeral-exit quit-config))
3201       ;; Finally signal the quit.
3202       (signal 'quit nil))
3203      ;; The group was successfully selected.
3204      (t
3205       (gnus-set-global-variables)
3206       ;; Save the active value in effect when the group was entered.
3207       (setq gnus-newsgroup-active
3208             (gnus-copy-sequence
3209              (gnus-active gnus-newsgroup-name)))
3210       ;; You can change the summary buffer in some way with this hook.
3211       (gnus-run-hooks 'gnus-select-group-hook)
3212       (gnus-update-format-specifications
3213        nil 'summary 'summary-mode 'summary-dummy)
3214       (gnus-update-summary-mark-positions)
3215       ;; Do score processing.
3216       (when gnus-use-scoring
3217         (gnus-possibly-score-headers))
3218       ;; Check whether to fill in the gaps in the threads.
3219       (when gnus-build-sparse-threads
3220         (gnus-build-sparse-threads))
3221       ;; Find the initial limit.
3222       (if gnus-show-threads
3223           (if show-all
3224               (let ((gnus-newsgroup-dormant nil))
3225                 (gnus-summary-initial-limit show-all))
3226             (gnus-summary-initial-limit show-all))
3227         ;; When untreaded, all articles are always shown.
3228         (setq gnus-newsgroup-limit
3229               (mapcar
3230                (lambda (header) (mail-header-number header))
3231                gnus-newsgroup-headers)))
3232       ;; Generate the summary buffer.
3233       (unless no-display
3234         (gnus-summary-prepare))
3235       (when gnus-use-trees
3236         (gnus-tree-open group)
3237         (setq gnus-summary-highlight-line-function
3238               'gnus-tree-highlight-article))
3239       ;; If the summary buffer is empty, but there are some low-scored
3240       ;; articles or some excluded dormants, we include these in the
3241       ;; buffer.
3242       (when (and (zerop (buffer-size))
3243                  (not no-display))
3244         (cond (gnus-newsgroup-dormant
3245                (gnus-summary-limit-include-dormant))
3246               ((and gnus-newsgroup-scored show-all)
3247                (gnus-summary-limit-include-expunged t))))
3248       ;; Function `gnus-apply-kill-file' must be called in this hook.
3249       (gnus-run-hooks 'gnus-apply-kill-hook)
3250       (if (and (zerop (buffer-size))
3251                (not no-display))
3252           (progn
3253             ;; This newsgroup is empty.
3254             (gnus-summary-catchup-and-exit nil t)
3255             (gnus-message 6 "No unread news")
3256             (when kill-buffer
3257               (gnus-kill-or-deaden-summary kill-buffer))
3258             ;; Return nil from this function.
3259             nil)
3260         ;; Hide conversation thread subtrees.  We cannot do this in
3261         ;; gnus-summary-prepare-hook since kill processing may not
3262         ;; work with hidden articles.
3263         (and gnus-show-threads
3264              gnus-thread-hide-subtree
3265              (gnus-summary-hide-all-threads))
3266         (when kill-buffer
3267           (gnus-kill-or-deaden-summary kill-buffer))
3268         ;; Show first unread article if requested.
3269         (if (and (not no-article)
3270                  (not no-display)
3271                  gnus-newsgroup-unreads
3272                  gnus-auto-select-first)
3273             (progn
3274               (gnus-configure-windows 'summary)
3275               (cond
3276                ((eq gnus-auto-select-first 'best)
3277                 (gnus-summary-best-unread-article))
3278                ((eq gnus-auto-select-first t)
3279                 (gnus-summary-first-unread-article))
3280                ((gnus-functionp gnus-auto-select-first)
3281                 (funcall gnus-auto-select-first))))
3282           ;; Don't select any articles, just move point to the first
3283           ;; article in the group.
3284           (goto-char (point-min))
3285           (gnus-summary-position-point)
3286           (gnus-configure-windows 'summary 'force)
3287           (gnus-set-mode-line 'summary))
3288         (when (get-buffer-window gnus-group-buffer t)
3289           ;; Gotta use windows, because recenter does weird stuff if
3290           ;; the current buffer ain't the displayed window.
3291           (let ((owin (selected-window)))
3292             (select-window (get-buffer-window gnus-group-buffer t))
3293             (when (gnus-group-goto-group group)
3294               (recenter))
3295             (select-window owin)))
3296         ;; Mark this buffer as "prepared".
3297         (setq gnus-newsgroup-prepared t)
3298         (gnus-run-hooks 'gnus-summary-prepared-hook)
3299         t)))))
3300
3301 (defun gnus-summary-prepare ()
3302   "Generate the summary buffer."
3303   (interactive)
3304   (let ((buffer-read-only nil))
3305     (erase-buffer)
3306     (setq gnus-newsgroup-data nil
3307           gnus-newsgroup-data-reverse nil)
3308     (gnus-run-hooks 'gnus-summary-generate-hook)
3309     ;; Generate the buffer, either with threads or without.
3310     (when gnus-newsgroup-headers
3311       (gnus-summary-prepare-threads
3312        (if gnus-show-threads
3313            (gnus-sort-gathered-threads
3314             (funcall gnus-summary-thread-gathering-function
3315                      (gnus-sort-threads
3316                       (gnus-cut-threads (gnus-make-threads)))))
3317          ;; Unthreaded display.
3318          (gnus-sort-articles gnus-newsgroup-headers))))
3319     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3320     ;; Call hooks for modifying summary buffer.
3321     (goto-char (point-min))
3322     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3323
3324 (defsubst gnus-general-simplify-subject (subject)
3325   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3326   (setq subject
3327         (cond
3328          ;; Truncate the subject.
3329          (gnus-simplify-subject-functions
3330           (gnus-map-function gnus-simplify-subject-functions subject))
3331          ((numberp gnus-summary-gather-subject-limit)
3332           (setq subject (gnus-simplify-subject-re subject))
3333           (if (> (length subject) gnus-summary-gather-subject-limit)
3334               (substring subject 0 gnus-summary-gather-subject-limit)
3335             subject))
3336          ;; Fuzzily simplify it.
3337          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3338           (gnus-simplify-subject-fuzzy subject))
3339          ;; Just remove the leading "Re:".
3340          (t
3341           (gnus-simplify-subject-re subject))))
3342
3343   (if (and gnus-summary-gather-exclude-subject
3344            (string-match gnus-summary-gather-exclude-subject subject))
3345       nil                               ; This article shouldn't be gathered
3346     subject))
3347
3348 (defun gnus-summary-simplify-subject-query ()
3349   "Query where the respool algorithm would put this article."
3350   (interactive)
3351   (gnus-summary-select-article)
3352   (message "%s"
3353            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3354
3355 (defun gnus-gather-threads-by-subject (threads)
3356   "Gather threads by looking at Subject headers."
3357   (if (not gnus-summary-make-false-root)
3358       threads
3359     (let ((hashtb (gnus-make-hashtable 1024))
3360           (prev threads)
3361           (result threads)
3362           subject hthread whole-subject)
3363       (while threads
3364         (setq subject (gnus-general-simplify-subject
3365                        (setq whole-subject (mail-header-subject
3366                                             (caar threads)))))
3367         (when subject
3368           (if (setq hthread (gnus-gethash subject hashtb))
3369               (progn
3370                 ;; We enter a dummy root into the thread, if we
3371                 ;; haven't done that already.
3372                 (unless (stringp (caar hthread))
3373                   (setcar hthread (list whole-subject (car hthread))))
3374                 ;; We add this new gathered thread to this gathered
3375                 ;; thread.
3376                 (setcdr (car hthread)
3377                         (nconc (cdar hthread) (list (car threads))))
3378                 ;; Remove it from the list of threads.
3379                 (setcdr prev (cdr threads))
3380                 (setq threads prev))
3381             ;; Enter this thread into the hash table.
3382             (gnus-sethash subject threads hashtb)))
3383         (setq prev threads)
3384         (setq threads (cdr threads)))
3385       result)))
3386
3387 (defun gnus-gather-threads-by-references (threads)
3388   "Gather threads by looking at References headers."
3389   (let ((idhashtb (gnus-make-hashtable 1024))
3390         (thhashtb (gnus-make-hashtable 1024))
3391         (prev threads)
3392         (result threads)
3393         ids references id gthread gid entered ref)
3394     (while threads
3395       (when (setq references (mail-header-references (caar threads)))
3396         (setq id (mail-header-id (caar threads))
3397               ids (gnus-split-references references)
3398               entered nil)
3399         (while (setq ref (pop ids))
3400           (setq ids (delete ref ids))
3401           (if (not (setq gid (gnus-gethash ref idhashtb)))
3402               (progn
3403                 (gnus-sethash ref id idhashtb)
3404                 (gnus-sethash id threads thhashtb))
3405             (setq gthread (gnus-gethash gid thhashtb))
3406             (unless entered
3407               ;; We enter a dummy root into the thread, if we
3408               ;; haven't done that already.
3409               (unless (stringp (caar gthread))
3410                 (setcar gthread (list (mail-header-subject (caar gthread))
3411                                       (car gthread))))
3412               ;; We add this new gathered thread to this gathered
3413               ;; thread.
3414               (setcdr (car gthread)
3415                       (nconc (cdar gthread) (list (car threads)))))
3416             ;; Add it into the thread hash table.
3417             (gnus-sethash id gthread thhashtb)
3418             (setq entered t)
3419             ;; Remove it from the list of threads.
3420             (setcdr prev (cdr threads))
3421             (setq threads prev))))
3422       (setq prev threads)
3423       (setq threads (cdr threads)))
3424     result))
3425
3426 (defun gnus-sort-gathered-threads (threads)
3427   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3428   (let ((result threads))
3429     (while threads
3430       (when (stringp (caar threads))
3431         (setcdr (car threads)
3432                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3433       (setq threads (cdr threads)))
3434     result))
3435
3436 (defun gnus-thread-loop-p (root thread)
3437   "Say whether ROOT is in THREAD."
3438   (let ((stack (list thread))
3439         (infloop 0)
3440         th)
3441     (while (setq thread (pop stack))
3442       (setq th (cdr thread))
3443       (while (and th
3444                   (not (eq (caar th) root)))
3445         (pop th))
3446       (if th
3447           ;; We have found a loop.
3448           (let (ref-dep)
3449             (setcdr thread (delq (car th) (cdr thread)))
3450             (if (boundp (setq ref-dep (intern "none"
3451                                               gnus-newsgroup-dependencies)))
3452                 (setcdr (symbol-value ref-dep)
3453                         (nconc (cdr (symbol-value ref-dep))
3454                                (list (car th))))
3455               (set ref-dep (list nil (car th))))
3456             (setq infloop 1
3457                   stack nil))
3458         ;; Push all the subthreads onto the stack.
3459         (push (cdr thread) stack)))
3460     infloop))
3461
3462 (defun gnus-make-threads ()
3463   "Go through the dependency hashtb and find the roots.  Return all threads."
3464   (let (threads)
3465     (while (catch 'infloop
3466              (mapatoms
3467               (lambda (refs)
3468                 ;; Deal with self-referencing References loops.
3469                 (when (and (car (symbol-value refs))
3470                            (not (zerop
3471                                  (apply
3472                                   '+
3473                                   (mapcar
3474                                    (lambda (thread)
3475                                      (gnus-thread-loop-p
3476                                       (car (symbol-value refs)) thread))
3477                                    (cdr (symbol-value refs)))))))
3478                   (setq threads nil)
3479                   (throw 'infloop t))
3480                 (unless (car (symbol-value refs))
3481                   ;; These threads do not refer back to any other articles,
3482                   ;; so they're roots.
3483                   (setq threads (append (cdr (symbol-value refs)) threads))))
3484               gnus-newsgroup-dependencies)))
3485     threads))
3486
3487 ;; Build the thread tree.
3488 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3489   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3490
3491 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3492 if it was already present.
3493
3494 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3495 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3496 Message-IDs will be renamed be renamed to a unique Message-ID before
3497 being entered.
3498
3499 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3500   (let* ((id (mail-header-id header))
3501          (id-dep (and id (intern id dependencies)))
3502          ref ref-dep ref-header)
3503     ;; Enter this `header' in the `dependencies' table.
3504     (cond
3505      ((not id-dep)
3506       (setq header nil))
3507      ;; The first two cases do the normal part: enter a new `header'
3508      ;; in the `dependencies' table.
3509      ((not (boundp id-dep))
3510       (set id-dep (list header)))
3511      ((null (car (symbol-value id-dep)))
3512       (setcar (symbol-value id-dep) header))
3513
3514      ;; From here the `header' was already present in the
3515      ;; `dependencies' table.
3516      (force-new
3517       ;; Overrides an existing entry;
3518       ;; just set the header part of the entry.
3519       (setcar (symbol-value id-dep) header))
3520
3521      ;; Renames the existing `header' to a unique Message-ID.
3522      ((not gnus-summary-ignore-duplicates)
3523       ;; An article with this Message-ID has already been seen.
3524       ;; We rename the Message-ID.
3525       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3526            (list header))
3527       (mail-header-set-id header id))
3528
3529      ;; The last case ignores an existing entry, except it adds any
3530      ;; additional Xrefs (in case the two articles came from different
3531      ;; servers.
3532      ;; Also sets `header' to `nil' meaning that the `dependencies'
3533      ;; table was *not* modified.
3534      (t
3535       (mail-header-set-xref
3536        (car (symbol-value id-dep))
3537        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3538                    "")
3539                (or (mail-header-xref header) "")))
3540       (setq header nil)))
3541
3542     (when header
3543       ;; First check if that we are not creating a References loop.
3544       (setq ref (gnus-parent-id (mail-header-references header)))
3545       (while (and ref
3546                   (setq ref-dep (intern-soft ref dependencies))
3547                   (boundp ref-dep)
3548                   (setq ref-header (car (symbol-value ref-dep))))
3549         (if (string= id ref)
3550             ;; Yuk!  This is a reference loop.  Make the article be a
3551             ;; root article.
3552             (progn
3553               (mail-header-set-references (car (symbol-value id-dep)) "none")
3554               (setq ref nil))
3555           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3556       (setq ref (gnus-parent-id (mail-header-references header)))
3557       (setq ref-dep (intern (or ref "none") dependencies))
3558       (if (boundp ref-dep)
3559           (setcdr (symbol-value ref-dep)
3560                   (nconc (cdr (symbol-value ref-dep))
3561                          (list (symbol-value id-dep))))
3562         (set ref-dep (list nil (symbol-value id-dep)))))
3563     header))
3564
3565 (defun gnus-build-sparse-threads ()
3566   (let ((headers gnus-newsgroup-headers)
3567         (mail-parse-charset gnus-newsgroup-charset)
3568         (gnus-summary-ignore-duplicates t)
3569         header references generation relations
3570         subject child end new-child date)
3571     ;; First we create an alist of generations/relations, where
3572     ;; generations is how much we trust the relation, and the relation
3573     ;; is parent/child.
3574     (gnus-message 7 "Making sparse threads...")
3575     (save-excursion
3576       (nnheader-set-temp-buffer " *gnus sparse threads*")
3577       (while (setq header (pop headers))
3578         (when (and (setq references (mail-header-references header))
3579                    (not (string= references "")))
3580           (insert references)
3581           (setq child (mail-header-id header)
3582                 subject (mail-header-subject header)
3583                 date (mail-header-date header)
3584                 generation 0)
3585           (while (search-backward ">" nil t)
3586             (setq end (1+ (point)))
3587             (when (search-backward "<" nil t)
3588               (setq new-child (buffer-substring (point) end))
3589               (push (list (incf generation)
3590                           child (setq child new-child)
3591                           subject date)
3592                     relations)))
3593           (when child
3594             (push (list (1+ generation) child nil subject) relations))
3595           (erase-buffer)))
3596       (kill-buffer (current-buffer)))
3597     ;; Sort over trustworthiness.
3598     (mapcar
3599      (lambda (relation)
3600        (when (gnus-dependencies-add-header
3601               (make-full-mail-header-from-decoded-header
3602                gnus-reffed-article-number
3603                (nth 3 relation) "" (or (nth 4 relation) "")
3604                (nth 1 relation)
3605                (or (nth 2 relation) "") 0 0 "")
3606               gnus-newsgroup-dependencies nil)
3607          (push gnus-reffed-article-number gnus-newsgroup-limit)
3608          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3609          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3610                gnus-newsgroup-reads)
3611          (decf gnus-reffed-article-number)))
3612      (sort relations 'car-less-than-car))
3613     (gnus-message 7 "Making sparse threads...done")))
3614
3615 (defun gnus-build-old-threads ()
3616   ;; Look at all the articles that refer back to old articles, and
3617   ;; fetch the headers for the articles that aren't there.  This will
3618   ;; build complete threads - if the roots haven't been expired by the
3619   ;; server, that is.
3620   (let ((mail-parse-charset gnus-newsgroup-charset)
3621         id heads)
3622     (mapatoms
3623      (lambda (refs)
3624        (when (not (car (symbol-value refs)))
3625          (setq heads (cdr (symbol-value refs)))
3626          (while heads
3627            (if (memq (mail-header-number (caar heads))
3628                      gnus-newsgroup-dormant)
3629                (setq heads (cdr heads))
3630              (setq id (symbol-name refs))
3631              (while (and (setq id (gnus-build-get-header id))
3632                          (not (car (gnus-id-to-thread id)))))
3633              (setq heads nil)))))
3634      gnus-newsgroup-dependencies)))
3635
3636 ;; This function has to be called with point after the article number
3637 ;; on the beginning of the line.
3638 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3639   (let ((eol (gnus-point-at-eol))
3640         (buffer (current-buffer))
3641         header)
3642
3643     ;; overview: [num subject from date id refs chars lines misc]
3644     (unwind-protect
3645         (progn
3646           (narrow-to-region (point) eol)
3647           (unless (eobp)
3648             (forward-char))
3649
3650           (setq header
3651                 (make-full-mail-header
3652                  number                         ; number
3653                  (nnheader-nov-field)           ; subject
3654                  (nnheader-nov-field)           ; from
3655                  (nnheader-nov-field)           ; date
3656                  (nnheader-nov-read-message-id) ; id
3657                  (nnheader-nov-field)           ; refs
3658                  (nnheader-nov-read-integer)    ; chars
3659                  (nnheader-nov-read-integer)    ; lines
3660                  (unless (eobp)
3661                    (if (looking-at "Xref: ")
3662                        (goto-char (match-end 0)))
3663                    (nnheader-nov-field))        ; Xref
3664                  (nnheader-nov-parse-extra))))  ; extra
3665
3666       (widen))
3667
3668     (when gnus-alter-header-function
3669       (funcall gnus-alter-header-function header))
3670     (gnus-dependencies-add-header header dependencies force-new)))
3671
3672 (defun gnus-build-get-header (id)
3673   "Look through the buffer of NOV lines and find the header to ID.
3674 Enter this line into the dependencies hash table, and return
3675 the id of the parent article (if any)."
3676   (let ((deps gnus-newsgroup-dependencies)
3677         found header)
3678     (prog1
3679         (save-excursion
3680           (set-buffer nntp-server-buffer)
3681           (let ((case-fold-search nil))
3682             (goto-char (point-min))
3683             (while (and (not found)
3684                         (search-forward id nil t))
3685               (beginning-of-line)
3686               (setq found (looking-at
3687                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3688                                    (regexp-quote id))))
3689               (or found (beginning-of-line 2)))
3690             (when found
3691               (beginning-of-line)
3692               (and
3693                (setq header (gnus-nov-parse-line
3694                              (read (current-buffer)) deps))
3695                (gnus-parent-id (mail-header-references header))))))
3696       (when header
3697         (let ((number (mail-header-number header)))
3698           (push number gnus-newsgroup-limit)
3699           (push header gnus-newsgroup-headers)
3700           (if (memq number gnus-newsgroup-unselected)
3701               (progn
3702                 (push number gnus-newsgroup-unreads)
3703                 (setq gnus-newsgroup-unselected
3704                       (delq number gnus-newsgroup-unselected)))
3705             (push number gnus-newsgroup-ancient)))))))
3706
3707 (defun gnus-build-all-threads ()
3708   "Read all the headers."
3709   (let ((gnus-summary-ignore-duplicates t)
3710         (mail-parse-charset gnus-newsgroup-charset)
3711         (dependencies gnus-newsgroup-dependencies)
3712         header article)
3713     (save-excursion
3714       (set-buffer nntp-server-buffer)
3715       (let ((case-fold-search nil))
3716         (goto-char (point-min))
3717         (while (not (eobp))
3718           (ignore-errors
3719             (setq article (read (current-buffer))
3720                   header (gnus-nov-parse-line article dependencies)))
3721           (when header
3722             (save-excursion
3723               (set-buffer gnus-summary-buffer)
3724               (push header gnus-newsgroup-headers)
3725               (if (memq (setq article (mail-header-number header))
3726                         gnus-newsgroup-unselected)
3727                   (progn
3728                     (push article gnus-newsgroup-unreads)
3729                     (setq gnus-newsgroup-unselected
3730                           (delq article gnus-newsgroup-unselected)))
3731                 (push article gnus-newsgroup-ancient)))
3732             (forward-line 1)))))))
3733
3734 (defun gnus-summary-update-article-line (article header)
3735   "Update the line for ARTICLE using HEADERS."
3736   (let* ((id (mail-header-id header))
3737          (thread (gnus-id-to-thread id)))
3738     (unless thread
3739       (error "Article in no thread"))
3740     ;; Update the thread.
3741     (setcar thread header)
3742     (gnus-summary-goto-subject article)
3743     (let* ((datal (gnus-data-find-list article))
3744            (data (car datal))
3745            (length (when (cdr datal)
3746                      (- (gnus-data-pos data)
3747                         (gnus-data-pos (cadr datal)))))
3748            (buffer-read-only nil)
3749            (level (gnus-summary-thread-level)))
3750       (gnus-delete-line)
3751       (gnus-summary-insert-line
3752        header level nil (gnus-article-mark article)
3753        (memq article gnus-newsgroup-replied)
3754        (memq article gnus-newsgroup-expirable)
3755        ;; Only insert the Subject string when it's different
3756        ;; from the previous Subject string.
3757        (if (and
3758             gnus-show-threads
3759             (gnus-subject-equal
3760              (condition-case ()
3761                  (mail-header-subject
3762                   (gnus-data-header
3763                    (cadr
3764                     (gnus-data-find-list
3765                      article
3766                      (gnus-data-list t)))))
3767                ;; Error on the side of excessive subjects.
3768                (error ""))
3769              (mail-header-subject header)))
3770            ""
3771          (mail-header-subject header))
3772        nil (cdr (assq article gnus-newsgroup-scored))
3773        (memq article gnus-newsgroup-processable))
3774       (when length
3775         (gnus-data-update-list
3776          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3777
3778 (defun gnus-summary-update-article (article &optional iheader)
3779   "Update ARTICLE in the summary buffer."
3780   (set-buffer gnus-summary-buffer)
3781   (let* ((header (gnus-summary-article-header article))
3782          (id (mail-header-id header))
3783          (data (gnus-data-find article))
3784          (thread (gnus-id-to-thread id))
3785          (references (mail-header-references header))
3786          (parent
3787           (gnus-id-to-thread
3788            (or (gnus-parent-id
3789                 (when (and references
3790                            (not (equal "" references)))
3791                   references))
3792                "none")))
3793          (buffer-read-only nil)
3794          (old (car thread)))
3795     (when thread
3796       (unless iheader
3797         (setcar thread nil)
3798         (when parent
3799           (delq thread parent)))
3800       (if (gnus-summary-insert-subject id header)
3801           ;; Set the (possibly) new article number in the data structure.
3802           (gnus-data-set-number data (gnus-id-to-article id))
3803         (setcar thread old)
3804         nil))))
3805
3806 (defun gnus-rebuild-thread (id &optional line)
3807   "Rebuild the thread containing ID.
3808 If LINE, insert the rebuilt thread starting on line LINE."
3809   (let ((buffer-read-only nil)
3810         old-pos current thread data)
3811     (if (not gnus-show-threads)
3812         (setq thread (list (car (gnus-id-to-thread id))))
3813       ;; Get the thread this article is part of.
3814       (setq thread (gnus-remove-thread id)))
3815     (setq old-pos (gnus-point-at-bol))
3816     (setq current (save-excursion
3817                     (and (re-search-backward "[\r\n]" nil t)
3818                          (gnus-summary-article-number))))
3819     ;; If this is a gathered thread, we have to go some re-gathering.
3820     (when (stringp (car thread))
3821       (let ((subject (car thread))
3822             roots thr)
3823         (setq thread (cdr thread))
3824         (while thread
3825           (unless (memq (setq thr (gnus-id-to-thread
3826                                    (gnus-root-id
3827                                     (mail-header-id (caar thread)))))
3828                         roots)
3829             (push thr roots))
3830           (setq thread (cdr thread)))
3831         ;; We now have all (unique) roots.
3832         (if (= (length roots) 1)
3833             ;; All the loose roots are now one solid root.
3834             (setq thread (car roots))
3835           (setq thread (cons subject (gnus-sort-threads roots))))))
3836     (let (threads)
3837       ;; We then insert this thread into the summary buffer.
3838       (when line
3839         (goto-char (point-min))
3840         (forward-line (1- line)))
3841       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3842         (if gnus-show-threads
3843             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3844           (gnus-summary-prepare-unthreaded thread))
3845         (setq data (nreverse gnus-newsgroup-data))
3846         (setq threads gnus-newsgroup-threads))
3847       ;; We splice the new data into the data structure.
3848       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3849       ;;!!! then we want to insert at the beginning of the buffer.
3850       ;;!!! That happens to be true with Gnus now, but that may
3851       ;;!!! change in the future.  Perhaps.
3852       (gnus-data-enter-list
3853        (if line nil current) data (- (point) old-pos))
3854       (setq gnus-newsgroup-threads
3855             (nconc threads gnus-newsgroup-threads))
3856       (gnus-data-compute-positions))))
3857
3858 (defun gnus-number-to-header (number)
3859   "Return the header for article NUMBER."
3860   (let ((headers gnus-newsgroup-headers))
3861     (while (and headers
3862                 (not (= number (mail-header-number (car headers)))))
3863       (pop headers))
3864     (when headers
3865       (car headers))))
3866
3867 (defun gnus-parent-headers (in-headers &optional generation)
3868   "Return the headers of the GENERATIONeth parent of HEADERS."
3869   (unless generation
3870     (setq generation 1))
3871   (let ((parent t)
3872         (headers in-headers)
3873         references)
3874     (while (and parent
3875                 (not (zerop generation))
3876                 (setq references (mail-header-references headers)))
3877       (setq headers (if (and references
3878                              (setq parent (gnus-parent-id references)))
3879                         (car (gnus-id-to-thread parent))
3880                       nil))
3881       (decf generation))
3882     (and (not (eq headers in-headers))
3883          headers)))
3884
3885 (defun gnus-id-to-thread (id)
3886   "Return the (sub-)thread where ID appears."
3887   (gnus-gethash id gnus-newsgroup-dependencies))
3888
3889 (defun gnus-id-to-article (id)
3890   "Return the article number of ID."
3891   (let ((thread (gnus-id-to-thread id)))
3892     (when (and thread
3893                (car thread))
3894       (mail-header-number (car thread)))))
3895
3896 (defun gnus-id-to-header (id)
3897   "Return the article headers of ID."
3898   (car (gnus-id-to-thread id)))
3899
3900 (defun gnus-article-displayed-root-p (article)
3901   "Say whether ARTICLE is a root(ish) article."
3902   (let ((level (gnus-summary-thread-level article))
3903         (refs (mail-header-references  (gnus-summary-article-header article)))
3904         particle)
3905     (cond
3906      ((null level) nil)
3907      ((zerop level) t)
3908      ((null refs) t)
3909      ((null (gnus-parent-id refs)) t)
3910      ((and (= 1 level)
3911            (null (setq particle (gnus-id-to-article
3912                                  (gnus-parent-id refs))))
3913            (null (gnus-summary-thread-level particle)))))))
3914
3915 (defun gnus-root-id (id)
3916   "Return the id of the root of the thread where ID appears."
3917   (let (last-id prev)
3918     (while (and id (setq prev (car (gnus-id-to-thread id))))
3919       (setq last-id id
3920             id (gnus-parent-id (mail-header-references prev))))
3921     last-id))
3922
3923 (defun gnus-articles-in-thread (thread)
3924   "Return the list of articles in THREAD."
3925   (cons (mail-header-number (car thread))
3926         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3927
3928 (defun gnus-remove-thread (id &optional dont-remove)
3929   "Remove the thread that has ID in it."
3930   (let (headers thread last-id)
3931     ;; First go up in this thread until we find the root.
3932     (setq last-id (gnus-root-id id)
3933           headers (message-flatten-list (gnus-id-to-thread last-id)))
3934     ;; We have now found the real root of this thread.  It might have
3935     ;; been gathered into some loose thread, so we have to search
3936     ;; through the threads to find the thread we wanted.
3937     (let ((threads gnus-newsgroup-threads)
3938           sub)
3939       (while threads
3940         (setq sub (car threads))
3941         (if (stringp (car sub))
3942             ;; This is a gathered thread, so we look at the roots
3943             ;; below it to find whether this article is in this
3944             ;; gathered root.
3945             (progn
3946               (setq sub (cdr sub))
3947               (while sub
3948                 (when (member (caar sub) headers)
3949                   (setq thread (car threads)
3950                         threads nil
3951                         sub nil))
3952                 (setq sub (cdr sub))))
3953           ;; It's an ordinary thread, so we check it.
3954           (when (eq (car sub) (car headers))
3955             (setq thread sub
3956                   threads nil)))
3957         (setq threads (cdr threads)))
3958       ;; If this article is in no thread, then it's a root.
3959       (if thread
3960           (unless dont-remove
3961             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3962         (setq thread (gnus-id-to-thread last-id)))
3963       (when thread
3964         (prog1
3965             thread                      ; We return this thread.
3966           (unless dont-remove
3967             (if (stringp (car thread))
3968                 (progn
3969                   ;; If we use dummy roots, then we have to remove the
3970                   ;; dummy root as well.
3971                   (when (eq gnus-summary-make-false-root 'dummy)
3972                     ;; We go to the dummy root by going to
3973                     ;; the first sub-"thread", and then one line up.
3974                     (gnus-summary-goto-article
3975                      (mail-header-number (caadr thread)))
3976                     (forward-line -1)
3977                     (gnus-delete-line)
3978                     (gnus-data-compute-positions))
3979                   (setq thread (cdr thread))
3980                   (while thread
3981                     (gnus-remove-thread-1 (car thread))
3982                     (setq thread (cdr thread))))
3983               (gnus-remove-thread-1 thread))))))))
3984
3985 (defun gnus-remove-thread-1 (thread)
3986   "Remove the thread THREAD recursively."
3987   (let ((number (mail-header-number (pop thread)))
3988         d)
3989     (setq thread (reverse thread))
3990     (while thread
3991       (gnus-remove-thread-1 (pop thread)))
3992     (when (setq d (gnus-data-find number))
3993       (goto-char (gnus-data-pos d))
3994       (gnus-summary-show-thread)
3995       (gnus-data-remove
3996        number
3997        (- (gnus-point-at-bol)
3998           (prog1
3999               (1+ (gnus-point-at-eol))
4000             (gnus-delete-line)))))))
4001
4002 (defun gnus-sort-threads-1 (threads func)
4003   (sort (mapcar (lambda (thread)
4004                   (cons (car thread)
4005                         (and (cdr thread)
4006                              (gnus-sort-threads-1 (cdr thread) func))))
4007                 threads) func))
4008
4009 (defun gnus-sort-threads (threads)
4010   "Sort THREADS."
4011   (if (not gnus-thread-sort-functions)
4012       threads
4013     (gnus-message 8 "Sorting threads...")
4014     (prog1
4015         (gnus-sort-threads-1
4016          threads
4017          (gnus-make-sort-function gnus-thread-sort-functions))
4018       (gnus-message 8 "Sorting threads...done"))))
4019
4020 (defun gnus-sort-articles (articles)
4021   "Sort ARTICLES."
4022   (when gnus-article-sort-functions
4023     (gnus-message 7 "Sorting articles...")
4024     (prog1
4025         (setq gnus-newsgroup-headers
4026               (sort articles (gnus-make-sort-function
4027                               gnus-article-sort-functions)))
4028       (gnus-message 7 "Sorting articles...done"))))
4029
4030 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4031 (defmacro gnus-thread-header (thread)
4032   "Return header of first article in THREAD.
4033 Note that THREAD must never, ever be anything else than a variable -
4034 using some other form will lead to serious barfage."
4035   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4036   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4037   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4038         (vector thread) 2))
4039
4040 (defsubst gnus-article-sort-by-number (h1 h2)
4041   "Sort articles by article number."
4042   (< (mail-header-number h1)
4043      (mail-header-number h2)))
4044
4045 (defun gnus-thread-sort-by-number (h1 h2)
4046   "Sort threads by root article number."
4047   (gnus-article-sort-by-number
4048    (gnus-thread-header h1) (gnus-thread-header h2)))
4049
4050 (defsubst gnus-article-sort-by-lines (h1 h2)
4051   "Sort articles by article Lines header."
4052   (< (mail-header-lines h1)
4053      (mail-header-lines h2)))
4054
4055 (defun gnus-thread-sort-by-lines (h1 h2)
4056   "Sort threads by root article Lines header."
4057   (gnus-article-sort-by-lines
4058    (gnus-thread-header h1) (gnus-thread-header h2)))
4059
4060 (defsubst gnus-article-sort-by-chars (h1 h2)
4061   "Sort articles by octet length."
4062   (< (mail-header-chars h1)
4063      (mail-header-chars h2)))
4064
4065 (defun gnus-thread-sort-by-chars (h1 h2)
4066   "Sort threads by root article octet length."
4067   (gnus-article-sort-by-chars
4068    (gnus-thread-header h1) (gnus-thread-header h2)))
4069
4070 (defsubst gnus-article-sort-by-author (h1 h2)
4071   "Sort articles by root author."
4072   (string-lessp
4073    (let ((addr (car (mime-entity-read-field h1 'From))))
4074      (or (std11-full-name-string addr)
4075          (std11-address-string addr)
4076          ""))
4077    (let ((addr (car (mime-entity-read-field h2 'From))))
4078      (or (std11-full-name-string addr)
4079          (std11-address-string addr)
4080          ""))
4081    ))
4082
4083 (defun gnus-thread-sort-by-author (h1 h2)
4084   "Sort threads by root author."
4085   (gnus-article-sort-by-author
4086    (gnus-thread-header h1)  (gnus-thread-header h2)))
4087
4088 (defsubst gnus-article-sort-by-subject (h1 h2)
4089   "Sort articles by root subject."
4090   (string-lessp
4091    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4092    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4093
4094 (defun gnus-thread-sort-by-subject (h1 h2)
4095   "Sort threads by root subject."
4096   (gnus-article-sort-by-subject
4097    (gnus-thread-header h1) (gnus-thread-header h2)))
4098
4099 (defsubst gnus-article-sort-by-date (h1 h2)
4100   "Sort articles by root article date."
4101   (time-less-p
4102    (gnus-date-get-time (mail-header-date h1))
4103    (gnus-date-get-time (mail-header-date h2))))
4104
4105 (defun gnus-thread-sort-by-date (h1 h2)
4106   "Sort threads by root article date."
4107   (gnus-article-sort-by-date
4108    (gnus-thread-header h1) (gnus-thread-header h2)))
4109
4110 (defsubst gnus-article-sort-by-score (h1 h2)
4111   "Sort articles by root article score.
4112 Unscored articles will be counted as having a score of zero."
4113   (> (or (cdr (assq (mail-header-number h1)
4114                     gnus-newsgroup-scored))
4115          gnus-summary-default-score 0)
4116      (or (cdr (assq (mail-header-number h2)
4117                     gnus-newsgroup-scored))
4118          gnus-summary-default-score 0)))
4119
4120 (defun gnus-thread-sort-by-score (h1 h2)
4121   "Sort threads by root article score."
4122   (gnus-article-sort-by-score
4123    (gnus-thread-header h1) (gnus-thread-header h2)))
4124
4125 (defun gnus-thread-sort-by-total-score (h1 h2)
4126   "Sort threads by the sum of all scores in the thread.
4127 Unscored articles will be counted as having a score of zero."
4128   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4129
4130 (defun gnus-thread-total-score (thread)
4131   ;; This function find the total score of THREAD.
4132   (cond ((null thread)
4133          0)
4134         ((consp thread)
4135          (if (stringp (car thread))
4136              (apply gnus-thread-score-function 0
4137                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4138            (gnus-thread-total-score-1 thread)))
4139         (t
4140          (gnus-thread-total-score-1 (list thread)))))
4141
4142 (defun gnus-thread-total-score-1 (root)
4143   ;; This function find the total score of the thread below ROOT.
4144   (setq root (car root))
4145   (apply gnus-thread-score-function
4146          (or (append
4147               (mapcar 'gnus-thread-total-score
4148                       (cdr (gnus-id-to-thread (mail-header-id root))))
4149               (when (> (mail-header-number root) 0)
4150                 (list (or (cdr (assq (mail-header-number root)
4151                                      gnus-newsgroup-scored))
4152                           gnus-summary-default-score 0))))
4153              (list gnus-summary-default-score)
4154              '(0))))
4155
4156 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4157 (defvar gnus-tmp-prev-subject nil)
4158 (defvar gnus-tmp-false-parent nil)
4159 (defvar gnus-tmp-root-expunged nil)
4160 (defvar gnus-tmp-dummy-line nil)
4161
4162 (eval-when-compile (defvar gnus-tmp-header))
4163 (defun gnus-extra-header (type &optional header)
4164   "Return the extra header of TYPE."
4165   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4166       ""))
4167
4168 (defvar gnus-tmp-thread-tree-header-string "")
4169
4170 (defvar gnus-sum-thread-tree-root "> "
4171   "With %B spec, used for the root of a thread.
4172 If nil, use subject instead.")
4173 (defvar gnus-sum-thread-tree-single-indent ""
4174   "With %B spec, used for a thread with just one message.
4175 If nil, use subject instead.")
4176 (defvar gnus-sum-thread-tree-vertical "| "
4177   "With %B spec, used for drawing a vertical line.")
4178 (defvar gnus-sum-thread-tree-indent "  "
4179   "With %B spec, used for indenting.")
4180 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4181   "With %B spec, used for a leaf with brothers.")
4182 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4183   "With %B spec, used for a leaf without brothers.")
4184
4185 (defun gnus-summary-prepare-threads (threads)
4186   "Prepare summary buffer from THREADS and indentation LEVEL.
4187 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4188 or a straight list of headers."
4189   (gnus-message 7 "Generating summary...")
4190
4191   (setq gnus-newsgroup-threads threads)
4192   (beginning-of-line)
4193
4194   (let ((gnus-tmp-level 0)
4195         (default-score (or gnus-summary-default-score 0))
4196         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4197         thread number subject stack state gnus-tmp-gathered beg-match
4198         new-roots gnus-tmp-new-adopts thread-end
4199         gnus-tmp-header gnus-tmp-unread
4200         gnus-tmp-replied gnus-tmp-subject-or-nil
4201         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4202         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4203         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4204         tree-stack)
4205
4206     (setq gnus-tmp-prev-subject nil)
4207
4208     (if (vectorp (car threads))
4209         ;; If this is a straight (sic) list of headers, then a
4210         ;; threaded summary display isn't required, so we just create
4211         ;; an unthreaded one.
4212         (gnus-summary-prepare-unthreaded threads)
4213
4214       ;; Do the threaded display.
4215
4216       (while (or threads stack gnus-tmp-new-adopts new-roots)
4217
4218         (if (and (= gnus-tmp-level 0)
4219                  (or (not stack)
4220                      (= (caar stack) 0))
4221                  (not gnus-tmp-false-parent)
4222                  (or gnus-tmp-new-adopts new-roots))
4223             (if gnus-tmp-new-adopts
4224                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4225                       thread (list (car gnus-tmp-new-adopts))
4226                       gnus-tmp-header (caar thread)
4227                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4228               (when new-roots
4229                 (setq thread (list (car new-roots))
4230                       gnus-tmp-header (caar thread)
4231                       new-roots (cdr new-roots))))
4232
4233           (if threads
4234               ;; If there are some threads, we do them before the
4235               ;; threads on the stack.
4236               (setq thread threads
4237                     gnus-tmp-header (caar thread))
4238             ;; There were no current threads, so we pop something off
4239             ;; the stack.
4240             (setq state (car stack)
4241                   gnus-tmp-level (car state)
4242                   tree-stack (cadr state)
4243                   thread (caddr state)
4244                   stack (cdr stack)
4245                   gnus-tmp-header (caar thread))))
4246
4247         (setq gnus-tmp-false-parent nil)
4248         (setq gnus-tmp-root-expunged nil)
4249         (setq thread-end nil)
4250
4251         (if (stringp gnus-tmp-header)
4252             ;; The header is a dummy root.
4253             (cond
4254              ((eq gnus-summary-make-false-root 'adopt)
4255               ;; We let the first article adopt the rest.
4256               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4257                                                (cddar thread)))
4258               (setq gnus-tmp-gathered
4259                     (nconc (mapcar
4260                             (lambda (h) (mail-header-number (car h)))
4261                             (cddar thread))
4262                            gnus-tmp-gathered))
4263               (setq thread (cons (list (caar thread)
4264                                        (cadar thread))
4265                                  (cdr thread)))
4266               (setq gnus-tmp-level -1
4267                     gnus-tmp-false-parent t))
4268              ((eq gnus-summary-make-false-root 'empty)
4269               ;; We print adopted articles with empty subject fields.
4270               (setq gnus-tmp-gathered
4271                     (nconc (mapcar
4272                             (lambda (h) (mail-header-number (car h)))
4273                             (cddar thread))
4274                            gnus-tmp-gathered))
4275               (setq gnus-tmp-level -1))
4276              ((eq gnus-summary-make-false-root 'dummy)
4277               ;; We remember that we probably want to output a dummy
4278               ;; root.
4279               (setq gnus-tmp-dummy-line gnus-tmp-header)
4280               (setq gnus-tmp-prev-subject gnus-tmp-header))
4281              (t
4282               ;; We do not make a root for the gathered
4283               ;; sub-threads at all.
4284               (setq gnus-tmp-level -1)))
4285
4286           (setq number (mail-header-number gnus-tmp-header)
4287                 subject (mail-header-subject gnus-tmp-header))
4288
4289           (cond
4290            ;; If the thread has changed subject, we might want to make
4291            ;; this subthread into a root.
4292            ((and (null gnus-thread-ignore-subject)
4293                  (not (zerop gnus-tmp-level))
4294                  gnus-tmp-prev-subject
4295                  (not (inline
4296                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4297             (setq new-roots (nconc new-roots (list (car thread)))
4298                   thread-end t
4299                   gnus-tmp-header nil))
4300            ;; If the article lies outside the current limit,
4301            ;; then we do not display it.
4302            ((not (memq number gnus-newsgroup-limit))
4303             (setq gnus-tmp-gathered
4304                   (nconc (mapcar
4305                           (lambda (h) (mail-header-number (car h)))
4306                           (cdar thread))
4307                          gnus-tmp-gathered))
4308             (setq gnus-tmp-new-adopts (if (cdar thread)
4309                                           (append gnus-tmp-new-adopts
4310                                                   (cdar thread))
4311                                         gnus-tmp-new-adopts)
4312                   thread-end t
4313                   gnus-tmp-header nil)
4314             (when (zerop gnus-tmp-level)
4315               (setq gnus-tmp-root-expunged t)))
4316            ;; Perhaps this article is to be marked as read?
4317            ((and gnus-summary-mark-below
4318                  (< (or (cdr (assq number gnus-newsgroup-scored))
4319                         default-score)
4320                     gnus-summary-mark-below)
4321                  ;; Don't touch sparse articles.
4322                  (not (gnus-summary-article-sparse-p number))
4323                  (not (gnus-summary-article-ancient-p number)))
4324             (setq gnus-newsgroup-unreads
4325                   (delq number gnus-newsgroup-unreads))
4326             (if gnus-newsgroup-auto-expire
4327                 (push number gnus-newsgroup-expirable)
4328               (push (cons number gnus-low-score-mark)
4329                     gnus-newsgroup-reads))))
4330
4331           (when gnus-tmp-header
4332             ;; We may have an old dummy line to output before this
4333             ;; article.
4334             (when (and gnus-tmp-dummy-line
4335                        (gnus-subject-equal
4336                         gnus-tmp-dummy-line
4337                         (mail-header-subject gnus-tmp-header)))
4338               (gnus-summary-insert-dummy-line
4339                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4340               (setq gnus-tmp-dummy-line nil))
4341
4342             ;; Compute the mark.
4343             (setq gnus-tmp-unread (gnus-article-mark number))
4344
4345             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4346                                   gnus-tmp-header gnus-tmp-level)
4347                   gnus-newsgroup-data)
4348
4349             ;; Actually insert the line.
4350             (setq
4351              gnus-tmp-subject-or-nil
4352              (cond
4353               ((and gnus-thread-ignore-subject
4354                     gnus-tmp-prev-subject
4355                     (not (inline (gnus-subject-equal
4356                                   gnus-tmp-prev-subject subject))))
4357                subject)
4358               ((zerop gnus-tmp-level)
4359                (if (and (eq gnus-summary-make-false-root 'empty)
4360                         (memq number gnus-tmp-gathered)
4361                         gnus-tmp-prev-subject
4362                         (inline (gnus-subject-equal
4363                                  gnus-tmp-prev-subject subject)))
4364                    gnus-summary-same-subject
4365                  subject))
4366               (t gnus-summary-same-subject)))
4367             (if (and (eq gnus-summary-make-false-root 'adopt)
4368                      (= gnus-tmp-level 1)
4369                      (memq number gnus-tmp-gathered))
4370                 (setq gnus-tmp-opening-bracket ?\<
4371                       gnus-tmp-closing-bracket ?\>)
4372               (setq gnus-tmp-opening-bracket ?\[
4373                     gnus-tmp-closing-bracket ?\]))
4374             (setq
4375              gnus-tmp-indentation
4376              (aref gnus-thread-indent-array gnus-tmp-level)
4377              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4378              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4379                                 gnus-summary-default-score 0)
4380              gnus-tmp-score-char
4381              (if (or (null gnus-summary-default-score)
4382                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4383                          gnus-summary-zcore-fuzz))
4384                  ?\ ;;;Whitespace
4385                (if (< gnus-tmp-score gnus-summary-default-score)
4386                    gnus-score-below-mark gnus-score-over-mark))
4387              gnus-tmp-replied
4388              (cond ((memq number gnus-newsgroup-processable)
4389                     gnus-process-mark)
4390                    ((memq number gnus-newsgroup-cached)
4391                     gnus-cached-mark)
4392                    ((memq number gnus-newsgroup-replied)
4393                     gnus-replied-mark)
4394                    ((memq number gnus-newsgroup-forwarded)
4395                     gnus-forwarded-mark)
4396                    ((memq number gnus-newsgroup-saved)
4397                     gnus-saved-mark)
4398                    ((memq number gnus-newsgroup-recent)
4399                     gnus-recent-mark)
4400                    ((memq number gnus-newsgroup-unseen)
4401                     gnus-unseen-mark)
4402                    (t gnus-no-mark))
4403              gnus-tmp-from (mail-header-from gnus-tmp-header)
4404              gnus-tmp-name
4405              (cond
4406               ((string-match "<[^>]+> *$" gnus-tmp-from)
4407                (setq beg-match (match-beginning 0))
4408                (or (and (string-match "^\".+\"" gnus-tmp-from)
4409                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4410                    (substring gnus-tmp-from 0 beg-match)))
4411               ((string-match "(.+)" gnus-tmp-from)
4412                (substring gnus-tmp-from
4413                           (1+ (match-beginning 0)) (1- (match-end 0))))
4414               (t gnus-tmp-from))
4415              gnus-tmp-thread-tree-header-string
4416              (cond
4417               ((not gnus-show-threads) "")
4418               ((zerop gnus-tmp-level)
4419                (if (cdar thread)
4420                    (or gnus-sum-thread-tree-root subject)
4421                  (or gnus-sum-thread-tree-single-indent subject)))
4422               (t
4423                (concat (apply 'concat
4424                               (mapcar (lambda (item)
4425                                         (if (= item 1)
4426                                             gnus-sum-thread-tree-vertical
4427                                           gnus-sum-thread-tree-indent))
4428                                       (cdr (reverse tree-stack))))
4429                        (if (nth 1 thread)
4430                            gnus-sum-thread-tree-leaf-with-other
4431                          gnus-sum-thread-tree-single-leaf)))))
4432             (when (string= gnus-tmp-name "")
4433               (setq gnus-tmp-name gnus-tmp-from))
4434             (unless (numberp gnus-tmp-lines)
4435               (setq gnus-tmp-lines -1))
4436             (if (= gnus-tmp-lines -1)
4437                 (setq gnus-tmp-lines "?")
4438               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4439             (gnus-put-text-property
4440              (point)
4441              (progn (eval gnus-summary-line-format-spec) (point))
4442              'gnus-number number)
4443             (when gnus-visual-p
4444               (forward-line -1)
4445               (gnus-run-hooks 'gnus-summary-update-hook)
4446               (forward-line 1))
4447
4448             (setq gnus-tmp-prev-subject subject)))
4449
4450         (when (nth 1 thread)
4451           (push (list (max 0 gnus-tmp-level)
4452                       (copy-list tree-stack)
4453                       (nthcdr 1 thread))
4454                 stack))
4455         (push (if (nth 1 thread) 1 0) tree-stack)
4456         (incf gnus-tmp-level)
4457         (setq threads (if thread-end nil (cdar thread)))
4458         (unless threads
4459           (setq gnus-tmp-level 0)))))
4460   (gnus-message 7 "Generating summary...done"))
4461
4462 (defun gnus-summary-prepare-unthreaded (headers)
4463   "Generate an unthreaded summary buffer based on HEADERS."
4464   (let (header number mark)
4465
4466     (beginning-of-line)
4467
4468     (while headers
4469       ;; We may have to root out some bad articles...
4470       (when (memq (setq number (mail-header-number
4471                                 (setq header (pop headers))))
4472                   gnus-newsgroup-limit)
4473         ;; Mark article as read when it has a low score.
4474         (when (and gnus-summary-mark-below
4475                    (< (or (cdr (assq number gnus-newsgroup-scored))
4476                           gnus-summary-default-score 0)
4477                       gnus-summary-mark-below)
4478                    (not (gnus-summary-article-ancient-p number)))
4479           (setq gnus-newsgroup-unreads
4480                 (delq number gnus-newsgroup-unreads))
4481           (if gnus-newsgroup-auto-expire
4482               (push number gnus-newsgroup-expirable)
4483             (push (cons number gnus-low-score-mark)
4484                   gnus-newsgroup-reads)))
4485
4486         (setq mark (gnus-article-mark number))
4487         (push (gnus-data-make number mark (1+ (point)) header 0)
4488               gnus-newsgroup-data)
4489         (gnus-summary-insert-line
4490          header 0 number
4491          mark (memq number gnus-newsgroup-replied)
4492          (memq number gnus-newsgroup-expirable)
4493          (mail-header-subject header) nil
4494          (cdr (assq number gnus-newsgroup-scored))
4495          (memq number gnus-newsgroup-processable))))))
4496
4497 (defun gnus-summary-remove-list-identifiers ()
4498   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4499   (let ((regexp (if (consp gnus-list-identifiers)
4500                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4501                   gnus-list-identifiers))
4502         changed subject)
4503     (when regexp
4504       (dolist (header gnus-newsgroup-headers)
4505         (setq subject (mail-header-subject header)
4506               changed nil)
4507         (while (string-match
4508                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4509                 subject)
4510           (setq subject
4511                 (concat (substring subject 0 (match-beginning 2))
4512                         (substring subject (match-end 0)))
4513                 changed t))
4514         (when (and changed
4515                    (string-match
4516                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4517           (setq subject
4518                 (concat (substring subject 0 (match-beginning 1))
4519                         (substring subject (match-end 1)))))
4520         (when changed
4521           (mail-header-set-subject header subject))))))
4522
4523 (defun gnus-fetch-headers (articles)
4524   "Fetch headers of ARTICLES."
4525   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4526     (gnus-message 5 "Fetching headers for %s..." name)
4527     (prog1
4528         (if (eq 'nov
4529                 (setq gnus-headers-retrieved-by
4530                       (gnus-retrieve-headers
4531                        articles gnus-newsgroup-name
4532                        ;; We might want to fetch old headers, but
4533                        ;; not if there is only 1 article.
4534                        (and (or (and
4535                                  (not (eq gnus-fetch-old-headers 'some))
4536                                  (not (numberp gnus-fetch-old-headers)))
4537                                 (> (length articles) 1))
4538                             gnus-fetch-old-headers))))
4539             (gnus-get-newsgroup-headers-xover
4540              articles nil nil gnus-newsgroup-name t)
4541           (gnus-get-newsgroup-headers))
4542       (gnus-message 5 "Fetching headers for %s...done" name))))
4543
4544 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4545   "Select newsgroup GROUP.
4546 If READ-ALL is non-nil, all articles in the group are selected.
4547 If SELECT-ARTICLES, only select those articles from GROUP."
4548   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4549          ;;!!! Dirty hack; should be removed.
4550          (gnus-summary-ignore-duplicates
4551           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4552               t
4553             gnus-summary-ignore-duplicates))
4554          (info (nth 2 entry))
4555          articles fetched-articles cached)
4556
4557     (unless (gnus-check-server
4558              (set (make-local-variable 'gnus-current-select-method)
4559                   (gnus-find-method-for-group group)))
4560       (error "Couldn't open server"))
4561
4562     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4563         (gnus-activate-group group)     ; Or we can activate it...
4564         (progn                          ; Or we bug out.
4565           (when (equal major-mode 'gnus-summary-mode)
4566             (kill-buffer (current-buffer)))
4567           (error "Couldn't activate group %s: %s"
4568                  group (gnus-status-message group))))
4569
4570     (unless (gnus-request-group group t)
4571       (when (equal major-mode 'gnus-summary-mode)
4572         (kill-buffer (current-buffer)))
4573       (error "Couldn't request group %s: %s"
4574              group (gnus-status-message group)))
4575
4576     (setq gnus-newsgroup-name group
4577           gnus-newsgroup-unselected nil
4578           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4579
4580     (let ((display (gnus-group-find-parameter group 'display)))
4581       (setq gnus-newsgroup-display
4582             (cond
4583              ((not (zerop (or (car-safe read-all) 0)))
4584               ;; The user entered the group with C-u SPC/RET, let's show
4585               ;; all articles.
4586               'gnus-not-ignore)
4587              ((eq display 'all)
4588               'gnus-not-ignore)
4589              ((arrayp display)
4590               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4591              ((numberp display)
4592               ;; The following is probably the "correct" solution, but
4593               ;; it makes Gnus fetch all headers and then limit the
4594               ;; articles (which is slow), so instead we hack the
4595               ;; select-articles parameter instead. -- Simon Josefsson
4596               ;; <jas@kth.se>
4597               ;;
4598               ;; (gnus-byte-compile
4599               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4600               ;;                         display)))))
4601               (setq select-articles
4602                     (gnus-uncompress-range
4603                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4604                              (if (> tmp 0)
4605                                  tmp
4606                                1))
4607                            (cdr (gnus-active group)))))
4608               nil)
4609              (t
4610               nil))))
4611
4612     (gnus-summary-setup-default-charset)
4613
4614     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4615     (when (gnus-virtual-group-p group)
4616       (setq cached gnus-newsgroup-cached))
4617
4618     (setq gnus-newsgroup-unreads
4619           (gnus-set-difference
4620            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4621            gnus-newsgroup-dormant))
4622
4623     (setq gnus-newsgroup-processable nil)
4624
4625     (gnus-update-read-articles group gnus-newsgroup-unreads)
4626
4627     ;; Adjust and set lists of article marks.
4628     (when info
4629       (gnus-adjust-marked-articles info))
4630
4631     (if (setq articles select-articles)
4632         (setq gnus-newsgroup-unselected
4633               (gnus-sorted-intersection
4634                gnus-newsgroup-unreads
4635                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4636       (setq articles (gnus-articles-to-read group read-all)))
4637
4638     (cond
4639      ((null articles)
4640       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4641       'quit)
4642      ((eq articles 0) nil)
4643      (t
4644       ;; Init the dependencies hash table.
4645       (setq gnus-newsgroup-dependencies
4646             (gnus-make-hashtable (length articles)))
4647       (gnus-set-global-variables)
4648       ;; Retrieve the headers and read them in.
4649       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4650
4651       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4652       (when cached
4653         (setq gnus-newsgroup-cached cached))
4654
4655       ;; Suppress duplicates?
4656       (when gnus-suppress-duplicates
4657         (gnus-dup-suppress-articles))
4658
4659       ;; Set the initial limit.
4660       (setq gnus-newsgroup-limit (copy-sequence articles))
4661       ;; Remove canceled articles from the list of unread articles.
4662       (setq fetched-articles
4663             (mapcar (lambda (headers) (mail-header-number headers))
4664                     gnus-newsgroup-headers))
4665       (setq gnus-newsgroup-articles fetched-articles)
4666       (setq gnus-newsgroup-unreads
4667             (gnus-set-sorted-intersection
4668              gnus-newsgroup-unreads fetched-articles))
4669
4670       (let ((marks (assq 'seen (gnus-info-marks info))))
4671         ;; The `seen' marks are treated specially.
4672         (when (setq gnus-newsgroup-seen (cdr marks))
4673           (dolist (article gnus-newsgroup-articles)
4674             (unless (gnus-member-of-range
4675                      article gnus-newsgroup-seen)
4676               (push article gnus-newsgroup-unseen)))))
4677
4678       ;; Removed marked articles that do not exist.
4679       (gnus-update-missing-marks
4680        (gnus-sorted-complement fetched-articles articles))
4681       ;; We might want to build some more threads first.
4682       (when (and gnus-fetch-old-headers
4683                  (eq gnus-headers-retrieved-by 'nov))
4684         (if (eq gnus-fetch-old-headers 'invisible)
4685             (gnus-build-all-threads)
4686           (gnus-build-old-threads)))
4687       ;; Let the Gnus agent mark articles as read.
4688       (when gnus-agent
4689         (gnus-agent-get-undownloaded-list))
4690       ;; Remove list identifiers from subject
4691       (when gnus-list-identifiers
4692         (gnus-summary-remove-list-identifiers))
4693       ;; Check whether auto-expire is to be done in this group.
4694       (setq gnus-newsgroup-auto-expire
4695             (gnus-group-auto-expirable-p group))
4696       ;; Set up the article buffer now, if necessary.
4697       (unless gnus-single-article-buffer
4698         (gnus-article-setup-buffer))
4699       ;; First and last article in this newsgroup.
4700       (when gnus-newsgroup-headers
4701         (setq gnus-newsgroup-begin
4702               (mail-header-number (car gnus-newsgroup-headers))
4703               gnus-newsgroup-end
4704               (mail-header-number
4705                (gnus-last-element gnus-newsgroup-headers))))
4706       ;; GROUP is successfully selected.
4707       (or gnus-newsgroup-headers t)))))
4708
4709 (defun gnus-summary-display-make-predicate (display)
4710   (require 'gnus-agent)
4711   (when (= (length display) 1)
4712     (setq display (car display)))
4713   (unless gnus-summary-display-cache
4714     (dolist (elem (append (list (cons 'read 'read)
4715                                 (cons 'unseen 'unseen))
4716                           gnus-article-mark-lists))
4717       (push (cons (cdr elem)
4718                   (gnus-byte-compile
4719                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4720             gnus-summary-display-cache)))
4721   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4722     (gnus-get-predicate display)))
4723
4724 ;; Uses the dynamically bound `number' variable.
4725 (defvar number)
4726 (defun gnus-article-marked-p (type &optional article)
4727   (let ((article (or article number)))
4728     (cond
4729      ((eq type 'tick)
4730       (memq article gnus-newsgroup-marked))
4731      ((eq type 'unsend)
4732       (memq article gnus-newsgroup-unsendable))
4733      ((eq type 'undownload)
4734       (memq article gnus-newsgroup-undownloaded))
4735      ((eq type 'download)
4736       (memq article gnus-newsgroup-downloadable))
4737      ((eq type 'unread)
4738       (memq article gnus-newsgroup-unreads))
4739      ((eq type 'read)
4740       (memq article gnus-newsgroup-reads))
4741      ((eq type 'dormant)
4742       (memq article gnus-newsgroup-dormant) )
4743      ((eq type 'expire)
4744       (memq article gnus-newsgroup-expirable))
4745      ((eq type 'reply)
4746       (memq article gnus-newsgroup-replied))
4747      ((eq type 'killed)
4748       (memq article gnus-newsgroup-killed))
4749      ((eq type 'bookmark)
4750       (assq article gnus-newsgroup-bookmarks))
4751      ((eq type 'score)
4752       (assq article gnus-newsgroup-scored))
4753      ((eq type 'save)
4754       (memq article gnus-newsgroup-saved))
4755      ((eq type 'cache)
4756       (memq article gnus-newsgroup-cached))
4757      ((eq type 'forward)
4758       (memq article gnus-newsgroup-forwarded))
4759      ((eq type 'seen)
4760       (not (memq article gnus-newsgroup-unseen)))
4761      ((eq type 'recent)
4762       (memq article gnus-newsgroup-recent))
4763      (t t))))
4764
4765 (defun gnus-articles-to-read (group &optional read-all)
4766   "Find out what articles the user wants to read."
4767   (let* ((articles
4768           ;; Select all articles if `read-all' is non-nil, or if there
4769           ;; are no unread articles.
4770           (if (or read-all
4771                   (and (zerop (length gnus-newsgroup-marked))
4772                        (zerop (length gnus-newsgroup-unreads)))
4773                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4774               ;; We want to select the headers for all the articles in
4775               ;; the group, so we select either all the active
4776               ;; articles in the group, or (if that's nil), the
4777               ;; articles in the cache.
4778               (or
4779                (gnus-uncompress-range (gnus-active group))
4780                (gnus-cache-articles-in-group group))
4781             ;; Select only the "normal" subset of articles.
4782             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4783                           (copy-sequence gnus-newsgroup-unreads))
4784                   '<)))
4785          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4786          (scored (length scored-list))
4787          (number (length articles))
4788          (marked (+ (length gnus-newsgroup-marked)
4789                     (length gnus-newsgroup-dormant)))
4790          (select
4791           (cond
4792            ((numberp read-all)
4793             read-all)
4794            (t
4795             (condition-case ()
4796                 (cond
4797                  ((and (or (<= scored marked) (= scored number))
4798                        (natnump gnus-large-newsgroup)
4799                        (> number gnus-large-newsgroup))
4800                   (let* ((cursor-in-echo-area nil)
4801                          (input
4802                           (read-from-minibuffer
4803                            (format
4804                             "How many articles from %s (max %d): "
4805                             (gnus-limit-string
4806                              (gnus-group-decoded-name gnus-newsgroup-name)
4807                              35)
4808                             number)
4809                            (cons (number-to-string gnus-large-newsgroup)
4810                                  0))))
4811                     (if (string-match "^[ \t]*$" input)
4812                         number
4813                       input)))
4814                  ((and (> scored marked) (< scored number)
4815                        (> (- scored number) 20))
4816                   (let ((input
4817                          (read-string
4818                           (format "%s %s (%d scored, %d total): "
4819                                   "How many articles from"
4820                                   (gnus-group-decoded-name group)
4821                                   scored number))))
4822                     (if (string-match "^[ \t]*$" input)
4823                         number input)))
4824                  (t number))
4825               (quit
4826                (message "Quit getting the articles to read")
4827                nil))))))
4828     (setq select (if (stringp select) (string-to-number select) select))
4829     (if (or (null select) (zerop select))
4830         select
4831       (if (and (not (zerop scored)) (<= (abs select) scored))
4832           (progn
4833             (setq articles (sort scored-list '<))
4834             (setq number (length articles)))
4835         (setq articles (copy-sequence articles)))
4836
4837       (when (< (abs select) number)
4838         (if (< select 0)
4839             ;; Select the N oldest articles.
4840             (setcdr (nthcdr (1- (abs select)) articles) nil)
4841           ;; Select the N most recent articles.
4842           (setq articles (nthcdr (- number select) articles))))
4843       (setq gnus-newsgroup-unselected
4844             (gnus-sorted-intersection
4845              gnus-newsgroup-unreads
4846              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4847       (when gnus-alter-articles-to-read-function
4848         (setq gnus-newsgroup-unreads
4849               (sort
4850                (funcall gnus-alter-articles-to-read-function
4851                         gnus-newsgroup-name gnus-newsgroup-unreads)
4852                '<)))
4853       articles)))
4854
4855 (defun gnus-killed-articles (killed articles)
4856   (let (out)
4857     (while articles
4858       (when (inline (gnus-member-of-range (car articles) killed))
4859         (push (car articles) out))
4860       (setq articles (cdr articles)))
4861     out))
4862
4863 (defun gnus-uncompress-marks (marks)
4864   "Uncompress the mark ranges in MARKS."
4865   (let ((uncompressed '(score bookmark))
4866         out)
4867     (while marks
4868       (if (memq (caar marks) uncompressed)
4869           (push (car marks) out)
4870         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4871       (setq marks (cdr marks)))
4872     out))
4873
4874 (defun gnus-article-mark-to-type (mark)
4875   "Return the type of MARK."
4876   (or (cadr (assq mark gnus-article-special-mark-lists))
4877       'list))
4878
4879 (defun gnus-article-unpropagatable-p (mark)
4880   "Return whether MARK should be propagated to backend."
4881   (memq mark gnus-article-unpropagated-mark-lists))
4882
4883 (defun gnus-adjust-marked-articles (info)
4884   "Set all article lists and remove all marks that are no longer valid."
4885   (let* ((marked-lists (gnus-info-marks info))
4886          (active (gnus-active (gnus-info-group info)))
4887          (min (car active))
4888          (max (cdr active))
4889          (types gnus-article-mark-lists)
4890          marks var articles article mark mark-type)
4891
4892     (dolist (marks marked-lists)
4893       (setq mark (car marks)
4894             mark-type (gnus-article-mark-to-type mark)
4895             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4896
4897       ;; We set the variable according to the type of the marks list,
4898       ;; and then adjust the marks to a subset of the active articles.
4899       (cond
4900        ;; Adjust "simple" lists.
4901        ((eq mark-type 'list)
4902         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4903         (when (memq mark '(tick dormant expire reply save))
4904           (while articles
4905             (when (or (< (setq article (pop articles)) min) (> article max))
4906               (set var (delq article (symbol-value var)))))))
4907        ;; Adjust assocs.
4908        ((eq mark-type 'tuple)
4909         (set var (setq articles (cdr marks)))
4910         (when (not (listp (cdr (symbol-value var))))
4911           (set var (list (symbol-value var))))
4912         (when (not (listp (cdr articles)))
4913           (setq articles (list articles)))
4914         (while articles
4915           (when (or (not (consp (setq article (pop articles))))
4916                     (< (car article) min)
4917                     (> (car article) max))
4918             (set var (delq article (symbol-value var))))))
4919        ((eq mark-type 'range)
4920         (cond
4921          ((eq mark 'seen))))))))
4922
4923 (defun gnus-update-missing-marks (missing)
4924   "Go through the list of MISSING articles and remove them from the mark lists."
4925   (when missing
4926     (let (var m)
4927       ;; Go through all types.
4928       (dolist (elem gnus-article-mark-lists)
4929         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4930           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4931           (when (symbol-value var)
4932             ;; This list has articles.  So we delete all missing
4933             ;; articles from it.
4934             (setq m missing)
4935             (while m
4936               (set var (delq (pop m) (symbol-value var))))))))))
4937
4938 (defun gnus-update-marks ()
4939   "Enter the various lists of marked articles into the newsgroup info list."
4940   (let ((types gnus-article-mark-lists)
4941         (info (gnus-get-info gnus-newsgroup-name))
4942         type list newmarked symbol delta-marks)
4943     (when info
4944       ;; Add all marks lists to the list of marks lists.
4945       (while (setq type (pop types))
4946         (setq list (symbol-value
4947                     (setq symbol
4948                           (intern (format "gnus-newsgroup-%s" (car type))))))
4949
4950         (when list
4951           ;; Get rid of the entries of the articles that have the
4952           ;; default score.
4953           (when (and (eq (cdr type) 'score)
4954                      gnus-save-score
4955                      list)
4956             (let* ((arts list)
4957                    (prev (cons nil list))
4958                    (all prev))
4959               (while arts
4960                 (if (or (not (consp (car arts)))
4961                         (= (cdar arts) gnus-summary-default-score))
4962                     (setcdr prev (cdr arts))
4963                   (setq prev arts))
4964                 (setq arts (cdr arts)))
4965               (setq list (cdr all)))))
4966
4967         (when (eq (cdr type) 'seen)
4968           (setq list
4969                 (if list
4970                     (gnus-add-to-range list gnus-newsgroup-unseen)
4971                   (gnus-compress-sequence gnus-newsgroup-articles))))
4972
4973         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4974           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4975
4976         (when (and (gnus-check-backend-function
4977                     'request-set-mark gnus-newsgroup-name)
4978                    (not (gnus-article-unpropagatable-p (cdr type))))
4979           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4980                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4981                  (add (gnus-remove-from-range
4982                        (gnus-copy-sequence list) old)))
4983             (when add
4984               (push (list add 'add (list (cdr type))) delta-marks))
4985             (when del
4986               (push (list del 'del (list (cdr type))) delta-marks))))
4987
4988         (when list
4989           (push (cons (cdr type) list) newmarked)))
4990
4991       (when delta-marks
4992         (unless (gnus-check-group gnus-newsgroup-name)
4993           (error "Can't open server for %s" gnus-newsgroup-name))
4994         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4995
4996       ;; Enter these new marks into the info of the group.
4997       (if (nthcdr 3 info)
4998           (setcar (nthcdr 3 info) newmarked)
4999         ;; Add the marks lists to the end of the info.
5000         (when newmarked
5001           (setcdr (nthcdr 2 info) (list newmarked))))
5002
5003       ;; Cut off the end of the info if there's nothing else there.
5004       (let ((i 5))
5005         (while (and (> i 2)
5006                     (not (nth i info)))
5007           (when (nthcdr (decf i) info)
5008             (setcdr (nthcdr i info) nil)))))))
5009
5010 (defun gnus-set-mode-line (where)
5011   "Set the mode line of the article or summary buffers.
5012 If WHERE is `summary', the summary mode line format will be used."
5013   ;; Is this mode line one we keep updated?
5014   (when (and (memq where gnus-updated-mode-lines)
5015              (symbol-value
5016               (intern (format "gnus-%s-mode-line-format-spec" where))))
5017     (let (mode-string)
5018       (save-excursion
5019         ;; We evaluate this in the summary buffer since these
5020         ;; variables are buffer-local to that buffer.
5021         (set-buffer gnus-summary-buffer)
5022         ;; We bind all these variables that are used in the `eval' form
5023         ;; below.
5024         (let* ((mformat (symbol-value
5025                          (intern
5026                           (format "gnus-%s-mode-line-format-spec" where))))
5027                (gnus-tmp-group-name (gnus-group-decoded-name
5028                                      gnus-newsgroup-name))
5029                (gnus-tmp-article-number (or gnus-current-article 0))
5030                (gnus-tmp-unread gnus-newsgroup-unreads)
5031                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5032                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5033                (gnus-tmp-unread-and-unselected
5034                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5035                             (zerop gnus-tmp-unselected))
5036                        "")
5037                       ((zerop gnus-tmp-unselected)
5038                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5039                       (t (format "{%d(+%d) more}"
5040                                  gnus-tmp-unread-and-unticked
5041                                  gnus-tmp-unselected))))
5042                (gnus-tmp-subject
5043                 (if (and gnus-current-headers
5044                          (vectorp gnus-current-headers))
5045                     (gnus-mode-string-quote
5046                      (mail-header-subject gnus-current-headers))
5047                   ""))
5048                bufname-length max-len
5049                gnus-tmp-header);; passed as argument to any user-format-funcs
5050           (setq mode-string (eval mformat))
5051           (setq bufname-length (if (string-match "%b" mode-string)
5052                                    (- (length
5053                                        (buffer-name
5054                                         (if (eq where 'summary)
5055                                             nil
5056                                           (get-buffer gnus-article-buffer))))
5057                                       2)
5058                                  0))
5059           (setq max-len (max 4 (if gnus-mode-non-string-length
5060                                    (- (window-width)
5061                                       gnus-mode-non-string-length
5062                                       bufname-length)
5063                                  (length mode-string))))
5064           ;; We might have to chop a bit of the string off...
5065           (when (> (length mode-string) max-len)
5066             (setq mode-string
5067                   (concat (gnus-truncate-string mode-string (- max-len 3))
5068                           "...")))
5069           ;; Pad the mode string a bit.
5070           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5071       ;; Update the mode line.
5072       (setq mode-line-buffer-identification
5073             (gnus-mode-line-buffer-identification (list mode-string)))
5074       (set-buffer-modified-p t))))
5075
5076 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5077   "Go through the HEADERS list and add all Xrefs to a hash table.
5078 The resulting hash table is returned, or nil if no Xrefs were found."
5079   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5080          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5081          (xref-hashtb (gnus-make-hashtable))
5082          start group entry number xrefs header)
5083     (while headers
5084       (setq header (pop headers))
5085       (when (and (setq xrefs (mail-header-xref header))
5086                  (not (memq (setq number (mail-header-number header))
5087                             unreads)))
5088         (setq start 0)
5089         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5090           (setq start (match-end 0))
5091           (setq group (if prefix
5092                           (concat prefix (substring xrefs (match-beginning 1)
5093                                                     (match-end 1)))
5094                         (substring xrefs (match-beginning 1) (match-end 1))))
5095           (setq number
5096                 (string-to-int (substring xrefs (match-beginning 2)
5097                                           (match-end 2))))
5098           (if (setq entry (gnus-gethash group xref-hashtb))
5099               (setcdr entry (cons number (cdr entry)))
5100             (gnus-sethash group (cons number nil) xref-hashtb)))))
5101     (and start xref-hashtb)))
5102
5103 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5104   "Look through all the headers and mark the Xrefs as read."
5105   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5106         name entry info xref-hashtb idlist method nth4)
5107     (save-excursion
5108       (set-buffer gnus-group-buffer)
5109       (when (setq xref-hashtb
5110                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5111         (mapatoms
5112          (lambda (group)
5113            (unless (string= from-newsgroup (setq name (symbol-name group)))
5114              (setq idlist (symbol-value group))
5115              ;; Dead groups are not updated.
5116              (and (prog1
5117                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5118                             info (nth 2 entry))
5119                     (when (stringp (setq nth4 (gnus-info-method info)))
5120                       (setq nth4 (gnus-server-to-method nth4))))
5121                   ;; Only do the xrefs if the group has the same
5122                   ;; select method as the group we have just read.
5123                   (or (gnus-methods-equal-p
5124                        nth4 (gnus-find-method-for-group from-newsgroup))
5125                       virtual
5126                       (equal nth4 (setq method (gnus-find-method-for-group
5127                                                 from-newsgroup)))
5128                       (and (equal (car nth4) (car method))
5129                            (equal (nth 1 nth4) (nth 1 method))))
5130                   gnus-use-cross-reference
5131                   (or (not (eq gnus-use-cross-reference t))
5132                       virtual
5133                       ;; Only do cross-references on subscribed
5134                       ;; groups, if that is what is wanted.
5135                       (<= (gnus-info-level info) gnus-level-subscribed))
5136                   (gnus-group-make-articles-read name idlist))))
5137          xref-hashtb)))))
5138
5139 (defun gnus-compute-read-articles (group articles)
5140   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5141          (info (nth 2 entry))
5142          (active (gnus-active group))
5143          ninfo)
5144     (when entry
5145       ;; First peel off all invalid article numbers.
5146       (when active
5147         (let ((ids articles)
5148               id first)
5149           (while (setq id (pop ids))
5150             (when (and first (> id (cdr active)))
5151               ;; We'll end up in this situation in one particular
5152               ;; obscure situation.  If you re-scan a group and get
5153               ;; a new article that is cross-posted to a different
5154               ;; group that has not been re-scanned, you might get
5155               ;; crossposted article that has a higher number than
5156               ;; Gnus believes possible.  So we re-activate this
5157               ;; group as well.  This might mean doing the
5158               ;; crossposting thingy will *increase* the number
5159               ;; of articles in some groups.  Tsk, tsk.
5160               (setq active (or (gnus-activate-group group) active)))
5161             (when (or (> id (cdr active))
5162                       (< id (car active)))
5163               (setq articles (delq id articles))))))
5164       ;; If the read list is nil, we init it.
5165       (if (and active
5166                (null (gnus-info-read info))
5167                (> (car active) 1))
5168           (setq ninfo (cons 1 (1- (car active))))
5169         (setq ninfo (gnus-info-read info)))
5170       ;; Then we add the read articles to the range.
5171       (gnus-add-to-range
5172        ninfo (setq articles (sort articles '<))))))
5173
5174 (defun gnus-group-make-articles-read (group articles)
5175   "Update the info of GROUP to say that ARTICLES are read."
5176   (let* ((num 0)
5177          (entry (gnus-gethash group gnus-newsrc-hashtb))
5178          (info (nth 2 entry))
5179          (active (gnus-active group))
5180          range)
5181     (when entry
5182       (setq range (gnus-compute-read-articles group articles))
5183       (save-excursion
5184         (set-buffer gnus-group-buffer)
5185         (gnus-undo-register
5186           `(progn
5187              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5188              (gnus-info-set-read ',info ',(gnus-info-read info))
5189              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5190              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5191              (gnus-group-update-group ,group t))))
5192       ;; Add the read articles to the range.
5193       (gnus-info-set-read info range)
5194       (gnus-request-set-mark group (list (list range 'add '(read))))
5195       ;; Then we have to re-compute how many unread
5196       ;; articles there are in this group.
5197       (when active
5198         (cond
5199          ((not range)
5200           (setq num (- (1+ (cdr active)) (car active))))
5201          ((not (listp (cdr range)))
5202           (setq num (- (cdr active) (- (1+ (cdr range))
5203                                        (car range)))))
5204          (t
5205           (while range
5206             (if (numberp (car range))
5207                 (setq num (1+ num))
5208               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5209             (setq range (cdr range)))
5210           (setq num (- (cdr active) num))))
5211         ;; Update the number of unread articles.
5212         (setcar entry num)
5213         ;; Update the group buffer.
5214         (gnus-group-update-group group t)))))
5215
5216 (defvar gnus-newsgroup-none-id 0)
5217
5218 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5219   (let ((cur nntp-server-buffer)
5220         (dependencies
5221          (or dependencies
5222              (save-excursion (set-buffer gnus-summary-buffer)
5223                              gnus-newsgroup-dependencies)))
5224         headers id end ref
5225         (mail-parse-charset gnus-newsgroup-charset)
5226         (mail-parse-ignored-charsets
5227          (save-excursion (condition-case nil
5228                              (set-buffer gnus-summary-buffer)
5229                            (error))
5230                          gnus-newsgroup-ignored-charsets)))
5231     (save-excursion
5232       (set-buffer nntp-server-buffer)
5233       ;; Translate all TAB characters into SPACE characters.
5234       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5235       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5236       (gnus-run-hooks 'gnus-parse-headers-hook)
5237       (let ((case-fold-search t)
5238             in-reply-to header p lines chars ctype)
5239         (goto-char (point-min))
5240         ;; Search to the beginning of the next header.  Error messages
5241         ;; do not begin with 2 or 3.
5242         (while (re-search-forward "^[23][0-9]+ " nil t)
5243           (setq id nil
5244                 ref nil)
5245           ;; This implementation of this function, with nine
5246           ;; search-forwards instead of the one re-search-forward and
5247           ;; a case (which basically was the old function) is actually
5248           ;; about twice as fast, even though it looks messier.  You
5249           ;; can't have everything, I guess.  Speed and elegance
5250           ;; doesn't always go hand in hand.
5251           (setq
5252            header
5253            (make-full-mail-header
5254             ;; Number.
5255             (prog1
5256                 (read cur)
5257               (end-of-line)
5258               (setq p (point))
5259               (narrow-to-region (point)
5260                                 (or (and (search-forward "\n.\n" nil t)
5261                                          (- (point) 2))
5262                                     (point))))
5263             ;; Subject.
5264             (progn
5265               (goto-char p)
5266               (if (search-forward "\nsubject:" nil t)
5267                   (nnheader-header-value)
5268                 "(none)"))
5269             ;; From.
5270             (progn
5271               (goto-char p)
5272               (if (search-forward "\nfrom:" nil t)
5273                   (nnheader-header-value)
5274                 "(nobody)"))
5275             ;; Date.
5276             (progn
5277               (goto-char p)
5278               (if (search-forward "\ndate:" nil t)
5279                   (nnheader-header-value) ""))
5280             ;; Message-ID.
5281             (progn
5282               (goto-char p)
5283               (setq id (if (re-search-forward
5284                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5285                            ;; We do it this way to make sure the Message-ID
5286                            ;; is (somewhat) syntactically valid.
5287                            (buffer-substring (match-beginning 1)
5288                                              (match-end 1))
5289                          ;; If there was no message-id, we just fake one
5290                          ;; to make subsequent routines simpler.
5291                          (nnheader-generate-fake-message-id))))
5292             ;; References.
5293             (progn
5294               (goto-char p)
5295               (if (search-forward "\nreferences:" nil t)
5296                   (progn
5297                     (setq end (point))
5298                     (prog1
5299                         (nnheader-header-value)
5300                       (setq ref
5301                             (buffer-substring
5302                              (progn
5303                                ;; (end-of-line)
5304                                (search-backward ">" end t)
5305                                (1+ (point)))
5306                              (progn
5307                                (search-backward "<" end t)
5308                                (point))))))
5309                 ;; Get the references from the in-reply-to header if there
5310                 ;; were no references and the in-reply-to header looks
5311                 ;; promising.
5312                 (if (and (search-forward "\nin-reply-to:" nil t)
5313                          (setq in-reply-to (nnheader-header-value))
5314                          (string-match "<[^>]+>" in-reply-to))
5315                     (let (ref2)
5316                       (setq ref (substring in-reply-to (match-beginning 0)
5317                                            (match-end 0)))
5318                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5319                         (setq ref2 (substring in-reply-to (match-beginning 0)
5320                                               (match-end 0)))
5321                         (when (> (length ref2) (length ref))
5322                           (setq ref ref2)))
5323                       ref)
5324                   (setq ref nil))))
5325             ;; Chars.
5326             (progn
5327               (goto-char p)
5328               (if (search-forward "\nchars: " nil t)
5329                   (if (numberp (setq chars (ignore-errors (read cur))))
5330                       chars -1)
5331                 -1))
5332             ;; Lines.
5333             (progn
5334               (goto-char p)
5335               (if (search-forward "\nlines: " nil t)
5336                   (if (numberp (setq lines (ignore-errors (read cur))))
5337                       lines -1)
5338                 -1))
5339             ;; Xref.
5340             (progn
5341               (goto-char p)
5342               (and (search-forward "\nxref:" nil t)
5343                    (nnheader-header-value)))
5344             ;; Extra.
5345             (when gnus-extra-headers
5346               (let ((extra gnus-extra-headers)
5347                     out)
5348                 (while extra
5349                   (goto-char p)
5350                   (when (search-forward
5351                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5352                     (push (cons (car extra) (nnheader-header-value)) out))
5353                   (pop extra))
5354                 out))))
5355           (goto-char p)
5356           (if (and (search-forward "\ncontent-type: " nil t)
5357                    (setq ctype (nnheader-header-value)))
5358               (mime-entity-set-content-type-internal
5359                header (mime-parse-Content-Type ctype)))
5360           (when (equal id ref)
5361             (setq ref nil))
5362
5363           (when gnus-alter-header-function
5364             (funcall gnus-alter-header-function header)
5365             (setq id (mail-header-id header)
5366                   ref (gnus-parent-id (mail-header-references header))))
5367
5368           (when (setq header
5369                       (gnus-dependencies-add-header
5370                        header dependencies force-new))
5371             (push header headers))
5372           (goto-char (point-max))
5373           (widen))
5374         (nreverse headers)))))
5375
5376 ;; Goes through the xover lines and returns a list of vectors
5377 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5378                                                   force-new dependencies
5379                                                   group also-fetch-heads)
5380   "Parse the news overview data in the server buffer.
5381 Return a list of headers that match SEQUENCE (see
5382 `nntp-retrieve-headers')."
5383   ;; Get the Xref when the users reads the articles since most/some
5384   ;; NNTP servers do not include Xrefs when using XOVER.
5385   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5386   (let ((mail-parse-charset gnus-newsgroup-charset)
5387         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5388         (cur nntp-server-buffer)
5389         (dependencies (or dependencies gnus-newsgroup-dependencies))
5390         (allp (cond
5391                ((eq gnus-read-all-available-headers t)
5392                 t)
5393                ((stringp gnus-read-all-available-headers)
5394                 (string-match gnus-read-all-available-headers group))
5395                (t
5396                 nil)))
5397         number headers header)
5398     (save-excursion
5399       (set-buffer nntp-server-buffer)
5400       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5401       ;; Allow the user to mangle the headers before parsing them.
5402       (gnus-run-hooks 'gnus-parse-headers-hook)
5403       (goto-char (point-min))
5404       (while (not (eobp))
5405         (condition-case ()
5406             (while (and (or sequence allp)
5407                         (not (eobp)))
5408               (setq number (read cur))
5409               (when (not allp)
5410                 (while (and sequence
5411                             (< (car sequence) number))
5412                   (setq sequence (cdr sequence))))
5413               (when (and (or allp
5414                              (and sequence
5415                                   (eq number (car sequence))))
5416                          (progn
5417                            (setq sequence (cdr sequence))
5418                            (setq header (inline
5419                                           (gnus-nov-parse-line
5420                                            number dependencies force-new)))))
5421                 (push header headers))
5422               (forward-line 1))
5423           (error
5424            (gnus-error 4 "Strange nov line (%d)"
5425                        (count-lines (point-min) (point)))))
5426         (forward-line 1))
5427       ;; A common bug in inn is that if you have posted an article and
5428       ;; then retrieves the active file, it will answer correctly --
5429       ;; the new article is included.  However, a NOV entry for the
5430       ;; article may not have been generated yet, so this may fail.
5431       ;; We work around this problem by retrieving the last few
5432       ;; headers using HEAD.
5433       (if (or (not also-fetch-heads)
5434               (not sequence))
5435           ;; We (probably) got all the headers.
5436           (nreverse headers)
5437         (let ((gnus-nov-is-evil t))
5438           (nconc
5439            (nreverse headers)
5440            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5441              (gnus-get-newsgroup-headers))))))))
5442
5443 (defun gnus-article-get-xrefs ()
5444   "Fill in the Xref value in `gnus-current-headers', if necessary.
5445 This is meant to be called in `gnus-article-internal-prepare-hook'."
5446   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5447                                  gnus-current-headers)))
5448     (or (not gnus-use-cross-reference)
5449         (not headers)
5450         (and (mail-header-xref headers)
5451              (not (string= (mail-header-xref headers) "")))
5452         (let ((case-fold-search t)
5453               xref)
5454           (save-restriction
5455             (nnheader-narrow-to-headers)
5456             (goto-char (point-min))
5457             (when (or (and (not (eobp))
5458                            (eq (downcase (char-after)) ?x)
5459                            (looking-at "Xref:"))
5460                       (search-forward "\nXref:" nil t))
5461               (goto-char (1+ (match-end 0)))
5462               (setq xref (buffer-substring (point)
5463                                            (progn (end-of-line) (point))))
5464               (mail-header-set-xref headers xref)))))))
5465
5466 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5467   "Find article ID and insert the summary line for that article.
5468 OLD-HEADER can either be a header or a line number to insert
5469 the subject line on."
5470   (let* ((line (and (numberp old-header) old-header))
5471          (old-header (and (vectorp old-header) old-header))
5472          (header (cond ((and old-header use-old-header)
5473                         old-header)
5474                        ((and (numberp id)
5475                              (gnus-number-to-header id))
5476                         (gnus-number-to-header id))
5477                        (t
5478                         (gnus-read-header id))))
5479          (number (and (numberp id) id))
5480          d)
5481     (when header
5482       ;; Rebuild the thread that this article is part of and go to the
5483       ;; article we have fetched.
5484       (when (and (not gnus-show-threads)
5485                  old-header)
5486         (when (and number
5487                    (setq d (gnus-data-find (mail-header-number old-header))))
5488           (goto-char (gnus-data-pos d))
5489           (gnus-data-remove
5490            number
5491            (- (gnus-point-at-bol)
5492               (prog1
5493                   (1+ (gnus-point-at-eol))
5494                 (gnus-delete-line))))))
5495       (when old-header
5496         (mail-header-set-number header (mail-header-number old-header)))
5497       (setq gnus-newsgroup-sparse
5498             (delq (setq number (mail-header-number header))
5499                   gnus-newsgroup-sparse))
5500       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5501       (push number gnus-newsgroup-limit)
5502       (gnus-rebuild-thread (mail-header-id header) line)
5503       (gnus-summary-goto-subject number nil t))
5504     (when (and (numberp number)
5505                (> number 0))
5506       ;; We have to update the boundaries even if we can't fetch the
5507       ;; article if ID is a number -- so that the next `P' or `N'
5508       ;; command will fetch the previous (or next) article even
5509       ;; if the one we tried to fetch this time has been canceled.
5510       (when (> number gnus-newsgroup-end)
5511         (setq gnus-newsgroup-end number))
5512       (when (< number gnus-newsgroup-begin)
5513         (setq gnus-newsgroup-begin number))
5514       (setq gnus-newsgroup-unselected
5515             (delq number gnus-newsgroup-unselected)))
5516     ;; Report back a success?
5517     (and header (mail-header-number header))))
5518
5519 ;;; Process/prefix in the summary buffer
5520
5521 (defun gnus-summary-work-articles (n)
5522   "Return a list of articles to be worked upon.
5523 The prefix argument, the list of process marked articles, and the
5524 current article will be taken into consideration."
5525   (save-excursion
5526     (set-buffer gnus-summary-buffer)
5527     (cond
5528      (n
5529       ;; A numerical prefix has been given.
5530       (setq n (prefix-numeric-value n))
5531       (let ((backward (< n 0))
5532             (n (abs (prefix-numeric-value n)))
5533             articles article)
5534         (save-excursion
5535           (while
5536               (and (> n 0)
5537                    (push (setq article (gnus-summary-article-number))
5538                          articles)
5539                    (if backward
5540                        (gnus-summary-find-prev nil article)
5541                      (gnus-summary-find-next nil article)))
5542             (decf n)))
5543         (nreverse articles)))
5544      ((and (gnus-region-active-p) (mark))
5545       (message "region active")
5546       ;; Work on the region between point and mark.
5547       (let ((max (max (point) (mark)))
5548             articles article)
5549         (save-excursion
5550           (goto-char (min (point) (mark)))
5551           (while
5552               (and
5553                (push (setq article (gnus-summary-article-number)) articles)
5554                (gnus-summary-find-next nil article)
5555                (< (point) max)))
5556           (nreverse articles))))
5557      (gnus-newsgroup-processable
5558       ;; There are process-marked articles present.
5559       ;; Save current state.
5560       (gnus-summary-save-process-mark)
5561       ;; Return the list.
5562       (reverse gnus-newsgroup-processable))
5563      (t
5564       ;; Just return the current article.
5565       (list (gnus-summary-article-number))))))
5566
5567 (defmacro gnus-summary-iterate (arg &rest forms)
5568   "Iterate over the process/prefixed articles and do FORMS.
5569 ARG is the interactive prefix given to the command.  FORMS will be
5570 executed with point over the summary line of the articles."
5571   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5572     `(let ((,articles (gnus-summary-work-articles ,arg)))
5573        (while ,articles
5574          (gnus-summary-goto-subject (car ,articles))
5575          ,@forms
5576          (pop ,articles)))))
5577
5578 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5579 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5580
5581 (defun gnus-summary-save-process-mark ()
5582   "Push the current set of process marked articles on the stack."
5583   (interactive)
5584   (push (copy-sequence gnus-newsgroup-processable)
5585         gnus-newsgroup-process-stack))
5586
5587 (defun gnus-summary-kill-process-mark ()
5588   "Push the current set of process marked articles on the stack and unmark."
5589   (interactive)
5590   (gnus-summary-save-process-mark)
5591   (gnus-summary-unmark-all-processable))
5592
5593 (defun gnus-summary-yank-process-mark ()
5594   "Pop the last process mark state off the stack and restore it."
5595   (interactive)
5596   (unless gnus-newsgroup-process-stack
5597     (error "Empty mark stack"))
5598   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5599
5600 (defun gnus-summary-process-mark-set (set)
5601   "Make SET into the current process marked articles."
5602   (gnus-summary-unmark-all-processable)
5603   (while set
5604     (gnus-summary-set-process-mark (pop set))))
5605
5606 ;;; Searching and stuff
5607
5608 (defun gnus-summary-search-group (&optional backward use-level)
5609   "Search for next unread newsgroup.
5610 If optional argument BACKWARD is non-nil, search backward instead."
5611   (save-excursion
5612     (set-buffer gnus-group-buffer)
5613     (when (gnus-group-search-forward
5614            backward nil (if use-level (gnus-group-group-level) nil))
5615       (gnus-group-group-name))))
5616
5617 (defun gnus-summary-best-group (&optional exclude-group)
5618   "Find the name of the best unread group.
5619 If EXCLUDE-GROUP, do not go to this group."
5620   (save-excursion
5621     (set-buffer gnus-group-buffer)
5622     (save-excursion
5623       (gnus-group-best-unread-group exclude-group))))
5624
5625 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5626   (if backward (gnus-summary-find-prev)
5627     (let* ((dummy (gnus-summary-article-intangible-p))
5628            (article (or article (gnus-summary-article-number)))
5629            (arts (gnus-data-find-list article))
5630            result)
5631       (when (and (not dummy)
5632                  (or (not gnus-summary-check-current)
5633                      (not unread)
5634                      (not (gnus-data-unread-p (car arts)))))
5635         (setq arts (cdr arts)))
5636       (when (setq result
5637                   (if unread
5638                       (progn
5639                         (while arts
5640                           (when (or (and undownloaded
5641                                          (eq gnus-undownloaded-mark
5642                                              (gnus-data-mark (car arts))))
5643                                     (gnus-data-unread-p (car arts)))
5644                             (setq result (car arts)
5645                                   arts nil))
5646                           (setq arts (cdr arts)))
5647                         result)
5648                     (car arts)))
5649         (goto-char (gnus-data-pos result))
5650         (gnus-data-number result)))))
5651
5652 (defun gnus-summary-find-prev (&optional unread article)
5653   (let* ((eobp (eobp))
5654          (article (or article (gnus-summary-article-number)))
5655          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5656          result)
5657     (when (and (not eobp)
5658                (or (not gnus-summary-check-current)
5659                    (not unread)
5660                    (not (gnus-data-unread-p (car arts)))))
5661       (setq arts (cdr arts)))
5662     (when (setq result
5663                 (if unread
5664                     (progn
5665                       (while arts
5666                         (when (gnus-data-unread-p (car arts))
5667                           (setq result (car arts)
5668                                 arts nil))
5669                         (setq arts (cdr arts)))
5670                       result)
5671                   (car arts)))
5672       (goto-char (gnus-data-pos result))
5673       (gnus-data-number result))))
5674
5675 (defun gnus-summary-find-subject (subject &optional unread backward article)
5676   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5677          (article (or article (gnus-summary-article-number)))
5678          (articles (gnus-data-list backward))
5679          (arts (gnus-data-find-list article articles))
5680          result)
5681     (when (or (not gnus-summary-check-current)
5682               (not unread)
5683               (not (gnus-data-unread-p (car arts))))
5684       (setq arts (cdr arts)))
5685     (while arts
5686       (and (or (not unread)
5687                (gnus-data-unread-p (car arts)))
5688            (vectorp (gnus-data-header (car arts)))
5689            (gnus-subject-equal
5690             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5691            (setq result (car arts)
5692                  arts nil))
5693       (setq arts (cdr arts)))
5694     (and result
5695          (goto-char (gnus-data-pos result))
5696          (gnus-data-number result))))
5697
5698 (defun gnus-summary-search-forward (&optional unread subject backward)
5699   "Search forward for an article.
5700 If UNREAD, look for unread articles.  If SUBJECT, look for
5701 articles with that subject.  If BACKWARD, search backward instead."
5702   (cond (subject (gnus-summary-find-subject subject unread backward))
5703         (backward (gnus-summary-find-prev unread))
5704         (t (gnus-summary-find-next unread))))
5705
5706 (defun gnus-recenter (&optional n)
5707   "Center point in window and redisplay frame.
5708 Also do horizontal recentering."
5709   (interactive "P")
5710   (when (and gnus-auto-center-summary
5711              (not (eq gnus-auto-center-summary 'vertical)))
5712     (gnus-horizontal-recenter))
5713   (recenter n))
5714
5715 (defun gnus-summary-recenter ()
5716   "Center point in the summary window.
5717 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5718 displayed, no centering will be performed."
5719   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5720   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5721   (interactive)
5722   (let* ((top (cond ((< (window-height) 4) 0)
5723                     ((< (window-height) 7) 1)
5724                     (t (if (numberp gnus-auto-center-summary)
5725                            gnus-auto-center-summary
5726                          2))))
5727          (height (1- (window-height)))
5728          (bottom (save-excursion (goto-char (point-max))
5729                                  (forward-line (- height))
5730                                  (point)))
5731          (window (get-buffer-window (current-buffer))))
5732     ;; The user has to want it.
5733     (when gnus-auto-center-summary
5734       (when (get-buffer-window gnus-article-buffer)
5735         ;; Only do recentering when the article buffer is displayed,
5736         ;; Set the window start to either `bottom', which is the biggest
5737         ;; possible valid number, or the second line from the top,
5738         ;; whichever is the least.
5739         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5740           (if (> bottom top-pos)
5741               ;; Keep the second line from the top visible
5742               (set-window-start window top-pos t)
5743             ;; Try to keep the bottom line visible; if it's partially
5744             ;; obscured, either scroll one more line to make it fully
5745             ;; visible, or revert to using TOP-POS.
5746             (save-excursion
5747               (goto-char (point-max))
5748               (forward-line -1)
5749               (let ((last-line-start (point)))
5750                 (goto-char bottom)
5751                 (set-window-start window (point) t)
5752                 (when (not (pos-visible-in-window-p last-line-start window))
5753                   (forward-line 1)
5754                   (set-window-start window (min (point) top-pos) t)))))))
5755       ;; Do horizontal recentering while we're at it.
5756       (when (and (get-buffer-window (current-buffer) t)
5757                  (not (eq gnus-auto-center-summary 'vertical)))
5758         (let ((selected (selected-window)))
5759           (select-window (get-buffer-window (current-buffer) t))
5760           (gnus-summary-position-point)
5761           (gnus-horizontal-recenter)
5762           (select-window selected))))))
5763
5764 (defun gnus-summary-jump-to-group (newsgroup)
5765   "Move point to NEWSGROUP in group mode buffer."
5766   ;; Keep update point of group mode buffer if visible.
5767   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5768       (save-window-excursion
5769         ;; Take care of tree window mode.
5770         (when (get-buffer-window gnus-group-buffer)
5771           (pop-to-buffer gnus-group-buffer))
5772         (gnus-group-jump-to-group newsgroup))
5773     (save-excursion
5774       ;; Take care of tree window mode.
5775       (if (get-buffer-window gnus-group-buffer)
5776           (pop-to-buffer gnus-group-buffer)
5777         (set-buffer gnus-group-buffer))
5778       (gnus-group-jump-to-group newsgroup))))
5779
5780 ;; This function returns a list of article numbers based on the
5781 ;; difference between the ranges of read articles in this group and
5782 ;; the range of active articles.
5783 (defun gnus-list-of-unread-articles (group)
5784   (let* ((read (gnus-info-read (gnus-get-info group)))
5785          (active (or (gnus-active group) (gnus-activate-group group)))
5786          (last (cdr active))
5787          first nlast unread)
5788     ;; If none are read, then all are unread.
5789     (if (not read)
5790         (setq first (car active))
5791       ;; If the range of read articles is a single range, then the
5792       ;; first unread article is the article after the last read
5793       ;; article.  Sounds logical, doesn't it?
5794       (if (and (not (listp (cdr read)))
5795                (or (< (car read) (car active))
5796                    (progn (setq read (list read))
5797                           nil)))
5798           (setq first (max (car active) (1+ (cdr read))))
5799         ;; `read' is a list of ranges.
5800         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5801                                   (caar read)))
5802                   1)
5803           (setq first (car active)))
5804         (while read
5805           (when first
5806             (while (< first nlast)
5807               (push first unread)
5808               (setq first (1+ first))))
5809           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5810           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5811           (setq read (cdr read)))))
5812     ;; And add the last unread articles.
5813     (while (<= first last)
5814       (push first unread)
5815       (setq first (1+ first)))
5816     ;; Return the list of unread articles.
5817     (delq 0 (nreverse unread))))
5818
5819 (defun gnus-list-of-read-articles (group)
5820   "Return a list of unread, unticked and non-dormant articles."
5821   (let* ((info (gnus-get-info group))
5822          (marked (gnus-info-marks info))
5823          (active (gnus-active group)))
5824     (and info active
5825          (gnus-set-difference
5826           (gnus-sorted-complement
5827            (gnus-uncompress-range active)
5828            (gnus-list-of-unread-articles group))
5829           (append
5830            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5831            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5832
5833 ;; Various summary commands
5834
5835 (defun gnus-summary-select-article-buffer ()
5836   "Reconfigure windows to show article buffer."
5837   (interactive)
5838   (if (not (gnus-buffer-live-p gnus-article-buffer))
5839       (error "There is no article buffer for this summary buffer")
5840     (gnus-configure-windows 'article)
5841     (select-window (get-buffer-window gnus-article-buffer))))
5842
5843 (defun gnus-summary-universal-argument (arg)
5844   "Perform any operation on all articles that are process/prefixed."
5845   (interactive "P")
5846   (let ((articles (gnus-summary-work-articles arg))
5847         func article)
5848     (if (eq
5849          (setq
5850           func
5851           (key-binding
5852            (read-key-sequence
5853             (substitute-command-keys
5854              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5855          'undefined)
5856         (gnus-error 1 "Undefined key")
5857       (save-excursion
5858         (while articles
5859           (gnus-summary-goto-subject (setq article (pop articles)))
5860           (let (gnus-newsgroup-processable)
5861             (command-execute func))
5862           (gnus-summary-remove-process-mark article)))))
5863   (gnus-summary-position-point))
5864
5865 (defun gnus-summary-toggle-truncation (&optional arg)
5866   "Toggle truncation of summary lines.
5867 With arg, turn line truncation on iff arg is positive."
5868   (interactive "P")
5869   (setq truncate-lines
5870         (if (null arg) (not truncate-lines)
5871           (> (prefix-numeric-value arg) 0)))
5872   (redraw-display))
5873
5874 (defun gnus-summary-reselect-current-group (&optional all rescan)
5875   "Exit and then reselect the current newsgroup.
5876 The prefix argument ALL means to select all articles."
5877   (interactive "P")
5878   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5879     (error "Ephemeral groups can't be reselected"))
5880   (let ((current-subject (gnus-summary-article-number))
5881         (group gnus-newsgroup-name))
5882     (setq gnus-newsgroup-begin nil)
5883     (gnus-summary-exit)
5884     ;; We have to adjust the point of group mode buffer because
5885     ;; point was moved to the next unread newsgroup by exiting.
5886     (gnus-summary-jump-to-group group)
5887     (when rescan
5888       (save-excursion
5889         (save-window-excursion
5890           ;; Don't show group contents.
5891           (set-window-start (selected-window) (point-max))
5892           (gnus-group-get-new-news-this-group 1))))
5893     (gnus-group-read-group all t)
5894     (gnus-summary-goto-subject current-subject nil t)))
5895
5896 (defun gnus-summary-rescan-group (&optional all)
5897   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5898   (interactive "P")
5899   (gnus-summary-reselect-current-group all t))
5900
5901 (defun gnus-summary-update-info (&optional non-destructive)
5902   (save-excursion
5903     (let ((group gnus-newsgroup-name))
5904       (when group
5905         (when gnus-newsgroup-kill-headers
5906           (setq gnus-newsgroup-killed
5907                 (gnus-compress-sequence
5908                  (nconc
5909                   (gnus-set-sorted-intersection
5910                    (gnus-uncompress-range gnus-newsgroup-killed)
5911                    (setq gnus-newsgroup-unselected
5912                          (sort gnus-newsgroup-unselected '<)))
5913                   (setq gnus-newsgroup-unreads
5914                         (sort gnus-newsgroup-unreads '<)))
5915                  t)))
5916         (unless (listp (cdr gnus-newsgroup-killed))
5917           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5918         (let ((headers gnus-newsgroup-headers))
5919           ;; Set the new ranges of read articles.
5920           (save-excursion
5921             (set-buffer gnus-group-buffer)
5922             (gnus-undo-force-boundary))
5923           (gnus-update-read-articles
5924            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5925           ;; Set the current article marks.
5926           (let ((gnus-newsgroup-scored
5927                  (if (and (not gnus-save-score)
5928                           (not non-destructive))
5929                      nil
5930                    gnus-newsgroup-scored)))
5931             (save-excursion
5932               (gnus-update-marks)))
5933           ;; Do the cross-ref thing.
5934           (when gnus-use-cross-reference
5935             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5936           ;; Do not switch windows but change the buffer to work.
5937           (set-buffer gnus-group-buffer)
5938           (unless (gnus-ephemeral-group-p group)
5939             (gnus-group-update-group group)))))))
5940
5941 (defun gnus-summary-save-newsrc (&optional force)
5942   "Save the current number of read/marked articles in the dribble buffer.
5943 The dribble buffer will then be saved.
5944 If FORCE (the prefix), also save the .newsrc file(s)."
5945   (interactive "P")
5946   (gnus-summary-update-info t)
5947   (if force
5948       (gnus-save-newsrc-file)
5949     (gnus-dribble-save)))
5950
5951 (defun gnus-summary-exit (&optional temporary)
5952   "Exit reading current newsgroup, and then return to group selection mode.
5953 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5954   (interactive)
5955   (gnus-set-global-variables)
5956   (gnus-kill-save-kill-buffer)
5957   (gnus-async-halt-prefetch)
5958   (let* ((group gnus-newsgroup-name)
5959          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5960          (mode major-mode)
5961          (group-point nil)
5962          (buf (current-buffer)))
5963     (unless quit-config
5964       ;; Do adaptive scoring, and possibly save score files.
5965       (when gnus-newsgroup-adaptive
5966         (gnus-score-adaptive))
5967       (when gnus-use-scoring
5968         (gnus-score-save)))
5969     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5970     ;; If we have several article buffers, we kill them at exit.
5971     (unless gnus-single-article-buffer
5972       (gnus-kill-buffer gnus-original-article-buffer)
5973       (setq gnus-article-current nil))
5974     (when gnus-use-cache
5975       (gnus-cache-possibly-remove-articles)
5976       (gnus-cache-save-buffers))
5977     (gnus-async-prefetch-remove-group group)
5978     (when gnus-suppress-duplicates
5979       (gnus-dup-enter-articles))
5980     (when gnus-use-trees
5981       (gnus-tree-close group))
5982     (when gnus-use-cache
5983       (gnus-cache-write-active))
5984     ;; Remove entries for this group.
5985     (nnmail-purge-split-history (gnus-group-real-name group))
5986     ;; Make all changes in this group permanent.
5987     (unless quit-config
5988       (gnus-run-hooks 'gnus-exit-group-hook)
5989       (gnus-summary-update-info))
5990     (gnus-close-group group)
5991     ;; Make sure where we were, and go to next newsgroup.
5992     (set-buffer gnus-group-buffer)
5993     (unless quit-config
5994       (gnus-group-jump-to-group group))
5995     (gnus-run-hooks 'gnus-summary-exit-hook)
5996     (unless (or quit-config
5997                 ;; If this group has disappeared from the summary
5998                 ;; buffer, don't skip forwards.
5999                 (not (string= group (gnus-group-group-name))))
6000       (gnus-group-next-unread-group 1))
6001     (setq group-point (point))
6002     (if temporary
6003         nil                             ;Nothing to do.
6004       ;; If we have several article buffers, we kill them at exit.
6005       (unless gnus-single-article-buffer
6006         (gnus-kill-buffer gnus-article-buffer)
6007         (gnus-kill-buffer gnus-original-article-buffer)
6008         (setq gnus-article-current nil))
6009       (set-buffer buf)
6010       (if (not gnus-kill-summary-on-exit)
6011           (progn
6012             (gnus-deaden-summary)
6013             (setq mode nil))
6014         ;; We set all buffer-local variables to nil.  It is unclear why
6015         ;; this is needed, but if we don't, buffer-local variables are
6016         ;; not garbage-collected, it seems.  This would the lead to en
6017         ;; ever-growing Emacs.
6018         (gnus-summary-clear-local-variables)
6019         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6020           (gnus-summary-clear-local-variables))
6021         (when (get-buffer gnus-article-buffer)
6022           (bury-buffer gnus-article-buffer))
6023         ;; We clear the global counterparts of the buffer-local
6024         ;; variables as well, just to be on the safe side.
6025         (set-buffer gnus-group-buffer)
6026         (gnus-summary-clear-local-variables)
6027         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6028           (gnus-summary-clear-local-variables)))
6029       (setq gnus-current-select-method gnus-select-method)
6030       (pop-to-buffer gnus-group-buffer)
6031       (if (not quit-config)
6032           (progn
6033             (goto-char group-point)
6034             (gnus-configure-windows 'group 'force)
6035             (unless (pos-visible-in-window-p)
6036               (forward-line (/ (static-if (featurep 'xemacs)
6037                                    (window-displayed-height)
6038                                  (1- (window-height)))
6039                                -2))
6040               (set-window-start (selected-window) (point))
6041               (goto-char group-point)))
6042         (gnus-handle-ephemeral-exit quit-config))
6043       ;; Return to group mode buffer.
6044       (when (eq mode 'gnus-summary-mode)
6045         (gnus-kill-buffer buf))
6046       ;; Clear the current group name.
6047       (unless quit-config
6048         (setq gnus-newsgroup-name nil)))))
6049
6050 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6051 (defun gnus-summary-exit-no-update (&optional no-questions)
6052   "Quit reading current newsgroup without updating read article info."
6053   (interactive)
6054   (let* ((group gnus-newsgroup-name)
6055          (quit-config (gnus-group-quit-config group)))
6056     (when (or no-questions
6057               gnus-expert-user
6058               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6059       (gnus-async-halt-prefetch)
6060       (mapcar 'funcall
6061               (delq 'gnus-summary-expire-articles
6062                     (copy-sequence gnus-summary-prepare-exit-hook)))
6063       ;; If we have several article buffers, we kill them at exit.
6064       (unless gnus-single-article-buffer
6065         (gnus-kill-buffer gnus-article-buffer)
6066         (gnus-kill-buffer gnus-original-article-buffer)
6067         (setq gnus-article-current nil))
6068       (if (not gnus-kill-summary-on-exit)
6069           (gnus-deaden-summary)
6070         (gnus-close-group group)
6071         (gnus-summary-clear-local-variables)
6072         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6073           (gnus-summary-clear-local-variables))
6074         (set-buffer gnus-group-buffer)
6075         (gnus-summary-clear-local-variables)
6076         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6077           (gnus-summary-clear-local-variables))
6078         (when (get-buffer gnus-summary-buffer)
6079           (kill-buffer gnus-summary-buffer)))
6080       (unless gnus-single-article-buffer
6081         (setq gnus-article-current nil))
6082       (when gnus-use-trees
6083         (gnus-tree-close group))
6084       (gnus-async-prefetch-remove-group group)
6085       (when (get-buffer gnus-article-buffer)
6086         (bury-buffer gnus-article-buffer))
6087       ;; Return to the group buffer.
6088       (gnus-configure-windows 'group 'force)
6089       ;; Clear the current group name.
6090       (setq gnus-newsgroup-name nil)
6091       (when (equal (gnus-group-group-name) group)
6092         (gnus-group-next-unread-group 1))
6093       (when quit-config
6094         (gnus-handle-ephemeral-exit quit-config)))))
6095
6096 (defun gnus-handle-ephemeral-exit (quit-config)
6097   "Handle movement when leaving an ephemeral group.
6098 The state which existed when entering the ephemeral is reset."
6099   (if (not (buffer-name (car quit-config)))
6100       (gnus-configure-windows 'group 'force)
6101     (set-buffer (car quit-config))
6102     (cond ((eq major-mode 'gnus-summary-mode)
6103            (gnus-set-global-variables))
6104           ((eq major-mode 'gnus-article-mode)
6105            (save-excursion
6106              ;; The `gnus-summary-buffer' variable may point
6107              ;; to the old summary buffer when using a single
6108              ;; article buffer.
6109              (unless (gnus-buffer-live-p gnus-summary-buffer)
6110                (set-buffer gnus-group-buffer))
6111              (set-buffer gnus-summary-buffer)
6112              (gnus-set-global-variables))))
6113     (if (or (eq (cdr quit-config) 'article)
6114             (eq (cdr quit-config) 'pick))
6115         (progn
6116           ;; The current article may be from the ephemeral group
6117           ;; thus it is best that we reload this article
6118           (gnus-summary-show-article)
6119           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6120               (gnus-configure-windows 'pick 'force)
6121             (gnus-configure-windows (cdr quit-config) 'force)))
6122       (gnus-configure-windows (cdr quit-config) 'force))
6123     (when (eq major-mode 'gnus-summary-mode)
6124       (gnus-summary-next-subject 1 nil t)
6125       (gnus-summary-recenter)
6126       (gnus-summary-position-point))))
6127
6128 (defun gnus-summary-preview-mime-message ()
6129   "MIME decode and play this message."
6130   (interactive)
6131   (let ((gnus-break-pages nil)
6132         (gnus-show-mime t))
6133     (gnus-summary-select-article gnus-show-all-headers t))
6134   (select-window (get-buffer-window gnus-article-buffer)))
6135
6136 ;;; Dead summaries.
6137
6138 (defvar gnus-dead-summary-mode-map nil)
6139
6140 (unless gnus-dead-summary-mode-map
6141   (setq gnus-dead-summary-mode-map (make-keymap))
6142   (suppress-keymap gnus-dead-summary-mode-map)
6143   (substitute-key-definition
6144    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6145   (let ((keys '("\C-d" "\r" "\177" [delete])))
6146     (while keys
6147       (define-key gnus-dead-summary-mode-map
6148         (pop keys) 'gnus-summary-wake-up-the-dead))))
6149
6150 (defvar gnus-dead-summary-mode nil
6151   "Minor mode for Gnus summary buffers.")
6152
6153 (defun gnus-dead-summary-mode (&optional arg)
6154   "Minor mode for Gnus summary buffers."
6155   (interactive "P")
6156   (when (eq major-mode 'gnus-summary-mode)
6157     (make-local-variable 'gnus-dead-summary-mode)
6158     (setq gnus-dead-summary-mode
6159           (if (null arg) (not gnus-dead-summary-mode)
6160             (> (prefix-numeric-value arg) 0)))
6161     (when gnus-dead-summary-mode
6162       (gnus-add-minor-mode
6163        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6164
6165 (defun gnus-deaden-summary ()
6166   "Make the current summary buffer into a dead summary buffer."
6167   ;; Kill any previous dead summary buffer.
6168   (when (and gnus-dead-summary
6169              (buffer-name gnus-dead-summary))
6170     (save-excursion
6171       (set-buffer gnus-dead-summary)
6172       (when gnus-dead-summary-mode
6173         (kill-buffer (current-buffer)))))
6174   ;; Make this the current dead summary.
6175   (setq gnus-dead-summary (current-buffer))
6176   (gnus-dead-summary-mode 1)
6177   (let ((name (buffer-name)))
6178     (when (string-match "Summary" name)
6179       (rename-buffer
6180        (concat (substring name 0 (match-beginning 0)) "Dead "
6181                (substring name (match-beginning 0)))
6182        t)
6183       (bury-buffer))))
6184
6185 (defun gnus-kill-or-deaden-summary (buffer)
6186   "Kill or deaden the summary BUFFER."
6187   (save-excursion
6188     (when (and (buffer-name buffer)
6189                (not gnus-single-article-buffer))
6190       (save-excursion
6191         (set-buffer buffer)
6192         (gnus-kill-buffer gnus-article-buffer)
6193         (gnus-kill-buffer gnus-original-article-buffer)))
6194     (cond (gnus-kill-summary-on-exit
6195            (when (and gnus-use-trees
6196                       (gnus-buffer-exists-p buffer))
6197              (save-excursion
6198                (set-buffer buffer)
6199                (gnus-tree-close gnus-newsgroup-name)))
6200            (gnus-kill-buffer buffer))
6201           ((gnus-buffer-exists-p buffer)
6202            (save-excursion
6203              (set-buffer buffer)
6204              (gnus-deaden-summary))))))
6205
6206 (defun gnus-summary-wake-up-the-dead (&rest args)
6207   "Wake up the dead summary buffer."
6208   (interactive)
6209   (gnus-dead-summary-mode -1)
6210   (let ((name (buffer-name)))
6211     (when (string-match "Dead " name)
6212       (rename-buffer
6213        (concat (substring name 0 (match-beginning 0))
6214                (substring name (match-end 0)))
6215        t)))
6216   (gnus-message 3 "This dead summary is now alive again"))
6217
6218 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6219 (defun gnus-summary-fetch-faq (&optional faq-dir)
6220   "Fetch the FAQ for the current group.
6221 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6222 in."
6223   (interactive
6224    (list
6225     (when current-prefix-arg
6226       (completing-read
6227        "Faq dir: " (and (listp gnus-group-faq-directory)
6228                         (mapcar (lambda (file) (list file))
6229                                 gnus-group-faq-directory))))))
6230   (let (gnus-faq-buffer)
6231     (when (setq gnus-faq-buffer
6232                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6233       (gnus-configure-windows 'summary-faq))))
6234
6235 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6236 (defun gnus-summary-describe-group (&optional force)
6237   "Describe the current newsgroup."
6238   (interactive "P")
6239   (gnus-group-describe-group force gnus-newsgroup-name))
6240
6241 (defun gnus-summary-describe-briefly ()
6242   "Describe summary mode commands briefly."
6243   (interactive)
6244   (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")))
6245
6246 ;; Walking around group mode buffer from summary mode.
6247
6248 (defun gnus-summary-next-group (&optional no-article target-group backward)
6249   "Exit current newsgroup and then select next unread newsgroup.
6250 If prefix argument NO-ARTICLE is non-nil, no article is selected
6251 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6252 previous group instead."
6253   (interactive "P")
6254   ;; Stop pre-fetching.
6255   (gnus-async-halt-prefetch)
6256   (let ((current-group gnus-newsgroup-name)
6257         (current-buffer (current-buffer))
6258         entered)
6259     ;; First we semi-exit this group to update Xrefs and all variables.
6260     ;; We can't do a real exit, because the window conf must remain
6261     ;; the same in case the user is prompted for info, and we don't
6262     ;; want the window conf to change before that...
6263     (gnus-summary-exit t)
6264     (while (not entered)
6265       ;; Then we find what group we are supposed to enter.
6266       (set-buffer gnus-group-buffer)
6267       (gnus-group-jump-to-group current-group)
6268       (setq target-group
6269             (or target-group
6270                 (if (eq gnus-keep-same-level 'best)
6271                     (gnus-summary-best-group gnus-newsgroup-name)
6272                   (gnus-summary-search-group backward gnus-keep-same-level))))
6273       (if (not target-group)
6274           ;; There are no further groups, so we return to the group
6275           ;; buffer.
6276           (progn
6277             (gnus-message 5 "Returning to the group buffer")
6278             (setq entered t)
6279             (when (gnus-buffer-live-p current-buffer)
6280               (set-buffer current-buffer)
6281               (gnus-summary-exit))
6282             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6283         ;; We try to enter the target group.
6284         (gnus-group-jump-to-group target-group)
6285         (let ((unreads (gnus-group-group-unread)))
6286           (if (and (or (eq t unreads)
6287                        (and unreads (not (zerop unreads))))
6288                    (gnus-summary-read-group
6289                     target-group nil no-article
6290                     (and (buffer-name current-buffer) current-buffer)
6291                     nil backward))
6292               (setq entered t)
6293             (setq current-group target-group
6294                   target-group nil)))))))
6295
6296 (defun gnus-summary-prev-group (&optional no-article)
6297   "Exit current newsgroup and then select previous unread newsgroup.
6298 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6299   (interactive "P")
6300   (gnus-summary-next-group no-article nil t))
6301
6302 ;; Walking around summary lines.
6303
6304 (defun gnus-summary-first-subject (&optional unread undownloaded)
6305   "Go to the first unread subject.
6306 If UNREAD is non-nil, go to the first unread article.
6307 Returns the article selected or nil if there are no unread articles."
6308   (interactive "P")
6309   (prog1
6310       (cond
6311        ;; Empty summary.
6312        ((null gnus-newsgroup-data)
6313         (gnus-message 3 "No articles in the group")
6314         nil)
6315        ;; Pick the first article.
6316        ((not unread)
6317         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6318         (gnus-data-number (car gnus-newsgroup-data)))
6319        ;; No unread articles.
6320        ((null gnus-newsgroup-unreads)
6321         (gnus-message 3 "No more unread articles")
6322         nil)
6323        ;; Find the first unread article.
6324        (t
6325         (let ((data gnus-newsgroup-data))
6326           (while (and data
6327                       (and (not (and undownloaded
6328                                      (eq gnus-undownloaded-mark
6329                                          (gnus-data-mark (car data)))))
6330                            (not (gnus-data-unread-p (car data)))))
6331             (setq data (cdr data)))
6332           (when data
6333             (goto-char (gnus-data-pos (car data)))
6334             (gnus-data-number (car data))))))
6335     (gnus-summary-position-point)))
6336
6337 (defun gnus-summary-next-subject (n &optional unread dont-display)
6338   "Go to next N'th summary line.
6339 If N is negative, go to the previous N'th subject line.
6340 If UNREAD is non-nil, only unread articles are selected.
6341 The difference between N and the actual number of steps taken is
6342 returned."
6343   (interactive "p")
6344   (let ((backward (< n 0))
6345         (n (abs n)))
6346     (while (and (> n 0)
6347                 (if backward
6348                     (gnus-summary-find-prev unread)
6349                   (gnus-summary-find-next unread)))
6350       (unless (zerop (setq n (1- n)))
6351         (gnus-summary-show-thread)))
6352     (when (/= 0 n)
6353       (gnus-message 7 "No more%s articles"
6354                     (if unread " unread" "")))
6355     (unless dont-display
6356       (gnus-summary-recenter)
6357       (gnus-summary-position-point))
6358     n))
6359
6360 (defun gnus-summary-next-unread-subject (n)
6361   "Go to next N'th unread summary line."
6362   (interactive "p")
6363   (gnus-summary-next-subject n t))
6364
6365 (defun gnus-summary-prev-subject (n &optional unread)
6366   "Go to previous N'th summary line.
6367 If optional argument UNREAD is non-nil, only unread article is selected."
6368   (interactive "p")
6369   (gnus-summary-next-subject (- n) unread))
6370
6371 (defun gnus-summary-prev-unread-subject (n)
6372   "Go to previous N'th unread summary line."
6373   (interactive "p")
6374   (gnus-summary-next-subject (- n) t))
6375
6376 (defun gnus-summary-goto-subject (article &optional force silent)
6377   "Go the subject line of ARTICLE.
6378 If FORCE, also allow jumping to articles not currently shown."
6379   (interactive "nArticle number: ")
6380   (let ((b (point))
6381         (data (gnus-data-find article)))
6382     ;; We read in the article if we have to.
6383     (and (not data)
6384          force
6385          (gnus-summary-insert-subject
6386           article
6387           (if (or (numberp force) (vectorp force)) force)
6388           t)
6389          (setq data (gnus-data-find article)))
6390     (goto-char b)
6391     (if (not data)
6392         (progn
6393           (unless silent
6394             (gnus-message 3 "Can't find article %d" article))
6395           nil)
6396       (let ((pt (gnus-data-pos data)))
6397         (goto-char pt)
6398         (gnus-summary-set-article-display-arrow pt))
6399       (gnus-summary-position-point)
6400       article)))
6401
6402 ;; Walking around summary lines with displaying articles.
6403
6404 (defun gnus-summary-expand-window (&optional arg)
6405   "Make the summary buffer take up the entire Emacs frame.
6406 Given a prefix, will force an `article' buffer configuration."
6407   (interactive "P")
6408   (if arg
6409       (gnus-configure-windows 'article 'force)
6410     (gnus-configure-windows 'summary 'force)))
6411
6412 (defun gnus-summary-display-article (article &optional all-header)
6413   "Display ARTICLE in article buffer."
6414   (when (gnus-buffer-live-p gnus-article-buffer)
6415     (with-current-buffer gnus-article-buffer
6416       (set-buffer-multibyte t)))
6417   (gnus-set-global-variables)
6418   (when (gnus-buffer-live-p gnus-article-buffer)
6419     (with-current-buffer gnus-article-buffer
6420       (setq gnus-article-charset gnus-newsgroup-charset)
6421       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6422   (if (null article)
6423       nil
6424     (prog1
6425         (if gnus-summary-display-article-function
6426             (funcall gnus-summary-display-article-function article all-header)
6427           (gnus-article-prepare article all-header))
6428       (with-current-buffer gnus-article-buffer
6429         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6430              nil))
6431       (gnus-run-hooks 'gnus-select-article-hook)
6432       (when (and gnus-current-article
6433                  (not (zerop gnus-current-article)))
6434         (gnus-summary-goto-subject gnus-current-article))
6435       (gnus-summary-recenter)
6436       (when (and gnus-use-trees gnus-show-threads)
6437         (gnus-possibly-generate-tree article)
6438         (gnus-highlight-selected-tree article))
6439       ;; Successfully display article.
6440       (gnus-article-set-window-start
6441        (cdr (assq article gnus-newsgroup-bookmarks))))))
6442
6443 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6444   "Select the current article.
6445 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6446 non-nil, the article will be re-fetched even if it already present in
6447 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6448 be displayed."
6449   ;; Make sure we are in the summary buffer to work around bbdb bug.
6450   (unless (eq major-mode 'gnus-summary-mode)
6451     (set-buffer gnus-summary-buffer))
6452   (let ((article (or article (gnus-summary-article-number)))
6453         (all-headers (not (not all-headers))) ;Must be T or NIL.
6454         gnus-summary-display-article-function)
6455     (and (not pseudo)
6456          (gnus-summary-article-pseudo-p article)
6457          (error "This is a pseudo-article"))
6458     (save-excursion
6459       (set-buffer gnus-summary-buffer)
6460       (if (or (and gnus-single-article-buffer
6461                    (or (null gnus-current-article)
6462                        (null gnus-article-current)
6463                        (null (get-buffer gnus-article-buffer))
6464                        (not (eq article (cdr gnus-article-current)))
6465                        (not (equal (car gnus-article-current)
6466                                    gnus-newsgroup-name))))
6467               (and (not gnus-single-article-buffer)
6468                    (or (null gnus-current-article)
6469                        (not (eq gnus-current-article article))))
6470               force)
6471           ;; The requested article is different from the current article.
6472           (progn
6473             (gnus-summary-display-article article all-headers)
6474             (when (or all-headers gnus-show-all-headers)
6475               (gnus-article-show-all-headers))
6476             (gnus-article-set-window-start
6477              (cdr (assq article gnus-newsgroup-bookmarks)))
6478             article)
6479         (when (or all-headers gnus-show-all-headers)
6480           (gnus-article-show-all-headers))
6481         'old))))
6482
6483 (defun gnus-summary-force-verify-and-decrypt ()
6484   (interactive)
6485   (let ((mm-verify-option 'known)
6486         (mm-decrypt-option 'known))
6487     (gnus-summary-select-article nil 'force)))
6488
6489 (defun gnus-summary-set-current-mark (&optional current-mark)
6490   "Obsolete function."
6491   nil)
6492
6493 (defun gnus-summary-next-article (&optional unread subject backward push)
6494   "Select the next article.
6495 If UNREAD, only unread articles are selected.
6496 If SUBJECT, only articles with SUBJECT are selected.
6497 If BACKWARD, the previous article is selected instead of the next."
6498   (interactive "P")
6499   (cond
6500    ;; Is there such an article?
6501    ((and (gnus-summary-search-forward unread subject backward)
6502          (or (gnus-summary-display-article (gnus-summary-article-number))
6503              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6504     (gnus-summary-position-point))
6505    ;; If not, we try the first unread, if that is wanted.
6506    ((and subject
6507          gnus-auto-select-same
6508          (gnus-summary-first-unread-article))
6509     (gnus-summary-position-point)
6510     (gnus-message 6 "Wrapped"))
6511    ;; Try to get next/previous article not displayed in this group.
6512    ((and gnus-auto-extend-newsgroup
6513          (not unread) (not subject))
6514     (gnus-summary-goto-article
6515      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6516      nil (count-lines (point-min) (point))))
6517    ;; Go to next/previous group.
6518    (t
6519     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6520       (gnus-summary-jump-to-group gnus-newsgroup-name))
6521     (let ((cmd last-command-char)
6522           (point
6523            (save-excursion
6524              (set-buffer gnus-group-buffer)
6525              (point)))
6526           (group
6527            (if (eq gnus-keep-same-level 'best)
6528                (gnus-summary-best-group gnus-newsgroup-name)
6529              (gnus-summary-search-group backward gnus-keep-same-level))))
6530       ;; For some reason, the group window gets selected.  We change
6531       ;; it back.
6532       (select-window (get-buffer-window (current-buffer)))
6533       ;; Select next unread newsgroup automagically.
6534       (cond
6535        ((or (not gnus-auto-select-next)
6536             (not cmd))
6537         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6538        ((or (eq gnus-auto-select-next 'quietly)
6539             (and (eq gnus-auto-select-next 'slightly-quietly)
6540                  push)
6541             (and (eq gnus-auto-select-next 'almost-quietly)
6542                  (gnus-summary-last-article-p)))
6543         ;; Select quietly.
6544         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6545             (gnus-summary-exit)
6546           (gnus-message 7 "No more%s articles (%s)..."
6547                         (if unread " unread" "")
6548                         (if group (concat "selecting " group)
6549                           "exiting"))
6550           (gnus-summary-next-group nil group backward)))
6551        (t
6552         (when (gnus-key-press-event-p last-input-event)
6553           (gnus-summary-walk-group-buffer
6554            gnus-newsgroup-name cmd unread backward point))))))))
6555
6556 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6557   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6558                       (?\C-p (gnus-group-prev-unread-group 1))))
6559         (cursor-in-echo-area t)
6560         keve key group ended)
6561     (save-excursion
6562       (set-buffer gnus-group-buffer)
6563       (goto-char start)
6564       (setq group
6565             (if (eq gnus-keep-same-level 'best)
6566                 (gnus-summary-best-group gnus-newsgroup-name)
6567               (gnus-summary-search-group backward gnus-keep-same-level))))
6568     (while (not ended)
6569       (gnus-message
6570        5 "No more%s articles%s" (if unread " unread" "")
6571        (if (and group
6572                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6573            (format " (Type %s for %s [%s])"
6574                    (single-key-description cmd) group
6575                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6576          (format " (Type %s to exit %s)"
6577                  (single-key-description cmd)
6578                  gnus-newsgroup-name)))
6579       ;; Confirm auto selection.
6580       (setq key (car (setq keve (gnus-read-event-char))))
6581       (setq ended t)
6582       (cond
6583        ((assq key keystrokes)
6584         (let ((obuf (current-buffer)))
6585           (switch-to-buffer gnus-group-buffer)
6586           (when group
6587             (gnus-group-jump-to-group group))
6588           (eval (cadr (assq key keystrokes)))
6589           (setq group (gnus-group-group-name))
6590           (switch-to-buffer obuf))
6591         (setq ended nil))
6592        ((equal key cmd)
6593         (if (or (not group)
6594                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6595             (gnus-summary-exit)
6596           (gnus-summary-next-group nil group backward)))
6597        (t
6598         (push (cdr keve) unread-command-events))))))
6599
6600 (defun gnus-summary-next-unread-article ()
6601   "Select unread article after current one."
6602   (interactive)
6603   (gnus-summary-next-article
6604    (or (not (eq gnus-summary-goto-unread 'never))
6605        (gnus-summary-last-article-p (gnus-summary-article-number)))
6606    (and gnus-auto-select-same
6607         (gnus-summary-article-subject))))
6608
6609 (defun gnus-summary-prev-article (&optional unread subject)
6610   "Select the article after the current one.
6611 If UNREAD is non-nil, only unread articles are selected."
6612   (interactive "P")
6613   (gnus-summary-next-article unread subject t))
6614
6615 (defun gnus-summary-prev-unread-article ()
6616   "Select unread article before current one."
6617   (interactive)
6618   (gnus-summary-prev-article
6619    (or (not (eq gnus-summary-goto-unread 'never))
6620        (gnus-summary-first-article-p (gnus-summary-article-number)))
6621    (and gnus-auto-select-same
6622         (gnus-summary-article-subject))))
6623
6624 (defun gnus-summary-next-page (&optional lines circular)
6625   "Show next page of the selected article.
6626 If at the end of the current article, select the next article.
6627 LINES says how many lines should be scrolled up.
6628
6629 If CIRCULAR is non-nil, go to the start of the article instead of
6630 selecting the next article when reaching the end of the current
6631 article."
6632   (interactive "P")
6633   (setq gnus-summary-buffer (current-buffer))
6634   (gnus-set-global-variables)
6635   (let ((article (gnus-summary-article-number))
6636         (article-window (get-buffer-window gnus-article-buffer t))
6637         endp)
6638     ;; If the buffer is empty, we have no article.
6639     (unless article
6640       (error "No article to select"))
6641     (gnus-configure-windows 'article)
6642     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6643         (if (and (eq gnus-summary-goto-unread 'never)
6644                  (not (gnus-summary-last-article-p article)))
6645             (gnus-summary-next-article)
6646           (gnus-summary-next-unread-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         (when article-window
6654           (gnus-eval-in-buffer-window gnus-article-buffer
6655             (setq endp (gnus-article-next-page lines)))
6656           (when endp
6657             (cond (circular
6658                    (gnus-summary-beginning-of-article))
6659                   (lines
6660                    (gnus-message 3 "End of message"))
6661                   ((null lines)
6662                    (if (and (eq gnus-summary-goto-unread 'never)
6663                             (not (gnus-summary-last-article-p article)))
6664                        (gnus-summary-next-article)
6665                      (gnus-summary-next-unread-article))))))))
6666     (gnus-summary-recenter)
6667     (gnus-summary-position-point)))
6668
6669 (defun gnus-summary-prev-page (&optional lines move)
6670   "Show previous page of selected article.
6671 Argument LINES specifies lines to be scrolled down.
6672 If MOVE, move to the previous unread article if point is at
6673 the beginning of the buffer."
6674   (interactive "P")
6675   (let ((article (gnus-summary-article-number))
6676         (article-window (get-buffer-window gnus-article-buffer t))
6677         endp)
6678     (gnus-configure-windows 'article)
6679     (if (or (null gnus-current-article)
6680             (null gnus-article-current)
6681             (/= article (cdr gnus-article-current))
6682             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6683         ;; Selected subject is different from current article's.
6684         (gnus-summary-display-article article)
6685       (gnus-summary-recenter)
6686       (when article-window
6687         (gnus-eval-in-buffer-window gnus-article-buffer
6688           (setq endp (gnus-article-prev-page lines)))
6689         (when (and move endp)
6690           (cond (lines
6691                  (gnus-message 3 "Beginning of message"))
6692                 ((null lines)
6693                  (if (and (eq gnus-summary-goto-unread 'never)
6694                           (not (gnus-summary-first-article-p article)))
6695                      (gnus-summary-prev-article)
6696                    (gnus-summary-prev-unread-article))))))))
6697   (gnus-summary-position-point))
6698
6699 (defun gnus-summary-prev-page-or-article (&optional lines)
6700   "Show previous page of selected article.
6701 Argument LINES specifies lines to be scrolled down.
6702 If at the beginning of the article, go to the next article."
6703   (interactive "P")
6704   (gnus-summary-prev-page lines t))
6705
6706 (defun gnus-summary-scroll-up (lines)
6707   "Scroll up (or down) one line current article.
6708 Argument LINES specifies lines to be scrolled up (or down if negative)."
6709   (interactive "p")
6710   (gnus-configure-windows 'article)
6711   (gnus-summary-show-thread)
6712   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6713     (gnus-eval-in-buffer-window gnus-article-buffer
6714       (cond ((> lines 0)
6715              (when (gnus-article-next-page lines)
6716                (gnus-message 3 "End of message")))
6717             ((< lines 0)
6718              (gnus-article-prev-page (- lines))))))
6719   (gnus-summary-recenter)
6720   (gnus-summary-position-point))
6721
6722 (defun gnus-summary-scroll-down (lines)
6723   "Scroll down (or up) one line current article.
6724 Argument LINES specifies lines to be scrolled down (or up if negative)."
6725   (interactive "p")
6726   (gnus-summary-scroll-up (- lines)))
6727
6728 (defun gnus-summary-next-same-subject ()
6729   "Select next article which has the same subject as current one."
6730   (interactive)
6731   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6732
6733 (defun gnus-summary-prev-same-subject ()
6734   "Select previous article which has the same subject as current one."
6735   (interactive)
6736   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6737
6738 (defun gnus-summary-next-unread-same-subject ()
6739   "Select next unread article which has the same subject as current one."
6740   (interactive)
6741   (gnus-summary-next-article t (gnus-summary-article-subject)))
6742
6743 (defun gnus-summary-prev-unread-same-subject ()
6744   "Select previous unread article which has the same subject as current one."
6745   (interactive)
6746   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6747
6748 (defun gnus-summary-first-unread-article ()
6749   "Select the first unread article.
6750 Return nil if there are no unread articles."
6751   (interactive)
6752   (prog1
6753       (when (gnus-summary-first-subject t)
6754         (gnus-summary-show-thread)
6755         (gnus-summary-first-subject t)
6756         (gnus-summary-display-article (gnus-summary-article-number)))
6757     (gnus-summary-position-point)))
6758
6759 (defun gnus-summary-first-unread-subject ()
6760   "Place the point on the subject line of the first unread article.
6761 Return nil if there are no unread articles."
6762   (interactive)
6763   (prog1
6764       (when (gnus-summary-first-subject t)
6765         (gnus-summary-show-thread)
6766         (gnus-summary-first-subject t))
6767     (gnus-summary-position-point)))
6768
6769 (defun gnus-summary-first-article ()
6770   "Select the first article.
6771 Return nil if there are no articles."
6772   (interactive)
6773   (prog1
6774       (when (gnus-summary-first-subject)
6775         (gnus-summary-show-thread)
6776         (gnus-summary-first-subject)
6777         (gnus-summary-display-article (gnus-summary-article-number)))
6778     (gnus-summary-position-point)))
6779
6780 (defun gnus-summary-best-unread-article ()
6781   "Select the unread article with the highest score."
6782   (interactive)
6783   (let ((best -1000000)
6784         (data gnus-newsgroup-data)
6785         article score)
6786     (while data
6787       (and (gnus-data-unread-p (car data))
6788            (> (setq score
6789                     (gnus-summary-article-score (gnus-data-number (car data))))
6790               best)
6791            (setq best score
6792                  article (gnus-data-number (car data))))
6793       (setq data (cdr data)))
6794     (prog1
6795         (if article
6796             (gnus-summary-goto-article article)
6797           (error "No unread articles"))
6798       (gnus-summary-position-point))))
6799
6800 (defun gnus-summary-last-subject ()
6801   "Go to the last displayed subject line in the group."
6802   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6803     (when article
6804       (gnus-summary-goto-subject article))))
6805
6806 (defun gnus-summary-goto-article (article &optional all-headers force)
6807   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6808 If ALL-HEADERS is non-nil, no header lines are hidden.
6809 If FORCE, go to the article even if it isn't displayed.  If FORCE
6810 is a number, it is the line the article is to be displayed on."
6811   (interactive
6812    (list
6813     (completing-read
6814      "Article number or Message-ID: "
6815      (mapcar (lambda (number) (list (int-to-string number)))
6816              gnus-newsgroup-limit))
6817     current-prefix-arg
6818     t))
6819   (prog1
6820       (if (and (stringp article)
6821                (string-match "@" article))
6822           (gnus-summary-refer-article article)
6823         (when (stringp article)
6824           (setq article (string-to-number article)))
6825         (if (gnus-summary-goto-subject article force)
6826             (gnus-summary-display-article article all-headers)
6827           (gnus-message 4 "Couldn't go to article %s" article) nil))
6828     (gnus-summary-position-point)))
6829
6830 (defun gnus-summary-goto-last-article ()
6831   "Go to the previously read article."
6832   (interactive)
6833   (prog1
6834       (when gnus-last-article
6835         (gnus-summary-goto-article gnus-last-article nil t))
6836     (gnus-summary-position-point)))
6837
6838 (defun gnus-summary-pop-article (number)
6839   "Pop one article off the history and go to the previous.
6840 NUMBER articles will be popped off."
6841   (interactive "p")
6842   (let (to)
6843     (setq gnus-newsgroup-history
6844           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6845     (if to
6846         (gnus-summary-goto-article (car to) nil t)
6847       (error "Article history empty")))
6848   (gnus-summary-position-point))
6849
6850 ;; Summary commands and functions for limiting the summary buffer.
6851
6852 (defun gnus-summary-limit-to-articles (n)
6853   "Limit the summary buffer to the next N articles.
6854 If not given a prefix, use the process marked articles instead."
6855   (interactive "P")
6856   (prog1
6857       (let ((articles (gnus-summary-work-articles n)))
6858         (setq gnus-newsgroup-processable nil)
6859         (gnus-summary-limit articles))
6860     (gnus-summary-position-point)))
6861
6862 (defun gnus-summary-pop-limit (&optional total)
6863   "Restore the previous limit.
6864 If given a prefix, remove all limits."
6865   (interactive "P")
6866   (when total
6867     (setq gnus-newsgroup-limits
6868           (list (mapcar (lambda (h) (mail-header-number h))
6869                         gnus-newsgroup-headers))))
6870   (unless gnus-newsgroup-limits
6871     (error "No limit to pop"))
6872   (prog1
6873       (gnus-summary-limit nil 'pop)
6874     (gnus-summary-position-point)))
6875
6876 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
6877   "Limit the summary buffer to articles that have subjects that match a regexp.
6878 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
6879   (interactive
6880    (list (read-string (if current-prefix-arg
6881                           "Exclude subject (regexp): "
6882                         "Limit to subject (regexp): "))
6883          nil current-prefix-arg))
6884   (unless header
6885     (setq header "subject"))
6886   (when (not (equal "" subject))
6887     (prog1
6888         (let ((articles (gnus-summary-find-matching
6889                          (or header "subject") subject 'all nil nil
6890                          not-matching)))
6891           (unless articles
6892             (error "Found no matches for \"%s\"" subject))
6893           (gnus-summary-limit articles))
6894       (gnus-summary-position-point))))
6895
6896 (defun gnus-summary-limit-to-author (from &optional not-matching)
6897   "Limit the summary buffer to articles that have authors that match a regexp.
6898 If NOT-MATCHING, excluding articles that have authors that match a regexp."
6899   (interactive
6900    (list (read-string (if current-prefix-arg
6901                           "Exclude author (regexp): "
6902                         "Limit to author (regexp): "))
6903          current-prefix-arg))
6904   (gnus-summary-limit-to-subject from "from" not-matching))
6905
6906 (defun gnus-summary-limit-to-age (age &optional younger-p)
6907   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6908 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6909 articles that are younger than AGE days."
6910   (interactive
6911    (let ((younger current-prefix-arg)
6912          (days-got nil)
6913          days)
6914      (while (not days-got)
6915        (setq days (if younger
6916                       (read-string "Limit to articles within (in days): ")
6917                     (read-string "Limit to articles older than (in days): ")))
6918        (when (> (length days) 0)
6919          (setq days (read days)))
6920        (if (numberp days)
6921            (setq days-got t)
6922          (message "Please enter a number.")
6923          (sleep-for 1)))
6924      (list days younger)))
6925   (prog1
6926       (let ((data gnus-newsgroup-data)
6927             (cutoff (days-to-time age))
6928             articles d date is-younger)
6929         (while (setq d (pop data))
6930           (when (and (vectorp (gnus-data-header d))
6931                      (setq date (mail-header-date (gnus-data-header d))))
6932             (setq is-younger (time-less-p
6933                               (time-since (condition-case ()
6934                                               (date-to-time date)
6935                                             (error '(0 0))))
6936                               cutoff))
6937             (when (if younger-p
6938                       is-younger
6939                     (not is-younger))
6940               (push (gnus-data-number d) articles))))
6941         (gnus-summary-limit (nreverse articles)))
6942     (gnus-summary-position-point)))
6943
6944 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
6945   "Limit the summary buffer to articles that match an 'extra' header."
6946   (interactive
6947    (let ((header
6948           (intern
6949            (gnus-completing-read
6950             (symbol-name (car gnus-extra-headers))
6951             (if current-prefix-arg
6952                 "Exclude extra header:"
6953               "Limit extra header:")
6954             (mapcar (lambda (x)
6955                       (cons (symbol-name x) x))
6956                     gnus-extra-headers)
6957             nil
6958             t))))
6959      (list header
6960            (read-string (format "%s header %s (regexp): "
6961                                 (if current-prefix-arg "Exclude" "Limit to")
6962                                 header))
6963            current-prefix-arg)))
6964   (when (not (equal "" regexp))
6965     (prog1
6966         (let ((articles (gnus-summary-find-matching
6967                          (cons 'extra header) regexp 'all nil nil
6968                          not-matching)))
6969           (unless articles
6970             (error "Found no matches for \"%s\"" regexp))
6971           (gnus-summary-limit articles))
6972       (gnus-summary-position-point))))
6973
6974 (defun gnus-summary-limit-to-display-predicate ()
6975   "Limit the summary buffer to the predicated in the `display' group parameter."
6976   (interactive)
6977   (unless gnus-newsgroup-display
6978     (error "There is no `display' group parameter"))
6979   (let (articles)
6980     (dolist (number gnus-newsgroup-articles)
6981       (when (funcall gnus-newsgroup-display)
6982         (push number articles)))
6983     (gnus-summary-limit articles))
6984   (gnus-summary-position-point))
6985
6986 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6987 (make-obsolete
6988  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6989
6990 (defun gnus-summary-limit-to-unread (&optional all)
6991   "Limit the summary buffer to articles that are not marked as read.
6992 If ALL is non-nil, limit strictly to unread articles."
6993   (interactive "P")
6994   (if all
6995       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6996     (gnus-summary-limit-to-marks
6997      ;; Concat all the marks that say that an article is read and have
6998      ;; those removed.
6999      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7000            gnus-killed-mark gnus-kill-file-mark
7001            gnus-low-score-mark gnus-expirable-mark
7002            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7003            gnus-duplicate-mark gnus-souped-mark)
7004      'reverse)))
7005
7006 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7007 (make-obsolete 'gnus-summary-delete-marked-with
7008                'gnus-summary-limit-exlude-marks)
7009
7010 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7011   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7012 If REVERSE, limit the summary buffer to articles that are marked
7013 with MARKS.  MARKS can either be a string of marks or a list of marks.
7014 Returns how many articles were removed."
7015   (interactive "sMarks: ")
7016   (gnus-summary-limit-to-marks marks t))
7017
7018 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7019   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7020 If REVERSE (the prefix), limit the summary buffer to articles that are
7021 not marked with MARKS.  MARKS can either be a string of marks or a
7022 list of marks.
7023 Returns how many articles were removed."
7024   (interactive "sMarks: \nP")
7025   (prog1
7026       (let ((data gnus-newsgroup-data)
7027             (marks (if (listp marks) marks
7028                      (append marks nil))) ; Transform to list.
7029             articles)
7030         (while data
7031           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7032                   (memq (gnus-data-mark (car data)) marks))
7033             (push (gnus-data-number (car data)) articles))
7034           (setq data (cdr data)))
7035         (gnus-summary-limit articles))
7036     (gnus-summary-position-point)))
7037
7038 (defun gnus-summary-limit-to-score (score)
7039   "Limit to articles with score at or above SCORE."
7040   (interactive "NLimit to articles with score of at least: ")
7041   (let ((data gnus-newsgroup-data)
7042         articles)
7043     (while data
7044       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7045                 score)
7046         (push (gnus-data-number (car data)) articles))
7047       (setq data (cdr data)))
7048     (prog1
7049         (gnus-summary-limit articles)
7050       (gnus-summary-position-point))))
7051
7052 (defun gnus-summary-limit-include-thread (id)
7053   "Display all the hidden articles that is in the thread with ID in it.
7054 When called interactively, ID is the Message-ID of the current
7055 article."
7056   (interactive (list (mail-header-id (gnus-summary-article-header))))
7057   (let ((articles (gnus-articles-in-thread
7058                    (gnus-id-to-thread (gnus-root-id id)))))
7059     (prog1
7060         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7061       (gnus-summary-limit-include-matching-articles
7062        "subject"
7063        (regexp-quote (gnus-simplify-subject-re
7064                       (mail-header-subject (gnus-id-to-header id)))))
7065       (gnus-summary-position-point))))
7066
7067 (defun gnus-summary-limit-include-matching-articles (header regexp)
7068   "Display all the hidden articles that have HEADERs that match REGEXP."
7069   (interactive (list (read-string "Match on header: ")
7070                      (read-string "Regexp: ")))
7071   (let ((articles (gnus-find-matching-articles header regexp)))
7072     (prog1
7073         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7074       (gnus-summary-position-point))))
7075
7076 (defun gnus-summary-limit-include-dormant ()
7077   "Display all the hidden articles that are marked as dormant.
7078 Note that this command only works on a subset of the articles currently
7079 fetched for this group."
7080   (interactive)
7081   (unless gnus-newsgroup-dormant
7082     (error "There are no dormant articles in this group"))
7083   (prog1
7084       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7085     (gnus-summary-position-point)))
7086
7087 (defun gnus-summary-limit-exclude-dormant ()
7088   "Hide all dormant articles."
7089   (interactive)
7090   (prog1
7091       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7092     (gnus-summary-position-point)))
7093
7094 (defun gnus-summary-limit-exclude-childless-dormant ()
7095   "Hide all dormant articles that have no children."
7096   (interactive)
7097   (let ((data (gnus-data-list t))
7098         articles d children)
7099     ;; Find all articles that are either not dormant or have
7100     ;; children.
7101     (while (setq d (pop data))
7102       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7103                 (and (setq children
7104                            (gnus-article-children (gnus-data-number d)))
7105                      (let (found)
7106                        (while children
7107                          (when (memq (car children) articles)
7108                            (setq children nil
7109                                  found t))
7110                          (pop children))
7111                        found)))
7112         (push (gnus-data-number d) articles)))
7113     ;; Do the limiting.
7114     (prog1
7115         (gnus-summary-limit articles)
7116       (gnus-summary-position-point))))
7117
7118 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7119   "Mark all unread excluded articles as read.
7120 If ALL, mark even excluded ticked and dormants as read."
7121   (interactive "P")
7122   (let ((articles (gnus-sorted-complement
7123                    (sort
7124                     (mapcar (lambda (h) (mail-header-number h))
7125                             gnus-newsgroup-headers)
7126                     '<)
7127                    (sort gnus-newsgroup-limit '<)))
7128         article)
7129     (setq gnus-newsgroup-unreads
7130           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7131     (if all
7132         (setq gnus-newsgroup-dormant nil
7133               gnus-newsgroup-marked nil
7134               gnus-newsgroup-reads
7135               (nconc
7136                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7137                gnus-newsgroup-reads))
7138       (while (setq article (pop articles))
7139         (unless (or (memq article gnus-newsgroup-dormant)
7140                     (memq article gnus-newsgroup-marked))
7141           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7142
7143 (defun gnus-summary-limit (articles &optional pop)
7144   (if pop
7145       ;; We pop the previous limit off the stack and use that.
7146       (setq articles (car gnus-newsgroup-limits)
7147             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7148     ;; We use the new limit, so we push the old limit on the stack.
7149     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7150   ;; Set the limit.
7151   (setq gnus-newsgroup-limit articles)
7152   (let ((total (length gnus-newsgroup-data))
7153         (data (gnus-data-find-list (gnus-summary-article-number)))
7154         (gnus-summary-mark-below nil)   ; Inhibit this.
7155         found)
7156     ;; This will do all the work of generating the new summary buffer
7157     ;; according to the new limit.
7158     (gnus-summary-prepare)
7159     ;; Hide any threads, possibly.
7160     (and gnus-show-threads
7161          gnus-thread-hide-subtree
7162          (gnus-summary-hide-all-threads))
7163     ;; Try to return to the article you were at, or one in the
7164     ;; neighborhood.
7165     (when data
7166       ;; We try to find some article after the current one.
7167       (while data
7168         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7169           (setq data nil
7170                 found t))
7171         (setq data (cdr data))))
7172     (unless found
7173       ;; If there is no data, that means that we were after the last
7174       ;; article.  The same goes when we can't find any articles
7175       ;; after the current one.
7176       (goto-char (point-max))
7177       (gnus-summary-find-prev))
7178     (gnus-set-mode-line 'summary)
7179     ;; We return how many articles were removed from the summary
7180     ;; buffer as a result of the new limit.
7181     (- total (length gnus-newsgroup-data))))
7182
7183 (defsubst gnus-invisible-cut-children (threads)
7184   (let ((num 0))
7185     (while threads
7186       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7187         (incf num))
7188       (pop threads))
7189     (< num 2)))
7190
7191 (defsubst gnus-cut-thread (thread)
7192   "Go forwards in the thread until we find an article that we want to display."
7193   (when (or (eq gnus-fetch-old-headers 'some)
7194             (eq gnus-fetch-old-headers 'invisible)
7195             (numberp gnus-fetch-old-headers)
7196             (eq gnus-build-sparse-threads 'some)
7197             (eq gnus-build-sparse-threads 'more))
7198     ;; Deal with old-fetched headers and sparse threads.
7199     (while (and
7200             thread
7201             (or
7202              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7203              (gnus-summary-article-ancient-p
7204               (mail-header-number (car thread))))
7205             (if (or (<= (length (cdr thread)) 1)
7206                     (eq gnus-fetch-old-headers 'invisible))
7207                 (setq gnus-newsgroup-limit
7208                       (delq (mail-header-number (car thread))
7209                             gnus-newsgroup-limit)
7210                       thread (cadr thread))
7211               (when (gnus-invisible-cut-children (cdr thread))
7212                 (let ((th (cdr thread)))
7213                   (while th
7214                     (if (memq (mail-header-number (caar th))
7215                               gnus-newsgroup-limit)
7216                         (setq thread (car th)
7217                               th nil)
7218                       (setq th (cdr th))))))))))
7219   thread)
7220
7221 (defun gnus-cut-threads (threads)
7222   "Cut off all uninteresting articles from the beginning of threads."
7223   (when (or (eq gnus-fetch-old-headers 'some)
7224             (eq gnus-fetch-old-headers 'invisible)
7225             (numberp gnus-fetch-old-headers)
7226             (eq gnus-build-sparse-threads 'some)
7227             (eq gnus-build-sparse-threads 'more))
7228     (let ((th threads))
7229       (while th
7230         (setcar th (gnus-cut-thread (car th)))
7231         (setq th (cdr th)))))
7232   ;; Remove nixed out threads.
7233   (delq nil threads))
7234
7235 (defun gnus-summary-initial-limit (&optional show-if-empty)
7236   "Figure out what the initial limit is supposed to be on group entry.
7237 This entails weeding out unwanted dormants, low-scored articles,
7238 fetch-old-headers verbiage, and so on."
7239   ;; Most groups have nothing to remove.
7240   (if (or gnus-inhibit-limiting
7241           (and (null gnus-newsgroup-dormant)
7242                (eq gnus-newsgroup-display 'gnus-not-ignore)
7243                (not (eq gnus-fetch-old-headers 'some))
7244                (not (numberp gnus-fetch-old-headers))
7245                (not (eq gnus-fetch-old-headers 'invisible))
7246                (null gnus-summary-expunge-below)
7247                (not (eq gnus-build-sparse-threads 'some))
7248                (not (eq gnus-build-sparse-threads 'more))
7249                (null gnus-thread-expunge-below)
7250                (not gnus-use-nocem)))
7251       ()                                ; Do nothing.
7252     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7253     (setq gnus-newsgroup-limit nil)
7254     (mapatoms
7255      (lambda (node)
7256        (unless (car (symbol-value node))
7257          ;; These threads have no parents -- they are roots.
7258          (let ((nodes (cdr (symbol-value node)))
7259                thread)
7260            (while nodes
7261              (if (and gnus-thread-expunge-below
7262                       (< (gnus-thread-total-score (car nodes))
7263                          gnus-thread-expunge-below))
7264                  (gnus-expunge-thread (pop nodes))
7265                (setq thread (pop nodes))
7266                (gnus-summary-limit-children thread))))))
7267      gnus-newsgroup-dependencies)
7268     ;; If this limitation resulted in an empty group, we might
7269     ;; pop the previous limit and use it instead.
7270     (when (and (not gnus-newsgroup-limit)
7271                show-if-empty)
7272       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7273     gnus-newsgroup-limit))
7274
7275 (defun gnus-summary-limit-children (thread)
7276   "Return 1 if this subthread is visible and 0 if it is not."
7277   ;; First we get the number of visible children to this thread.  This
7278   ;; is done by recursing down the thread using this function, so this
7279   ;; will really go down to a leaf article first, before slowly
7280   ;; working its way up towards the root.
7281   (when thread
7282     (let ((children
7283            (if (cdr thread)
7284                (apply '+ (mapcar 'gnus-summary-limit-children
7285                                  (cdr thread)))
7286              0))
7287           (number (mail-header-number (car thread)))
7288           score)
7289       (if (and
7290            (not (memq number gnus-newsgroup-marked))
7291            (or
7292             ;; If this article is dormant and has absolutely no visible
7293             ;; children, then this article isn't visible.
7294             (and (memq number gnus-newsgroup-dormant)
7295                  (zerop children))
7296             ;; If this is "fetch-old-headered" and there is no
7297             ;; visible children, then we don't want this article.
7298             (and (or (eq gnus-fetch-old-headers 'some)
7299                      (numberp gnus-fetch-old-headers))
7300                  (gnus-summary-article-ancient-p number)
7301                  (zerop children))
7302             ;; If this is "fetch-old-headered" and `invisible', then
7303             ;; we don't want this article.
7304             (and (eq gnus-fetch-old-headers 'invisible)
7305                  (gnus-summary-article-ancient-p number))
7306             ;; If this is a sparsely inserted article with no children,
7307             ;; we don't want it.
7308             (and (eq gnus-build-sparse-threads 'some)
7309                  (gnus-summary-article-sparse-p number)
7310                  (zerop children))
7311             ;; If we use expunging, and this article is really
7312             ;; low-scored, then we don't want this article.
7313             (when (and gnus-summary-expunge-below
7314                        (< (setq score
7315                                 (or (cdr (assq number gnus-newsgroup-scored))
7316                                     gnus-summary-default-score))
7317                           gnus-summary-expunge-below))
7318               ;; We increase the expunge-tally here, but that has
7319               ;; nothing to do with the limits, really.
7320               (incf gnus-newsgroup-expunged-tally)
7321               ;; We also mark as read here, if that's wanted.
7322               (when (and gnus-summary-mark-below
7323                          (< score gnus-summary-mark-below))
7324                 (setq gnus-newsgroup-unreads
7325                       (delq number gnus-newsgroup-unreads))
7326                 (if gnus-newsgroup-auto-expire
7327                     (push number gnus-newsgroup-expirable)
7328                   (push (cons number gnus-low-score-mark)
7329                         gnus-newsgroup-reads)))
7330               t)
7331             ;; Do the `display' group parameter.
7332             (and gnus-newsgroup-display
7333                  (not (funcall gnus-newsgroup-display)))
7334             ;; Check NoCeM things.
7335             (if (and gnus-use-nocem
7336                      (gnus-nocem-unwanted-article-p
7337                       (mail-header-id (car thread))))
7338                 (progn
7339                   (setq gnus-newsgroup-unreads
7340                         (delq number gnus-newsgroup-unreads))
7341                   t))))
7342           ;; Nope, invisible article.
7343           0
7344         ;; Ok, this article is to be visible, so we add it to the limit
7345         ;; and return 1.
7346         (push number gnus-newsgroup-limit)
7347         1))))
7348
7349 (defun gnus-expunge-thread (thread)
7350   "Mark all articles in THREAD as read."
7351   (let* ((number (mail-header-number (car thread))))
7352     (incf gnus-newsgroup-expunged-tally)
7353     ;; We also mark as read here, if that's wanted.
7354     (setq gnus-newsgroup-unreads
7355           (delq number gnus-newsgroup-unreads))
7356     (if gnus-newsgroup-auto-expire
7357         (push number gnus-newsgroup-expirable)
7358       (push (cons number gnus-low-score-mark)
7359             gnus-newsgroup-reads)))
7360   ;; Go recursively through all subthreads.
7361   (mapcar 'gnus-expunge-thread (cdr thread)))
7362
7363 ;; Summary article oriented commands
7364
7365 (defun gnus-summary-refer-parent-article (n)
7366   "Refer parent article N times.
7367 If N is negative, go to ancestor -N instead.
7368 The difference between N and the number of articles fetched is returned."
7369   (interactive "p")
7370   (let ((skip 1)
7371         error header ref)
7372     (when (not (natnump n))
7373       (setq skip (abs n)
7374             n 1))
7375     (while (and (> n 0)
7376                 (not error))
7377       (setq header (gnus-summary-article-header))
7378       (if (and (eq (mail-header-number header)
7379                    (cdr gnus-article-current))
7380                (equal gnus-newsgroup-name
7381                       (car gnus-article-current)))
7382           ;; If we try to find the parent of the currently
7383           ;; displayed article, then we take a look at the actual
7384           ;; References header, since this is slightly more
7385           ;; reliable than the References field we got from the
7386           ;; server.
7387           (save-excursion
7388             (set-buffer gnus-original-article-buffer)
7389             (nnheader-narrow-to-headers)
7390             (unless (setq ref (message-fetch-field "references"))
7391               (setq ref (message-fetch-field "in-reply-to")))
7392             (widen))
7393         (setq ref
7394               ;; It's not the current article, so we take a bet on
7395               ;; the value we got from the server.
7396               (mail-header-references header)))
7397       (if (and ref
7398                (not (equal ref "")))
7399           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7400             (gnus-message 1 "Couldn't find parent"))
7401         (gnus-message 1 "No references in article %d"
7402                       (gnus-summary-article-number))
7403         (setq error t))
7404       (decf n))
7405     (gnus-summary-position-point)
7406     n))
7407
7408 (defun gnus-summary-refer-references ()
7409   "Fetch all articles mentioned in the References header.
7410 Return the number of articles fetched."
7411   (interactive)
7412   (let ((ref (mail-header-references (gnus-summary-article-header)))
7413         (current (gnus-summary-article-number))
7414         (n 0))
7415     (if (or (not ref)
7416             (equal ref ""))
7417         (error "No References in the current article")
7418       ;; For each Message-ID in the References header...
7419       (while (string-match "<[^>]*>" ref)
7420         (incf n)
7421         ;; ... fetch that article.
7422         (gnus-summary-refer-article
7423          (prog1 (match-string 0 ref)
7424            (setq ref (substring ref (match-end 0))))))
7425       (gnus-summary-goto-subject current)
7426       (gnus-summary-position-point)
7427       n)))
7428
7429 (defun gnus-summary-refer-thread (&optional limit)
7430   "Fetch all articles in the current thread.
7431 If LIMIT (the numerical prefix), fetch that many old headers instead
7432 of what's specified by the `gnus-refer-thread-limit' variable."
7433   (interactive "P")
7434   (let ((id (mail-header-id (gnus-summary-article-header)))
7435         (limit (if limit (prefix-numeric-value limit)
7436                  gnus-refer-thread-limit)))
7437     ;; We want to fetch LIMIT *old* headers, but we also have to
7438     ;; re-fetch all the headers in the current buffer, because many of
7439     ;; them may be undisplayed.  So we adjust LIMIT.
7440     (when (numberp limit)
7441       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7442     (unless (eq gnus-fetch-old-headers 'invisible)
7443       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7444       ;; Retrieve the headers and read them in.
7445       (if (eq (gnus-retrieve-headers
7446                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7447               'nov)
7448           (gnus-build-all-threads)
7449         (error "Can't fetch thread from backends that don't support NOV"))
7450       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7451     (gnus-summary-limit-include-thread id)))
7452
7453 (defun gnus-summary-refer-article (message-id)
7454   "Fetch an article specified by MESSAGE-ID."
7455   (interactive "sMessage-ID: ")
7456   (when (and (stringp message-id)
7457              (not (zerop (length message-id))))
7458     ;; Construct the correct Message-ID if necessary.
7459     ;; Suggested by tale@pawl.rpi.edu.
7460     (unless (string-match "^<" message-id)
7461       (setq message-id (concat "<" message-id)))
7462     (unless (string-match ">$" message-id)
7463       (setq message-id (concat message-id ">")))
7464     (let* ((header (gnus-id-to-header message-id))
7465            (sparse (and header
7466                         (gnus-summary-article-sparse-p
7467                          (mail-header-number header))
7468                         (memq (mail-header-number header)
7469                               gnus-newsgroup-limit)))
7470            number)
7471       (cond
7472        ;; If the article is present in the buffer we just go to it.
7473        ((and header
7474              (or (not (gnus-summary-article-sparse-p
7475                        (mail-header-number header)))
7476                  sparse))
7477         (prog1
7478             (gnus-summary-goto-article
7479              (mail-header-number header) nil t)
7480           (when sparse
7481             (gnus-summary-update-article (mail-header-number header)))))
7482        (t
7483         ;; We fetch the article.
7484         (catch 'found
7485           (dolist (gnus-override-method (gnus-refer-article-methods))
7486             (gnus-check-server gnus-override-method)
7487             ;; Fetch the header, and display the article.
7488             (when (setq number (gnus-summary-insert-subject message-id))
7489               (gnus-summary-select-article nil nil nil number)
7490               (throw 'found t)))
7491           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7492
7493 (defun gnus-refer-article-methods ()
7494   "Return a list of referrable methods."
7495   (cond
7496    ;; No method, so we default to current and native.
7497    ((null gnus-refer-article-method)
7498     (list gnus-current-select-method gnus-select-method))
7499    ;; Current.
7500    ((eq 'current gnus-refer-article-method)
7501     (list gnus-current-select-method))
7502    ;; List of select methods.
7503    ((not (and (symbolp (car gnus-refer-article-method))
7504               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7505     (let (out)
7506       (dolist (method gnus-refer-article-method)
7507         (push (if (eq 'current method)
7508                   gnus-current-select-method
7509                 method)
7510               out))
7511       (nreverse out)))
7512    ;; One single select method.
7513    (t
7514     (list gnus-refer-article-method))))
7515
7516 (defun gnus-summary-edit-parameters ()
7517   "Edit the group parameters of the current group."
7518   (interactive)
7519   (gnus-group-edit-group gnus-newsgroup-name 'params))
7520
7521 (defun gnus-summary-customize-parameters ()
7522   "Customize the group parameters of the current group."
7523   (interactive)
7524   (gnus-group-customize gnus-newsgroup-name))
7525
7526 (defun gnus-summary-enter-digest-group (&optional force)
7527   "Enter an nndoc group based on the current article.
7528 If FORCE, force a digest interpretation.  If not, try
7529 to guess what the document format is."
7530   (interactive "P")
7531   (let ((conf gnus-current-window-configuration))
7532     (save-excursion
7533       (gnus-summary-select-article))
7534     (setq gnus-current-window-configuration conf)
7535     (let* ((name (format "%s-%d"
7536                          (gnus-group-prefixed-name
7537                           gnus-newsgroup-name (list 'nndoc ""))
7538                          (save-excursion
7539                            (set-buffer gnus-summary-buffer)
7540                            gnus-current-article)))
7541            (ogroup gnus-newsgroup-name)
7542            (params (append (gnus-info-params (gnus-get-info ogroup))
7543                            (list (cons 'to-group ogroup))
7544                            (list (cons 'save-article-group ogroup))))
7545            (case-fold-search t)
7546            (buf (current-buffer))
7547            dig to-address)
7548       (save-excursion
7549         (set-buffer gnus-original-article-buffer)
7550         ;; Have the digest group inherit the main mail address of
7551         ;; the parent article.
7552         (when (setq to-address (or (message-fetch-field "reply-to")
7553                                    (message-fetch-field "from")))
7554           (setq params (append
7555                         (list (cons 'to-address
7556                                     (funcall gnus-decode-encoded-word-function
7557                                              to-address))))))
7558         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7559         (insert-buffer-substring gnus-original-article-buffer)
7560         ;; Remove lines that may lead nndoc to misinterpret the
7561         ;; document type.
7562         (narrow-to-region
7563          (goto-char (point-min))
7564          (or (search-forward "\n\n" nil t) (point)))
7565         (goto-char (point-min))
7566         (delete-matching-lines "^Path:\\|^From ")
7567         (widen))
7568       (unwind-protect
7569           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7570                     (gnus-newsgroup-ephemeral-ignored-charsets
7571                      gnus-newsgroup-ignored-charsets))
7572                 (gnus-group-read-ephemeral-group
7573                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7574                               (nndoc-article-type
7575                                ,(if force 'mbox 'guess))) t))
7576               ;; Make all postings to this group go to the parent group.
7577               (nconc (gnus-info-params (gnus-get-info name))
7578                      params)
7579             ;; Couldn't select this doc group.
7580             (switch-to-buffer buf)
7581             (gnus-set-global-variables)
7582             (gnus-configure-windows 'summary)
7583             (gnus-message 3 "Article couldn't be entered?"))
7584         (kill-buffer dig)))))
7585
7586 (defun gnus-summary-read-document (n)
7587   "Open a new group based on the current article(s).
7588 This will allow you to read digests and other similar
7589 documents as newsgroups.
7590 Obeys the standard process/prefix convention."
7591   (interactive "P")
7592   (let* ((articles (gnus-summary-work-articles n))
7593          (ogroup gnus-newsgroup-name)
7594          (params (append (gnus-info-params (gnus-get-info ogroup))
7595                          (list (cons 'to-group ogroup))))
7596          article group egroup groups vgroup)
7597     (while (setq article (pop articles))
7598       (setq group (format "%s-%d" gnus-newsgroup-name article))
7599       (gnus-summary-remove-process-mark article)
7600       (when (gnus-summary-display-article article)
7601         (save-excursion
7602           (with-temp-buffer
7603             (insert-buffer-substring gnus-original-article-buffer)
7604             ;; Remove some headers that may lead nndoc to make
7605             ;; the wrong guess.
7606             (message-narrow-to-head)
7607             (goto-char (point-min))
7608             (delete-matching-lines "^\\(Path\\):\\|^From ")
7609             (widen)
7610             (if (setq egroup
7611                       (gnus-group-read-ephemeral-group
7612                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7613                                      (nndoc-article-type guess))
7614                        t nil t))
7615                 (progn
7616                   ;; Make all postings to this group go to the parent group.
7617                   (nconc (gnus-info-params (gnus-get-info egroup))
7618                          params)
7619                   (push egroup groups))
7620               ;; Couldn't select this doc group.
7621               (gnus-error 3 "Article couldn't be entered"))))))
7622     ;; Now we have selected all the documents.
7623     (cond
7624      ((not groups)
7625       (error "None of the articles could be interpreted as documents"))
7626      ((gnus-group-read-ephemeral-group
7627        (setq vgroup (format
7628                      "nnvirtual:%s-%s" gnus-newsgroup-name
7629                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7630        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7631        t
7632        (cons (current-buffer) 'summary)))
7633      (t
7634       (error "Couldn't select virtual nndoc group")))))
7635
7636 (defun gnus-summary-isearch-article (&optional regexp-p)
7637   "Do incremental search forward on the current article.
7638 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7639   (interactive "P")
7640   (let* ((gnus-inhibit-treatment t)
7641          (old (gnus-summary-select-article)))
7642     (gnus-configure-windows 'article)
7643     (gnus-eval-in-buffer-window gnus-article-buffer
7644       (save-restriction
7645         (widen)
7646         (when (eq 'old old)
7647           (gnus-article-show-all-headers))
7648         (goto-char (point-min))
7649         (isearch-forward regexp-p)))))
7650
7651 (defun gnus-summary-search-article-forward (regexp &optional backward)
7652   "Search for an article containing REGEXP forward.
7653 If BACKWARD, search backward instead."
7654   (interactive
7655    (list (read-string
7656           (format "Search article %s (regexp%s): "
7657                   (if current-prefix-arg "backward" "forward")
7658                   (if gnus-last-search-regexp
7659                       (concat ", default " gnus-last-search-regexp)
7660                     "")))
7661          current-prefix-arg))
7662   (if (string-equal regexp "")
7663       (setq regexp (or gnus-last-search-regexp ""))
7664     (setq gnus-last-search-regexp regexp)
7665     (setq gnus-article-before-search gnus-current-article))
7666   ;; Intentionally set gnus-last-article.
7667   (setq gnus-last-article gnus-article-before-search)
7668   (let ((gnus-last-article gnus-last-article))
7669     (if (gnus-summary-search-article regexp backward)
7670         (gnus-summary-show-thread)
7671       (error "Search failed: \"%s\"" regexp))))
7672
7673 (defun gnus-summary-search-article-backward (regexp)
7674   "Search for an article containing REGEXP backward."
7675   (interactive
7676    (list (read-string
7677           (format "Search article backward (regexp%s): "
7678                   (if gnus-last-search-regexp
7679                       (concat ", default " gnus-last-search-regexp)
7680                     "")))))
7681   (gnus-summary-search-article-forward regexp 'backward))
7682
7683 (eval-when-compile
7684   (defmacro gnus-summary-search-article-position-point (regexp backward)
7685     "Dehighlight the last matched text and goto the beginning position."
7686     (` (if (and gnus-summary-search-article-matched-data
7687                 (let ((text (caddr gnus-summary-search-article-matched-data))
7688                       (inhibit-read-only t)
7689                       buffer-read-only)
7690                   (delete-region
7691                    (goto-char (car gnus-summary-search-article-matched-data))
7692                    (cadr gnus-summary-search-article-matched-data))
7693                   (insert text)
7694                   (string-match (, regexp) text)))
7695            (if (, backward) (beginning-of-line) (end-of-line))
7696          (goto-char (if (, backward) (point-max) (point-min))))))
7697
7698   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7699     "Place point where X-Face image is displayed."
7700     (if (featurep 'xemacs)
7701         (` (let ((end (if (search-forward "\n\n" nil t)
7702                           (goto-char (1- (point)))
7703                         (point-min)))
7704                  extent)
7705              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7706              (unless (and (re-search-forward "^From:" end t)
7707                           (setq extent (extent-at (point)))
7708                           (extent-begin-glyph extent))
7709                (goto-char (, opoint)))))
7710       (` (let ((end (if (search-forward "\n\n" nil t)
7711                         (goto-char (1- (point)))
7712                       (point-min)))
7713                (start (or (search-backward "\n\n" nil t) (point-min))))
7714            (goto-char
7715             (or (text-property-any start end 'x-face-image t);; x-face-e21
7716                 (text-property-any start end 'x-face-mule-bitmap-image t)
7717                 (, opoint)))))))
7718
7719   (defmacro gnus-summary-search-article-highlight-matched-text
7720     (backward treated x-face)
7721     "Highlight matched text in the function `gnus-summary-search-article'."
7722     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7723              (end (set-marker (make-marker) (match-end 0)))
7724              (inhibit-read-only t)
7725              buffer-read-only)
7726          (unless treated
7727            (let ((,@
7728                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7729                     (mapcar
7730                      (lambda (item) (setq items (delq item items)))
7731                      '(gnus-treat-buttonize
7732                        gnus-treat-fill-article
7733                        gnus-treat-fill-long-lines
7734                        gnus-treat-emphasize
7735                        gnus-treat-highlight-headers
7736                        gnus-treat-highlight-citation
7737                        gnus-treat-highlight-signature
7738                        gnus-treat-overstrike
7739                        gnus-treat-display-xface
7740                        gnus-treat-buttonize-head
7741                        gnus-treat-decode-article-as-default-mime-charset))
7742                     (static-if (featurep 'xemacs)
7743                         items
7744                       (cons '(x-face-mule-delete-x-face-field
7745                               (quote never))
7746                             items))))
7747                  (gnus-treat-display-xface
7748                   (when (, x-face) gnus-treat-display-xface)))
7749              (gnus-article-prepare-mime-display)))
7750          (goto-char (if (, backward) start end))
7751          (when (, x-face)
7752            (gnus-summary-search-article-highlight-goto-x-face (point)))
7753          (setq gnus-summary-search-article-matched-data
7754                (list start end (buffer-substring start end)))
7755          (unless (eq start end);; matched text has been deleted. :-<
7756            (put-text-property start end 'face
7757                               (or (find-face 'isearch)
7758                                   'secondary-selection))))))
7759   )
7760
7761 (defun gnus-summary-search-article (regexp &optional backward)
7762   "Search for an article containing REGEXP.
7763 Optional argument BACKWARD means do search for backward.
7764 `gnus-select-article-hook' is not called during the search."
7765   ;; We have to require this here to make sure that the following
7766   ;; dynamic binding isn't shadowed by autoloading.
7767   (require 'gnus-async)
7768   (require 'gnus-art)
7769   (let ((gnus-select-article-hook nil)  ;Disable hook.
7770         (gnus-article-prepare-hook nil)
7771         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7772         (gnus-use-article-prefetch nil)
7773         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7774         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7775         (sum (current-buffer))
7776         (found nil)
7777         point treated)
7778     (gnus-save-hidden-threads
7779       (static-if (featurep 'xemacs)
7780           (let ((gnus-inhibit-treatment t))
7781             (setq treated (eq 'old (gnus-summary-select-article)))
7782             (when (and treated
7783                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7784                                  (window-live-p (get-buffer-window
7785                                                  gnus-article-buffer t)))))
7786               (gnus-summary-select-article nil t)
7787               (setq treated nil)))
7788         (let ((gnus-inhibit-treatment t)
7789               (x-face-mule-delete-x-face-field 'never))
7790           (setq treated (eq 'old (gnus-summary-select-article)))
7791           (when (and treated
7792                      (not
7793                       (and (gnus-buffer-live-p gnus-article-buffer)
7794                            (window-live-p (get-buffer-window
7795                                            gnus-article-buffer t))
7796                            (or (not (string-match "^\\^X-Face:" regexp))
7797                                (with-current-buffer gnus-article-buffer
7798                                  gnus-summary-search-article-matched-data)))))
7799             (gnus-summary-select-article nil t)
7800             (setq treated nil))))
7801       (set-buffer gnus-article-buffer)
7802       (widen)
7803       (if treated
7804           (progn
7805             (gnus-article-show-all-headers)
7806             (gnus-summary-search-article-position-point regexp backward))
7807         (goto-char (if backward (point-max) (point-min))))
7808       (while (not found)
7809         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7810         (if (if backward
7811                 (re-search-backward regexp nil t)
7812               (re-search-forward regexp nil t))
7813             ;; We found the regexp.
7814             (progn
7815               (gnus-summary-search-article-highlight-matched-text
7816                backward treated (string-match "^\\^X-Face:" regexp))
7817               (setq found 'found)
7818               (forward-line
7819                (/ (- 2 (window-height
7820                         (get-buffer-window gnus-article-buffer t)))
7821                   2))
7822               (set-window-start
7823                (get-buffer-window (current-buffer))
7824                (point))
7825               (set-buffer sum)
7826               (setq point (point)))
7827           ;; We didn't find it, so we go to the next article.
7828           (set-buffer sum)
7829           (setq found 'not)
7830           (while (eq found 'not)
7831             (if (not (if backward (gnus-summary-find-prev)
7832                        (gnus-summary-find-next)))
7833                 ;; No more articles.
7834                 (setq found t)
7835               ;; Select the next article and adjust point.
7836               (unless (gnus-summary-article-sparse-p
7837                        (gnus-summary-article-number))
7838                 (setq found nil)
7839                 (let ((gnus-inhibit-treatment t))
7840                   (gnus-summary-select-article))
7841                 (setq treated nil)
7842                 (set-buffer gnus-article-buffer)
7843                 (widen)
7844                 (goto-char (if backward (point-max) (point-min))))))))
7845       (gnus-message 7 ""))
7846     ;; Return whether we found the regexp.
7847     (when (eq found 'found)
7848       (goto-char point)
7849       (gnus-summary-show-thread)
7850       (gnus-summary-goto-subject gnus-current-article)
7851       (gnus-summary-position-point)
7852       t)))
7853
7854 (defun gnus-find-matching-articles (header regexp)
7855   "Return a list of all articles that match REGEXP on HEADER.
7856 This search includes all articles in the current group that Gnus has
7857 fetched headers for, whether they are displayed or not."
7858   (let ((articles nil)
7859         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7860         (case-fold-search t))
7861     (dolist (header gnus-newsgroup-headers)
7862       (when (string-match regexp (funcall func header))
7863         (push (mail-header-number header) articles)))
7864     (nreverse articles)))
7865
7866 (defun gnus-summary-find-matching (header regexp &optional backward unread
7867                                           not-case-fold not-matching)
7868   "Return a list of all articles that match REGEXP on HEADER.
7869 The search stars on the current article and goes forwards unless
7870 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7871 If UNREAD is non-nil, only unread articles will
7872 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7873 in the comparisons. If NOT-MATCHING, return a list of all articles that
7874 not match REGEXP on HEADER."
7875   (let ((case-fold-search (not not-case-fold))
7876         articles d func)
7877     (if (consp header)
7878         (if (eq (car header) 'extra)
7879             (setq func
7880                   `(lambda (h)
7881                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7882                          "")))
7883           (error "%s is an invalid header" header))
7884       (unless (fboundp (intern (concat "mail-header-" header)))
7885         (error "%s is not a valid header" header))
7886       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7887     (dolist (d (if (eq backward 'all)
7888                    gnus-newsgroup-data
7889                  (gnus-data-find-list
7890                   (gnus-summary-article-number)
7891                   (gnus-data-list backward))))
7892       (when (and (or (not unread)       ; We want all articles...
7893                      (gnus-data-unread-p d)) ; Or just unreads.
7894                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7895                  (if not-matching
7896                      (not (string-match
7897                            regexp
7898                            (funcall func (gnus-data-header d))))
7899                    (string-match regexp
7900                                  (funcall func (gnus-data-header d)))))
7901         (push (gnus-data-number d) articles))) ; Success!
7902     (nreverse articles)))
7903
7904 (defun gnus-summary-execute-command (header regexp command &optional backward)
7905   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7906 If HEADER is an empty string (or nil), the match is done on the entire
7907 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7908   (interactive
7909    (list (let ((completion-ignore-case t))
7910            (completing-read
7911             "Header name: "
7912             (mapcar (lambda (header) (list (format "%s" header)))
7913                     (append
7914                      '("Number" "Subject" "From" "Lines" "Date"
7915                        "Message-ID" "Xref" "References" "Body")
7916                      gnus-extra-headers))
7917             nil 'require-match))
7918          (read-string "Regexp: ")
7919          (read-key-sequence "Command: ")
7920          current-prefix-arg))
7921   (when (equal header "Body")
7922     (setq header ""))
7923   ;; Hidden thread subtrees must be searched as well.
7924   (gnus-summary-show-all-threads)
7925   ;; We don't want to change current point nor window configuration.
7926   (save-excursion
7927     (save-window-excursion
7928       (gnus-message 6 "Executing %s..." (key-description command))
7929       ;; We'd like to execute COMMAND interactively so as to give arguments.
7930       (gnus-execute header regexp
7931                     `(call-interactively ',(key-binding command))
7932                     backward)
7933       (gnus-message 6 "Executing %s...done" (key-description command)))))
7934
7935 (defun gnus-summary-beginning-of-article ()
7936   "Scroll the article back to the beginning."
7937   (interactive)
7938   (gnus-summary-select-article)
7939   (gnus-configure-windows 'article)
7940   (gnus-eval-in-buffer-window gnus-article-buffer
7941     (widen)
7942     (goto-char (point-min))
7943     (when gnus-page-broken
7944       (gnus-narrow-to-page))))
7945
7946 (defun gnus-summary-end-of-article ()
7947   "Scroll to the end of the article."
7948   (interactive)
7949   (gnus-summary-select-article)
7950   (gnus-configure-windows 'article)
7951   (gnus-eval-in-buffer-window gnus-article-buffer
7952     (widen)
7953     (goto-char (point-max))
7954     (recenter -3)
7955     (when gnus-page-broken
7956       (gnus-narrow-to-page))))
7957
7958 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7959   "Truncate to LEN and quote all \"(\"'s in STRING."
7960   (gnus-replace-in-string (if (and len (> (length string) len))
7961                               (substring string 0 len) 
7962                             string)
7963                           "[()]" "\\\\\\&"))
7964
7965 (defun gnus-summary-print-article (&optional filename n)
7966   "Generate and print a PostScript image of the N next (mail) articles.
7967
7968 If N is negative, print the N previous articles.  If N is nil and articles
7969 have been marked with the process mark, print these instead.
7970
7971 If the optional first argument FILENAME is nil, send the image to the
7972 printer.  If FILENAME is a string, save the PostScript image in a file with
7973 that name.  If FILENAME is a number, prompt the user for the name of the file
7974 to save in."
7975   (interactive (list (ps-print-preprint current-prefix-arg)))
7976   (dolist (article (gnus-summary-work-articles n))
7977     (gnus-summary-select-article nil nil 'pseudo article)
7978     (gnus-eval-in-buffer-window gnus-article-buffer
7979       (let ((buffer (generate-new-buffer " *print*")))
7980         (unwind-protect
7981             (progn
7982               (copy-to-buffer buffer (point-min) (point-max))
7983               (set-buffer buffer)
7984               (gnus-article-delete-invisible-text)
7985               (when (gnus-visual-p 'article-highlight 'highlight)
7986                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7987                 ;; highlight.
7988                 (let ((gnus-article-buffer buffer))
7989                   (gnus-article-highlight-citation t)
7990                   (gnus-article-highlight-signature)))
7991               (let ((ps-left-header
7992                      (list
7993                       (concat "("
7994                               (gnus-summary-print-truncate-and-quote
7995                                (mail-header-subject gnus-current-headers) 
7996                                66) ")")
7997                       (concat "("
7998                               (gnus-summary-print-truncate-and-quote
7999                                (mail-header-from gnus-current-headers) 
8000                                45) ")")))
8001                     (ps-right-header
8002                      (list
8003                       "/pagenumberstring load"
8004                       (concat "("
8005                               (mail-header-date gnus-current-headers) ")"))))
8006                 (gnus-run-hooks 'gnus-ps-print-hook)
8007                 (save-excursion
8008                   (if window-system
8009                       (ps-spool-buffer-with-faces)
8010                     (ps-spool-buffer)))))
8011           (kill-buffer buffer))))
8012     (gnus-summary-remove-process-mark article))
8013   (ps-despool filename))
8014
8015 (defun gnus-summary-show-article (&optional arg)
8016   "Force re-fetching of the current article.
8017 If ARG (the prefix) is a number, show the article with the charset
8018 defined in `gnus-summary-show-article-charset-alist', or the charset
8019 input.
8020 If ARG (the prefix) is non-nil and not a number, show the raw article
8021 without any article massaging functions being run."
8022   (interactive "P")
8023   (cond
8024    ((numberp arg)
8025     (gnus-summary-show-article t)
8026     (let ((gnus-newsgroup-charset
8027            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8028                (mm-read-coding-system
8029                 "View as charset: "
8030                 (save-excursion
8031                   (set-buffer gnus-article-buffer)
8032                   (let ((coding-systems
8033                          (detect-coding-region (point) (point-max))))
8034                     (or (car-safe coding-systems)
8035                         coding-systems))))))
8036           (gnus-newsgroup-ignored-charsets 'gnus-all))
8037       (gnus-summary-select-article nil 'force)
8038       (let ((deps gnus-newsgroup-dependencies)
8039             head header lines)
8040         (save-excursion
8041           (set-buffer gnus-original-article-buffer)
8042           (save-restriction
8043             (message-narrow-to-head)
8044             (setq head (buffer-string))
8045             (goto-char (point-min))
8046             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8047               (goto-char (point-max))
8048               (widen)
8049               (setq lines (1- (count-lines (point) (point-max))))))
8050           (with-temp-buffer
8051             (insert (format "211 %d Article retrieved.\n"
8052                             (cdr gnus-article-current)))
8053             (insert head)
8054             (if lines (insert (format "Lines: %d\n" lines)))
8055             (insert ".\n")
8056             (let ((nntp-server-buffer (current-buffer)))
8057               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8058         (gnus-data-set-header
8059          (gnus-data-find (cdr gnus-article-current))
8060          header)
8061         (gnus-summary-update-article-line
8062          (cdr gnus-article-current) header)
8063         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8064           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8065    ((not arg)
8066     ;; Select the article the normal way.
8067     (gnus-summary-select-article nil 'force))
8068    (t
8069     ;; We have to require this here to make sure that the following
8070     ;; dynamic binding isn't shadowed by autoloading.
8071     (require 'gnus-async)
8072     (require 'gnus-art)
8073     ;; Bind the article treatment functions to nil.
8074     (let ((gnus-have-all-headers t)
8075           gnus-article-prepare-hook
8076           gnus-article-decode-hook
8077           gnus-break-pages
8078           gnus-show-mime
8079           (gnus-inhibit-treatment t))
8080       (gnus-summary-select-article nil 'force))))
8081   (gnus-summary-goto-subject gnus-current-article)
8082   (gnus-summary-position-point))
8083
8084 (defun gnus-summary-show-raw-article ()
8085   "Show the raw article without any article massaging functions being run."
8086   (interactive)
8087   (gnus-summary-show-article t))
8088
8089 (defun gnus-summary-verbose-headers (&optional arg)
8090   "Toggle permanent full header display.
8091 If ARG is a positive number, turn header display on.
8092 If ARG is a negative number, turn header display off."
8093   (interactive "P")
8094   (setq gnus-show-all-headers
8095         (cond ((or (not (numberp arg))
8096                    (zerop arg))
8097                (not gnus-show-all-headers))
8098               ((natnump arg)
8099                t)))
8100   (gnus-summary-show-article))
8101
8102 (defun gnus-summary-toggle-header (&optional arg)
8103   "Show the headers if they are hidden, or hide them if they are shown.
8104 If ARG is a positive number, show the entire header.
8105 If ARG is a negative number, hide the unwanted header lines."
8106   (interactive "P")
8107   (save-excursion
8108     (set-buffer gnus-article-buffer)
8109     (save-restriction
8110       (let* ((buffer-read-only nil)
8111              (inhibit-point-motion-hooks t)
8112              hidden e)
8113         (setq hidden
8114               (if (numberp arg)
8115                   (>= arg 0)
8116                 (save-restriction
8117                   (article-narrow-to-head)
8118                   (gnus-article-hidden-text-p 'headers))))
8119         (goto-char (point-min))
8120         (when (search-forward "\n\n" nil t)
8121           (delete-region (point-min) (1- (point))))
8122         (goto-char (point-min))
8123         (save-excursion
8124           (set-buffer gnus-original-article-buffer)
8125           (goto-char (point-min))
8126           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8127         (insert-buffer-substring gnus-original-article-buffer 1 e)
8128         (save-restriction
8129           (narrow-to-region (point-min) (point))
8130           (article-decode-encoded-words)
8131           (if  hidden
8132               (let ((gnus-treat-hide-headers nil)
8133                     (gnus-treat-hide-boring-headers nil))
8134                 (setq gnus-article-wash-types
8135                       (delq 'headers gnus-article-wash-types))
8136                 (gnus-treat-article 'head))
8137             (gnus-treat-article 'head)))
8138         (gnus-set-mode-line 'article)))))
8139
8140 (defun gnus-summary-show-all-headers ()
8141   "Make all header lines visible."
8142   (interactive)
8143   (gnus-summary-toggle-header 1))
8144
8145 (defun gnus-summary-toggle-mime (&optional arg)
8146   "Toggle MIME processing.
8147 If ARG is a positive number, turn MIME processing on."
8148   (interactive "P")
8149   (setq gnus-show-mime
8150         (if (null arg)
8151             (not gnus-show-mime)
8152           (> (prefix-numeric-value arg) 0)))
8153   (gnus-summary-select-article t 'force))
8154
8155 (defun gnus-summary-caesar-message (&optional arg)
8156   "Caesar rotate the current article by 13.
8157 The numerical prefix specifies how many places to rotate each letter
8158 forward."
8159   (interactive "P")
8160   (gnus-summary-select-article)
8161   (let ((mail-header-separator ""))
8162     (gnus-eval-in-buffer-window gnus-article-buffer
8163       (save-restriction
8164         (widen)
8165         (let ((start (window-start))
8166               buffer-read-only)
8167           (message-caesar-buffer-body arg)
8168           (set-window-start (get-buffer-window (current-buffer)) start))))))
8169
8170 (defun gnus-summary-stop-page-breaking ()
8171   "Stop page breaking in the current article."
8172   (interactive)
8173   (gnus-summary-select-article)
8174   (gnus-eval-in-buffer-window gnus-article-buffer
8175     (widen)
8176     (when (gnus-visual-p 'page-marker)
8177       (let ((buffer-read-only nil))
8178         (gnus-remove-text-with-property 'gnus-prev)
8179         (gnus-remove-text-with-property 'gnus-next))
8180       (setq gnus-page-broken nil))))
8181
8182 (defun gnus-summary-move-article (&optional n to-newsgroup
8183                                             select-method action)
8184   "Move the current article to a different newsgroup.
8185 If N is a positive number, move the N next articles.
8186 If N is a negative number, move the N previous articles.
8187 If N is nil and any articles have been marked with the process mark,
8188 move those articles instead.
8189 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8190 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8191 re-spool using this method.
8192
8193 For this function to work, both the current newsgroup and the
8194 newsgroup that you want to move to have to support the `request-move'
8195 and `request-accept' functions.
8196
8197 ACTION can be either `move' (the default), `crosspost' or `copy'."
8198   (interactive "P")
8199   (unless action
8200     (setq action 'move))
8201   ;; Check whether the source group supports the required functions.
8202   (cond ((and (eq action 'move)
8203               (not (gnus-check-backend-function
8204                     'request-move-article gnus-newsgroup-name)))
8205          (error "The current group does not support article moving"))
8206         ((and (eq action 'crosspost)
8207               (not (gnus-check-backend-function
8208                     'request-replace-article gnus-newsgroup-name)))
8209          (error "The current group does not support article editing")))
8210   (let ((articles (gnus-summary-work-articles n))
8211         (prefix (if (gnus-check-backend-function
8212                      'request-move-article gnus-newsgroup-name)
8213                     (gnus-group-real-prefix gnus-newsgroup-name)
8214                   ""))
8215         (names '((move "Move" "Moving")
8216                  (copy "Copy" "Copying")
8217                  (crosspost "Crosspost" "Crossposting")))
8218         (copy-buf (save-excursion
8219                     (nnheader-set-temp-buffer " *copy article*")))
8220         (default-marks gnus-article-mark-lists)
8221         (no-expire-marks (delete '(expirable . expire)
8222                                  (copy-sequence gnus-article-mark-lists)))
8223         art-group to-method new-xref article to-groups)
8224     (unless (assq action names)
8225       (error "Unknown action %s" action))
8226     ;; Read the newsgroup name.
8227     (when (and (not to-newsgroup)
8228                (not select-method))
8229       (setq to-newsgroup
8230             (gnus-read-move-group-name
8231              (cadr (assq action names))
8232              (symbol-value (intern (format "gnus-current-%s-group" action)))
8233              articles prefix))
8234       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8235     (setq to-method (or select-method
8236                         (gnus-server-to-method
8237                          (gnus-group-method to-newsgroup))))
8238     ;; Check the method we are to move this article to...
8239     (unless (gnus-check-backend-function
8240              'request-accept-article (car to-method))
8241       (error "%s does not support article copying" (car to-method)))
8242     (unless (gnus-check-server to-method)
8243       (error "Can't open server %s" (car to-method)))
8244     (gnus-message 6 "%s to %s: %s..."
8245                   (caddr (assq action names))
8246                   (or (car select-method) to-newsgroup) articles)
8247     (while articles
8248       (setq article (pop articles))
8249       (setq
8250        art-group
8251        (cond
8252         ;; Move the article.
8253         ((eq action 'move)
8254          ;; Remove this article from future suppression.
8255          (gnus-dup-unsuppress-article article)
8256          (gnus-request-move-article
8257           article                       ; Article to move
8258           gnus-newsgroup-name           ; From newsgroup
8259           (nth 1 (gnus-find-method-for-group
8260                   gnus-newsgroup-name)) ; Server
8261           (list 'gnus-request-accept-article
8262                 to-newsgroup (list 'quote select-method)
8263                 (not articles) t)       ; Accept form
8264           (not articles)))              ; Only save nov last time
8265         ;; Copy the article.
8266         ((eq action 'copy)
8267          (save-excursion
8268            (set-buffer copy-buf)
8269            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8270              (gnus-request-accept-article
8271               to-newsgroup select-method (not articles) t))))
8272         ;; Crosspost the article.
8273         ((eq action 'crosspost)
8274          (let ((xref (message-tokenize-header
8275                       (mail-header-xref (gnus-summary-article-header article))
8276                       " ")))
8277            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8278                                   ":" (number-to-string article)))
8279            (unless xref
8280              (setq xref (list (system-name))))
8281            (setq new-xref
8282                  (concat
8283                   (mapconcat 'identity
8284                              (delete "Xref:" (delete new-xref xref))
8285                              " ")
8286                   " " new-xref))
8287            (save-excursion
8288              (set-buffer copy-buf)
8289              ;; First put the article in the destination group.
8290              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8291              (when (consp (setq art-group
8292                                 (gnus-request-accept-article
8293                                  to-newsgroup select-method (not articles))))
8294                (setq new-xref (concat new-xref " " (car art-group)
8295                                       ":"
8296                                       (number-to-string (cdr art-group))))
8297                ;; Now we have the new Xrefs header, so we insert
8298                ;; it and replace the new article.
8299                (nnheader-replace-header "Xref" new-xref)
8300                (gnus-request-replace-article
8301                 (cdr art-group) to-newsgroup (current-buffer))
8302                art-group))))))
8303       (cond
8304        ((not art-group)
8305         (gnus-message 1 "Couldn't %s article %s: %s"
8306                       (cadr (assq action names)) article
8307                       (nnheader-get-report (car to-method))))
8308        ((eq art-group 'junk)
8309         (when (eq action 'move)
8310           (gnus-summary-mark-article article gnus-canceled-mark)
8311           (gnus-message 4 "Deleted article %s" article)))
8312        (t
8313         (let* ((pto-group (gnus-group-prefixed-name
8314                            (car art-group) to-method))
8315                (entry
8316                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8317                (info (nth 2 entry))
8318                (to-group (gnus-info-group info))
8319                to-marks)
8320           ;; Update the group that has been moved to.
8321           (when (and info
8322                      (memq action '(move copy)))
8323             (unless (member to-group to-groups)
8324               (push to-group to-groups))
8325
8326             (unless (memq article gnus-newsgroup-unreads)
8327               (push 'read to-marks)
8328               (gnus-info-set-read
8329                info (gnus-add-to-range (gnus-info-read info)
8330                                        (list (cdr art-group)))))
8331
8332             ;; See whether the article is to be put in the cache.
8333             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8334                              default-marks
8335                            no-expire-marks))
8336                   (to-article (cdr art-group)))
8337
8338               ;; Enter the article into the cache in the new group,
8339               ;; if that is required.
8340               (when gnus-use-cache
8341                 (gnus-cache-possibly-enter-article
8342                  to-group to-article
8343                  (let ((header (copy-sequence
8344                                 (gnus-summary-article-header article))))
8345                    (mail-header-set-number header to-article)
8346                    header)
8347                  (memq article gnus-newsgroup-marked)
8348                  (memq article gnus-newsgroup-dormant)
8349                  (memq article gnus-newsgroup-unreads)))
8350
8351               (when gnus-preserve-marks
8352                 ;; Copy any marks over to the new group.
8353                 (when (and (equal to-group gnus-newsgroup-name)
8354                            (not (memq article gnus-newsgroup-unreads)))
8355                   ;; Mark this article as read in this group.
8356                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8357                   (setcdr (gnus-active to-group) to-article)
8358                   (setcdr gnus-newsgroup-active to-article))
8359
8360                 (while marks
8361                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8362                     (when (memq article (symbol-value
8363                                          (intern (format "gnus-newsgroup-%s"
8364                                                          (caar marks)))))
8365                       (push (cdar marks) to-marks)
8366                       ;; If the other group is the same as this group,
8367                       ;; then we have to add the mark to the list.
8368                       (when (equal to-group gnus-newsgroup-name)
8369                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8370                              (cons to-article
8371                                    (symbol-value
8372                                     (intern (format "gnus-newsgroup-%s"
8373                                                     (caar marks)))))))
8374                       ;; Copy the marks to other group.
8375                       (gnus-add-marked-articles
8376                        to-group (cdar marks) (list to-article) info)))
8377                   (setq marks (cdr marks)))
8378
8379                 (gnus-request-set-mark to-group (list (list (list to-article)
8380                                                             'add
8381                                                             to-marks))))
8382
8383               (gnus-dribble-enter
8384                (concat "(gnus-group-set-info '"
8385                        (gnus-prin1-to-string (gnus-get-info to-group))
8386                        ")"))))
8387
8388           ;; Update the Xref header in this article to point to
8389           ;; the new crossposted article we have just created.
8390           (when (eq action 'crosspost)
8391             (save-excursion
8392               (set-buffer copy-buf)
8393               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8394               (nnheader-replace-header "Xref" new-xref)
8395               (gnus-request-replace-article
8396                article gnus-newsgroup-name (current-buffer)))))
8397
8398         ;;;!!!Why is this necessary?
8399         (set-buffer gnus-summary-buffer)
8400
8401         (gnus-summary-goto-subject article)
8402         (when (eq action 'move)
8403           (gnus-summary-mark-article article gnus-canceled-mark))))
8404       (gnus-summary-remove-process-mark article))
8405     ;; Re-activate all groups that have been moved to.
8406     (save-excursion
8407       (set-buffer gnus-group-buffer)
8408       (let ((gnus-group-marked to-groups))
8409         (gnus-group-get-new-news-this-group nil t)))
8410
8411     (gnus-kill-buffer copy-buf)
8412     (gnus-summary-position-point)
8413     (gnus-set-mode-line 'summary)))
8414
8415 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8416   "Move the current article to a different newsgroup.
8417 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8418 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8419 re-spool using this method."
8420   (interactive "P")
8421   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8422
8423 (defun gnus-summary-crosspost-article (&optional n)
8424   "Crosspost the current article to some other group."
8425   (interactive "P")
8426   (gnus-summary-move-article n nil nil 'crosspost))
8427
8428 (defcustom gnus-summary-respool-default-method nil
8429   "Default method type for respooling an article.
8430 If nil, use to the current newsgroup method."
8431   :type 'symbol
8432   :group 'gnus-summary-mail)
8433
8434 (defun gnus-summary-respool-article (&optional n method)
8435   "Respool the current article.
8436 The article will be squeezed through the mail spooling process again,
8437 which means that it will be put in some mail newsgroup or other
8438 depending on `nnmail-split-methods'.
8439 If N is a positive number, respool the N next articles.
8440 If N is a negative number, respool the N previous articles.
8441 If N is nil and any articles have been marked with the process mark,
8442 respool those articles instead.
8443
8444 Respooling can be done both from mail groups and \"real\" newsgroups.
8445 In the former case, the articles in question will be moved from the
8446 current group into whatever groups they are destined to.  In the
8447 latter case, they will be copied into the relevant groups."
8448   (interactive
8449    (list current-prefix-arg
8450          (let* ((methods (gnus-methods-using 'respool))
8451                 (methname
8452                  (symbol-name (or gnus-summary-respool-default-method
8453                                   (car (gnus-find-method-for-group
8454                                         gnus-newsgroup-name)))))
8455                 (method
8456                  (gnus-completing-read
8457                   methname "What backend do you want to use when respooling?"
8458                   methods nil t nil 'gnus-mail-method-history))
8459                 ms)
8460            (cond
8461             ((zerop (length (setq ms (gnus-servers-using-backend
8462                                       (intern method)))))
8463              (list (intern method) ""))
8464             ((= 1 (length ms))
8465              (car ms))
8466             (t
8467              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8468                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8469                            ms-alist))))))))
8470   (unless method
8471     (error "No method given for respooling"))
8472   (if (assoc (symbol-name
8473               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8474              (gnus-methods-using 'respool))
8475       (gnus-summary-move-article n nil method)
8476     (gnus-summary-copy-article n nil method)))
8477
8478 (defun gnus-summary-import-article (file &optional edit)
8479   "Import an arbitrary file into a mail newsgroup."
8480   (interactive "fImport file: \nP")
8481   (let ((group gnus-newsgroup-name)
8482         (now (current-time))
8483         atts lines group-art)
8484     (unless (gnus-check-backend-function 'request-accept-article group)
8485       (error "%s does not support article importing" group))
8486     (or (file-readable-p file)
8487         (not (file-regular-p file))
8488         (error "Can't read %s" file))
8489     (save-excursion
8490       (set-buffer (gnus-get-buffer-create " *import file*"))
8491       (erase-buffer)
8492       (nnheader-insert-file-contents file)
8493       (goto-char (point-min))
8494       (if (nnheader-article-p)
8495           (save-restriction
8496             (goto-char (point-min))
8497             (search-forward "\n\n" nil t)
8498             (narrow-to-region (point-min) (1- (point)))
8499             (goto-char (point-min))
8500             (unless (re-search-forward "^date:" nil t)
8501               (goto-char (point-max))
8502               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8503         ;; This doesn't look like an article, so we fudge some headers.
8504         (setq atts (file-attributes file)
8505               lines (count-lines (point-min) (point-max)))
8506         (insert "From: " (read-string "From: ") "\n"
8507                 "Subject: " (read-string "Subject: ") "\n"
8508                 "Date: " (message-make-date (nth 5 atts)) "\n"
8509                 "Message-ID: " (message-make-message-id) "\n"
8510                 "Lines: " (int-to-string lines) "\n"
8511                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8512       (setq group-art (gnus-request-accept-article group nil t))
8513       (kill-buffer (current-buffer)))
8514     (setq gnus-newsgroup-active (gnus-activate-group group))
8515     (forward-line 1)
8516     (gnus-summary-goto-article (cdr group-art) nil t)
8517     (when edit
8518       (gnus-summary-edit-article))))
8519
8520 (defun gnus-summary-create-article ()
8521   "Create an article in a mail newsgroup."
8522   (interactive)
8523   (let ((group gnus-newsgroup-name)
8524         (now (current-time))
8525         group-art)
8526     (unless (gnus-check-backend-function 'request-accept-article group)
8527       (error "%s does not support article importing" group))
8528     (save-excursion
8529       (set-buffer (gnus-get-buffer-create " *import file*"))
8530       (erase-buffer)
8531       (goto-char (point-min))
8532       ;; This doesn't look like an article, so we fudge some headers.
8533       (insert "From: " (read-string "From: ") "\n"
8534               "Subject: " (read-string "Subject: ") "\n"
8535               "Date: " (message-make-date now) "\n"
8536               "Message-ID: " (message-make-message-id) "\n")
8537       (setq group-art (gnus-request-accept-article group nil t))
8538       (kill-buffer (current-buffer)))
8539     (setq gnus-newsgroup-active (gnus-activate-group group))
8540     (forward-line 1)
8541     (gnus-summary-goto-article (cdr group-art) nil t)
8542     (gnus-summary-edit-article)))
8543
8544 (defun gnus-summary-article-posted-p ()
8545   "Say whether the current (mail) article is available from news as well.
8546 This will be the case if the article has both been mailed and posted."
8547   (interactive)
8548   (let ((id (mail-header-references (gnus-summary-article-header)))
8549         (gnus-override-method (car (gnus-refer-article-methods))))
8550     (if (gnus-request-head id "")
8551         (gnus-message 2 "The current message was found on %s"
8552                       gnus-override-method)
8553       (gnus-message 2 "The current message couldn't be found on %s"
8554                     gnus-override-method)
8555       nil)))
8556
8557 (defun gnus-summary-expire-articles (&optional now)
8558   "Expire all articles that are marked as expirable in the current group."
8559   (interactive)
8560   (when (gnus-check-backend-function
8561          'request-expire-articles gnus-newsgroup-name)
8562     ;; This backend supports expiry.
8563     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8564            (expirable (if total
8565                           (progn
8566                             ;; We need to update the info for
8567                             ;; this group for `gnus-list-of-read-articles'
8568                             ;; to give us the right answer.
8569                             (gnus-run-hooks 'gnus-exit-group-hook)
8570                             (gnus-summary-update-info)
8571                             (gnus-list-of-read-articles gnus-newsgroup-name))
8572                         (setq gnus-newsgroup-expirable
8573                               (sort gnus-newsgroup-expirable '<))))
8574            (expiry-wait (if now 'immediate
8575                           (gnus-group-find-parameter
8576                            gnus-newsgroup-name 'expiry-wait)))
8577            (nnmail-expiry-target
8578             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8579                 nnmail-expiry-target))
8580            es)
8581       (when expirable
8582         ;; There are expirable articles in this group, so we run them
8583         ;; through the expiry process.
8584         (gnus-message 6 "Expiring articles...")
8585         (unless (gnus-check-group gnus-newsgroup-name)
8586           (error "Can't open server for %s" gnus-newsgroup-name))
8587         ;; The list of articles that weren't expired is returned.
8588         (save-excursion
8589           (if expiry-wait
8590               (let ((nnmail-expiry-wait-function nil)
8591                     (nnmail-expiry-wait expiry-wait))
8592                 (setq es (gnus-request-expire-articles
8593                           expirable gnus-newsgroup-name)))
8594             (setq es (gnus-request-expire-articles
8595                       expirable gnus-newsgroup-name)))
8596           (unless total
8597             (setq gnus-newsgroup-expirable es))
8598           ;; We go through the old list of expirable, and mark all
8599           ;; really expired articles as nonexistent.
8600           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8601             (let ((gnus-use-cache nil))
8602               (while expirable
8603                 (unless (memq (car expirable) es)
8604                   (when (gnus-data-find (car expirable))
8605                     (gnus-summary-mark-article
8606                      (car expirable) gnus-canceled-mark)))
8607                 (setq expirable (cdr expirable))))))
8608         (gnus-message 6 "Expiring articles...done")))))
8609
8610 (defun gnus-summary-expire-articles-now ()
8611   "Expunge all expirable articles in the current group.
8612 This means that *all* articles that are marked as expirable will be
8613 deleted forever, right now."
8614   (interactive)
8615   (or gnus-expert-user
8616       (gnus-yes-or-no-p
8617        "Are you really, really, really sure you want to delete all these messages? ")
8618       (error "Phew!"))
8619   (gnus-summary-expire-articles t))
8620
8621 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8622 (defun gnus-summary-delete-article (&optional n)
8623   "Delete the N next (mail) articles.
8624 This command actually deletes articles.  This is not a marking
8625 command.  The article will disappear forever from your life, never to
8626 return.
8627 If N is negative, delete backwards.
8628 If N is nil and articles have been marked with the process mark,
8629 delete these instead."
8630   (interactive "P")
8631   (unless (gnus-check-backend-function 'request-expire-articles
8632                                        gnus-newsgroup-name)
8633     (error "The current newsgroup does not support article deletion"))
8634   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8635     (error "Couldn't open server"))
8636   ;; Compute the list of articles to delete.
8637   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8638         not-deleted)
8639     (if (and gnus-novice-user
8640              (not (gnus-yes-or-no-p
8641                    (format "Do you really want to delete %s forever? "
8642                            (if (> (length articles) 1)
8643                                (format "these %s articles" (length articles))
8644                              "this article")))))
8645         ()
8646       ;; Delete the articles.
8647       (setq not-deleted (gnus-request-expire-articles
8648                          articles gnus-newsgroup-name 'force))
8649       (while articles
8650         (gnus-summary-remove-process-mark (car articles))
8651         ;; The backend might not have been able to delete the article
8652         ;; after all.
8653         (unless (memq (car articles) not-deleted)
8654           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8655         (setq articles (cdr articles)))
8656       (when not-deleted
8657         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8658     (gnus-summary-position-point)
8659     (gnus-set-mode-line 'summary)
8660     not-deleted))
8661
8662 (defun gnus-summary-edit-article (&optional force)
8663   "Edit the current article.
8664 This will have permanent effect only in mail groups.
8665 If FORCE is non-nil, allow editing of articles even in read-only
8666 groups."
8667   (interactive "P")
8668   (save-excursion
8669     (set-buffer gnus-summary-buffer)
8670     (let ((mail-parse-charset gnus-newsgroup-charset)
8671           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8672       (gnus-set-global-variables)
8673       (when (and (not force)
8674                  (gnus-group-read-only-p))
8675         (error "The current newsgroup does not support article editing"))
8676       (gnus-summary-show-article t)
8677       (gnus-article-edit-article
8678        'ignore
8679        `(lambda (no-highlight)
8680           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8681                 (message-options message-options)
8682                 (message-options-set-recipient)
8683                 (mail-parse-ignored-charsets
8684                  ',gnus-newsgroup-ignored-charsets))
8685             (gnus-summary-edit-article-done
8686              ,(or (mail-header-references gnus-current-headers) "")
8687              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8688
8689 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8690
8691 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8692                                                  no-highlight)
8693   "Make edits to the current article permanent."
8694   (interactive)
8695   (save-excursion
8696     ;; The buffer restriction contains the entire article if it exists.
8697     (when (article-goto-body)
8698       (let ((lines (count-lines (point) (point-max)))
8699             (length (- (point-max) (point)))
8700             (case-fold-search t)
8701             (body (copy-marker (point))))
8702         (goto-char (point-min))
8703         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8704           (delete-region (match-beginning 1) (match-end 1))
8705           (insert (number-to-string length)))
8706         (goto-char (point-min))
8707         (when (re-search-forward
8708                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8709           (delete-region (match-beginning 1) (match-end 1))
8710           (insert (number-to-string length)))
8711         (goto-char (point-min))
8712         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8713           (delete-region (match-beginning 1) (match-end 1))
8714           (insert (number-to-string lines))))))
8715   ;; Replace the article.
8716   (let ((buf (current-buffer)))
8717     (with-temp-buffer
8718       (insert-buffer-substring buf)
8719
8720       (if (and (not read-only)
8721                (not (gnus-request-replace-article
8722                      (cdr gnus-article-current) (car gnus-article-current)
8723                      (current-buffer) t)))
8724           (error "Couldn't replace article")
8725         ;; Update the summary buffer.
8726         (if (and references
8727                  (equal (message-tokenize-header references " ")
8728                         (message-tokenize-header
8729                          (or (message-fetch-field "references") "") " ")))
8730             ;; We only have to update this line.
8731             (save-excursion
8732               (save-restriction
8733                 (message-narrow-to-head)
8734                 (let ((head (buffer-string))
8735                       header)
8736                   (with-temp-buffer
8737                     (insert (format "211 %d Article retrieved.\n"
8738                                     (cdr gnus-article-current)))
8739                     (insert head)
8740                     (insert ".\n")
8741                     (let ((nntp-server-buffer (current-buffer)))
8742                       (setq header (car (gnus-get-newsgroup-headers
8743                                          (save-excursion
8744                                            (set-buffer gnus-summary-buffer)
8745                                            gnus-newsgroup-dependencies)
8746                                          t))))
8747                     (save-excursion
8748                       (set-buffer gnus-summary-buffer)
8749                       (gnus-data-set-header
8750                        (gnus-data-find (cdr gnus-article-current))
8751                        header)
8752                       (gnus-summary-update-article-line
8753                        (cdr gnus-article-current) header)
8754                       (if (gnus-summary-goto-subject
8755                            (cdr gnus-article-current) nil t)
8756                           (gnus-summary-update-secondary-mark
8757                            (cdr gnus-article-current))))))))
8758           ;; Update threads.
8759           (set-buffer (or buffer gnus-summary-buffer))
8760           (gnus-summary-update-article (cdr gnus-article-current))
8761           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8762               (gnus-summary-update-secondary-mark
8763                (cdr gnus-article-current))))
8764         ;; Prettify the article buffer again.
8765         (unless no-highlight
8766           (save-excursion
8767             (set-buffer gnus-article-buffer)
8768             ;;;!!! Fix this -- article should be rehighlighted.
8769             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8770             (set-buffer gnus-original-article-buffer)
8771             (gnus-request-article
8772              (cdr gnus-article-current)
8773              (car gnus-article-current) (current-buffer))))
8774         ;; Prettify the summary buffer line.
8775         (when (gnus-visual-p 'summary-highlight 'highlight)
8776           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8777
8778 (defun gnus-summary-edit-wash (key)
8779   "Perform editing command KEY in the article buffer."
8780   (interactive
8781    (list
8782     (progn
8783       (message "%s" (concat (this-command-keys) "- "))
8784       (read-char))))
8785   (message "")
8786   (gnus-summary-edit-article)
8787   (execute-kbd-macro (concat (this-command-keys) key))
8788   (gnus-article-edit-done))
8789
8790 (defun gnus-summary-toggle-smiley (&optional arg)
8791   "Toggle the display of smilies as small graphical icons."
8792   (interactive "P")
8793   (save-excursion
8794     (set-buffer gnus-article-buffer)
8795     (gnus-smiley-display arg)))
8796
8797 ;;; Respooling
8798
8799 (defun gnus-summary-respool-query (&optional silent trace)
8800   "Query where the respool algorithm would put this article."
8801   (interactive)
8802   (let (gnus-mark-article-hook)
8803     (gnus-summary-select-article)
8804     (save-excursion
8805       (set-buffer gnus-original-article-buffer)
8806       (save-restriction
8807         (message-narrow-to-head)
8808         (let ((groups (nnmail-article-group 'identity trace)))
8809           (unless silent
8810             (if groups
8811                 (message "This message would go to %s"
8812                          (mapconcat 'car groups ", "))
8813               (message "This message would go to no groups"))
8814             groups))))))
8815
8816 (defun gnus-summary-respool-trace ()
8817   "Trace where the respool algorithm would put this article.
8818 Display a buffer showing all fancy splitting patterns which matched."
8819   (interactive)
8820   (gnus-summary-respool-query nil t))
8821
8822 ;; Summary marking commands.
8823
8824 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8825   "Mark articles which has the same subject as read, and then select the next.
8826 If UNMARK is positive, remove any kind of mark.
8827 If UNMARK is negative, tick articles."
8828   (interactive "P")
8829   (when unmark
8830     (setq unmark (prefix-numeric-value unmark)))
8831   (let ((count
8832          (gnus-summary-mark-same-subject
8833           (gnus-summary-article-subject) unmark)))
8834     ;; Select next unread article.  If auto-select-same mode, should
8835     ;; select the first unread article.
8836     (gnus-summary-next-article t (and gnus-auto-select-same
8837                                       (gnus-summary-article-subject)))
8838     (gnus-message 7 "%d article%s marked as %s"
8839                   count (if (= count 1) " is" "s are")
8840                   (if unmark "unread" "read"))))
8841
8842 (defun gnus-summary-kill-same-subject (&optional unmark)
8843   "Mark articles which has the same subject as read.
8844 If UNMARK is positive, remove any kind of mark.
8845 If UNMARK is negative, tick articles."
8846   (interactive "P")
8847   (when unmark
8848     (setq unmark (prefix-numeric-value unmark)))
8849   (let ((count
8850          (gnus-summary-mark-same-subject
8851           (gnus-summary-article-subject) unmark)))
8852     ;; If marked as read, go to next unread subject.
8853     (when (null unmark)
8854       ;; Go to next unread subject.
8855       (gnus-summary-next-subject 1 t))
8856     (gnus-message 7 "%d articles are marked as %s"
8857                   count (if unmark "unread" "read"))))
8858
8859 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8860   "Mark articles with same SUBJECT as read, and return marked number.
8861 If optional argument UNMARK is positive, remove any kinds of marks.
8862 If optional argument UNMARK is negative, mark articles as unread instead."
8863   (let ((count 1))
8864     (save-excursion
8865       (cond
8866        ((null unmark)                   ; Mark as read.
8867         (while (and
8868                 (progn
8869                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8870                   (gnus-summary-show-thread) t)
8871                 (gnus-summary-find-subject subject))
8872           (setq count (1+ count))))
8873        ((> unmark 0)                    ; Tick.
8874         (while (and
8875                 (progn
8876                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8877                   (gnus-summary-show-thread) t)
8878                 (gnus-summary-find-subject subject))
8879           (setq count (1+ count))))
8880        (t                               ; Mark as unread.
8881         (while (and
8882                 (progn
8883                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8884                   (gnus-summary-show-thread) t)
8885                 (gnus-summary-find-subject subject))
8886           (setq count (1+ count)))))
8887       (gnus-set-mode-line 'summary)
8888       ;; Return the number of marked articles.
8889       count)))
8890
8891 (defun gnus-summary-mark-as-processable (n &optional unmark)
8892   "Set the process mark on the next N articles.
8893 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8894 the process mark instead.  The difference between N and the actual
8895 number of articles marked is returned."
8896   (interactive "P")
8897   (if (and (null n) (gnus-region-active-p))
8898       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8899     (setq n (prefix-numeric-value n))
8900     (let ((backward (< n 0))
8901           (n (abs n)))
8902       (while (and
8903               (> n 0)
8904               (if unmark
8905                   (gnus-summary-remove-process-mark
8906                    (gnus-summary-article-number))
8907                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8908               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8909         (setq n (1- n)))
8910       (when (/= 0 n)
8911         (gnus-message 7 "No more articles"))
8912       (gnus-summary-recenter)
8913       (gnus-summary-position-point)
8914       n)))
8915
8916 (defun gnus-summary-unmark-as-processable (n)
8917   "Remove the process mark from the next N articles.
8918 If N is negative, unmark backward instead.  The difference between N and
8919 the actual number of articles unmarked is returned."
8920   (interactive "P")
8921   (gnus-summary-mark-as-processable n t))
8922
8923 (defun gnus-summary-unmark-all-processable ()
8924   "Remove the process mark from all articles."
8925   (interactive)
8926   (save-excursion
8927     (while gnus-newsgroup-processable
8928       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8929   (gnus-summary-position-point))
8930
8931 (defun gnus-summary-add-mark (article type)
8932   "Mark ARTICLE with a mark of TYPE."
8933   (let ((vtype (car (assq type gnus-article-mark-lists)))
8934         var)
8935     (if (not vtype)
8936         (error "No such mark type: %s" type)
8937       (setq var (intern (format "gnus-newsgroup-%s" type)))
8938       (set var (cons article (symbol-value var)))
8939       (if (memq type '(processable cached replied forwarded recent saved))
8940           (gnus-summary-update-secondary-mark article)
8941         ;;; !!! This is bogus.  We should find out what primary
8942         ;;; !!! mark we want to set.
8943         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8944
8945 (defun gnus-summary-mark-as-expirable (n)
8946   "Mark N articles forward as expirable.
8947 If N is negative, mark backward instead.  The difference between N and
8948 the actual number of articles marked is returned."
8949   (interactive "p")
8950   (gnus-summary-mark-forward n gnus-expirable-mark))
8951
8952 (defun gnus-summary-mark-article-as-replied (article)
8953   "Mark ARTICLE as replied to and update the summary line.
8954 ARTICLE can also be a list of articles."
8955   (interactive (list (gnus-summary-article-number)))
8956   (let ((articles (if (listp article) article (list article))))
8957     (dolist (article articles)
8958       (push article gnus-newsgroup-replied)
8959       (let ((buffer-read-only nil))
8960         (when (gnus-summary-goto-subject article nil t)
8961           (gnus-summary-update-secondary-mark article))))))
8962
8963 (defun gnus-summary-mark-article-as-forwarded (article)
8964   "Mark ARTICLE as forwarded and update the summary line.
8965 ARTICLE can also be a list of articles."
8966   (let ((articles (if (listp article) article (list article))))
8967     (dolist (article articles)
8968       (push article gnus-newsgroup-forwarded)
8969       (let ((buffer-read-only nil))
8970         (when (gnus-summary-goto-subject article nil t)
8971           (gnus-summary-update-secondary-mark article))))))
8972
8973 (defun gnus-summary-set-bookmark (article)
8974   "Set a bookmark in current article."
8975   (interactive (list (gnus-summary-article-number)))
8976   (when (or (not (get-buffer gnus-article-buffer))
8977             (not gnus-current-article)
8978             (not gnus-article-current)
8979             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8980     (error "No current article selected"))
8981   ;; Remove old bookmark, if one exists.
8982   (let ((old (assq article gnus-newsgroup-bookmarks)))
8983     (when old
8984       (setq gnus-newsgroup-bookmarks
8985             (delq old gnus-newsgroup-bookmarks))))
8986   ;; Set the new bookmark, which is on the form
8987   ;; (article-number . line-number-in-body).
8988   (push
8989    (cons article
8990          (save-excursion
8991            (set-buffer gnus-article-buffer)
8992            (count-lines
8993             (min (point)
8994                  (save-excursion
8995                    (goto-char (point-min))
8996                    (search-forward "\n\n" nil t)
8997                    (point)))
8998             (point))))
8999    gnus-newsgroup-bookmarks)
9000   (gnus-message 6 "A bookmark has been added to the current article."))
9001
9002 (defun gnus-summary-remove-bookmark (article)
9003   "Remove the bookmark from the current article."
9004   (interactive (list (gnus-summary-article-number)))
9005   ;; Remove old bookmark, if one exists.
9006   (let ((old (assq article gnus-newsgroup-bookmarks)))
9007     (if old
9008         (progn
9009           (setq gnus-newsgroup-bookmarks
9010                 (delq old gnus-newsgroup-bookmarks))
9011           (gnus-message 6 "Removed bookmark."))
9012       (gnus-message 6 "No bookmark in current article."))))
9013
9014 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9015 (defun gnus-summary-mark-as-dormant (n)
9016   "Mark N articles forward as dormant.
9017 If N is negative, mark backward instead.  The difference between N and
9018 the actual number of articles marked is returned."
9019   (interactive "p")
9020   (gnus-summary-mark-forward n gnus-dormant-mark))
9021
9022 (defun gnus-summary-set-process-mark (article)
9023   "Set the process mark on ARTICLE and update the summary line."
9024   (setq gnus-newsgroup-processable
9025         (cons article
9026               (delq article gnus-newsgroup-processable)))
9027   (when (gnus-summary-goto-subject article)
9028     (gnus-summary-show-thread)
9029     (gnus-summary-goto-subject article)
9030     (gnus-summary-update-secondary-mark article)))
9031
9032 (defun gnus-summary-remove-process-mark (article)
9033   "Remove the process mark from ARTICLE and update the summary line."
9034   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9035   (when (gnus-summary-goto-subject article)
9036     (gnus-summary-show-thread)
9037     (gnus-summary-goto-subject article)
9038     (gnus-summary-update-secondary-mark article)))
9039
9040 (defun gnus-summary-set-saved-mark (article)
9041   "Set the process mark on ARTICLE and update the summary line."
9042   (push article gnus-newsgroup-saved)
9043   (when (gnus-summary-goto-subject article)
9044     (gnus-summary-update-secondary-mark article)))
9045
9046 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9047   "Mark N articles as read forwards.
9048 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9049 The difference between N and the actual number of articles marked is
9050 returned.
9051 Iff NO-EXPIRE, auto-expiry will be inhibited."
9052   (interactive "p")
9053   (gnus-summary-show-thread)
9054   (let ((backward (< n 0))
9055         (gnus-summary-goto-unread
9056          (and gnus-summary-goto-unread
9057               (not (eq gnus-summary-goto-unread 'never))
9058               (not (memq mark (list gnus-unread-mark
9059                                     gnus-ticked-mark gnus-dormant-mark)))))
9060         (n (abs n))
9061         (mark (or mark gnus-del-mark)))
9062     (while (and (> n 0)
9063                 (gnus-summary-mark-article nil mark no-expire)
9064                 (zerop (gnus-summary-next-subject
9065                         (if backward -1 1)
9066                         (and gnus-summary-goto-unread
9067                              (not (eq gnus-summary-goto-unread 'never)))
9068                         t)))
9069       (setq n (1- n)))
9070     (when (/= 0 n)
9071       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9072     (gnus-summary-recenter)
9073     (gnus-summary-position-point)
9074     (gnus-set-mode-line 'summary)
9075     n))
9076
9077 (defun gnus-summary-mark-article-as-read (mark)
9078   "Mark the current article quickly as read with MARK."
9079   (let ((article (gnus-summary-article-number)))
9080     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9081     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9082     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9083     (push (cons article mark) gnus-newsgroup-reads)
9084     ;; Possibly remove from cache, if that is used.
9085     (when gnus-use-cache
9086       (gnus-cache-enter-remove-article article))
9087     ;; Allow the backend to change the mark.
9088     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9089     ;; Check for auto-expiry.
9090     (when (and gnus-newsgroup-auto-expire
9091                (memq mark gnus-auto-expirable-marks))
9092       (setq mark gnus-expirable-mark)
9093       ;; Let the backend know about the mark change.
9094       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9095       (push article gnus-newsgroup-expirable))
9096     ;; Set the mark in the buffer.
9097     (gnus-summary-update-mark mark 'unread)
9098     t))
9099
9100 (defun gnus-summary-mark-article-as-unread (mark)
9101   "Mark the current article quickly as unread with MARK."
9102   (let* ((article (gnus-summary-article-number))
9103          (old-mark (gnus-summary-article-mark article)))
9104     ;; Allow the backend to change the mark.
9105     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9106     (if (eq mark old-mark)
9107         t
9108       (if (<= article 0)
9109           (progn
9110             (gnus-error 1 "Can't mark negative article numbers")
9111             nil)
9112         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9113         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9114         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9115         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9116         (cond ((= mark gnus-ticked-mark)
9117                (push article gnus-newsgroup-marked))
9118               ((= mark gnus-dormant-mark)
9119                (push article gnus-newsgroup-dormant))
9120               (t
9121                (push article gnus-newsgroup-unreads)))
9122         (gnus-pull article gnus-newsgroup-reads)
9123
9124         ;; See whether the article is to be put in the cache.
9125         (and gnus-use-cache
9126              (vectorp (gnus-summary-article-header article))
9127              (save-excursion
9128                (gnus-cache-possibly-enter-article
9129                 gnus-newsgroup-name article
9130                 (gnus-summary-article-header article)
9131                 (= mark gnus-ticked-mark)
9132                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9133
9134         ;; Fix the mark.
9135         (gnus-summary-update-mark mark 'unread)
9136         t))))
9137
9138 (defun gnus-summary-mark-article (&optional article mark no-expire)
9139   "Mark ARTICLE with MARK.  MARK can be any character.
9140 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9141 `??' (dormant) and `?E' (expirable).
9142 If MARK is nil, then the default character `?r' is used.
9143 If ARTICLE is nil, then the article on the current line will be
9144 marked.
9145 Iff NO-EXPIRE, auto-expiry will be inhibited."
9146   ;; The mark might be a string.
9147   (when (stringp mark)
9148     (setq mark (aref mark 0)))
9149   ;; If no mark is given, then we check auto-expiring.
9150   (when (null mark)
9151     (setq mark gnus-del-mark))
9152   (when (and (not no-expire)
9153              gnus-newsgroup-auto-expire
9154              (memq mark gnus-auto-expirable-marks))
9155     (setq mark gnus-expirable-mark))
9156   (let ((article (or article (gnus-summary-article-number)))
9157         (old-mark (gnus-summary-article-mark article)))
9158     ;; Allow the backend to change the mark.
9159     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9160     (if (eq mark old-mark)
9161         t
9162       (unless article
9163         (error "No article on current line"))
9164       (if (not (if (or (= mark gnus-unread-mark)
9165                        (= mark gnus-ticked-mark)
9166                        (= mark gnus-dormant-mark))
9167                    (gnus-mark-article-as-unread article mark)
9168                  (gnus-mark-article-as-read article mark)))
9169           t
9170         ;; See whether the article is to be put in the cache.
9171         (and gnus-use-cache
9172              (not (= mark gnus-canceled-mark))
9173              (vectorp (gnus-summary-article-header article))
9174              (save-excursion
9175                (gnus-cache-possibly-enter-article
9176                 gnus-newsgroup-name article
9177                 (gnus-summary-article-header article)
9178                 (= mark gnus-ticked-mark)
9179                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9180
9181         (when (gnus-summary-goto-subject article nil t)
9182           (let ((buffer-read-only nil))
9183             (gnus-summary-show-thread)
9184             ;; Fix the mark.
9185             (gnus-summary-update-mark mark 'unread)
9186             t))))))
9187
9188 (defun gnus-summary-update-secondary-mark (article)
9189   "Update the secondary (read, process, cache) mark."
9190   (gnus-summary-update-mark
9191    (cond ((memq article gnus-newsgroup-processable)
9192           gnus-process-mark)
9193          ((memq article gnus-newsgroup-cached)
9194           gnus-cached-mark)
9195          ((memq article gnus-newsgroup-replied)
9196           gnus-replied-mark)
9197          ((memq article gnus-newsgroup-forwarded)
9198           gnus-forwarded-mark)
9199          ((memq article gnus-newsgroup-saved)
9200           gnus-saved-mark)
9201          ((memq article gnus-newsgroup-recent)
9202           gnus-recent-mark)
9203          ((memq article gnus-newsgroup-unseen)
9204           gnus-unseen-mark)
9205          (t gnus-no-mark))
9206    'replied)
9207   (when (gnus-visual-p 'summary-highlight 'highlight)
9208     (gnus-run-hooks 'gnus-summary-update-hook))
9209   t)
9210
9211 (defun gnus-summary-update-mark (mark type)
9212   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9213         (buffer-read-only nil))
9214     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9215     (when forward
9216       (when (looking-at "\r")
9217         (incf forward))
9218       (when (<= (+ forward (point)) (point-max))
9219         ;; Go to the right position on the line.
9220         (goto-char (+ forward (point)))
9221         ;; Replace the old mark with the new mark.
9222         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9223         ;; Optionally update the marks by some user rule.
9224         (when (eq type 'unread)
9225           (gnus-data-set-mark
9226            (gnus-data-find (gnus-summary-article-number)) mark)
9227           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9228
9229 (defun gnus-mark-article-as-read (article &optional mark)
9230   "Enter ARTICLE in the pertinent lists and remove it from others."
9231   ;; Make the article expirable.
9232   (let ((mark (or mark gnus-del-mark)))
9233     (if (= mark gnus-expirable-mark)
9234         (push article gnus-newsgroup-expirable)
9235       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9236     ;; Remove from unread and marked lists.
9237     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9238     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9239     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9240     (push (cons article mark) gnus-newsgroup-reads)
9241     ;; Possibly remove from cache, if that is used.
9242     (when gnus-use-cache
9243       (gnus-cache-enter-remove-article article))
9244     t))
9245
9246 (defun gnus-mark-article-as-unread (article &optional mark)
9247   "Enter ARTICLE in the pertinent lists and remove it from others."
9248   (let ((mark (or mark gnus-ticked-mark)))
9249     (if (<= article 0)
9250         (progn
9251           (gnus-error 1 "Can't mark negative article numbers")
9252           nil)
9253       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9254             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9255             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9256             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9257
9258       ;; Unsuppress duplicates?
9259       (when gnus-suppress-duplicates
9260         (gnus-dup-unsuppress-article article))
9261
9262       (cond ((= mark gnus-ticked-mark)
9263              (push article gnus-newsgroup-marked))
9264             ((= mark gnus-dormant-mark)
9265              (push article gnus-newsgroup-dormant))
9266             (t
9267              (push article gnus-newsgroup-unreads)))
9268       (gnus-pull article gnus-newsgroup-reads)
9269       t)))
9270
9271 (defalias 'gnus-summary-mark-as-unread-forward
9272   'gnus-summary-tick-article-forward)
9273 (make-obsolete 'gnus-summary-mark-as-unread-forward
9274                'gnus-summary-tick-article-forward)
9275 (defun gnus-summary-tick-article-forward (n)
9276   "Tick N articles forwards.
9277 If N is negative, tick backwards instead.
9278 The difference between N and the number of articles ticked is returned."
9279   (interactive "p")
9280   (gnus-summary-mark-forward n gnus-ticked-mark))
9281
9282 (defalias 'gnus-summary-mark-as-unread-backward
9283   'gnus-summary-tick-article-backward)
9284 (make-obsolete 'gnus-summary-mark-as-unread-backward
9285                'gnus-summary-tick-article-backward)
9286 (defun gnus-summary-tick-article-backward (n)
9287   "Tick N articles backwards.
9288 The difference between N and the number of articles ticked is returned."
9289   (interactive "p")
9290   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9291
9292 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9293 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9294 (defun gnus-summary-tick-article (&optional article clear-mark)
9295   "Mark current article as unread.
9296 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9297 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9298   (interactive)
9299   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9300                                        gnus-ticked-mark)))
9301
9302 (defun gnus-summary-mark-as-read-forward (n)
9303   "Mark N articles as read forwards.
9304 If N is negative, mark backwards instead.
9305 The difference between N and the actual number of articles marked is
9306 returned."
9307   (interactive "p")
9308   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9309
9310 (defun gnus-summary-mark-as-read-backward (n)
9311   "Mark the N articles as read backwards.
9312 The difference between N and the actual number of articles marked is
9313 returned."
9314   (interactive "p")
9315   (gnus-summary-mark-forward
9316    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9317
9318 (defun gnus-summary-mark-as-read (&optional article mark)
9319   "Mark current article as read.
9320 ARTICLE specifies the article to be marked as read.
9321 MARK specifies a string to be inserted at the beginning of the line."
9322   (gnus-summary-mark-article article mark))
9323
9324 (defun gnus-summary-clear-mark-forward (n)
9325   "Clear marks from N articles forward.
9326 If N is negative, clear backward instead.
9327 The difference between N and the number of marks cleared is returned."
9328   (interactive "p")
9329   (gnus-summary-mark-forward n gnus-unread-mark))
9330
9331 (defun gnus-summary-clear-mark-backward (n)
9332   "Clear marks from N articles backward.
9333 The difference between N and the number of marks cleared is returned."
9334   (interactive "p")
9335   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9336
9337 (defun gnus-summary-mark-unread-as-read ()
9338   "Intended to be used by `gnus-summary-mark-article-hook'."
9339   (when (memq gnus-current-article gnus-newsgroup-unreads)
9340     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9341
9342 (defun gnus-summary-mark-read-and-unread-as-read ()
9343   "Intended to be used by `gnus-summary-mark-article-hook'."
9344   (let ((mark (gnus-summary-article-mark)))
9345     (when (or (gnus-unread-mark-p mark)
9346               (gnus-read-mark-p mark))
9347       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9348
9349 (defun gnus-summary-mark-unread-as-ticked ()
9350   "Intended to be used by `gnus-summary-mark-article-hook'."
9351   (when (memq gnus-current-article gnus-newsgroup-unreads)
9352     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9353
9354 (defun gnus-summary-mark-region-as-read (point mark all)
9355   "Mark all unread articles between point and mark as read.
9356 If given a prefix, mark all articles between point and mark as read,
9357 even ticked and dormant ones."
9358   (interactive "r\nP")
9359   (save-excursion
9360     (let (article)
9361       (goto-char point)
9362       (beginning-of-line)
9363       (while (and
9364               (< (point) mark)
9365               (progn
9366                 (when (or all
9367                           (memq (setq article (gnus-summary-article-number))
9368                                 gnus-newsgroup-unreads))
9369                   (gnus-summary-mark-article article gnus-del-mark))
9370                 t)
9371               (gnus-summary-find-next))))))
9372
9373 (defun gnus-summary-mark-below (score mark)
9374   "Mark articles with score less than SCORE with MARK."
9375   (interactive "P\ncMark: ")
9376   (setq score (if score
9377                   (prefix-numeric-value score)
9378                 (or gnus-summary-default-score 0)))
9379   (save-excursion
9380     (set-buffer gnus-summary-buffer)
9381     (goto-char (point-min))
9382     (while
9383         (progn
9384           (and (< (gnus-summary-article-score) score)
9385                (gnus-summary-mark-article nil mark))
9386           (gnus-summary-find-next)))))
9387
9388 (defun gnus-summary-kill-below (&optional score)
9389   "Mark articles with score below SCORE as read."
9390   (interactive "P")
9391   (gnus-summary-mark-below score gnus-killed-mark))
9392
9393 (defun gnus-summary-clear-above (&optional score)
9394   "Clear all marks from articles with score above SCORE."
9395   (interactive "P")
9396   (gnus-summary-mark-above score gnus-unread-mark))
9397
9398 (defun gnus-summary-tick-above (&optional score)
9399   "Tick all articles with score above SCORE."
9400   (interactive "P")
9401   (gnus-summary-mark-above score gnus-ticked-mark))
9402
9403 (defun gnus-summary-mark-above (score mark)
9404   "Mark articles with score over SCORE with MARK."
9405   (interactive "P\ncMark: ")
9406   (setq score (if score
9407                   (prefix-numeric-value score)
9408                 (or gnus-summary-default-score 0)))
9409   (save-excursion
9410     (set-buffer gnus-summary-buffer)
9411     (goto-char (point-min))
9412     (while (and (progn
9413                   (when (> (gnus-summary-article-score) score)
9414                     (gnus-summary-mark-article nil mark))
9415                   t)
9416                 (gnus-summary-find-next)))))
9417
9418 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9419 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9420 (defun gnus-summary-limit-include-expunged (&optional no-error)
9421   "Display all the hidden articles that were expunged for low scores."
9422   (interactive)
9423   (let ((buffer-read-only nil))
9424     (let ((scored gnus-newsgroup-scored)
9425           headers h)
9426       (while scored
9427         (unless (gnus-summary-article-header (caar scored))
9428           (and (setq h (gnus-number-to-header (caar scored)))
9429                (< (cdar scored) gnus-summary-expunge-below)
9430                (push h headers)))
9431         (setq scored (cdr scored)))
9432       (if (not headers)
9433           (when (not no-error)
9434             (error "No expunged articles hidden"))
9435         (goto-char (point-min))
9436         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9437         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9438         (mapcar (lambda (x) (push (mail-header-number x)
9439                                   gnus-newsgroup-limit))
9440                 headers)
9441         (gnus-summary-prepare-unthreaded (nreverse headers))
9442         (goto-char (point-min))
9443         (gnus-summary-position-point)
9444         t))))
9445
9446 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9447   "Mark all unread articles in this newsgroup as read.
9448 If prefix argument ALL is non-nil, ticked and dormant articles will
9449 also be marked as read.
9450 If QUIETLY is non-nil, no questions will be asked.
9451 If TO-HERE is non-nil, it should be a point in the buffer.  All
9452 articles before (after, if REVERSE is set) this point will be marked as read.
9453 Note that this function will only catch up the unread article
9454 in the current summary buffer limitation.
9455 The number of articles marked as read is returned."
9456   (interactive "P")
9457   (prog1
9458       (save-excursion
9459         (when (or quietly
9460                   (not gnus-interactive-catchup) ;Without confirmation?
9461                   gnus-expert-user
9462                   (gnus-y-or-n-p
9463                    (if all
9464                        "Mark absolutely all articles as read? "
9465                      "Mark all unread articles as read? ")))
9466           (if (and not-mark
9467                    (not gnus-newsgroup-adaptive)
9468                    (not gnus-newsgroup-auto-expire)
9469                    (not gnus-suppress-duplicates)
9470                    (or (not gnus-use-cache)
9471                        (eq gnus-use-cache 'passive)))
9472               (progn
9473                 (when all
9474                   (setq gnus-newsgroup-marked nil
9475                         gnus-newsgroup-dormant nil))
9476                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9477             ;; We actually mark all articles as canceled, which we
9478             ;; have to do when using auto-expiry or adaptive scoring.
9479             (gnus-summary-show-all-threads)
9480             (if (and to-here reverse)
9481                 (progn
9482                   (goto-char to-here)
9483                   (while (and
9484                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9485                           (gnus-summary-find-next (not all) nil nil t))))
9486               (when (gnus-summary-first-subject (not all) t)
9487                 (while (and
9488                         (if to-here (< (point) to-here) t)
9489                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9490                         (gnus-summary-find-next (not all) nil nil t)))))
9491             (gnus-set-mode-line 'summary))
9492           t))
9493     (gnus-summary-position-point)))
9494
9495 (defun gnus-summary-catchup-to-here (&optional all)
9496   "Mark all unticked articles before the current one as read.
9497 If ALL is non-nil, also mark ticked and dormant articles as read."
9498   (interactive "P")
9499   (save-excursion
9500     (gnus-save-hidden-threads
9501       (let ((beg (point)))
9502         ;; We check that there are unread articles.
9503         (when (or all (gnus-summary-find-prev))
9504           (gnus-summary-catchup all t beg)))))
9505   (gnus-summary-position-point))
9506
9507 (defun gnus-summary-catchup-from-here (&optional all)
9508   "Mark all unticked articles after the current one as read.
9509 If ALL is non-nil, also mark ticked and dormant articles as read."
9510   (interactive "P")
9511   (save-excursion
9512     (gnus-save-hidden-threads
9513       (let ((beg (point)))
9514         ;; We check that there are unread articles.
9515         (when (or all (gnus-summary-find-next))
9516           (gnus-summary-catchup all t beg nil t)))))
9517   (gnus-summary-position-point))
9518
9519 (defun gnus-summary-catchup-all (&optional quietly)
9520   "Mark all articles in this newsgroup as read."
9521   (interactive "P")
9522   (gnus-summary-catchup t quietly))
9523
9524 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9525   "Mark all unread articles in this group as read, then exit.
9526 If prefix argument ALL is non-nil, all articles are marked as read.
9527 If QUIETLY is non-nil, no questions will be asked."
9528   (interactive "P")
9529   (when (gnus-summary-catchup all quietly nil 'fast)
9530     ;; Select next newsgroup or exit.
9531     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9532              (eq gnus-auto-select-next 'quietly))
9533         (gnus-summary-next-group nil)
9534       (gnus-summary-exit))))
9535
9536 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9537   "Mark all articles in this newsgroup as read, and then exit."
9538   (interactive "P")
9539   (gnus-summary-catchup-and-exit t quietly))
9540
9541 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9542   "Mark all articles in this group as read and select the next group.
9543 If given a prefix, mark all articles, unread as well as ticked, as
9544 read."
9545   (interactive "P")
9546   (save-excursion
9547     (gnus-summary-catchup all))
9548   (gnus-summary-next-group))
9549
9550 ;;;
9551 ;;; with article
9552 ;;;
9553
9554 (defmacro gnus-with-article (article &rest forms)
9555   "Select ARTICLE and perform FORMS in the original article buffer.
9556 Then replace the article with the result."
9557   `(progn
9558      ;; We don't want the article to be marked as read.
9559      (let (gnus-mark-article-hook)
9560        (gnus-summary-select-article t t nil ,article))
9561      (set-buffer gnus-original-article-buffer)
9562      ,@forms
9563      (if (not (gnus-check-backend-function
9564                'request-replace-article (car gnus-article-current)))
9565          (gnus-message 5 "Read-only group; not replacing")
9566        (unless (gnus-request-replace-article
9567                 ,article (car gnus-article-current)
9568                 (current-buffer) t)
9569          (error "Couldn't replace article")))
9570      ;; The cache and backlog have to be flushed somewhat.
9571      (when gnus-keep-backlog
9572        (gnus-backlog-remove-article
9573         (car gnus-article-current) (cdr gnus-article-current)))
9574      (when gnus-use-cache
9575        (gnus-cache-update-article
9576         (car gnus-article-current) (cdr gnus-article-current)))))
9577
9578 (put 'gnus-with-article 'lisp-indent-function 1)
9579 (put 'gnus-with-article 'edebug-form-spec '(form body))
9580
9581 ;; Thread-based commands.
9582
9583 (defun gnus-summary-articles-in-thread (&optional article)
9584   "Return a list of all articles in the current thread.
9585 If ARTICLE is non-nil, return all articles in the thread that starts
9586 with that article."
9587   (let* ((article (or article (gnus-summary-article-number)))
9588          (data (gnus-data-find-list article))
9589          (top-level (gnus-data-level (car data)))
9590          (top-subject
9591           (cond ((null gnus-thread-operation-ignore-subject)
9592                  (gnus-simplify-subject-re
9593                   (mail-header-subject (gnus-data-header (car data)))))
9594                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9595                  (gnus-simplify-subject-fuzzy
9596                   (mail-header-subject (gnus-data-header (car data)))))
9597                 (t nil)))
9598          (end-point (save-excursion
9599                       (if (gnus-summary-go-to-next-thread)
9600                           (point) (point-max))))
9601          articles)
9602     (while (and data
9603                 (< (gnus-data-pos (car data)) end-point))
9604       (when (or (not top-subject)
9605                 (string= top-subject
9606                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9607                              (gnus-simplify-subject-fuzzy
9608                               (mail-header-subject
9609                                (gnus-data-header (car data))))
9610                            (gnus-simplify-subject-re
9611                             (mail-header-subject
9612                              (gnus-data-header (car data)))))))
9613         (push (gnus-data-number (car data)) articles))
9614       (unless (and (setq data (cdr data))
9615                    (> (gnus-data-level (car data)) top-level))
9616         (setq data nil)))
9617     ;; Return the list of articles.
9618     (nreverse articles)))
9619
9620 (defun gnus-summary-rethread-current ()
9621   "Rethread the thread the current article is part of."
9622   (interactive)
9623   (let* ((gnus-show-threads t)
9624          (article (gnus-summary-article-number))
9625          (id (mail-header-id (gnus-summary-article-header)))
9626          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9627     (unless id
9628       (error "No article on the current line"))
9629     (gnus-rebuild-thread id)
9630     (gnus-summary-goto-subject article)))
9631
9632 (defun gnus-summary-reparent-thread ()
9633   "Make the current article child of the marked (or previous) article.
9634
9635 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9636 is non-nil or the Subject: of both articles are the same."
9637   (interactive)
9638   (unless (not (gnus-group-read-only-p))
9639     (error "The current newsgroup does not support article editing"))
9640   (unless (<= (length gnus-newsgroup-processable) 1)
9641     (error "No more than one article may be marked"))
9642   (save-window-excursion
9643     (let ((gnus-article-buffer " *reparent*")
9644           (current-article (gnus-summary-article-number))
9645           ;; First grab the marked article, otherwise one line up.
9646           (parent-article (if (not (null gnus-newsgroup-processable))
9647                               (car gnus-newsgroup-processable)
9648                             (save-excursion
9649                               (if (eq (forward-line -1) 0)
9650                                   (gnus-summary-article-number)
9651                                 (error "Beginning of summary buffer"))))))
9652       (unless (not (eq current-article parent-article))
9653         (error "An article may not be self-referential"))
9654       (let ((message-id (mail-header-id
9655                          (gnus-summary-article-header parent-article))))
9656         (unless (and message-id (not (equal message-id "")))
9657           (error "No message-id in desired parent"))
9658         (gnus-with-article current-article
9659           (save-restriction
9660             (goto-char (point-min))
9661             (message-narrow-to-head)
9662             (if (re-search-forward "^References: " nil t)
9663                 (progn
9664                   (re-search-forward "^[^ \t]" nil t)
9665                   (forward-line -1)
9666                   (end-of-line)
9667                   (insert " " message-id))
9668               (insert "References: " message-id "\n"))))
9669         (set-buffer gnus-summary-buffer)
9670         (gnus-summary-unmark-all-processable)
9671         (gnus-summary-update-article current-article)
9672         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9673             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9674         (gnus-summary-rethread-current)
9675         (gnus-message 3 "Article %d is now the child of article %d"
9676                       current-article parent-article)))))
9677
9678 (defun gnus-summary-toggle-threads (&optional arg)
9679   "Toggle showing conversation threads.
9680 If ARG is positive number, turn showing conversation threads on."
9681   (interactive "P")
9682   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9683     (setq gnus-show-threads
9684           (if (null arg) (not gnus-show-threads)
9685             (> (prefix-numeric-value arg) 0)))
9686     (gnus-summary-prepare)
9687     (gnus-summary-goto-subject current)
9688     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9689     (gnus-summary-position-point)))
9690
9691 (defun gnus-summary-show-all-threads ()
9692   "Show all threads."
9693   (interactive)
9694   (save-excursion
9695     (let ((buffer-read-only nil))
9696       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9697   (gnus-summary-position-point))
9698
9699 (defun gnus-summary-show-thread ()
9700   "Show thread subtrees.
9701 Returns nil if no thread was there to be shown."
9702   (interactive)
9703   (let ((buffer-read-only nil)
9704         (orig (point))
9705         ;; first goto end then to beg, to have point at beg after let
9706         (end (progn (end-of-line) (point)))
9707         (beg (progn (beginning-of-line) (point))))
9708     (prog1
9709         ;; Any hidden lines here?
9710         (search-forward "\r" end t)
9711       (subst-char-in-region beg end ?\^M ?\n t)
9712       (goto-char orig)
9713       (gnus-summary-position-point))))
9714
9715 (defun gnus-summary-hide-all-threads ()
9716   "Hide all thread subtrees."
9717   (interactive)
9718   (save-excursion
9719     (goto-char (point-min))
9720     (gnus-summary-hide-thread)
9721     (while (zerop (gnus-summary-next-thread 1 t))
9722       (gnus-summary-hide-thread)))
9723   (gnus-summary-position-point))
9724
9725 (defun gnus-summary-hide-thread ()
9726   "Hide thread subtrees.
9727 Returns nil if no threads were there to be hidden."
9728   (interactive)
9729   (let ((buffer-read-only nil)
9730         (start (point))
9731         (article (gnus-summary-article-number)))
9732     (goto-char start)
9733     ;; Go forward until either the buffer ends or the subthread
9734     ;; ends.
9735     (when (and (not (eobp))
9736                (or (zerop (gnus-summary-next-thread 1 t))
9737                    (goto-char (point-max))))
9738       (prog1
9739           (if (and (> (point) start)
9740                    (search-backward "\n" start t))
9741               (progn
9742                 (subst-char-in-region start (point) ?\n ?\^M)
9743                 (gnus-summary-goto-subject article))
9744             (goto-char start)
9745             nil)))))
9746
9747 (defun gnus-summary-go-to-next-thread (&optional previous)
9748   "Go to the same level (or less) next thread.
9749 If PREVIOUS is non-nil, go to previous thread instead.
9750 Return the article number moved to, or nil if moving was impossible."
9751   (let ((level (gnus-summary-thread-level))
9752         (way (if previous -1 1))
9753         (beg (point)))
9754     (forward-line way)
9755     (while (and (not (eobp))
9756                 (< level (gnus-summary-thread-level)))
9757       (forward-line way))
9758     (if (eobp)
9759         (progn
9760           (goto-char beg)
9761           nil)
9762       (setq beg (point))
9763       (prog1
9764           (gnus-summary-article-number)
9765         (goto-char beg)))))
9766
9767 (defun gnus-summary-next-thread (n &optional silent)
9768   "Go to the same level next N'th thread.
9769 If N is negative, search backward instead.
9770 Returns the difference between N and the number of skips actually
9771 done.
9772
9773 If SILENT, don't output messages."
9774   (interactive "p")
9775   (let ((backward (< n 0))
9776         (n (abs n)))
9777     (while (and (> n 0)
9778                 (gnus-summary-go-to-next-thread backward))
9779       (decf n))
9780     (unless silent
9781       (gnus-summary-position-point))
9782     (when (and (not silent) (/= 0 n))
9783       (gnus-message 7 "No more threads"))
9784     n))
9785
9786 (defun gnus-summary-prev-thread (n)
9787   "Go to the same level previous N'th thread.
9788 Returns the difference between N and the number of skips actually
9789 done."
9790   (interactive "p")
9791   (gnus-summary-next-thread (- n)))
9792
9793 (defun gnus-summary-go-down-thread ()
9794   "Go down one level in the current thread."
9795   (let ((children (gnus-summary-article-children)))
9796     (when children
9797       (gnus-summary-goto-subject (car children)))))
9798
9799 (defun gnus-summary-go-up-thread ()
9800   "Go up one level in the current thread."
9801   (let ((parent (gnus-summary-article-parent)))
9802     (when parent
9803       (gnus-summary-goto-subject parent))))
9804
9805 (defun gnus-summary-down-thread (n)
9806   "Go down thread N steps.
9807 If N is negative, go up instead.
9808 Returns the difference between N and how many steps down that were
9809 taken."
9810   (interactive "p")
9811   (let ((up (< n 0))
9812         (n (abs n)))
9813     (while (and (> n 0)
9814                 (if up (gnus-summary-go-up-thread)
9815                   (gnus-summary-go-down-thread)))
9816       (setq n (1- n)))
9817     (gnus-summary-position-point)
9818     (when (/= 0 n)
9819       (gnus-message 7 "Can't go further"))
9820     n))
9821
9822 (defun gnus-summary-up-thread (n)
9823   "Go up thread N steps.
9824 If N is negative, go down instead.
9825 Returns the difference between N and how many steps down that were
9826 taken."
9827   (interactive "p")
9828   (gnus-summary-down-thread (- n)))
9829
9830 (defun gnus-summary-top-thread ()
9831   "Go to the top of the thread."
9832   (interactive)
9833   (while (gnus-summary-go-up-thread))
9834   (gnus-summary-article-number))
9835
9836 (defun gnus-summary-kill-thread (&optional unmark)
9837   "Mark articles under current thread as read.
9838 If the prefix argument is positive, remove any kinds of marks.
9839 If the prefix argument is negative, tick articles instead."
9840   (interactive "P")
9841   (when unmark
9842     (setq unmark (prefix-numeric-value unmark)))
9843   (let ((articles (gnus-summary-articles-in-thread)))
9844     (save-excursion
9845       ;; Expand the thread.
9846       (gnus-summary-show-thread)
9847       ;; Mark all the articles.
9848       (while articles
9849         (gnus-summary-goto-subject (car articles))
9850         (cond ((null unmark)
9851                (gnus-summary-mark-article-as-read gnus-killed-mark))
9852               ((> unmark 0)
9853                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9854               (t
9855                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9856         (setq articles (cdr articles))))
9857     ;; Hide killed subtrees.
9858     (and (null unmark)
9859          gnus-thread-hide-killed
9860          (gnus-summary-hide-thread))
9861     ;; If marked as read, go to next unread subject.
9862     (when (null unmark)
9863       ;; Go to next unread subject.
9864       (gnus-summary-next-subject 1 t)))
9865   (gnus-set-mode-line 'summary))
9866
9867 ;; Summary sorting commands
9868
9869 (defun gnus-summary-sort-by-number (&optional reverse)
9870   "Sort the summary buffer by article number.
9871 Argument REVERSE means reverse order."
9872   (interactive "P")
9873   (gnus-summary-sort 'number reverse))
9874
9875 (defun gnus-summary-sort-by-author (&optional reverse)
9876   "Sort the summary buffer by author name alphabetically.
9877 If `case-fold-search' is non-nil, case of letters is ignored.
9878 Argument REVERSE means reverse order."
9879   (interactive "P")
9880   (gnus-summary-sort 'author reverse))
9881
9882 (defun gnus-summary-sort-by-subject (&optional reverse)
9883   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9884 If `case-fold-search' is non-nil, case of letters is ignored.
9885 Argument REVERSE means reverse order."
9886   (interactive "P")
9887   (gnus-summary-sort 'subject reverse))
9888
9889 (defun gnus-summary-sort-by-date (&optional reverse)
9890   "Sort the summary buffer by date.
9891 Argument REVERSE means reverse order."
9892   (interactive "P")
9893   (gnus-summary-sort 'date reverse))
9894
9895 (defun gnus-summary-sort-by-score (&optional reverse)
9896   "Sort the summary buffer by score.
9897 Argument REVERSE means reverse order."
9898   (interactive "P")
9899   (gnus-summary-sort 'score reverse))
9900
9901 (defun gnus-summary-sort-by-lines (&optional reverse)
9902   "Sort the summary buffer by the number of lines.
9903 Argument REVERSE means reverse order."
9904   (interactive "P")
9905   (gnus-summary-sort 'lines reverse))
9906
9907 (defun gnus-summary-sort-by-chars (&optional reverse)
9908   "Sort the summary buffer by article length.
9909 Argument REVERSE means reverse order."
9910   (interactive "P")
9911   (gnus-summary-sort 'chars reverse))
9912
9913 (defun gnus-summary-sort-by-original (&optional reverse)
9914   "Sort the summary buffer using the default sorting method.
9915 Argument REVERSE means reverse order."
9916   (interactive "P")
9917   (let* ((buffer-read-only)
9918          (gnus-summary-prepare-hook nil))
9919     ;; We do the sorting by regenerating the threads.
9920     (gnus-summary-prepare)
9921     ;; Hide subthreads if needed.
9922     (when (and gnus-show-threads gnus-thread-hide-subtree)
9923       (gnus-summary-hide-all-threads))))
9924
9925 (defun gnus-summary-sort (predicate reverse)
9926   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9927   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9928          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9929          (gnus-thread-sort-functions
9930           (if (not reverse)
9931               thread
9932             `(lambda (t1 t2)
9933                (,thread t2 t1))))
9934          (gnus-sort-gathered-threads-function
9935           gnus-thread-sort-functions)
9936          (gnus-article-sort-functions
9937           (if (not reverse)
9938               article
9939             `(lambda (t1 t2)
9940                (,article t2 t1))))
9941          (buffer-read-only)
9942          (gnus-summary-prepare-hook nil))
9943     ;; We do the sorting by regenerating the threads.
9944     (gnus-summary-prepare)
9945     ;; Hide subthreads if needed.
9946     (when (and gnus-show-threads gnus-thread-hide-subtree)
9947       (gnus-summary-hide-all-threads))))
9948
9949 ;; Summary saving commands.
9950
9951 (defun gnus-summary-save-article (&optional n not-saved)
9952   "Save the current article using the default saver function.
9953 If N is a positive number, save the N next articles.
9954 If N is a negative number, save the N previous articles.
9955 If N is nil and any articles have been marked with the process mark,
9956 save those articles instead.
9957 The variable `gnus-default-article-saver' specifies the saver function."
9958   (interactive "P")
9959   (let* ((articles (gnus-summary-work-articles n))
9960          (save-buffer (save-excursion
9961                         (nnheader-set-temp-buffer " *Gnus Save*")))
9962          (num (length articles))
9963          header file)
9964     (dolist (article articles)
9965       (setq header (gnus-summary-article-header article))
9966       (if (not (vectorp header))
9967           ;; This is a pseudo-article.
9968           (if (assq 'name header)
9969               (gnus-copy-file (cdr (assq 'name header)))
9970             (gnus-message 1 "Article %d is unsaveable" article))
9971         ;; This is a real article.
9972         (save-window-excursion
9973           (let ((gnus-display-mime-function nil)
9974                 (gnus-article-prepare-hook nil))
9975             (gnus-summary-select-article t nil nil article)))
9976         (save-excursion
9977           (set-buffer save-buffer)
9978           (erase-buffer)
9979           (insert-buffer-substring gnus-original-article-buffer))
9980         (setq file (gnus-article-save save-buffer file num))
9981         (gnus-summary-remove-process-mark article)
9982         (unless not-saved
9983           (gnus-summary-set-saved-mark article))))
9984     (gnus-kill-buffer save-buffer)
9985     (gnus-summary-position-point)
9986     (gnus-set-mode-line 'summary)
9987     n))
9988
9989 (defun gnus-summary-pipe-output (&optional arg)
9990   "Pipe the current article to a subprocess.
9991 If N is a positive number, pipe the N next articles.
9992 If N is a negative number, pipe the N previous articles.
9993 If N is nil and any articles have been marked with the process mark,
9994 pipe those articles instead."
9995   (interactive "P")
9996   (require 'gnus-art)
9997   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9998     (gnus-summary-save-article arg t))
9999   (let ((buffer (get-buffer "*Shell Command Output*")))
10000     (if (and buffer
10001              (with-current-buffer buffer (> (point-max) (point-min))))
10002         (gnus-configure-windows 'pipe))))
10003
10004 (defun gnus-summary-save-article-mail (&optional arg)
10005   "Append the current article to an mail file.
10006 If N is a positive number, save the N next articles.
10007 If N is a negative number, save the N previous articles.
10008 If N is nil and any articles have been marked with the process mark,
10009 save those articles instead."
10010   (interactive "P")
10011   (require 'gnus-art)
10012   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10013     (gnus-summary-save-article arg)))
10014
10015 (defun gnus-summary-save-article-rmail (&optional arg)
10016   "Append the current article to an rmail file.
10017 If N is a positive number, save the N next articles.
10018 If N is a negative number, save the N previous articles.
10019 If N is nil and any articles have been marked with the process mark,
10020 save those articles instead."
10021   (interactive "P")
10022   (require 'gnus-art)
10023   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10024     (gnus-summary-save-article arg)))
10025
10026 (defun gnus-summary-save-article-file (&optional arg)
10027   "Append the current article to a file.
10028 If N is a positive number, save the N next articles.
10029 If N is a negative number, save the N previous articles.
10030 If N is nil and any articles have been marked with the process mark,
10031 save those articles instead."
10032   (interactive "P")
10033   (require 'gnus-art)
10034   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10035     (gnus-summary-save-article arg)))
10036
10037 (defun gnus-summary-write-article-file (&optional arg)
10038   "Write the current article to a file, deleting the previous file.
10039 If N is a positive number, save the N next articles.
10040 If N is a negative number, save the N previous articles.
10041 If N is nil and any articles have been marked with the process mark,
10042 save those articles instead."
10043   (interactive "P")
10044   (require 'gnus-art)
10045   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10046     (gnus-summary-save-article arg)))
10047
10048 (defun gnus-summary-save-article-body-file (&optional arg)
10049   "Append the current article body to a file.
10050 If N is a positive number, save the N next articles.
10051 If N is a negative number, save the N previous articles.
10052 If N is nil and any articles have been marked with the process mark,
10053 save those articles instead."
10054   (interactive "P")
10055   (require 'gnus-art)
10056   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10057     (gnus-summary-save-article arg)))
10058
10059 (defun gnus-summary-pipe-message (program)
10060   "Pipe the current article through PROGRAM."
10061   (interactive "sProgram: ")
10062   (gnus-summary-select-article)
10063   (let ((mail-header-separator ""))
10064     (gnus-eval-in-buffer-window gnus-article-buffer
10065       (save-restriction
10066         (widen)
10067         (let ((start (window-start))
10068               buffer-read-only)
10069           (message-pipe-buffer-body program)
10070           (set-window-start (get-buffer-window (current-buffer)) start))))))
10071
10072 (defun gnus-get-split-value (methods)
10073   "Return a value based on the split METHODS."
10074   (let (split-name method result match)
10075     (when methods
10076       (save-excursion
10077         (set-buffer gnus-original-article-buffer)
10078         (save-restriction
10079           (nnheader-narrow-to-headers)
10080           (while (and methods (not split-name))
10081             (goto-char (point-min))
10082             (setq method (pop methods))
10083             (setq match (car method))
10084             (when (cond
10085                    ((stringp match)
10086                     ;; Regular expression.
10087                     (ignore-errors
10088                       (re-search-forward match nil t)))
10089                    ((gnus-functionp match)
10090                     ;; Function.
10091                     (save-restriction
10092                       (widen)
10093                       (setq result (funcall match gnus-newsgroup-name))))
10094                    ((consp match)
10095                     ;; Form.
10096                     (save-restriction
10097                       (widen)
10098                       (setq result (eval match)))))
10099               (setq split-name (cdr method))
10100               (cond ((stringp result)
10101                      (push (expand-file-name
10102                             result gnus-article-save-directory)
10103                            split-name))
10104                     ((consp result)
10105                      (setq split-name (append result split-name)))))))))
10106     (nreverse split-name)))
10107
10108 (defun gnus-valid-move-group-p (group)
10109   (and (boundp group)
10110        (symbol-name group)
10111        (symbol-value group)
10112        (gnus-get-function (gnus-find-method-for-group
10113                            (symbol-name group)) 'request-accept-article t)))
10114
10115 (defun gnus-read-move-group-name (prompt default articles prefix)
10116   "Read a group name."
10117   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10118          (minibuffer-confirm-incomplete nil) ; XEmacs
10119          (prom
10120           (format "%s %s to:"
10121                   prompt
10122                   (if (> (length articles) 1)
10123                       (format "these %d articles" (length articles))
10124                     "this article")))
10125          (to-newsgroup
10126           (cond
10127            ((null split-name)
10128             (gnus-completing-read default prom
10129                                   gnus-active-hashtb
10130                                   'gnus-valid-move-group-p
10131                                   nil prefix
10132                                   'gnus-group-history))
10133            ((= 1 (length split-name))
10134             (gnus-completing-read (car split-name) prom
10135                                   gnus-active-hashtb
10136                                   'gnus-valid-move-group-p
10137                                   nil nil
10138                                   'gnus-group-history))
10139            (t
10140             (gnus-completing-read nil prom
10141                                   (mapcar (lambda (el) (list el))
10142                                           (nreverse split-name))
10143                                   nil nil nil
10144                                   'gnus-group-history))))
10145          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10146     (when to-newsgroup
10147       (if (or (string= to-newsgroup "")
10148               (string= to-newsgroup prefix))
10149           (setq to-newsgroup default))
10150       (unless to-newsgroup
10151         (error "No group name entered"))
10152       (or (gnus-active to-newsgroup)
10153           (gnus-activate-group to-newsgroup nil nil to-method)
10154           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10155                                      to-newsgroup))
10156               (or (and (gnus-request-create-group to-newsgroup to-method)
10157                        (gnus-activate-group
10158                         to-newsgroup nil nil to-method)
10159                        (gnus-subscribe-group to-newsgroup))
10160                   (error "Couldn't create group %s" to-newsgroup)))
10161           (error "No such group: %s" to-newsgroup)))
10162     to-newsgroup))
10163
10164 (defun gnus-summary-save-parts (type dir n &optional reverse)
10165   "Save parts matching TYPE to DIR.
10166 If REVERSE, save parts that do not match TYPE."
10167   (interactive
10168    (list (read-string "Save parts of type: "
10169                       (or (car gnus-summary-save-parts-type-history)
10170                           gnus-summary-save-parts-default-mime)
10171                       'gnus-summary-save-parts-type-history)
10172          (setq gnus-summary-save-parts-last-directory
10173                (read-file-name "Save to directory: "
10174                                gnus-summary-save-parts-last-directory
10175                                nil t))
10176          current-prefix-arg))
10177   (gnus-summary-iterate n
10178     (let ((gnus-display-mime-function nil)
10179           (gnus-inhibit-treatment t))
10180       (gnus-summary-select-article))
10181     (save-excursion
10182       (set-buffer gnus-article-buffer)
10183       (let ((handles (or gnus-article-mime-handles
10184                          (mm-dissect-buffer) (mm-uu-dissect))))
10185         (when handles
10186           (gnus-summary-save-parts-1 type dir handles reverse)
10187           (unless gnus-article-mime-handles ;; Don't destroy this case.
10188             (mm-destroy-parts handles)))))))
10189
10190 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10191   (if (stringp (car handle))
10192       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10193               (cdr handle))
10194     (when (if reverse
10195               (not (string-match type (mm-handle-media-type handle)))
10196             (string-match type (mm-handle-media-type handle)))
10197       (let ((file (expand-file-name
10198                    (file-name-nondirectory
10199                     (or
10200                      (mail-content-type-get
10201                       (mm-handle-disposition handle) 'filename)
10202                      (concat gnus-newsgroup-name
10203                              "." (number-to-string
10204                                   (cdr gnus-article-current)))))
10205                    dir)))
10206         (unless (file-exists-p file)
10207           (mm-save-part-to-file handle file))))))
10208
10209 ;; Summary extract commands
10210
10211 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10212   (let ((buffer-read-only nil)
10213         (article (gnus-summary-article-number))
10214         after-article b e)
10215     (unless (gnus-summary-goto-subject article)
10216       (error "No such article: %d" article))
10217     (gnus-summary-position-point)
10218     ;; If all commands are to be bunched up on one line, we collect
10219     ;; them here.
10220     (unless gnus-view-pseudos-separately
10221       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10222             files action)
10223         (while ps
10224           (setq action (cdr (assq 'action (car ps))))
10225           (setq files (list (cdr (assq 'name (car ps)))))
10226           (while (and ps (cdr ps)
10227                       (string= (or action "1")
10228                                (or (cdr (assq 'action (cadr ps))) "2")))
10229             (push (cdr (assq 'name (cadr ps))) files)
10230             (setcdr ps (cddr ps)))
10231           (when files
10232             (when (not (string-match "%s" action))
10233               (push " " files))
10234             (push " " files)
10235             (when (assq 'execute (car ps))
10236               (setcdr (assq 'execute (car ps))
10237                       (funcall (if (string-match "%s" action)
10238                                    'format 'concat)
10239                                action
10240                                (mapconcat
10241                                 (lambda (f)
10242                                   (if (equal f " ")
10243                                       f
10244                                     (gnus-quote-arg-for-sh-or-csh f)))
10245                                 files " ")))))
10246           (setq ps (cdr ps)))))
10247     (if (and gnus-view-pseudos (not not-view))
10248         (while pslist
10249           (when (assq 'execute (car pslist))
10250             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10251                                   (eq gnus-view-pseudos 'not-confirm)))
10252           (setq pslist (cdr pslist)))
10253       (save-excursion
10254         (while pslist
10255           (setq after-article (or (cdr (assq 'article (car pslist)))
10256                                   (gnus-summary-article-number)))
10257           (gnus-summary-goto-subject after-article)
10258           (forward-line 1)
10259           (setq b (point))
10260           (insert "    " (file-name-nondirectory
10261                           (cdr (assq 'name (car pslist))))
10262                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10263           (setq e (point))
10264           (forward-line -1)             ; back to `b'
10265           (gnus-add-text-properties
10266            b (1- e) (list 'gnus-number gnus-reffed-article-number
10267                           gnus-mouse-face-prop gnus-mouse-face))
10268           (gnus-data-enter
10269            after-article gnus-reffed-article-number
10270            gnus-unread-mark b (car pslist) 0 (- e b))
10271           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10272           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10273           (setq pslist (cdr pslist)))))))
10274
10275 (defun gnus-pseudos< (p1 p2)
10276   (let ((c1 (cdr (assq 'action p1)))
10277         (c2 (cdr (assq 'action p2))))
10278     (and c1 c2 (string< c1 c2))))
10279
10280 (defun gnus-request-pseudo-article (props)
10281   (cond ((assq 'execute props)
10282          (gnus-execute-command (cdr (assq 'execute props)))))
10283   (let ((gnus-current-article (gnus-summary-article-number)))
10284     (gnus-run-hooks 'gnus-mark-article-hook)))
10285
10286 (defun gnus-execute-command (command &optional automatic)
10287   (save-excursion
10288     (gnus-article-setup-buffer)
10289     (set-buffer gnus-article-buffer)
10290     (setq buffer-read-only nil)
10291     (let ((command (if automatic command
10292                      (read-string "Command: " (cons command 0)))))
10293       (erase-buffer)
10294       (insert "$ " command "\n\n")
10295       (if gnus-view-pseudo-asynchronously
10296           (start-process "gnus-execute" (current-buffer) shell-file-name
10297                          shell-command-switch command)
10298         (call-process shell-file-name nil t nil
10299                       shell-command-switch command)))))
10300
10301 ;; Summary kill commands.
10302
10303 (defun gnus-summary-edit-global-kill (article)
10304   "Edit the \"global\" kill file."
10305   (interactive (list (gnus-summary-article-number)))
10306   (gnus-group-edit-global-kill article))
10307
10308 (defun gnus-summary-edit-local-kill ()
10309   "Edit a local kill file applied to the current newsgroup."
10310   (interactive)
10311   (setq gnus-current-headers (gnus-summary-article-header))
10312   (gnus-group-edit-local-kill
10313    (gnus-summary-article-number) gnus-newsgroup-name))
10314
10315 ;;; Header reading.
10316
10317 (defun gnus-read-header (id &optional header)
10318   "Read the headers of article ID and enter them into the Gnus system."
10319   (let ((group gnus-newsgroup-name)
10320         (gnus-override-method
10321          (or
10322           gnus-override-method
10323           (and (gnus-news-group-p gnus-newsgroup-name)
10324                (car (gnus-refer-article-methods)))))
10325         where)
10326     ;; First we check to see whether the header in question is already
10327     ;; fetched.
10328     (if (stringp id)
10329         ;; This is a Message-ID.
10330         (setq header (or header (gnus-id-to-header id)))
10331       ;; This is an article number.
10332       (setq header (or header (gnus-summary-article-header id))))
10333     (if (and header
10334              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10335         ;; We have found the header.
10336         header
10337       ;; If this is a sparse article, we have to nix out its
10338       ;; previous entry in the thread hashtb.
10339       (when (and header
10340                  (gnus-summary-article-sparse-p (mail-header-number header)))
10341         (let* ((parent (gnus-parent-id (mail-header-references header)))
10342                (thread (and parent (gnus-id-to-thread parent))))
10343           (when thread
10344             (delq (assq header thread) thread))))
10345       ;; We have to really fetch the header to this article.
10346       (save-excursion
10347         (set-buffer nntp-server-buffer)
10348         (when (setq where (gnus-request-head id group))
10349           (nnheader-fold-continuation-lines)
10350           (goto-char (point-max))
10351           (insert ".\n")
10352           (goto-char (point-min))
10353           (insert "211 ")
10354           (princ (cond
10355                   ((numberp id) id)
10356                   ((cdr where) (cdr where))
10357                   (header (mail-header-number header))
10358                   (t gnus-reffed-article-number))
10359                  (current-buffer))
10360           (insert " Article retrieved.\n"))
10361         (if (or (not where)
10362                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10363             ()                          ; Malformed head.
10364           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10365             (when (and (stringp id)
10366                        (not (string= (gnus-group-real-name group)
10367                                      (car where))))
10368               ;; If we fetched by Message-ID and the article came
10369               ;; from a different group, we fudge some bogus article
10370               ;; numbers for this article.
10371               (mail-header-set-number header gnus-reffed-article-number))
10372             (save-excursion
10373               (set-buffer gnus-summary-buffer)
10374               (decf gnus-reffed-article-number)
10375               (gnus-remove-header (mail-header-number header))
10376               (push header gnus-newsgroup-headers)
10377               (setq gnus-current-headers header)
10378               (push (mail-header-number header) gnus-newsgroup-limit)))
10379           header)))))
10380
10381 (defun gnus-remove-header (number)
10382   "Remove header NUMBER from `gnus-newsgroup-headers'."
10383   (if (and gnus-newsgroup-headers
10384            (= number (mail-header-number (car gnus-newsgroup-headers))))
10385       (pop gnus-newsgroup-headers)
10386     (let ((headers gnus-newsgroup-headers))
10387       (while (and (cdr headers)
10388                   (not (= number (mail-header-number (cadr headers)))))
10389         (pop headers))
10390       (when (cdr headers)
10391         (setcdr headers (cddr headers))))))
10392
10393 ;;;
10394 ;;; summary highlights
10395 ;;;
10396
10397 (defun gnus-highlight-selected-summary ()
10398   "Highlight selected article in summary buffer."
10399   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10400   (when gnus-summary-selected-face
10401     (save-excursion
10402       (let* ((beg (progn (beginning-of-line) (point)))
10403              (end (progn (end-of-line) (point)))
10404              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10405              (from (if (get-text-property beg gnus-mouse-face-prop)
10406                        beg
10407                      (or (next-single-property-change
10408                           beg gnus-mouse-face-prop nil end)
10409                          beg)))
10410              (to
10411               (if (= from end)
10412                   (- from 2)
10413                 (or (next-single-property-change
10414                      from gnus-mouse-face-prop nil end)
10415                     end))))
10416         ;; If no mouse-face prop on line we will have to = from = end,
10417         ;; so we highlight the entire line instead.
10418         (when (= (+ to 2) from)
10419           (setq from beg)
10420           (setq to end))
10421         (if gnus-newsgroup-selected-overlay
10422             ;; Move old overlay.
10423             (gnus-move-overlay
10424              gnus-newsgroup-selected-overlay from to (current-buffer))
10425           ;; Create new overlay.
10426           (gnus-overlay-put
10427            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10428            'face gnus-summary-selected-face))))))
10429
10430 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10431 (defun gnus-summary-highlight-line ()
10432   "Highlight current line according to `gnus-summary-highlight'."
10433   (let* ((list gnus-summary-highlight)
10434          (p (point))
10435          (end (progn (end-of-line) (point)))
10436          ;; now find out where the line starts and leave point there.
10437          (beg (progn (beginning-of-line) (point)))
10438          (article (gnus-summary-article-number))
10439          (score (or (cdr (assq (or article gnus-current-article)
10440                                gnus-newsgroup-scored))
10441                     gnus-summary-default-score 0))
10442          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10443          (inhibit-read-only t))
10444     ;; Eval the cars of the lists until we find a match.
10445     (let ((default gnus-summary-default-score)
10446           (default-high gnus-summary-default-high-score)
10447           (default-low gnus-summary-default-low-score))
10448       (while (and list
10449                   (not (eval (caar list))))
10450         (setq list (cdr list))))
10451     (let ((face (cdar list)))
10452       (unless (eq face (get-text-property beg 'face))
10453         (gnus-put-text-property-excluding-characters-with-faces
10454          beg end 'face
10455          (setq face (if (boundp face) (symbol-value face) face)))
10456         (when gnus-summary-highlight-line-function
10457           (funcall gnus-summary-highlight-line-function article face))))
10458     (goto-char p)))
10459
10460 (defun gnus-update-read-articles (group unread &optional compute)
10461   "Update the list of read articles in GROUP."
10462   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10463          (entry (gnus-gethash group gnus-newsrc-hashtb))
10464          (info (nth 2 entry))
10465          (prev 1)
10466          (unread (sort (copy-sequence unread) '<))
10467          read)
10468     (if (or (not info) (not active))
10469         ;; There is no info on this group if it was, in fact,
10470         ;; killed.  Gnus stores no information on killed groups, so
10471         ;; there's nothing to be done.
10472         ;; One could store the information somewhere temporarily,
10473         ;; perhaps...  Hmmm...
10474         ()
10475       ;; Remove any negative articles numbers.
10476       (while (and unread (< (car unread) 0))
10477         (setq unread (cdr unread)))
10478       ;; Remove any expired article numbers
10479       (while (and unread (< (car unread) (car active)))
10480         (setq unread (cdr unread)))
10481       ;; Compute the ranges of read articles by looking at the list of
10482       ;; unread articles.
10483       (while unread
10484         (when (/= (car unread) prev)
10485           (push (if (= prev (1- (car unread))) prev
10486                   (cons prev (1- (car unread))))
10487                 read))
10488         (setq prev (1+ (car unread)))
10489         (setq unread (cdr unread)))
10490       (when (<= prev (cdr active))
10491         (push (cons prev (cdr active)) read))
10492       (setq read (if (> (length read) 1) (nreverse read) read))
10493       (if compute
10494           read
10495         (save-excursion
10496           (let (setmarkundo)
10497             ;; Propagate the read marks to the backend.
10498             (when (gnus-check-backend-function 'request-set-mark group)
10499               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10500                     (add (gnus-remove-from-range read (gnus-info-read info))))
10501                 (when (or add del)
10502                   (unless (gnus-check-group group)
10503                     (error "Can't open server for %s" group))
10504                   (gnus-request-set-mark
10505                    group (delq nil (list (if add (list add 'add '(read)))
10506                                          (if del (list del 'del '(read))))))
10507                   (setq setmarkundo
10508                         `(gnus-request-set-mark
10509                           ,group
10510                           ',(delq nil (list
10511                                        (if del (list del 'add '(read)))
10512                                        (if add (list add 'del '(read))))))))))
10513             (set-buffer gnus-group-buffer)
10514             (gnus-undo-register
10515               `(progn
10516                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10517                  (gnus-info-set-read ',info ',(gnus-info-read info))
10518                  (gnus-get-unread-articles-in-group ',info
10519                                                     (gnus-active ,group))
10520                  (gnus-group-update-group ,group t)
10521                  ,setmarkundo))))
10522         ;; Enter this list into the group info.
10523         (gnus-info-set-read info read)
10524         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10525         (gnus-get-unread-articles-in-group info (gnus-active group))
10526         t))))
10527
10528 (defun gnus-offer-save-summaries ()
10529   "Offer to save all active summary buffers."
10530   (let (buffers)
10531     ;; Go through all buffers and find all summaries.
10532     (dolist (buffer (buffer-list))
10533       (when (and (setq buffer (buffer-name buffer))
10534                  (string-match "Summary" buffer)
10535                  (save-excursion
10536                    (set-buffer buffer)
10537                    ;; We check that this is, indeed, a summary buffer.
10538                    (and (eq major-mode 'gnus-summary-mode)
10539                         ;; Also make sure this isn't bogus.
10540                         gnus-newsgroup-prepared
10541                         ;; Also make sure that this isn't a
10542                         ;; dead summary buffer.
10543                         (not gnus-dead-summary-mode))))
10544         (push buffer buffers)))
10545     ;; Go through all these summary buffers and offer to save them.
10546     (when buffers
10547       (save-excursion
10548         (map-y-or-n-p
10549          "Update summary buffer %s? "
10550          (lambda (buf)
10551            (switch-to-buffer buf)
10552            (gnus-summary-exit))
10553          buffers)))))
10554
10555
10556 ;;; @ for mime-partial
10557 ;;;
10558
10559 (defun gnus-request-partial-message ()
10560   (save-excursion
10561     (let ((number (gnus-summary-article-number))
10562           (group gnus-newsgroup-name)
10563           (mother gnus-article-buffer))
10564       (set-buffer (get-buffer-create " *Partial Article*"))
10565       (erase-buffer)
10566       (setq mime-preview-buffer mother)
10567       (gnus-request-article-this-buffer number group)
10568       (mime-parse-buffer)
10569       )))
10570
10571 (autoload 'mime-combine-message/partial-pieces-automatically
10572   "mime-partial"
10573   "Internal method to combine message/partial messages automatically.")
10574
10575 (mime-add-condition
10576  'action '((type . message)(subtype . partial)
10577            (major-mode . gnus-original-article-mode)
10578            (method . mime-combine-message/partial-pieces-automatically)
10579            (summary-buffer-exp . gnus-summary-buffer)
10580            (request-partial-message-method . gnus-request-partial-message)
10581            ))
10582
10583
10584 ;;; @ for message/rfc822
10585 ;;;
10586
10587 (defun gnus-mime-extract-message/rfc822 (entity situation)
10588   (let (group article num cwin swin cur)
10589     (with-temp-buffer
10590       (mime-insert-entity-content entity)
10591       (setq group (or (cdr (assq 'group situation))
10592                       (completing-read "Group: "
10593                                        gnus-active-hashtb
10594                                        nil
10595                                        (gnus-read-active-file-p)
10596                                        gnus-newsgroup-name))
10597             article (gnus-request-accept-article group)))
10598     (when (and (consp article)
10599                (numberp (setq article (cdr article))))
10600       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10601             cwin (get-buffer-window (current-buffer) t))
10602       (save-window-excursion
10603         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10604             (select-window swin)
10605           (set-buffer gnus-summary-buffer))
10606         (setq cur gnus-current-article)
10607         (forward-line num)
10608         (let (gnus-show-threads)
10609           (gnus-summary-goto-subject article t))
10610         (gnus-summary-clear-mark-forward 1)
10611         (gnus-summary-goto-subject cur))
10612       (when (and cwin (window-frame cwin))
10613         (select-frame (window-frame cwin)))
10614       (when (boundp 'mime-acting-situation-to-override)
10615         (set-alist 'mime-acting-situation-to-override
10616                    'group
10617                    group)
10618         (set-alist 'mime-acting-situation-to-override
10619                    'after-method
10620                    `(progn
10621                       (save-current-buffer
10622                         (set-buffer gnus-group-buffer)
10623                         (gnus-activate-group ,group))
10624                       (gnus-summary-goto-article ,cur
10625                                                  gnus-show-all-headers)))
10626         (set-alist 'mime-acting-situation-to-override
10627                    'number num)))))
10628
10629 (mime-add-condition
10630  'action '((type . message)(subtype . rfc822)
10631            (major-mode . gnus-original-article-mode)
10632            (method . gnus-mime-extract-message/rfc822)
10633            (mode . "extract")
10634            ))
10635
10636 (mime-add-condition
10637  'action '((type . message)(subtype . news)
10638            (major-mode . gnus-original-article-mode)
10639            (method . gnus-mime-extract-message/rfc822)
10640            (mode . "extract")
10641            ))
10642
10643 (defun gnus-mime-extract-multipart (entity situation)
10644   (let ((children (mime-entity-children entity))
10645         mime-acting-situation-to-override
10646         f)
10647     (while children
10648       (mime-play-entity (car children)
10649                         (cons (assq 'mode situation)
10650                               mime-acting-situation-to-override))
10651       (setq children (cdr children)))
10652     (if (setq f (cdr (assq 'after-method
10653                            mime-acting-situation-to-override)))
10654         (eval f)
10655       )))
10656
10657 (mime-add-condition
10658  'action '((type . multipart)
10659            (method . gnus-mime-extract-multipart)
10660            (mode . "extract")
10661            )
10662  'with-default)
10663
10664
10665 ;;; @ end
10666 ;;;
10667
10668 (defun gnus-summary-setup-default-charset ()
10669   "Setup newsgroup default charset."
10670   (if (equal gnus-newsgroup-name "nndraft:drafts")
10671       (setq gnus-newsgroup-charset nil)
10672     (let* ((ignored-charsets
10673             (or gnus-newsgroup-ephemeral-ignored-charsets
10674                 (append
10675                  (and gnus-newsgroup-name
10676                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10677                  gnus-newsgroup-ignored-charsets))))
10678       (setq gnus-newsgroup-charset
10679             (or gnus-newsgroup-ephemeral-charset
10680                 (and gnus-newsgroup-name
10681                      (gnus-parameter-charset gnus-newsgroup-name))
10682                 gnus-default-charset))
10683       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10684            ignored-charsets))))
10685
10686 ;;;
10687 ;;; Mime Commands
10688 ;;;
10689
10690 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10691   "Display the current article buffer fully MIME-buttonized.
10692 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10693 treated as multipart/mixed."
10694   (interactive "P")
10695   (require 'gnus-art)
10696   (let ((gnus-unbuttonized-mime-types nil)
10697         (gnus-mime-display-multipart-as-mixed show-all-parts))
10698     (gnus-summary-show-article)))
10699
10700 (defun gnus-summary-repair-multipart (article)
10701   "Add a Content-Type header to a multipart article without one."
10702   (interactive (list (gnus-summary-article-number)))
10703   (gnus-with-article article
10704     (message-narrow-to-head)
10705     (message-remove-header "Mime-Version")
10706     (goto-char (point-max))
10707     (insert "Mime-Version: 1.0\n")
10708     (widen)
10709     (when (search-forward "\n--" nil t)
10710       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10711         (message-narrow-to-head)
10712         (message-remove-header "Content-Type")
10713         (goto-char (point-max))
10714         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10715                         separator))
10716         (widen))))
10717   (let (gnus-mark-article-hook)
10718     (gnus-summary-select-article t t nil article)))
10719
10720 (defun gnus-summary-toggle-display-buttonized ()
10721   "Toggle the buttonizing of the article buffer."
10722   (interactive)
10723   (require 'gnus-art)
10724   (if (setq gnus-inhibit-mime-unbuttonizing
10725             (not gnus-inhibit-mime-unbuttonizing))
10726       (let ((gnus-unbuttonized-mime-types nil))
10727         (gnus-summary-show-article))
10728     (gnus-summary-show-article)))
10729
10730 ;;;
10731 ;;; Intelli-mouse commmands
10732 ;;;
10733
10734 (defun gnus-wheel-summary-scroll (event)
10735   (interactive "e")
10736   (let ((amount (if (memq 'shift (event-modifiers event))
10737                     (car gnus-wheel-scroll-amount)
10738                   (cdr gnus-wheel-scroll-amount)))
10739         (direction (- (* (static-if (featurep 'xemacs)
10740                              (event-button event)
10741                            (cond ((eq 'mouse-4 (event-basic-type event))
10742                                   4)
10743                                  ((eq 'mouse-5 (event-basic-type event))
10744                                   5)))
10745                          2) 9))
10746         edge)
10747     (gnus-summary-scroll-up (* amount direction))
10748     (when (gnus-eval-in-buffer-window gnus-article-buffer
10749             (save-restriction
10750               (widen)
10751               (and (if (< 0 direction)
10752                        (gnus-article-next-page 0)
10753                      (gnus-article-prev-page 0)
10754                      (bobp))
10755                    (if (setq edge (get-text-property
10756                                    (point-min) 'gnus-wheel-edge))
10757                        (setq edge (* edge direction))
10758                      (setq edge -1))
10759                    (or (plusp edge)
10760                        (let ((buffer-read-only nil)
10761                              (inhibit-read-only t))
10762                          (put-text-property (point-min) (point-max)
10763                                             'gnus-wheel-edge direction)
10764                          nil))
10765                    (or (> edge gnus-wheel-edge-resistance)
10766                        (let ((buffer-read-only nil)
10767                              (inhibit-read-only t))
10768                          (put-text-property (point-min) (point-max)
10769                                             'gnus-wheel-edge
10770                                             (* (1+ edge) direction))
10771                          nil))
10772                    (eq last-command 'gnus-wheel-summary-scroll))))
10773       (gnus-summary-next-article nil nil (minusp direction)))))
10774
10775 (defun gnus-wheel-install ()
10776   "Enable mouse wheel support on summary window."
10777   (when gnus-use-wheel
10778     (let ((keys
10779            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10780       (dolist (key keys)
10781         (define-key gnus-summary-mode-map key
10782           'gnus-wheel-summary-scroll)))))
10783
10784 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10785
10786 ;;;
10787 ;;; Traditional PGP commmands
10788 ;;;
10789
10790 (defun gnus-summary-decrypt-article (&optional force)
10791   "Decrypt the current article in traditional PGP way.
10792 This will have permanent effect only in mail groups.
10793 If FORCE is non-nil, allow editing of articles even in read-only
10794 groups."
10795   (interactive "P")
10796   (gnus-summary-select-article t)
10797   (gnus-eval-in-buffer-window gnus-article-buffer
10798     (save-excursion
10799       (save-restriction
10800         (widen)
10801         (goto-char (point-min))
10802         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10803           (error "Not a traditional PGP message!"))
10804         (let ((armor-start (match-beginning 0)))
10805           (if (and (pgg-decrypt-region armor-start (point-max))
10806                    (or force (not (gnus-group-read-only-p))))
10807               (let ((inhibit-read-only t)
10808                     buffer-read-only)
10809                 (delete-region armor-start
10810                                (progn
10811                                  (re-search-forward "^-+END PGP" nil t)
10812                                  (beginning-of-line 2)
10813                                  (point)))
10814                 (insert-buffer-substring pgg-output-buffer))))))))
10815
10816 (defun gnus-summary-verify-article ()
10817   "Verify the current article in traditional PGP way."
10818   (interactive)
10819   (save-excursion
10820     (set-buffer gnus-original-article-buffer)
10821     (goto-char (point-min))
10822     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10823       (error "Not a traditional PGP message!"))
10824     (re-search-forward "^-+END PGP" nil t)
10825     (beginning-of-line 2)
10826     (call-interactively (function pgg-verify-region))))
10827
10828 ;;;
10829 ;;; Generic summary marking commands
10830 ;;;
10831
10832 (defvar gnus-summary-marking-alist
10833   '((read gnus-del-mark "d")
10834     (unread gnus-unread-mark "u")
10835     (ticked gnus-ticked-mark "!")
10836     (dormant gnus-dormant-mark "?")
10837     (expirable gnus-expirable-mark "e"))
10838   "An alist of names/marks/keystrokes.")
10839
10840 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10841 (defvar gnus-summary-mark-map)
10842
10843 (defun gnus-summary-make-all-marking-commands ()
10844   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10845   (dolist (elem gnus-summary-marking-alist)
10846     (apply 'gnus-summary-make-marking-command elem)))
10847
10848 (defun gnus-summary-make-marking-command (name mark keystroke)
10849   (let ((map (make-sparse-keymap)))
10850     (define-key gnus-summary-generic-mark-map keystroke map)
10851     (dolist (lway `((next "next" next nil "n")
10852                     (next-unread "next unread" next t "N")
10853                     (prev "previous" prev nil "p")
10854                     (prev-unread "previous unread" prev t "P")
10855                     (nomove "" nil nil ,keystroke)))
10856       (let ((func (gnus-summary-make-marking-command-1
10857                    mark (car lway) lway name)))
10858         (setq func (eval func))
10859         (define-key map (nth 4 lway) func)))))
10860
10861 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10862   `(defun ,(intern
10863             (format "gnus-summary-put-mark-as-%s%s"
10864                     name (if (eq way 'nomove)
10865                              ""
10866                            (concat "-" (symbol-name way)))))
10867      (n)
10868      ,(format
10869        "Mark the current article as %s%s.
10870 If N, the prefix, then repeat N times.
10871 If N is negative, move in reverse order.
10872 The difference between N and the actual number of articles marked is
10873 returned."
10874        name (car (cdr lway)))
10875      (interactive "p")
10876      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10877
10878 (defun gnus-summary-generic-mark (n mark move unread)
10879   "Mark N articles with MARK."
10880   (unless (eq major-mode 'gnus-summary-mode)
10881     (error "This command can only be used in the summary buffer"))
10882   (gnus-summary-show-thread)
10883   (let ((nummove
10884          (cond
10885           ((eq move 'next) 1)
10886           ((eq move 'prev) -1)
10887           (t 0))))
10888     (if (zerop nummove)
10889         (setq n 1)
10890       (when (< n 0)
10891         (setq n (abs n)
10892               nummove (* -1 nummove))))
10893     (while (and (> n 0)
10894                 (gnus-summary-mark-article nil mark)
10895                 (zerop (gnus-summary-next-subject nummove unread t)))
10896       (setq n (1- n)))
10897     (when (/= 0 n)
10898       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10899     (gnus-summary-recenter)
10900     (gnus-summary-position-point)
10901     (gnus-set-mode-line 'summary)
10902     n))
10903
10904 (defun gnus-summary-insert-articles (articles)
10905   (when (setq articles
10906               (gnus-set-difference articles
10907                                    (mapcar (lambda (h) (mail-header-number h))
10908                                            gnus-newsgroup-headers)))
10909     (setq gnus-newsgroup-headers
10910           (merge 'list
10911                  gnus-newsgroup-headers
10912                  (gnus-fetch-headers articles)
10913                  'gnus-article-sort-by-number))
10914     ;; Suppress duplicates?
10915     (when gnus-suppress-duplicates
10916       (gnus-dup-suppress-articles))
10917
10918     ;; We might want to build some more threads first.
10919     (when (and gnus-fetch-old-headers
10920                (eq gnus-headers-retrieved-by 'nov))
10921       (if (eq gnus-fetch-old-headers 'invisible)
10922           (gnus-build-all-threads)
10923         (gnus-build-old-threads)))
10924     ;; Let the Gnus agent mark articles as read.
10925     (when gnus-agent
10926       (gnus-agent-get-undownloaded-list))
10927     ;; Remove list identifiers from subject
10928     (when gnus-list-identifiers
10929       (gnus-summary-remove-list-identifiers))
10930     ;; First and last article in this newsgroup.
10931     (when gnus-newsgroup-headers
10932       (setq gnus-newsgroup-begin
10933             (mail-header-number (car gnus-newsgroup-headers))
10934             gnus-newsgroup-end
10935             (mail-header-number
10936              (gnus-last-element gnus-newsgroup-headers))))
10937     (when gnus-use-scoring
10938       (gnus-possibly-score-headers))))
10939
10940 (defun gnus-summary-insert-old-articles (&optional all)
10941   "Insert all old articles in this group.
10942 If ALL is non-nil, already read articles become readable.
10943 If ALL is a number, fetch this number of articles."
10944   (interactive "P")
10945   (prog1
10946       (let ((old (mapcar 'car gnus-newsgroup-data))
10947             (i (car gnus-newsgroup-active))
10948             older len)
10949         (while (<= i (cdr gnus-newsgroup-active))
10950           (or (memq i old) (push i older))
10951           (incf i))
10952         (setq len (length older))
10953         (cond
10954          ((null older) nil)
10955          ((numberp all)
10956           (if (< all len)
10957               (setq older (subseq older 0 all))))
10958          (all nil)
10959          (t
10960           (if (and (numberp gnus-large-newsgroup)
10961                    (> len gnus-large-newsgroup))
10962               (let ((input
10963                      (read-string
10964                       (format
10965                        "How many articles from %s (default %d): "
10966                        (gnus-limit-string
10967                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10968                        len))))
10969                 (unless (string-match "^[ \t]*$" input)
10970                   (setq all (string-to-number input))
10971                   (if (< all len)
10972                       (setq older (subseq older 0 all))))))))
10973         (if (not older)
10974             (message "No old news.")
10975           (gnus-summary-insert-articles older)
10976           (gnus-summary-limit (gnus-union older old))))
10977     (gnus-summary-position-point)))
10978
10979 (defun gnus-summary-insert-new-articles ()
10980   "Insert all new articles in this group."
10981   (interactive)
10982   (prog1
10983       (let ((old (mapcar 'car gnus-newsgroup-data))
10984             (old-active gnus-newsgroup-active)
10985             (nnmail-fetched-sources (list t))
10986             i new)
10987         (setq gnus-newsgroup-active
10988               (gnus-activate-group gnus-newsgroup-name 'scan))
10989         (setq i (1+ (cdr old-active)))
10990         (while (<= i (cdr gnus-newsgroup-active))
10991           (push i new)
10992           (incf i))
10993         (if (not new)
10994             (message "No gnus is bad news.")
10995           (setq new (nreverse new))
10996           (gnus-summary-insert-articles new)
10997           (setq gnus-newsgroup-unreads
10998                 (append gnus-newsgroup-unreads new))
10999           (gnus-summary-limit (gnus-union old new))))
11000     (gnus-summary-position-point)))
11001
11002 (gnus-summary-make-all-marking-commands)
11003
11004 (gnus-ems-redefine)
11005
11006 (provide 'gnus-sum)
11007
11008 (run-hooks 'gnus-sum-load-hook)
11009
11010 ;;; gnus-sum.el ends here