2441e5b66da1ffbc1e80ac23f35e3e20f1a70cf0
[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   "An alist of format specifications that can appear in summary lines.
1135 These are paired with what variables they correspond with, along with
1136 the type of the variable (string, integer, character, etc).")
1137
1138 (defvar gnus-summary-dummy-line-format-alist
1139   `((?S gnus-tmp-subject ?s)
1140     (?N gnus-tmp-number ?d)
1141     (?u gnus-tmp-user-defined ?s)))
1142
1143 (defvar gnus-summary-mode-line-format-alist
1144   `((?G gnus-tmp-group-name ?s)
1145     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1146     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1147     (?A gnus-tmp-article-number ?d)
1148     (?Z gnus-tmp-unread-and-unselected ?s)
1149     (?V gnus-version ?s)
1150     (?U gnus-tmp-unread-and-unticked ?d)
1151     (?S gnus-tmp-subject ?s)
1152     (?e gnus-tmp-unselected ?d)
1153     (?u gnus-tmp-user-defined ?s)
1154     (?d (length gnus-newsgroup-dormant) ?d)
1155     (?t (length gnus-newsgroup-marked) ?d)
1156     (?r (length gnus-newsgroup-reads) ?d)
1157     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1158     (?E gnus-newsgroup-expunged-tally ?d)
1159     (?s (gnus-current-score-file-nondirectory) ?s)))
1160
1161 (defvar gnus-last-search-regexp nil
1162   "Default regexp for article search command.")
1163
1164 (defvar gnus-summary-search-article-matched-data nil
1165   "Last matched data of article search command.  It is the local variable
1166 in `gnus-article-buffer' which consists of the list of start position,
1167 end position and text.")
1168
1169 (defvar gnus-last-shell-command nil
1170   "Default shell command on article.")
1171
1172 (defvar gnus-newsgroup-begin nil)
1173 (defvar gnus-newsgroup-end nil)
1174 (defvar gnus-newsgroup-last-rmail nil)
1175 (defvar gnus-newsgroup-last-mail nil)
1176 (defvar gnus-newsgroup-last-folder nil)
1177 (defvar gnus-newsgroup-last-file nil)
1178 (defvar gnus-newsgroup-auto-expire nil)
1179 (defvar gnus-newsgroup-active nil)
1180
1181 (defvar gnus-newsgroup-data nil)
1182 (defvar gnus-newsgroup-data-reverse nil)
1183 (defvar gnus-newsgroup-limit nil)
1184 (defvar gnus-newsgroup-limits nil)
1185
1186 (defvar gnus-newsgroup-unreads nil
1187   "List of unread articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-unselected nil
1190   "List of unselected unread articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-reads nil
1193   "Alist of read articles and article marks in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-expunged-tally nil)
1196
1197 (defvar gnus-newsgroup-marked nil
1198   "List of ticked articles in the current newsgroup (a subset of unread art).")
1199
1200 (defvar gnus-newsgroup-killed nil
1201   "List of ranges of articles that have been through the scoring process.")
1202
1203 (defvar gnus-newsgroup-cached nil
1204   "List of articles that come from the article cache.")
1205
1206 (defvar gnus-newsgroup-saved nil
1207   "List of articles that have been saved.")
1208
1209 (defvar gnus-newsgroup-kill-headers nil)
1210
1211 (defvar gnus-newsgroup-replied nil
1212   "List of articles that have been replied to in the current newsgroup.")
1213
1214 (defvar gnus-newsgroup-forwarded nil
1215   "List of articles that have been forwarded in the current newsgroup.")
1216
1217 (defvar gnus-newsgroup-recent nil
1218   "List of articles that have are recent in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-expirable nil
1221   "List of articles in the current newsgroup that can be expired.")
1222
1223 (defvar gnus-newsgroup-processable nil
1224   "List of articles in the current newsgroup that can be processed.")
1225
1226 (defvar gnus-newsgroup-downloadable nil
1227   "List of articles in the current newsgroup that can be processed.")
1228
1229 (defvar gnus-newsgroup-undownloaded nil
1230   "List of articles in the current newsgroup that haven't been downloaded..")
1231
1232 (defvar gnus-newsgroup-unsendable nil
1233   "List of articles in the current newsgroup that won't be sent.")
1234
1235 (defvar gnus-newsgroup-bookmarks nil
1236   "List of articles in the current newsgroup that have bookmarks.")
1237
1238 (defvar gnus-newsgroup-dormant nil
1239   "List of dormant articles in the current newsgroup.")
1240
1241 (defvar gnus-newsgroup-unseen nil
1242   "List of unseen articles in the current newsgroup.")
1243
1244 (defvar gnus-newsgroup-seen nil
1245   "Range of seen articles in the current newsgroup.")
1246
1247 (defvar gnus-newsgroup-articles nil
1248   "List of articles in the current newsgroup.")
1249
1250 (defvar gnus-newsgroup-scored nil
1251   "List of scored articles in the current newsgroup.")
1252
1253 (defvar gnus-newsgroup-incorporated nil
1254   "List of incorporated articles in the current newsgroup.")
1255
1256 (defvar gnus-newsgroup-headers nil
1257   "List of article headers in the current newsgroup.")
1258
1259 (defvar gnus-newsgroup-threads nil)
1260
1261 (defvar gnus-newsgroup-prepared nil
1262   "Whether the current group has been prepared properly.")
1263
1264 (defvar gnus-newsgroup-ancient nil
1265   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-sparse nil)
1268
1269 (defvar gnus-current-article nil)
1270 (defvar gnus-article-current nil)
1271 (defvar gnus-current-headers nil)
1272 (defvar gnus-have-all-headers nil)
1273 (defvar gnus-last-article nil)
1274 (defvar gnus-newsgroup-history nil)
1275 (defvar gnus-newsgroup-charset nil)
1276 (defvar gnus-newsgroup-ephemeral-charset nil)
1277 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1278
1279 (defvar gnus-article-before-search nil)
1280
1281 (defconst gnus-summary-local-variables
1282   '(gnus-newsgroup-name
1283     gnus-newsgroup-begin gnus-newsgroup-end
1284     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1285     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1286     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1287     gnus-newsgroup-unselected gnus-newsgroup-marked
1288     gnus-newsgroup-reads gnus-newsgroup-saved
1289     gnus-newsgroup-replied gnus-newsgroup-forwarded
1290     gnus-newsgroup-recent
1291     gnus-newsgroup-expirable
1292     gnus-newsgroup-processable gnus-newsgroup-killed
1293     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1294     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1295     gnus-newsgroup-seen gnus-newsgroup-articles
1296     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1297     gnus-newsgroup-headers gnus-newsgroup-threads
1298     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1299     gnus-current-article gnus-current-headers gnus-have-all-headers
1300     gnus-last-article gnus-article-internal-prepare-hook
1301     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1302     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1303     gnus-thread-expunge-below
1304     gnus-score-alist gnus-current-score-file
1305     (gnus-summary-expunge-below . global)
1306     (gnus-summary-mark-below . global)
1307     (gnus-orphan-score . global)
1308     gnus-newsgroup-active gnus-scores-exclude-files
1309     gnus-newsgroup-history gnus-newsgroup-ancient
1310     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1311     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1312     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1313     (gnus-newsgroup-expunged-tally . 0)
1314     gnus-cache-removable-articles gnus-newsgroup-cached
1315     gnus-newsgroup-data gnus-newsgroup-data-reverse
1316     gnus-newsgroup-limit gnus-newsgroup-limits
1317     gnus-newsgroup-charset gnus-newsgroup-display
1318     gnus-newsgroup-incorporated)
1319   "Variables that are buffer-local to the summary buffers.")
1320
1321 (defvar gnus-newsgroup-variables nil
1322   "Variables that have separate values in the newsgroups.")
1323
1324 ;; Byte-compiler warning.
1325 (eval-when-compile (defvar gnus-article-mode-map))
1326
1327 ;; Subject simplification.
1328
1329 (defun gnus-simplify-whitespace (str)
1330   "Remove excessive whitespace from STR."
1331   (let ((mystr str))
1332     ;; Multiple spaces.
1333     (while (string-match "[ \t][ \t]+" mystr)
1334       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1335                           " "
1336                           (substring mystr (match-end 0)))))
1337     ;; Leading spaces.
1338     (when (string-match "^[ \t]+" mystr)
1339       (setq mystr (substring mystr (match-end 0))))
1340     ;; Trailing spaces.
1341     (when (string-match "[ \t]+$" mystr)
1342       (setq mystr (substring mystr 0 (match-beginning 0))))
1343     mystr))
1344
1345 (defsubst gnus-simplify-subject-re (subject)
1346   "Remove \"Re:\" from subject lines."
1347   (if (string-match message-subject-re-regexp subject)
1348       (substring subject (match-end 0))
1349     subject))
1350
1351 (defun gnus-simplify-subject (subject &optional re-only)
1352   "Remove `Re:' and words in parentheses.
1353 If RE-ONLY is non-nil, strip leading `Re:'s only."
1354   (let ((case-fold-search t))           ;Ignore case.
1355     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1356     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1357       (setq subject (substring subject (match-end 0))))
1358     ;; Remove uninteresting prefixes.
1359     (when (and (not re-only)
1360                gnus-simplify-ignored-prefixes
1361                (string-match gnus-simplify-ignored-prefixes subject))
1362       (setq subject (substring subject (match-end 0))))
1363     ;; Remove words in parentheses from end.
1364     (unless re-only
1365       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1366         (setq subject (substring subject 0 (match-beginning 0)))))
1367     ;; Return subject string.
1368     subject))
1369
1370 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1371 ;; all whitespace.
1372 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1373   (goto-char (point-min))
1374   (while (re-search-forward regexp nil t)
1375     (replace-match (or newtext ""))))
1376
1377 (defun gnus-simplify-buffer-fuzzy ()
1378   "Simplify string in the buffer fuzzily.
1379 The string in the accessible portion of the current buffer is simplified.
1380 It is assumed to be a single-line subject.
1381 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1382 matter is removed.  Additional things can be deleted by setting
1383 `gnus-simplify-subject-fuzzy-regexp'."
1384   (let ((case-fold-search t)
1385         (modified-tick))
1386     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1387
1388     (while (not (eq modified-tick (buffer-modified-tick)))
1389       (setq modified-tick (buffer-modified-tick))
1390       (cond
1391        ((listp gnus-simplify-subject-fuzzy-regexp)
1392         (mapcar 'gnus-simplify-buffer-fuzzy-step
1393                 gnus-simplify-subject-fuzzy-regexp))
1394        (gnus-simplify-subject-fuzzy-regexp
1395         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1396       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1397       (gnus-simplify-buffer-fuzzy-step
1398        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1399       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1400
1401     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1402     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1403     (gnus-simplify-buffer-fuzzy-step " $")
1404     (gnus-simplify-buffer-fuzzy-step "^ +")))
1405
1406 (defun gnus-simplify-subject-fuzzy (subject)
1407   "Simplify a subject string fuzzily.
1408 See `gnus-simplify-buffer-fuzzy' for details."
1409   (save-excursion
1410     (gnus-set-work-buffer)
1411     (let ((case-fold-search t))
1412       ;; Remove uninteresting prefixes.
1413       (when (and gnus-simplify-ignored-prefixes
1414                  (string-match gnus-simplify-ignored-prefixes subject))
1415         (setq subject (substring subject (match-end 0))))
1416       (insert subject)
1417       (inline (gnus-simplify-buffer-fuzzy))
1418       (buffer-string))))
1419
1420 (defsubst gnus-simplify-subject-fully (subject)
1421   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1422   (cond
1423    (gnus-simplify-subject-functions
1424     (gnus-map-function gnus-simplify-subject-functions subject))
1425    ((null gnus-summary-gather-subject-limit)
1426     (gnus-simplify-subject-re subject))
1427    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1428     (gnus-simplify-subject-fuzzy subject))
1429    ((numberp gnus-summary-gather-subject-limit)
1430     (gnus-limit-string (gnus-simplify-subject-re subject)
1431                        gnus-summary-gather-subject-limit))
1432    (t
1433     subject)))
1434
1435 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1436   "Check whether two subjects are equal.
1437 If optional argument simple-first is t, first argument is already
1438 simplified."
1439   (cond
1440    ((null simple-first)
1441     (equal (gnus-simplify-subject-fully s1)
1442            (gnus-simplify-subject-fully s2)))
1443    (t
1444     (equal s1
1445            (gnus-simplify-subject-fully s2)))))
1446
1447 (defun gnus-summary-bubble-group ()
1448   "Increase the score of the current group.
1449 This is a handy function to add to `gnus-summary-exit-hook' to
1450 increase the score of each group you read."
1451   (gnus-group-add-score gnus-newsgroup-name))
1452
1453 \f
1454 ;;;
1455 ;;; Gnus summary mode
1456 ;;;
1457
1458 (put 'gnus-summary-mode 'mode-class 'special)
1459
1460 (defvar gnus-article-commands-menu)
1461
1462 (when t
1463   ;; Non-orthogonal keys
1464
1465   (gnus-define-keys gnus-summary-mode-map
1466     " " gnus-summary-next-page
1467     "\177" gnus-summary-prev-page
1468     [delete] gnus-summary-prev-page
1469     [backspace] gnus-summary-prev-page
1470     "\r" gnus-summary-scroll-up
1471     "\M-\r" gnus-summary-scroll-down
1472     "n" gnus-summary-next-unread-article
1473     "p" gnus-summary-prev-unread-article
1474     "N" gnus-summary-next-article
1475     "P" gnus-summary-prev-article
1476     "\M-\C-n" gnus-summary-next-same-subject
1477     "\M-\C-p" gnus-summary-prev-same-subject
1478     "\M-n" gnus-summary-next-unread-subject
1479     "\M-p" gnus-summary-prev-unread-subject
1480     "." gnus-summary-first-unread-article
1481     "," gnus-summary-best-unread-article
1482     "\M-s" gnus-summary-search-article-forward
1483     "\M-r" gnus-summary-search-article-backward
1484     "<" gnus-summary-beginning-of-article
1485     ">" gnus-summary-end-of-article
1486     "j" gnus-summary-goto-article
1487     "^" gnus-summary-refer-parent-article
1488     "\M-^" gnus-summary-refer-article
1489     "u" gnus-summary-tick-article-forward
1490     "!" gnus-summary-tick-article-forward
1491     "U" gnus-summary-tick-article-backward
1492     "d" gnus-summary-mark-as-read-forward
1493     "D" gnus-summary-mark-as-read-backward
1494     "E" gnus-summary-mark-as-expirable
1495     "\M-u" gnus-summary-clear-mark-forward
1496     "\M-U" gnus-summary-clear-mark-backward
1497     "k" gnus-summary-kill-same-subject-and-select
1498     "\C-k" gnus-summary-kill-same-subject
1499     "\M-\C-k" gnus-summary-kill-thread
1500     "\M-\C-l" gnus-summary-lower-thread
1501     "e" gnus-summary-edit-article
1502     "#" gnus-summary-mark-as-processable
1503     "\M-#" gnus-summary-unmark-as-processable
1504     "\M-\C-t" gnus-summary-toggle-threads
1505     "\M-\C-s" gnus-summary-show-thread
1506     "\M-\C-h" gnus-summary-hide-thread
1507     "\M-\C-f" gnus-summary-next-thread
1508     "\M-\C-b" gnus-summary-prev-thread
1509     [(meta down)] gnus-summary-next-thread
1510     [(meta up)] gnus-summary-prev-thread
1511     "\M-\C-u" gnus-summary-up-thread
1512     "\M-\C-d" gnus-summary-down-thread
1513     "&" gnus-summary-execute-command
1514     "c" gnus-summary-catchup-and-exit
1515     "\C-w" gnus-summary-mark-region-as-read
1516     "\C-t" gnus-summary-toggle-truncation
1517     "?" gnus-summary-mark-as-dormant
1518     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1519     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1520     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1521     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1522     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1523     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1524     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1525     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1526     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1527     "=" gnus-summary-expand-window
1528     "\C-x\C-s" gnus-summary-reselect-current-group
1529     "\M-g" gnus-summary-rescan-group
1530     "w" gnus-summary-stop-page-breaking
1531     "\C-c\C-r" gnus-summary-caesar-message
1532     "\M-t" gnus-summary-toggle-mime
1533     "f" gnus-summary-followup
1534     "F" gnus-summary-followup-with-original
1535     "C" gnus-summary-cancel-article
1536     "r" gnus-summary-reply
1537     "R" gnus-summary-reply-with-original
1538     "\C-c\C-f" gnus-summary-mail-forward
1539     "o" gnus-summary-save-article
1540     "\C-o" gnus-summary-save-article-mail
1541     "|" gnus-summary-pipe-output
1542     "\M-k" gnus-summary-edit-local-kill
1543     "\M-K" gnus-summary-edit-global-kill
1544     ;; "V" gnus-version
1545     "\C-c\C-d" gnus-summary-describe-group
1546     "q" gnus-summary-exit
1547     "Q" gnus-summary-exit-no-update
1548     "\C-c\C-i" gnus-info-find-node
1549     gnus-mouse-2 gnus-mouse-pick-article
1550     "m" gnus-summary-mail-other-window
1551     "a" gnus-summary-post-news
1552     "x" gnus-summary-limit-to-unread
1553     "s" gnus-summary-isearch-article
1554     "t" gnus-article-toggle-headers
1555     "g" gnus-summary-show-article
1556     "l" gnus-summary-goto-last-article
1557     "v" gnus-summary-preview-mime-message
1558     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1559     "\C-d" gnus-summary-enter-digest-group
1560     "\M-\C-d" gnus-summary-read-document
1561     "\M-\C-e" gnus-summary-edit-parameters
1562     "\M-\C-a" gnus-summary-customize-parameters
1563     "\C-c\C-b" gnus-bug
1564     "\C-c\C-n" gnus-namazu-search
1565     "*" gnus-cache-enter-article
1566     "\M-*" gnus-cache-remove-article
1567     "\M-&" gnus-summary-universal-argument
1568     "\C-l" gnus-recenter
1569     "I" gnus-summary-increase-score
1570     "L" gnus-summary-lower-score
1571     "\M-i" gnus-symbolic-argument
1572     "h" gnus-summary-select-article-buffer
1573
1574     "V" gnus-summary-score-map
1575     "X" gnus-uu-extract-map
1576     "S" gnus-summary-send-map)
1577
1578   ;; Sort of orthogonal keymap
1579   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1580     "t" gnus-summary-tick-article-forward
1581     "!" gnus-summary-tick-article-forward
1582     "d" gnus-summary-mark-as-read-forward
1583     "r" gnus-summary-mark-as-read-forward
1584     "c" gnus-summary-clear-mark-forward
1585     " " gnus-summary-clear-mark-forward
1586     "e" gnus-summary-mark-as-expirable
1587     "x" gnus-summary-mark-as-expirable
1588     "?" gnus-summary-mark-as-dormant
1589     "b" gnus-summary-set-bookmark
1590     "B" gnus-summary-remove-bookmark
1591     "#" gnus-summary-mark-as-processable
1592     "\M-#" gnus-summary-unmark-as-processable
1593     "S" gnus-summary-limit-include-expunged
1594     "C" gnus-summary-catchup
1595     "H" gnus-summary-catchup-to-here
1596     "h" gnus-summary-catchup-from-here
1597     "\C-c" gnus-summary-catchup-all
1598     "k" gnus-summary-kill-same-subject-and-select
1599     "K" gnus-summary-kill-same-subject
1600     "P" gnus-uu-mark-map)
1601
1602   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1603     "c" gnus-summary-clear-above
1604     "u" gnus-summary-tick-above
1605     "m" gnus-summary-mark-above
1606     "k" gnus-summary-kill-below)
1607
1608   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1609     "/" gnus-summary-limit-to-subject
1610     "n" gnus-summary-limit-to-articles
1611     "w" gnus-summary-pop-limit
1612     "s" gnus-summary-limit-to-subject
1613     "a" gnus-summary-limit-to-author
1614     "u" gnus-summary-limit-to-unread
1615     "m" gnus-summary-limit-to-marks
1616     "M" gnus-summary-limit-exclude-marks
1617     "v" gnus-summary-limit-to-score
1618     "*" gnus-summary-limit-include-cached
1619     "D" gnus-summary-limit-include-dormant
1620     "T" gnus-summary-limit-include-thread
1621     "d" gnus-summary-limit-exclude-dormant
1622     "t" gnus-summary-limit-to-age
1623     "x" gnus-summary-limit-to-extra
1624     "p" gnus-summary-limit-to-display-predicate
1625     "E" gnus-summary-limit-include-expunged
1626     "c" gnus-summary-limit-exclude-childless-dormant
1627     "C" gnus-summary-limit-mark-excluded-as-read
1628     "o" gnus-summary-insert-old-articles
1629     "N" gnus-summary-insert-new-articles)
1630
1631   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1632     "n" gnus-summary-next-unread-article
1633     "p" gnus-summary-prev-unread-article
1634     "N" gnus-summary-next-article
1635     "P" gnus-summary-prev-article
1636     "\C-n" gnus-summary-next-same-subject
1637     "\C-p" gnus-summary-prev-same-subject
1638     "\M-n" gnus-summary-next-unread-subject
1639     "\M-p" gnus-summary-prev-unread-subject
1640     "f" gnus-summary-first-unread-article
1641     "b" gnus-summary-best-unread-article
1642     "j" gnus-summary-goto-article
1643     "g" gnus-summary-goto-subject
1644     "l" gnus-summary-goto-last-article
1645     "o" gnus-summary-pop-article)
1646
1647   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1648     "k" gnus-summary-kill-thread
1649     "l" gnus-summary-lower-thread
1650     "i" gnus-summary-raise-thread
1651     "T" gnus-summary-toggle-threads
1652     "t" gnus-summary-rethread-current
1653     "^" gnus-summary-reparent-thread
1654     "s" gnus-summary-show-thread
1655     "S" gnus-summary-show-all-threads
1656     "h" gnus-summary-hide-thread
1657     "H" gnus-summary-hide-all-threads
1658     "n" gnus-summary-next-thread
1659     "p" gnus-summary-prev-thread
1660     "u" gnus-summary-up-thread
1661     "o" gnus-summary-top-thread
1662     "d" gnus-summary-down-thread
1663     "#" gnus-uu-mark-thread
1664     "\M-#" gnus-uu-unmark-thread)
1665
1666   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1667     "g" gnus-summary-prepare
1668     "c" gnus-summary-insert-cached-articles)
1669
1670   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1671     "c" gnus-summary-catchup-and-exit
1672     "C" gnus-summary-catchup-all-and-exit
1673     "E" gnus-summary-exit-no-update
1674     "J" gnus-summary-jump-to-other-group
1675     "Q" gnus-summary-exit
1676     "Z" gnus-summary-exit
1677     "n" gnus-summary-catchup-and-goto-next-group
1678     "R" gnus-summary-reselect-current-group
1679     "G" gnus-summary-rescan-group
1680     "N" gnus-summary-next-group
1681     "s" gnus-summary-save-newsrc
1682     "P" gnus-summary-prev-group)
1683
1684   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1685     " " gnus-summary-next-page
1686     "n" gnus-summary-next-page
1687     "\177" gnus-summary-prev-page
1688     [delete] gnus-summary-prev-page
1689     "p" gnus-summary-prev-page
1690     "\r" gnus-summary-scroll-up
1691     "\M-\r" gnus-summary-scroll-down
1692     "<" gnus-summary-beginning-of-article
1693     ">" gnus-summary-end-of-article
1694     "b" gnus-summary-beginning-of-article
1695     "e" gnus-summary-end-of-article
1696     "^" gnus-summary-refer-parent-article
1697     "r" gnus-summary-refer-parent-article
1698     "D" gnus-summary-enter-digest-group
1699     "R" gnus-summary-refer-references
1700     "T" gnus-summary-refer-thread
1701     "g" gnus-summary-show-article
1702     "s" gnus-summary-isearch-article
1703     "P" gnus-summary-print-article
1704     "M" gnus-mailing-list-insinuate
1705     "t" gnus-article-babel)
1706
1707   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1708     "b" gnus-article-add-buttons
1709     "B" gnus-article-add-buttons-to-head
1710     "o" gnus-article-treat-overstrike
1711     "e" gnus-article-emphasize
1712     "w" gnus-article-fill-cited-article
1713     "Q" gnus-article-fill-long-lines
1714     "C" gnus-article-capitalize-sentences
1715     "c" gnus-article-remove-cr
1716     "Z" gnus-article-decode-HZ
1717     "f" gnus-article-display-x-face
1718     "l" gnus-summary-stop-page-breaking
1719     "r" gnus-summary-caesar-message
1720     "t" gnus-article-toggle-headers
1721     "g" gnus-summary-toggle-smiley
1722     "v" gnus-summary-verbose-headers
1723     "m" gnus-summary-toggle-mime
1724     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1725     "p" gnus-article-verify-x-pgp-sig
1726     "d" gnus-article-treat-dumbquotes
1727     "s" gnus-smiley-display)
1728
1729   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1730     "a" gnus-article-hide
1731     "h" gnus-article-toggle-headers
1732     "b" gnus-article-hide-boring-headers
1733     "s" gnus-article-hide-signature
1734     "c" gnus-article-hide-citation
1735     "C" gnus-article-hide-citation-in-followups
1736     "l" gnus-article-hide-list-identifiers
1737     "p" gnus-article-hide-pgp
1738     "B" gnus-article-strip-banner
1739     "P" gnus-article-hide-pem
1740     "\C-c" gnus-article-hide-citation-maybe)
1741
1742   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1743     "a" gnus-article-highlight
1744     "h" gnus-article-highlight-headers
1745     "c" gnus-article-highlight-citation
1746     "s" gnus-article-highlight-signature)
1747
1748   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1749     "z" gnus-article-date-ut
1750     "u" gnus-article-date-ut
1751     "l" gnus-article-date-local
1752     "p" gnus-article-date-english
1753     "e" gnus-article-date-lapsed
1754     "o" gnus-article-date-original
1755     "i" gnus-article-date-iso8601
1756     "s" gnus-article-date-user)
1757
1758   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1759     "t" gnus-article-remove-trailing-blank-lines
1760     "l" gnus-article-strip-leading-blank-lines
1761     "m" gnus-article-strip-multiple-blank-lines
1762     "a" gnus-article-strip-blank-lines
1763     "A" gnus-article-strip-all-blank-lines
1764     "s" gnus-article-strip-leading-space
1765     "e" gnus-article-strip-trailing-space
1766     "w" gnus-article-remove-leading-whitespace)
1767
1768   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1769     "v" gnus-version
1770     "f" gnus-summary-fetch-faq
1771     "d" gnus-summary-describe-group
1772     "h" gnus-summary-describe-briefly
1773     "i" gnus-info-find-node)
1774
1775   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1776     "e" gnus-summary-expire-articles
1777     "\M-\C-e" gnus-summary-expire-articles-now
1778     "\177" gnus-summary-delete-article
1779     [delete] gnus-summary-delete-article
1780     [backspace] gnus-summary-delete-article
1781     "m" gnus-summary-move-article
1782     "r" gnus-summary-respool-article
1783     "w" gnus-summary-edit-article
1784     "c" gnus-summary-copy-article
1785     "B" gnus-summary-crosspost-article
1786     "q" gnus-summary-respool-query
1787     "t" gnus-summary-respool-trace
1788     "i" gnus-summary-import-article
1789     "I" gnus-summary-create-article
1790     "p" gnus-summary-article-posted-p)
1791
1792   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1793     "o" gnus-summary-save-article
1794     "m" gnus-summary-save-article-mail
1795     "F" gnus-summary-write-article-file
1796     "r" gnus-summary-save-article-rmail
1797     "f" gnus-summary-save-article-file
1798     "b" gnus-summary-save-article-body-file
1799     "h" gnus-summary-save-article-folder
1800     "v" gnus-summary-save-article-vm
1801     "p" gnus-summary-pipe-output
1802     "s" gnus-soup-add-article)
1803
1804   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1805     "b" gnus-summary-display-buttonized
1806     "m" gnus-summary-repair-multipart
1807     "v" gnus-article-view-part
1808     "o" gnus-article-save-part
1809     "c" gnus-article-copy-part
1810     "C" gnus-article-view-part-as-charset
1811     "e" gnus-article-externalize-part
1812     "E" gnus-article-encrypt-body
1813     "i" gnus-article-inline-part
1814     "|" gnus-article-pipe-part))
1815
1816 (defvar gnus-article-post-menu nil)
1817
1818 (defun gnus-summary-make-menu-bar ()
1819   (gnus-turn-off-edit-menu 'summary)
1820
1821   (unless (boundp 'gnus-summary-misc-menu)
1822
1823     (easy-menu-define
1824      gnus-summary-kill-menu gnus-summary-mode-map ""
1825      (cons
1826       "Score"
1827       (nconc
1828        (list
1829         ["Customize" gnus-score-customize t])
1830        (gnus-make-score-map 'increase)
1831        (gnus-make-score-map 'lower)
1832        '(("Mark"
1833           ["Kill below" gnus-summary-kill-below t]
1834           ["Mark above" gnus-summary-mark-above t]
1835           ["Tick above" gnus-summary-tick-above t]
1836           ["Clear above" gnus-summary-clear-above t])
1837          ["Current score" gnus-summary-current-score t]
1838          ["Set score" gnus-summary-set-score t]
1839          ["Switch current score file..." gnus-score-change-score-file t]
1840          ["Set mark below..." gnus-score-set-mark-below t]
1841          ["Set expunge below..." gnus-score-set-expunge-below t]
1842          ["Edit current score file" gnus-score-edit-current-scores t]
1843          ["Edit score file" gnus-score-edit-file t]
1844          ["Trace score" gnus-score-find-trace t]
1845          ["Find words" gnus-score-find-favourite-words t]
1846          ["Rescore buffer" gnus-summary-rescore t]
1847          ["Increase score..." gnus-summary-increase-score t]
1848          ["Lower score..." gnus-summary-lower-score t]))))
1849
1850     ;; Define both the Article menu in the summary buffer and the equivalent
1851     ;; Commands menu in the article buffer here for consistency.
1852     (let ((innards
1853            `(("Hide"
1854               ["All" gnus-article-hide t]
1855               ["Headers" gnus-article-toggle-headers t]
1856               ["Signature" gnus-article-hide-signature t]
1857               ["Citation" gnus-article-hide-citation t]
1858               ["List identifiers" gnus-article-hide-list-identifiers t]
1859               ["PGP" gnus-article-hide-pgp t]
1860               ["Banner" gnus-article-strip-banner t]
1861               ["Boring headers" gnus-article-hide-boring-headers t])
1862              ("Highlight"
1863               ["All" gnus-article-highlight t]
1864               ["Headers" gnus-article-highlight-headers t]
1865               ["Signature" gnus-article-highlight-signature t]
1866               ["Citation" gnus-article-highlight-citation t])
1867              ("Date"
1868               ["Local" gnus-article-date-local t]
1869               ["ISO8601" gnus-article-date-iso8601 t]
1870               ["UT" gnus-article-date-ut t]
1871               ["Original" gnus-article-date-original t]
1872               ["Lapsed" gnus-article-date-lapsed t]
1873               ["User-defined" gnus-article-date-user t])
1874              ("Washing"
1875               ("Remove Blanks"
1876                ["Leading" gnus-article-strip-leading-blank-lines t]
1877                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1878                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1879                ["All of the above" gnus-article-strip-blank-lines t]
1880                ["All" gnus-article-strip-all-blank-lines t]
1881                ["Leading space" gnus-article-strip-leading-space t]
1882                ["Trailing space" gnus-article-strip-trailing-space t]
1883                ["Leading space in headers"
1884                 gnus-article-remove-leading-whitespace t])
1885               ["Overstrike" gnus-article-treat-overstrike t]
1886               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1887               ["Emphasis" gnus-article-emphasize t]
1888               ["Word wrap" gnus-article-fill-cited-article t]
1889               ["Fill long lines" gnus-article-fill-long-lines t]
1890               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1891               ["CR" gnus-article-remove-cr t]
1892               ["Show X-Face" gnus-article-display-x-face t]
1893               ["Rot 13" gnus-summary-caesar-message
1894                ,@(if (featurep 'xemacs) '(t)
1895                    '(:help "\"Caesar rotate\" article by 13"))]
1896               ["Unix pipe" gnus-summary-pipe-message t]
1897               ["Add buttons" gnus-article-add-buttons t]
1898               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1899               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1900               ["Toggle MIME" gnus-summary-toggle-mime t]
1901               ["Verbose header" gnus-summary-verbose-headers t]
1902               ["Toggle header" gnus-summary-toggle-header t]
1903               ["Toggle smiley" gnus-summary-toggle-smiley t]
1904               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1905               ["HZ" gnus-article-decode-HZ t])
1906              ("Output"
1907               ["Save in default format" gnus-summary-save-article
1908                ,@(if (featurep 'xemacs) '(t)
1909                    '(:help "Save article using default method"))]
1910               ["Save in file" gnus-summary-save-article-file
1911                ,@(if (featurep 'xemacs) '(t)
1912                    '(:help "Save article in file"))]
1913               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1914               ["Save in MH folder" gnus-summary-save-article-folder t]
1915               ["Save in VM folder" gnus-summary-save-article-vm t]
1916               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1917               ["Save body in file" gnus-summary-save-article-body-file t]
1918               ["Pipe through a filter" gnus-summary-pipe-output t]
1919               ["Add to SOUP packet" gnus-soup-add-article t]
1920               ["Print" gnus-summary-print-article t])
1921              ("Backend"
1922               ["Respool article..." gnus-summary-respool-article t]
1923               ["Move article..." gnus-summary-move-article
1924                (gnus-check-backend-function
1925                 'request-move-article gnus-newsgroup-name)]
1926               ["Copy article..." gnus-summary-copy-article t]
1927               ["Crosspost article..." gnus-summary-crosspost-article
1928                (gnus-check-backend-function
1929                 'request-replace-article gnus-newsgroup-name)]
1930               ["Import file..." gnus-summary-import-article t]
1931               ["Create article..." gnus-summary-create-article t]
1932               ["Check if posted" gnus-summary-article-posted-p t]
1933               ["Edit article" gnus-summary-edit-article
1934                (not (gnus-group-read-only-p))]
1935               ["Delete article" gnus-summary-delete-article
1936                (gnus-check-backend-function
1937                 'request-expire-articles gnus-newsgroup-name)]
1938               ["Query respool" gnus-summary-respool-query t]
1939               ["Trace respool" gnus-summary-respool-trace t]
1940               ["Delete expirable articles" gnus-summary-expire-articles-now
1941                (gnus-check-backend-function
1942                 'request-expire-articles gnus-newsgroup-name)])
1943              ("Extract"
1944               ["Uudecode" gnus-uu-decode-uu
1945                ,@(if (featurep 'xemacs) '(t)
1946                    '(:help "Decode uuencoded article(s)"))]
1947               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1948               ["Unshar" gnus-uu-decode-unshar t]
1949               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1950               ["Save" gnus-uu-decode-save t]
1951               ["Binhex" gnus-uu-decode-binhex t]
1952               ["Postscript" gnus-uu-decode-postscript t])
1953              ("Cache"
1954               ["Enter article" gnus-cache-enter-article t]
1955               ["Remove article" gnus-cache-remove-article t])
1956              ["Translate" gnus-article-babel t]
1957              ["Select article buffer" gnus-summary-select-article-buffer t]
1958              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1959              ["Isearch article..." gnus-summary-isearch-article t]
1960              ["Beginning of the article" gnus-summary-beginning-of-article t]
1961              ["End of the article" gnus-summary-end-of-article t]
1962              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1963              ["Fetch referenced articles" gnus-summary-refer-references t]
1964              ["Fetch current thread" gnus-summary-refer-thread t]
1965              ["Fetch article with id..." gnus-summary-refer-article t]
1966              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1967              ["Redisplay" gnus-summary-show-article t]
1968              ["Raw article" gnus-summary-show-raw-article t])))
1969       (easy-menu-define
1970        gnus-summary-article-menu gnus-summary-mode-map ""
1971        (cons "Article" innards))
1972
1973       (if (not (keymapp gnus-summary-article-menu))
1974           (easy-menu-define
1975            gnus-article-commands-menu gnus-article-mode-map ""
1976            (cons "Commands" innards))
1977         ;; in Emacs, don't share menu.
1978         (setq gnus-article-commands-menu
1979               (copy-keymap gnus-summary-article-menu))
1980         (define-key gnus-article-mode-map [menu-bar commands]
1981           (cons "Commands" gnus-article-commands-menu))))
1982
1983     (easy-menu-define
1984      gnus-summary-thread-menu gnus-summary-mode-map ""
1985      '("Threads"
1986        ["Toggle threading" gnus-summary-toggle-threads t]
1987        ["Hide threads" gnus-summary-hide-all-threads t]
1988        ["Show threads" gnus-summary-show-all-threads t]
1989        ["Hide thread" gnus-summary-hide-thread t]
1990        ["Show thread" gnus-summary-show-thread t]
1991        ["Go to next thread" gnus-summary-next-thread t]
1992        ["Go to previous thread" gnus-summary-prev-thread t]
1993        ["Go down thread" gnus-summary-down-thread t]
1994        ["Go up thread" gnus-summary-up-thread t]
1995        ["Top of thread" gnus-summary-top-thread t]
1996        ["Mark thread as read" gnus-summary-kill-thread t]
1997        ["Lower thread score" gnus-summary-lower-thread t]
1998        ["Raise thread score" gnus-summary-raise-thread t]
1999        ["Rethread current" gnus-summary-rethread-current t]))
2000
2001     (easy-menu-define
2002      gnus-summary-post-menu gnus-summary-mode-map ""
2003      `("Post"
2004        ["Post an article" gnus-summary-post-news
2005         ,@(if (featurep 'xemacs) '(t)
2006             '(:help "Post an article"))]
2007        ["Followup" gnus-summary-followup
2008         ,@(if (featurep 'xemacs) '(t)
2009             '(:help "Post followup to this article"))]
2010        ["Followup and yank" gnus-summary-followup-with-original
2011         ,@(if (featurep 'xemacs) '(t)
2012             '(:help "Post followup to this article, quoting its contents"))]
2013        ["Supersede article" gnus-summary-supersede-article t]
2014        ["Cancel article" gnus-summary-cancel-article
2015         ,@(if (featurep 'xemacs) '(t)
2016             '(:help "Cancel an article you posted"))]
2017        ["Reply" gnus-summary-reply t]
2018        ["Reply and yank" gnus-summary-reply-with-original t]
2019        ["Wide reply" gnus-summary-wide-reply t]
2020        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2021         ,@(if (featurep 'xemacs) '(t)
2022             '(:help "Mail a reply, quoting this article"))]
2023        ["Mail forward" gnus-summary-mail-forward t]
2024        ["Post forward" gnus-summary-post-forward t]
2025        ["Digest and mail" gnus-summary-digest-mail-forward t]
2026        ["Digest and post" gnus-summary-digest-post-forward t]
2027        ["Resend message" gnus-summary-resend-message t]
2028        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2029        ["Send a mail" gnus-summary-mail-other-window t]
2030        ["Uuencode and post" gnus-uu-post-news
2031         ,@(if (featurep 'xemacs) '(t)
2032             '(:help "Post a uuencoded article"))]
2033        ["Followup via news" gnus-summary-followup-to-mail t]
2034        ["Followup via news and yank"
2035         gnus-summary-followup-to-mail-with-original t]
2036        ;;("Draft"
2037        ;;["Send" gnus-summary-send-draft t]
2038        ;;["Send bounced" gnus-resend-bounced-mail t])
2039        ))
2040
2041     (cond
2042      ((not (keymapp gnus-summary-post-menu))
2043       (setq gnus-article-post-menu gnus-summary-post-menu))
2044      ((not gnus-article-post-menu)
2045       ;; Don't share post menu.
2046       (setq gnus-article-post-menu
2047             (copy-keymap gnus-summary-post-menu))))
2048     (define-key gnus-article-mode-map [menu-bar post]
2049       (cons "Post" gnus-article-post-menu))
2050
2051     (easy-menu-define
2052      gnus-summary-misc-menu gnus-summary-mode-map ""
2053      `("Gnus"
2054        ("Mark Read"
2055         ["Mark as read" gnus-summary-mark-as-read-forward t]
2056         ["Mark same subject and select"
2057          gnus-summary-kill-same-subject-and-select t]
2058         ["Mark same subject" gnus-summary-kill-same-subject t]
2059         ["Catchup" gnus-summary-catchup
2060          ,@(if (featurep 'xemacs) '(t)
2061              '(:help "Mark unread articles in this group as read"))]
2062         ["Catchup all" gnus-summary-catchup-all t]
2063         ["Catchup to here" gnus-summary-catchup-to-here t]
2064         ["Catchup from here" gnus-summary-catchup-from-here t]
2065         ["Catchup region" gnus-summary-mark-region-as-read t]
2066         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2067        ("Mark Various"
2068         ["Tick" gnus-summary-tick-article-forward t]
2069         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2070         ["Remove marks" gnus-summary-clear-mark-forward t]
2071         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2072         ["Set bookmark" gnus-summary-set-bookmark t]
2073         ["Remove bookmark" gnus-summary-remove-bookmark t])
2074        ("Mark Limit"
2075         ["Marks..." gnus-summary-limit-to-marks t]
2076         ["Subject..." gnus-summary-limit-to-subject t]
2077         ["Author..." gnus-summary-limit-to-author t]
2078         ["Age..." gnus-summary-limit-to-age t]
2079         ["Extra..." gnus-summary-limit-to-extra t]
2080         ["Score" gnus-summary-limit-to-score t]
2081         ["Score" gnus-summary-limit-to-display-predicate t]
2082         ["Unread" gnus-summary-limit-to-unread t]
2083         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2084         ["Articles" gnus-summary-limit-to-articles t]
2085         ["Pop limit" gnus-summary-pop-limit t]
2086         ["Show dormant" gnus-summary-limit-include-dormant t]
2087         ["Hide childless dormant"
2088          gnus-summary-limit-exclude-childless-dormant t]
2089         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2090         ["Hide marked" gnus-summary-limit-exclude-marks t]
2091         ["Show expunged" gnus-summary-limit-include-expunged t])
2092        ("Process Mark"
2093         ["Set mark" gnus-summary-mark-as-processable t]
2094         ["Remove mark" gnus-summary-unmark-as-processable t]
2095         ["Remove all marks" gnus-summary-unmark-all-processable t]
2096         ["Mark above" gnus-uu-mark-over t]
2097         ["Mark series" gnus-uu-mark-series t]
2098         ["Mark region" gnus-uu-mark-region t]
2099         ["Unmark region" gnus-uu-unmark-region t]
2100         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2101         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2102         ["Mark all" gnus-uu-mark-all t]
2103         ["Mark buffer" gnus-uu-mark-buffer t]
2104         ["Mark sparse" gnus-uu-mark-sparse t]
2105         ["Mark thread" gnus-uu-mark-thread t]
2106         ["Unmark thread" gnus-uu-unmark-thread t]
2107         ("Process Mark Sets"
2108          ["Kill" gnus-summary-kill-process-mark t]
2109          ["Yank" gnus-summary-yank-process-mark
2110           gnus-newsgroup-process-stack]
2111          ["Save" gnus-summary-save-process-mark t]))
2112        ("Scroll article"
2113         ["Page forward" gnus-summary-next-page
2114          ,@(if (featurep 'xemacs) '(t)
2115              '(:help "Show next page of article"))]
2116         ["Page backward" gnus-summary-prev-page
2117          ,@(if (featurep 'xemacs) '(t)
2118              '(:help "Show previous page of article"))]
2119         ["Line forward" gnus-summary-scroll-up t])
2120        ("Move"
2121         ["Next unread article" gnus-summary-next-unread-article t]
2122         ["Previous unread article" gnus-summary-prev-unread-article t]
2123         ["Next article" gnus-summary-next-article t]
2124         ["Previous article" gnus-summary-prev-article t]
2125         ["Next unread subject" gnus-summary-next-unread-subject t]
2126         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2127         ["Next article same subject" gnus-summary-next-same-subject t]
2128         ["Previous article same subject" gnus-summary-prev-same-subject t]
2129         ["First unread article" gnus-summary-first-unread-article t]
2130         ["Best unread article" gnus-summary-best-unread-article t]
2131         ["Go to subject number..." gnus-summary-goto-subject t]
2132         ["Go to article number..." gnus-summary-goto-article t]
2133         ["Go to the last article" gnus-summary-goto-last-article t]
2134         ["Pop article off history" gnus-summary-pop-article t])
2135        ("Sort"
2136         ["Sort by number" gnus-summary-sort-by-number t]
2137         ["Sort by author" gnus-summary-sort-by-author t]
2138         ["Sort by subject" gnus-summary-sort-by-subject t]
2139         ["Sort by date" gnus-summary-sort-by-date t]
2140         ["Sort by score" gnus-summary-sort-by-score t]
2141         ["Sort by lines" gnus-summary-sort-by-lines t]
2142         ["Sort by characters" gnus-summary-sort-by-chars t]
2143         ["Original sort" gnus-summary-sort-by-original t])
2144        ("Help"
2145         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2146         ["Describe group" gnus-summary-describe-group t]
2147         ["Read manual" gnus-info-find-node t])
2148        ("Modes"
2149         ["Pick and read" gnus-pick-mode t]
2150         ["Binary" gnus-binary-mode t])
2151        ("Regeneration"
2152         ["Regenerate" gnus-summary-prepare t]
2153         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2154         ["Toggle threading" gnus-summary-toggle-threads t])
2155        ["See old articles" gnus-summary-insert-old-articles t]
2156        ["See new articles" gnus-summary-insert-new-articles t]
2157        ["Filter articles..." gnus-summary-execute-command t]
2158        ["Run command on subjects..." gnus-summary-universal-argument t]
2159        ["Search articles forward..." gnus-summary-search-article-forward t]
2160        ["Search articles backward..." gnus-summary-search-article-backward t]
2161        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2162        ["Expand window" gnus-summary-expand-window t]
2163        ["Expire expirable articles" gnus-summary-expire-articles
2164         (gnus-check-backend-function
2165          'request-expire-articles gnus-newsgroup-name)]
2166        ["Edit local kill file" gnus-summary-edit-local-kill t]
2167        ["Edit main kill file" gnus-summary-edit-global-kill t]
2168        ["Edit group parameters" gnus-summary-edit-parameters t]
2169        ["Customize group parameters" gnus-summary-customize-parameters t]
2170        ["Send a bug report" gnus-bug t]
2171        ("Exit"
2172         ["Catchup and exit" gnus-summary-catchup-and-exit
2173          ,@(if (featurep 'xemacs) '(t)
2174              '(:help "Mark unread articles in this group as read, then exit"))]
2175         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2176         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2177         ["Exit group" gnus-summary-exit
2178          ,@(if (featurep 'xemacs) '(t)
2179              '(:help "Exit current group, return to group selection mode"))]
2180         ["Exit group without updating" gnus-summary-exit-no-update t]
2181         ["Exit and goto next group" gnus-summary-next-group t]
2182         ["Exit and goto prev group" gnus-summary-prev-group t]
2183         ["Reselect group" gnus-summary-reselect-current-group t]
2184         ["Rescan group" gnus-summary-rescan-group t]
2185         ["Update dribble" gnus-summary-save-newsrc t])))
2186
2187     (gnus-run-hooks 'gnus-summary-menu-hook)))
2188
2189 (defvar gnus-summary-tool-bar-map nil)
2190
2191 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2192 (defun gnus-summary-make-tool-bar ()
2193   (if (and (fboundp 'tool-bar-add-item-from-menu)
2194            (default-value 'tool-bar-mode)
2195            (not gnus-summary-tool-bar-map))
2196       (setq gnus-summary-tool-bar-map
2197             (let ((tool-bar-map (make-sparse-keymap))
2198                   (load-path (mm-image-load-path)))
2199               (tool-bar-add-item-from-menu
2200                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2201               (tool-bar-add-item-from-menu
2202                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2203               (tool-bar-add-item-from-menu
2204                'gnus-summary-post-news "post" gnus-summary-mode-map)
2205               (tool-bar-add-item-from-menu
2206                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2207               (tool-bar-add-item-from-menu
2208                'gnus-summary-followup "followup" gnus-summary-mode-map)
2209               (tool-bar-add-item-from-menu
2210                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2211               (tool-bar-add-item-from-menu
2212                'gnus-summary-reply "reply" gnus-summary-mode-map)
2213               (tool-bar-add-item-from-menu
2214                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2215               (tool-bar-add-item-from-menu
2216                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2217               (tool-bar-add-item-from-menu
2218                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2219               (tool-bar-add-item-from-menu
2220                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2221               (tool-bar-add-item-from-menu
2222                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2223               (tool-bar-add-item-from-menu
2224                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2225               (tool-bar-add-item-from-menu
2226                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2227               (tool-bar-add-item-from-menu
2228                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2229               tool-bar-map)))
2230   (if gnus-summary-tool-bar-map
2231       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2232
2233 (defun gnus-score-set-default (var value)
2234   "A version of set that updates the GNU Emacs menu-bar."
2235   (set var value)
2236   ;; It is the message that forces the active status to be updated.
2237   (message ""))
2238
2239 (defun gnus-make-score-map (type)
2240   "Make a summary score map of type TYPE."
2241   (if t
2242       nil
2243     (let ((headers '(("author" "from" string)
2244                      ("subject" "subject" string)
2245                      ("article body" "body" string)
2246                      ("article head" "head" string)
2247                      ("xref" "xref" string)
2248                      ("extra header" "extra" string)
2249                      ("lines" "lines" number)
2250                      ("followups to author" "followup" string)))
2251           (types '((number ("less than" <)
2252                            ("greater than" >)
2253                            ("equal" =))
2254                    (string ("substring" s)
2255                            ("exact string" e)
2256                            ("fuzzy string" f)
2257                            ("regexp" r))))
2258           (perms '(("temporary" (current-time-string))
2259                    ("permanent" nil)
2260                    ("immediate" now)))
2261           header)
2262       (list
2263        (apply
2264         'nconc
2265         (list
2266          (if (eq type 'lower)
2267              "Lower score"
2268            "Increase score"))
2269         (let (outh)
2270           (while headers
2271             (setq header (car headers))
2272             (setq outh
2273                   (cons
2274                    (apply
2275                     'nconc
2276                     (list (car header))
2277                     (let ((ts (cdr (assoc (nth 2 header) types)))
2278                           outt)
2279                       (while ts
2280                         (setq outt
2281                               (cons
2282                                (apply
2283                                 'nconc
2284                                 (list (caar ts))
2285                                 (let ((ps perms)
2286                                       outp)
2287                                   (while ps
2288                                     (setq outp
2289                                           (cons
2290                                            (vector
2291                                             (caar ps)
2292                                             (list
2293                                              'gnus-summary-score-entry
2294                                              (nth 1 header)
2295                                              (if (or (string= (nth 1 header)
2296                                                               "head")
2297                                                      (string= (nth 1 header)
2298                                                               "body"))
2299                                                  ""
2300                                                (list 'gnus-summary-header
2301                                                      (nth 1 header)))
2302                                              (list 'quote (nth 1 (car ts)))
2303                                              (list 'gnus-score-delta-default
2304                                                    nil)
2305                                              (nth 1 (car ps))
2306                                              t)
2307                                             t)
2308                                            outp))
2309                                     (setq ps (cdr ps)))
2310                                   (list (nreverse outp))))
2311                                outt))
2312                         (setq ts (cdr ts)))
2313                       (list (nreverse outt))))
2314                    outh))
2315             (setq headers (cdr headers)))
2316           (list (nreverse outh))))))))
2317
2318 \f
2319
2320 (defun gnus-summary-mode (&optional group)
2321   "Major mode for reading articles.
2322
2323 All normal editing commands are switched off.
2324 \\<gnus-summary-mode-map>
2325 Each line in this buffer represents one article.  To read an
2326 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2327 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2328 respectively.
2329
2330 You can also post articles and send mail from this buffer.  To
2331 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2332 of an article, type `\\[gnus-summary-reply]'.
2333
2334 There are approx. one gazillion commands you can execute in this
2335 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2336
2337 The following commands are available:
2338
2339 \\{gnus-summary-mode-map}"
2340   (interactive)
2341   (kill-all-local-variables)
2342   (when (gnus-visual-p 'summary-menu 'menu)
2343     (gnus-summary-make-menu-bar)
2344     (gnus-summary-make-tool-bar))
2345   (gnus-summary-make-local-variables)
2346   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2347     (gnus-summary-make-local-variables))
2348   (gnus-make-thread-indent-array)
2349   (gnus-simplify-mode-line)
2350   (setq major-mode 'gnus-summary-mode)
2351   (setq mode-name "Summary")
2352   (make-local-variable 'minor-mode-alist)
2353   (use-local-map gnus-summary-mode-map)
2354   (buffer-disable-undo)
2355   (setq buffer-read-only t)             ;Disable modification
2356   (setq truncate-lines t)
2357   (setq selective-display t)
2358   (setq selective-display-ellipses t)   ;Display `...'
2359   (gnus-summary-set-display-table)
2360   (gnus-set-default-directory)
2361   (setq gnus-newsgroup-name group)
2362   (unless (gnus-news-group-p group)
2363     (setq gnus-newsgroup-incorporated
2364           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2365   (make-local-variable 'gnus-summary-line-format)
2366   (make-local-variable 'gnus-summary-line-format-spec)
2367   (make-local-variable 'gnus-summary-dummy-line-format)
2368   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2369   (make-local-variable 'gnus-summary-mark-positions)
2370   (make-local-hook 'pre-command-hook)
2371   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2372   (gnus-run-hooks 'gnus-summary-mode-hook)
2373   (turn-on-gnus-mailing-list-mode)
2374   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2375   (gnus-update-summary-mark-positions))
2376
2377 (defun gnus-summary-make-local-variables ()
2378   "Make all the local summary buffer variables."
2379   (let (global)
2380     (dolist (local gnus-summary-local-variables)
2381       (if (consp local)
2382           (progn
2383             (if (eq (cdr local) 'global)
2384                 ;; Copy the global value of the variable.
2385                 (setq global (symbol-value (car local)))
2386               ;; Use the value from the list.
2387               (setq global (eval (cdr local))))
2388             (set (make-local-variable (car local)) global))
2389         ;; Simple nil-valued local variable.
2390         (set (make-local-variable local) nil)))))
2391
2392 (defun gnus-summary-clear-local-variables ()
2393   (let ((locals gnus-summary-local-variables))
2394     (while locals
2395       (if (consp (car locals))
2396           (and (vectorp (caar locals))
2397                (set (caar locals) nil))
2398         (and (vectorp (car locals))
2399              (set (car locals) nil)))
2400       (setq locals (cdr locals)))))
2401
2402 ;; Summary data functions.
2403
2404 (defmacro gnus-data-number (data)
2405   `(car ,data))
2406
2407 (defmacro gnus-data-set-number (data number)
2408   `(setcar ,data ,number))
2409
2410 (defmacro gnus-data-mark (data)
2411   `(nth 1 ,data))
2412
2413 (defmacro gnus-data-set-mark (data mark)
2414   `(setcar (nthcdr 1 ,data) ,mark))
2415
2416 (defmacro gnus-data-pos (data)
2417   `(nth 2 ,data))
2418
2419 (defmacro gnus-data-set-pos (data pos)
2420   `(setcar (nthcdr 2 ,data) ,pos))
2421
2422 (defmacro gnus-data-header (data)
2423   `(nth 3 ,data))
2424
2425 (defmacro gnus-data-set-header (data header)
2426   `(setcar (nthcdr 3 ,data) ,header))
2427
2428 (defmacro gnus-data-level (data)
2429   `(nth 4 ,data))
2430
2431 (defmacro gnus-data-unread-p (data)
2432   `(= (nth 1 ,data) gnus-unread-mark))
2433
2434 (defmacro gnus-data-read-p (data)
2435   `(/= (nth 1 ,data) gnus-unread-mark))
2436
2437 (defmacro gnus-data-pseudo-p (data)
2438   `(consp (nth 3 ,data)))
2439
2440 (defmacro gnus-data-find (number)
2441   `(assq ,number gnus-newsgroup-data))
2442
2443 (defmacro gnus-data-find-list (number &optional data)
2444   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2445      (memq (assq ,number bdata)
2446            bdata)))
2447
2448 (defmacro gnus-data-make (number mark pos header level)
2449   `(list ,number ,mark ,pos ,header ,level))
2450
2451 (defun gnus-data-enter (after-article number mark pos header level offset)
2452   (let ((data (gnus-data-find-list after-article)))
2453     (unless data
2454       (error "No such article: %d" after-article))
2455     (setcdr data (cons (gnus-data-make number mark pos header level)
2456                        (cdr data)))
2457     (setq gnus-newsgroup-data-reverse nil)
2458     (gnus-data-update-list (cddr data) offset)))
2459
2460 (defun gnus-data-enter-list (after-article list &optional offset)
2461   (when list
2462     (let ((data (and after-article (gnus-data-find-list after-article)))
2463           (ilist list))
2464       (if (not (or data
2465                    after-article))
2466           (let ((odata gnus-newsgroup-data))
2467             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2468             (when offset
2469               (gnus-data-update-list odata offset)))
2470         ;; Find the last element in the list to be spliced into the main
2471         ;; list.
2472         (while (cdr list)
2473           (setq list (cdr list)))
2474         (if (not data)
2475             (progn
2476               (setcdr list gnus-newsgroup-data)
2477               (setq gnus-newsgroup-data ilist)
2478               (when offset
2479                 (gnus-data-update-list (cdr list) offset)))
2480           (setcdr list (cdr data))
2481           (setcdr data ilist)
2482           (when offset
2483             (gnus-data-update-list (cdr list) offset))))
2484       (setq gnus-newsgroup-data-reverse nil))))
2485
2486 (defun gnus-data-remove (article &optional offset)
2487   (let ((data gnus-newsgroup-data))
2488     (if (= (gnus-data-number (car data)) article)
2489         (progn
2490           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2491                 gnus-newsgroup-data-reverse nil)
2492           (when offset
2493             (gnus-data-update-list gnus-newsgroup-data offset)))
2494       (while (cdr data)
2495         (when (= (gnus-data-number (cadr data)) article)
2496           (setcdr data (cddr data))
2497           (when offset
2498             (gnus-data-update-list (cdr data) offset))
2499           (setq data nil
2500                 gnus-newsgroup-data-reverse nil))
2501         (setq data (cdr data))))))
2502
2503 (defmacro gnus-data-list (backward)
2504   `(if ,backward
2505        (or gnus-newsgroup-data-reverse
2506            (setq gnus-newsgroup-data-reverse
2507                  (reverse gnus-newsgroup-data)))
2508      gnus-newsgroup-data))
2509
2510 (defun gnus-data-update-list (data offset)
2511   "Add OFFSET to the POS of all data entries in DATA."
2512   (setq gnus-newsgroup-data-reverse nil)
2513   (while data
2514     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2515     (setq data (cdr data))))
2516
2517 (defun gnus-summary-article-pseudo-p (article)
2518   "Say whether this article is a pseudo article or not."
2519   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2520
2521 (defmacro gnus-summary-article-sparse-p (article)
2522   "Say whether this article is a sparse article or not."
2523   `(memq ,article gnus-newsgroup-sparse))
2524
2525 (defmacro gnus-summary-article-ancient-p (article)
2526   "Say whether this article is a sparse article or not."
2527   `(memq ,article gnus-newsgroup-ancient))
2528
2529 (defun gnus-article-parent-p (number)
2530   "Say whether this article is a parent or not."
2531   (let ((data (gnus-data-find-list number)))
2532     (and (cdr data)                     ; There has to be an article after...
2533          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2534             (gnus-data-level (nth 1 data))))))
2535
2536 (defun gnus-article-children (number)
2537   "Return a list of all children to NUMBER."
2538   (let* ((data (gnus-data-find-list number))
2539          (level (gnus-data-level (car data)))
2540          children)
2541     (setq data (cdr data))
2542     (while (and data
2543                 (= (gnus-data-level (car data)) (1+ level)))
2544       (push (gnus-data-number (car data)) children)
2545       (setq data (cdr data)))
2546     children))
2547
2548 (defmacro gnus-summary-skip-intangible ()
2549   "If the current article is intangible, then jump to a different article."
2550   '(let ((to (get-text-property (point) 'gnus-intangible)))
2551      (and to (gnus-summary-goto-subject to))))
2552
2553 (defmacro gnus-summary-article-intangible-p ()
2554   "Say whether this article is intangible or not."
2555   '(get-text-property (point) 'gnus-intangible))
2556
2557 (defun gnus-article-read-p (article)
2558   "Say whether ARTICLE is read or not."
2559   (not (or (memq article gnus-newsgroup-marked)
2560            (memq article gnus-newsgroup-unreads)
2561            (memq article gnus-newsgroup-unselected)
2562            (memq article gnus-newsgroup-dormant))))
2563
2564 ;; Some summary mode macros.
2565
2566 (defmacro gnus-summary-article-number ()
2567   "The article number of the article on the current line.
2568 If there isn's an article number here, then we return the current
2569 article number."
2570   '(progn
2571      (gnus-summary-skip-intangible)
2572      (or (get-text-property (point) 'gnus-number)
2573          (gnus-summary-last-subject))))
2574
2575 (defmacro gnus-summary-article-header (&optional number)
2576   "Return the header of article NUMBER."
2577   `(gnus-data-header (gnus-data-find
2578                       ,(or number '(gnus-summary-article-number)))))
2579
2580 (defmacro gnus-summary-thread-level (&optional number)
2581   "Return the level of thread that starts with article NUMBER."
2582   `(if (and (eq gnus-summary-make-false-root 'dummy)
2583             (get-text-property (point) 'gnus-intangible))
2584        0
2585      (gnus-data-level (gnus-data-find
2586                        ,(or number '(gnus-summary-article-number))))))
2587
2588 (defmacro gnus-summary-article-mark (&optional number)
2589   "Return the mark of article NUMBER."
2590   `(gnus-data-mark (gnus-data-find
2591                     ,(or number '(gnus-summary-article-number)))))
2592
2593 (defmacro gnus-summary-article-pos (&optional number)
2594   "Return the position of the line of article NUMBER."
2595   `(gnus-data-pos (gnus-data-find
2596                    ,(or number '(gnus-summary-article-number)))))
2597
2598 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2599 (defmacro gnus-summary-article-subject (&optional number)
2600   "Return current subject string or nil if nothing."
2601   `(let ((headers
2602           ,(if number
2603                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2604              '(gnus-data-header (assq (gnus-summary-article-number)
2605                                       gnus-newsgroup-data)))))
2606      (and headers
2607           (vectorp headers)
2608           (mail-header-subject headers))))
2609
2610 (defmacro gnus-summary-article-score (&optional number)
2611   "Return current article score."
2612   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2613                   gnus-newsgroup-scored))
2614        gnus-summary-default-score 0))
2615
2616 (defun gnus-summary-article-children (&optional number)
2617   "Return a list of article numbers that are children of article NUMBER."
2618   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2619          (level (gnus-data-level (car data)))
2620          l children)
2621     (while (and (setq data (cdr data))
2622                 (> (setq l (gnus-data-level (car data))) level))
2623       (and (= (1+ level) l)
2624            (push (gnus-data-number (car data))
2625                  children)))
2626     (nreverse children)))
2627
2628 (defun gnus-summary-article-parent (&optional number)
2629   "Return the article number of the parent of article NUMBER."
2630   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2631                                     (gnus-data-list t)))
2632          (level (gnus-data-level (car data))))
2633     (if (zerop level)
2634         ()                              ; This is a root.
2635       ;; We search until we find an article with a level less than
2636       ;; this one.  That function has to be the parent.
2637       (while (and (setq data (cdr data))
2638                   (not (< (gnus-data-level (car data)) level))))
2639       (and data (gnus-data-number (car data))))))
2640
2641 (defun gnus-unread-mark-p (mark)
2642   "Say whether MARK is the unread mark."
2643   (= mark gnus-unread-mark))
2644
2645 (defun gnus-read-mark-p (mark)
2646   "Say whether MARK is one of the marks that mark as read.
2647 This is all marks except unread, ticked, dormant, and expirable."
2648   (not (or (= mark gnus-unread-mark)
2649            (= mark gnus-ticked-mark)
2650            (= mark gnus-dormant-mark)
2651            (= mark gnus-expirable-mark))))
2652
2653 (defmacro gnus-article-mark (number)
2654   "Return the MARK of article NUMBER.
2655 This macro should only be used when computing the mark the \"first\"
2656 time; i.e., when generating the summary lines.  After that,
2657 `gnus-summary-article-mark' should be used to examine the
2658 marks of articles."
2659   `(cond
2660     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2661     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2662     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2663     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2664     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2665     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2666     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2667     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2668            gnus-ancient-mark))))
2669
2670 ;; Saving hidden threads.
2671
2672 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2673 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2674
2675 (defmacro gnus-save-hidden-threads (&rest forms)
2676   "Save hidden threads, eval FORMS, and restore the hidden threads."
2677   (let ((config (make-symbol "config")))
2678     `(let ((,config (gnus-hidden-threads-configuration)))
2679        (unwind-protect
2680            (save-excursion
2681              ,@forms)
2682          (gnus-restore-hidden-threads-configuration ,config)))))
2683
2684 (defun gnus-data-compute-positions ()
2685   "Compute the positions of all articles."
2686   (setq gnus-newsgroup-data-reverse nil)
2687   (let ((data gnus-newsgroup-data))
2688     (save-excursion
2689       (gnus-save-hidden-threads
2690         (gnus-summary-show-all-threads)
2691         (goto-char (point-min))
2692         (while data
2693           (while (get-text-property (point) 'gnus-intangible)
2694             (forward-line 1))
2695           (gnus-data-set-pos (car data) (+ (point) 3))
2696           (setq data (cdr data))
2697           (forward-line 1))))))
2698
2699 (defun gnus-hidden-threads-configuration ()
2700   "Return the current hidden threads configuration."
2701   (save-excursion
2702     (let (config)
2703       (goto-char (point-min))
2704       (while (search-forward "\r" nil t)
2705         (push (1- (point)) config))
2706       config)))
2707
2708 (defun gnus-restore-hidden-threads-configuration (config)
2709   "Restore hidden threads configuration from CONFIG."
2710   (save-excursion
2711     (let (point buffer-read-only)
2712       (while (setq point (pop config))
2713         (when (and (< point (point-max))
2714                    (goto-char point)
2715                    (eq (char-after) ?\n))
2716           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2717
2718 ;; Various summary mode internalish functions.
2719
2720 (defun gnus-mouse-pick-article (e)
2721   (interactive "e")
2722   (mouse-set-point e)
2723   (gnus-summary-next-page nil t))
2724
2725 (defun gnus-summary-set-display-table ()
2726   "Change the display table.
2727 Odd characters have a tendency to mess
2728 up nicely formatted displays - we make all possible glyphs
2729 display only a single character."
2730
2731   ;; We start from the standard display table, if any.
2732   (let ((table (or (copy-sequence standard-display-table)
2733                    (make-display-table)))
2734         (i 32))
2735     ;; Nix out all the control chars...
2736     (while (>= (setq i (1- i)) 0)
2737       (aset table i [??]))
2738     ;; ... but not newline and cr, of course.  (cr is necessary for the
2739     ;; selective display).
2740     (aset table ?\n nil)
2741     (aset table ?\r nil)
2742     ;; We keep TAB as well.
2743     (aset table ?\t nil)
2744     ;; We nix out any glyphs over 126 that are not set already.
2745     (let ((i 256))
2746       (while (>= (setq i (1- i)) 127)
2747         ;; Only modify if the entry is nil.
2748         (unless (aref table i)
2749           (aset table i [??]))))
2750     (setq buffer-display-table table)))
2751
2752 (defun gnus-summary-set-article-display-arrow (pos)
2753   "Update the overlay arrow to point to line at position POS."
2754   (when (and gnus-summary-display-arrow
2755              (boundp 'overlay-arrow-position)
2756              (boundp 'overlay-arrow-string))
2757     (save-excursion
2758       (goto-char pos)
2759       (beginning-of-line)
2760       (unless overlay-arrow-position
2761         (setq overlay-arrow-position (make-marker)))
2762       (setq overlay-arrow-string "=>"
2763             overlay-arrow-position (set-marker overlay-arrow-position
2764                                                (point)
2765                                                (current-buffer))))))
2766
2767 (defun gnus-summary-buffer-name (group)
2768   "Return the summary buffer name of GROUP."
2769   (concat "*Summary " group "*"))
2770
2771 (defun gnus-summary-setup-buffer (group)
2772   "Initialize summary buffer."
2773   (let ((buffer (gnus-summary-buffer-name group)))
2774     (if (get-buffer buffer)
2775         (progn
2776           (set-buffer buffer)
2777           (setq gnus-summary-buffer (current-buffer))
2778           (not gnus-newsgroup-prepared))
2779       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2780       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2781       (gnus-summary-mode group)
2782       (when gnus-carpal
2783         (gnus-carpal-setup-buffer 'summary))
2784       (unless gnus-single-article-buffer
2785         (make-local-variable 'gnus-article-buffer)
2786         (make-local-variable 'gnus-article-current)
2787         (make-local-variable 'gnus-original-article-buffer))
2788       (setq gnus-newsgroup-name group)
2789       ;; Set any local variables in the group parameters.
2790       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2791       t)))
2792
2793 (defun gnus-set-global-variables ()
2794   "Set the global equivalents of the buffer-local variables.
2795 They are set to the latest values they had.  These reflect the summary
2796 buffer that was in action when the last article was fetched."
2797   (when (eq major-mode 'gnus-summary-mode)
2798     (setq gnus-summary-buffer (current-buffer))
2799     (let ((name gnus-newsgroup-name)
2800           (marked gnus-newsgroup-marked)
2801           (unread gnus-newsgroup-unreads)
2802           (headers gnus-current-headers)
2803           (data gnus-newsgroup-data)
2804           (summary gnus-summary-buffer)
2805           (article-buffer gnus-article-buffer)
2806           (original gnus-original-article-buffer)
2807           (gac gnus-article-current)
2808           (reffed gnus-reffed-article-number)
2809           (score-file gnus-current-score-file)
2810           (default-charset gnus-newsgroup-charset)
2811           vlist)
2812       (let ((locals gnus-newsgroup-variables))
2813         (while locals
2814           (if (consp (car locals))
2815               (push (eval (caar locals)) vlist)
2816             (push (eval (car locals)) vlist))
2817           (setq locals (cdr locals)))
2818         (setq vlist (nreverse vlist)))
2819       (save-excursion
2820         (set-buffer gnus-group-buffer)
2821         (setq gnus-newsgroup-name name
2822               gnus-newsgroup-marked marked
2823               gnus-newsgroup-unreads unread
2824               gnus-current-headers headers
2825               gnus-newsgroup-data data
2826               gnus-article-current gac
2827               gnus-summary-buffer summary
2828               gnus-article-buffer article-buffer
2829               gnus-original-article-buffer original
2830               gnus-reffed-article-number reffed
2831               gnus-current-score-file score-file
2832               gnus-newsgroup-charset default-charset)
2833         (let ((locals gnus-newsgroup-variables))
2834           (while locals
2835             (if (consp (car locals))
2836                 (set (caar locals) (pop vlist))
2837               (set (car locals) (pop vlist)))
2838             (setq locals (cdr locals))))
2839         ;; The article buffer also has local variables.
2840         (when (gnus-buffer-live-p gnus-article-buffer)
2841           (set-buffer gnus-article-buffer)
2842           (setq gnus-summary-buffer summary))))))
2843
2844 (defun gnus-summary-article-unread-p (article)
2845   "Say whether ARTICLE is unread or not."
2846   (memq article gnus-newsgroup-unreads))
2847
2848 (defun gnus-summary-first-article-p (&optional article)
2849   "Return whether ARTICLE is the first article in the buffer."
2850   (if (not (setq article (or article (gnus-summary-article-number))))
2851       nil
2852     (eq article (caar gnus-newsgroup-data))))
2853
2854 (defun gnus-summary-last-article-p (&optional article)
2855   "Return whether ARTICLE is the last article in the buffer."
2856   (if (not (setq article (or article (gnus-summary-article-number))))
2857       ;; All non-existent numbers are the last article.  :-)
2858       t
2859     (not (cdr (gnus-data-find-list article)))))
2860
2861 (defun gnus-make-thread-indent-array ()
2862   (let ((n 200))
2863     (unless (and gnus-thread-indent-array
2864                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2865       (setq gnus-thread-indent-array (make-vector 201 "")
2866             gnus-thread-indent-array-level gnus-thread-indent-level)
2867       (while (>= n 0)
2868         (aset gnus-thread-indent-array n
2869               (make-string (* n gnus-thread-indent-level) ? ))
2870         (setq n (1- n))))))
2871
2872 (defun gnus-update-summary-mark-positions ()
2873   "Compute where the summary marks are to go."
2874   (save-excursion
2875     (when (gnus-buffer-exists-p gnus-summary-buffer)
2876       (set-buffer gnus-summary-buffer))
2877     (let ((gnus-replied-mark 129)
2878           (gnus-score-below-mark 130)
2879           (gnus-score-over-mark 130)
2880           (gnus-download-mark 131)
2881           (spec gnus-summary-line-format-spec)
2882           gnus-visual pos)
2883       (save-excursion
2884         (gnus-set-work-buffer)
2885         (let ((gnus-summary-line-format-spec spec)
2886               (gnus-newsgroup-downloadable '((0 . t))))
2887           (gnus-summary-insert-line
2888            (make-full-mail-header 0 "" "nobody"
2889                                   "05 Apr 2001 23:33:09 +0400"
2890                                   "" "" 0 0 "" nil)
2891            0 nil 128 t nil "" nil 1)
2892           (goto-char (point-min))
2893           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2894                                              (- (point) 2)))))
2895           (goto-char (point-min))
2896           (push (cons 'replied (and (search-forward "\201" nil t)
2897                                     (- (point) 2)))
2898                 pos)
2899           (goto-char (point-min))
2900           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2901                 pos)
2902           (goto-char (point-min))
2903           (push (cons 'download
2904                       (and (search-forward "\203" nil t) (- (point) 2)))
2905                 pos)))
2906       (setq gnus-summary-mark-positions pos))))
2907
2908 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2909   "Insert a dummy root in the summary buffer."
2910   (beginning-of-line)
2911   (gnus-add-text-properties
2912    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2913    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2914
2915 (defun gnus-summary-extract-address-component (from)
2916   (or (car (funcall gnus-extract-address-components from))
2917       from))
2918
2919 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2920   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
2921                                 default-mime-charset)))
2922     ;; Is it really necessary to do this next part for each summary line?
2923     ;; Luckily, doesn't seem to slow things down much.
2924     (or
2925      (and gnus-ignored-from-addresses
2926           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2927           (let ((extra-headers (mail-header-extra header))
2928                 to
2929                 newsgroups)
2930             (cond
2931              ((setq to (cdr (assq 'To extra-headers)))
2932               (concat "-> "
2933                       (gnus-summary-extract-address-component
2934                        (funcall gnus-decode-encoded-word-function to))))
2935              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2936               (concat "=> " newsgroups)))))
2937      (gnus-summary-extract-address-component gnus-tmp-from))))
2938
2939 (defun gnus-summary-insert-line (gnus-tmp-header
2940                                  gnus-tmp-level gnus-tmp-current
2941                                  gnus-tmp-unread gnus-tmp-replied
2942                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2943                                  &optional gnus-tmp-dummy gnus-tmp-score
2944                                  gnus-tmp-process)
2945   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2946          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2947          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2948          (gnus-tmp-score-char
2949           (if (or (null gnus-summary-default-score)
2950                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2951                       gnus-summary-zcore-fuzz))
2952               ?\ ;;;Whitespace
2953             (if (< gnus-tmp-score gnus-summary-default-score)
2954                 gnus-score-below-mark gnus-score-over-mark)))
2955          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2956          (gnus-tmp-replied
2957           (cond (gnus-tmp-process gnus-process-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2959                  gnus-cached-mark)
2960                 (gnus-tmp-replied gnus-replied-mark)
2961                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2962                  gnus-forwarded-mark)
2963                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2964                  gnus-saved-mark)
2965                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2966                  gnus-recent-mark)
2967                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2968                  gnus-unseen-mark)
2969                 (t gnus-no-mark)))
2970          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2971          (gnus-tmp-name
2972           (cond
2973            ((string-match "<[^>]+> *$" gnus-tmp-from)
2974             (let ((beg (match-beginning 0)))
2975               (or (and (string-match "^\".+\"" gnus-tmp-from)
2976                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2977                   (substring gnus-tmp-from 0 beg))))
2978            ((string-match "(.+)" gnus-tmp-from)
2979             (substring gnus-tmp-from
2980                        (1+ (match-beginning 0)) (1- (match-end 0))))
2981            (t gnus-tmp-from)))
2982          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2983          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2984          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2985          (buffer-read-only nil))
2986     (when (string= gnus-tmp-name "")
2987       (setq gnus-tmp-name gnus-tmp-from))
2988     (unless (numberp gnus-tmp-lines)
2989       (setq gnus-tmp-lines -1))
2990     (if (= gnus-tmp-lines -1)
2991         (setq gnus-tmp-lines "?")
2992       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2993     (gnus-put-text-property-excluding-characters-with-faces
2994      (point)
2995      (progn (eval gnus-summary-line-format-spec) (point))
2996      'gnus-number gnus-tmp-number)
2997     (when (gnus-visual-p 'summary-highlight 'highlight)
2998       (forward-line -1)
2999       (gnus-run-hooks 'gnus-summary-update-hook)
3000       (forward-line 1))))
3001
3002 (defun gnus-summary-update-line (&optional dont-update)
3003   "Update summary line after change."
3004   (when (and gnus-summary-default-score
3005              (not gnus-summary-inhibit-highlight))
3006     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3007            (article (gnus-summary-article-number))
3008            (score (gnus-summary-article-score article)))
3009       (unless dont-update
3010         (if (and gnus-summary-mark-below
3011                  (< (gnus-summary-article-score)
3012                     gnus-summary-mark-below))
3013             ;; This article has a low score, so we mark it as read.
3014             (when (memq article gnus-newsgroup-unreads)
3015               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3016           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3017             ;; This article was previously marked as read on account
3018             ;; of a low score, but now it has risen, so we mark it as
3019             ;; unread.
3020             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3021         (gnus-summary-update-mark
3022          (if (or (null gnus-summary-default-score)
3023                  (<= (abs (- score gnus-summary-default-score))
3024                      gnus-summary-zcore-fuzz))
3025              ?\ ;;;Whitespace
3026            (if (< score gnus-summary-default-score)
3027                gnus-score-below-mark gnus-score-over-mark))
3028          'score))
3029       ;; Do visual highlighting.
3030       (when (gnus-visual-p 'summary-highlight 'highlight)
3031         (gnus-run-hooks 'gnus-summary-update-hook)))))
3032
3033 (defvar gnus-tmp-new-adopts nil)
3034
3035 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3036   "Return the number of articles in THREAD.
3037 This may be 0 in some cases -- if none of the articles in
3038 the thread are to be displayed."
3039   (let* ((number
3040           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3041           (cond
3042            ((not (listp thread))
3043             1)
3044            ((and (consp thread) (cdr thread))
3045             (apply
3046              '+ 1 (mapcar
3047                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3048            ((null thread)
3049             1)
3050            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3051             1)
3052            (t 0))))
3053     (when (and level (zerop level) gnus-tmp-new-adopts)
3054       (incf number
3055             (apply '+ (mapcar
3056                        'gnus-summary-number-of-articles-in-thread
3057                        gnus-tmp-new-adopts))))
3058     (if char
3059         (if (> number 1) gnus-not-empty-thread-mark
3060           gnus-empty-thread-mark)
3061       number)))
3062
3063 (defun gnus-summary-set-local-parameters (group)
3064   "Go through the local params of GROUP and set all variable specs in that list."
3065   (let ((params (gnus-group-find-parameter group))
3066         (vars '(quit-config))           ; Ignore quit-config.
3067         elem)
3068     (while params
3069       (setq elem (car params)
3070             params (cdr params))
3071       (and (consp elem)                 ; Has to be a cons.
3072            (consp (cdr elem))           ; The cdr has to be a list.
3073            (symbolp (car elem))         ; Has to be a symbol in there.
3074            (not (memq (car elem) vars))
3075            (ignore-errors               ; So we set it.
3076              (push (car elem) vars)
3077              (make-local-variable (car elem))
3078              (set (car elem) (eval (nth 1 elem))))))))
3079
3080 (defun gnus-summary-read-group (group &optional show-all no-article
3081                                       kill-buffer no-display backward
3082                                       select-articles)
3083   "Start reading news in newsgroup GROUP.
3084 If SHOW-ALL is non-nil, already read articles are also listed.
3085 If NO-ARTICLE is non-nil, no article is selected initially.
3086 If NO-DISPLAY, don't generate a summary buffer."
3087   (let (result)
3088     (while (and group
3089                 (null (setq result
3090                             (let ((gnus-auto-select-next nil))
3091                               (or (gnus-summary-read-group-1
3092                                    group show-all no-article
3093                                    kill-buffer no-display
3094                                    select-articles)
3095                                   (setq show-all nil
3096                                         select-articles nil)))))
3097                 (eq gnus-auto-select-next 'quietly))
3098       (set-buffer gnus-group-buffer)
3099       ;; The entry function called above goes to the next
3100       ;; group automatically, so we go two groups back
3101       ;; if we are searching for the previous group.
3102       (when backward
3103         (gnus-group-prev-unread-group 2))
3104       (if (not (equal group (gnus-group-group-name)))
3105           (setq group (gnus-group-group-name))
3106         (setq group nil)))
3107     result))
3108
3109 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3110   "Directly jump to the other GROUP from summary buffer.
3111 If SHOW-ALL is non-nil, already read articles are also listed."
3112   (interactive
3113    (if (eq gnus-summary-buffer (current-buffer))
3114        (list (completing-read
3115               "Group: " gnus-active-hashtb nil t
3116               (when (and gnus-newsgroup-name
3117                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3118                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3119               'gnus-group-history)
3120              current-prefix-arg)
3121      (error "%s must be invoked from a gnus summary buffer." this-command)))
3122   (unless (or (zerop (length group))
3123               (and gnus-newsgroup-name
3124                    (string-equal gnus-newsgroup-name group)))
3125     (gnus-summary-exit)
3126     (gnus-summary-read-group group show-all
3127                              gnus-dont-select-after-jump-to-other-group)))
3128
3129 (defun gnus-summary-read-group-1 (group show-all no-article
3130                                         kill-buffer no-display
3131                                         &optional select-articles)
3132   ;; Killed foreign groups can't be entered.
3133   ;;  (when (and (not (gnus-group-native-p group))
3134   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3135   ;;    (error "Dead non-native groups can't be entered"))
3136   (gnus-message 5 "Retrieving newsgroup: %s..."
3137                 (gnus-group-decoded-name group))
3138   (let* ((new-group (gnus-summary-setup-buffer group))
3139          (quit-config (gnus-group-quit-config group))
3140          (did-select (and new-group (gnus-select-newsgroup
3141                                      group show-all select-articles))))
3142     (cond
3143      ;; This summary buffer exists already, so we just select it.
3144      ((not new-group)
3145       (gnus-set-global-variables)
3146       (when kill-buffer
3147         (gnus-kill-or-deaden-summary kill-buffer))
3148       (gnus-configure-windows 'summary 'force)
3149       (gnus-set-mode-line 'summary)
3150       (gnus-summary-position-point)
3151       (message "")
3152       t)
3153      ;; We couldn't select this group.
3154      ((null did-select)
3155       (when (and (eq major-mode 'gnus-summary-mode)
3156                  (not (equal (current-buffer) kill-buffer)))
3157         (kill-buffer (current-buffer))
3158         (if (not quit-config)
3159             (progn
3160               ;; Update the info -- marks might need to be removed,
3161               ;; for instance.
3162               (gnus-summary-update-info)
3163               (set-buffer gnus-group-buffer)
3164               (gnus-group-jump-to-group group)
3165               (gnus-group-next-unread-group 1))
3166           (gnus-handle-ephemeral-exit quit-config)))
3167       (let ((grpinfo (gnus-get-info group)))
3168         (if (null (gnus-info-read grpinfo))
3169             (gnus-message 3 "Group %s contains no messages"
3170                           (gnus-group-decoded-name group))
3171           (gnus-message 3 "Can't select group")))
3172       nil)
3173      ;; The user did a `C-g' while prompting for number of articles,
3174      ;; so we exit this group.
3175      ((eq did-select 'quit)
3176       (and (eq major-mode 'gnus-summary-mode)
3177            (not (equal (current-buffer) kill-buffer))
3178            (kill-buffer (current-buffer)))
3179       (when kill-buffer
3180         (gnus-kill-or-deaden-summary kill-buffer))
3181       (if (not quit-config)
3182           (progn
3183             (set-buffer gnus-group-buffer)
3184             (gnus-group-jump-to-group group)
3185             (gnus-group-next-unread-group 1)
3186             (gnus-configure-windows 'group 'force))
3187         (gnus-handle-ephemeral-exit quit-config))
3188       ;; Finally signal the quit.
3189       (signal 'quit nil))
3190      ;; The group was successfully selected.
3191      (t
3192       (gnus-set-global-variables)
3193       ;; Save the active value in effect when the group was entered.
3194       (setq gnus-newsgroup-active
3195             (gnus-copy-sequence
3196              (gnus-active gnus-newsgroup-name)))
3197       ;; You can change the summary buffer in some way with this hook.
3198       (gnus-run-hooks 'gnus-select-group-hook)
3199       (gnus-update-format-specifications
3200        nil 'summary 'summary-mode 'summary-dummy)
3201       (gnus-update-summary-mark-positions)
3202       ;; Do score processing.
3203       (when gnus-use-scoring
3204         (gnus-possibly-score-headers))
3205       ;; Check whether to fill in the gaps in the threads.
3206       (when gnus-build-sparse-threads
3207         (gnus-build-sparse-threads))
3208       ;; Find the initial limit.
3209       (if gnus-show-threads
3210           (if show-all
3211               (let ((gnus-newsgroup-dormant nil))
3212                 (gnus-summary-initial-limit show-all))
3213             (gnus-summary-initial-limit show-all))
3214         ;; When untreaded, all articles are always shown.
3215         (setq gnus-newsgroup-limit
3216               (mapcar
3217                (lambda (header) (mail-header-number header))
3218                gnus-newsgroup-headers)))
3219       ;; Generate the summary buffer.
3220       (unless no-display
3221         (gnus-summary-prepare))
3222       (when gnus-use-trees
3223         (gnus-tree-open group)
3224         (setq gnus-summary-highlight-line-function
3225               'gnus-tree-highlight-article))
3226       ;; If the summary buffer is empty, but there are some low-scored
3227       ;; articles or some excluded dormants, we include these in the
3228       ;; buffer.
3229       (when (and (zerop (buffer-size))
3230                  (not no-display))
3231         (cond (gnus-newsgroup-dormant
3232                (gnus-summary-limit-include-dormant))
3233               ((and gnus-newsgroup-scored show-all)
3234                (gnus-summary-limit-include-expunged t))))
3235       ;; Function `gnus-apply-kill-file' must be called in this hook.
3236       (gnus-run-hooks 'gnus-apply-kill-hook)
3237       (if (and (zerop (buffer-size))
3238                (not no-display))
3239           (progn
3240             ;; This newsgroup is empty.
3241             (gnus-summary-catchup-and-exit nil t)
3242             (gnus-message 6 "No unread news")
3243             (when kill-buffer
3244               (gnus-kill-or-deaden-summary kill-buffer))
3245             ;; Return nil from this function.
3246             nil)
3247         ;; Hide conversation thread subtrees.  We cannot do this in
3248         ;; gnus-summary-prepare-hook since kill processing may not
3249         ;; work with hidden articles.
3250         (and gnus-show-threads
3251              gnus-thread-hide-subtree
3252              (gnus-summary-hide-all-threads))
3253         (when kill-buffer
3254           (gnus-kill-or-deaden-summary kill-buffer))
3255         ;; Show first unread article if requested.
3256         (if (and (not no-article)
3257                  (not no-display)
3258                  gnus-newsgroup-unreads
3259                  gnus-auto-select-first)
3260             (progn
3261               (gnus-configure-windows 'summary)
3262               (cond
3263                ((eq gnus-auto-select-first 'best)
3264                 (gnus-summary-best-unread-article))
3265                ((eq gnus-auto-select-first t)
3266                 (gnus-summary-first-unread-article))
3267                ((gnus-functionp gnus-auto-select-first)
3268                 (funcall gnus-auto-select-first))))
3269           ;; Don't select any articles, just move point to the first
3270           ;; article in the group.
3271           (goto-char (point-min))
3272           (gnus-summary-position-point)
3273           (gnus-configure-windows 'summary 'force)
3274           (gnus-set-mode-line 'summary))
3275         (when (get-buffer-window gnus-group-buffer t)
3276           ;; Gotta use windows, because recenter does weird stuff if
3277           ;; the current buffer ain't the displayed window.
3278           (let ((owin (selected-window)))
3279             (select-window (get-buffer-window gnus-group-buffer t))
3280             (when (gnus-group-goto-group group)
3281               (recenter))
3282             (select-window owin)))
3283         ;; Mark this buffer as "prepared".
3284         (setq gnus-newsgroup-prepared t)
3285         (gnus-run-hooks 'gnus-summary-prepared-hook)
3286         t)))))
3287
3288 (defun gnus-summary-prepare ()
3289   "Generate the summary buffer."
3290   (interactive)
3291   (let ((buffer-read-only nil))
3292     (erase-buffer)
3293     (setq gnus-newsgroup-data nil
3294           gnus-newsgroup-data-reverse nil)
3295     (gnus-run-hooks 'gnus-summary-generate-hook)
3296     ;; Generate the buffer, either with threads or without.
3297     (when gnus-newsgroup-headers
3298       (gnus-summary-prepare-threads
3299        (if gnus-show-threads
3300            (gnus-sort-gathered-threads
3301             (funcall gnus-summary-thread-gathering-function
3302                      (gnus-sort-threads
3303                       (gnus-cut-threads (gnus-make-threads)))))
3304          ;; Unthreaded display.
3305          (gnus-sort-articles gnus-newsgroup-headers))))
3306     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3307     ;; Call hooks for modifying summary buffer.
3308     (goto-char (point-min))
3309     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3310
3311 (defsubst gnus-general-simplify-subject (subject)
3312   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3313   (setq subject
3314         (cond
3315          ;; Truncate the subject.
3316          (gnus-simplify-subject-functions
3317           (gnus-map-function gnus-simplify-subject-functions subject))
3318          ((numberp gnus-summary-gather-subject-limit)
3319           (setq subject (gnus-simplify-subject-re subject))
3320           (if (> (length subject) gnus-summary-gather-subject-limit)
3321               (substring subject 0 gnus-summary-gather-subject-limit)
3322             subject))
3323          ;; Fuzzily simplify it.
3324          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3325           (gnus-simplify-subject-fuzzy subject))
3326          ;; Just remove the leading "Re:".
3327          (t
3328           (gnus-simplify-subject-re subject))))
3329
3330   (if (and gnus-summary-gather-exclude-subject
3331            (string-match gnus-summary-gather-exclude-subject subject))
3332       nil                               ; This article shouldn't be gathered
3333     subject))
3334
3335 (defun gnus-summary-simplify-subject-query ()
3336   "Query where the respool algorithm would put this article."
3337   (interactive)
3338   (gnus-summary-select-article)
3339   (message "%s"
3340            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3341
3342 (defun gnus-gather-threads-by-subject (threads)
3343   "Gather threads by looking at Subject headers."
3344   (if (not gnus-summary-make-false-root)
3345       threads
3346     (let ((hashtb (gnus-make-hashtable 1024))
3347           (prev threads)
3348           (result threads)
3349           subject hthread whole-subject)
3350       (while threads
3351         (setq subject (gnus-general-simplify-subject
3352                        (setq whole-subject (mail-header-subject
3353                                             (caar threads)))))
3354         (when subject
3355           (if (setq hthread (gnus-gethash subject hashtb))
3356               (progn
3357                 ;; We enter a dummy root into the thread, if we
3358                 ;; haven't done that already.
3359                 (unless (stringp (caar hthread))
3360                   (setcar hthread (list whole-subject (car hthread))))
3361                 ;; We add this new gathered thread to this gathered
3362                 ;; thread.
3363                 (setcdr (car hthread)
3364                         (nconc (cdar hthread) (list (car threads))))
3365                 ;; Remove it from the list of threads.
3366                 (setcdr prev (cdr threads))
3367                 (setq threads prev))
3368             ;; Enter this thread into the hash table.
3369             (gnus-sethash subject threads hashtb)))
3370         (setq prev threads)
3371         (setq threads (cdr threads)))
3372       result)))
3373
3374 (defun gnus-gather-threads-by-references (threads)
3375   "Gather threads by looking at References headers."
3376   (let ((idhashtb (gnus-make-hashtable 1024))
3377         (thhashtb (gnus-make-hashtable 1024))
3378         (prev threads)
3379         (result threads)
3380         ids references id gthread gid entered ref)
3381     (while threads
3382       (when (setq references (mail-header-references (caar threads)))
3383         (setq id (mail-header-id (caar threads))
3384               ids (gnus-split-references references)
3385               entered nil)
3386         (while (setq ref (pop ids))
3387           (setq ids (delete ref ids))
3388           (if (not (setq gid (gnus-gethash ref idhashtb)))
3389               (progn
3390                 (gnus-sethash ref id idhashtb)
3391                 (gnus-sethash id threads thhashtb))
3392             (setq gthread (gnus-gethash gid thhashtb))
3393             (unless entered
3394               ;; We enter a dummy root into the thread, if we
3395               ;; haven't done that already.
3396               (unless (stringp (caar gthread))
3397                 (setcar gthread (list (mail-header-subject (caar gthread))
3398                                       (car gthread))))
3399               ;; We add this new gathered thread to this gathered
3400               ;; thread.
3401               (setcdr (car gthread)
3402                       (nconc (cdar gthread) (list (car threads)))))
3403             ;; Add it into the thread hash table.
3404             (gnus-sethash id gthread thhashtb)
3405             (setq entered t)
3406             ;; Remove it from the list of threads.
3407             (setcdr prev (cdr threads))
3408             (setq threads prev))))
3409       (setq prev threads)
3410       (setq threads (cdr threads)))
3411     result))
3412
3413 (defun gnus-sort-gathered-threads (threads)
3414   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3415   (let ((result threads))
3416     (while threads
3417       (when (stringp (caar threads))
3418         (setcdr (car threads)
3419                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3420       (setq threads (cdr threads)))
3421     result))
3422
3423 (defun gnus-thread-loop-p (root thread)
3424   "Say whether ROOT is in THREAD."
3425   (let ((stack (list thread))
3426         (infloop 0)
3427         th)
3428     (while (setq thread (pop stack))
3429       (setq th (cdr thread))
3430       (while (and th
3431                   (not (eq (caar th) root)))
3432         (pop th))
3433       (if th
3434           ;; We have found a loop.
3435           (let (ref-dep)
3436             (setcdr thread (delq (car th) (cdr thread)))
3437             (if (boundp (setq ref-dep (intern "none"
3438                                               gnus-newsgroup-dependencies)))
3439                 (setcdr (symbol-value ref-dep)
3440                         (nconc (cdr (symbol-value ref-dep))
3441                                (list (car th))))
3442               (set ref-dep (list nil (car th))))
3443             (setq infloop 1
3444                   stack nil))
3445         ;; Push all the subthreads onto the stack.
3446         (push (cdr thread) stack)))
3447     infloop))
3448
3449 (defun gnus-make-threads ()
3450   "Go through the dependency hashtb and find the roots.  Return all threads."
3451   (let (threads)
3452     (while (catch 'infloop
3453              (mapatoms
3454               (lambda (refs)
3455                 ;; Deal with self-referencing References loops.
3456                 (when (and (car (symbol-value refs))
3457                            (not (zerop
3458                                  (apply
3459                                   '+
3460                                   (mapcar
3461                                    (lambda (thread)
3462                                      (gnus-thread-loop-p
3463                                       (car (symbol-value refs)) thread))
3464                                    (cdr (symbol-value refs)))))))
3465                   (setq threads nil)
3466                   (throw 'infloop t))
3467                 (unless (car (symbol-value refs))
3468                   ;; These threads do not refer back to any other articles,
3469                   ;; so they're roots.
3470                   (setq threads (append (cdr (symbol-value refs)) threads))))
3471               gnus-newsgroup-dependencies)))
3472     threads))
3473
3474 ;; Build the thread tree.
3475 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3476   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3477
3478 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3479 if it was already present.
3480
3481 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3482 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3483 Message-IDs will be renamed be renamed to a unique Message-ID before
3484 being entered.
3485
3486 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3487   (let* ((id (mail-header-id header))
3488          (id-dep (and id (intern id dependencies)))
3489          ref ref-dep ref-header)
3490     ;; Enter this `header' in the `dependencies' table.
3491     (cond
3492      ((not id-dep)
3493       (setq header nil))
3494      ;; The first two cases do the normal part: enter a new `header'
3495      ;; in the `dependencies' table.
3496      ((not (boundp id-dep))
3497       (set id-dep (list header)))
3498      ((null (car (symbol-value id-dep)))
3499       (setcar (symbol-value id-dep) header))
3500
3501      ;; From here the `header' was already present in the
3502      ;; `dependencies' table.
3503      (force-new
3504       ;; Overrides an existing entry;
3505       ;; just set the header part of the entry.
3506       (setcar (symbol-value id-dep) header))
3507
3508      ;; Renames the existing `header' to a unique Message-ID.
3509      ((not gnus-summary-ignore-duplicates)
3510       ;; An article with this Message-ID has already been seen.
3511       ;; We rename the Message-ID.
3512       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3513            (list header))
3514       (mail-header-set-id header id))
3515
3516      ;; The last case ignores an existing entry, except it adds any
3517      ;; additional Xrefs (in case the two articles came from different
3518      ;; servers.
3519      ;; Also sets `header' to `nil' meaning that the `dependencies'
3520      ;; table was *not* modified.
3521      (t
3522       (mail-header-set-xref
3523        (car (symbol-value id-dep))
3524        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3525                    "")
3526                (or (mail-header-xref header) "")))
3527       (setq header nil)))
3528
3529     (when header
3530       ;; First check if that we are not creating a References loop.
3531       (setq ref (gnus-parent-id (mail-header-references header)))
3532       (while (and ref
3533                   (setq ref-dep (intern-soft ref dependencies))
3534                   (boundp ref-dep)
3535                   (setq ref-header (car (symbol-value ref-dep))))
3536         (if (string= id ref)
3537             ;; Yuk!  This is a reference loop.  Make the article be a
3538             ;; root article.
3539             (progn
3540               (mail-header-set-references (car (symbol-value id-dep)) "none")
3541               (setq ref nil))
3542           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3543       (setq ref (gnus-parent-id (mail-header-references header)))
3544       (setq ref-dep (intern (or ref "none") dependencies))
3545       (if (boundp ref-dep)
3546           (setcdr (symbol-value ref-dep)
3547                   (nconc (cdr (symbol-value ref-dep))
3548                          (list (symbol-value id-dep))))
3549         (set ref-dep (list nil (symbol-value id-dep)))))
3550     header))
3551
3552 (defun gnus-build-sparse-threads ()
3553   (let ((headers gnus-newsgroup-headers)
3554         (mail-parse-charset gnus-newsgroup-charset)
3555         (gnus-summary-ignore-duplicates t)
3556         header references generation relations
3557         subject child end new-child date)
3558     ;; First we create an alist of generations/relations, where
3559     ;; generations is how much we trust the relation, and the relation
3560     ;; is parent/child.
3561     (gnus-message 7 "Making sparse threads...")
3562     (save-excursion
3563       (nnheader-set-temp-buffer " *gnus sparse threads*")
3564       (while (setq header (pop headers))
3565         (when (and (setq references (mail-header-references header))
3566                    (not (string= references "")))
3567           (insert references)
3568           (setq child (mail-header-id header)
3569                 subject (mail-header-subject header)
3570                 date (mail-header-date header)
3571                 generation 0)
3572           (while (search-backward ">" nil t)
3573             (setq end (1+ (point)))
3574             (when (search-backward "<" nil t)
3575               (setq new-child (buffer-substring (point) end))
3576               (push (list (incf generation)
3577                           child (setq child new-child)
3578                           subject date)
3579                     relations)))
3580           (when child
3581             (push (list (1+ generation) child nil subject) relations))
3582           (erase-buffer)))
3583       (kill-buffer (current-buffer)))
3584     ;; Sort over trustworthiness.
3585     (mapcar
3586      (lambda (relation)
3587        (when (gnus-dependencies-add-header
3588               (make-full-mail-header-from-decoded-header
3589                gnus-reffed-article-number
3590                (nth 3 relation) "" (or (nth 4 relation) "")
3591                (nth 1 relation)
3592                (or (nth 2 relation) "") 0 0 "")
3593               gnus-newsgroup-dependencies nil)
3594          (push gnus-reffed-article-number gnus-newsgroup-limit)
3595          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3596          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3597                gnus-newsgroup-reads)
3598          (decf gnus-reffed-article-number)))
3599      (sort relations 'car-less-than-car))
3600     (gnus-message 7 "Making sparse threads...done")))
3601
3602 (defun gnus-build-old-threads ()
3603   ;; Look at all the articles that refer back to old articles, and
3604   ;; fetch the headers for the articles that aren't there.  This will
3605   ;; build complete threads - if the roots haven't been expired by the
3606   ;; server, that is.
3607   (let ((mail-parse-charset gnus-newsgroup-charset)
3608         id heads)
3609     (mapatoms
3610      (lambda (refs)
3611        (when (not (car (symbol-value refs)))
3612          (setq heads (cdr (symbol-value refs)))
3613          (while heads
3614            (if (memq (mail-header-number (caar heads))
3615                      gnus-newsgroup-dormant)
3616                (setq heads (cdr heads))
3617              (setq id (symbol-name refs))
3618              (while (and (setq id (gnus-build-get-header id))
3619                          (not (car (gnus-id-to-thread id)))))
3620              (setq heads nil)))))
3621      gnus-newsgroup-dependencies)))
3622
3623 ;; This function has to be called with point after the article number
3624 ;; on the beginning of the line.
3625 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3626   (let ((eol (gnus-point-at-eol))
3627         (buffer (current-buffer))
3628         header)
3629
3630     ;; overview: [num subject from date id refs chars lines misc]
3631     (unwind-protect
3632         (progn
3633           (narrow-to-region (point) eol)
3634           (unless (eobp)
3635             (forward-char))
3636
3637           (setq header
3638                 (make-full-mail-header
3639                  number                         ; number
3640                  (nnheader-nov-field)           ; subject
3641                  (nnheader-nov-field)           ; from
3642                  (nnheader-nov-field)           ; date
3643                  (nnheader-nov-read-message-id) ; id
3644                  (nnheader-nov-field)           ; refs
3645                  (nnheader-nov-read-integer)    ; chars
3646                  (nnheader-nov-read-integer)    ; lines
3647                  (unless (eobp)
3648                    (if (looking-at "Xref: ")
3649                        (goto-char (match-end 0)))
3650                    (nnheader-nov-field))        ; Xref
3651                  (nnheader-nov-parse-extra))))  ; extra
3652
3653       (widen))
3654
3655     (when gnus-alter-header-function
3656       (funcall gnus-alter-header-function header))
3657     (gnus-dependencies-add-header header dependencies force-new)))
3658
3659 (defun gnus-build-get-header (id)
3660   "Look through the buffer of NOV lines and find the header to ID.
3661 Enter this line into the dependencies hash table, and return
3662 the id of the parent article (if any)."
3663   (let ((deps gnus-newsgroup-dependencies)
3664         found header)
3665     (prog1
3666         (save-excursion
3667           (set-buffer nntp-server-buffer)
3668           (let ((case-fold-search nil))
3669             (goto-char (point-min))
3670             (while (and (not found)
3671                         (search-forward id nil t))
3672               (beginning-of-line)
3673               (setq found (looking-at
3674                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3675                                    (regexp-quote id))))
3676               (or found (beginning-of-line 2)))
3677             (when found
3678               (beginning-of-line)
3679               (and
3680                (setq header (gnus-nov-parse-line
3681                              (read (current-buffer)) deps))
3682                (gnus-parent-id (mail-header-references header))))))
3683       (when header
3684         (let ((number (mail-header-number header)))
3685           (push number gnus-newsgroup-limit)
3686           (push header gnus-newsgroup-headers)
3687           (if (memq number gnus-newsgroup-unselected)
3688               (progn
3689                 (push number gnus-newsgroup-unreads)
3690                 (setq gnus-newsgroup-unselected
3691                       (delq number gnus-newsgroup-unselected)))
3692             (push number gnus-newsgroup-ancient)))))))
3693
3694 (defun gnus-build-all-threads ()
3695   "Read all the headers."
3696   (let ((gnus-summary-ignore-duplicates t)
3697         (mail-parse-charset gnus-newsgroup-charset)
3698         (dependencies gnus-newsgroup-dependencies)
3699         header article)
3700     (save-excursion
3701       (set-buffer nntp-server-buffer)
3702       (let ((case-fold-search nil))
3703         (goto-char (point-min))
3704         (while (not (eobp))
3705           (ignore-errors
3706             (setq article (read (current-buffer))
3707                   header (gnus-nov-parse-line article dependencies)))
3708           (when header
3709             (save-excursion
3710               (set-buffer gnus-summary-buffer)
3711               (push header gnus-newsgroup-headers)
3712               (if (memq (setq article (mail-header-number header))
3713                         gnus-newsgroup-unselected)
3714                   (progn
3715                     (push article gnus-newsgroup-unreads)
3716                     (setq gnus-newsgroup-unselected
3717                           (delq article gnus-newsgroup-unselected)))
3718                 (push article gnus-newsgroup-ancient)))
3719             (forward-line 1)))))))
3720
3721 (defun gnus-summary-update-article-line (article header)
3722   "Update the line for ARTICLE using HEADERS."
3723   (let* ((id (mail-header-id header))
3724          (thread (gnus-id-to-thread id)))
3725     (unless thread
3726       (error "Article in no thread"))
3727     ;; Update the thread.
3728     (setcar thread header)
3729     (gnus-summary-goto-subject article)
3730     (let* ((datal (gnus-data-find-list article))
3731            (data (car datal))
3732            (length (when (cdr datal)
3733                      (- (gnus-data-pos data)
3734                         (gnus-data-pos (cadr datal)))))
3735            (buffer-read-only nil)
3736            (level (gnus-summary-thread-level)))
3737       (gnus-delete-line)
3738       (gnus-summary-insert-line
3739        header level nil (gnus-article-mark article)
3740        (memq article gnus-newsgroup-replied)
3741        (memq article gnus-newsgroup-expirable)
3742        ;; Only insert the Subject string when it's different
3743        ;; from the previous Subject string.
3744        (if (and
3745             gnus-show-threads
3746             (gnus-subject-equal
3747              (condition-case ()
3748                  (mail-header-subject
3749                   (gnus-data-header
3750                    (cadr
3751                     (gnus-data-find-list
3752                      article
3753                      (gnus-data-list t)))))
3754                ;; Error on the side of excessive subjects.
3755                (error ""))
3756              (mail-header-subject header)))
3757            ""
3758          (mail-header-subject header))
3759        nil (cdr (assq article gnus-newsgroup-scored))
3760        (memq article gnus-newsgroup-processable))
3761       (when length
3762         (gnus-data-update-list
3763          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3764
3765 (defun gnus-summary-update-article (article &optional iheader)
3766   "Update ARTICLE in the summary buffer."
3767   (set-buffer gnus-summary-buffer)
3768   (let* ((header (gnus-summary-article-header article))
3769          (id (mail-header-id header))
3770          (data (gnus-data-find article))
3771          (thread (gnus-id-to-thread id))
3772          (references (mail-header-references header))
3773          (parent
3774           (gnus-id-to-thread
3775            (or (gnus-parent-id
3776                 (when (and references
3777                            (not (equal "" references)))
3778                   references))
3779                "none")))
3780          (buffer-read-only nil)
3781          (old (car thread)))
3782     (when thread
3783       (unless iheader
3784         (setcar thread nil)
3785         (when parent
3786           (delq thread parent)))
3787       (if (gnus-summary-insert-subject id header)
3788           ;; Set the (possibly) new article number in the data structure.
3789           (gnus-data-set-number data (gnus-id-to-article id))
3790         (setcar thread old)
3791         nil))))
3792
3793 (defun gnus-rebuild-thread (id &optional line)
3794   "Rebuild the thread containing ID.
3795 If LINE, insert the rebuilt thread starting on line LINE."
3796   (let ((buffer-read-only nil)
3797         old-pos current thread data)
3798     (if (not gnus-show-threads)
3799         (setq thread (list (car (gnus-id-to-thread id))))
3800       ;; Get the thread this article is part of.
3801       (setq thread (gnus-remove-thread id)))
3802     (setq old-pos (gnus-point-at-bol))
3803     (setq current (save-excursion
3804                     (and (re-search-backward "[\r\n]" nil t)
3805                          (gnus-summary-article-number))))
3806     ;; If this is a gathered thread, we have to go some re-gathering.
3807     (when (stringp (car thread))
3808       (let ((subject (car thread))
3809             roots thr)
3810         (setq thread (cdr thread))
3811         (while thread
3812           (unless (memq (setq thr (gnus-id-to-thread
3813                                    (gnus-root-id
3814                                     (mail-header-id (caar thread)))))
3815                         roots)
3816             (push thr roots))
3817           (setq thread (cdr thread)))
3818         ;; We now have all (unique) roots.
3819         (if (= (length roots) 1)
3820             ;; All the loose roots are now one solid root.
3821             (setq thread (car roots))
3822           (setq thread (cons subject (gnus-sort-threads roots))))))
3823     (let (threads)
3824       ;; We then insert this thread into the summary buffer.
3825       (when line
3826         (goto-char (point-min))
3827         (forward-line (1- line)))
3828       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3829         (if gnus-show-threads
3830             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3831           (gnus-summary-prepare-unthreaded thread))
3832         (setq data (nreverse gnus-newsgroup-data))
3833         (setq threads gnus-newsgroup-threads))
3834       ;; We splice the new data into the data structure.
3835       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3836       ;;!!! then we want to insert at the beginning of the buffer.
3837       ;;!!! That happens to be true with Gnus now, but that may
3838       ;;!!! change in the future.  Perhaps.
3839       (gnus-data-enter-list
3840        (if line nil current) data (- (point) old-pos))
3841       (setq gnus-newsgroup-threads
3842             (nconc threads gnus-newsgroup-threads))
3843       (gnus-data-compute-positions))))
3844
3845 (defun gnus-number-to-header (number)
3846   "Return the header for article NUMBER."
3847   (let ((headers gnus-newsgroup-headers))
3848     (while (and headers
3849                 (not (= number (mail-header-number (car headers)))))
3850       (pop headers))
3851     (when headers
3852       (car headers))))
3853
3854 (defun gnus-parent-headers (in-headers &optional generation)
3855   "Return the headers of the GENERATIONeth parent of HEADERS."
3856   (unless generation
3857     (setq generation 1))
3858   (let ((parent t)
3859         (headers in-headers)
3860         references)
3861     (while (and parent
3862                 (not (zerop generation))
3863                 (setq references (mail-header-references headers)))
3864       (setq headers (if (and references
3865                              (setq parent (gnus-parent-id references)))
3866                         (car (gnus-id-to-thread parent))
3867                       nil))
3868       (decf generation))
3869     (and (not (eq headers in-headers))
3870          headers)))
3871
3872 (defun gnus-id-to-thread (id)
3873   "Return the (sub-)thread where ID appears."
3874   (gnus-gethash id gnus-newsgroup-dependencies))
3875
3876 (defun gnus-id-to-article (id)
3877   "Return the article number of ID."
3878   (let ((thread (gnus-id-to-thread id)))
3879     (when (and thread
3880                (car thread))
3881       (mail-header-number (car thread)))))
3882
3883 (defun gnus-id-to-header (id)
3884   "Return the article headers of ID."
3885   (car (gnus-id-to-thread id)))
3886
3887 (defun gnus-article-displayed-root-p (article)
3888   "Say whether ARTICLE is a root(ish) article."
3889   (let ((level (gnus-summary-thread-level article))
3890         (refs (mail-header-references  (gnus-summary-article-header article)))
3891         particle)
3892     (cond
3893      ((null level) nil)
3894      ((zerop level) t)
3895      ((null refs) t)
3896      ((null (gnus-parent-id refs)) t)
3897      ((and (= 1 level)
3898            (null (setq particle (gnus-id-to-article
3899                                  (gnus-parent-id refs))))
3900            (null (gnus-summary-thread-level particle)))))))
3901
3902 (defun gnus-root-id (id)
3903   "Return the id of the root of the thread where ID appears."
3904   (let (last-id prev)
3905     (while (and id (setq prev (car (gnus-id-to-thread id))))
3906       (setq last-id id
3907             id (gnus-parent-id (mail-header-references prev))))
3908     last-id))
3909
3910 (defun gnus-articles-in-thread (thread)
3911   "Return the list of articles in THREAD."
3912   (cons (mail-header-number (car thread))
3913         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3914
3915 (defun gnus-remove-thread (id &optional dont-remove)
3916   "Remove the thread that has ID in it."
3917   (let (headers thread last-id)
3918     ;; First go up in this thread until we find the root.
3919     (setq last-id (gnus-root-id id)
3920           headers (message-flatten-list (gnus-id-to-thread last-id)))
3921     ;; We have now found the real root of this thread.  It might have
3922     ;; been gathered into some loose thread, so we have to search
3923     ;; through the threads to find the thread we wanted.
3924     (let ((threads gnus-newsgroup-threads)
3925           sub)
3926       (while threads
3927         (setq sub (car threads))
3928         (if (stringp (car sub))
3929             ;; This is a gathered thread, so we look at the roots
3930             ;; below it to find whether this article is in this
3931             ;; gathered root.
3932             (progn
3933               (setq sub (cdr sub))
3934               (while sub
3935                 (when (member (caar sub) headers)
3936                   (setq thread (car threads)
3937                         threads nil
3938                         sub nil))
3939                 (setq sub (cdr sub))))
3940           ;; It's an ordinary thread, so we check it.
3941           (when (eq (car sub) (car headers))
3942             (setq thread sub
3943                   threads nil)))
3944         (setq threads (cdr threads)))
3945       ;; If this article is in no thread, then it's a root.
3946       (if thread
3947           (unless dont-remove
3948             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3949         (setq thread (gnus-id-to-thread last-id)))
3950       (when thread
3951         (prog1
3952             thread                      ; We return this thread.
3953           (unless dont-remove
3954             (if (stringp (car thread))
3955                 (progn
3956                   ;; If we use dummy roots, then we have to remove the
3957                   ;; dummy root as well.
3958                   (when (eq gnus-summary-make-false-root 'dummy)
3959                     ;; We go to the dummy root by going to
3960                     ;; the first sub-"thread", and then one line up.
3961                     (gnus-summary-goto-article
3962                      (mail-header-number (caadr thread)))
3963                     (forward-line -1)
3964                     (gnus-delete-line)
3965                     (gnus-data-compute-positions))
3966                   (setq thread (cdr thread))
3967                   (while thread
3968                     (gnus-remove-thread-1 (car thread))
3969                     (setq thread (cdr thread))))
3970               (gnus-remove-thread-1 thread))))))))
3971
3972 (defun gnus-remove-thread-1 (thread)
3973   "Remove the thread THREAD recursively."
3974   (let ((number (mail-header-number (pop thread)))
3975         d)
3976     (setq thread (reverse thread))
3977     (while thread
3978       (gnus-remove-thread-1 (pop thread)))
3979     (when (setq d (gnus-data-find number))
3980       (goto-char (gnus-data-pos d))
3981       (gnus-summary-show-thread)
3982       (gnus-data-remove
3983        number
3984        (- (gnus-point-at-bol)
3985           (prog1
3986               (1+ (gnus-point-at-eol))
3987             (gnus-delete-line)))))))
3988
3989 (defun gnus-sort-threads-1 (threads func)
3990   (sort (mapcar (lambda (thread)
3991                   (cons (car thread)
3992                         (and (cdr thread)
3993                              (gnus-sort-threads-1 (cdr thread) func))))
3994                 threads) func))
3995
3996 (defun gnus-sort-threads (threads)
3997   "Sort THREADS."
3998   (if (not gnus-thread-sort-functions)
3999       threads
4000     (gnus-message 8 "Sorting threads...")
4001     (prog1
4002         (gnus-sort-threads-1
4003          threads
4004          (gnus-make-sort-function gnus-thread-sort-functions))
4005       (gnus-message 8 "Sorting threads...done"))))
4006
4007 (defun gnus-sort-articles (articles)
4008   "Sort ARTICLES."
4009   (when gnus-article-sort-functions
4010     (gnus-message 7 "Sorting articles...")
4011     (prog1
4012         (setq gnus-newsgroup-headers
4013               (sort articles (gnus-make-sort-function
4014                               gnus-article-sort-functions)))
4015       (gnus-message 7 "Sorting articles...done"))))
4016
4017 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4018 (defmacro gnus-thread-header (thread)
4019   "Return header of first article in THREAD.
4020 Note that THREAD must never, ever be anything else than a variable -
4021 using some other form will lead to serious barfage."
4022   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4023   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4024   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4025         (vector thread) 2))
4026
4027 (defsubst gnus-article-sort-by-number (h1 h2)
4028   "Sort articles by article number."
4029   (< (mail-header-number h1)
4030      (mail-header-number h2)))
4031
4032 (defun gnus-thread-sort-by-number (h1 h2)
4033   "Sort threads by root article number."
4034   (gnus-article-sort-by-number
4035    (gnus-thread-header h1) (gnus-thread-header h2)))
4036
4037 (defsubst gnus-article-sort-by-lines (h1 h2)
4038   "Sort articles by article Lines header."
4039   (< (mail-header-lines h1)
4040      (mail-header-lines h2)))
4041
4042 (defun gnus-thread-sort-by-lines (h1 h2)
4043   "Sort threads by root article Lines header."
4044   (gnus-article-sort-by-lines
4045    (gnus-thread-header h1) (gnus-thread-header h2)))
4046
4047 (defsubst gnus-article-sort-by-chars (h1 h2)
4048   "Sort articles by octet length."
4049   (< (mail-header-chars h1)
4050      (mail-header-chars h2)))
4051
4052 (defun gnus-thread-sort-by-chars (h1 h2)
4053   "Sort threads by root article octet length."
4054   (gnus-article-sort-by-chars
4055    (gnus-thread-header h1) (gnus-thread-header h2)))
4056
4057 (defsubst gnus-article-sort-by-author (h1 h2)
4058   "Sort articles by root author."
4059   (string-lessp
4060    (let ((addr (car (mime-entity-read-field h1 'From))))
4061      (or (std11-full-name-string addr)
4062          (std11-address-string addr)
4063          ""))
4064    (let ((addr (car (mime-entity-read-field h2 'From))))
4065      (or (std11-full-name-string addr)
4066          (std11-address-string addr)
4067          ""))
4068    ))
4069
4070 (defun gnus-thread-sort-by-author (h1 h2)
4071   "Sort threads by root author."
4072   (gnus-article-sort-by-author
4073    (gnus-thread-header h1)  (gnus-thread-header h2)))
4074
4075 (defsubst gnus-article-sort-by-subject (h1 h2)
4076   "Sort articles by root subject."
4077   (string-lessp
4078    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4079    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4080
4081 (defun gnus-thread-sort-by-subject (h1 h2)
4082   "Sort threads by root subject."
4083   (gnus-article-sort-by-subject
4084    (gnus-thread-header h1) (gnus-thread-header h2)))
4085
4086 (defsubst gnus-article-sort-by-date (h1 h2)
4087   "Sort articles by root article date."
4088   (time-less-p
4089    (gnus-date-get-time (mail-header-date h1))
4090    (gnus-date-get-time (mail-header-date h2))))
4091
4092 (defun gnus-thread-sort-by-date (h1 h2)
4093   "Sort threads by root article date."
4094   (gnus-article-sort-by-date
4095    (gnus-thread-header h1) (gnus-thread-header h2)))
4096
4097 (defsubst gnus-article-sort-by-score (h1 h2)
4098   "Sort articles by root article score.
4099 Unscored articles will be counted as having a score of zero."
4100   (> (or (cdr (assq (mail-header-number h1)
4101                     gnus-newsgroup-scored))
4102          gnus-summary-default-score 0)
4103      (or (cdr (assq (mail-header-number h2)
4104                     gnus-newsgroup-scored))
4105          gnus-summary-default-score 0)))
4106
4107 (defun gnus-thread-sort-by-score (h1 h2)
4108   "Sort threads by root article score."
4109   (gnus-article-sort-by-score
4110    (gnus-thread-header h1) (gnus-thread-header h2)))
4111
4112 (defun gnus-thread-sort-by-total-score (h1 h2)
4113   "Sort threads by the sum of all scores in the thread.
4114 Unscored articles will be counted as having a score of zero."
4115   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4116
4117 (defun gnus-thread-total-score (thread)
4118   ;; This function find the total score of THREAD.
4119   (cond ((null thread)
4120          0)
4121         ((consp thread)
4122          (if (stringp (car thread))
4123              (apply gnus-thread-score-function 0
4124                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4125            (gnus-thread-total-score-1 thread)))
4126         (t
4127          (gnus-thread-total-score-1 (list thread)))))
4128
4129 (defun gnus-thread-total-score-1 (root)
4130   ;; This function find the total score of the thread below ROOT.
4131   (setq root (car root))
4132   (apply gnus-thread-score-function
4133          (or (append
4134               (mapcar 'gnus-thread-total-score
4135                       (cdr (gnus-id-to-thread (mail-header-id root))))
4136               (when (> (mail-header-number root) 0)
4137                 (list (or (cdr (assq (mail-header-number root)
4138                                      gnus-newsgroup-scored))
4139                           gnus-summary-default-score 0))))
4140              (list gnus-summary-default-score)
4141              '(0))))
4142
4143 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4144 (defvar gnus-tmp-prev-subject nil)
4145 (defvar gnus-tmp-false-parent nil)
4146 (defvar gnus-tmp-root-expunged nil)
4147 (defvar gnus-tmp-dummy-line nil)
4148
4149 (eval-when-compile (defvar gnus-tmp-header))
4150 (defun gnus-extra-header (type &optional header)
4151   "Return the extra header of TYPE."
4152   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4153       ""))
4154
4155 (defvar gnus-tmp-thread-tree-header-string "")
4156
4157 (defvar gnus-sum-thread-tree-root "> "
4158   "With %B spec, used for the root of a thread.
4159 If nil, use subject instead.")
4160 (defvar gnus-sum-thread-tree-single-indent ""
4161   "With %B spec, used for a thread with just one message.
4162 If nil, use subject instead.")
4163 (defvar gnus-sum-thread-tree-vertical "| "
4164   "With %B spec, used for drawing a vertical line.")
4165 (defvar gnus-sum-thread-tree-indent "  "
4166   "With %B spec, used for indenting.")
4167 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4168   "With %B spec, used for a leaf with brothers.")
4169 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4170   "With %B spec, used for a leaf without brothers.")
4171
4172 (defun gnus-summary-prepare-threads (threads)
4173   "Prepare summary buffer from THREADS and indentation LEVEL.
4174 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4175 or a straight list of headers."
4176   (gnus-message 7 "Generating summary...")
4177
4178   (setq gnus-newsgroup-threads threads)
4179   (beginning-of-line)
4180
4181   (let ((gnus-tmp-level 0)
4182         (default-score (or gnus-summary-default-score 0))
4183         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4184         thread number subject stack state gnus-tmp-gathered beg-match
4185         new-roots gnus-tmp-new-adopts thread-end
4186         gnus-tmp-header gnus-tmp-unread
4187         gnus-tmp-replied gnus-tmp-subject-or-nil
4188         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4189         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4190         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4191         tree-stack)
4192
4193     (setq gnus-tmp-prev-subject nil)
4194
4195     (if (vectorp (car threads))
4196         ;; If this is a straight (sic) list of headers, then a
4197         ;; threaded summary display isn't required, so we just create
4198         ;; an unthreaded one.
4199         (gnus-summary-prepare-unthreaded threads)
4200
4201       ;; Do the threaded display.
4202
4203       (while (or threads stack gnus-tmp-new-adopts new-roots)
4204
4205         (if (and (= gnus-tmp-level 0)
4206                  (or (not stack)
4207                      (= (caar stack) 0))
4208                  (not gnus-tmp-false-parent)
4209                  (or gnus-tmp-new-adopts new-roots))
4210             (if gnus-tmp-new-adopts
4211                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4212                       thread (list (car gnus-tmp-new-adopts))
4213                       gnus-tmp-header (caar thread)
4214                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4215               (when new-roots
4216                 (setq thread (list (car new-roots))
4217                       gnus-tmp-header (caar thread)
4218                       new-roots (cdr new-roots))))
4219
4220           (if threads
4221               ;; If there are some threads, we do them before the
4222               ;; threads on the stack.
4223               (setq thread threads
4224                     gnus-tmp-header (caar thread))
4225             ;; There were no current threads, so we pop something off
4226             ;; the stack.
4227             (setq state (car stack)
4228                   gnus-tmp-level (car state)
4229                   tree-stack (cadr state)
4230                   thread (caddr state)
4231                   stack (cdr stack)
4232                   gnus-tmp-header (caar thread))))
4233
4234         (setq gnus-tmp-false-parent nil)
4235         (setq gnus-tmp-root-expunged nil)
4236         (setq thread-end nil)
4237
4238         (if (stringp gnus-tmp-header)
4239             ;; The header is a dummy root.
4240             (cond
4241              ((eq gnus-summary-make-false-root 'adopt)
4242               ;; We let the first article adopt the rest.
4243               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4244                                                (cddar thread)))
4245               (setq gnus-tmp-gathered
4246                     (nconc (mapcar
4247                             (lambda (h) (mail-header-number (car h)))
4248                             (cddar thread))
4249                            gnus-tmp-gathered))
4250               (setq thread (cons (list (caar thread)
4251                                        (cadar thread))
4252                                  (cdr thread)))
4253               (setq gnus-tmp-level -1
4254                     gnus-tmp-false-parent t))
4255              ((eq gnus-summary-make-false-root 'empty)
4256               ;; We print adopted articles with empty subject fields.
4257               (setq gnus-tmp-gathered
4258                     (nconc (mapcar
4259                             (lambda (h) (mail-header-number (car h)))
4260                             (cddar thread))
4261                            gnus-tmp-gathered))
4262               (setq gnus-tmp-level -1))
4263              ((eq gnus-summary-make-false-root 'dummy)
4264               ;; We remember that we probably want to output a dummy
4265               ;; root.
4266               (setq gnus-tmp-dummy-line gnus-tmp-header)
4267               (setq gnus-tmp-prev-subject gnus-tmp-header))
4268              (t
4269               ;; We do not make a root for the gathered
4270               ;; sub-threads at all.
4271               (setq gnus-tmp-level -1)))
4272
4273           (setq number (mail-header-number gnus-tmp-header)
4274                 subject (mail-header-subject gnus-tmp-header))
4275
4276           (cond
4277            ;; If the thread has changed subject, we might want to make
4278            ;; this subthread into a root.
4279            ((and (null gnus-thread-ignore-subject)
4280                  (not (zerop gnus-tmp-level))
4281                  gnus-tmp-prev-subject
4282                  (not (inline
4283                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4284             (setq new-roots (nconc new-roots (list (car thread)))
4285                   thread-end t
4286                   gnus-tmp-header nil))
4287            ;; If the article lies outside the current limit,
4288            ;; then we do not display it.
4289            ((not (memq number gnus-newsgroup-limit))
4290             (setq gnus-tmp-gathered
4291                   (nconc (mapcar
4292                           (lambda (h) (mail-header-number (car h)))
4293                           (cdar thread))
4294                          gnus-tmp-gathered))
4295             (setq gnus-tmp-new-adopts (if (cdar thread)
4296                                           (append gnus-tmp-new-adopts
4297                                                   (cdar thread))
4298                                         gnus-tmp-new-adopts)
4299                   thread-end t
4300                   gnus-tmp-header nil)
4301             (when (zerop gnus-tmp-level)
4302               (setq gnus-tmp-root-expunged t)))
4303            ;; Perhaps this article is to be marked as read?
4304            ((and gnus-summary-mark-below
4305                  (< (or (cdr (assq number gnus-newsgroup-scored))
4306                         default-score)
4307                     gnus-summary-mark-below)
4308                  ;; Don't touch sparse articles.
4309                  (not (gnus-summary-article-sparse-p number))
4310                  (not (gnus-summary-article-ancient-p number)))
4311             (setq gnus-newsgroup-unreads
4312                   (delq number gnus-newsgroup-unreads))
4313             (if gnus-newsgroup-auto-expire
4314                 (push number gnus-newsgroup-expirable)
4315               (push (cons number gnus-low-score-mark)
4316                     gnus-newsgroup-reads))))
4317
4318           (when gnus-tmp-header
4319             ;; We may have an old dummy line to output before this
4320             ;; article.
4321             (when (and gnus-tmp-dummy-line
4322                        (gnus-subject-equal
4323                         gnus-tmp-dummy-line
4324                         (mail-header-subject gnus-tmp-header)))
4325               (gnus-summary-insert-dummy-line
4326                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4327               (setq gnus-tmp-dummy-line nil))
4328
4329             ;; Compute the mark.
4330             (setq gnus-tmp-unread (gnus-article-mark number))
4331
4332             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4333                                   gnus-tmp-header gnus-tmp-level)
4334                   gnus-newsgroup-data)
4335
4336             ;; Actually insert the line.
4337             (setq
4338              gnus-tmp-subject-or-nil
4339              (cond
4340               ((and gnus-thread-ignore-subject
4341                     gnus-tmp-prev-subject
4342                     (not (inline (gnus-subject-equal
4343                                   gnus-tmp-prev-subject subject))))
4344                subject)
4345               ((zerop gnus-tmp-level)
4346                (if (and (eq gnus-summary-make-false-root 'empty)
4347                         (memq number gnus-tmp-gathered)
4348                         gnus-tmp-prev-subject
4349                         (inline (gnus-subject-equal
4350                                  gnus-tmp-prev-subject subject)))
4351                    gnus-summary-same-subject
4352                  subject))
4353               (t gnus-summary-same-subject)))
4354             (if (and (eq gnus-summary-make-false-root 'adopt)
4355                      (= gnus-tmp-level 1)
4356                      (memq number gnus-tmp-gathered))
4357                 (setq gnus-tmp-opening-bracket ?\<
4358                       gnus-tmp-closing-bracket ?\>)
4359               (setq gnus-tmp-opening-bracket ?\[
4360                     gnus-tmp-closing-bracket ?\]))
4361             (setq
4362              gnus-tmp-indentation
4363              (aref gnus-thread-indent-array gnus-tmp-level)
4364              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4365              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4366                                 gnus-summary-default-score 0)
4367              gnus-tmp-score-char
4368              (if (or (null gnus-summary-default-score)
4369                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4370                          gnus-summary-zcore-fuzz))
4371                  ?\ ;;;Whitespace
4372                (if (< gnus-tmp-score gnus-summary-default-score)
4373                    gnus-score-below-mark gnus-score-over-mark))
4374              gnus-tmp-replied
4375              (cond ((memq number gnus-newsgroup-processable)
4376                     gnus-process-mark)
4377                    ((memq number gnus-newsgroup-cached)
4378                     gnus-cached-mark)
4379                    ((memq number gnus-newsgroup-replied)
4380                     gnus-replied-mark)
4381                    ((memq number gnus-newsgroup-forwarded)
4382                     gnus-forwarded-mark)
4383                    ((memq number gnus-newsgroup-saved)
4384                     gnus-saved-mark)
4385                    ((memq number gnus-newsgroup-recent)
4386                     gnus-recent-mark)
4387                    ((memq number gnus-newsgroup-unseen)
4388                     gnus-unseen-mark)
4389                    (t gnus-no-mark))
4390              gnus-tmp-from (mail-header-from gnus-tmp-header)
4391              gnus-tmp-name
4392              (cond
4393               ((string-match "<[^>]+> *$" gnus-tmp-from)
4394                (setq beg-match (match-beginning 0))
4395                (or (and (string-match "^\".+\"" gnus-tmp-from)
4396                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4397                    (substring gnus-tmp-from 0 beg-match)))
4398               ((string-match "(.+)" gnus-tmp-from)
4399                (substring gnus-tmp-from
4400                           (1+ (match-beginning 0)) (1- (match-end 0))))
4401               (t gnus-tmp-from))
4402              gnus-tmp-thread-tree-header-string
4403              (cond
4404               ((not gnus-show-threads) "")
4405               ((zerop gnus-tmp-level)
4406                (if (cdar thread)
4407                    (or gnus-sum-thread-tree-root subject)
4408                  (or gnus-sum-thread-tree-single-indent subject)))
4409               (t
4410                (concat (apply 'concat
4411                               (mapcar (lambda (item)
4412                                         (if (= item 1)
4413                                             gnus-sum-thread-tree-vertical
4414                                           gnus-sum-thread-tree-indent))
4415                                       (cdr (reverse tree-stack))))
4416                        (if (nth 1 thread)
4417                            gnus-sum-thread-tree-leaf-with-other
4418                          gnus-sum-thread-tree-single-leaf)))))
4419             (when (string= gnus-tmp-name "")
4420               (setq gnus-tmp-name gnus-tmp-from))
4421             (unless (numberp gnus-tmp-lines)
4422               (setq gnus-tmp-lines -1))
4423             (if (= gnus-tmp-lines -1)
4424                 (setq gnus-tmp-lines "?")
4425               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4426             (gnus-put-text-property
4427              (point)
4428              (progn (eval gnus-summary-line-format-spec) (point))
4429              'gnus-number number)
4430             (when gnus-visual-p
4431               (forward-line -1)
4432               (gnus-run-hooks 'gnus-summary-update-hook)
4433               (forward-line 1))
4434
4435             (setq gnus-tmp-prev-subject subject)))
4436
4437         (when (nth 1 thread)
4438           (push (list (max 0 gnus-tmp-level)
4439                       (copy-list tree-stack)
4440                       (nthcdr 1 thread))
4441                 stack))
4442         (push (if (nth 1 thread) 1 0) tree-stack)
4443         (incf gnus-tmp-level)
4444         (setq threads (if thread-end nil (cdar thread)))
4445         (unless threads
4446           (setq gnus-tmp-level 0)))))
4447   (gnus-message 7 "Generating summary...done"))
4448
4449 (defun gnus-summary-prepare-unthreaded (headers)
4450   "Generate an unthreaded summary buffer based on HEADERS."
4451   (let (header number mark)
4452
4453     (beginning-of-line)
4454
4455     (while headers
4456       ;; We may have to root out some bad articles...
4457       (when (memq (setq number (mail-header-number
4458                                 (setq header (pop headers))))
4459                   gnus-newsgroup-limit)
4460         ;; Mark article as read when it has a low score.
4461         (when (and gnus-summary-mark-below
4462                    (< (or (cdr (assq number gnus-newsgroup-scored))
4463                           gnus-summary-default-score 0)
4464                       gnus-summary-mark-below)
4465                    (not (gnus-summary-article-ancient-p number)))
4466           (setq gnus-newsgroup-unreads
4467                 (delq number gnus-newsgroup-unreads))
4468           (if gnus-newsgroup-auto-expire
4469               (push number gnus-newsgroup-expirable)
4470             (push (cons number gnus-low-score-mark)
4471                   gnus-newsgroup-reads)))
4472
4473         (setq mark (gnus-article-mark number))
4474         (push (gnus-data-make number mark (1+ (point)) header 0)
4475               gnus-newsgroup-data)
4476         (gnus-summary-insert-line
4477          header 0 number
4478          mark (memq number gnus-newsgroup-replied)
4479          (memq number gnus-newsgroup-expirable)
4480          (mail-header-subject header) nil
4481          (cdr (assq number gnus-newsgroup-scored))
4482          (memq number gnus-newsgroup-processable))))))
4483
4484 (defun gnus-summary-remove-list-identifiers ()
4485   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4486   (let ((regexp (if (consp gnus-list-identifiers)
4487                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4488                   gnus-list-identifiers))
4489         changed subject)
4490     (when regexp
4491       (dolist (header gnus-newsgroup-headers)
4492         (setq subject (mail-header-subject header)
4493               changed nil)
4494         (while (string-match
4495                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4496                 subject)
4497           (setq subject
4498                 (concat (substring subject 0 (match-beginning 2))
4499                         (substring subject (match-end 0)))
4500                 changed t))
4501         (when (and changed
4502                    (string-match
4503                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4504           (setq subject
4505                 (concat (substring subject 0 (match-beginning 1))
4506                         (substring subject (match-end 1)))))
4507         (when changed
4508           (mail-header-set-subject header subject))))))
4509
4510 (defun gnus-fetch-headers (articles)
4511   "Fetch headers of ARTICLES."
4512   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4513     (gnus-message 5 "Fetching headers for %s..." name)
4514     (prog1
4515         (if (eq 'nov
4516                 (setq gnus-headers-retrieved-by
4517                       (gnus-retrieve-headers
4518                        articles gnus-newsgroup-name
4519                        ;; We might want to fetch old headers, but
4520                        ;; not if there is only 1 article.
4521                        (and (or (and
4522                                  (not (eq gnus-fetch-old-headers 'some))
4523                                  (not (numberp gnus-fetch-old-headers)))
4524                                 (> (length articles) 1))
4525                             gnus-fetch-old-headers))))
4526             (gnus-get-newsgroup-headers-xover
4527              articles nil nil gnus-newsgroup-name t)
4528           (gnus-get-newsgroup-headers))
4529       (gnus-message 5 "Fetching headers for %s...done" name))))
4530
4531 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4532   "Select newsgroup GROUP.
4533 If READ-ALL is non-nil, all articles in the group are selected.
4534 If SELECT-ARTICLES, only select those articles from GROUP."
4535   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4536          ;;!!! Dirty hack; should be removed.
4537          (gnus-summary-ignore-duplicates
4538           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4539               t
4540             gnus-summary-ignore-duplicates))
4541          (info (nth 2 entry))
4542          articles fetched-articles cached)
4543
4544     (unless (gnus-check-server
4545              (set (make-local-variable 'gnus-current-select-method)
4546                   (gnus-find-method-for-group group)))
4547       (error "Couldn't open server"))
4548
4549     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4550         (gnus-activate-group group)     ; Or we can activate it...
4551         (progn                          ; Or we bug out.
4552           (when (equal major-mode 'gnus-summary-mode)
4553             (kill-buffer (current-buffer)))
4554           (error "Couldn't activate group %s: %s"
4555                  group (gnus-status-message group))))
4556
4557     (unless (gnus-request-group group t)
4558       (when (equal major-mode 'gnus-summary-mode)
4559         (kill-buffer (current-buffer)))
4560       (error "Couldn't request group %s: %s"
4561              group (gnus-status-message group)))
4562
4563     (setq gnus-newsgroup-name group
4564           gnus-newsgroup-unselected nil
4565           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4566
4567     (let ((display (gnus-group-find-parameter group 'display)))
4568       (setq gnus-newsgroup-display
4569             (cond
4570              ((eq display 'all)
4571               'gnus-not-ignore)
4572              ((arrayp display)
4573               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4574              (t
4575               nil))))
4576
4577     (gnus-summary-setup-default-charset)
4578
4579     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4580     (when (gnus-virtual-group-p group)
4581       (setq cached gnus-newsgroup-cached))
4582
4583     (setq gnus-newsgroup-unreads
4584           (gnus-set-difference
4585            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4586            gnus-newsgroup-dormant))
4587
4588     (setq gnus-newsgroup-processable nil)
4589
4590     (gnus-update-read-articles group gnus-newsgroup-unreads)
4591
4592     ;; Adjust and set lists of article marks.
4593     (when info
4594       (gnus-adjust-marked-articles info))
4595
4596     (if (setq articles select-articles)
4597         (setq gnus-newsgroup-unselected
4598               (gnus-sorted-intersection
4599                gnus-newsgroup-unreads
4600                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4601       (setq articles (gnus-articles-to-read group read-all)))
4602
4603     (cond
4604      ((null articles)
4605       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4606       'quit)
4607      ((eq articles 0) nil)
4608      (t
4609       ;; Init the dependencies hash table.
4610       (setq gnus-newsgroup-dependencies
4611             (gnus-make-hashtable (length articles)))
4612       (gnus-set-global-variables)
4613       ;; Retrieve the headers and read them in.
4614       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4615
4616       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4617       (when cached
4618         (setq gnus-newsgroup-cached cached))
4619
4620       ;; Suppress duplicates?
4621       (when gnus-suppress-duplicates
4622         (gnus-dup-suppress-articles))
4623
4624       ;; Set the initial limit.
4625       (setq gnus-newsgroup-limit (copy-sequence articles))
4626       ;; Remove canceled articles from the list of unread articles.
4627       (setq fetched-articles
4628             (mapcar (lambda (headers) (mail-header-number headers))
4629                     gnus-newsgroup-headers))
4630       (setq gnus-newsgroup-articles fetched-articles)
4631       (setq gnus-newsgroup-unreads
4632             (gnus-set-sorted-intersection
4633              gnus-newsgroup-unreads fetched-articles))
4634
4635       (let ((marks (assq 'seen (gnus-info-marks info))))
4636         ;; The `seen' marks are treated specially.
4637         (when (setq gnus-newsgroup-seen (cdr marks))
4638           (dolist (article gnus-newsgroup-articles)
4639             (unless (gnus-member-of-range
4640                      article gnus-newsgroup-seen)
4641               (push article gnus-newsgroup-unseen)))))
4642
4643       ;; Removed marked articles that do not exist.
4644       (gnus-update-missing-marks
4645        (gnus-sorted-complement fetched-articles articles))
4646       ;; We might want to build some more threads first.
4647       (when (and gnus-fetch-old-headers
4648                  (eq gnus-headers-retrieved-by 'nov))
4649         (if (eq gnus-fetch-old-headers 'invisible)
4650             (gnus-build-all-threads)
4651           (gnus-build-old-threads)))
4652       ;; Let the Gnus agent mark articles as read.
4653       (when gnus-agent
4654         (gnus-agent-get-undownloaded-list))
4655       ;; Remove list identifiers from subject
4656       (when gnus-list-identifiers
4657         (gnus-summary-remove-list-identifiers))
4658       ;; Check whether auto-expire is to be done in this group.
4659       (setq gnus-newsgroup-auto-expire
4660             (gnus-group-auto-expirable-p group))
4661       ;; Set up the article buffer now, if necessary.
4662       (unless gnus-single-article-buffer
4663         (gnus-article-setup-buffer))
4664       ;; First and last article in this newsgroup.
4665       (when gnus-newsgroup-headers
4666         (setq gnus-newsgroup-begin
4667               (mail-header-number (car gnus-newsgroup-headers))
4668               gnus-newsgroup-end
4669               (mail-header-number
4670                (gnus-last-element gnus-newsgroup-headers))))
4671       ;; GROUP is successfully selected.
4672       (or gnus-newsgroup-headers t)))))
4673
4674 (defun gnus-summary-display-make-predicate (display)
4675   (require 'gnus-agent)
4676   (when (= (length display) 1)
4677     (setq display (car display)))
4678   (unless gnus-summary-display-cache
4679     (dolist (elem (append (list (cons 'read 'read)
4680                                 (cons 'unseen 'unseen))
4681                           gnus-article-mark-lists))
4682       (push (cons (cdr elem)
4683                   (gnus-byte-compile
4684                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4685             gnus-summary-display-cache)))
4686   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4687     (gnus-get-predicate display)))
4688
4689 ;; Uses the dynamically bound `number' variable.
4690 (defvar number)
4691 (defun gnus-article-marked-p (type &optional article)
4692   (let ((article (or article number)))
4693     (cond
4694      ((eq type 'tick)
4695       (memq article gnus-newsgroup-marked))
4696      ((eq type 'unsend)
4697       (memq article gnus-newsgroup-unsendable))
4698      ((eq type 'undownload)
4699       (memq article gnus-newsgroup-undownloaded))
4700      ((eq type 'download)
4701       (memq article gnus-newsgroup-downloadable))
4702      ((eq type 'unread)
4703       (memq article gnus-newsgroup-unreads))
4704      ((eq type 'read)
4705       (memq article gnus-newsgroup-reads))
4706      ((eq type 'dormant)
4707       (memq article gnus-newsgroup-dormant) )
4708      ((eq type 'expire)
4709       (memq article gnus-newsgroup-expirable))
4710      ((eq type 'reply)
4711       (memq article gnus-newsgroup-replied))
4712      ((eq type 'killed)
4713       (memq article gnus-newsgroup-killed))
4714      ((eq type 'bookmark)
4715       (assq article gnus-newsgroup-bookmarks))
4716      ((eq type 'score)
4717       (assq article gnus-newsgroup-scored))
4718      ((eq type 'save)
4719       (memq article gnus-newsgroup-saved))
4720      ((eq type 'cache)
4721       (memq article gnus-newsgroup-cached))
4722      ((eq type 'forward)
4723       (memq article gnus-newsgroup-forwarded))
4724      ((eq type 'seen)
4725       (not (memq article gnus-newsgroup-unseen)))
4726      ((eq type 'recent)
4727       (memq article gnus-newsgroup-recent))
4728      (t t))))
4729
4730 (defun gnus-articles-to-read (group &optional read-all)
4731   "Find out what articles the user wants to read."
4732   (let* ((articles
4733           ;; Select all articles if `read-all' is non-nil, or if there
4734           ;; are no unread articles.
4735           (if (or read-all
4736                   (and (zerop (length gnus-newsgroup-marked))
4737                        (zerop (length gnus-newsgroup-unreads)))
4738                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4739               ;; We want to select the headers for all the articles in
4740               ;; the group, so we select either all the active
4741               ;; articles in the group, or (if that's nil), the
4742               ;; articles in the cache.
4743               (or
4744                (gnus-uncompress-range (gnus-active group))
4745                (gnus-cache-articles-in-group group))
4746             ;; Select only the "normal" subset of articles.
4747             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4748                           (copy-sequence gnus-newsgroup-unreads))
4749                   '<)))
4750          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4751          (scored (length scored-list))
4752          (number (length articles))
4753          (marked (+ (length gnus-newsgroup-marked)
4754                     (length gnus-newsgroup-dormant)))
4755          (select
4756           (cond
4757            ((numberp read-all)
4758             read-all)
4759            (t
4760             (condition-case ()
4761                 (cond
4762                  ((and (or (<= scored marked) (= scored number))
4763                        (natnump gnus-large-newsgroup)
4764                        (> number gnus-large-newsgroup))
4765                   (let* ((cursor-in-echo-area nil)
4766                          (input
4767                           (read-from-minibuffer
4768                            (format
4769                             "How many articles from %s (max %d): "
4770                             (gnus-limit-string
4771                              (gnus-group-decoded-name gnus-newsgroup-name)
4772                              35)
4773                             number)
4774                            (cons (number-to-string gnus-large-newsgroup)
4775                                  0))))
4776                     (if (string-match "^[ \t]*$" input)
4777                         number
4778                       input)))
4779                  ((and (> scored marked) (< scored number)
4780                        (> (- scored number) 20))
4781                   (let ((input
4782                          (read-string
4783                           (format "%s %s (%d scored, %d total): "
4784                                   "How many articles from"
4785                                   (gnus-group-decoded-name group)
4786                                   scored number))))
4787                     (if (string-match "^[ \t]*$" input)
4788                         number input)))
4789                  (t number))
4790               (quit
4791                (message "Quit getting the articles to read")
4792                nil))))))
4793     (setq select (if (stringp select) (string-to-number select) select))
4794     (if (or (null select) (zerop select))
4795         select
4796       (if (and (not (zerop scored)) (<= (abs select) scored))
4797           (progn
4798             (setq articles (sort scored-list '<))
4799             (setq number (length articles)))
4800         (setq articles (copy-sequence articles)))
4801
4802       (when (< (abs select) number)
4803         (if (< select 0)
4804             ;; Select the N oldest articles.
4805             (setcdr (nthcdr (1- (abs select)) articles) nil)
4806           ;; Select the N most recent articles.
4807           (setq articles (nthcdr (- number select) articles))))
4808       (setq gnus-newsgroup-unselected
4809             (gnus-sorted-intersection
4810              gnus-newsgroup-unreads
4811              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4812       (when gnus-alter-articles-to-read-function
4813         (setq gnus-newsgroup-unreads
4814               (sort
4815                (funcall gnus-alter-articles-to-read-function
4816                         gnus-newsgroup-name gnus-newsgroup-unreads)
4817                '<)))
4818       articles)))
4819
4820 (defun gnus-killed-articles (killed articles)
4821   (let (out)
4822     (while articles
4823       (when (inline (gnus-member-of-range (car articles) killed))
4824         (push (car articles) out))
4825       (setq articles (cdr articles)))
4826     out))
4827
4828 (defun gnus-uncompress-marks (marks)
4829   "Uncompress the mark ranges in MARKS."
4830   (let ((uncompressed '(score bookmark))
4831         out)
4832     (while marks
4833       (if (memq (caar marks) uncompressed)
4834           (push (car marks) out)
4835         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4836       (setq marks (cdr marks)))
4837     out))
4838
4839 (defun gnus-article-mark-to-type (mark)
4840   "Return the type of MARK."
4841   (or (cadr (assq mark gnus-article-special-mark-lists))
4842       'list))
4843
4844 (defun gnus-article-unpropagatable-p (mark)
4845   "Return whether MARK should be propagated to backend."
4846   (memq mark gnus-article-unpropagated-mark-lists))
4847
4848 (defun gnus-adjust-marked-articles (info)
4849   "Set all article lists and remove all marks that are no longer valid."
4850   (let* ((marked-lists (gnus-info-marks info))
4851          (active (gnus-active (gnus-info-group info)))
4852          (min (car active))
4853          (max (cdr active))
4854          (types gnus-article-mark-lists)
4855          marks var articles article mark mark-type)
4856
4857     (dolist (marks marked-lists)
4858       (setq mark (car marks)
4859             mark-type (gnus-article-mark-to-type mark)
4860             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4861
4862       ;; We set the variable according to the type of the marks list,
4863       ;; and then adjust the marks to a subset of the active articles.
4864       (cond
4865        ;; Adjust "simple" lists.
4866        ((eq mark-type 'list)
4867         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4868         (when (memq mark '(tick dormant expire reply save))
4869           (while articles
4870             (when (or (< (setq article (pop articles)) min) (> article max))
4871               (set var (delq article (symbol-value var)))))))
4872        ;; Adjust assocs.
4873        ((eq mark-type 'tuple)
4874         (set var (setq articles (cdr marks)))
4875         (when (not (listp (cdr (symbol-value var))))
4876           (set var (list (symbol-value var))))
4877         (when (not (listp (cdr articles)))
4878           (setq articles (list articles)))
4879         (while articles
4880           (when (or (not (consp (setq article (pop articles))))
4881                     (< (car article) min)
4882                     (> (car article) max))
4883             (set var (delq article (symbol-value var))))))
4884        ((eq mark-type 'range)
4885         (cond
4886          ((eq mark 'seen))))))))
4887
4888 (defun gnus-update-missing-marks (missing)
4889   "Go through the list of MISSING articles and remove them from the mark lists."
4890   (when missing
4891     (let (var m)
4892       ;; Go through all types.
4893       (dolist (elem gnus-article-mark-lists)
4894         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4895           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4896           (when (symbol-value var)
4897             ;; This list has articles.  So we delete all missing
4898             ;; articles from it.
4899             (setq m missing)
4900             (while m
4901               (set var (delq (pop m) (symbol-value var))))))))))
4902
4903 (defun gnus-update-marks ()
4904   "Enter the various lists of marked articles into the newsgroup info list."
4905   (let ((types gnus-article-mark-lists)
4906         (info (gnus-get-info gnus-newsgroup-name))
4907         type list newmarked symbol delta-marks)
4908     (when info
4909       ;; Add all marks lists to the list of marks lists.
4910       (while (setq type (pop types))
4911         (setq list (symbol-value
4912                     (setq symbol
4913                           (intern (format "gnus-newsgroup-%s" (car type))))))
4914
4915         (when list
4916           ;; Get rid of the entries of the articles that have the
4917           ;; default score.
4918           (when (and (eq (cdr type) 'score)
4919                      gnus-save-score
4920                      list)
4921             (let* ((arts list)
4922                    (prev (cons nil list))
4923                    (all prev))
4924               (while arts
4925                 (if (or (not (consp (car arts)))
4926                         (= (cdar arts) gnus-summary-default-score))
4927                     (setcdr prev (cdr arts))
4928                   (setq prev arts))
4929                 (setq arts (cdr arts)))
4930               (setq list (cdr all)))))
4931
4932         (when (eq (cdr type) 'seen)
4933           (setq list
4934                 (if list
4935                     (gnus-add-to-range list gnus-newsgroup-unseen)
4936                   (gnus-compress-sequence gnus-newsgroup-articles))))
4937
4938         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4939           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4940
4941         (when (and (gnus-check-backend-function
4942                     'request-set-mark gnus-newsgroup-name)
4943                    (not (gnus-article-unpropagatable-p (cdr type))))
4944           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4945                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4946                  (add (gnus-remove-from-range
4947                        (gnus-copy-sequence list) old)))
4948             (when add
4949               (push (list add 'add (list (cdr type))) delta-marks))
4950             (when del
4951               (push (list del 'del (list (cdr type))) delta-marks))))
4952
4953         (when list
4954           (push (cons (cdr type) list) newmarked)))
4955
4956       (when delta-marks
4957         (unless (gnus-check-group gnus-newsgroup-name)
4958           (error "Can't open server for %s" gnus-newsgroup-name))
4959         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4960
4961       ;; Enter these new marks into the info of the group.
4962       (if (nthcdr 3 info)
4963           (setcar (nthcdr 3 info) newmarked)
4964         ;; Add the marks lists to the end of the info.
4965         (when newmarked
4966           (setcdr (nthcdr 2 info) (list newmarked))))
4967
4968       ;; Cut off the end of the info if there's nothing else there.
4969       (let ((i 5))
4970         (while (and (> i 2)
4971                     (not (nth i info)))
4972           (when (nthcdr (decf i) info)
4973             (setcdr (nthcdr i info) nil)))))))
4974
4975 (defun gnus-set-mode-line (where)
4976   "Set the mode line of the article or summary buffers.
4977 If WHERE is `summary', the summary mode line format will be used."
4978   ;; Is this mode line one we keep updated?
4979   (when (and (memq where gnus-updated-mode-lines)
4980              (symbol-value
4981               (intern (format "gnus-%s-mode-line-format-spec" where))))
4982     (let (mode-string)
4983       (save-excursion
4984         ;; We evaluate this in the summary buffer since these
4985         ;; variables are buffer-local to that buffer.
4986         (set-buffer gnus-summary-buffer)
4987         ;; We bind all these variables that are used in the `eval' form
4988         ;; below.
4989         (let* ((mformat (symbol-value
4990                          (intern
4991                           (format "gnus-%s-mode-line-format-spec" where))))
4992                (gnus-tmp-group-name (gnus-group-decoded-name
4993                                      gnus-newsgroup-name))
4994                (gnus-tmp-article-number (or gnus-current-article 0))
4995                (gnus-tmp-unread gnus-newsgroup-unreads)
4996                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4997                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4998                (gnus-tmp-unread-and-unselected
4999                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5000                             (zerop gnus-tmp-unselected))
5001                        "")
5002                       ((zerop gnus-tmp-unselected)
5003                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5004                       (t (format "{%d(+%d) more}"
5005                                  gnus-tmp-unread-and-unticked
5006                                  gnus-tmp-unselected))))
5007                (gnus-tmp-subject
5008                 (if (and gnus-current-headers
5009                          (vectorp gnus-current-headers))
5010                     (gnus-mode-string-quote
5011                      (mail-header-subject gnus-current-headers))
5012                   ""))
5013                bufname-length max-len
5014                gnus-tmp-header);; passed as argument to any user-format-funcs
5015           (setq mode-string (eval mformat))
5016           (setq bufname-length (if (string-match "%b" mode-string)
5017                                    (- (length
5018                                        (buffer-name
5019                                         (if (eq where 'summary)
5020                                             nil
5021                                           (get-buffer gnus-article-buffer))))
5022                                       2)
5023                                  0))
5024           (setq max-len (max 4 (if gnus-mode-non-string-length
5025                                    (- (window-width)
5026                                       gnus-mode-non-string-length
5027                                       bufname-length)
5028                                  (length mode-string))))
5029           ;; We might have to chop a bit of the string off...
5030           (when (> (length mode-string) max-len)
5031             (setq mode-string
5032                   (concat (gnus-truncate-string mode-string (- max-len 3))
5033                           "...")))
5034           ;; Pad the mode string a bit.
5035           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5036       ;; Update the mode line.
5037       (setq mode-line-buffer-identification
5038             (gnus-mode-line-buffer-identification (list mode-string)))
5039       (set-buffer-modified-p t))))
5040
5041 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5042   "Go through the HEADERS list and add all Xrefs to a hash table.
5043 The resulting hash table is returned, or nil if no Xrefs were found."
5044   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5045          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5046          (xref-hashtb (gnus-make-hashtable))
5047          start group entry number xrefs header)
5048     (while headers
5049       (setq header (pop headers))
5050       (when (and (setq xrefs (mail-header-xref header))
5051                  (not (memq (setq number (mail-header-number header))
5052                             unreads)))
5053         (setq start 0)
5054         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5055           (setq start (match-end 0))
5056           (setq group (if prefix
5057                           (concat prefix (substring xrefs (match-beginning 1)
5058                                                     (match-end 1)))
5059                         (substring xrefs (match-beginning 1) (match-end 1))))
5060           (setq number
5061                 (string-to-int (substring xrefs (match-beginning 2)
5062                                           (match-end 2))))
5063           (if (setq entry (gnus-gethash group xref-hashtb))
5064               (setcdr entry (cons number (cdr entry)))
5065             (gnus-sethash group (cons number nil) xref-hashtb)))))
5066     (and start xref-hashtb)))
5067
5068 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5069   "Look through all the headers and mark the Xrefs as read."
5070   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5071         name entry info xref-hashtb idlist method nth4)
5072     (save-excursion
5073       (set-buffer gnus-group-buffer)
5074       (when (setq xref-hashtb
5075                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5076         (mapatoms
5077          (lambda (group)
5078            (unless (string= from-newsgroup (setq name (symbol-name group)))
5079              (setq idlist (symbol-value group))
5080              ;; Dead groups are not updated.
5081              (and (prog1
5082                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5083                             info (nth 2 entry))
5084                     (when (stringp (setq nth4 (gnus-info-method info)))
5085                       (setq nth4 (gnus-server-to-method nth4))))
5086                   ;; Only do the xrefs if the group has the same
5087                   ;; select method as the group we have just read.
5088                   (or (gnus-methods-equal-p
5089                        nth4 (gnus-find-method-for-group from-newsgroup))
5090                       virtual
5091                       (equal nth4 (setq method (gnus-find-method-for-group
5092                                                 from-newsgroup)))
5093                       (and (equal (car nth4) (car method))
5094                            (equal (nth 1 nth4) (nth 1 method))))
5095                   gnus-use-cross-reference
5096                   (or (not (eq gnus-use-cross-reference t))
5097                       virtual
5098                       ;; Only do cross-references on subscribed
5099                       ;; groups, if that is what is wanted.
5100                       (<= (gnus-info-level info) gnus-level-subscribed))
5101                   (gnus-group-make-articles-read name idlist))))
5102          xref-hashtb)))))
5103
5104 (defun gnus-compute-read-articles (group articles)
5105   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5106          (info (nth 2 entry))
5107          (active (gnus-active group))
5108          ninfo)
5109     (when entry
5110       ;; First peel off all invalid article numbers.
5111       (when active
5112         (let ((ids articles)
5113               id first)
5114           (while (setq id (pop ids))
5115             (when (and first (> id (cdr active)))
5116               ;; We'll end up in this situation in one particular
5117               ;; obscure situation.  If you re-scan a group and get
5118               ;; a new article that is cross-posted to a different
5119               ;; group that has not been re-scanned, you might get
5120               ;; crossposted article that has a higher number than
5121               ;; Gnus believes possible.  So we re-activate this
5122               ;; group as well.  This might mean doing the
5123               ;; crossposting thingy will *increase* the number
5124               ;; of articles in some groups.  Tsk, tsk.
5125               (setq active (or (gnus-activate-group group) active)))
5126             (when (or (> id (cdr active))
5127                       (< id (car active)))
5128               (setq articles (delq id articles))))))
5129       ;; If the read list is nil, we init it.
5130       (if (and active
5131                (null (gnus-info-read info))
5132                (> (car active) 1))
5133           (setq ninfo (cons 1 (1- (car active))))
5134         (setq ninfo (gnus-info-read info)))
5135       ;; Then we add the read articles to the range.
5136       (gnus-add-to-range
5137        ninfo (setq articles (sort articles '<))))))
5138
5139 (defun gnus-group-make-articles-read (group articles)
5140   "Update the info of GROUP to say that ARTICLES are read."
5141   (let* ((num 0)
5142          (entry (gnus-gethash group gnus-newsrc-hashtb))
5143          (info (nth 2 entry))
5144          (active (gnus-active group))
5145          range)
5146     (when entry
5147       (setq range (gnus-compute-read-articles group articles))
5148       (save-excursion
5149         (set-buffer gnus-group-buffer)
5150         (gnus-undo-register
5151           `(progn
5152              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5153              (gnus-info-set-read ',info ',(gnus-info-read info))
5154              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5155              (gnus-group-update-group ,group t))))
5156       ;; Add the read articles to the range.
5157       (gnus-info-set-read info range)
5158       (gnus-request-set-mark group (list (list range 'add '(read))))
5159       ;; Then we have to re-compute how many unread
5160       ;; articles there are in this group.
5161       (when active
5162         (cond
5163          ((not range)
5164           (setq num (- (1+ (cdr active)) (car active))))
5165          ((not (listp (cdr range)))
5166           (setq num (- (cdr active) (- (1+ (cdr range))
5167                                        (car range)))))
5168          (t
5169           (while range
5170             (if (numberp (car range))
5171                 (setq num (1+ num))
5172               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5173             (setq range (cdr range)))
5174           (setq num (- (cdr active) num))))
5175         ;; Update the number of unread articles.
5176         (setcar entry num)
5177         ;; Update the group buffer.
5178         (gnus-group-update-group group t)))))
5179
5180 (defvar gnus-newsgroup-none-id 0)
5181
5182 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5183   (let ((cur nntp-server-buffer)
5184         (dependencies
5185          (or dependencies
5186              (save-excursion (set-buffer gnus-summary-buffer)
5187                              gnus-newsgroup-dependencies)))
5188         headers id end ref
5189         (mail-parse-charset gnus-newsgroup-charset)
5190         (mail-parse-ignored-charsets
5191          (save-excursion (condition-case nil
5192                              (set-buffer gnus-summary-buffer)
5193                            (error))
5194                          gnus-newsgroup-ignored-charsets)))
5195     (save-excursion
5196       (set-buffer nntp-server-buffer)
5197       ;; Translate all TAB characters into SPACE characters.
5198       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5199       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5200       (gnus-run-hooks 'gnus-parse-headers-hook)
5201       (let ((case-fold-search t)
5202             in-reply-to header p lines chars ctype)
5203         (goto-char (point-min))
5204         ;; Search to the beginning of the next header.  Error messages
5205         ;; do not begin with 2 or 3.
5206         (while (re-search-forward "^[23][0-9]+ " nil t)
5207           (setq id nil
5208                 ref nil)
5209           ;; This implementation of this function, with nine
5210           ;; search-forwards instead of the one re-search-forward and
5211           ;; a case (which basically was the old function) is actually
5212           ;; about twice as fast, even though it looks messier.  You
5213           ;; can't have everything, I guess.  Speed and elegance
5214           ;; doesn't always go hand in hand.
5215           (setq
5216            header
5217            (make-full-mail-header
5218             ;; Number.
5219             (prog1
5220                 (read cur)
5221               (end-of-line)
5222               (setq p (point))
5223               (narrow-to-region (point)
5224                                 (or (and (search-forward "\n.\n" nil t)
5225                                          (- (point) 2))
5226                                     (point))))
5227             ;; Subject.
5228             (progn
5229               (goto-char p)
5230               (if (search-forward "\nsubject:" nil t)
5231                   (nnheader-header-value)
5232                 "(none)"))
5233             ;; From.
5234             (progn
5235               (goto-char p)
5236               (if (search-forward "\nfrom:" nil t)
5237                   (nnheader-header-value)
5238                 "(nobody)"))
5239             ;; Date.
5240             (progn
5241               (goto-char p)
5242               (if (search-forward "\ndate:" nil t)
5243                   (nnheader-header-value) ""))
5244             ;; Message-ID.
5245             (progn
5246               (goto-char p)
5247               (setq id (if (re-search-forward
5248                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5249                            ;; We do it this way to make sure the Message-ID
5250                            ;; is (somewhat) syntactically valid.
5251                            (buffer-substring (match-beginning 1)
5252                                              (match-end 1))
5253                          ;; If there was no message-id, we just fake one
5254                          ;; to make subsequent routines simpler.
5255                          (nnheader-generate-fake-message-id))))
5256             ;; References.
5257             (progn
5258               (goto-char p)
5259               (if (search-forward "\nreferences:" nil t)
5260                   (progn
5261                     (setq end (point))
5262                     (prog1
5263                         (nnheader-header-value)
5264                       (setq ref
5265                             (buffer-substring
5266                              (progn
5267                                ;; (end-of-line)
5268                                (search-backward ">" end t)
5269                                (1+ (point)))
5270                              (progn
5271                                (search-backward "<" end t)
5272                                (point))))))
5273                 ;; Get the references from the in-reply-to header if there
5274                 ;; were no references and the in-reply-to header looks
5275                 ;; promising.
5276                 (if (and (search-forward "\nin-reply-to:" nil t)
5277                          (setq in-reply-to (nnheader-header-value))
5278                          (string-match "<[^>]+>" in-reply-to))
5279                     (let (ref2)
5280                       (setq ref (substring in-reply-to (match-beginning 0)
5281                                            (match-end 0)))
5282                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5283                         (setq ref2 (substring in-reply-to (match-beginning 0)
5284                                               (match-end 0)))
5285                         (when (> (length ref2) (length ref))
5286                           (setq ref ref2)))
5287                       ref)
5288                   (setq ref nil))))
5289             ;; Chars.
5290             (progn
5291               (goto-char p)
5292               (if (search-forward "\nchars: " nil t)
5293                   (if (numberp (setq chars (ignore-errors (read cur))))
5294                       chars -1)
5295                 -1))
5296             ;; Lines.
5297             (progn
5298               (goto-char p)
5299               (if (search-forward "\nlines: " nil t)
5300                   (if (numberp (setq lines (ignore-errors (read cur))))
5301                       lines -1)
5302                 -1))
5303             ;; Xref.
5304             (progn
5305               (goto-char p)
5306               (and (search-forward "\nxref:" nil t)
5307                    (nnheader-header-value)))
5308             ;; Extra.
5309             (when gnus-extra-headers
5310               (let ((extra gnus-extra-headers)
5311                     out)
5312                 (while extra
5313                   (goto-char p)
5314                   (when (search-forward
5315                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5316                     (push (cons (car extra) (nnheader-header-value)) out))
5317                   (pop extra))
5318                 out))))
5319           (goto-char p)
5320           (if (and (search-forward "\ncontent-type: " nil t)
5321                    (setq ctype (nnheader-header-value)))
5322               (mime-entity-set-content-type-internal
5323                header (mime-parse-Content-Type ctype)))
5324           (when (equal id ref)
5325             (setq ref nil))
5326
5327           (when gnus-alter-header-function
5328             (funcall gnus-alter-header-function header)
5329             (setq id (mail-header-id header)
5330                   ref (gnus-parent-id (mail-header-references header))))
5331
5332           (when (setq header
5333                       (gnus-dependencies-add-header
5334                        header dependencies force-new))
5335             (push header headers))
5336           (goto-char (point-max))
5337           (widen))
5338         (nreverse headers)))))
5339
5340 ;; Goes through the xover lines and returns a list of vectors
5341 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5342                                                   force-new dependencies
5343                                                   group also-fetch-heads)
5344   "Parse the news overview data in the server buffer.
5345 Return a list of headers that match SEQUENCE (see
5346 `nntp-retrieve-headers')."
5347   ;; Get the Xref when the users reads the articles since most/some
5348   ;; NNTP servers do not include Xrefs when using XOVER.
5349   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5350   (let ((mail-parse-charset gnus-newsgroup-charset)
5351         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5352         (cur nntp-server-buffer)
5353         (dependencies (or dependencies gnus-newsgroup-dependencies))
5354         (allp (cond
5355                ((eq gnus-read-all-available-headers t)
5356                 t)
5357                ((stringp gnus-read-all-available-headers)
5358                 (string-match gnus-read-all-available-headers group))
5359                (t
5360                 nil)))
5361         number headers header)
5362     (save-excursion
5363       (set-buffer nntp-server-buffer)
5364       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5365       ;; Allow the user to mangle the headers before parsing them.
5366       (gnus-run-hooks 'gnus-parse-headers-hook)
5367       (goto-char (point-min))
5368       (while (not (eobp))
5369         (condition-case ()
5370             (while (and (or sequence allp)
5371                         (not (eobp)))
5372               (setq number (read cur))
5373               (when (not allp)
5374                 (while (and sequence
5375                             (< (car sequence) number))
5376                   (setq sequence (cdr sequence))))
5377               (when (and (or allp
5378                              (and sequence
5379                                   (eq number (car sequence))))
5380                          (progn
5381                            (setq sequence (cdr sequence))
5382                            (setq header (inline
5383                                           (gnus-nov-parse-line
5384                                            number dependencies force-new)))))
5385                 (push header headers))
5386               (forward-line 1))
5387           (error
5388            (gnus-error 4 "Strange nov line (%d)"
5389                        (count-lines (point-min) (point)))))
5390         (forward-line 1))
5391       ;; A common bug in inn is that if you have posted an article and
5392       ;; then retrieves the active file, it will answer correctly --
5393       ;; the new article is included.  However, a NOV entry for the
5394       ;; article may not have been generated yet, so this may fail.
5395       ;; We work around this problem by retrieving the last few
5396       ;; headers using HEAD.
5397       (if (or (not also-fetch-heads)
5398               (not sequence))
5399           ;; We (probably) got all the headers.
5400           (nreverse headers)
5401         (let ((gnus-nov-is-evil t))
5402           (nconc
5403            (nreverse headers)
5404            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5405              (gnus-get-newsgroup-headers))))))))
5406
5407 (defun gnus-article-get-xrefs ()
5408   "Fill in the Xref value in `gnus-current-headers', if necessary.
5409 This is meant to be called in `gnus-article-internal-prepare-hook'."
5410   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5411                                  gnus-current-headers)))
5412     (or (not gnus-use-cross-reference)
5413         (not headers)
5414         (and (mail-header-xref headers)
5415              (not (string= (mail-header-xref headers) "")))
5416         (let ((case-fold-search t)
5417               xref)
5418           (save-restriction
5419             (nnheader-narrow-to-headers)
5420             (goto-char (point-min))
5421             (when (or (and (not (eobp))
5422                            (eq (downcase (char-after)) ?x)
5423                            (looking-at "Xref:"))
5424                       (search-forward "\nXref:" nil t))
5425               (goto-char (1+ (match-end 0)))
5426               (setq xref (buffer-substring (point)
5427                                            (progn (end-of-line) (point))))
5428               (mail-header-set-xref headers xref)))))))
5429
5430 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5431   "Find article ID and insert the summary line for that article.
5432 OLD-HEADER can either be a header or a line number to insert
5433 the subject line on."
5434   (let* ((line (and (numberp old-header) old-header))
5435          (old-header (and (vectorp old-header) old-header))
5436          (header (cond ((and old-header use-old-header)
5437                         old-header)
5438                        ((and (numberp id)
5439                              (gnus-number-to-header id))
5440                         (gnus-number-to-header id))
5441                        (t
5442                         (gnus-read-header id))))
5443          (number (and (numberp id) id))
5444          d)
5445     (when header
5446       ;; Rebuild the thread that this article is part of and go to the
5447       ;; article we have fetched.
5448       (when (and (not gnus-show-threads)
5449                  old-header)
5450         (when (and number
5451                    (setq d (gnus-data-find (mail-header-number old-header))))
5452           (goto-char (gnus-data-pos d))
5453           (gnus-data-remove
5454            number
5455            (- (gnus-point-at-bol)
5456               (prog1
5457                   (1+ (gnus-point-at-eol))
5458                 (gnus-delete-line))))))
5459       (when old-header
5460         (mail-header-set-number header (mail-header-number old-header)))
5461       (setq gnus-newsgroup-sparse
5462             (delq (setq number (mail-header-number header))
5463                   gnus-newsgroup-sparse))
5464       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5465       (push number gnus-newsgroup-limit)
5466       (gnus-rebuild-thread (mail-header-id header) line)
5467       (gnus-summary-goto-subject number nil t))
5468     (when (and (numberp number)
5469                (> number 0))
5470       ;; We have to update the boundaries even if we can't fetch the
5471       ;; article if ID is a number -- so that the next `P' or `N'
5472       ;; command will fetch the previous (or next) article even
5473       ;; if the one we tried to fetch this time has been canceled.
5474       (when (> number gnus-newsgroup-end)
5475         (setq gnus-newsgroup-end number))
5476       (when (< number gnus-newsgroup-begin)
5477         (setq gnus-newsgroup-begin number))
5478       (setq gnus-newsgroup-unselected
5479             (delq number gnus-newsgroup-unselected)))
5480     ;; Report back a success?
5481     (and header (mail-header-number header))))
5482
5483 ;;; Process/prefix in the summary buffer
5484
5485 (defun gnus-summary-work-articles (n)
5486   "Return a list of articles to be worked upon.
5487 The prefix argument, the list of process marked articles, and the
5488 current article will be taken into consideration."
5489   (save-excursion
5490     (set-buffer gnus-summary-buffer)
5491     (cond
5492      (n
5493       ;; A numerical prefix has been given.
5494       (setq n (prefix-numeric-value n))
5495       (let ((backward (< n 0))
5496             (n (abs (prefix-numeric-value n)))
5497             articles article)
5498         (save-excursion
5499           (while
5500               (and (> n 0)
5501                    (push (setq article (gnus-summary-article-number))
5502                          articles)
5503                    (if backward
5504                        (gnus-summary-find-prev nil article)
5505                      (gnus-summary-find-next nil article)))
5506             (decf n)))
5507         (nreverse articles)))
5508      ((and (gnus-region-active-p) (mark))
5509       (message "region active")
5510       ;; Work on the region between point and mark.
5511       (let ((max (max (point) (mark)))
5512             articles article)
5513         (save-excursion
5514           (goto-char (min (point) (mark)))
5515           (while
5516               (and
5517                (push (setq article (gnus-summary-article-number)) articles)
5518                (gnus-summary-find-next nil article)
5519                (< (point) max)))
5520           (nreverse articles))))
5521      (gnus-newsgroup-processable
5522       ;; There are process-marked articles present.
5523       ;; Save current state.
5524       (gnus-summary-save-process-mark)
5525       ;; Return the list.
5526       (reverse gnus-newsgroup-processable))
5527      (t
5528       ;; Just return the current article.
5529       (list (gnus-summary-article-number))))))
5530
5531 (defmacro gnus-summary-iterate (arg &rest forms)
5532   "Iterate over the process/prefixed articles and do FORMS.
5533 ARG is the interactive prefix given to the command.  FORMS will be
5534 executed with point over the summary line of the articles."
5535   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5536     `(let ((,articles (gnus-summary-work-articles ,arg)))
5537        (while ,articles
5538          (gnus-summary-goto-subject (car ,articles))
5539          ,@forms
5540          (pop ,articles)))))
5541
5542 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5543 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5544
5545 (defun gnus-summary-save-process-mark ()
5546   "Push the current set of process marked articles on the stack."
5547   (interactive)
5548   (push (copy-sequence gnus-newsgroup-processable)
5549         gnus-newsgroup-process-stack))
5550
5551 (defun gnus-summary-kill-process-mark ()
5552   "Push the current set of process marked articles on the stack and unmark."
5553   (interactive)
5554   (gnus-summary-save-process-mark)
5555   (gnus-summary-unmark-all-processable))
5556
5557 (defun gnus-summary-yank-process-mark ()
5558   "Pop the last process mark state off the stack and restore it."
5559   (interactive)
5560   (unless gnus-newsgroup-process-stack
5561     (error "Empty mark stack"))
5562   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5563
5564 (defun gnus-summary-process-mark-set (set)
5565   "Make SET into the current process marked articles."
5566   (gnus-summary-unmark-all-processable)
5567   (while set
5568     (gnus-summary-set-process-mark (pop set))))
5569
5570 ;;; Searching and stuff
5571
5572 (defun gnus-summary-search-group (&optional backward use-level)
5573   "Search for next unread newsgroup.
5574 If optional argument BACKWARD is non-nil, search backward instead."
5575   (save-excursion
5576     (set-buffer gnus-group-buffer)
5577     (when (gnus-group-search-forward
5578            backward nil (if use-level (gnus-group-group-level) nil))
5579       (gnus-group-group-name))))
5580
5581 (defun gnus-summary-best-group (&optional exclude-group)
5582   "Find the name of the best unread group.
5583 If EXCLUDE-GROUP, do not go to this group."
5584   (save-excursion
5585     (set-buffer gnus-group-buffer)
5586     (save-excursion
5587       (gnus-group-best-unread-group exclude-group))))
5588
5589 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5590   (if backward (gnus-summary-find-prev)
5591     (let* ((dummy (gnus-summary-article-intangible-p))
5592            (article (or article (gnus-summary-article-number)))
5593            (arts (gnus-data-find-list article))
5594            result)
5595       (when (and (not dummy)
5596                  (or (not gnus-summary-check-current)
5597                      (not unread)
5598                      (not (gnus-data-unread-p (car arts)))))
5599         (setq arts (cdr arts)))
5600       (when (setq result
5601                   (if unread
5602                       (progn
5603                         (while arts
5604                           (when (or (and undownloaded
5605                                          (eq gnus-undownloaded-mark
5606                                              (gnus-data-mark (car arts))))
5607                                     (gnus-data-unread-p (car arts)))
5608                             (setq result (car arts)
5609                                   arts nil))
5610                           (setq arts (cdr arts)))
5611                         result)
5612                     (car arts)))
5613         (goto-char (gnus-data-pos result))
5614         (gnus-data-number result)))))
5615
5616 (defun gnus-summary-find-prev (&optional unread article)
5617   (let* ((eobp (eobp))
5618          (article (or article (gnus-summary-article-number)))
5619          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5620          result)
5621     (when (and (not eobp)
5622                (or (not gnus-summary-check-current)
5623                    (not unread)
5624                    (not (gnus-data-unread-p (car arts)))))
5625       (setq arts (cdr arts)))
5626     (when (setq result
5627                 (if unread
5628                     (progn
5629                       (while arts
5630                         (when (gnus-data-unread-p (car arts))
5631                           (setq result (car arts)
5632                                 arts nil))
5633                         (setq arts (cdr arts)))
5634                       result)
5635                   (car arts)))
5636       (goto-char (gnus-data-pos result))
5637       (gnus-data-number result))))
5638
5639 (defun gnus-summary-find-subject (subject &optional unread backward article)
5640   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5641          (article (or article (gnus-summary-article-number)))
5642          (articles (gnus-data-list backward))
5643          (arts (gnus-data-find-list article articles))
5644          result)
5645     (when (or (not gnus-summary-check-current)
5646               (not unread)
5647               (not (gnus-data-unread-p (car arts))))
5648       (setq arts (cdr arts)))
5649     (while arts
5650       (and (or (not unread)
5651                (gnus-data-unread-p (car arts)))
5652            (vectorp (gnus-data-header (car arts)))
5653            (gnus-subject-equal
5654             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5655            (setq result (car arts)
5656                  arts nil))
5657       (setq arts (cdr arts)))
5658     (and result
5659          (goto-char (gnus-data-pos result))
5660          (gnus-data-number result))))
5661
5662 (defun gnus-summary-search-forward (&optional unread subject backward)
5663   "Search forward for an article.
5664 If UNREAD, look for unread articles.  If SUBJECT, look for
5665 articles with that subject.  If BACKWARD, search backward instead."
5666   (cond (subject (gnus-summary-find-subject subject unread backward))
5667         (backward (gnus-summary-find-prev unread))
5668         (t (gnus-summary-find-next unread))))
5669
5670 (defun gnus-recenter (&optional n)
5671   "Center point in window and redisplay frame.
5672 Also do horizontal recentering."
5673   (interactive "P")
5674   (when (and gnus-auto-center-summary
5675              (not (eq gnus-auto-center-summary 'vertical)))
5676     (gnus-horizontal-recenter))
5677   (recenter n))
5678
5679 (defun gnus-summary-recenter ()
5680   "Center point in the summary window.
5681 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5682 displayed, no centering will be performed."
5683   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5684   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5685   (interactive)
5686   (let* ((top (cond ((< (window-height) 4) 0)
5687                     ((< (window-height) 7) 1)
5688                     (t (if (numberp gnus-auto-center-summary)
5689                            gnus-auto-center-summary
5690                          2))))
5691          (height (1- (window-height)))
5692          (bottom (save-excursion (goto-char (point-max))
5693                                  (forward-line (- height))
5694                                  (point)))
5695          (window (get-buffer-window (current-buffer))))
5696     ;; The user has to want it.
5697     (when gnus-auto-center-summary
5698       (when (get-buffer-window gnus-article-buffer)
5699         ;; Only do recentering when the article buffer is displayed,
5700         ;; Set the window start to either `bottom', which is the biggest
5701         ;; possible valid number, or the second line from the top,
5702         ;; whichever is the least.
5703         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5704           (if (> bottom top-pos)
5705               ;; Keep the second line from the top visible
5706               (set-window-start window top-pos t)
5707             ;; Try to keep the bottom line visible; if it's partially
5708             ;; obscured, either scroll one more line to make it fully
5709             ;; visible, or revert to using TOP-POS.
5710             (save-excursion
5711               (goto-char (point-max))
5712               (forward-line -1)
5713               (let ((last-line-start (point)))
5714                 (goto-char bottom)
5715                 (set-window-start window (point) t)
5716                 (when (not (pos-visible-in-window-p last-line-start window))
5717                   (forward-line 1)
5718                   (set-window-start window (min (point) top-pos) t)))))))
5719       ;; Do horizontal recentering while we're at it.
5720       (when (and (get-buffer-window (current-buffer) t)
5721                  (not (eq gnus-auto-center-summary 'vertical)))
5722         (let ((selected (selected-window)))
5723           (select-window (get-buffer-window (current-buffer) t))
5724           (gnus-summary-position-point)
5725           (gnus-horizontal-recenter)
5726           (select-window selected))))))
5727
5728 (defun gnus-summary-jump-to-group (newsgroup)
5729   "Move point to NEWSGROUP in group mode buffer."
5730   ;; Keep update point of group mode buffer if visible.
5731   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5732       (save-window-excursion
5733         ;; Take care of tree window mode.
5734         (when (get-buffer-window gnus-group-buffer)
5735           (pop-to-buffer gnus-group-buffer))
5736         (gnus-group-jump-to-group newsgroup))
5737     (save-excursion
5738       ;; Take care of tree window mode.
5739       (if (get-buffer-window gnus-group-buffer)
5740           (pop-to-buffer gnus-group-buffer)
5741         (set-buffer gnus-group-buffer))
5742       (gnus-group-jump-to-group newsgroup))))
5743
5744 ;; This function returns a list of article numbers based on the
5745 ;; difference between the ranges of read articles in this group and
5746 ;; the range of active articles.
5747 (defun gnus-list-of-unread-articles (group)
5748   (let* ((read (gnus-info-read (gnus-get-info group)))
5749          (active (or (gnus-active group) (gnus-activate-group group)))
5750          (last (cdr active))
5751          first nlast unread)
5752     ;; If none are read, then all are unread.
5753     (if (not read)
5754         (setq first (car active))
5755       ;; If the range of read articles is a single range, then the
5756       ;; first unread article is the article after the last read
5757       ;; article.  Sounds logical, doesn't it?
5758       (if (and (not (listp (cdr read)))
5759                (or (< (car read) (car active))
5760                    (progn (setq read (list read))
5761                           nil)))
5762           (setq first (max (car active) (1+ (cdr read))))
5763         ;; `read' is a list of ranges.
5764         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5765                                   (caar read)))
5766                   1)
5767           (setq first (car active)))
5768         (while read
5769           (when first
5770             (while (< first nlast)
5771               (push first unread)
5772               (setq first (1+ first))))
5773           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5774           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5775           (setq read (cdr read)))))
5776     ;; And add the last unread articles.
5777     (while (<= first last)
5778       (push first unread)
5779       (setq first (1+ first)))
5780     ;; Return the list of unread articles.
5781     (delq 0 (nreverse unread))))
5782
5783 (defun gnus-list-of-read-articles (group)
5784   "Return a list of unread, unticked and non-dormant articles."
5785   (let* ((info (gnus-get-info group))
5786          (marked (gnus-info-marks info))
5787          (active (gnus-active group)))
5788     (and info active
5789          (gnus-set-difference
5790           (gnus-sorted-complement
5791            (gnus-uncompress-range active)
5792            (gnus-list-of-unread-articles group))
5793           (append
5794            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5795            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5796
5797 ;; Various summary commands
5798
5799 (defun gnus-summary-select-article-buffer ()
5800   "Reconfigure windows to show article buffer."
5801   (interactive)
5802   (if (not (gnus-buffer-live-p gnus-article-buffer))
5803       (error "There is no article buffer for this summary buffer")
5804     (gnus-configure-windows 'article)
5805     (select-window (get-buffer-window gnus-article-buffer))))
5806
5807 (defun gnus-summary-universal-argument (arg)
5808   "Perform any operation on all articles that are process/prefixed."
5809   (interactive "P")
5810   (let ((articles (gnus-summary-work-articles arg))
5811         func article)
5812     (if (eq
5813          (setq
5814           func
5815           (key-binding
5816            (read-key-sequence
5817             (substitute-command-keys
5818              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5819          'undefined)
5820         (gnus-error 1 "Undefined key")
5821       (save-excursion
5822         (while articles
5823           (gnus-summary-goto-subject (setq article (pop articles)))
5824           (let (gnus-newsgroup-processable)
5825             (command-execute func))
5826           (gnus-summary-remove-process-mark article)))))
5827   (gnus-summary-position-point))
5828
5829 (defun gnus-summary-toggle-truncation (&optional arg)
5830   "Toggle truncation of summary lines.
5831 With arg, turn line truncation on iff arg is positive."
5832   (interactive "P")
5833   (setq truncate-lines
5834         (if (null arg) (not truncate-lines)
5835           (> (prefix-numeric-value arg) 0)))
5836   (redraw-display))
5837
5838 (defun gnus-summary-reselect-current-group (&optional all rescan)
5839   "Exit and then reselect the current newsgroup.
5840 The prefix argument ALL means to select all articles."
5841   (interactive "P")
5842   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5843     (error "Ephemeral groups can't be reselected"))
5844   (let ((current-subject (gnus-summary-article-number))
5845         (group gnus-newsgroup-name))
5846     (setq gnus-newsgroup-begin nil)
5847     (gnus-summary-exit)
5848     ;; We have to adjust the point of group mode buffer because
5849     ;; point was moved to the next unread newsgroup by exiting.
5850     (gnus-summary-jump-to-group group)
5851     (when rescan
5852       (save-excursion
5853         (save-window-excursion
5854           ;; Don't show group contents.
5855           (set-window-start (selected-window) (point-max))
5856           (gnus-group-get-new-news-this-group 1))))
5857     (gnus-group-read-group all t)
5858     (gnus-summary-goto-subject current-subject nil t)))
5859
5860 (defun gnus-summary-rescan-group (&optional all)
5861   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5862   (interactive "P")
5863   (gnus-summary-reselect-current-group all t))
5864
5865 (defun gnus-summary-update-info (&optional non-destructive)
5866   (save-excursion
5867     (let ((group gnus-newsgroup-name))
5868       (when group
5869         (when gnus-newsgroup-kill-headers
5870           (setq gnus-newsgroup-killed
5871                 (gnus-compress-sequence
5872                  (nconc
5873                   (gnus-set-sorted-intersection
5874                    (gnus-uncompress-range gnus-newsgroup-killed)
5875                    (setq gnus-newsgroup-unselected
5876                          (sort gnus-newsgroup-unselected '<)))
5877                   (setq gnus-newsgroup-unreads
5878                         (sort gnus-newsgroup-unreads '<)))
5879                  t)))
5880         (unless (listp (cdr gnus-newsgroup-killed))
5881           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5882         (let ((headers gnus-newsgroup-headers))
5883           ;; Set the new ranges of read articles.
5884           (save-excursion
5885             (set-buffer gnus-group-buffer)
5886             (gnus-undo-force-boundary))
5887           (gnus-update-read-articles
5888            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5889           ;; Set the current article marks.
5890           (let ((gnus-newsgroup-scored
5891                  (if (and (not gnus-save-score)
5892                           (not non-destructive))
5893                      nil
5894                    gnus-newsgroup-scored)))
5895             (save-excursion
5896               (gnus-update-marks)))
5897           ;; Do the cross-ref thing.
5898           (when gnus-use-cross-reference
5899             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5900           ;; Do not switch windows but change the buffer to work.
5901           (set-buffer gnus-group-buffer)
5902           (unless (gnus-ephemeral-group-p group)
5903             (gnus-group-update-group group)))))))
5904
5905 (defun gnus-summary-save-newsrc (&optional force)
5906   "Save the current number of read/marked articles in the dribble buffer.
5907 The dribble buffer will then be saved.
5908 If FORCE (the prefix), also save the .newsrc file(s)."
5909   (interactive "P")
5910   (gnus-summary-update-info t)
5911   (if force
5912       (gnus-save-newsrc-file)
5913     (gnus-dribble-save)))
5914
5915 (defun gnus-summary-exit (&optional temporary)
5916   "Exit reading current newsgroup, and then return to group selection mode.
5917 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5918   (interactive)
5919   (gnus-set-global-variables)
5920   (gnus-kill-save-kill-buffer)
5921   (gnus-async-halt-prefetch)
5922   (let* ((group gnus-newsgroup-name)
5923          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5924          (mode major-mode)
5925          (group-point nil)
5926          (buf (current-buffer)))
5927     (unless quit-config
5928       ;; Do adaptive scoring, and possibly save score files.
5929       (when gnus-newsgroup-adaptive
5930         (gnus-score-adaptive))
5931       (when gnus-use-scoring
5932         (gnus-score-save)))
5933     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5934     ;; If we have several article buffers, we kill them at exit.
5935     (unless gnus-single-article-buffer
5936       (gnus-kill-buffer gnus-original-article-buffer)
5937       (setq gnus-article-current nil))
5938     (when gnus-use-cache
5939       (gnus-cache-possibly-remove-articles)
5940       (gnus-cache-save-buffers))
5941     (gnus-async-prefetch-remove-group group)
5942     (when gnus-suppress-duplicates
5943       (gnus-dup-enter-articles))
5944     (when gnus-use-trees
5945       (gnus-tree-close group))
5946     (when gnus-use-cache
5947       (gnus-cache-write-active))
5948     ;; Remove entries for this group.
5949     (nnmail-purge-split-history (gnus-group-real-name group))
5950     ;; Make all changes in this group permanent.
5951     (unless quit-config
5952       (gnus-run-hooks 'gnus-exit-group-hook)
5953       (gnus-summary-update-info))
5954     (gnus-close-group group)
5955     ;; Make sure where we were, and go to next newsgroup.
5956     (set-buffer gnus-group-buffer)
5957     (unless quit-config
5958       (gnus-group-jump-to-group group))
5959     (gnus-run-hooks 'gnus-summary-exit-hook)
5960     (unless (or quit-config
5961                 ;; If this group has disappeared from the summary
5962                 ;; buffer, don't skip forwards.
5963                 (not (string= group (gnus-group-group-name))))
5964       (gnus-group-next-unread-group 1))
5965     (setq group-point (point))
5966     (if temporary
5967         nil                             ;Nothing to do.
5968       ;; If we have several article buffers, we kill them at exit.
5969       (unless gnus-single-article-buffer
5970         (gnus-kill-buffer gnus-article-buffer)
5971         (gnus-kill-buffer gnus-original-article-buffer)
5972         (setq gnus-article-current nil))
5973       (set-buffer buf)
5974       (if (not gnus-kill-summary-on-exit)
5975           (progn
5976             (gnus-deaden-summary)
5977             (setq mode nil))
5978         ;; We set all buffer-local variables to nil.  It is unclear why
5979         ;; this is needed, but if we don't, buffer-local variables are
5980         ;; not garbage-collected, it seems.  This would the lead to en
5981         ;; ever-growing Emacs.
5982         (gnus-summary-clear-local-variables)
5983         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5984           (gnus-summary-clear-local-variables))
5985         (when (get-buffer gnus-article-buffer)
5986           (bury-buffer gnus-article-buffer))
5987         ;; We clear the global counterparts of the buffer-local
5988         ;; variables as well, just to be on the safe side.
5989         (set-buffer gnus-group-buffer)
5990         (gnus-summary-clear-local-variables)
5991         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5992           (gnus-summary-clear-local-variables)))
5993       (setq gnus-current-select-method gnus-select-method)
5994       (pop-to-buffer gnus-group-buffer)
5995       (if (not quit-config)
5996           (progn
5997             (goto-char group-point)
5998             (gnus-configure-windows 'group 'force)
5999             (unless (pos-visible-in-window-p)
6000               (forward-line (/ (static-if (featurep 'xemacs)
6001                                    (window-displayed-height)
6002                                  (1- (window-height)))
6003                                -2))
6004               (set-window-start (selected-window) (point))
6005               (goto-char group-point)))
6006         (gnus-handle-ephemeral-exit quit-config))
6007       ;; Return to group mode buffer.
6008       (when (eq mode 'gnus-summary-mode)
6009         (gnus-kill-buffer buf))
6010       ;; Clear the current group name.
6011       (unless quit-config
6012         (setq gnus-newsgroup-name nil)))))
6013
6014 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6015 (defun gnus-summary-exit-no-update (&optional no-questions)
6016   "Quit reading current newsgroup without updating read article info."
6017   (interactive)
6018   (let* ((group gnus-newsgroup-name)
6019          (quit-config (gnus-group-quit-config group)))
6020     (when (or no-questions
6021               gnus-expert-user
6022               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6023       (gnus-async-halt-prefetch)
6024       (mapcar 'funcall
6025               (delq 'gnus-summary-expire-articles
6026                     (copy-sequence gnus-summary-prepare-exit-hook)))
6027       ;; If we have several article buffers, we kill them at exit.
6028       (unless gnus-single-article-buffer
6029         (gnus-kill-buffer gnus-article-buffer)
6030         (gnus-kill-buffer gnus-original-article-buffer)
6031         (setq gnus-article-current nil))
6032       (if (not gnus-kill-summary-on-exit)
6033           (gnus-deaden-summary)
6034         (gnus-close-group group)
6035         (gnus-summary-clear-local-variables)
6036         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6037           (gnus-summary-clear-local-variables))
6038         (set-buffer gnus-group-buffer)
6039         (gnus-summary-clear-local-variables)
6040         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6041           (gnus-summary-clear-local-variables))
6042         (when (get-buffer gnus-summary-buffer)
6043           (kill-buffer gnus-summary-buffer)))
6044       (unless gnus-single-article-buffer
6045         (setq gnus-article-current nil))
6046       (when gnus-use-trees
6047         (gnus-tree-close group))
6048       (gnus-async-prefetch-remove-group group)
6049       (when (get-buffer gnus-article-buffer)
6050         (bury-buffer gnus-article-buffer))
6051       ;; Return to the group buffer.
6052       (gnus-configure-windows 'group 'force)
6053       ;; Clear the current group name.
6054       (setq gnus-newsgroup-name nil)
6055       (when (equal (gnus-group-group-name) group)
6056         (gnus-group-next-unread-group 1))
6057       (when quit-config
6058         (gnus-handle-ephemeral-exit quit-config)))))
6059
6060 (defun gnus-handle-ephemeral-exit (quit-config)
6061   "Handle movement when leaving an ephemeral group.
6062 The state which existed when entering the ephemeral is reset."
6063   (if (not (buffer-name (car quit-config)))
6064       (gnus-configure-windows 'group 'force)
6065     (set-buffer (car quit-config))
6066     (cond ((eq major-mode 'gnus-summary-mode)
6067            (gnus-set-global-variables))
6068           ((eq major-mode 'gnus-article-mode)
6069            (save-excursion
6070              ;; The `gnus-summary-buffer' variable may point
6071              ;; to the old summary buffer when using a single
6072              ;; article buffer.
6073              (unless (gnus-buffer-live-p gnus-summary-buffer)
6074                (set-buffer gnus-group-buffer))
6075              (set-buffer gnus-summary-buffer)
6076              (gnus-set-global-variables))))
6077     (if (or (eq (cdr quit-config) 'article)
6078             (eq (cdr quit-config) 'pick))
6079         (progn
6080           ;; The current article may be from the ephemeral group
6081           ;; thus it is best that we reload this article
6082           (gnus-summary-show-article)
6083           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6084               (gnus-configure-windows 'pick 'force)
6085             (gnus-configure-windows (cdr quit-config) 'force)))
6086       (gnus-configure-windows (cdr quit-config) 'force))
6087     (when (eq major-mode 'gnus-summary-mode)
6088       (gnus-summary-next-subject 1 nil t)
6089       (gnus-summary-recenter)
6090       (gnus-summary-position-point))))
6091
6092 (defun gnus-summary-preview-mime-message ()
6093   "MIME decode and play this message."
6094   (interactive)
6095   (let ((gnus-break-pages nil)
6096         (gnus-show-mime t))
6097     (gnus-summary-select-article gnus-show-all-headers t))
6098   (select-window (get-buffer-window gnus-article-buffer)))
6099
6100 ;;; Dead summaries.
6101
6102 (defvar gnus-dead-summary-mode-map nil)
6103
6104 (unless gnus-dead-summary-mode-map
6105   (setq gnus-dead-summary-mode-map (make-keymap))
6106   (suppress-keymap gnus-dead-summary-mode-map)
6107   (substitute-key-definition
6108    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6109   (let ((keys '("\C-d" "\r" "\177" [delete])))
6110     (while keys
6111       (define-key gnus-dead-summary-mode-map
6112         (pop keys) 'gnus-summary-wake-up-the-dead))))
6113
6114 (defvar gnus-dead-summary-mode nil
6115   "Minor mode for Gnus summary buffers.")
6116
6117 (defun gnus-dead-summary-mode (&optional arg)
6118   "Minor mode for Gnus summary buffers."
6119   (interactive "P")
6120   (when (eq major-mode 'gnus-summary-mode)
6121     (make-local-variable 'gnus-dead-summary-mode)
6122     (setq gnus-dead-summary-mode
6123           (if (null arg) (not gnus-dead-summary-mode)
6124             (> (prefix-numeric-value arg) 0)))
6125     (when gnus-dead-summary-mode
6126       (gnus-add-minor-mode
6127        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6128
6129 (defun gnus-deaden-summary ()
6130   "Make the current summary buffer into a dead summary buffer."
6131   ;; Kill any previous dead summary buffer.
6132   (when (and gnus-dead-summary
6133              (buffer-name gnus-dead-summary))
6134     (save-excursion
6135       (set-buffer gnus-dead-summary)
6136       (when gnus-dead-summary-mode
6137         (kill-buffer (current-buffer)))))
6138   ;; Make this the current dead summary.
6139   (setq gnus-dead-summary (current-buffer))
6140   (gnus-dead-summary-mode 1)
6141   (let ((name (buffer-name)))
6142     (when (string-match "Summary" name)
6143       (rename-buffer
6144        (concat (substring name 0 (match-beginning 0)) "Dead "
6145                (substring name (match-beginning 0)))
6146        t)
6147       (bury-buffer))))
6148
6149 (defun gnus-kill-or-deaden-summary (buffer)
6150   "Kill or deaden the summary BUFFER."
6151   (save-excursion
6152     (when (and (buffer-name buffer)
6153                (not gnus-single-article-buffer))
6154       (save-excursion
6155         (set-buffer buffer)
6156         (gnus-kill-buffer gnus-article-buffer)
6157         (gnus-kill-buffer gnus-original-article-buffer)))
6158     (cond (gnus-kill-summary-on-exit
6159            (when (and gnus-use-trees
6160                       (gnus-buffer-exists-p buffer))
6161              (save-excursion
6162                (set-buffer buffer)
6163                (gnus-tree-close gnus-newsgroup-name)))
6164            (gnus-kill-buffer buffer))
6165           ((gnus-buffer-exists-p buffer)
6166            (save-excursion
6167              (set-buffer buffer)
6168              (gnus-deaden-summary))))))
6169
6170 (defun gnus-summary-wake-up-the-dead (&rest args)
6171   "Wake up the dead summary buffer."
6172   (interactive)
6173   (gnus-dead-summary-mode -1)
6174   (let ((name (buffer-name)))
6175     (when (string-match "Dead " name)
6176       (rename-buffer
6177        (concat (substring name 0 (match-beginning 0))
6178                (substring name (match-end 0)))
6179        t)))
6180   (gnus-message 3 "This dead summary is now alive again"))
6181
6182 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6183 (defun gnus-summary-fetch-faq (&optional faq-dir)
6184   "Fetch the FAQ for the current group.
6185 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6186 in."
6187   (interactive
6188    (list
6189     (when current-prefix-arg
6190       (completing-read
6191        "Faq dir: " (and (listp gnus-group-faq-directory)
6192                         (mapcar (lambda (file) (list file))
6193                                 gnus-group-faq-directory))))))
6194   (let (gnus-faq-buffer)
6195     (when (setq gnus-faq-buffer
6196                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6197       (gnus-configure-windows 'summary-faq))))
6198
6199 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6200 (defun gnus-summary-describe-group (&optional force)
6201   "Describe the current newsgroup."
6202   (interactive "P")
6203   (gnus-group-describe-group force gnus-newsgroup-name))
6204
6205 (defun gnus-summary-describe-briefly ()
6206   "Describe summary mode commands briefly."
6207   (interactive)
6208   (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")))
6209
6210 ;; Walking around group mode buffer from summary mode.
6211
6212 (defun gnus-summary-next-group (&optional no-article target-group backward)
6213   "Exit current newsgroup and then select next unread newsgroup.
6214 If prefix argument NO-ARTICLE is non-nil, no article is selected
6215 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6216 previous group instead."
6217   (interactive "P")
6218   ;; Stop pre-fetching.
6219   (gnus-async-halt-prefetch)
6220   (let ((current-group gnus-newsgroup-name)
6221         (current-buffer (current-buffer))
6222         entered)
6223     ;; First we semi-exit this group to update Xrefs and all variables.
6224     ;; We can't do a real exit, because the window conf must remain
6225     ;; the same in case the user is prompted for info, and we don't
6226     ;; want the window conf to change before that...
6227     (gnus-summary-exit t)
6228     (while (not entered)
6229       ;; Then we find what group we are supposed to enter.
6230       (set-buffer gnus-group-buffer)
6231       (gnus-group-jump-to-group current-group)
6232       (setq target-group
6233             (or target-group
6234                 (if (eq gnus-keep-same-level 'best)
6235                     (gnus-summary-best-group gnus-newsgroup-name)
6236                   (gnus-summary-search-group backward gnus-keep-same-level))))
6237       (if (not target-group)
6238           ;; There are no further groups, so we return to the group
6239           ;; buffer.
6240           (progn
6241             (gnus-message 5 "Returning to the group buffer")
6242             (setq entered t)
6243             (when (gnus-buffer-live-p current-buffer)
6244               (set-buffer current-buffer)
6245               (gnus-summary-exit))
6246             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6247         ;; We try to enter the target group.
6248         (gnus-group-jump-to-group target-group)
6249         (let ((unreads (gnus-group-group-unread)))
6250           (if (and (or (eq t unreads)
6251                        (and unreads (not (zerop unreads))))
6252                    (gnus-summary-read-group
6253                     target-group nil no-article
6254                     (and (buffer-name current-buffer) current-buffer)
6255                     nil backward))
6256               (setq entered t)
6257             (setq current-group target-group
6258                   target-group nil)))))))
6259
6260 (defun gnus-summary-prev-group (&optional no-article)
6261   "Exit current newsgroup and then select previous unread newsgroup.
6262 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6263   (interactive "P")
6264   (gnus-summary-next-group no-article nil t))
6265
6266 ;; Walking around summary lines.
6267
6268 (defun gnus-summary-first-subject (&optional unread undownloaded)
6269   "Go to the first unread subject.
6270 If UNREAD is non-nil, go to the first unread article.
6271 Returns the article selected or nil if there are no unread articles."
6272   (interactive "P")
6273   (prog1
6274       (cond
6275        ;; Empty summary.
6276        ((null gnus-newsgroup-data)
6277         (gnus-message 3 "No articles in the group")
6278         nil)
6279        ;; Pick the first article.
6280        ((not unread)
6281         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6282         (gnus-data-number (car gnus-newsgroup-data)))
6283        ;; No unread articles.
6284        ((null gnus-newsgroup-unreads)
6285         (gnus-message 3 "No more unread articles")
6286         nil)
6287        ;; Find the first unread article.
6288        (t
6289         (let ((data gnus-newsgroup-data))
6290           (while (and data
6291                       (and (not (and undownloaded
6292                                      (eq gnus-undownloaded-mark
6293                                          (gnus-data-mark (car data)))))
6294                            (not (gnus-data-unread-p (car data)))))
6295             (setq data (cdr data)))
6296           (when data
6297             (goto-char (gnus-data-pos (car data)))
6298             (gnus-data-number (car data))))))
6299     (gnus-summary-position-point)))
6300
6301 (defun gnus-summary-next-subject (n &optional unread dont-display)
6302   "Go to next N'th summary line.
6303 If N is negative, go to the previous N'th subject line.
6304 If UNREAD is non-nil, only unread articles are selected.
6305 The difference between N and the actual number of steps taken is
6306 returned."
6307   (interactive "p")
6308   (let ((backward (< n 0))
6309         (n (abs n)))
6310     (while (and (> n 0)
6311                 (if backward
6312                     (gnus-summary-find-prev unread)
6313                   (gnus-summary-find-next unread)))
6314       (unless (zerop (setq n (1- n)))
6315         (gnus-summary-show-thread)))
6316     (when (/= 0 n)
6317       (gnus-message 7 "No more%s articles"
6318                     (if unread " unread" "")))
6319     (unless dont-display
6320       (gnus-summary-recenter)
6321       (gnus-summary-position-point))
6322     n))
6323
6324 (defun gnus-summary-next-unread-subject (n)
6325   "Go to next N'th unread summary line."
6326   (interactive "p")
6327   (gnus-summary-next-subject n t))
6328
6329 (defun gnus-summary-prev-subject (n &optional unread)
6330   "Go to previous N'th summary line.
6331 If optional argument UNREAD is non-nil, only unread article is selected."
6332   (interactive "p")
6333   (gnus-summary-next-subject (- n) unread))
6334
6335 (defun gnus-summary-prev-unread-subject (n)
6336   "Go to previous N'th unread summary line."
6337   (interactive "p")
6338   (gnus-summary-next-subject (- n) t))
6339
6340 (defun gnus-summary-goto-subject (article &optional force silent)
6341   "Go the subject line of ARTICLE.
6342 If FORCE, also allow jumping to articles not currently shown."
6343   (interactive "nArticle number: ")
6344   (let ((b (point))
6345         (data (gnus-data-find article)))
6346     ;; We read in the article if we have to.
6347     (and (not data)
6348          force
6349          (gnus-summary-insert-subject
6350           article
6351           (if (or (numberp force) (vectorp force)) force)
6352           t)
6353          (setq data (gnus-data-find article)))
6354     (goto-char b)
6355     (if (not data)
6356         (progn
6357           (unless silent
6358             (gnus-message 3 "Can't find article %d" article))
6359           nil)
6360       (let ((pt (gnus-data-pos data)))
6361         (goto-char pt)
6362         (gnus-summary-set-article-display-arrow pt))
6363       (gnus-summary-position-point)
6364       article)))
6365
6366 ;; Walking around summary lines with displaying articles.
6367
6368 (defun gnus-summary-expand-window (&optional arg)
6369   "Make the summary buffer take up the entire Emacs frame.
6370 Given a prefix, will force an `article' buffer configuration."
6371   (interactive "P")
6372   (if arg
6373       (gnus-configure-windows 'article 'force)
6374     (gnus-configure-windows 'summary 'force)))
6375
6376 (defun gnus-summary-display-article (article &optional all-header)
6377   "Display ARTICLE in article buffer."
6378   (when (gnus-buffer-live-p gnus-article-buffer)
6379     (with-current-buffer gnus-article-buffer
6380       (set-buffer-multibyte t)))
6381   (gnus-set-global-variables)
6382   (when (gnus-buffer-live-p gnus-article-buffer)
6383     (with-current-buffer gnus-article-buffer
6384       (setq gnus-article-charset gnus-newsgroup-charset)
6385       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6386   (if (null article)
6387       nil
6388     (prog1
6389         (if gnus-summary-display-article-function
6390             (funcall gnus-summary-display-article-function article all-header)
6391           (gnus-article-prepare article all-header))
6392       (with-current-buffer gnus-article-buffer
6393         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6394              nil))
6395       (gnus-run-hooks 'gnus-select-article-hook)
6396       (when (and gnus-current-article
6397                  (not (zerop gnus-current-article)))
6398         (gnus-summary-goto-subject gnus-current-article))
6399       (gnus-summary-recenter)
6400       (when (and gnus-use-trees gnus-show-threads)
6401         (gnus-possibly-generate-tree article)
6402         (gnus-highlight-selected-tree article))
6403       ;; Successfully display article.
6404       (gnus-article-set-window-start
6405        (cdr (assq article gnus-newsgroup-bookmarks))))))
6406
6407 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6408   "Select the current article.
6409 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6410 non-nil, the article will be re-fetched even if it already present in
6411 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6412 be displayed."
6413   ;; Make sure we are in the summary buffer to work around bbdb bug.
6414   (unless (eq major-mode 'gnus-summary-mode)
6415     (set-buffer gnus-summary-buffer))
6416   (let ((article (or article (gnus-summary-article-number)))
6417         (all-headers (not (not all-headers))) ;Must be T or NIL.
6418         gnus-summary-display-article-function)
6419     (and (not pseudo)
6420          (gnus-summary-article-pseudo-p article)
6421          (error "This is a pseudo-article"))
6422     (save-excursion
6423       (set-buffer gnus-summary-buffer)
6424       (if (or (and gnus-single-article-buffer
6425                    (or (null gnus-current-article)
6426                        (null gnus-article-current)
6427                        (null (get-buffer gnus-article-buffer))
6428                        (not (eq article (cdr gnus-article-current)))
6429                        (not (equal (car gnus-article-current)
6430                                    gnus-newsgroup-name))))
6431               (and (not gnus-single-article-buffer)
6432                    (or (null gnus-current-article)
6433                        (not (eq gnus-current-article article))))
6434               force)
6435           ;; The requested article is different from the current article.
6436           (progn
6437             (gnus-summary-display-article article all-headers)
6438             (when (or all-headers gnus-show-all-headers)
6439               (gnus-article-show-all-headers))
6440             (gnus-article-set-window-start
6441              (cdr (assq article gnus-newsgroup-bookmarks)))
6442             article)
6443         (when (or all-headers gnus-show-all-headers)
6444           (gnus-article-show-all-headers))
6445         'old))))
6446
6447 (defun gnus-summary-force-verify-and-decrypt ()
6448   (interactive)
6449   (let ((mm-verify-option 'known)
6450         (mm-decrypt-option 'known))
6451     (gnus-summary-select-article nil 'force)))
6452
6453 (defun gnus-summary-set-current-mark (&optional current-mark)
6454   "Obsolete function."
6455   nil)
6456
6457 (defun gnus-summary-next-article (&optional unread subject backward push)
6458   "Select the next article.
6459 If UNREAD, only unread articles are selected.
6460 If SUBJECT, only articles with SUBJECT are selected.
6461 If BACKWARD, the previous article is selected instead of the next."
6462   (interactive "P")
6463   (cond
6464    ;; Is there such an article?
6465    ((and (gnus-summary-search-forward unread subject backward)
6466          (or (gnus-summary-display-article (gnus-summary-article-number))
6467              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6468     (gnus-summary-position-point))
6469    ;; If not, we try the first unread, if that is wanted.
6470    ((and subject
6471          gnus-auto-select-same
6472          (gnus-summary-first-unread-article))
6473     (gnus-summary-position-point)
6474     (gnus-message 6 "Wrapped"))
6475    ;; Try to get next/previous article not displayed in this group.
6476    ((and gnus-auto-extend-newsgroup
6477          (not unread) (not subject))
6478     (gnus-summary-goto-article
6479      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6480      nil (count-lines (point-min) (point))))
6481    ;; Go to next/previous group.
6482    (t
6483     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6484       (gnus-summary-jump-to-group gnus-newsgroup-name))
6485     (let ((cmd last-command-char)
6486           (point
6487            (save-excursion
6488              (set-buffer gnus-group-buffer)
6489              (point)))
6490           (group
6491            (if (eq gnus-keep-same-level 'best)
6492                (gnus-summary-best-group gnus-newsgroup-name)
6493              (gnus-summary-search-group backward gnus-keep-same-level))))
6494       ;; For some reason, the group window gets selected.  We change
6495       ;; it back.
6496       (select-window (get-buffer-window (current-buffer)))
6497       ;; Select next unread newsgroup automagically.
6498       (cond
6499        ((or (not gnus-auto-select-next)
6500             (not cmd))
6501         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6502        ((or (eq gnus-auto-select-next 'quietly)
6503             (and (eq gnus-auto-select-next 'slightly-quietly)
6504                  push)
6505             (and (eq gnus-auto-select-next 'almost-quietly)
6506                  (gnus-summary-last-article-p)))
6507         ;; Select quietly.
6508         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6509             (gnus-summary-exit)
6510           (gnus-message 7 "No more%s articles (%s)..."
6511                         (if unread " unread" "")
6512                         (if group (concat "selecting " group)
6513                           "exiting"))
6514           (gnus-summary-next-group nil group backward)))
6515        (t
6516         (when (gnus-key-press-event-p last-input-event)
6517           (gnus-summary-walk-group-buffer
6518            gnus-newsgroup-name cmd unread backward point))))))))
6519
6520 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6521   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6522                       (?\C-p (gnus-group-prev-unread-group 1))))
6523         (cursor-in-echo-area t)
6524         keve key group ended)
6525     (save-excursion
6526       (set-buffer gnus-group-buffer)
6527       (goto-char start)
6528       (setq group
6529             (if (eq gnus-keep-same-level 'best)
6530                 (gnus-summary-best-group gnus-newsgroup-name)
6531               (gnus-summary-search-group backward gnus-keep-same-level))))
6532     (while (not ended)
6533       (gnus-message
6534        5 "No more%s articles%s" (if unread " unread" "")
6535        (if (and group
6536                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6537            (format " (Type %s for %s [%s])"
6538                    (single-key-description cmd) group
6539                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6540          (format " (Type %s to exit %s)"
6541                  (single-key-description cmd)
6542                  gnus-newsgroup-name)))
6543       ;; Confirm auto selection.
6544       (setq key (car (setq keve (gnus-read-event-char))))
6545       (setq ended t)
6546       (cond
6547        ((assq key keystrokes)
6548         (let ((obuf (current-buffer)))
6549           (switch-to-buffer gnus-group-buffer)
6550           (when group
6551             (gnus-group-jump-to-group group))
6552           (eval (cadr (assq key keystrokes)))
6553           (setq group (gnus-group-group-name))
6554           (switch-to-buffer obuf))
6555         (setq ended nil))
6556        ((equal key cmd)
6557         (if (or (not group)
6558                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6559             (gnus-summary-exit)
6560           (gnus-summary-next-group nil group backward)))
6561        (t
6562         (push (cdr keve) unread-command-events))))))
6563
6564 (defun gnus-summary-next-unread-article ()
6565   "Select unread article after current one."
6566   (interactive)
6567   (gnus-summary-next-article
6568    (or (not (eq gnus-summary-goto-unread 'never))
6569        (gnus-summary-last-article-p (gnus-summary-article-number)))
6570    (and gnus-auto-select-same
6571         (gnus-summary-article-subject))))
6572
6573 (defun gnus-summary-prev-article (&optional unread subject)
6574   "Select the article after the current one.
6575 If UNREAD is non-nil, only unread articles are selected."
6576   (interactive "P")
6577   (gnus-summary-next-article unread subject t))
6578
6579 (defun gnus-summary-prev-unread-article ()
6580   "Select unread article before current one."
6581   (interactive)
6582   (gnus-summary-prev-article
6583    (or (not (eq gnus-summary-goto-unread 'never))
6584        (gnus-summary-first-article-p (gnus-summary-article-number)))
6585    (and gnus-auto-select-same
6586         (gnus-summary-article-subject))))
6587
6588 (defun gnus-summary-next-page (&optional lines circular)
6589   "Show next page of the selected article.
6590 If at the end of the current article, select the next article.
6591 LINES says how many lines should be scrolled up.
6592
6593 If CIRCULAR is non-nil, go to the start of the article instead of
6594 selecting the next article when reaching the end of the current
6595 article."
6596   (interactive "P")
6597   (setq gnus-summary-buffer (current-buffer))
6598   (gnus-set-global-variables)
6599   (let ((article (gnus-summary-article-number))
6600         (article-window (get-buffer-window gnus-article-buffer t))
6601         endp)
6602     ;; If the buffer is empty, we have no article.
6603     (unless article
6604       (error "No article to select"))
6605     (gnus-configure-windows 'article)
6606     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6607         (if (and (eq gnus-summary-goto-unread 'never)
6608                  (not (gnus-summary-last-article-p article)))
6609             (gnus-summary-next-article)
6610           (gnus-summary-next-unread-article))
6611       (if (or (null gnus-current-article)
6612               (null gnus-article-current)
6613               (/= article (cdr gnus-article-current))
6614               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6615           ;; Selected subject is different from current article's.
6616           (gnus-summary-display-article article)
6617         (when article-window
6618           (gnus-eval-in-buffer-window gnus-article-buffer
6619             (setq endp (gnus-article-next-page lines)))
6620           (when endp
6621             (cond (circular
6622                    (gnus-summary-beginning-of-article))
6623                   (lines
6624                    (gnus-message 3 "End of message"))
6625                   ((null lines)
6626                    (if (and (eq gnus-summary-goto-unread 'never)
6627                             (not (gnus-summary-last-article-p article)))
6628                        (gnus-summary-next-article)
6629                      (gnus-summary-next-unread-article))))))))
6630     (gnus-summary-recenter)
6631     (gnus-summary-position-point)))
6632
6633 (defun gnus-summary-prev-page (&optional lines move)
6634   "Show previous page of selected article.
6635 Argument LINES specifies lines to be scrolled down.
6636 If MOVE, move to the previous unread article if point is at
6637 the beginning of the buffer."
6638   (interactive "P")
6639   (let ((article (gnus-summary-article-number))
6640         (article-window (get-buffer-window gnus-article-buffer t))
6641         endp)
6642     (gnus-configure-windows 'article)
6643     (if (or (null gnus-current-article)
6644             (null gnus-article-current)
6645             (/= article (cdr gnus-article-current))
6646             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6647         ;; Selected subject is different from current article's.
6648         (gnus-summary-display-article article)
6649       (gnus-summary-recenter)
6650       (when article-window
6651         (gnus-eval-in-buffer-window gnus-article-buffer
6652           (setq endp (gnus-article-prev-page lines)))
6653         (when (and move endp)
6654           (cond (lines
6655                  (gnus-message 3 "Beginning of message"))
6656                 ((null lines)
6657                  (if (and (eq gnus-summary-goto-unread 'never)
6658                           (not (gnus-summary-first-article-p article)))
6659                      (gnus-summary-prev-article)
6660                    (gnus-summary-prev-unread-article))))))))
6661   (gnus-summary-position-point))
6662
6663 (defun gnus-summary-prev-page-or-article (&optional lines)
6664   "Show previous page of selected article.
6665 Argument LINES specifies lines to be scrolled down.
6666 If at the beginning of the article, go to the next article."
6667   (interactive "P")
6668   (gnus-summary-prev-page lines t))
6669
6670 (defun gnus-summary-scroll-up (lines)
6671   "Scroll up (or down) one line current article.
6672 Argument LINES specifies lines to be scrolled up (or down if negative)."
6673   (interactive "p")
6674   (gnus-configure-windows 'article)
6675   (gnus-summary-show-thread)
6676   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6677     (gnus-eval-in-buffer-window gnus-article-buffer
6678       (cond ((> lines 0)
6679              (when (gnus-article-next-page lines)
6680                (gnus-message 3 "End of message")))
6681             ((< lines 0)
6682              (gnus-article-prev-page (- lines))))))
6683   (gnus-summary-recenter)
6684   (gnus-summary-position-point))
6685
6686 (defun gnus-summary-scroll-down (lines)
6687   "Scroll down (or up) one line current article.
6688 Argument LINES specifies lines to be scrolled down (or up if negative)."
6689   (interactive "p")
6690   (gnus-summary-scroll-up (- lines)))
6691
6692 (defun gnus-summary-next-same-subject ()
6693   "Select next article which has the same subject as current one."
6694   (interactive)
6695   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6696
6697 (defun gnus-summary-prev-same-subject ()
6698   "Select previous article which has the same subject as current one."
6699   (interactive)
6700   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6701
6702 (defun gnus-summary-next-unread-same-subject ()
6703   "Select next unread article which has the same subject as current one."
6704   (interactive)
6705   (gnus-summary-next-article t (gnus-summary-article-subject)))
6706
6707 (defun gnus-summary-prev-unread-same-subject ()
6708   "Select previous unread article which has the same subject as current one."
6709   (interactive)
6710   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6711
6712 (defun gnus-summary-first-unread-article ()
6713   "Select the first unread article.
6714 Return nil if there are no unread articles."
6715   (interactive)
6716   (prog1
6717       (when (gnus-summary-first-subject t)
6718         (gnus-summary-show-thread)
6719         (gnus-summary-first-subject t)
6720         (gnus-summary-display-article (gnus-summary-article-number)))
6721     (gnus-summary-position-point)))
6722
6723 (defun gnus-summary-first-unread-subject ()
6724   "Place the point on the subject line of the first unread article.
6725 Return nil if there are no unread articles."
6726   (interactive)
6727   (prog1
6728       (when (gnus-summary-first-subject t)
6729         (gnus-summary-show-thread)
6730         (gnus-summary-first-subject t))
6731     (gnus-summary-position-point)))
6732
6733 (defun gnus-summary-first-article ()
6734   "Select the first article.
6735 Return nil if there are no articles."
6736   (interactive)
6737   (prog1
6738       (when (gnus-summary-first-subject)
6739         (gnus-summary-show-thread)
6740         (gnus-summary-first-subject)
6741         (gnus-summary-display-article (gnus-summary-article-number)))
6742     (gnus-summary-position-point)))
6743
6744 (defun gnus-summary-best-unread-article ()
6745   "Select the unread article with the highest score."
6746   (interactive)
6747   (let ((best -1000000)
6748         (data gnus-newsgroup-data)
6749         article score)
6750     (while data
6751       (and (gnus-data-unread-p (car data))
6752            (> (setq score
6753                     (gnus-summary-article-score (gnus-data-number (car data))))
6754               best)
6755            (setq best score
6756                  article (gnus-data-number (car data))))
6757       (setq data (cdr data)))
6758     (prog1
6759         (if article
6760             (gnus-summary-goto-article article)
6761           (error "No unread articles"))
6762       (gnus-summary-position-point))))
6763
6764 (defun gnus-summary-last-subject ()
6765   "Go to the last displayed subject line in the group."
6766   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6767     (when article
6768       (gnus-summary-goto-subject article))))
6769
6770 (defun gnus-summary-goto-article (article &optional all-headers force)
6771   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6772 If ALL-HEADERS is non-nil, no header lines are hidden.
6773 If FORCE, go to the article even if it isn't displayed.  If FORCE
6774 is a number, it is the line the article is to be displayed on."
6775   (interactive
6776    (list
6777     (completing-read
6778      "Article number or Message-ID: "
6779      (mapcar (lambda (number) (list (int-to-string number)))
6780              gnus-newsgroup-limit))
6781     current-prefix-arg
6782     t))
6783   (prog1
6784       (if (and (stringp article)
6785                (string-match "@" article))
6786           (gnus-summary-refer-article article)
6787         (when (stringp article)
6788           (setq article (string-to-number article)))
6789         (if (gnus-summary-goto-subject article force)
6790             (gnus-summary-display-article article all-headers)
6791           (gnus-message 4 "Couldn't go to article %s" article) nil))
6792     (gnus-summary-position-point)))
6793
6794 (defun gnus-summary-goto-last-article ()
6795   "Go to the previously read article."
6796   (interactive)
6797   (prog1
6798       (when gnus-last-article
6799         (gnus-summary-goto-article gnus-last-article nil t))
6800     (gnus-summary-position-point)))
6801
6802 (defun gnus-summary-pop-article (number)
6803   "Pop one article off the history and go to the previous.
6804 NUMBER articles will be popped off."
6805   (interactive "p")
6806   (let (to)
6807     (setq gnus-newsgroup-history
6808           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6809     (if to
6810         (gnus-summary-goto-article (car to) nil t)
6811       (error "Article history empty")))
6812   (gnus-summary-position-point))
6813
6814 ;; Summary commands and functions for limiting the summary buffer.
6815
6816 (defun gnus-summary-limit-to-articles (n)
6817   "Limit the summary buffer to the next N articles.
6818 If not given a prefix, use the process marked articles instead."
6819   (interactive "P")
6820   (prog1
6821       (let ((articles (gnus-summary-work-articles n)))
6822         (setq gnus-newsgroup-processable nil)
6823         (gnus-summary-limit articles))
6824     (gnus-summary-position-point)))
6825
6826 (defun gnus-summary-pop-limit (&optional total)
6827   "Restore the previous limit.
6828 If given a prefix, remove all limits."
6829   (interactive "P")
6830   (when total
6831     (setq gnus-newsgroup-limits
6832           (list (mapcar (lambda (h) (mail-header-number h))
6833                         gnus-newsgroup-headers))))
6834   (unless gnus-newsgroup-limits
6835     (error "No limit to pop"))
6836   (prog1
6837       (gnus-summary-limit nil 'pop)
6838     (gnus-summary-position-point)))
6839
6840 (defun gnus-summary-limit-to-subject (subject &optional header)
6841   "Limit the summary buffer to articles that have subjects that match a regexp."
6842   (interactive "sLimit to subject (regexp): ")
6843   (unless header
6844     (setq header "subject"))
6845   (when (not (equal "" subject))
6846     (prog1
6847         (let ((articles (gnus-summary-find-matching
6848                          (or header "subject") subject 'all)))
6849           (unless articles
6850             (error "Found no matches for \"%s\"" subject))
6851           (gnus-summary-limit articles))
6852       (gnus-summary-position-point))))
6853
6854 (defun gnus-summary-limit-to-author (from)
6855   "Limit the summary buffer to articles that have authors that match a regexp."
6856   (interactive "sLimit to author (regexp): ")
6857   (gnus-summary-limit-to-subject from "from"))
6858
6859 (defun gnus-summary-limit-to-age (age &optional younger-p)
6860   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6861 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6862 articles that are younger than AGE days."
6863   (interactive
6864    (let ((younger current-prefix-arg)
6865          (days-got nil)
6866          days)
6867      (while (not days-got)
6868        (setq days (if younger
6869                       (read-string "Limit to articles within (in days): ")
6870                     (read-string "Limit to articles older than (in days): ")))
6871        (when (> (length days) 0)
6872          (setq days (read days)))
6873        (if (numberp days)
6874            (setq days-got t)
6875          (message "Please enter a number.")
6876          (sleep-for 1)))
6877      (list days younger)))
6878   (prog1
6879       (let ((data gnus-newsgroup-data)
6880             (cutoff (days-to-time age))
6881             articles d date is-younger)
6882         (while (setq d (pop data))
6883           (when (and (vectorp (gnus-data-header d))
6884                      (setq date (mail-header-date (gnus-data-header d))))
6885             (setq is-younger (time-less-p
6886                               (time-since (condition-case ()
6887                                               (date-to-time date)
6888                                             (error '(0 0))))
6889                               cutoff))
6890             (when (if younger-p
6891                       is-younger
6892                     (not is-younger))
6893               (push (gnus-data-number d) articles))))
6894         (gnus-summary-limit (nreverse articles)))
6895     (gnus-summary-position-point)))
6896
6897 (defun gnus-summary-limit-to-extra (header regexp)
6898   "Limit the summary buffer to articles that match an 'extra' header."
6899   (interactive
6900    (let ((header
6901           (intern
6902            (gnus-completing-read
6903             (symbol-name (car gnus-extra-headers))
6904             "Limit extra header:"
6905             (mapcar (lambda (x)
6906                       (cons (symbol-name x) x))
6907                     gnus-extra-headers)
6908             nil
6909             t))))
6910      (list header
6911            (read-string (format "Limit to header %s (regexp): " header)))))
6912   (when (not (equal "" regexp))
6913     (prog1
6914         (let ((articles (gnus-summary-find-matching
6915                          (cons 'extra header) regexp 'all)))
6916           (unless articles
6917             (error "Found no matches for \"%s\"" regexp))
6918           (gnus-summary-limit articles))
6919       (gnus-summary-position-point))))
6920
6921 (defun gnus-summary-limit-to-display-predicate ()
6922   "Limit the summary buffer to the predicated in the `display' group parameter."
6923   (interactive)
6924   (unless gnus-newsgroup-display
6925     (error "There is no `diplay' group parameter"))
6926   (let (articles)
6927     (dolist (number gnus-newsgroup-articles)
6928       (when (funcall gnus-newsgroup-display)
6929         (push number articles)))
6930     (gnus-summary-limit articles))
6931   (gnus-summary-position-point))
6932
6933 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6934 (make-obsolete
6935  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6936
6937 (defun gnus-summary-limit-to-unread (&optional all)
6938   "Limit the summary buffer to articles that are not marked as read.
6939 If ALL is non-nil, limit strictly to unread articles."
6940   (interactive "P")
6941   (if all
6942       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6943     (gnus-summary-limit-to-marks
6944      ;; Concat all the marks that say that an article is read and have
6945      ;; those removed.
6946      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6947            gnus-killed-mark gnus-kill-file-mark
6948            gnus-low-score-mark gnus-expirable-mark
6949            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6950            gnus-duplicate-mark gnus-souped-mark)
6951      'reverse)))
6952
6953 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6954 (make-obsolete 'gnus-summary-delete-marked-with
6955                'gnus-summary-limit-exlude-marks)
6956
6957 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6958   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6959 If REVERSE, limit the summary buffer to articles that are marked
6960 with MARKS.  MARKS can either be a string of marks or a list of marks.
6961 Returns how many articles were removed."
6962   (interactive "sMarks: ")
6963   (gnus-summary-limit-to-marks marks t))
6964
6965 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6966   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6967 If REVERSE (the prefix), limit the summary buffer to articles that are
6968 not marked with MARKS.  MARKS can either be a string of marks or a
6969 list of marks.
6970 Returns how many articles were removed."
6971   (interactive "sMarks: \nP")
6972   (prog1
6973       (let ((data gnus-newsgroup-data)
6974             (marks (if (listp marks) marks
6975                      (append marks nil))) ; Transform to list.
6976             articles)
6977         (while data
6978           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6979                   (memq (gnus-data-mark (car data)) marks))
6980             (push (gnus-data-number (car data)) articles))
6981           (setq data (cdr data)))
6982         (gnus-summary-limit articles))
6983     (gnus-summary-position-point)))
6984
6985 (defun gnus-summary-limit-to-score (&optional score)
6986   "Limit to articles with score at or above SCORE."
6987   (interactive "P")
6988   (setq score (if score
6989                   (prefix-numeric-value score)
6990                 (or gnus-summary-default-score 0)))
6991   (let ((data gnus-newsgroup-data)
6992         articles)
6993     (while data
6994       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6995                 score)
6996         (push (gnus-data-number (car data)) articles))
6997       (setq data (cdr data)))
6998     (prog1
6999         (gnus-summary-limit articles)
7000       (gnus-summary-position-point))))
7001
7002 (defun gnus-summary-limit-include-thread (id)
7003   "Display all the hidden articles that is in the thread with ID in it.
7004 When called interactively, ID is the Message-ID of the current
7005 article."
7006   (interactive (list (mail-header-id (gnus-summary-article-header))))
7007   (let ((articles (gnus-articles-in-thread
7008                    (gnus-id-to-thread (gnus-root-id id)))))
7009     (prog1
7010         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7011       (gnus-summary-limit-include-matching-articles
7012        "subject"
7013        (regexp-quote (gnus-simplify-subject-re
7014                       (mail-header-subject (gnus-id-to-header id)))))
7015       (gnus-summary-position-point))))
7016
7017 (defun gnus-summary-limit-include-matching-articles (header regexp)
7018   "Display all the hidden articles that have HEADERs that match REGEXP."
7019   (interactive (list (read-string "Match on header: ")
7020                      (read-string "Regexp: ")))
7021   (let ((articles (gnus-find-matching-articles header regexp)))
7022     (prog1
7023         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7024       (gnus-summary-position-point))))
7025
7026 (defun gnus-summary-limit-include-dormant ()
7027   "Display all the hidden articles that are marked as dormant.
7028 Note that this command only works on a subset of the articles currently
7029 fetched for this group."
7030   (interactive)
7031   (unless gnus-newsgroup-dormant
7032     (error "There are no dormant articles in this group"))
7033   (prog1
7034       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7035     (gnus-summary-position-point)))
7036
7037 (defun gnus-summary-limit-exclude-dormant ()
7038   "Hide all dormant articles."
7039   (interactive)
7040   (prog1
7041       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7042     (gnus-summary-position-point)))
7043
7044 (defun gnus-summary-limit-exclude-childless-dormant ()
7045   "Hide all dormant articles that have no children."
7046   (interactive)
7047   (let ((data (gnus-data-list t))
7048         articles d children)
7049     ;; Find all articles that are either not dormant or have
7050     ;; children.
7051     (while (setq d (pop data))
7052       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7053                 (and (setq children
7054                            (gnus-article-children (gnus-data-number d)))
7055                      (let (found)
7056                        (while children
7057                          (when (memq (car children) articles)
7058                            (setq children nil
7059                                  found t))
7060                          (pop children))
7061                        found)))
7062         (push (gnus-data-number d) articles)))
7063     ;; Do the limiting.
7064     (prog1
7065         (gnus-summary-limit articles)
7066       (gnus-summary-position-point))))
7067
7068 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7069   "Mark all unread excluded articles as read.
7070 If ALL, mark even excluded ticked and dormants as read."
7071   (interactive "P")
7072   (let ((articles (gnus-sorted-complement
7073                    (sort
7074                     (mapcar (lambda (h) (mail-header-number h))
7075                             gnus-newsgroup-headers)
7076                     '<)
7077                    (sort gnus-newsgroup-limit '<)))
7078         article)
7079     (setq gnus-newsgroup-unreads
7080           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7081     (if all
7082         (setq gnus-newsgroup-dormant nil
7083               gnus-newsgroup-marked nil
7084               gnus-newsgroup-reads
7085               (nconc
7086                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7087                gnus-newsgroup-reads))
7088       (while (setq article (pop articles))
7089         (unless (or (memq article gnus-newsgroup-dormant)
7090                     (memq article gnus-newsgroup-marked))
7091           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7092
7093 (defun gnus-summary-limit (articles &optional pop)
7094   (if pop
7095       ;; We pop the previous limit off the stack and use that.
7096       (setq articles (car gnus-newsgroup-limits)
7097             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7098     ;; We use the new limit, so we push the old limit on the stack.
7099     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7100   ;; Set the limit.
7101   (setq gnus-newsgroup-limit articles)
7102   (let ((total (length gnus-newsgroup-data))
7103         (data (gnus-data-find-list (gnus-summary-article-number)))
7104         (gnus-summary-mark-below nil)   ; Inhibit this.
7105         found)
7106     ;; This will do all the work of generating the new summary buffer
7107     ;; according to the new limit.
7108     (gnus-summary-prepare)
7109     ;; Hide any threads, possibly.
7110     (and gnus-show-threads
7111          gnus-thread-hide-subtree
7112          (gnus-summary-hide-all-threads))
7113     ;; Try to return to the article you were at, or one in the
7114     ;; neighborhood.
7115     (when data
7116       ;; We try to find some article after the current one.
7117       (while data
7118         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7119           (setq data nil
7120                 found t))
7121         (setq data (cdr data))))
7122     (unless found
7123       ;; If there is no data, that means that we were after the last
7124       ;; article.  The same goes when we can't find any articles
7125       ;; after the current one.
7126       (goto-char (point-max))
7127       (gnus-summary-find-prev))
7128     (gnus-set-mode-line 'summary)
7129     ;; We return how many articles were removed from the summary
7130     ;; buffer as a result of the new limit.
7131     (- total (length gnus-newsgroup-data))))
7132
7133 (defsubst gnus-invisible-cut-children (threads)
7134   (let ((num 0))
7135     (while threads
7136       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7137         (incf num))
7138       (pop threads))
7139     (< num 2)))
7140
7141 (defsubst gnus-cut-thread (thread)
7142   "Go forwards in the thread until we find an article that we want to display."
7143   (when (or (eq gnus-fetch-old-headers 'some)
7144             (eq gnus-fetch-old-headers 'invisible)
7145             (numberp gnus-fetch-old-headers)
7146             (eq gnus-build-sparse-threads 'some)
7147             (eq gnus-build-sparse-threads 'more))
7148     ;; Deal with old-fetched headers and sparse threads.
7149     (while (and
7150             thread
7151             (or
7152              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7153              (gnus-summary-article-ancient-p
7154               (mail-header-number (car thread))))
7155             (if (or (<= (length (cdr thread)) 1)
7156                     (eq gnus-fetch-old-headers 'invisible))
7157                 (setq gnus-newsgroup-limit
7158                       (delq (mail-header-number (car thread))
7159                             gnus-newsgroup-limit)
7160                       thread (cadr thread))
7161               (when (gnus-invisible-cut-children (cdr thread))
7162                 (let ((th (cdr thread)))
7163                   (while th
7164                     (if (memq (mail-header-number (caar th))
7165                               gnus-newsgroup-limit)
7166                         (setq thread (car th)
7167                               th nil)
7168                       (setq th (cdr th))))))))))
7169   thread)
7170
7171 (defun gnus-cut-threads (threads)
7172   "Cut off all uninteresting articles from the beginning of threads."
7173   (when (or (eq gnus-fetch-old-headers 'some)
7174             (eq gnus-fetch-old-headers 'invisible)
7175             (numberp gnus-fetch-old-headers)
7176             (eq gnus-build-sparse-threads 'some)
7177             (eq gnus-build-sparse-threads 'more))
7178     (let ((th threads))
7179       (while th
7180         (setcar th (gnus-cut-thread (car th)))
7181         (setq th (cdr th)))))
7182   ;; Remove nixed out threads.
7183   (delq nil threads))
7184
7185 (defun gnus-summary-initial-limit (&optional show-if-empty)
7186   "Figure out what the initial limit is supposed to be on group entry.
7187 This entails weeding out unwanted dormants, low-scored articles,
7188 fetch-old-headers verbiage, and so on."
7189   ;; Most groups have nothing to remove.
7190   (if (or gnus-inhibit-limiting
7191           (and (null gnus-newsgroup-dormant)
7192                (eq gnus-newsgroup-display 'gnus-not-ignore)
7193                (not (eq gnus-fetch-old-headers 'some))
7194                (not (numberp gnus-fetch-old-headers))
7195                (not (eq gnus-fetch-old-headers 'invisible))
7196                (null gnus-summary-expunge-below)
7197                (not (eq gnus-build-sparse-threads 'some))
7198                (not (eq gnus-build-sparse-threads 'more))
7199                (null gnus-thread-expunge-below)
7200                (not gnus-use-nocem)))
7201       ()                                ; Do nothing.
7202     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7203     (setq gnus-newsgroup-limit nil)
7204     (mapatoms
7205      (lambda (node)
7206        (unless (car (symbol-value node))
7207          ;; These threads have no parents -- they are roots.
7208          (let ((nodes (cdr (symbol-value node)))
7209                thread)
7210            (while nodes
7211              (if (and gnus-thread-expunge-below
7212                       (< (gnus-thread-total-score (car nodes))
7213                          gnus-thread-expunge-below))
7214                  (gnus-expunge-thread (pop nodes))
7215                (setq thread (pop nodes))
7216                (gnus-summary-limit-children thread))))))
7217      gnus-newsgroup-dependencies)
7218     ;; If this limitation resulted in an empty group, we might
7219     ;; pop the previous limit and use it instead.
7220     (when (and (not gnus-newsgroup-limit)
7221                show-if-empty)
7222       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7223     gnus-newsgroup-limit))
7224
7225 (defun gnus-summary-limit-children (thread)
7226   "Return 1 if this subthread is visible and 0 if it is not."
7227   ;; First we get the number of visible children to this thread.  This
7228   ;; is done by recursing down the thread using this function, so this
7229   ;; will really go down to a leaf article first, before slowly
7230   ;; working its way up towards the root.
7231   (when thread
7232     (let ((children
7233            (if (cdr thread)
7234                (apply '+ (mapcar 'gnus-summary-limit-children
7235                                  (cdr thread)))
7236              0))
7237           (number (mail-header-number (car thread)))
7238           score)
7239       (if (and
7240            (not (memq number gnus-newsgroup-marked))
7241            (or
7242             ;; If this article is dormant and has absolutely no visible
7243             ;; children, then this article isn't visible.
7244             (and (memq number gnus-newsgroup-dormant)
7245                  (zerop children))
7246             ;; If this is "fetch-old-headered" and there is no
7247             ;; visible children, then we don't want this article.
7248             (and (or (eq gnus-fetch-old-headers 'some)
7249                      (numberp gnus-fetch-old-headers))
7250                  (gnus-summary-article-ancient-p number)
7251                  (zerop children))
7252             ;; If this is "fetch-old-headered" and `invisible', then
7253             ;; we don't want this article.
7254             (and (eq gnus-fetch-old-headers 'invisible)
7255                  (gnus-summary-article-ancient-p number))
7256             ;; If this is a sparsely inserted article with no children,
7257             ;; we don't want it.
7258             (and (eq gnus-build-sparse-threads 'some)
7259                  (gnus-summary-article-sparse-p number)
7260                  (zerop children))
7261             ;; If we use expunging, and this article is really
7262             ;; low-scored, then we don't want this article.
7263             (when (and gnus-summary-expunge-below
7264                        (< (setq score
7265                                 (or (cdr (assq number gnus-newsgroup-scored))
7266                                     gnus-summary-default-score))
7267                           gnus-summary-expunge-below))
7268               ;; We increase the expunge-tally here, but that has
7269               ;; nothing to do with the limits, really.
7270               (incf gnus-newsgroup-expunged-tally)
7271               ;; We also mark as read here, if that's wanted.
7272               (when (and gnus-summary-mark-below
7273                          (< score gnus-summary-mark-below))
7274                 (setq gnus-newsgroup-unreads
7275                       (delq number gnus-newsgroup-unreads))
7276                 (if gnus-newsgroup-auto-expire
7277                     (push number gnus-newsgroup-expirable)
7278                   (push (cons number gnus-low-score-mark)
7279                         gnus-newsgroup-reads)))
7280               t)
7281             ;; Do the `display' group parameter.
7282             (and gnus-newsgroup-display
7283                  (not (funcall gnus-newsgroup-display)))
7284             ;; Check NoCeM things.
7285             (if (and gnus-use-nocem
7286                      (gnus-nocem-unwanted-article-p
7287                       (mail-header-id (car thread))))
7288                 (progn
7289                   (setq gnus-newsgroup-unreads
7290                         (delq number gnus-newsgroup-unreads))
7291                   t))))
7292           ;; Nope, invisible article.
7293           0
7294         ;; Ok, this article is to be visible, so we add it to the limit
7295         ;; and return 1.
7296         (push number gnus-newsgroup-limit)
7297         1))))
7298
7299 (defun gnus-expunge-thread (thread)
7300   "Mark all articles in THREAD as read."
7301   (let* ((number (mail-header-number (car thread))))
7302     (incf gnus-newsgroup-expunged-tally)
7303     ;; We also mark as read here, if that's wanted.
7304     (setq gnus-newsgroup-unreads
7305           (delq number gnus-newsgroup-unreads))
7306     (if gnus-newsgroup-auto-expire
7307         (push number gnus-newsgroup-expirable)
7308       (push (cons number gnus-low-score-mark)
7309             gnus-newsgroup-reads)))
7310   ;; Go recursively through all subthreads.
7311   (mapcar 'gnus-expunge-thread (cdr thread)))
7312
7313 ;; Summary article oriented commands
7314
7315 (defun gnus-summary-refer-parent-article (n)
7316   "Refer parent article N times.
7317 If N is negative, go to ancestor -N instead.
7318 The difference between N and the number of articles fetched is returned."
7319   (interactive "p")
7320   (let ((skip 1)
7321         error header ref)
7322     (when (not (natnump n))
7323       (setq skip (abs n)
7324             n 1))
7325     (while (and (> n 0)
7326                 (not error))
7327       (setq header (gnus-summary-article-header))
7328       (if (and (eq (mail-header-number header)
7329                    (cdr gnus-article-current))
7330                (equal gnus-newsgroup-name
7331                       (car gnus-article-current)))
7332           ;; If we try to find the parent of the currently
7333           ;; displayed article, then we take a look at the actual
7334           ;; References header, since this is slightly more
7335           ;; reliable than the References field we got from the
7336           ;; server.
7337           (save-excursion
7338             (set-buffer gnus-original-article-buffer)
7339             (nnheader-narrow-to-headers)
7340             (unless (setq ref (message-fetch-field "references"))
7341               (setq ref (message-fetch-field "in-reply-to")))
7342             (widen))
7343         (setq ref
7344               ;; It's not the current article, so we take a bet on
7345               ;; the value we got from the server.
7346               (mail-header-references header)))
7347       (if (and ref
7348                (not (equal ref "")))
7349           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7350             (gnus-message 1 "Couldn't find parent"))
7351         (gnus-message 1 "No references in article %d"
7352                       (gnus-summary-article-number))
7353         (setq error t))
7354       (decf n))
7355     (gnus-summary-position-point)
7356     n))
7357
7358 (defun gnus-summary-refer-references ()
7359   "Fetch all articles mentioned in the References header.
7360 Return the number of articles fetched."
7361   (interactive)
7362   (let ((ref (mail-header-references (gnus-summary-article-header)))
7363         (current (gnus-summary-article-number))
7364         (n 0))
7365     (if (or (not ref)
7366             (equal ref ""))
7367         (error "No References in the current article")
7368       ;; For each Message-ID in the References header...
7369       (while (string-match "<[^>]*>" ref)
7370         (incf n)
7371         ;; ... fetch that article.
7372         (gnus-summary-refer-article
7373          (prog1 (match-string 0 ref)
7374            (setq ref (substring ref (match-end 0))))))
7375       (gnus-summary-goto-subject current)
7376       (gnus-summary-position-point)
7377       n)))
7378
7379 (defun gnus-summary-refer-thread (&optional limit)
7380   "Fetch all articles in the current thread.
7381 If LIMIT (the numerical prefix), fetch that many old headers instead
7382 of what's specified by the `gnus-refer-thread-limit' variable."
7383   (interactive "P")
7384   (let ((id (mail-header-id (gnus-summary-article-header)))
7385         (limit (if limit (prefix-numeric-value limit)
7386                  gnus-refer-thread-limit)))
7387     ;; We want to fetch LIMIT *old* headers, but we also have to
7388     ;; re-fetch all the headers in the current buffer, because many of
7389     ;; them may be undisplayed.  So we adjust LIMIT.
7390     (when (numberp limit)
7391       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7392     (unless (eq gnus-fetch-old-headers 'invisible)
7393       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7394       ;; Retrieve the headers and read them in.
7395       (if (eq (gnus-retrieve-headers
7396                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7397               'nov)
7398           (gnus-build-all-threads)
7399         (error "Can't fetch thread from backends that don't support NOV"))
7400       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7401     (gnus-summary-limit-include-thread id)))
7402
7403 (defun gnus-summary-refer-article (message-id)
7404   "Fetch an article specified by MESSAGE-ID."
7405   (interactive "sMessage-ID: ")
7406   (when (and (stringp message-id)
7407              (not (zerop (length message-id))))
7408     ;; Construct the correct Message-ID if necessary.
7409     ;; Suggested by tale@pawl.rpi.edu.
7410     (unless (string-match "^<" message-id)
7411       (setq message-id (concat "<" message-id)))
7412     (unless (string-match ">$" message-id)
7413       (setq message-id (concat message-id ">")))
7414     (let* ((header (gnus-id-to-header message-id))
7415            (sparse (and header
7416                         (gnus-summary-article-sparse-p
7417                          (mail-header-number header))
7418                         (memq (mail-header-number header)
7419                               gnus-newsgroup-limit)))
7420            number)
7421       (cond
7422        ;; If the article is present in the buffer we just go to it.
7423        ((and header
7424              (or (not (gnus-summary-article-sparse-p
7425                        (mail-header-number header)))
7426                  sparse))
7427         (prog1
7428             (gnus-summary-goto-article
7429              (mail-header-number header) nil t)
7430           (when sparse
7431             (gnus-summary-update-article (mail-header-number header)))))
7432        (t
7433         ;; We fetch the article.
7434         (catch 'found
7435           (dolist (gnus-override-method (gnus-refer-article-methods))
7436             (gnus-check-server gnus-override-method)
7437             ;; Fetch the header, and display the article.
7438             (when (setq number (gnus-summary-insert-subject message-id))
7439               (gnus-summary-select-article nil nil nil number)
7440               (throw 'found t)))
7441           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7442
7443 (defun gnus-refer-article-methods ()
7444   "Return a list of referrable methods."
7445   (cond
7446    ;; No method, so we default to current and native.
7447    ((null gnus-refer-article-method)
7448     (list gnus-current-select-method gnus-select-method))
7449    ;; Current.
7450    ((eq 'current gnus-refer-article-method)
7451     (list gnus-current-select-method))
7452    ;; List of select methods.
7453    ((not (and (symbolp (car gnus-refer-article-method))
7454               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7455     (let (out)
7456       (dolist (method gnus-refer-article-method)
7457         (push (if (eq 'current method)
7458                   gnus-current-select-method
7459                 method)
7460               out))
7461       (nreverse out)))
7462    ;; One single select method.
7463    (t
7464     (list gnus-refer-article-method))))
7465
7466 (defun gnus-summary-edit-parameters ()
7467   "Edit the group parameters of the current group."
7468   (interactive)
7469   (gnus-group-edit-group gnus-newsgroup-name 'params))
7470
7471 (defun gnus-summary-customize-parameters ()
7472   "Customize the group parameters of the current group."
7473   (interactive)
7474   (gnus-group-customize gnus-newsgroup-name))
7475
7476 (defun gnus-summary-enter-digest-group (&optional force)
7477   "Enter an nndoc group based on the current article.
7478 If FORCE, force a digest interpretation.  If not, try
7479 to guess what the document format is."
7480   (interactive "P")
7481   (let ((conf gnus-current-window-configuration))
7482     (save-excursion
7483       (gnus-summary-select-article))
7484     (setq gnus-current-window-configuration conf)
7485     (let* ((name (format "%s-%d"
7486                          (gnus-group-prefixed-name
7487                           gnus-newsgroup-name (list 'nndoc ""))
7488                          (save-excursion
7489                            (set-buffer gnus-summary-buffer)
7490                            gnus-current-article)))
7491            (ogroup gnus-newsgroup-name)
7492            (params (append (gnus-info-params (gnus-get-info ogroup))
7493                            (list (cons 'to-group ogroup))
7494                            (list (cons 'save-article-group ogroup))))
7495            (case-fold-search t)
7496            (buf (current-buffer))
7497            dig to-address)
7498       (save-excursion
7499         (set-buffer gnus-original-article-buffer)
7500         ;; Have the digest group inherit the main mail address of
7501         ;; the parent article.
7502         (when (setq to-address (or (message-fetch-field "reply-to")
7503                                    (message-fetch-field "from")))
7504           (setq params (append
7505                         (list (cons 'to-address
7506                                     (funcall gnus-decode-encoded-word-function
7507                                              to-address))))))
7508         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7509         (insert-buffer-substring gnus-original-article-buffer)
7510         ;; Remove lines that may lead nndoc to misinterpret the
7511         ;; document type.
7512         (narrow-to-region
7513          (goto-char (point-min))
7514          (or (search-forward "\n\n" nil t) (point)))
7515         (goto-char (point-min))
7516         (delete-matching-lines "^Path:\\|^From ")
7517         (widen))
7518       (unwind-protect
7519           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7520                     (gnus-newsgroup-ephemeral-ignored-charsets
7521                      gnus-newsgroup-ignored-charsets))
7522                 (gnus-group-read-ephemeral-group
7523                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7524                               (nndoc-article-type
7525                                ,(if force 'mbox 'guess))) t))
7526               ;; Make all postings to this group go to the parent group.
7527               (nconc (gnus-info-params (gnus-get-info name))
7528                      params)
7529             ;; Couldn't select this doc group.
7530             (switch-to-buffer buf)
7531             (gnus-set-global-variables)
7532             (gnus-configure-windows 'summary)
7533             (gnus-message 3 "Article couldn't be entered?"))
7534         (kill-buffer dig)))))
7535
7536 (defun gnus-summary-read-document (n)
7537   "Open a new group based on the current article(s).
7538 This will allow you to read digests and other similar
7539 documents as newsgroups.
7540 Obeys the standard process/prefix convention."
7541   (interactive "P")
7542   (let* ((articles (gnus-summary-work-articles n))
7543          (ogroup gnus-newsgroup-name)
7544          (params (append (gnus-info-params (gnus-get-info ogroup))
7545                          (list (cons 'to-group ogroup))))
7546          article group egroup groups vgroup)
7547     (while (setq article (pop articles))
7548       (setq group (format "%s-%d" gnus-newsgroup-name article))
7549       (gnus-summary-remove-process-mark article)
7550       (when (gnus-summary-display-article article)
7551         (save-excursion
7552           (with-temp-buffer
7553             (insert-buffer-substring gnus-original-article-buffer)
7554             ;; Remove some headers that may lead nndoc to make
7555             ;; the wrong guess.
7556             (message-narrow-to-head)
7557             (goto-char (point-min))
7558             (delete-matching-lines "^\\(Path\\):\\|^From ")
7559             (widen)
7560             (if (setq egroup
7561                       (gnus-group-read-ephemeral-group
7562                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7563                                      (nndoc-article-type guess))
7564                        t nil t))
7565                 (progn
7566                   ;; Make all postings to this group go to the parent group.
7567                   (nconc (gnus-info-params (gnus-get-info egroup))
7568                          params)
7569                   (push egroup groups))
7570               ;; Couldn't select this doc group.
7571               (gnus-error 3 "Article couldn't be entered"))))))
7572     ;; Now we have selected all the documents.
7573     (cond
7574      ((not groups)
7575       (error "None of the articles could be interpreted as documents"))
7576      ((gnus-group-read-ephemeral-group
7577        (setq vgroup (format
7578                      "nnvirtual:%s-%s" gnus-newsgroup-name
7579                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7580        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7581        t
7582        (cons (current-buffer) 'summary)))
7583      (t
7584       (error "Couldn't select virtual nndoc group")))))
7585
7586 (defun gnus-summary-isearch-article (&optional regexp-p)
7587   "Do incremental search forward on the current article.
7588 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7589   (interactive "P")
7590   (let* ((gnus-inhibit-treatment t)
7591          (old (gnus-summary-select-article)))
7592     (gnus-configure-windows 'article)
7593     (gnus-eval-in-buffer-window gnus-article-buffer
7594       (save-restriction
7595         (widen)
7596         (when (eq 'old old)
7597           (gnus-article-show-all-headers))
7598         (goto-char (point-min))
7599         (isearch-forward regexp-p)))))
7600
7601 (defun gnus-summary-search-article-forward (regexp &optional backward)
7602   "Search for an article containing REGEXP forward.
7603 If BACKWARD, search backward instead."
7604   (interactive
7605    (list (read-string
7606           (format "Search article %s (regexp%s): "
7607                   (if current-prefix-arg "backward" "forward")
7608                   (if gnus-last-search-regexp
7609                       (concat ", default " gnus-last-search-regexp)
7610                     "")))
7611          current-prefix-arg))
7612   (if (string-equal regexp "")
7613       (setq regexp (or gnus-last-search-regexp ""))
7614     (setq gnus-last-search-regexp regexp)
7615     (setq gnus-article-before-search gnus-current-article))
7616   ;; Intentionally set gnus-last-article.
7617   (setq gnus-last-article gnus-article-before-search)
7618   (let ((gnus-last-article gnus-last-article))
7619     (if (gnus-summary-search-article regexp backward)
7620         (gnus-summary-show-thread)
7621       (error "Search failed: \"%s\"" regexp))))
7622
7623 (defun gnus-summary-search-article-backward (regexp)
7624   "Search for an article containing REGEXP backward."
7625   (interactive
7626    (list (read-string
7627           (format "Search article backward (regexp%s): "
7628                   (if gnus-last-search-regexp
7629                       (concat ", default " gnus-last-search-regexp)
7630                     "")))))
7631   (gnus-summary-search-article-forward regexp 'backward))
7632
7633 (eval-when-compile
7634   (defmacro gnus-summary-search-article-position-point (regexp backward)
7635     "Dehighlight the last matched text and goto the beginning position."
7636     (` (if (and gnus-summary-search-article-matched-data
7637                 (let ((text (caddr gnus-summary-search-article-matched-data))
7638                       (inhibit-read-only t)
7639                       buffer-read-only)
7640                   (delete-region
7641                    (goto-char (car gnus-summary-search-article-matched-data))
7642                    (cadr gnus-summary-search-article-matched-data))
7643                   (insert text)
7644                   (string-match (, regexp) text)))
7645            (if (, backward) (beginning-of-line) (end-of-line))
7646          (goto-char (if (, backward) (point-max) (point-min))))))
7647
7648   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7649     "Place point where X-Face image is displayed."
7650     (if (featurep 'xemacs)
7651         (` (let ((end (if (search-forward "\n\n" nil t)
7652                           (goto-char (1- (point)))
7653                         (point-min)))
7654                  extent)
7655              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7656              (unless (and (re-search-forward "^From:" end t)
7657                           (setq extent (extent-at (point)))
7658                           (extent-begin-glyph extent))
7659                (goto-char (, opoint)))))
7660       (` (let ((end (if (search-forward "\n\n" nil t)
7661                         (goto-char (1- (point)))
7662                       (point-min)))
7663                (start (or (search-backward "\n\n" nil t) (point-min))))
7664            (goto-char
7665             (or (text-property-any start end 'x-face-image t);; x-face-e21
7666                 (text-property-any start end 'x-face-mule-bitmap-image t)
7667                 (, opoint)))))))
7668
7669   (defmacro gnus-summary-search-article-highlight-matched-text
7670     (backward treated x-face)
7671     "Highlight matched text in the function `gnus-summary-search-article'."
7672     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7673              (end (set-marker (make-marker) (match-end 0)))
7674              (inhibit-read-only t)
7675              buffer-read-only)
7676          (unless treated
7677            (let ((,@
7678                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7679                     (mapcar
7680                      (lambda (item) (setq items (delq item items)))
7681                      '(gnus-treat-buttonize
7682                        gnus-treat-fill-article
7683                        gnus-treat-fill-long-lines
7684                        gnus-treat-emphasize
7685                        gnus-treat-highlight-headers
7686                        gnus-treat-highlight-citation
7687                        gnus-treat-highlight-signature
7688                        gnus-treat-overstrike
7689                        gnus-treat-display-xface
7690                        gnus-treat-buttonize-head
7691                        gnus-treat-decode-article-as-default-mime-charset))
7692                     (static-if (featurep 'xemacs)
7693                         items
7694                       (cons '(x-face-mule-delete-x-face-field
7695                               (quote never))
7696                             items))))
7697                  (gnus-treat-display-xface
7698                   (when (, x-face) gnus-treat-display-xface)))
7699              (gnus-article-prepare-mime-display)))
7700          (goto-char (if (, backward) start end))
7701          (when (, x-face)
7702            (gnus-summary-search-article-highlight-goto-x-face (point)))
7703          (setq gnus-summary-search-article-matched-data
7704                (list start end (buffer-substring start end)))
7705          (unless (eq start end);; matched text has been deleted. :-<
7706            (put-text-property start end 'face
7707                               (or (find-face 'isearch)
7708                                   'secondary-selection))))))
7709   )
7710
7711 (defun gnus-summary-search-article (regexp &optional backward)
7712   "Search for an article containing REGEXP.
7713 Optional argument BACKWARD means do search for backward.
7714 `gnus-select-article-hook' is not called during the search."
7715   ;; We have to require this here to make sure that the following
7716   ;; dynamic binding isn't shadowed by autoloading.
7717   (require 'gnus-async)
7718   (require 'gnus-art)
7719   (let ((gnus-select-article-hook nil)  ;Disable hook.
7720         (gnus-article-prepare-hook nil)
7721         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7722         (gnus-use-article-prefetch nil)
7723         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7724         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7725         (sum (current-buffer))
7726         (found nil)
7727         point treated)
7728     (gnus-save-hidden-threads
7729       (static-if (featurep 'xemacs)
7730           (let ((gnus-inhibit-treatment t))
7731             (setq treated (eq 'old (gnus-summary-select-article)))
7732             (when (and treated
7733                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7734                                  (window-live-p (get-buffer-window
7735                                                  gnus-article-buffer t)))))
7736               (gnus-summary-select-article nil t)
7737               (setq treated nil)))
7738         (let ((gnus-inhibit-treatment t)
7739               (x-face-mule-delete-x-face-field 'never))
7740           (setq treated (eq 'old (gnus-summary-select-article)))
7741           (when (and treated
7742                      (not
7743                       (and (gnus-buffer-live-p gnus-article-buffer)
7744                            (window-live-p (get-buffer-window
7745                                            gnus-article-buffer t))
7746                            (or (not (string-match "^\\^X-Face:" regexp))
7747                                (with-current-buffer gnus-article-buffer
7748                                  gnus-summary-search-article-matched-data)))))
7749             (gnus-summary-select-article nil t)
7750             (setq treated nil))))
7751       (set-buffer gnus-article-buffer)
7752       (widen)
7753       (if treated
7754           (progn
7755             (gnus-article-show-all-headers)
7756             (gnus-summary-search-article-position-point regexp backward))
7757         (goto-char (if backward (point-max) (point-min))))
7758       (while (not found)
7759         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7760         (if (if backward
7761                 (re-search-backward regexp nil t)
7762               (re-search-forward regexp nil t))
7763             ;; We found the regexp.
7764             (progn
7765               (gnus-summary-search-article-highlight-matched-text
7766                backward treated (string-match "^\\^X-Face:" regexp))
7767               (setq found 'found)
7768               (forward-line
7769                (/ (- 2 (window-height
7770                         (get-buffer-window gnus-article-buffer t)))
7771                   2))
7772               (set-window-start
7773                (get-buffer-window (current-buffer))
7774                (point))
7775               (set-buffer sum)
7776               (setq point (point)))
7777           ;; We didn't find it, so we go to the next article.
7778           (set-buffer sum)
7779           (setq found 'not)
7780           (while (eq found 'not)
7781             (if (not (if backward (gnus-summary-find-prev)
7782                        (gnus-summary-find-next)))
7783                 ;; No more articles.
7784                 (setq found t)
7785               ;; Select the next article and adjust point.
7786               (unless (gnus-summary-article-sparse-p
7787                        (gnus-summary-article-number))
7788                 (setq found nil)
7789                 (let ((gnus-inhibit-treatment t))
7790                   (gnus-summary-select-article))
7791                 (setq treated nil)
7792                 (set-buffer gnus-article-buffer)
7793                 (widen)
7794                 (goto-char (if backward (point-max) (point-min))))))))
7795       (gnus-message 7 ""))
7796     ;; Return whether we found the regexp.
7797     (when (eq found 'found)
7798       (goto-char point)
7799       (gnus-summary-show-thread)
7800       (gnus-summary-goto-subject gnus-current-article)
7801       (gnus-summary-position-point)
7802       t)))
7803
7804 (defun gnus-find-matching-articles (header regexp)
7805   "Return a list of all articles that match REGEXP on HEADER.
7806 This search includes all articles in the current group that Gnus has
7807 fetched headers for, whether they are displayed or not."
7808   (let ((articles nil)
7809         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7810         (case-fold-search t))
7811     (dolist (header gnus-newsgroup-headers)
7812       (when (string-match regexp (funcall func header))
7813         (push (mail-header-number header) articles)))
7814     (nreverse articles)))
7815
7816 (defun gnus-summary-find-matching (header regexp &optional backward unread
7817                                           not-case-fold)
7818   "Return a list of all articles that match REGEXP on HEADER.
7819 The search stars on the current article and goes forwards unless
7820 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7821 If UNREAD is non-nil, only unread articles will
7822 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7823 in the comparisons."
7824   (let ((case-fold-search (not not-case-fold))
7825         articles d func)
7826     (if (consp header)
7827         (if (eq (car header) 'extra)
7828             (setq func
7829                   `(lambda (h)
7830                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7831                          "")))
7832           (error "%s is an invalid header" header))
7833       (unless (fboundp (intern (concat "mail-header-" header)))
7834         (error "%s is not a valid header" header))
7835       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7836     (dolist (d (if (eq backward 'all)
7837                    gnus-newsgroup-data
7838                  (gnus-data-find-list
7839                   (gnus-summary-article-number)
7840                   (gnus-data-list backward))))
7841       (when (and (or (not unread)       ; We want all articles...
7842                      (gnus-data-unread-p d)) ; Or just unreads.
7843                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7844                  (string-match regexp
7845                                (funcall func (gnus-data-header d)))) ; Match.
7846         (push (gnus-data-number d) articles))) ; Success!
7847     (nreverse articles)))
7848
7849 (defun gnus-summary-execute-command (header regexp command &optional backward)
7850   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7851 If HEADER is an empty string (or nil), the match is done on the entire
7852 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7853   (interactive
7854    (list (let ((completion-ignore-case t))
7855            (completing-read
7856             "Header name: "
7857             (mapcar (lambda (header) (list (format "%s" header)))
7858                     (append
7859                      '("Number" "Subject" "From" "Lines" "Date"
7860                        "Message-ID" "Xref" "References" "Body")
7861                      gnus-extra-headers))
7862             nil 'require-match))
7863          (read-string "Regexp: ")
7864          (read-key-sequence "Command: ")
7865          current-prefix-arg))
7866   (when (equal header "Body")
7867     (setq header ""))
7868   ;; Hidden thread subtrees must be searched as well.
7869   (gnus-summary-show-all-threads)
7870   ;; We don't want to change current point nor window configuration.
7871   (save-excursion
7872     (save-window-excursion
7873       (gnus-message 6 "Executing %s..." (key-description command))
7874       ;; We'd like to execute COMMAND interactively so as to give arguments.
7875       (gnus-execute header regexp
7876                     `(call-interactively ',(key-binding command))
7877                     backward)
7878       (gnus-message 6 "Executing %s...done" (key-description command)))))
7879
7880 (defun gnus-summary-beginning-of-article ()
7881   "Scroll the article back to the beginning."
7882   (interactive)
7883   (gnus-summary-select-article)
7884   (gnus-configure-windows 'article)
7885   (gnus-eval-in-buffer-window gnus-article-buffer
7886     (widen)
7887     (goto-char (point-min))
7888     (when gnus-page-broken
7889       (gnus-narrow-to-page))))
7890
7891 (defun gnus-summary-end-of-article ()
7892   "Scroll to the end of the article."
7893   (interactive)
7894   (gnus-summary-select-article)
7895   (gnus-configure-windows 'article)
7896   (gnus-eval-in-buffer-window gnus-article-buffer
7897     (widen)
7898     (goto-char (point-max))
7899     (recenter -3)
7900     (when gnus-page-broken
7901       (gnus-narrow-to-page))))
7902
7903 (defun gnus-summary-print-article (&optional filename n)
7904   "Generate and print a PostScript image of the N next (mail) articles.
7905
7906 If N is negative, print the N previous articles.  If N is nil and articles
7907 have been marked with the process mark, print these instead.
7908
7909 If the optional first argument FILENAME is nil, send the image to the
7910 printer.  If FILENAME is a string, save the PostScript image in a file with
7911 that name.  If FILENAME is a number, prompt the user for the name of the file
7912 to save in."
7913   (interactive (list (ps-print-preprint current-prefix-arg)))
7914   (dolist (article (gnus-summary-work-articles n))
7915     (gnus-summary-select-article nil nil 'pseudo article)
7916     (gnus-eval-in-buffer-window gnus-article-buffer
7917       (let ((buffer (generate-new-buffer " *print*")))
7918         (unwind-protect
7919             (progn
7920               (copy-to-buffer buffer (point-min) (point-max))
7921               (set-buffer buffer)
7922               (gnus-article-delete-invisible-text)
7923               (when (gnus-visual-p 'article-highlight 'highlight)
7924                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7925                 ;; highlight.
7926                 (let ((gnus-article-buffer buffer))
7927                   (gnus-article-highlight-citation t)
7928                   (gnus-article-highlight-signature)))
7929               (let ((ps-left-header
7930                      (list
7931                       (concat "("
7932                               (mail-header-subject gnus-current-headers) ")")
7933                       (concat "("
7934                               (mail-header-from gnus-current-headers) ")")))
7935                     (ps-right-header
7936                      (list
7937                       "/pagenumberstring load"
7938                       (concat "("
7939                               (mail-header-date gnus-current-headers) ")"))))
7940                 (gnus-run-hooks 'gnus-ps-print-hook)
7941                 (save-excursion
7942                   (if window-system
7943                       (ps-spool-buffer-with-faces)
7944                     (ps-spool-buffer)))))
7945           (kill-buffer buffer))))
7946     (gnus-summary-remove-process-mark article))
7947   (ps-despool filename))
7948
7949 (defun gnus-summary-show-article (&optional arg)
7950   "Force re-fetching of the current article.
7951 If ARG (the prefix) is a number, show the article with the charset
7952 defined in `gnus-summary-show-article-charset-alist', or the charset
7953 input.
7954 If ARG (the prefix) is non-nil and not a number, show the raw article
7955 without any article massaging functions being run."
7956   (interactive "P")
7957   (cond
7958    ((numberp arg)
7959     (gnus-summary-show-article t)
7960     (let ((gnus-newsgroup-charset
7961            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7962                (mm-read-coding-system
7963                 "View as charset: "
7964                 (save-excursion
7965                   (set-buffer gnus-article-buffer)
7966                   (let ((coding-systems
7967                          (detect-coding-region (point) (point-max))))
7968                     (or (car-safe coding-systems)
7969                         coding-systems))))))
7970           (gnus-newsgroup-ignored-charsets 'gnus-all))
7971       (gnus-summary-select-article nil 'force)
7972       (let ((deps gnus-newsgroup-dependencies)
7973             head header)
7974         (save-excursion
7975           (set-buffer gnus-original-article-buffer)
7976           (save-restriction
7977             (message-narrow-to-head)
7978             (setq head (buffer-string)))
7979           (with-temp-buffer
7980             (insert (format "211 %d Article retrieved.\n"
7981                             (cdr gnus-article-current)))
7982             (insert head)
7983             (insert ".\n")
7984             (let ((nntp-server-buffer (current-buffer)))
7985               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7986         (gnus-data-set-header
7987          (gnus-data-find (cdr gnus-article-current))
7988          header)
7989         (gnus-summary-update-article-line
7990          (cdr gnus-article-current) header)
7991         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7992           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7993    ((not arg)
7994     ;; Select the article the normal way.
7995     (gnus-summary-select-article nil 'force))
7996    (t
7997     ;; We have to require this here to make sure that the following
7998     ;; dynamic binding isn't shadowed by autoloading.
7999     (require 'gnus-async)
8000     (require 'gnus-art)
8001     ;; Bind the article treatment functions to nil.
8002     (let ((gnus-have-all-headers t)
8003           gnus-article-prepare-hook
8004           gnus-article-decode-hook
8005           gnus-break-pages
8006           gnus-show-mime
8007           (gnus-inhibit-treatment t))
8008       (gnus-summary-select-article nil 'force))))
8009   (gnus-summary-goto-subject gnus-current-article)
8010   (gnus-summary-position-point))
8011
8012 (defun gnus-summary-show-raw-article ()
8013   "Show the raw article without any article massaging functions being run."
8014   (interactive)
8015   (gnus-summary-show-article t))
8016
8017 (defun gnus-summary-verbose-headers (&optional arg)
8018   "Toggle permanent full header display.
8019 If ARG is a positive number, turn header display on.
8020 If ARG is a negative number, turn header display off."
8021   (interactive "P")
8022   (setq gnus-show-all-headers
8023         (cond ((or (not (numberp arg))
8024                    (zerop arg))
8025                (not gnus-show-all-headers))
8026               ((natnump arg)
8027                t)))
8028   (gnus-summary-show-article))
8029
8030 (defun gnus-summary-toggle-header (&optional arg)
8031   "Show the headers if they are hidden, or hide them if they are shown.
8032 If ARG is a positive number, show the entire header.
8033 If ARG is a negative number, hide the unwanted header lines."
8034   (interactive "P")
8035   (save-excursion
8036     (set-buffer gnus-article-buffer)
8037     (save-restriction
8038       (let* ((buffer-read-only nil)
8039              (inhibit-point-motion-hooks t)
8040              hidden e)
8041         (setq hidden
8042               (if (numberp arg)
8043                   (>= arg 0)
8044                 (save-restriction
8045                   (article-narrow-to-head)
8046                   (gnus-article-hidden-text-p 'headers))))
8047         (goto-char (point-min))
8048         (when (search-forward "\n\n" nil t)
8049           (delete-region (point-min) (1- (point))))
8050         (goto-char (point-min))
8051         (save-excursion
8052           (set-buffer gnus-original-article-buffer)
8053           (goto-char (point-min))
8054           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8055         (insert-buffer-substring gnus-original-article-buffer 1 e)
8056         (save-restriction
8057           (narrow-to-region (point-min) (point))
8058           (article-decode-encoded-words)
8059           (if  hidden
8060               (let ((gnus-treat-hide-headers nil)
8061                     (gnus-treat-hide-boring-headers nil))
8062                 (setq gnus-article-wash-types
8063                       (delq 'headers gnus-article-wash-types))
8064                 (gnus-treat-article 'head))
8065             (gnus-treat-article 'head)))
8066         (gnus-set-mode-line 'article)))))
8067
8068 (defun gnus-summary-show-all-headers ()
8069   "Make all header lines visible."
8070   (interactive)
8071   (gnus-summary-toggle-header 1))
8072
8073 (defun gnus-summary-toggle-mime (&optional arg)
8074   "Toggle MIME processing.
8075 If ARG is a positive number, turn MIME processing on."
8076   (interactive "P")
8077   (setq gnus-show-mime
8078         (if (null arg)
8079             (not gnus-show-mime)
8080           (> (prefix-numeric-value arg) 0)))
8081   (gnus-summary-select-article t 'force))
8082
8083 (defun gnus-summary-caesar-message (&optional arg)
8084   "Caesar rotate the current article by 13.
8085 The numerical prefix specifies how many places to rotate each letter
8086 forward."
8087   (interactive "P")
8088   (gnus-summary-select-article)
8089   (let ((mail-header-separator ""))
8090     (gnus-eval-in-buffer-window gnus-article-buffer
8091       (save-restriction
8092         (widen)
8093         (let ((start (window-start))
8094               buffer-read-only)
8095           (message-caesar-buffer-body arg)
8096           (set-window-start (get-buffer-window (current-buffer)) start))))))
8097
8098 (defun gnus-summary-stop-page-breaking ()
8099   "Stop page breaking in the current article."
8100   (interactive)
8101   (gnus-summary-select-article)
8102   (gnus-eval-in-buffer-window gnus-article-buffer
8103     (widen)
8104     (when (gnus-visual-p 'page-marker)
8105       (let ((buffer-read-only nil))
8106         (gnus-remove-text-with-property 'gnus-prev)
8107         (gnus-remove-text-with-property 'gnus-next))
8108       (setq gnus-page-broken nil))))
8109
8110 (defun gnus-summary-move-article (&optional n to-newsgroup
8111                                             select-method action)
8112   "Move the current article to a different newsgroup.
8113 If N is a positive number, move the N next articles.
8114 If N is a negative number, move the N previous articles.
8115 If N is nil and any articles have been marked with the process mark,
8116 move those articles instead.
8117 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8118 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8119 re-spool using this method.
8120
8121 For this function to work, both the current newsgroup and the
8122 newsgroup that you want to move to have to support the `request-move'
8123 and `request-accept' functions.
8124
8125 ACTION can be either `move' (the default), `crosspost' or `copy'."
8126   (interactive "P")
8127   (unless action
8128     (setq action 'move))
8129   ;; Check whether the source group supports the required functions.
8130   (cond ((and (eq action 'move)
8131               (not (gnus-check-backend-function
8132                     'request-move-article gnus-newsgroup-name)))
8133          (error "The current group does not support article moving"))
8134         ((and (eq action 'crosspost)
8135               (not (gnus-check-backend-function
8136                     'request-replace-article gnus-newsgroup-name)))
8137          (error "The current group does not support article editing")))
8138   (let ((articles (gnus-summary-work-articles n))
8139         (prefix (if (gnus-check-backend-function
8140                      'request-move-article gnus-newsgroup-name)
8141                     (gnus-group-real-prefix gnus-newsgroup-name)
8142                   ""))
8143         (names '((move "Move" "Moving")
8144                  (copy "Copy" "Copying")
8145                  (crosspost "Crosspost" "Crossposting")))
8146         (copy-buf (save-excursion
8147                     (nnheader-set-temp-buffer " *copy article*")))
8148         (default-marks gnus-article-mark-lists)
8149         (no-expire-marks (delete '(expirable . expire)
8150                                  (copy-sequence gnus-article-mark-lists)))
8151         art-group to-method new-xref article to-groups)
8152     (unless (assq action names)
8153       (error "Unknown action %s" action))
8154     ;; Read the newsgroup name.
8155     (when (and (not to-newsgroup)
8156                (not select-method))
8157       (setq to-newsgroup
8158             (gnus-read-move-group-name
8159              (cadr (assq action names))
8160              (symbol-value (intern (format "gnus-current-%s-group" action)))
8161              articles prefix))
8162       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8163     (setq to-method (or select-method
8164                         (gnus-server-to-method
8165                          (gnus-group-method to-newsgroup))))
8166     ;; Check the method we are to move this article to...
8167     (unless (gnus-check-backend-function
8168              'request-accept-article (car to-method))
8169       (error "%s does not support article copying" (car to-method)))
8170     (unless (gnus-check-server to-method)
8171       (error "Can't open server %s" (car to-method)))
8172     (gnus-message 6 "%s to %s: %s..."
8173                   (caddr (assq action names))
8174                   (or (car select-method) to-newsgroup) articles)
8175     (while articles
8176       (setq article (pop articles))
8177       (setq
8178        art-group
8179        (cond
8180         ;; Move the article.
8181         ((eq action 'move)
8182          ;; Remove this article from future suppression.
8183          (gnus-dup-unsuppress-article article)
8184          (gnus-request-move-article
8185           article                       ; Article to move
8186           gnus-newsgroup-name           ; From newsgroup
8187           (nth 1 (gnus-find-method-for-group
8188                   gnus-newsgroup-name)) ; Server
8189           (list 'gnus-request-accept-article
8190                 to-newsgroup (list 'quote select-method)
8191                 (not articles) t)       ; Accept form
8192           (not articles)))              ; Only save nov last time
8193         ;; Copy the article.
8194         ((eq action 'copy)
8195          (save-excursion
8196            (set-buffer copy-buf)
8197            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8198              (gnus-request-accept-article
8199               to-newsgroup select-method (not articles) t))))
8200         ;; Crosspost the article.
8201         ((eq action 'crosspost)
8202          (let ((xref (message-tokenize-header
8203                       (mail-header-xref (gnus-summary-article-header article))
8204                       " ")))
8205            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8206                                   ":" article))
8207            (unless xref
8208              (setq xref (list (system-name))))
8209            (setq new-xref
8210                  (concat
8211                   (mapconcat 'identity
8212                              (delete "Xref:" (delete new-xref xref))
8213                              " ")
8214                   " " new-xref))
8215            (save-excursion
8216              (set-buffer copy-buf)
8217              ;; First put the article in the destination group.
8218              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8219              (when (consp (setq art-group
8220                                 (gnus-request-accept-article
8221                                  to-newsgroup select-method (not articles))))
8222                (setq new-xref (concat new-xref " " (car art-group)
8223                                       ":" (cdr art-group)))
8224                ;; Now we have the new Xrefs header, so we insert
8225                ;; it and replace the new article.
8226                (nnheader-replace-header "Xref" new-xref)
8227                (gnus-request-replace-article
8228                 (cdr art-group) to-newsgroup (current-buffer))
8229                art-group))))))
8230       (cond
8231        ((not art-group)
8232         (gnus-message 1 "Couldn't %s article %s: %s"
8233                       (cadr (assq action names)) article
8234                       (nnheader-get-report (car to-method))))
8235        ((eq art-group 'junk)
8236         (when (eq action 'move)
8237           (gnus-summary-mark-article article gnus-canceled-mark)
8238           (gnus-message 4 "Deleted article %s" article)))
8239        (t
8240         (let* ((pto-group (gnus-group-prefixed-name
8241                            (car art-group) to-method))
8242                (entry
8243                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8244                (info (nth 2 entry))
8245                (to-group (gnus-info-group info))
8246                to-marks)
8247           ;; Update the group that has been moved to.
8248           (when (and info
8249                      (memq action '(move copy)))
8250             (unless (member to-group to-groups)
8251               (push to-group to-groups))
8252
8253             (unless (memq article gnus-newsgroup-unreads)
8254               (push 'read to-marks)
8255               (gnus-info-set-read
8256                info (gnus-add-to-range (gnus-info-read info)
8257                                        (list (cdr art-group)))))
8258
8259             ;; See whether the article is to be put in the cache.
8260             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8261                              default-marks
8262                            no-expire-marks))
8263                   (to-article (cdr art-group)))
8264
8265               ;; Enter the article into the cache in the new group,
8266               ;; if that is required.
8267               (when gnus-use-cache
8268                 (gnus-cache-possibly-enter-article
8269                  to-group to-article
8270                  (let ((header (copy-sequence
8271                                 (gnus-summary-article-header article))))
8272                    (mail-header-set-number header to-article)
8273                    header)
8274                  (memq article gnus-newsgroup-marked)
8275                  (memq article gnus-newsgroup-dormant)
8276                  (memq article gnus-newsgroup-unreads)))
8277
8278               (when gnus-preserve-marks
8279                 ;; Copy any marks over to the new group.
8280                 (when (and (equal to-group gnus-newsgroup-name)
8281                            (not (memq article gnus-newsgroup-unreads)))
8282                   ;; Mark this article as read in this group.
8283                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8284                   (setcdr (gnus-active to-group) to-article)
8285                   (setcdr gnus-newsgroup-active to-article))
8286
8287                 (while marks
8288                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8289                     (when (memq article (symbol-value
8290                                          (intern (format "gnus-newsgroup-%s"
8291                                                          (caar marks)))))
8292                       (push (cdar marks) to-marks)
8293                       ;; If the other group is the same as this group,
8294                       ;; then we have to add the mark to the list.
8295                       (when (equal to-group gnus-newsgroup-name)
8296                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8297                              (cons to-article
8298                                    (symbol-value
8299                                     (intern (format "gnus-newsgroup-%s"
8300                                                     (caar marks)))))))
8301                       ;; Copy the marks to other group.
8302                       (gnus-add-marked-articles
8303                        to-group (cdar marks) (list to-article) info)))
8304                   (setq marks (cdr marks)))
8305
8306                 (gnus-request-set-mark to-group (list (list (list to-article)
8307                                                             'add
8308                                                             to-marks))))
8309
8310               (gnus-dribble-enter
8311                (concat "(gnus-group-set-info '"
8312                        (gnus-prin1-to-string (gnus-get-info to-group))
8313                        ")"))))
8314
8315           ;; Update the Xref header in this article to point to
8316           ;; the new crossposted article we have just created.
8317           (when (eq action 'crosspost)
8318             (save-excursion
8319               (set-buffer copy-buf)
8320               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8321               (nnheader-replace-header "Xref" new-xref)
8322               (gnus-request-replace-article
8323                article gnus-newsgroup-name (current-buffer)))))
8324
8325         ;;;!!!Why is this necessary?
8326         (set-buffer gnus-summary-buffer)
8327
8328         (gnus-summary-goto-subject article)
8329         (when (eq action 'move)
8330           (gnus-summary-mark-article article gnus-canceled-mark))))
8331       (gnus-summary-remove-process-mark article))
8332     ;; Re-activate all groups that have been moved to.
8333     (while to-groups
8334       (save-excursion
8335         (set-buffer gnus-group-buffer)
8336         (when (gnus-group-goto-group (car to-groups) t)
8337           (gnus-group-get-new-news-this-group 1 t))
8338         (pop to-groups)))
8339
8340     (gnus-kill-buffer copy-buf)
8341     (gnus-summary-position-point)
8342     (gnus-set-mode-line 'summary)))
8343
8344 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8345   "Move the current article to a different newsgroup.
8346 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8347 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8348 re-spool using this method."
8349   (interactive "P")
8350   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8351
8352 (defun gnus-summary-crosspost-article (&optional n)
8353   "Crosspost the current article to some other group."
8354   (interactive "P")
8355   (gnus-summary-move-article n nil nil 'crosspost))
8356
8357 (defcustom gnus-summary-respool-default-method nil
8358   "Default method for respooling an article.
8359 If nil, use to the current newsgroup method."
8360   :type '(choice (gnus-select-method :value (nnml ""))
8361                  (const nil))
8362   :group 'gnus-summary-mail)
8363
8364 (defun gnus-summary-respool-article (&optional n method)
8365   "Respool the current article.
8366 The article will be squeezed through the mail spooling process again,
8367 which means that it will be put in some mail newsgroup or other
8368 depending on `nnmail-split-methods'.
8369 If N is a positive number, respool the N next articles.
8370 If N is a negative number, respool the N previous articles.
8371 If N is nil and any articles have been marked with the process mark,
8372 respool those articles instead.
8373
8374 Respooling can be done both from mail groups and \"real\" newsgroups.
8375 In the former case, the articles in question will be moved from the
8376 current group into whatever groups they are destined to.  In the
8377 latter case, they will be copied into the relevant groups."
8378   (interactive
8379    (list current-prefix-arg
8380          (let* ((methods (gnus-methods-using 'respool))
8381                 (methname
8382                  (symbol-name (or gnus-summary-respool-default-method
8383                                   (car (gnus-find-method-for-group
8384                                         gnus-newsgroup-name)))))
8385                 (method
8386                  (gnus-completing-read
8387                   methname "What backend do you want to use when respooling?"
8388                   methods nil t nil 'gnus-mail-method-history))
8389                 ms)
8390            (cond
8391             ((zerop (length (setq ms (gnus-servers-using-backend
8392                                       (intern method)))))
8393              (list (intern method) ""))
8394             ((= 1 (length ms))
8395              (car ms))
8396             (t
8397              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8398                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8399                            ms-alist))))))))
8400   (unless method
8401     (error "No method given for respooling"))
8402   (if (assoc (symbol-name
8403               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8404              (gnus-methods-using 'respool))
8405       (gnus-summary-move-article n nil method)
8406     (gnus-summary-copy-article n nil method)))
8407
8408 (defun gnus-summary-import-article (file &optional edit)
8409   "Import an arbitrary file into a mail newsgroup."
8410   (interactive "fImport file: \nP")
8411   (let ((group gnus-newsgroup-name)
8412         (now (current-time))
8413         atts lines group-art)
8414     (unless (gnus-check-backend-function 'request-accept-article group)
8415       (error "%s does not support article importing" group))
8416     (or (file-readable-p file)
8417         (not (file-regular-p file))
8418         (error "Can't read %s" file))
8419     (save-excursion
8420       (set-buffer (gnus-get-buffer-create " *import file*"))
8421       (erase-buffer)
8422       (nnheader-insert-file-contents file)
8423       (goto-char (point-min))
8424       (if (nnheader-article-p)
8425           (save-restriction
8426             (goto-char (point-min))
8427             (search-forward "\n\n" nil t)
8428             (narrow-to-region (point-min) (1- (point)))
8429             (goto-char (point-min))
8430             (unless (re-search-forward "^date:" nil t)
8431               (goto-char (point-max))
8432               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8433         ;; This doesn't look like an article, so we fudge some headers.
8434         (setq atts (file-attributes file)
8435               lines (count-lines (point-min) (point-max)))
8436         (insert "From: " (read-string "From: ") "\n"
8437                 "Subject: " (read-string "Subject: ") "\n"
8438                 "Date: " (message-make-date (nth 5 atts)) "\n"
8439                 "Message-ID: " (message-make-message-id) "\n"
8440                 "Lines: " (int-to-string lines) "\n"
8441                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8442       (setq group-art (gnus-request-accept-article group nil t))
8443       (kill-buffer (current-buffer)))
8444     (setq gnus-newsgroup-active (gnus-activate-group group))
8445     (forward-line 1)
8446     (gnus-summary-goto-article (cdr group-art) nil t)
8447     (when edit
8448       (gnus-summary-edit-article))))
8449
8450 (defun gnus-summary-create-article ()
8451   "Create an article in a mail newsgroup."
8452   (interactive)
8453   (let ((group gnus-newsgroup-name)
8454         (now (current-time))
8455         group-art)
8456     (unless (gnus-check-backend-function 'request-accept-article group)
8457       (error "%s does not support article importing" group))
8458     (save-excursion
8459       (set-buffer (gnus-get-buffer-create " *import file*"))
8460       (erase-buffer)
8461       (goto-char (point-min))
8462       ;; This doesn't look like an article, so we fudge some headers.
8463       (insert "From: " (read-string "From: ") "\n"
8464               "Subject: " (read-string "Subject: ") "\n"
8465               "Date: " (message-make-date now) "\n"
8466               "Message-ID: " (message-make-message-id) "\n")
8467       (setq group-art (gnus-request-accept-article group nil t))
8468       (kill-buffer (current-buffer)))
8469     (setq gnus-newsgroup-active (gnus-activate-group group))
8470     (forward-line 1)
8471     (gnus-summary-goto-article (cdr group-art) nil t)
8472     (gnus-summary-edit-article)))
8473
8474 (defun gnus-summary-article-posted-p ()
8475   "Say whether the current (mail) article is available from news as well.
8476 This will be the case if the article has both been mailed and posted."
8477   (interactive)
8478   (let ((id (mail-header-references (gnus-summary-article-header)))
8479         (gnus-override-method (car (gnus-refer-article-methods))))
8480     (if (gnus-request-head id "")
8481         (gnus-message 2 "The current message was found on %s"
8482                       gnus-override-method)
8483       (gnus-message 2 "The current message couldn't be found on %s"
8484                     gnus-override-method)
8485       nil)))
8486
8487 (defun gnus-summary-expire-articles (&optional now)
8488   "Expire all articles that are marked as expirable in the current group."
8489   (interactive)
8490   (when (gnus-check-backend-function
8491          'request-expire-articles gnus-newsgroup-name)
8492     ;; This backend supports expiry.
8493     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8494            (expirable (if total
8495                           (progn
8496                             ;; We need to update the info for
8497                             ;; this group for `gnus-list-of-read-articles'
8498                             ;; to give us the right answer.
8499                             (gnus-run-hooks 'gnus-exit-group-hook)
8500                             (gnus-summary-update-info)
8501                             (gnus-list-of-read-articles gnus-newsgroup-name))
8502                         (setq gnus-newsgroup-expirable
8503                               (sort gnus-newsgroup-expirable '<))))
8504            (expiry-wait (if now 'immediate
8505                           (gnus-group-find-parameter
8506                            gnus-newsgroup-name 'expiry-wait)))
8507            (nnmail-expiry-target
8508             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8509                 nnmail-expiry-target))
8510            es)
8511       (when expirable
8512         ;; There are expirable articles in this group, so we run them
8513         ;; through the expiry process.
8514         (gnus-message 6 "Expiring articles...")
8515         (unless (gnus-check-group gnus-newsgroup-name)
8516           (error "Can't open server for %s" gnus-newsgroup-name))
8517         ;; The list of articles that weren't expired is returned.
8518         (save-excursion
8519           (if expiry-wait
8520               (let ((nnmail-expiry-wait-function nil)
8521                     (nnmail-expiry-wait expiry-wait))
8522                 (setq es (gnus-request-expire-articles
8523                           expirable gnus-newsgroup-name)))
8524             (setq es (gnus-request-expire-articles
8525                       expirable gnus-newsgroup-name)))
8526           (unless total
8527             (setq gnus-newsgroup-expirable es))
8528           ;; We go through the old list of expirable, and mark all
8529           ;; really expired articles as nonexistent.
8530           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8531             (let ((gnus-use-cache nil))
8532               (while expirable
8533                 (unless (memq (car expirable) es)
8534                   (when (gnus-data-find (car expirable))
8535                     (gnus-summary-mark-article
8536                      (car expirable) gnus-canceled-mark)))
8537                 (setq expirable (cdr expirable))))))
8538         (gnus-message 6 "Expiring articles...done")))))
8539
8540 (defun gnus-summary-expire-articles-now ()
8541   "Expunge all expirable articles in the current group.
8542 This means that *all* articles that are marked as expirable will be
8543 deleted forever, right now."
8544   (interactive)
8545   (or gnus-expert-user
8546       (gnus-yes-or-no-p
8547        "Are you really, really, really sure you want to delete all these messages? ")
8548       (error "Phew!"))
8549   (gnus-summary-expire-articles t))
8550
8551 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8552 (defun gnus-summary-delete-article (&optional n)
8553   "Delete the N next (mail) articles.
8554 This command actually deletes articles.  This is not a marking
8555 command.  The article will disappear forever from your life, never to
8556 return.
8557 If N is negative, delete backwards.
8558 If N is nil and articles have been marked with the process mark,
8559 delete these instead."
8560   (interactive "P")
8561   (unless (gnus-check-backend-function 'request-expire-articles
8562                                        gnus-newsgroup-name)
8563     (error "The current newsgroup does not support article deletion"))
8564   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8565     (error "Couldn't open server"))
8566   ;; Compute the list of articles to delete.
8567   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8568         not-deleted)
8569     (if (and gnus-novice-user
8570              (not (gnus-yes-or-no-p
8571                    (format "Do you really want to delete %s forever? "
8572                            (if (> (length articles) 1)
8573                                (format "these %s articles" (length articles))
8574                              "this article")))))
8575         ()
8576       ;; Delete the articles.
8577       (setq not-deleted (gnus-request-expire-articles
8578                          articles gnus-newsgroup-name 'force))
8579       (while articles
8580         (gnus-summary-remove-process-mark (car articles))
8581         ;; The backend might not have been able to delete the article
8582         ;; after all.
8583         (unless (memq (car articles) not-deleted)
8584           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8585         (setq articles (cdr articles)))
8586       (when not-deleted
8587         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8588     (gnus-summary-position-point)
8589     (gnus-set-mode-line 'summary)
8590     not-deleted))
8591
8592 (defun gnus-summary-edit-article (&optional force)
8593   "Edit the current article.
8594 This will have permanent effect only in mail groups.
8595 If FORCE is non-nil, allow editing of articles even in read-only
8596 groups."
8597   (interactive "P")
8598   (save-excursion
8599     (set-buffer gnus-summary-buffer)
8600     (let ((mail-parse-charset gnus-newsgroup-charset)
8601           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8602       (gnus-set-global-variables)
8603       (when (and (not force)
8604                  (gnus-group-read-only-p))
8605         (error "The current newsgroup does not support article editing"))
8606       (gnus-summary-show-article t)
8607       (gnus-article-edit-article
8608        'ignore
8609        `(lambda (no-highlight)
8610           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8611                 (message-options message-options)
8612                 (message-options-set-recipient)
8613                 (mail-parse-ignored-charsets
8614                  ',gnus-newsgroup-ignored-charsets))
8615             (gnus-summary-edit-article-done
8616              ,(or (mail-header-references gnus-current-headers) "")
8617              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8618
8619 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8620
8621 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8622                                                  no-highlight)
8623   "Make edits to the current article permanent."
8624   (interactive)
8625   (save-excursion
8626     ;; The buffer restriction contains the entire article if it exists.
8627     (when (article-goto-body)
8628       (let ((lines (count-lines (point) (point-max)))
8629             (length (- (point-max) (point)))
8630             (case-fold-search t)
8631             (body (copy-marker (point))))
8632         (goto-char (point-min))
8633         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8634           (delete-region (match-beginning 1) (match-end 1))
8635           (insert (number-to-string length)))
8636         (goto-char (point-min))
8637         (when (re-search-forward
8638                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8639           (delete-region (match-beginning 1) (match-end 1))
8640           (insert (number-to-string length)))
8641         (goto-char (point-min))
8642         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8643           (delete-region (match-beginning 1) (match-end 1))
8644           (insert (number-to-string lines))))))
8645   ;; Replace the article.
8646   (let ((buf (current-buffer)))
8647     (with-temp-buffer
8648       (insert-buffer-substring buf)
8649
8650       (if (and (not read-only)
8651                (not (gnus-request-replace-article
8652                      (cdr gnus-article-current) (car gnus-article-current)
8653                      (current-buffer) t)))
8654           (error "Couldn't replace article")
8655         ;; Update the summary buffer.
8656         (if (and references
8657                  (equal (message-tokenize-header references " ")
8658                         (message-tokenize-header
8659                          (or (message-fetch-field "references") "") " ")))
8660             ;; We only have to update this line.
8661             (save-excursion
8662               (save-restriction
8663                 (message-narrow-to-head)
8664                 (let ((head (buffer-string))
8665                       header)
8666                   (with-temp-buffer
8667                     (insert (format "211 %d Article retrieved.\n"
8668                                     (cdr gnus-article-current)))
8669                     (insert head)
8670                     (insert ".\n")
8671                     (let ((nntp-server-buffer (current-buffer)))
8672                       (setq header (car (gnus-get-newsgroup-headers
8673                                          (save-excursion
8674                                            (set-buffer gnus-summary-buffer)
8675                                            gnus-newsgroup-dependencies)
8676                                          t))))
8677                     (save-excursion
8678                       (set-buffer gnus-summary-buffer)
8679                       (gnus-data-set-header
8680                        (gnus-data-find (cdr gnus-article-current))
8681                        header)
8682                       (gnus-summary-update-article-line
8683                        (cdr gnus-article-current) header)
8684                       (if (gnus-summary-goto-subject
8685                            (cdr gnus-article-current) nil t)
8686                           (gnus-summary-update-secondary-mark
8687                            (cdr gnus-article-current))))))))
8688           ;; Update threads.
8689           (set-buffer (or buffer gnus-summary-buffer))
8690           (gnus-summary-update-article (cdr gnus-article-current))
8691           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8692               (gnus-summary-update-secondary-mark
8693                (cdr gnus-article-current))))
8694         ;; Prettify the article buffer again.
8695         (unless no-highlight
8696           (save-excursion
8697             (set-buffer gnus-article-buffer)
8698             ;;;!!! Fix this -- article should be rehighlighted.
8699             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8700             (set-buffer gnus-original-article-buffer)
8701             (gnus-request-article
8702              (cdr gnus-article-current)
8703              (car gnus-article-current) (current-buffer))))
8704         ;; Prettify the summary buffer line.
8705         (when (gnus-visual-p 'summary-highlight 'highlight)
8706           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8707
8708 (defun gnus-summary-edit-wash (key)
8709   "Perform editing command KEY in the article buffer."
8710   (interactive
8711    (list
8712     (progn
8713       (message "%s" (concat (this-command-keys) "- "))
8714       (read-char))))
8715   (message "")
8716   (gnus-summary-edit-article)
8717   (execute-kbd-macro (concat (this-command-keys) key))
8718   (gnus-article-edit-done))
8719
8720 (defun gnus-summary-toggle-smiley (&optional arg)
8721   "Toggle the display of smilies as small graphical icons."
8722   (interactive "P")
8723   (save-excursion
8724     (set-buffer gnus-article-buffer)
8725     (gnus-smiley-display arg)))
8726
8727 ;;; Respooling
8728
8729 (defun gnus-summary-respool-query (&optional silent trace)
8730   "Query where the respool algorithm would put this article."
8731   (interactive)
8732   (let (gnus-mark-article-hook)
8733     (gnus-summary-select-article)
8734     (save-excursion
8735       (set-buffer gnus-original-article-buffer)
8736       (save-restriction
8737         (message-narrow-to-head)
8738         (let ((groups (nnmail-article-group 'identity trace)))
8739           (unless silent
8740             (if groups
8741                 (message "This message would go to %s"
8742                          (mapconcat 'car groups ", "))
8743               (message "This message would go to no groups"))
8744             groups))))))
8745
8746 (defun gnus-summary-respool-trace ()
8747   "Trace where the respool algorithm would put this article.
8748 Display a buffer showing all fancy splitting patterns which matched."
8749   (interactive)
8750   (gnus-summary-respool-query nil t))
8751
8752 ;; Summary marking commands.
8753
8754 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8755   "Mark articles which has the same subject as read, and then select the next.
8756 If UNMARK is positive, remove any kind of mark.
8757 If UNMARK is negative, tick articles."
8758   (interactive "P")
8759   (when unmark
8760     (setq unmark (prefix-numeric-value unmark)))
8761   (let ((count
8762          (gnus-summary-mark-same-subject
8763           (gnus-summary-article-subject) unmark)))
8764     ;; Select next unread article.  If auto-select-same mode, should
8765     ;; select the first unread article.
8766     (gnus-summary-next-article t (and gnus-auto-select-same
8767                                       (gnus-summary-article-subject)))
8768     (gnus-message 7 "%d article%s marked as %s"
8769                   count (if (= count 1) " is" "s are")
8770                   (if unmark "unread" "read"))))
8771
8772 (defun gnus-summary-kill-same-subject (&optional unmark)
8773   "Mark articles which has the same subject as read.
8774 If UNMARK is positive, remove any kind of mark.
8775 If UNMARK is negative, tick articles."
8776   (interactive "P")
8777   (when unmark
8778     (setq unmark (prefix-numeric-value unmark)))
8779   (let ((count
8780          (gnus-summary-mark-same-subject
8781           (gnus-summary-article-subject) unmark)))
8782     ;; If marked as read, go to next unread subject.
8783     (when (null unmark)
8784       ;; Go to next unread subject.
8785       (gnus-summary-next-subject 1 t))
8786     (gnus-message 7 "%d articles are marked as %s"
8787                   count (if unmark "unread" "read"))))
8788
8789 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8790   "Mark articles with same SUBJECT as read, and return marked number.
8791 If optional argument UNMARK is positive, remove any kinds of marks.
8792 If optional argument UNMARK is negative, mark articles as unread instead."
8793   (let ((count 1))
8794     (save-excursion
8795       (cond
8796        ((null unmark)                   ; Mark as read.
8797         (while (and
8798                 (progn
8799                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8800                   (gnus-summary-show-thread) t)
8801                 (gnus-summary-find-subject subject))
8802           (setq count (1+ count))))
8803        ((> unmark 0)                    ; Tick.
8804         (while (and
8805                 (progn
8806                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8807                   (gnus-summary-show-thread) t)
8808                 (gnus-summary-find-subject subject))
8809           (setq count (1+ count))))
8810        (t                               ; Mark as unread.
8811         (while (and
8812                 (progn
8813                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8814                   (gnus-summary-show-thread) t)
8815                 (gnus-summary-find-subject subject))
8816           (setq count (1+ count)))))
8817       (gnus-set-mode-line 'summary)
8818       ;; Return the number of marked articles.
8819       count)))
8820
8821 (defun gnus-summary-mark-as-processable (n &optional unmark)
8822   "Set the process mark on the next N articles.
8823 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8824 the process mark instead.  The difference between N and the actual
8825 number of articles marked is returned."
8826   (interactive "P")
8827   (if (and (null n) (gnus-region-active-p))
8828       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8829     (setq n (prefix-numeric-value n))
8830     (let ((backward (< n 0))
8831           (n (abs n)))
8832       (while (and
8833               (> n 0)
8834               (if unmark
8835                   (gnus-summary-remove-process-mark
8836                    (gnus-summary-article-number))
8837                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8838               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8839         (setq n (1- n)))
8840       (when (/= 0 n)
8841         (gnus-message 7 "No more articles"))
8842       (gnus-summary-recenter)
8843       (gnus-summary-position-point)
8844       n)))
8845
8846 (defun gnus-summary-unmark-as-processable (n)
8847   "Remove the process mark from the next N articles.
8848 If N is negative, unmark backward instead.  The difference between N and
8849 the actual number of articles unmarked is returned."
8850   (interactive "P")
8851   (gnus-summary-mark-as-processable n t))
8852
8853 (defun gnus-summary-unmark-all-processable ()
8854   "Remove the process mark from all articles."
8855   (interactive)
8856   (save-excursion
8857     (while gnus-newsgroup-processable
8858       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8859   (gnus-summary-position-point))
8860
8861 (defun gnus-summary-add-mark (article type)
8862   "Mark ARTICLE with a mark of TYPE."
8863   (let ((vtype (car (assq type gnus-article-mark-lists)))
8864         var)
8865     (if (not vtype)
8866         (error "No such mark type: %s" type)
8867       (setq var (intern (format "gnus-newsgroup-%s" type)))
8868       (set var (cons article (symbol-value var)))
8869       (if (memq type '(processable cached replied forwarded recent saved))
8870           (gnus-summary-update-secondary-mark article)
8871         ;;; !!! This is bogus.  We should find out what primary
8872         ;;; !!! mark we want to set.
8873         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8874
8875 (defun gnus-summary-mark-as-expirable (n)
8876   "Mark N articles forward as expirable.
8877 If N is negative, mark backward instead.  The difference between N and
8878 the actual number of articles marked is returned."
8879   (interactive "p")
8880   (gnus-summary-mark-forward n gnus-expirable-mark))
8881
8882 (defun gnus-summary-mark-article-as-replied (article)
8883   "Mark ARTICLE as replied to and update the summary line.
8884 ARTICLE can also be a list of articles."
8885   (interactive (list (gnus-summary-article-number)))
8886   (let ((articles (if (listp article) article (list article))))
8887     (dolist (article articles)
8888       (push article gnus-newsgroup-replied)
8889       (let ((buffer-read-only nil))
8890         (when (gnus-summary-goto-subject article nil t)
8891           (gnus-summary-update-secondary-mark article))))))
8892
8893 (defun gnus-summary-mark-article-as-forwarded (article)
8894   "Mark ARTICLE as forwarded and update the summary line.
8895 ARTICLE can also be a list of articles."
8896   (let ((articles (if (listp article) article (list article))))
8897     (dolist (article articles)
8898       (push article gnus-newsgroup-forwarded)
8899       (let ((buffer-read-only nil))
8900         (when (gnus-summary-goto-subject article nil t)
8901           (gnus-summary-update-secondary-mark article))))))
8902
8903 (defun gnus-summary-set-bookmark (article)
8904   "Set a bookmark in current article."
8905   (interactive (list (gnus-summary-article-number)))
8906   (when (or (not (get-buffer gnus-article-buffer))
8907             (not gnus-current-article)
8908             (not gnus-article-current)
8909             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8910     (error "No current article selected"))
8911   ;; Remove old bookmark, if one exists.
8912   (let ((old (assq article gnus-newsgroup-bookmarks)))
8913     (when old
8914       (setq gnus-newsgroup-bookmarks
8915             (delq old gnus-newsgroup-bookmarks))))
8916   ;; Set the new bookmark, which is on the form
8917   ;; (article-number . line-number-in-body).
8918   (push
8919    (cons article
8920          (save-excursion
8921            (set-buffer gnus-article-buffer)
8922            (count-lines
8923             (min (point)
8924                  (save-excursion
8925                    (goto-char (point-min))
8926                    (search-forward "\n\n" nil t)
8927                    (point)))
8928             (point))))
8929    gnus-newsgroup-bookmarks)
8930   (gnus-message 6 "A bookmark has been added to the current article."))
8931
8932 (defun gnus-summary-remove-bookmark (article)
8933   "Remove the bookmark from the current article."
8934   (interactive (list (gnus-summary-article-number)))
8935   ;; Remove old bookmark, if one exists.
8936   (let ((old (assq article gnus-newsgroup-bookmarks)))
8937     (if old
8938         (progn
8939           (setq gnus-newsgroup-bookmarks
8940                 (delq old gnus-newsgroup-bookmarks))
8941           (gnus-message 6 "Removed bookmark."))
8942       (gnus-message 6 "No bookmark in current article."))))
8943
8944 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8945 (defun gnus-summary-mark-as-dormant (n)
8946   "Mark N articles forward as dormant.
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-dormant-mark))
8951
8952 (defun gnus-summary-set-process-mark (article)
8953   "Set the process mark on ARTICLE and update the summary line."
8954   (setq gnus-newsgroup-processable
8955         (cons article
8956               (delq article gnus-newsgroup-processable)))
8957   (when (gnus-summary-goto-subject article)
8958     (gnus-summary-show-thread)
8959     (gnus-summary-goto-subject article)
8960     (gnus-summary-update-secondary-mark article)))
8961
8962 (defun gnus-summary-remove-process-mark (article)
8963   "Remove the process mark from ARTICLE and update the summary line."
8964   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8965   (when (gnus-summary-goto-subject article)
8966     (gnus-summary-show-thread)
8967     (gnus-summary-goto-subject article)
8968     (gnus-summary-update-secondary-mark article)))
8969
8970 (defun gnus-summary-set-saved-mark (article)
8971   "Set the process mark on ARTICLE and update the summary line."
8972   (push article gnus-newsgroup-saved)
8973   (when (gnus-summary-goto-subject article)
8974     (gnus-summary-update-secondary-mark article)))
8975
8976 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8977   "Mark N articles as read forwards.
8978 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8979 The difference between N and the actual number of articles marked is
8980 returned.
8981 Iff NO-EXPIRE, auto-expiry will be inhibited."
8982   (interactive "p")
8983   (gnus-summary-show-thread)
8984   (let ((backward (< n 0))
8985         (gnus-summary-goto-unread
8986          (and gnus-summary-goto-unread
8987               (not (eq gnus-summary-goto-unread 'never))
8988               (not (memq mark (list gnus-unread-mark
8989                                     gnus-ticked-mark gnus-dormant-mark)))))
8990         (n (abs n))
8991         (mark (or mark gnus-del-mark)))
8992     (while (and (> n 0)
8993                 (gnus-summary-mark-article nil mark no-expire)
8994                 (zerop (gnus-summary-next-subject
8995                         (if backward -1 1)
8996                         (and gnus-summary-goto-unread
8997                              (not (eq gnus-summary-goto-unread 'never)))
8998                         t)))
8999       (setq n (1- n)))
9000     (when (/= 0 n)
9001       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9002     (gnus-summary-recenter)
9003     (gnus-summary-position-point)
9004     (gnus-set-mode-line 'summary)
9005     n))
9006
9007 (defun gnus-summary-mark-article-as-read (mark)
9008   "Mark the current article quickly as read with MARK."
9009   (let ((article (gnus-summary-article-number)))
9010     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9011     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9012     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9013     (push (cons article mark) gnus-newsgroup-reads)
9014     ;; Possibly remove from cache, if that is used.
9015     (when gnus-use-cache
9016       (gnus-cache-enter-remove-article article))
9017     ;; Allow the backend to change the mark.
9018     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9019     ;; Check for auto-expiry.
9020     (when (and gnus-newsgroup-auto-expire
9021                (memq mark gnus-auto-expirable-marks))
9022       (setq mark gnus-expirable-mark)
9023       ;; Let the backend know about the mark change.
9024       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9025       (push article gnus-newsgroup-expirable))
9026     ;; Set the mark in the buffer.
9027     (gnus-summary-update-mark mark 'unread)
9028     t))
9029
9030 (defun gnus-summary-mark-article-as-unread (mark)
9031   "Mark the current article quickly as unread with MARK."
9032   (let* ((article (gnus-summary-article-number))
9033          (old-mark (gnus-summary-article-mark article)))
9034     ;; Allow the backend to change the mark.
9035     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9036     (if (eq mark old-mark)
9037         t
9038       (if (<= article 0)
9039           (progn
9040             (gnus-error 1 "Can't mark negative article numbers")
9041             nil)
9042         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9043         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9044         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9045         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9046         (cond ((= mark gnus-ticked-mark)
9047                (push article gnus-newsgroup-marked))
9048               ((= mark gnus-dormant-mark)
9049                (push article gnus-newsgroup-dormant))
9050               (t
9051                (push article gnus-newsgroup-unreads)))
9052         (gnus-pull article gnus-newsgroup-reads)
9053
9054         ;; See whether the article is to be put in the cache.
9055         (and gnus-use-cache
9056              (vectorp (gnus-summary-article-header article))
9057              (save-excursion
9058                (gnus-cache-possibly-enter-article
9059                 gnus-newsgroup-name article
9060                 (gnus-summary-article-header article)
9061                 (= mark gnus-ticked-mark)
9062                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9063
9064         ;; Fix the mark.
9065         (gnus-summary-update-mark mark 'unread)
9066         t))))
9067
9068 (defun gnus-summary-mark-article (&optional article mark no-expire)
9069   "Mark ARTICLE with MARK.  MARK can be any character.
9070 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9071 `??' (dormant) and `?E' (expirable).
9072 If MARK is nil, then the default character `?r' is used.
9073 If ARTICLE is nil, then the article on the current line will be
9074 marked.
9075 Iff NO-EXPIRE, auto-expiry will be inhibited."
9076   ;; The mark might be a string.
9077   (when (stringp mark)
9078     (setq mark (aref mark 0)))
9079   ;; If no mark is given, then we check auto-expiring.
9080   (when (null mark)
9081     (setq mark gnus-del-mark))
9082   (when (and (not no-expire)
9083              gnus-newsgroup-auto-expire
9084              (memq mark gnus-auto-expirable-marks))
9085     (setq mark gnus-expirable-mark))
9086   (let ((article (or article (gnus-summary-article-number)))
9087         (old-mark (gnus-summary-article-mark article)))
9088     ;; Allow the backend to change the mark.
9089     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9090     (if (eq mark old-mark)
9091         t
9092       (unless article
9093         (error "No article on current line"))
9094       (if (not (if (or (= mark gnus-unread-mark)
9095                        (= mark gnus-ticked-mark)
9096                        (= mark gnus-dormant-mark))
9097                    (gnus-mark-article-as-unread article mark)
9098                  (gnus-mark-article-as-read article mark)))
9099           t
9100         ;; See whether the article is to be put in the cache.
9101         (and gnus-use-cache
9102              (not (= mark gnus-canceled-mark))
9103              (vectorp (gnus-summary-article-header article))
9104              (save-excursion
9105                (gnus-cache-possibly-enter-article
9106                 gnus-newsgroup-name article
9107                 (gnus-summary-article-header article)
9108                 (= mark gnus-ticked-mark)
9109                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9110
9111         (when (gnus-summary-goto-subject article nil t)
9112           (let ((buffer-read-only nil))
9113             (gnus-summary-show-thread)
9114             ;; Fix the mark.
9115             (gnus-summary-update-mark mark 'unread)
9116             t))))))
9117
9118 (defun gnus-summary-update-secondary-mark (article)
9119   "Update the secondary (read, process, cache) mark."
9120   (gnus-summary-update-mark
9121    (cond ((memq article gnus-newsgroup-processable)
9122           gnus-process-mark)
9123          ((memq article gnus-newsgroup-cached)
9124           gnus-cached-mark)
9125          ((memq article gnus-newsgroup-replied)
9126           gnus-replied-mark)
9127          ((memq article gnus-newsgroup-forwarded)
9128           gnus-forwarded-mark)
9129          ((memq article gnus-newsgroup-saved)
9130           gnus-saved-mark)
9131          ((memq article gnus-newsgroup-recent)
9132           gnus-recent-mark)
9133          ((memq article gnus-newsgroup-unseen)
9134           gnus-unseen-mark)
9135          (t gnus-no-mark))
9136    'replied)
9137   (when (gnus-visual-p 'summary-highlight 'highlight)
9138     (gnus-run-hooks 'gnus-summary-update-hook))
9139   t)
9140
9141 (defun gnus-summary-update-mark (mark type)
9142   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9143         (buffer-read-only nil))
9144     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9145     (when forward
9146       (when (looking-at "\r")
9147         (incf forward))
9148       (when (<= (+ forward (point)) (point-max))
9149         ;; Go to the right position on the line.
9150         (goto-char (+ forward (point)))
9151         ;; Replace the old mark with the new mark.
9152         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9153         ;; Optionally update the marks by some user rule.
9154         (when (eq type 'unread)
9155           (gnus-data-set-mark
9156            (gnus-data-find (gnus-summary-article-number)) mark)
9157           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9158
9159 (defun gnus-mark-article-as-read (article &optional mark)
9160   "Enter ARTICLE in the pertinent lists and remove it from others."
9161   ;; Make the article expirable.
9162   (let ((mark (or mark gnus-del-mark)))
9163     (if (= mark gnus-expirable-mark)
9164         (push article gnus-newsgroup-expirable)
9165       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9166     ;; Remove from unread and marked lists.
9167     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9168     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9169     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9170     (push (cons article mark) gnus-newsgroup-reads)
9171     ;; Possibly remove from cache, if that is used.
9172     (when gnus-use-cache
9173       (gnus-cache-enter-remove-article article))
9174     t))
9175
9176 (defun gnus-mark-article-as-unread (article &optional mark)
9177   "Enter ARTICLE in the pertinent lists and remove it from others."
9178   (let ((mark (or mark gnus-ticked-mark)))
9179     (if (<= article 0)
9180         (progn
9181           (gnus-error 1 "Can't mark negative article numbers")
9182           nil)
9183       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9184             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9185             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9186             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9187
9188       ;; Unsuppress duplicates?
9189       (when gnus-suppress-duplicates
9190         (gnus-dup-unsuppress-article article))
9191
9192       (cond ((= mark gnus-ticked-mark)
9193              (push article gnus-newsgroup-marked))
9194             ((= mark gnus-dormant-mark)
9195              (push article gnus-newsgroup-dormant))
9196             (t
9197              (push article gnus-newsgroup-unreads)))
9198       (gnus-pull article gnus-newsgroup-reads)
9199       t)))
9200
9201 (defalias 'gnus-summary-mark-as-unread-forward
9202   'gnus-summary-tick-article-forward)
9203 (make-obsolete 'gnus-summary-mark-as-unread-forward
9204                'gnus-summary-tick-article-forward)
9205 (defun gnus-summary-tick-article-forward (n)
9206   "Tick N articles forwards.
9207 If N is negative, tick backwards instead.
9208 The difference between N and the number of articles ticked is returned."
9209   (interactive "p")
9210   (gnus-summary-mark-forward n gnus-ticked-mark))
9211
9212 (defalias 'gnus-summary-mark-as-unread-backward
9213   'gnus-summary-tick-article-backward)
9214 (make-obsolete 'gnus-summary-mark-as-unread-backward
9215                'gnus-summary-tick-article-backward)
9216 (defun gnus-summary-tick-article-backward (n)
9217   "Tick N articles backwards.
9218 The difference between N and the number of articles ticked is returned."
9219   (interactive "p")
9220   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9221
9222 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9223 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9224 (defun gnus-summary-tick-article (&optional article clear-mark)
9225   "Mark current article as unread.
9226 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9227 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9228   (interactive)
9229   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9230                                        gnus-ticked-mark)))
9231
9232 (defun gnus-summary-mark-as-read-forward (n)
9233   "Mark N articles as read forwards.
9234 If N is negative, mark backwards instead.
9235 The difference between N and the actual number of articles marked is
9236 returned."
9237   (interactive "p")
9238   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9239
9240 (defun gnus-summary-mark-as-read-backward (n)
9241   "Mark the N articles as read backwards.
9242 The difference between N and the actual number of articles marked is
9243 returned."
9244   (interactive "p")
9245   (gnus-summary-mark-forward
9246    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9247
9248 (defun gnus-summary-mark-as-read (&optional article mark)
9249   "Mark current article as read.
9250 ARTICLE specifies the article to be marked as read.
9251 MARK specifies a string to be inserted at the beginning of the line."
9252   (gnus-summary-mark-article article mark))
9253
9254 (defun gnus-summary-clear-mark-forward (n)
9255   "Clear marks from N articles forward.
9256 If N is negative, clear backward instead.
9257 The difference between N and the number of marks cleared is returned."
9258   (interactive "p")
9259   (gnus-summary-mark-forward n gnus-unread-mark))
9260
9261 (defun gnus-summary-clear-mark-backward (n)
9262   "Clear marks from N articles backward.
9263 The difference between N and the number of marks cleared is returned."
9264   (interactive "p")
9265   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9266
9267 (defun gnus-summary-mark-unread-as-read ()
9268   "Intended to be used by `gnus-summary-mark-article-hook'."
9269   (when (memq gnus-current-article gnus-newsgroup-unreads)
9270     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9271
9272 (defun gnus-summary-mark-read-and-unread-as-read ()
9273   "Intended to be used by `gnus-summary-mark-article-hook'."
9274   (let ((mark (gnus-summary-article-mark)))
9275     (when (or (gnus-unread-mark-p mark)
9276               (gnus-read-mark-p mark))
9277       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9278
9279 (defun gnus-summary-mark-unread-as-ticked ()
9280   "Intended to be used by `gnus-summary-mark-article-hook'."
9281   (when (memq gnus-current-article gnus-newsgroup-unreads)
9282     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9283
9284 (defun gnus-summary-mark-region-as-read (point mark all)
9285   "Mark all unread articles between point and mark as read.
9286 If given a prefix, mark all articles between point and mark as read,
9287 even ticked and dormant ones."
9288   (interactive "r\nP")
9289   (save-excursion
9290     (let (article)
9291       (goto-char point)
9292       (beginning-of-line)
9293       (while (and
9294               (< (point) mark)
9295               (progn
9296                 (when (or all
9297                           (memq (setq article (gnus-summary-article-number))
9298                                 gnus-newsgroup-unreads))
9299                   (gnus-summary-mark-article article gnus-del-mark))
9300                 t)
9301               (gnus-summary-find-next))))))
9302
9303 (defun gnus-summary-mark-below (score mark)
9304   "Mark articles with score less than SCORE with MARK."
9305   (interactive "P\ncMark: ")
9306   (setq score (if score
9307                   (prefix-numeric-value score)
9308                 (or gnus-summary-default-score 0)))
9309   (save-excursion
9310     (set-buffer gnus-summary-buffer)
9311     (goto-char (point-min))
9312     (while
9313         (progn
9314           (and (< (gnus-summary-article-score) score)
9315                (gnus-summary-mark-article nil mark))
9316           (gnus-summary-find-next)))))
9317
9318 (defun gnus-summary-kill-below (&optional score)
9319   "Mark articles with score below SCORE as read."
9320   (interactive "P")
9321   (gnus-summary-mark-below score gnus-killed-mark))
9322
9323 (defun gnus-summary-clear-above (&optional score)
9324   "Clear all marks from articles with score above SCORE."
9325   (interactive "P")
9326   (gnus-summary-mark-above score gnus-unread-mark))
9327
9328 (defun gnus-summary-tick-above (&optional score)
9329   "Tick all articles with score above SCORE."
9330   (interactive "P")
9331   (gnus-summary-mark-above score gnus-ticked-mark))
9332
9333 (defun gnus-summary-mark-above (score mark)
9334   "Mark articles with score over SCORE with MARK."
9335   (interactive "P\ncMark: ")
9336   (setq score (if score
9337                   (prefix-numeric-value score)
9338                 (or gnus-summary-default-score 0)))
9339   (save-excursion
9340     (set-buffer gnus-summary-buffer)
9341     (goto-char (point-min))
9342     (while (and (progn
9343                   (when (> (gnus-summary-article-score) score)
9344                     (gnus-summary-mark-article nil mark))
9345                   t)
9346                 (gnus-summary-find-next)))))
9347
9348 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9349 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9350 (defun gnus-summary-limit-include-expunged (&optional no-error)
9351   "Display all the hidden articles that were expunged for low scores."
9352   (interactive)
9353   (let ((buffer-read-only nil))
9354     (let ((scored gnus-newsgroup-scored)
9355           headers h)
9356       (while scored
9357         (unless (gnus-summary-article-header (caar scored))
9358           (and (setq h (gnus-number-to-header (caar scored)))
9359                (< (cdar scored) gnus-summary-expunge-below)
9360                (push h headers)))
9361         (setq scored (cdr scored)))
9362       (if (not headers)
9363           (when (not no-error)
9364             (error "No expunged articles hidden"))
9365         (goto-char (point-min))
9366         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9367         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9368         (mapcar (lambda (x) (push (mail-header-number x)
9369                                   gnus-newsgroup-limit))
9370                 headers)
9371         (gnus-summary-prepare-unthreaded (nreverse headers))
9372         (goto-char (point-min))
9373         (gnus-summary-position-point)
9374         t))))
9375
9376 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9377   "Mark all unread articles in this newsgroup as read.
9378 If prefix argument ALL is non-nil, ticked and dormant articles will
9379 also be marked as read.
9380 If QUIETLY is non-nil, no questions will be asked.
9381 If TO-HERE is non-nil, it should be a point in the buffer.  All
9382 articles before (after, if REVERSE is set) this point will be marked as read.
9383 Note that this function will only catch up the unread article
9384 in the current summary buffer limitation.
9385 The number of articles marked as read is returned."
9386   (interactive "P")
9387   (prog1
9388       (save-excursion
9389         (when (or quietly
9390                   (not gnus-interactive-catchup) ;Without confirmation?
9391                   gnus-expert-user
9392                   (gnus-y-or-n-p
9393                    (if all
9394                        "Mark absolutely all articles as read? "
9395                      "Mark all unread articles as read? ")))
9396           (if (and not-mark
9397                    (not gnus-newsgroup-adaptive)
9398                    (not gnus-newsgroup-auto-expire)
9399                    (not gnus-suppress-duplicates)
9400                    (or (not gnus-use-cache)
9401                        (eq gnus-use-cache 'passive)))
9402               (progn
9403                 (when all
9404                   (setq gnus-newsgroup-marked nil
9405                         gnus-newsgroup-dormant nil))
9406                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9407             ;; We actually mark all articles as canceled, which we
9408             ;; have to do when using auto-expiry or adaptive scoring.
9409             (gnus-summary-show-all-threads)
9410             (if (and to-here reverse)
9411                 (progn
9412                   (goto-char to-here)
9413                   (while (and
9414                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9415                           (gnus-summary-find-next (not all) nil nil t))))
9416               (when (gnus-summary-first-subject (not all) t)
9417                 (while (and
9418                         (if to-here (< (point) to-here) t)
9419                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9420                         (gnus-summary-find-next (not all) nil nil t)))))
9421             (gnus-set-mode-line 'summary))
9422           t))
9423     (gnus-summary-position-point)))
9424
9425 (defun gnus-summary-catchup-to-here (&optional all)
9426   "Mark all unticked articles before the current one as read.
9427 If ALL is non-nil, also mark ticked and dormant articles as read."
9428   (interactive "P")
9429   (save-excursion
9430     (gnus-save-hidden-threads
9431       (let ((beg (point)))
9432         ;; We check that there are unread articles.
9433         (when (or all (gnus-summary-find-prev))
9434           (gnus-summary-catchup all t beg)))))
9435   (gnus-summary-position-point))
9436
9437 (defun gnus-summary-catchup-from-here (&optional all)
9438   "Mark all unticked articles after the current one as read.
9439 If ALL is non-nil, also mark ticked and dormant articles as read."
9440   (interactive "P")
9441   (save-excursion
9442     (gnus-save-hidden-threads
9443       (let ((beg (point)))
9444         ;; We check that there are unread articles.
9445         (when (or all (gnus-summary-find-next))
9446           (gnus-summary-catchup all t beg nil t)))))
9447   (gnus-summary-position-point))
9448
9449 (defun gnus-summary-catchup-all (&optional quietly)
9450   "Mark all articles in this newsgroup as read."
9451   (interactive "P")
9452   (gnus-summary-catchup t quietly))
9453
9454 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9455   "Mark all unread articles in this group as read, then exit.
9456 If prefix argument ALL is non-nil, all articles are marked as read.
9457 If QUIETLY is non-nil, no questions will be asked."
9458   (interactive "P")
9459   (when (gnus-summary-catchup all quietly nil 'fast)
9460     ;; Select next newsgroup or exit.
9461     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9462              (eq gnus-auto-select-next 'quietly))
9463         (gnus-summary-next-group nil)
9464       (gnus-summary-exit))))
9465
9466 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9467   "Mark all articles in this newsgroup as read, and then exit."
9468   (interactive "P")
9469   (gnus-summary-catchup-and-exit t quietly))
9470
9471 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9472   "Mark all articles in this group as read and select the next group.
9473 If given a prefix, mark all articles, unread as well as ticked, as
9474 read."
9475   (interactive "P")
9476   (save-excursion
9477     (gnus-summary-catchup all))
9478   (gnus-summary-next-group))
9479
9480 ;;;
9481 ;;; with article
9482 ;;;
9483
9484 (defmacro gnus-with-article (article &rest forms)
9485   "Select ARTICLE and perform FORMS in the original article buffer.
9486 Then replace the article with the result."
9487   `(progn
9488      ;; We don't want the article to be marked as read.
9489      (let (gnus-mark-article-hook)
9490        (gnus-summary-select-article t t nil ,article))
9491      (set-buffer gnus-original-article-buffer)
9492      ,@forms
9493      (if (not (gnus-check-backend-function
9494                'request-replace-article (car gnus-article-current)))
9495          (gnus-message 5 "Read-only group; not replacing")
9496        (unless (gnus-request-replace-article
9497                 ,article (car gnus-article-current)
9498                 (current-buffer) t)
9499          (error "Couldn't replace article")))
9500      ;; The cache and backlog have to be flushed somewhat.
9501      (when gnus-keep-backlog
9502        (gnus-backlog-remove-article
9503         (car gnus-article-current) (cdr gnus-article-current)))
9504      (when gnus-use-cache
9505        (gnus-cache-update-article
9506         (car gnus-article-current) (cdr gnus-article-current)))))
9507
9508 (put 'gnus-with-article 'lisp-indent-function 1)
9509 (put 'gnus-with-article 'edebug-form-spec '(form body))
9510
9511 ;; Thread-based commands.
9512
9513 (defun gnus-summary-articles-in-thread (&optional article)
9514   "Return a list of all articles in the current thread.
9515 If ARTICLE is non-nil, return all articles in the thread that starts
9516 with that article."
9517   (let* ((article (or article (gnus-summary-article-number)))
9518          (data (gnus-data-find-list article))
9519          (top-level (gnus-data-level (car data)))
9520          (top-subject
9521           (cond ((null gnus-thread-operation-ignore-subject)
9522                  (gnus-simplify-subject-re
9523                   (mail-header-subject (gnus-data-header (car data)))))
9524                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9525                  (gnus-simplify-subject-fuzzy
9526                   (mail-header-subject (gnus-data-header (car data)))))
9527                 (t nil)))
9528          (end-point (save-excursion
9529                       (if (gnus-summary-go-to-next-thread)
9530                           (point) (point-max))))
9531          articles)
9532     (while (and data
9533                 (< (gnus-data-pos (car data)) end-point))
9534       (when (or (not top-subject)
9535                 (string= top-subject
9536                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9537                              (gnus-simplify-subject-fuzzy
9538                               (mail-header-subject
9539                                (gnus-data-header (car data))))
9540                            (gnus-simplify-subject-re
9541                             (mail-header-subject
9542                              (gnus-data-header (car data)))))))
9543         (push (gnus-data-number (car data)) articles))
9544       (unless (and (setq data (cdr data))
9545                    (> (gnus-data-level (car data)) top-level))
9546         (setq data nil)))
9547     ;; Return the list of articles.
9548     (nreverse articles)))
9549
9550 (defun gnus-summary-rethread-current ()
9551   "Rethread the thread the current article is part of."
9552   (interactive)
9553   (let* ((gnus-show-threads t)
9554          (article (gnus-summary-article-number))
9555          (id (mail-header-id (gnus-summary-article-header)))
9556          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9557     (unless id
9558       (error "No article on the current line"))
9559     (gnus-rebuild-thread id)
9560     (gnus-summary-goto-subject article)))
9561
9562 (defun gnus-summary-reparent-thread ()
9563   "Make the current article child of the marked (or previous) article.
9564
9565 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9566 is non-nil or the Subject: of both articles are the same."
9567   (interactive)
9568   (unless (not (gnus-group-read-only-p))
9569     (error "The current newsgroup does not support article editing"))
9570   (unless (<= (length gnus-newsgroup-processable) 1)
9571     (error "No more than one article may be marked"))
9572   (save-window-excursion
9573     (let ((gnus-article-buffer " *reparent*")
9574           (current-article (gnus-summary-article-number))
9575           ;; First grab the marked article, otherwise one line up.
9576           (parent-article (if (not (null gnus-newsgroup-processable))
9577                               (car gnus-newsgroup-processable)
9578                             (save-excursion
9579                               (if (eq (forward-line -1) 0)
9580                                   (gnus-summary-article-number)
9581                                 (error "Beginning of summary buffer"))))))
9582       (unless (not (eq current-article parent-article))
9583         (error "An article may not be self-referential"))
9584       (let ((message-id (mail-header-id
9585                          (gnus-summary-article-header parent-article))))
9586         (unless (and message-id (not (equal message-id "")))
9587           (error "No message-id in desired parent"))
9588         (gnus-with-article current-article
9589           (save-restriction
9590             (goto-char (point-min))
9591             (message-narrow-to-head)
9592             (if (re-search-forward "^References: " nil t)
9593                 (progn
9594                   (re-search-forward "^[^ \t]" nil t)
9595                   (forward-line -1)
9596                   (end-of-line)
9597                   (insert " " message-id))
9598               (insert "References: " message-id "\n"))))
9599         (set-buffer gnus-summary-buffer)
9600         (gnus-summary-unmark-all-processable)
9601         (gnus-summary-update-article current-article)
9602         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9603             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9604         (gnus-summary-rethread-current)
9605         (gnus-message 3 "Article %d is now the child of article %d"
9606                       current-article parent-article)))))
9607
9608 (defun gnus-summary-toggle-threads (&optional arg)
9609   "Toggle showing conversation threads.
9610 If ARG is positive number, turn showing conversation threads on."
9611   (interactive "P")
9612   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9613     (setq gnus-show-threads
9614           (if (null arg) (not gnus-show-threads)
9615             (> (prefix-numeric-value arg) 0)))
9616     (gnus-summary-prepare)
9617     (gnus-summary-goto-subject current)
9618     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9619     (gnus-summary-position-point)))
9620
9621 (defun gnus-summary-show-all-threads ()
9622   "Show all threads."
9623   (interactive)
9624   (save-excursion
9625     (let ((buffer-read-only nil))
9626       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9627   (gnus-summary-position-point))
9628
9629 (defun gnus-summary-show-thread ()
9630   "Show thread subtrees.
9631 Returns nil if no thread was there to be shown."
9632   (interactive)
9633   (let ((buffer-read-only nil)
9634         (orig (point))
9635         ;; first goto end then to beg, to have point at beg after let
9636         (end (progn (end-of-line) (point)))
9637         (beg (progn (beginning-of-line) (point))))
9638     (prog1
9639         ;; Any hidden lines here?
9640         (search-forward "\r" end t)
9641       (subst-char-in-region beg end ?\^M ?\n t)
9642       (goto-char orig)
9643       (gnus-summary-position-point))))
9644
9645 (defun gnus-summary-hide-all-threads ()
9646   "Hide all thread subtrees."
9647   (interactive)
9648   (save-excursion
9649     (goto-char (point-min))
9650     (gnus-summary-hide-thread)
9651     (while (zerop (gnus-summary-next-thread 1 t))
9652       (gnus-summary-hide-thread)))
9653   (gnus-summary-position-point))
9654
9655 (defun gnus-summary-hide-thread ()
9656   "Hide thread subtrees.
9657 Returns nil if no threads were there to be hidden."
9658   (interactive)
9659   (let ((buffer-read-only nil)
9660         (start (point))
9661         (article (gnus-summary-article-number)))
9662     (goto-char start)
9663     ;; Go forward until either the buffer ends or the subthread
9664     ;; ends.
9665     (when (and (not (eobp))
9666                (or (zerop (gnus-summary-next-thread 1 t))
9667                    (goto-char (point-max))))
9668       (prog1
9669           (if (and (> (point) start)
9670                    (search-backward "\n" start t))
9671               (progn
9672                 (subst-char-in-region start (point) ?\n ?\^M)
9673                 (gnus-summary-goto-subject article))
9674             (goto-char start)
9675             nil)))))
9676
9677 (defun gnus-summary-go-to-next-thread (&optional previous)
9678   "Go to the same level (or less) next thread.
9679 If PREVIOUS is non-nil, go to previous thread instead.
9680 Return the article number moved to, or nil if moving was impossible."
9681   (let ((level (gnus-summary-thread-level))
9682         (way (if previous -1 1))
9683         (beg (point)))
9684     (forward-line way)
9685     (while (and (not (eobp))
9686                 (< level (gnus-summary-thread-level)))
9687       (forward-line way))
9688     (if (eobp)
9689         (progn
9690           (goto-char beg)
9691           nil)
9692       (setq beg (point))
9693       (prog1
9694           (gnus-summary-article-number)
9695         (goto-char beg)))))
9696
9697 (defun gnus-summary-next-thread (n &optional silent)
9698   "Go to the same level next N'th thread.
9699 If N is negative, search backward instead.
9700 Returns the difference between N and the number of skips actually
9701 done.
9702
9703 If SILENT, don't output messages."
9704   (interactive "p")
9705   (let ((backward (< n 0))
9706         (n (abs n)))
9707     (while (and (> n 0)
9708                 (gnus-summary-go-to-next-thread backward))
9709       (decf n))
9710     (unless silent
9711       (gnus-summary-position-point))
9712     (when (and (not silent) (/= 0 n))
9713       (gnus-message 7 "No more threads"))
9714     n))
9715
9716 (defun gnus-summary-prev-thread (n)
9717   "Go to the same level previous N'th thread.
9718 Returns the difference between N and the number of skips actually
9719 done."
9720   (interactive "p")
9721   (gnus-summary-next-thread (- n)))
9722
9723 (defun gnus-summary-go-down-thread ()
9724   "Go down one level in the current thread."
9725   (let ((children (gnus-summary-article-children)))
9726     (when children
9727       (gnus-summary-goto-subject (car children)))))
9728
9729 (defun gnus-summary-go-up-thread ()
9730   "Go up one level in the current thread."
9731   (let ((parent (gnus-summary-article-parent)))
9732     (when parent
9733       (gnus-summary-goto-subject parent))))
9734
9735 (defun gnus-summary-down-thread (n)
9736   "Go down thread N steps.
9737 If N is negative, go up instead.
9738 Returns the difference between N and how many steps down that were
9739 taken."
9740   (interactive "p")
9741   (let ((up (< n 0))
9742         (n (abs n)))
9743     (while (and (> n 0)
9744                 (if up (gnus-summary-go-up-thread)
9745                   (gnus-summary-go-down-thread)))
9746       (setq n (1- n)))
9747     (gnus-summary-position-point)
9748     (when (/= 0 n)
9749       (gnus-message 7 "Can't go further"))
9750     n))
9751
9752 (defun gnus-summary-up-thread (n)
9753   "Go up thread N steps.
9754 If N is negative, go down instead.
9755 Returns the difference between N and how many steps down that were
9756 taken."
9757   (interactive "p")
9758   (gnus-summary-down-thread (- n)))
9759
9760 (defun gnus-summary-top-thread ()
9761   "Go to the top of the thread."
9762   (interactive)
9763   (while (gnus-summary-go-up-thread))
9764   (gnus-summary-article-number))
9765
9766 (defun gnus-summary-kill-thread (&optional unmark)
9767   "Mark articles under current thread as read.
9768 If the prefix argument is positive, remove any kinds of marks.
9769 If the prefix argument is negative, tick articles instead."
9770   (interactive "P")
9771   (when unmark
9772     (setq unmark (prefix-numeric-value unmark)))
9773   (let ((articles (gnus-summary-articles-in-thread)))
9774     (save-excursion
9775       ;; Expand the thread.
9776       (gnus-summary-show-thread)
9777       ;; Mark all the articles.
9778       (while articles
9779         (gnus-summary-goto-subject (car articles))
9780         (cond ((null unmark)
9781                (gnus-summary-mark-article-as-read gnus-killed-mark))
9782               ((> unmark 0)
9783                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9784               (t
9785                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9786         (setq articles (cdr articles))))
9787     ;; Hide killed subtrees.
9788     (and (null unmark)
9789          gnus-thread-hide-killed
9790          (gnus-summary-hide-thread))
9791     ;; If marked as read, go to next unread subject.
9792     (when (null unmark)
9793       ;; Go to next unread subject.
9794       (gnus-summary-next-subject 1 t)))
9795   (gnus-set-mode-line 'summary))
9796
9797 ;; Summary sorting commands
9798
9799 (defun gnus-summary-sort-by-number (&optional reverse)
9800   "Sort the summary buffer by article number.
9801 Argument REVERSE means reverse order."
9802   (interactive "P")
9803   (gnus-summary-sort 'number reverse))
9804
9805 (defun gnus-summary-sort-by-author (&optional reverse)
9806   "Sort the summary buffer by author name alphabetically.
9807 If `case-fold-search' is non-nil, case of letters is ignored.
9808 Argument REVERSE means reverse order."
9809   (interactive "P")
9810   (gnus-summary-sort 'author reverse))
9811
9812 (defun gnus-summary-sort-by-subject (&optional reverse)
9813   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9814 If `case-fold-search' is non-nil, case of letters is ignored.
9815 Argument REVERSE means reverse order."
9816   (interactive "P")
9817   (gnus-summary-sort 'subject reverse))
9818
9819 (defun gnus-summary-sort-by-date (&optional reverse)
9820   "Sort the summary buffer by date.
9821 Argument REVERSE means reverse order."
9822   (interactive "P")
9823   (gnus-summary-sort 'date reverse))
9824
9825 (defun gnus-summary-sort-by-score (&optional reverse)
9826   "Sort the summary buffer by score.
9827 Argument REVERSE means reverse order."
9828   (interactive "P")
9829   (gnus-summary-sort 'score reverse))
9830
9831 (defun gnus-summary-sort-by-lines (&optional reverse)
9832   "Sort the summary buffer by the number of lines.
9833 Argument REVERSE means reverse order."
9834   (interactive "P")
9835   (gnus-summary-sort 'lines reverse))
9836
9837 (defun gnus-summary-sort-by-chars (&optional reverse)
9838   "Sort the summary buffer by article length.
9839 Argument REVERSE means reverse order."
9840   (interactive "P")
9841   (gnus-summary-sort 'chars reverse))
9842
9843 (defun gnus-summary-sort-by-original (&optional reverse)
9844   "Sort the summary buffer using the default sorting method.
9845 Argument REVERSE means reverse order."
9846   (interactive "P")
9847   (let* ((buffer-read-only)
9848          (gnus-summary-prepare-hook nil))
9849     ;; We do the sorting by regenerating the threads.
9850     (gnus-summary-prepare)
9851     ;; Hide subthreads if needed.
9852     (when (and gnus-show-threads gnus-thread-hide-subtree)
9853       (gnus-summary-hide-all-threads))))
9854
9855 (defun gnus-summary-sort (predicate reverse)
9856   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9857   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9858          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9859          (gnus-thread-sort-functions
9860           (if (not reverse)
9861               thread
9862             `(lambda (t1 t2)
9863                (,thread t2 t1))))
9864          (gnus-sort-gathered-threads-function
9865           gnus-thread-sort-functions)
9866          (gnus-article-sort-functions
9867           (if (not reverse)
9868               article
9869             `(lambda (t1 t2)
9870                (,article t2 t1))))
9871          (buffer-read-only)
9872          (gnus-summary-prepare-hook nil))
9873     ;; We do the sorting by regenerating the threads.
9874     (gnus-summary-prepare)
9875     ;; Hide subthreads if needed.
9876     (when (and gnus-show-threads gnus-thread-hide-subtree)
9877       (gnus-summary-hide-all-threads))))
9878
9879 ;; Summary saving commands.
9880
9881 (defun gnus-summary-save-article (&optional n not-saved)
9882   "Save the current article using the default saver function.
9883 If N is a positive number, save the N next articles.
9884 If N is a negative number, save the N previous articles.
9885 If N is nil and any articles have been marked with the process mark,
9886 save those articles instead.
9887 The variable `gnus-default-article-saver' specifies the saver function."
9888   (interactive "P")
9889   (let* ((articles (gnus-summary-work-articles n))
9890          (save-buffer (save-excursion
9891                         (nnheader-set-temp-buffer " *Gnus Save*")))
9892          (num (length articles))
9893          header file)
9894     (dolist (article articles)
9895       (setq header (gnus-summary-article-header article))
9896       (if (not (vectorp header))
9897           ;; This is a pseudo-article.
9898           (if (assq 'name header)
9899               (gnus-copy-file (cdr (assq 'name header)))
9900             (gnus-message 1 "Article %d is unsaveable" article))
9901         ;; This is a real article.
9902         (save-window-excursion
9903           (gnus-summary-select-article t nil nil article))
9904         (save-excursion
9905           (set-buffer save-buffer)
9906           (erase-buffer)
9907           (insert-buffer-substring gnus-original-article-buffer))
9908         (setq file (gnus-article-save save-buffer file num))
9909         (gnus-summary-remove-process-mark article)
9910         (unless not-saved
9911           (gnus-summary-set-saved-mark article))))
9912     (gnus-kill-buffer save-buffer)
9913     (gnus-summary-position-point)
9914     (gnus-set-mode-line 'summary)
9915     n))
9916
9917 (defun gnus-summary-pipe-output (&optional arg)
9918   "Pipe the current article to a subprocess.
9919 If N is a positive number, pipe the N next articles.
9920 If N is a negative number, pipe the N previous articles.
9921 If N is nil and any articles have been marked with the process mark,
9922 pipe those articles instead."
9923   (interactive "P")
9924   (require 'gnus-art)
9925   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9926     (gnus-summary-save-article arg t))
9927   (let ((buffer (get-buffer "*Shell Command Output*")))
9928     (if (and buffer
9929              (with-current-buffer buffer (> (point-max) (point-min))))
9930         (gnus-configure-windows 'pipe))))
9931
9932 (defun gnus-summary-save-article-mail (&optional arg)
9933   "Append the current article to an mail file.
9934 If N is a positive number, save the N next articles.
9935 If N is a negative number, save the N previous articles.
9936 If N is nil and any articles have been marked with the process mark,
9937 save those articles instead."
9938   (interactive "P")
9939   (require 'gnus-art)
9940   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9941     (gnus-summary-save-article arg)))
9942
9943 (defun gnus-summary-save-article-rmail (&optional arg)
9944   "Append the current article to an rmail file.
9945 If N is a positive number, save the N next articles.
9946 If N is a negative number, save the N previous articles.
9947 If N is nil and any articles have been marked with the process mark,
9948 save those articles instead."
9949   (interactive "P")
9950   (require 'gnus-art)
9951   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9952     (gnus-summary-save-article arg)))
9953
9954 (defun gnus-summary-save-article-file (&optional arg)
9955   "Append the current article to a file.
9956 If N is a positive number, save the N next articles.
9957 If N is a negative number, save the N previous articles.
9958 If N is nil and any articles have been marked with the process mark,
9959 save those articles instead."
9960   (interactive "P")
9961   (require 'gnus-art)
9962   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9963     (gnus-summary-save-article arg)))
9964
9965 (defun gnus-summary-write-article-file (&optional arg)
9966   "Write the current article to a file, deleting the previous file.
9967 If N is a positive number, save the N next articles.
9968 If N is a negative number, save the N previous articles.
9969 If N is nil and any articles have been marked with the process mark,
9970 save those articles instead."
9971   (interactive "P")
9972   (require 'gnus-art)
9973   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9974     (gnus-summary-save-article arg)))
9975
9976 (defun gnus-summary-save-article-body-file (&optional arg)
9977   "Append the current article body to a file.
9978 If N is a positive number, save the N next articles.
9979 If N is a negative number, save the N previous articles.
9980 If N is nil and any articles have been marked with the process mark,
9981 save those articles instead."
9982   (interactive "P")
9983   (require 'gnus-art)
9984   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9985     (gnus-summary-save-article arg)))
9986
9987 (defun gnus-summary-pipe-message (program)
9988   "Pipe the current article through PROGRAM."
9989   (interactive "sProgram: ")
9990   (gnus-summary-select-article)
9991   (let ((mail-header-separator ""))
9992     (gnus-eval-in-buffer-window gnus-article-buffer
9993       (save-restriction
9994         (widen)
9995         (let ((start (window-start))
9996               buffer-read-only)
9997           (message-pipe-buffer-body program)
9998           (set-window-start (get-buffer-window (current-buffer)) start))))))
9999
10000 (defun gnus-get-split-value (methods)
10001   "Return a value based on the split METHODS."
10002   (let (split-name method result match)
10003     (when methods
10004       (save-excursion
10005         (set-buffer gnus-original-article-buffer)
10006         (save-restriction
10007           (nnheader-narrow-to-headers)
10008           (while (and methods (not split-name))
10009             (goto-char (point-min))
10010             (setq method (pop methods))
10011             (setq match (car method))
10012             (when (cond
10013                    ((stringp match)
10014                     ;; Regular expression.
10015                     (ignore-errors
10016                       (re-search-forward match nil t)))
10017                    ((gnus-functionp match)
10018                     ;; Function.
10019                     (save-restriction
10020                       (widen)
10021                       (setq result (funcall match gnus-newsgroup-name))))
10022                    ((consp match)
10023                     ;; Form.
10024                     (save-restriction
10025                       (widen)
10026                       (setq result (eval match)))))
10027               (setq split-name (cdr method))
10028               (cond ((stringp result)
10029                      (push (expand-file-name
10030                             result gnus-article-save-directory)
10031                            split-name))
10032                     ((consp result)
10033                      (setq split-name (append result split-name)))))))))
10034     (nreverse split-name)))
10035
10036 (defun gnus-valid-move-group-p (group)
10037   (and (boundp group)
10038        (symbol-name group)
10039        (symbol-value group)
10040        (gnus-get-function (gnus-find-method-for-group
10041                            (symbol-name group)) 'request-accept-article t)))
10042
10043 (defun gnus-read-move-group-name (prompt default articles prefix)
10044   "Read a group name."
10045   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10046          (minibuffer-confirm-incomplete nil) ; XEmacs
10047          (prom
10048           (format "%s %s to:"
10049                   prompt
10050                   (if (> (length articles) 1)
10051                       (format "these %d articles" (length articles))
10052                     "this article")))
10053          (to-newsgroup
10054           (cond
10055            ((null split-name)
10056             (gnus-completing-read default prom
10057                                   gnus-active-hashtb
10058                                   'gnus-valid-move-group-p
10059                                   nil prefix
10060                                   'gnus-group-history))
10061            ((= 1 (length split-name))
10062             (gnus-completing-read (car split-name) prom
10063                                   gnus-active-hashtb
10064                                   'gnus-valid-move-group-p
10065                                   nil nil
10066                                   'gnus-group-history))
10067            (t
10068             (gnus-completing-read nil prom
10069                                   (mapcar (lambda (el) (list el))
10070                                           (nreverse split-name))
10071                                   nil nil nil
10072                                   'gnus-group-history))))
10073          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10074     (when to-newsgroup
10075       (if (or (string= to-newsgroup "")
10076               (string= to-newsgroup prefix))
10077           (setq to-newsgroup default))
10078       (unless to-newsgroup
10079         (error "No group name entered"))
10080       (or (gnus-active to-newsgroup)
10081           (gnus-activate-group to-newsgroup nil nil to-method)
10082           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10083                                      to-newsgroup))
10084               (or (and (gnus-request-create-group to-newsgroup to-method)
10085                        (gnus-activate-group
10086                         to-newsgroup nil nil to-method)
10087                        (gnus-subscribe-group to-newsgroup))
10088                   (error "Couldn't create group %s" to-newsgroup)))
10089           (error "No such group: %s" to-newsgroup)))
10090     to-newsgroup))
10091
10092 (defun gnus-summary-save-parts (type dir n &optional reverse)
10093   "Save parts matching TYPE to DIR.
10094 If REVERSE, save parts that do not match TYPE."
10095   (interactive
10096    (list (read-string "Save parts of type: "
10097                       (or (car gnus-summary-save-parts-type-history)
10098                           gnus-summary-save-parts-default-mime)
10099                       'gnus-summary-save-parts-type-history)
10100          (setq gnus-summary-save-parts-last-directory
10101                (read-file-name "Save to directory: "
10102                                gnus-summary-save-parts-last-directory
10103                                nil t))
10104          current-prefix-arg))
10105   (gnus-summary-iterate n
10106     (let ((gnus-display-mime-function nil)
10107           (gnus-inhibit-treatment t))
10108       (gnus-summary-select-article))
10109     (save-excursion
10110       (set-buffer gnus-article-buffer)
10111       (let ((handles (or gnus-article-mime-handles
10112                          (mm-dissect-buffer) (mm-uu-dissect))))
10113         (when handles
10114           (gnus-summary-save-parts-1 type dir handles reverse)
10115           (unless gnus-article-mime-handles ;; Don't destroy this case.
10116             (mm-destroy-parts handles)))))))
10117
10118 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10119   (if (stringp (car handle))
10120       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10121               (cdr handle))
10122     (when (if reverse
10123               (not (string-match type (mm-handle-media-type handle)))
10124             (string-match type (mm-handle-media-type handle)))
10125       (let ((file (expand-file-name
10126                    (file-name-nondirectory
10127                     (or
10128                      (mail-content-type-get
10129                       (mm-handle-disposition handle) 'filename)
10130                      (concat gnus-newsgroup-name
10131                              "." (number-to-string
10132                                   (cdr gnus-article-current)))))
10133                    dir)))
10134         (unless (file-exists-p file)
10135           (mm-save-part-to-file handle file))))))
10136
10137 ;; Summary extract commands
10138
10139 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10140   (let ((buffer-read-only nil)
10141         (article (gnus-summary-article-number))
10142         after-article b e)
10143     (unless (gnus-summary-goto-subject article)
10144       (error "No such article: %d" article))
10145     (gnus-summary-position-point)
10146     ;; If all commands are to be bunched up on one line, we collect
10147     ;; them here.
10148     (unless gnus-view-pseudos-separately
10149       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10150             files action)
10151         (while ps
10152           (setq action (cdr (assq 'action (car ps))))
10153           (setq files (list (cdr (assq 'name (car ps)))))
10154           (while (and ps (cdr ps)
10155                       (string= (or action "1")
10156                                (or (cdr (assq 'action (cadr ps))) "2")))
10157             (push (cdr (assq 'name (cadr ps))) files)
10158             (setcdr ps (cddr ps)))
10159           (when files
10160             (when (not (string-match "%s" action))
10161               (push " " files))
10162             (push " " files)
10163             (when (assq 'execute (car ps))
10164               (setcdr (assq 'execute (car ps))
10165                       (funcall (if (string-match "%s" action)
10166                                    'format 'concat)
10167                                action
10168                                (mapconcat
10169                                 (lambda (f)
10170                                   (if (equal f " ")
10171                                       f
10172                                     (gnus-quote-arg-for-sh-or-csh f)))
10173                                 files " ")))))
10174           (setq ps (cdr ps)))))
10175     (if (and gnus-view-pseudos (not not-view))
10176         (while pslist
10177           (when (assq 'execute (car pslist))
10178             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10179                                   (eq gnus-view-pseudos 'not-confirm)))
10180           (setq pslist (cdr pslist)))
10181       (save-excursion
10182         (while pslist
10183           (setq after-article (or (cdr (assq 'article (car pslist)))
10184                                   (gnus-summary-article-number)))
10185           (gnus-summary-goto-subject after-article)
10186           (forward-line 1)
10187           (setq b (point))
10188           (insert "    " (file-name-nondirectory
10189                           (cdr (assq 'name (car pslist))))
10190                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10191           (setq e (point))
10192           (forward-line -1)             ; back to `b'
10193           (gnus-add-text-properties
10194            b (1- e) (list 'gnus-number gnus-reffed-article-number
10195                           gnus-mouse-face-prop gnus-mouse-face))
10196           (gnus-data-enter
10197            after-article gnus-reffed-article-number
10198            gnus-unread-mark b (car pslist) 0 (- e b))
10199           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10200           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10201           (setq pslist (cdr pslist)))))))
10202
10203 (defun gnus-pseudos< (p1 p2)
10204   (let ((c1 (cdr (assq 'action p1)))
10205         (c2 (cdr (assq 'action p2))))
10206     (and c1 c2 (string< c1 c2))))
10207
10208 (defun gnus-request-pseudo-article (props)
10209   (cond ((assq 'execute props)
10210          (gnus-execute-command (cdr (assq 'execute props)))))
10211   (let ((gnus-current-article (gnus-summary-article-number)))
10212     (gnus-run-hooks 'gnus-mark-article-hook)))
10213
10214 (defun gnus-execute-command (command &optional automatic)
10215   (save-excursion
10216     (gnus-article-setup-buffer)
10217     (set-buffer gnus-article-buffer)
10218     (setq buffer-read-only nil)
10219     (let ((command (if automatic command
10220                      (read-string "Command: " (cons command 0)))))
10221       (erase-buffer)
10222       (insert "$ " command "\n\n")
10223       (if gnus-view-pseudo-asynchronously
10224           (start-process "gnus-execute" (current-buffer) shell-file-name
10225                          shell-command-switch command)
10226         (call-process shell-file-name nil t nil
10227                       shell-command-switch command)))))
10228
10229 ;; Summary kill commands.
10230
10231 (defun gnus-summary-edit-global-kill (article)
10232   "Edit the \"global\" kill file."
10233   (interactive (list (gnus-summary-article-number)))
10234   (gnus-group-edit-global-kill article))
10235
10236 (defun gnus-summary-edit-local-kill ()
10237   "Edit a local kill file applied to the current newsgroup."
10238   (interactive)
10239   (setq gnus-current-headers (gnus-summary-article-header))
10240   (gnus-group-edit-local-kill
10241    (gnus-summary-article-number) gnus-newsgroup-name))
10242
10243 ;;; Header reading.
10244
10245 (defun gnus-read-header (id &optional header)
10246   "Read the headers of article ID and enter them into the Gnus system."
10247   (let ((group gnus-newsgroup-name)
10248         (gnus-override-method
10249          (or
10250           gnus-override-method
10251           (and (gnus-news-group-p gnus-newsgroup-name)
10252                (car (gnus-refer-article-methods)))))
10253         where)
10254     ;; First we check to see whether the header in question is already
10255     ;; fetched.
10256     (if (stringp id)
10257         ;; This is a Message-ID.
10258         (setq header (or header (gnus-id-to-header id)))
10259       ;; This is an article number.
10260       (setq header (or header (gnus-summary-article-header id))))
10261     (if (and header
10262              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10263         ;; We have found the header.
10264         header
10265       ;; If this is a sparse article, we have to nix out its
10266       ;; previous entry in the thread hashtb.
10267       (when (and header
10268                  (gnus-summary-article-sparse-p (mail-header-number header)))
10269         (let* ((parent (gnus-parent-id (mail-header-references header)))
10270                (thread (and parent (gnus-id-to-thread parent))))
10271           (when thread
10272             (delq (assq header thread) thread))))
10273       ;; We have to really fetch the header to this article.
10274       (save-excursion
10275         (set-buffer nntp-server-buffer)
10276         (when (setq where (gnus-request-head id group))
10277           (nnheader-fold-continuation-lines)
10278           (goto-char (point-max))
10279           (insert ".\n")
10280           (goto-char (point-min))
10281           (insert "211 ")
10282           (princ (cond
10283                   ((numberp id) id)
10284                   ((cdr where) (cdr where))
10285                   (header (mail-header-number header))
10286                   (t gnus-reffed-article-number))
10287                  (current-buffer))
10288           (insert " Article retrieved.\n"))
10289         (if (or (not where)
10290                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10291             ()                          ; Malformed head.
10292           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10293             (when (and (stringp id)
10294                        (not (string= (gnus-group-real-name group)
10295                                      (car where))))
10296               ;; If we fetched by Message-ID and the article came
10297               ;; from a different group, we fudge some bogus article
10298               ;; numbers for this article.
10299               (mail-header-set-number header gnus-reffed-article-number))
10300             (save-excursion
10301               (set-buffer gnus-summary-buffer)
10302               (decf gnus-reffed-article-number)
10303               (gnus-remove-header (mail-header-number header))
10304               (push header gnus-newsgroup-headers)
10305               (setq gnus-current-headers header)
10306               (push (mail-header-number header) gnus-newsgroup-limit)))
10307           header)))))
10308
10309 (defun gnus-remove-header (number)
10310   "Remove header NUMBER from `gnus-newsgroup-headers'."
10311   (if (and gnus-newsgroup-headers
10312            (= number (mail-header-number (car gnus-newsgroup-headers))))
10313       (pop gnus-newsgroup-headers)
10314     (let ((headers gnus-newsgroup-headers))
10315       (while (and (cdr headers)
10316                   (not (= number (mail-header-number (cadr headers)))))
10317         (pop headers))
10318       (when (cdr headers)
10319         (setcdr headers (cddr headers))))))
10320
10321 ;;;
10322 ;;; summary highlights
10323 ;;;
10324
10325 (defun gnus-highlight-selected-summary ()
10326   "Highlight selected article in summary buffer."
10327   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10328   (when gnus-summary-selected-face
10329     (save-excursion
10330       (let* ((beg (progn (beginning-of-line) (point)))
10331              (end (progn (end-of-line) (point)))
10332              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10333              (from (if (get-text-property beg gnus-mouse-face-prop)
10334                        beg
10335                      (or (next-single-property-change
10336                           beg gnus-mouse-face-prop nil end)
10337                          beg)))
10338              (to
10339               (if (= from end)
10340                   (- from 2)
10341                 (or (next-single-property-change
10342                      from gnus-mouse-face-prop nil end)
10343                     end))))
10344         ;; If no mouse-face prop on line we will have to = from = end,
10345         ;; so we highlight the entire line instead.
10346         (when (= (+ to 2) from)
10347           (setq from beg)
10348           (setq to end))
10349         (if gnus-newsgroup-selected-overlay
10350             ;; Move old overlay.
10351             (gnus-move-overlay
10352              gnus-newsgroup-selected-overlay from to (current-buffer))
10353           ;; Create new overlay.
10354           (gnus-overlay-put
10355            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10356            'face gnus-summary-selected-face))))))
10357
10358 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10359 (defun gnus-summary-highlight-line ()
10360   "Highlight current line according to `gnus-summary-highlight'."
10361   (let* ((list gnus-summary-highlight)
10362          (p (point))
10363          (end (progn (end-of-line) (point)))
10364          ;; now find out where the line starts and leave point there.
10365          (beg (progn (beginning-of-line) (point)))
10366          (article (gnus-summary-article-number))
10367          (score (or (cdr (assq (or article gnus-current-article)
10368                                gnus-newsgroup-scored))
10369                     gnus-summary-default-score 0))
10370          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10371          (inhibit-read-only t))
10372     ;; Eval the cars of the lists until we find a match.
10373     (let ((default gnus-summary-default-score)
10374           (default-high gnus-summary-default-high-score)
10375           (default-low gnus-summary-default-low-score))
10376       (while (and list
10377                   (not (eval (caar list))))
10378         (setq list (cdr list))))
10379     (let ((face (cdar list)))
10380       (unless (eq face (get-text-property beg 'face))
10381         (gnus-put-text-property-excluding-characters-with-faces
10382          beg end 'face
10383          (setq face (if (boundp face) (symbol-value face) face)))
10384         (when gnus-summary-highlight-line-function
10385           (funcall gnus-summary-highlight-line-function article face))))
10386     (goto-char p)))
10387
10388 (defun gnus-update-read-articles (group unread &optional compute)
10389   "Update the list of read articles in GROUP."
10390   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10391          (entry (gnus-gethash group gnus-newsrc-hashtb))
10392          (info (nth 2 entry))
10393          (prev 1)
10394          (unread (sort (copy-sequence unread) '<))
10395          read)
10396     (if (or (not info) (not active))
10397         ;; There is no info on this group if it was, in fact,
10398         ;; killed.  Gnus stores no information on killed groups, so
10399         ;; there's nothing to be done.
10400         ;; One could store the information somewhere temporarily,
10401         ;; perhaps...  Hmmm...
10402         ()
10403       ;; Remove any negative articles numbers.
10404       (while (and unread (< (car unread) 0))
10405         (setq unread (cdr unread)))
10406       ;; Remove any expired article numbers
10407       (while (and unread (< (car unread) (car active)))
10408         (setq unread (cdr unread)))
10409       ;; Compute the ranges of read articles by looking at the list of
10410       ;; unread articles.
10411       (while unread
10412         (when (/= (car unread) prev)
10413           (push (if (= prev (1- (car unread))) prev
10414                   (cons prev (1- (car unread))))
10415                 read))
10416         (setq prev (1+ (car unread)))
10417         (setq unread (cdr unread)))
10418       (when (<= prev (cdr active))
10419         (push (cons prev (cdr active)) read))
10420       (setq read (if (> (length read) 1) (nreverse read) read))
10421       (if compute
10422           read
10423         (save-excursion
10424           (let (setmarkundo)
10425             ;; Propagate the read marks to the backend.
10426             (when (gnus-check-backend-function 'request-set-mark group)
10427               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10428                     (add (gnus-remove-from-range read (gnus-info-read info))))
10429                 (when (or add del)
10430                   (unless (gnus-check-group group)
10431                     (error "Can't open server for %s" group))
10432                   (gnus-request-set-mark
10433                    group (delq nil (list (if add (list add 'add '(read)))
10434                                          (if del (list del 'del '(read))))))
10435                   (setq setmarkundo
10436                         `(gnus-request-set-mark
10437                           ,group
10438                           ',(delq nil (list
10439                                        (if del (list del 'add '(read)))
10440                                        (if add (list add 'del '(read))))))))))
10441             (set-buffer gnus-group-buffer)
10442             (gnus-undo-register
10443               `(progn
10444                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10445                  (gnus-info-set-read ',info ',(gnus-info-read info))
10446                  (gnus-get-unread-articles-in-group ',info
10447                                                     (gnus-active ,group))
10448                  (gnus-group-update-group ,group t)
10449                  ,setmarkundo))))
10450         ;; Enter this list into the group info.
10451         (gnus-info-set-read info read)
10452         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10453         (gnus-get-unread-articles-in-group info (gnus-active group))
10454         t))))
10455
10456 (defun gnus-offer-save-summaries ()
10457   "Offer to save all active summary buffers."
10458   (let (buffers)
10459     ;; Go through all buffers and find all summaries.
10460     (dolist (buffer (buffer-list))
10461       (when (and (setq buffer (buffer-name buffer))
10462                  (string-match "Summary" buffer)
10463                  (save-excursion
10464                    (set-buffer buffer)
10465                    ;; We check that this is, indeed, a summary buffer.
10466                    (and (eq major-mode 'gnus-summary-mode)
10467                         ;; Also make sure this isn't bogus.
10468                         gnus-newsgroup-prepared
10469                         ;; Also make sure that this isn't a
10470                         ;; dead summary buffer.
10471                         (not gnus-dead-summary-mode))))
10472         (push buffer buffers)))
10473     ;; Go through all these summary buffers and offer to save them.
10474     (when buffers
10475       (save-excursion
10476         (map-y-or-n-p
10477          "Update summary buffer %s? "
10478          (lambda (buf)
10479            (switch-to-buffer buf)
10480            (gnus-summary-exit))
10481          buffers)))))
10482
10483
10484 ;;; @ for mime-partial
10485 ;;;
10486
10487 (defun gnus-request-partial-message ()
10488   (save-excursion
10489     (let ((number (gnus-summary-article-number))
10490           (group gnus-newsgroup-name)
10491           (mother gnus-article-buffer))
10492       (set-buffer (get-buffer-create " *Partial Article*"))
10493       (erase-buffer)
10494       (setq mime-preview-buffer mother)
10495       (gnus-request-article-this-buffer number group)
10496       (mime-parse-buffer)
10497       )))
10498
10499 (autoload 'mime-combine-message/partial-pieces-automatically
10500   "mime-partial"
10501   "Internal method to combine message/partial messages automatically.")
10502
10503 (mime-add-condition
10504  'action '((type . message)(subtype . partial)
10505            (major-mode . gnus-original-article-mode)
10506            (method . mime-combine-message/partial-pieces-automatically)
10507            (summary-buffer-exp . gnus-summary-buffer)
10508            (request-partial-message-method . gnus-request-partial-message)
10509            ))
10510
10511
10512 ;;; @ for message/rfc822
10513 ;;;
10514
10515 (defun gnus-mime-extract-message/rfc822 (entity situation)
10516   (let (group article num cwin swin cur)
10517     (with-temp-buffer
10518       (mime-insert-entity-content entity)
10519       (setq group (or (cdr (assq 'group situation))
10520                       (completing-read "Group: "
10521                                        gnus-active-hashtb
10522                                        nil
10523                                        (gnus-read-active-file-p)
10524                                        gnus-newsgroup-name))
10525             article (gnus-request-accept-article group)))
10526     (when (and (consp article)
10527                (numberp (setq article (cdr article))))
10528       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10529             cwin (get-buffer-window (current-buffer) t))
10530       (save-window-excursion
10531         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10532             (select-window swin)
10533           (set-buffer gnus-summary-buffer))
10534         (setq cur gnus-current-article)
10535         (forward-line num)
10536         (let (gnus-show-threads)
10537           (gnus-summary-goto-subject article t))
10538         (gnus-summary-clear-mark-forward 1)
10539         (gnus-summary-goto-subject cur))
10540       (when (and cwin (window-frame cwin))
10541         (select-frame (window-frame cwin)))
10542       (when (boundp 'mime-acting-situation-to-override)
10543         (set-alist 'mime-acting-situation-to-override
10544                    'group
10545                    group)
10546         (set-alist 'mime-acting-situation-to-override
10547                    'after-method
10548                    `(progn
10549                       (save-current-buffer
10550                         (set-buffer gnus-group-buffer)
10551                         (gnus-activate-group ,group))
10552                       (gnus-summary-goto-article ,cur
10553                                                  gnus-show-all-headers)))
10554         (set-alist 'mime-acting-situation-to-override
10555                    'number num)))))
10556
10557 (mime-add-condition
10558  'action '((type . message)(subtype . rfc822)
10559            (major-mode . gnus-original-article-mode)
10560            (method . gnus-mime-extract-message/rfc822)
10561            (mode . "extract")
10562            ))
10563
10564 (mime-add-condition
10565  'action '((type . message)(subtype . news)
10566            (major-mode . gnus-original-article-mode)
10567            (method . gnus-mime-extract-message/rfc822)
10568            (mode . "extract")
10569            ))
10570
10571 (defun gnus-mime-extract-multipart (entity situation)
10572   (let ((children (mime-entity-children entity))
10573         mime-acting-situation-to-override
10574         f)
10575     (while children
10576       (mime-play-entity (car children)
10577                         (cons (assq 'mode situation)
10578                               mime-acting-situation-to-override))
10579       (setq children (cdr children)))
10580     (if (setq f (cdr (assq 'after-method
10581                            mime-acting-situation-to-override)))
10582         (eval f)
10583       )))
10584
10585 (mime-add-condition
10586  'action '((type . multipart)
10587            (method . gnus-mime-extract-multipart)
10588            (mode . "extract")
10589            )
10590  'with-default)
10591
10592
10593 ;;; @ end
10594 ;;;
10595
10596 (defun gnus-summary-setup-default-charset ()
10597   "Setup newsgroup default charset."
10598   (if (equal gnus-newsgroup-name "nndraft:drafts")
10599       (setq gnus-newsgroup-charset nil)
10600     (let* ((ignored-charsets
10601             (or gnus-newsgroup-ephemeral-ignored-charsets
10602                 (append
10603                  (and gnus-newsgroup-name
10604                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10605                  gnus-newsgroup-ignored-charsets))))
10606       (setq gnus-newsgroup-charset
10607             (or gnus-newsgroup-ephemeral-charset
10608                 (and gnus-newsgroup-name
10609                      (gnus-parameter-charset gnus-newsgroup-name))
10610                 gnus-default-charset))
10611       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10612            ignored-charsets))))
10613
10614 ;;;
10615 ;;; Mime Commands
10616 ;;;
10617
10618 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10619   "Display the current article buffer fully MIME-buttonized.
10620 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10621 treated as multipart/mixed."
10622   (interactive "P")
10623   (require 'gnus-art)
10624   (let ((gnus-unbuttonized-mime-types nil)
10625         (gnus-mime-display-multipart-as-mixed show-all-parts))
10626     (gnus-summary-show-article)))
10627
10628 (defun gnus-summary-repair-multipart (article)
10629   "Add a Content-Type header to a multipart article without one."
10630   (interactive (list (gnus-summary-article-number)))
10631   (gnus-with-article article
10632     (message-narrow-to-head)
10633     (message-remove-header "Mime-Version")
10634     (goto-char (point-max))
10635     (insert "Mime-Version: 1.0\n")
10636     (widen)
10637     (when (search-forward "\n--" nil t)
10638       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10639         (message-narrow-to-head)
10640         (message-remove-header "Content-Type")
10641         (goto-char (point-max))
10642         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10643                         separator))
10644         (widen))))
10645   (let (gnus-mark-article-hook)
10646     (gnus-summary-select-article t t nil article)))
10647
10648 (defun gnus-summary-toggle-display-buttonized ()
10649   "Toggle the buttonizing of the article buffer."
10650   (interactive)
10651   (require 'gnus-art)
10652   (if (setq gnus-inhibit-mime-unbuttonizing
10653             (not gnus-inhibit-mime-unbuttonizing))
10654       (let ((gnus-unbuttonized-mime-types nil))
10655         (gnus-summary-show-article))
10656     (gnus-summary-show-article)))
10657
10658 ;;;
10659 ;;; Intelli-mouse commmands
10660 ;;;
10661
10662 (defun gnus-wheel-summary-scroll (event)
10663   (interactive "e")
10664   (let ((amount (if (memq 'shift (event-modifiers event))
10665                     (car gnus-wheel-scroll-amount)
10666                   (cdr gnus-wheel-scroll-amount)))
10667         (direction (- (* (static-if (featurep 'xemacs)
10668                              (event-button event)
10669                            (cond ((eq 'mouse-4 (event-basic-type event))
10670                                   4)
10671                                  ((eq 'mouse-5 (event-basic-type event))
10672                                   5)))
10673                          2) 9))
10674         edge)
10675     (gnus-summary-scroll-up (* amount direction))
10676     (when (gnus-eval-in-buffer-window gnus-article-buffer
10677             (save-restriction
10678               (widen)
10679               (and (if (< 0 direction)
10680                        (gnus-article-next-page 0)
10681                      (gnus-article-prev-page 0)
10682                      (bobp))
10683                    (if (setq edge (get-text-property
10684                                    (point-min) 'gnus-wheel-edge))
10685                        (setq edge (* edge direction))
10686                      (setq edge -1))
10687                    (or (plusp edge)
10688                        (let ((buffer-read-only nil)
10689                              (inhibit-read-only t))
10690                          (put-text-property (point-min) (point-max)
10691                                             'gnus-wheel-edge direction)
10692                          nil))
10693                    (or (> edge gnus-wheel-edge-resistance)
10694                        (let ((buffer-read-only nil)
10695                              (inhibit-read-only t))
10696                          (put-text-property (point-min) (point-max)
10697                                             'gnus-wheel-edge
10698                                             (* (1+ edge) direction))
10699                          nil))
10700                    (eq last-command 'gnus-wheel-summary-scroll))))
10701       (gnus-summary-next-article nil nil (minusp direction)))))
10702
10703 (defun gnus-wheel-install ()
10704   "Enable mouse wheel support on summary window."
10705   (when gnus-use-wheel
10706     (let ((keys
10707            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10708       (dolist (key keys)
10709         (define-key gnus-summary-mode-map key
10710           'gnus-wheel-summary-scroll)))))
10711
10712 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10713
10714 ;;;
10715 ;;; Traditional PGP commmands
10716 ;;;
10717
10718 (defun gnus-summary-decrypt-article (&optional force)
10719   "Decrypt the current article in traditional PGP way.
10720 This will have permanent effect only in mail groups.
10721 If FORCE is non-nil, allow editing of articles even in read-only
10722 groups."
10723   (interactive "P")
10724   (gnus-summary-select-article t)
10725   (gnus-eval-in-buffer-window gnus-article-buffer
10726     (save-excursion
10727       (save-restriction
10728         (widen)
10729         (goto-char (point-min))
10730         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10731           (error "Not a traditional PGP message!"))
10732         (let ((armor-start (match-beginning 0)))
10733           (if (and (pgg-decrypt-region armor-start (point-max))
10734                    (or force (not (gnus-group-read-only-p))))
10735               (let ((inhibit-read-only t)
10736                     buffer-read-only)
10737                 (delete-region armor-start
10738                                (progn
10739                                  (re-search-forward "^-+END PGP" nil t)
10740                                  (beginning-of-line 2)
10741                                  (point)))
10742                 (insert-buffer-substring pgg-output-buffer))))))))
10743
10744 (defun gnus-summary-verify-article ()
10745   "Verify the current article in traditional PGP way."
10746   (interactive)
10747   (save-excursion
10748     (set-buffer gnus-original-article-buffer)
10749     (goto-char (point-min))
10750     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10751       (error "Not a traditional PGP message!"))
10752     (re-search-forward "^-+END PGP" nil t)
10753     (beginning-of-line 2)
10754     (call-interactively (function pgg-verify-region))))
10755
10756 ;;;
10757 ;;; Generic summary marking commands
10758 ;;;
10759
10760 (defvar gnus-summary-marking-alist
10761   '((read gnus-del-mark "d")
10762     (unread gnus-unread-mark "u")
10763     (ticked gnus-ticked-mark "!")
10764     (dormant gnus-dormant-mark "?")
10765     (expirable gnus-expirable-mark "e"))
10766   "An alist of names/marks/keystrokes.")
10767
10768 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10769 (defvar gnus-summary-mark-map)
10770
10771 (defun gnus-summary-make-all-marking-commands ()
10772   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10773   (dolist (elem gnus-summary-marking-alist)
10774     (apply 'gnus-summary-make-marking-command elem)))
10775
10776 (defun gnus-summary-make-marking-command (name mark keystroke)
10777   (let ((map (make-sparse-keymap)))
10778     (define-key gnus-summary-generic-mark-map keystroke map)
10779     (dolist (lway `((next "next" next nil "n")
10780                     (next-unread "next unread" next t "N")
10781                     (prev "previous" prev nil "p")
10782                     (prev-unread "previous unread" prev t "P")
10783                     (nomove "" nil nil ,keystroke)))
10784       (let ((func (gnus-summary-make-marking-command-1
10785                    mark (car lway) lway name)))
10786         (setq func (eval func))
10787         (define-key map (nth 4 lway) func)))))
10788
10789 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10790   `(defun ,(intern
10791             (format "gnus-summary-put-mark-as-%s%s"
10792                     name (if (eq way 'nomove)
10793                              ""
10794                            (concat "-" (symbol-name way)))))
10795      (n)
10796      ,(format
10797        "Mark the current article as %s%s.
10798 If N, the prefix, then repeat N times.
10799 If N is negative, move in reverse order.
10800 The difference between N and the actual number of articles marked is
10801 returned."
10802        name (car (cdr lway)))
10803      (interactive "p")
10804      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10805
10806 (defun gnus-summary-generic-mark (n mark move unread)
10807   "Mark N articles with MARK."
10808   (unless (eq major-mode 'gnus-summary-mode)
10809     (error "This command can only be used in the summary buffer"))
10810   (gnus-summary-show-thread)
10811   (let ((nummove
10812          (cond
10813           ((eq move 'next) 1)
10814           ((eq move 'prev) -1)
10815           (t 0))))
10816     (if (zerop nummove)
10817         (setq n 1)
10818       (when (< n 0)
10819         (setq n (abs n)
10820               nummove (* -1 nummove))))
10821     (while (and (> n 0)
10822                 (gnus-summary-mark-article nil mark)
10823                 (zerop (gnus-summary-next-subject nummove unread t)))
10824       (setq n (1- n)))
10825     (when (/= 0 n)
10826       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10827     (gnus-summary-recenter)
10828     (gnus-summary-position-point)
10829     (gnus-set-mode-line 'summary)
10830     n))
10831
10832 (defun gnus-summary-insert-articles (articles)
10833   (when (setq articles
10834               (gnus-set-difference articles
10835                                    (mapcar (lambda (h) (mail-header-number h))
10836                                            gnus-newsgroup-headers)))
10837     (setq gnus-newsgroup-headers
10838           (merge 'list
10839                  gnus-newsgroup-headers
10840                  (gnus-fetch-headers articles)
10841                  'gnus-article-sort-by-number))
10842     ;; Suppress duplicates?
10843     (when gnus-suppress-duplicates
10844       (gnus-dup-suppress-articles))
10845
10846     ;; We might want to build some more threads first.
10847     (when (and gnus-fetch-old-headers
10848                (eq gnus-headers-retrieved-by 'nov))
10849       (if (eq gnus-fetch-old-headers 'invisible)
10850           (gnus-build-all-threads)
10851         (gnus-build-old-threads)))
10852     ;; Let the Gnus agent mark articles as read.
10853     (when gnus-agent
10854       (gnus-agent-get-undownloaded-list))
10855     ;; Remove list identifiers from subject
10856     (when gnus-list-identifiers
10857       (gnus-summary-remove-list-identifiers))
10858     ;; First and last article in this newsgroup.
10859     (when gnus-newsgroup-headers
10860       (setq gnus-newsgroup-begin
10861             (mail-header-number (car gnus-newsgroup-headers))
10862             gnus-newsgroup-end
10863             (mail-header-number
10864              (gnus-last-element gnus-newsgroup-headers))))
10865     (when gnus-use-scoring
10866       (gnus-possibly-score-headers))))
10867
10868 (defun gnus-summary-insert-old-articles (&optional all)
10869   "Insert all old articles in this group.
10870 If ALL is non-nil, already read articles become readable.
10871 If ALL is a number, fetch this number of articles."
10872   (interactive "P")
10873   (prog1
10874       (let ((old (mapcar 'car gnus-newsgroup-data))
10875             (i (car gnus-newsgroup-active))
10876             older len)
10877         (while (<= i (cdr gnus-newsgroup-active))
10878           (or (memq i old) (push i older))
10879           (incf i))
10880         (setq len (length older))
10881         (cond
10882          ((null older) nil)
10883          ((numberp all)
10884           (if (< all len)
10885               (setq older (subseq older 0 all))))
10886          (all nil)
10887          (t
10888           (if (and (numberp gnus-large-newsgroup)
10889                    (> len gnus-large-newsgroup))
10890               (let ((input
10891                      (read-string
10892                       (format
10893                        "How many articles from %s (default %d): "
10894                        (gnus-limit-string
10895                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10896                        len))))
10897                 (unless (string-match "^[ \t]*$" input)
10898                   (setq all (string-to-number input))
10899                   (if (< all len)
10900                       (setq older (subseq older 0 all))))))))
10901         (if (not older)
10902             (message "No old news.")
10903           (gnus-summary-insert-articles older)
10904           (gnus-summary-limit (gnus-union older old))))
10905     (gnus-summary-position-point)))
10906
10907 (defun gnus-summary-insert-new-articles ()
10908   "Insert all new articles in this group."
10909   (interactive)
10910   (prog1
10911       (let ((old (mapcar 'car gnus-newsgroup-data))
10912             (old-active gnus-newsgroup-active)
10913             (nnmail-fetched-sources (list t))
10914             i new)
10915         (setq gnus-newsgroup-active
10916               (gnus-activate-group gnus-newsgroup-name 'scan))
10917         (setq i (1+ (cdr old-active)))
10918         (while (<= i (cdr gnus-newsgroup-active))
10919           (push i new)
10920           (incf i))
10921         (if (not new)
10922             (message "No gnus is bad news.")
10923           (setq new (nreverse new))
10924           (gnus-summary-insert-articles new)
10925           (setq gnus-newsgroup-unreads
10926                 (append gnus-newsgroup-unreads new))
10927           (gnus-summary-limit (gnus-union old new))))
10928     (gnus-summary-position-point)))
10929
10930 (gnus-summary-make-all-marking-commands)
10931
10932 (gnus-ems-redefine)
10933
10934 (provide 'gnus-sum)
10935
10936 (run-hooks 'gnus-sum-load-hook)
10937
10938 ;;; gnus-sum.el ends here