Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 If threads are hidden, you have to run the command
252 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
253 to expose hidden threads."
254   :group 'gnus-thread
255   :type 'boolean)
256
257 (defcustom gnus-thread-hide-killed t
258   "*If non-nil, hide killed threads automatically."
259   :group 'gnus-thread
260   :type 'boolean)
261
262 (defcustom gnus-thread-ignore-subject t
263   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
264 If nil, articles that have different subjects from their parents will
265 start separate threads."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-operation-ignore-subject t
270   "*If non-nil, subjects will be ignored when doing thread commands.
271 This affects commands like `gnus-summary-kill-thread' and
272 `gnus-summary-lower-thread'.
273
274 If this variable is nil, articles in the same thread with different
275 subjects will not be included in the operation in question.  If this
276 variable is `fuzzy', only articles that have subjects that are fuzzily
277 equal will be included."
278   :group 'gnus-thread
279   :type '(choice (const :tag "off" nil)
280                  (const fuzzy)
281                  (sexp :tag "on" t)))
282
283 (defcustom gnus-thread-indent-level 4
284   "*Number that says how much each sub-thread should be indented."
285   :group 'gnus-thread
286   :type 'integer)
287
288 (defcustom gnus-auto-extend-newsgroup t
289   "*If non-nil, extend newsgroup forward and backward when requested."
290   :group 'gnus-summary-choose
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-first t
294   "*If nil, don't select the first unread article when entering a group.
295 If this variable is `best', select the highest-scored unread article
296 in the group.  If t, select the first unread article.
297
298 This variable can also be a function to place point on a likely
299 subject line.  Useful values include `gnus-summary-first-unread-subject',
300 `gnus-summary-first-unread-article' and
301 `gnus-summary-best-unread-article'.
302
303 If you want to prevent automatic selection of the first unread article
304 in some newsgroups, set the variable to nil in
305 `gnus-select-group-hook'."
306   :group 'gnus-group-select
307   :type '(choice (const :tag "none" nil)
308                  (const best)
309                  (sexp :menu-tag "first" t)
310                  (function-item gnus-summary-first-unread-subject)
311                  (function-item gnus-summary-first-unread-article)
312                  (function-item gnus-summary-best-unread-article)))
313
314 (defcustom gnus-dont-select-after-jump-to-other-group nil
315   "If non-nil, don't select the first unread article after entering the
316 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
317 it is depend on the value of `gnus-auto-select-first' whether to select
318 or not."
319   :group 'gnus-group-select
320   :type 'boolean)
321
322 (defcustom gnus-auto-select-next t
323   "*If non-nil, offer to go to the next group from the end of the previous.
324 If the value is t and the next newsgroup is empty, Gnus will exit
325 summary mode and go back to group mode.  If the value is neither nil
326 nor t, Gnus will select the following unread newsgroup.  In
327 particular, if the value is the symbol `quietly', the next unread
328 newsgroup will be selected without any confirmation, and if it is
329 `almost-quietly', the next group will be selected without any
330 confirmation if you are located on the last article in the group.
331 Finally, if this variable is `slightly-quietly', the `Z n' command
332 will go to the next group without confirmation."
333   :group 'gnus-summary-maneuvering
334   :type '(choice (const :tag "off" nil)
335                  (const quietly)
336                  (const almost-quietly)
337                  (const slightly-quietly)
338                  (sexp :menu-tag "on" t)))
339
340 (defcustom gnus-auto-select-same nil
341   "*If non-nil, select the next article with the same subject.
342 If there are no more articles with the same subject, go to
343 the first unread article."
344   :group 'gnus-summary-maneuvering
345   :type 'boolean)
346
347 (defcustom gnus-summary-check-current nil
348   "*If non-nil, consider the current article when moving.
349 The \"unread\" movement commands will stay on the same line if the
350 current article is unread."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-center-summary t
355   "*If non-nil, always center the current summary buffer.
356 In particular, if `vertical' do only vertical recentering.  If non-nil
357 and non-`vertical', do both horizontal and vertical recentering."
358   :group 'gnus-summary-maneuvering
359   :type '(choice (const :tag "none" nil)
360                  (const vertical)
361                  (integer :tag "height")
362                  (sexp :menu-tag "both" t)))
363
364 (defcustom gnus-show-all-headers nil
365   "*If non-nil, don't hide any headers."
366   :group 'gnus-article-hiding
367   :group 'gnus-article-headers
368   :type 'boolean)
369
370 (defcustom gnus-summary-ignore-duplicates nil
371   "*If non-nil, ignore articles with identical Message-ID headers."
372   :group 'gnus-summary
373   :type 'boolean)
374
375 (defcustom gnus-single-article-buffer t
376   "*If non-nil, display all articles in the same buffer.
377 If nil, each group will get its own article buffer."
378   :group 'gnus-article-various
379   :type 'boolean)
380
381 (defcustom gnus-break-pages t
382   "*If non-nil, do page breaking on articles.
383 The page delimiter is specified by the `gnus-page-delimiter'
384 variable."
385   :group 'gnus-article-various
386   :type 'boolean)
387
388 (defcustom gnus-show-mime t
389   "*If non-nil, do mime processing of articles.
390 The articles will simply be fed to the function given by
391 `gnus-article-display-method-for-mime'."
392   :group 'gnus-article-mime
393   :type 'boolean)
394
395 (defcustom gnus-move-split-methods nil
396   "*Variable used to suggest where articles are to be moved to.
397 It uses the same syntax as the `gnus-split-methods' variable.
398 However, whereas `gnus-split-methods' specifies file names as targets,
399 this variable specifies group names."
400   :group 'gnus-summary-mail
401   :type '(repeat (choice (list :value (fun) function)
402                          (cons :value ("" "") regexp (repeat string))
403                          (sexp :value nil))))
404
405 (defcustom gnus-unread-mark ?\ ;;;Whitespace
406   "*Mark used for unread articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-ticked-mark ?!
411   "*Mark used for ticked articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-dormant-mark ??
416   "*Mark used for dormant articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-del-mark ?r
421   "*Mark used for del'd articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-read-mark ?R
426   "*Mark used for read articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-expirable-mark ?E
431   "*Mark used for expirable articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-killed-mark ?K
436   "*Mark used for killed articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-souped-mark ?F
441   "*Mark used for souped articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-kill-file-mark ?X
446   "*Mark used for articles killed by kill files."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-low-score-mark ?Y
451   "*Mark used for articles with a low score."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-catchup-mark ?C
456   "*Mark used for articles that are caught up."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-replied-mark ?A
461   "*Mark used for articles that have been replied to."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-forwarded-mark ?F
466   "*Mark used for articles that have been forwarded."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-recent-mark ?N
471   "*Mark used for articles that are recent."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-cached-mark ?*
476   "*Mark used for articles that are in the cache."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-saved-mark ?S
481   "*Mark used for articles that have been saved."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-unseen-mark ?.
486   "*Mark used for articles that haven't been seen."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-no-mark ?\ ;;;Whitespace
491   "*Mark used for articles that have no other secondary mark."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ancient-mark ?O
496   "*Mark used for ancient articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-sparse-mark ?Q
501   "*Mark used for sparsely reffed articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-canceled-mark ?G
506   "*Mark used for canceled articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-duplicate-mark ?M
511   "*Mark used for duplicate articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-undownloaded-mark ?@
516   "*Mark used for articles that weren't downloaded."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-downloadable-mark ?%
521   "*Mark used for articles that are to be downloaded."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-unsendable-mark ?=
526   "*Mark used for articles that won't be sent."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-score-over-mark ?+
531   "*Score mark used for articles with high scores."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-score-below-mark ?-
536   "*Score mark used for articles with low scores."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
541   "*There is no thread under the article."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-not-empty-thread-mark ?=
546   "*There is a thread under the article."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-view-pseudo-asynchronously nil
551   "*If non-nil, Gnus will view pseudo-articles asynchronously."
552   :group 'gnus-extract-view
553   :type 'boolean)
554
555 (defcustom gnus-auto-expirable-marks
556   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
557         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
558         gnus-souped-mark gnus-duplicate-mark)
559   "*The list of marks converted into expiration if a group is auto-expirable."
560   :version "21.1"
561   :group 'gnus-summary
562   :type '(repeat character))
563
564 (defcustom gnus-inhibit-user-auto-expire t
565   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
566   :version "21.1"
567   :group 'gnus-summary
568   :type 'boolean)
569
570 (defcustom gnus-view-pseudos nil
571   "*If `automatic', pseudo-articles will be viewed automatically.
572 If `not-confirm', pseudos will be viewed automatically, and the user
573 will not be asked to confirm the command."
574   :group 'gnus-extract-view
575   :type '(choice (const :tag "off" nil)
576                  (const automatic)
577                  (const not-confirm)))
578
579 (defcustom gnus-view-pseudos-separately t
580   "*If non-nil, one pseudo-article will be created for each file to be viewed.
581 If nil, all files that use the same viewing command will be given as a
582 list of parameters to that command."
583   :group 'gnus-extract-view
584   :type 'boolean)
585
586 (defcustom gnus-insert-pseudo-articles t
587   "*If non-nil, insert pseudo-articles when decoding articles."
588   :group 'gnus-extract-view
589   :type 'boolean)
590
591 (defcustom gnus-summary-dummy-line-format
592   "  %(:                          :%) %S\n"
593   "*The format specification for the dummy roots in the summary buffer.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions.
596
597 %S  The subject"
598   :group 'gnus-threading
599   :type 'string)
600
601 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
602   "*The format specification for the summary mode line.
603 It works along the same lines as a normal formatting string,
604 with some simple extensions:
605
606 %G  Group name
607 %p  Unprefixed group name
608 %A  Current article number
609 %z  Current article score
610 %V  Gnus version
611 %U  Number of unread articles in the group
612 %e  Number of unselected articles in the group
613 %Z  A string with unread/unselected article counts
614 %g  Shortish group name
615 %S  Subject of the current article
616 %u  User-defined spec
617 %s  Current score file name
618 %d  Number of dormant articles
619 %r  Number of articles that have been marked as read in this session
620 %E  Number of articles expunged by the score files"
621   :group 'gnus-summary-format
622   :type 'string)
623
624 (defcustom gnus-list-identifiers nil
625   "Regexp that matches list identifiers to be removed from subject.
626 This can also be a list of regexps."
627   :version "21.1"
628   :group 'gnus-summary-format
629   :group 'gnus-article-hiding
630   :type '(choice (const :tag "none" nil)
631                  (regexp :value ".*")
632                  (repeat :value (".*") regexp)))
633
634 (defcustom gnus-summary-mark-below 0
635   "*Mark all articles with a score below this variable as read.
636 This variable is local to each summary buffer and usually set by the
637 score file."
638   :group 'gnus-score-default
639   :type 'integer)
640
641 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
642   "*List of functions used for sorting articles in the summary buffer.
643
644 Each function takes two articles and returns non-nil if the first
645 article should be sorted before the other.  If you use more than one
646 function, the primary sort function should be the last.  You should
647 probably always include `gnus-article-sort-by-number' in the list of
648 sorting functions -- preferably first.  Also note that sorting by date
649 is often much slower than sorting by number, and the sorting order is
650 very similar.  (Sorting by date means sorting by the time the message
651 was sent, sorting by number means sorting by arrival time.)
652
653 Ready-made functions include `gnus-article-sort-by-number',
654 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
655 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
656
657 When threading is turned on, the variable `gnus-thread-sort-functions'
658 controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-article-sort-by-number)
661                          (function-item gnus-article-sort-by-author)
662                          (function-item gnus-article-sort-by-subject)
663                          (function-item gnus-article-sort-by-date)
664                          (function-item gnus-article-sort-by-score)
665                          (function :tag "other"))))
666
667 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
668   "*List of functions used for sorting threads in the summary buffer.
669 By default, threads are sorted by article number.
670
671 Each function takes two threads and returns non-nil if the first
672 thread should be sorted before the other.  If you use more than one
673 function, the primary sort function should be the last.  You should
674 probably always include `gnus-thread-sort-by-number' in the list of
675 sorting functions -- preferably first.  Also note that sorting by date
676 is often much slower than sorting by number, and the sorting order is
677 very similar.  (Sorting by date means sorting by the time the message
678 was sent, sorting by number means sorting by arrival time.)
679
680 Ready-made functions include `gnus-thread-sort-by-number',
681 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
682 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
683 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
684
685 When threading is turned off, the variable
686 `gnus-article-sort-functions' controls how articles are sorted."
687   :group 'gnus-summary-sort
688   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
689                          (function-item gnus-thread-sort-by-author)
690                          (function-item gnus-thread-sort-by-subject)
691                          (function-item gnus-thread-sort-by-date)
692                          (function-item gnus-thread-sort-by-score)
693                          (function-item gnus-thread-sort-by-total-score)
694                          (function :tag "other"))))
695
696 (defcustom gnus-thread-score-function '+
697   "*Function used for calculating the total score of a thread.
698
699 The function is called with the scores of the article and each
700 subthread and should then return the score of the thread.
701
702 Some functions you can use are `+', `max', or `min'."
703   :group 'gnus-summary-sort
704   :type 'function)
705
706 (defcustom gnus-summary-expunge-below nil
707   "All articles that have a score less than this variable will be expunged.
708 This variable is local to the summary buffers."
709   :group 'gnus-score-default
710   :type '(choice (const :tag "off" nil)
711                  integer))
712
713 (defcustom gnus-thread-expunge-below nil
714   "All threads that have a total score less than this variable will be expunged.
715 See `gnus-thread-score-function' for en explanation of what a
716 \"thread score\" is.
717
718 This variable is local to the summary buffers."
719   :group 'gnus-threading
720   :group 'gnus-score-default
721   :type '(choice (const :tag "off" nil)
722                  integer))
723
724 (defcustom gnus-summary-mode-hook nil
725   "*A hook for Gnus summary mode.
726 This hook is run before any variables are set in the summary buffer."
727   :options '(turn-on-gnus-mailing-list-mode)
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
732 (when (featurep 'xemacs)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
734   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
735   (add-hook 'gnus-summary-mode-hook
736             'gnus-xmas-switch-horizontal-scrollbar-off))
737
738 (defcustom gnus-summary-menu-hook nil
739   "*Hook run after the creation of the summary mode menu."
740   :group 'gnus-summary-visual
741   :type 'hook)
742
743 (defcustom gnus-summary-exit-hook nil
744   "*A hook called on exit from the summary buffer.
745 It will be called with point in the group buffer."
746   :group 'gnus-summary-exit
747   :type 'hook)
748
749 (defcustom gnus-summary-prepare-hook nil
750   "*A hook called after the summary buffer has been generated.
751 If you want to modify the summary buffer, you can use this hook."
752   :group 'gnus-summary-various
753   :type 'hook)
754
755 (defcustom gnus-summary-prepared-hook nil
756   "*A hook called as the last thing after the summary buffer has been generated."
757   :group 'gnus-summary-various
758   :type 'hook)
759
760 (defcustom gnus-summary-generate-hook nil
761   "*A hook run just before generating the summary buffer.
762 This hook is commonly used to customize threading variables and the
763 like."
764   :group 'gnus-summary-various
765   :type 'hook)
766
767 (defcustom gnus-select-group-hook nil
768   "*A hook called when a newsgroup is selected.
769
770 If you'd like to simplify subjects like the
771 `gnus-summary-next-same-subject' command does, you can use the
772 following hook:
773
774  (setq gnus-select-group-hook
775       (list
776         (lambda ()
777           (mapcar (lambda (header)
778                      (mail-header-set-subject
779                       header
780                       (gnus-simplify-subject
781                        (mail-header-subject header) 're-only)))
782                   gnus-newsgroup-headers))))"
783   :group 'gnus-group-select
784   :type 'hook)
785
786 (defcustom gnus-select-article-hook nil
787   "*A hook called when an article is selected."
788   :group 'gnus-summary-choose
789   :type 'hook)
790
791 (defcustom gnus-visual-mark-article-hook
792   (list 'gnus-highlight-selected-summary)
793   "*Hook run after selecting an article in the summary buffer.
794 It is meant to be used for highlighting the article in some way.  It
795 is not run if `gnus-visual' is nil."
796   :group 'gnus-summary-visual
797   :type 'hook)
798
799 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
800   "*A hook called before parsing the headers."
801   :group 'gnus-various
802   :type 'hook)
803
804 (defcustom gnus-exit-group-hook nil
805   "*A hook called when exiting summary mode.
806 This hook is not called from the non-updating exit commands like `Q'."
807   :group 'gnus-various
808   :type 'hook)
809
810 (defcustom gnus-summary-update-hook
811   (list 'gnus-summary-highlight-line)
812   "*A hook called when a summary line is changed.
813 The hook will not be called if `gnus-visual' is nil.
814
815 The default function `gnus-summary-highlight-line' will
816 highlight the line according to the `gnus-summary-highlight'
817 variable."
818   :group 'gnus-summary-visual
819   :type 'hook)
820
821 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
822   "*A hook called when an article is selected for the first time.
823 The hook is intended to mark an article as read (or unread)
824 automatically when it is selected."
825   :group 'gnus-summary-choose
826   :type 'hook)
827
828 (defcustom gnus-group-no-more-groups-hook nil
829   "*A hook run when returning to group mode having no more (unread) groups."
830   :group 'gnus-group-select
831   :type 'hook)
832
833 (defcustom gnus-ps-print-hook nil
834   "*A hook run before ps-printing something from Gnus."
835   :group 'gnus-summary
836   :type 'hook)
837
838 (defcustom gnus-summary-display-arrow
839   (and (fboundp 'display-graphic-p)
840        (display-graphic-p))
841   "*If non-nil, display an arrow highlighting the current article."
842   :version "21.1"
843   :group 'gnus-summary
844   :type 'boolean)
845
846 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
847   "Face used for highlighting the current article in the summary buffer."
848   :group 'gnus-summary-visual
849   :type 'face)
850
851 (defcustom gnus-summary-highlight
852   '(((= mark gnus-canceled-mark)
853      . gnus-summary-cancelled-face)
854     ((and (> score default-high)
855           (or (= mark gnus-dormant-mark)
856               (= mark gnus-ticked-mark)))
857      . gnus-summary-high-ticked-face)
858     ((and (< score default-low)
859           (or (= mark gnus-dormant-mark)
860               (= mark gnus-ticked-mark)))
861      . gnus-summary-low-ticked-face)
862     ((or (= mark gnus-dormant-mark)
863          (= mark gnus-ticked-mark))
864      . gnus-summary-normal-ticked-face)
865     ((and (> score default-high) (= mark gnus-ancient-mark))
866      . gnus-summary-high-ancient-face)
867     ((and (< score default-low) (= mark gnus-ancient-mark))
868      . gnus-summary-low-ancient-face)
869     ((= mark gnus-ancient-mark)
870      . gnus-summary-normal-ancient-face)
871     ((and (> score default-high) (= mark gnus-unread-mark))
872      . gnus-summary-high-unread-face)
873     ((and (< score default-low) (= mark gnus-unread-mark))
874      . gnus-summary-low-unread-face)
875     ((= mark gnus-unread-mark)
876      . gnus-summary-normal-unread-face)
877     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
878                                                   gnus-undownloaded-mark)))
879      . gnus-summary-high-unread-face)
880     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
881                                                  gnus-undownloaded-mark)))
882      . gnus-summary-low-unread-face)
883     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
884           (memq article gnus-newsgroup-unreads))
885      . gnus-summary-normal-unread-face)
886     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
887      . gnus-summary-normal-read-face)
888     ((> score default-high)
889      . gnus-summary-high-read-face)
890     ((< score default-low)
891      . gnus-summary-low-read-face)
892     (t
893      . gnus-summary-normal-read-face))
894   "*Controls the highlighting of summary buffer lines.
895
896 A list of (FORM . FACE) pairs.  When deciding how a a particular
897 summary line should be displayed, each form is evaluated.  The content
898 of the face field after the first true form is used.  You can change
899 how those summary lines are displayed, by editing the face field.
900
901 You can use the following variables in the FORM field.
902
903 score:        The article's score
904 default:      The default article score.
905 default-high: The default score for high scored articles.
906 default-low:  The default score for low scored articles.
907 below:        The score below which articles are automatically marked as read.
908 mark:         The articles mark."
909   :group 'gnus-summary-visual
910   :type '(repeat (cons (sexp :tag "Form" nil)
911                        face)))
912
913 (defcustom gnus-alter-header-function nil
914   "Function called to allow alteration of article header structures.
915 The function is called with one parameter, the article header vector,
916 which it may alter in any way.")
917
918 (defvar gnus-decode-encoded-word-function
919   (mime-find-field-decoder 'From 'nov)
920   "Variable that says which function should be used to decode a string with encoded words.")
921
922 (defcustom gnus-extra-headers nil
923   "*Extra headers to parse."
924   :version "21.1"
925   :group 'gnus-summary
926   :type '(repeat symbol))
927
928 (defcustom gnus-ignored-from-addresses
929   (and user-mail-address (regexp-quote user-mail-address))
930   "*Regexp of From headers that may be suppressed in favor of To headers."
931   :version "21.1"
932   :group 'gnus-summary
933   :type 'regexp)
934
935 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
936   "List of charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939   :version "21.1"
940   :type '(repeat symbol)
941   :group 'gnus-charset)
942
943 (gnus-define-group-parameter
944  ignored-charsets
945  :type list
946  :function-document
947  "Return the ignored charsets of GROUP."
948  :variable gnus-group-ignored-charsets-alist
949  :variable-default
950  '(("alt\\.chinese\\.text" iso-8859-1))
951  :variable-document
952  "Alist of regexps (to match group names) and charsets that should be ignored.
953 When these charsets are used in the \"charset\" parameter, the
954 default charset will be used instead."
955  :variable-group gnus-charset
956  :variable-type '(repeat (cons (regexp :tag "Group")
957                                (repeat symbol)))
958  :parameter-type '(choice :tag "Ignored charsets"
959                           :value nil
960                           (repeat (symbol)))
961  :parameter-document       "\
962 List of charsets that should be ignored.
963
964 When these charsets are used in the \"charset\" parameter, the
965 default charset will be used instead.")
966
967 (defcustom gnus-group-highlight-words-alist nil
968   "Alist of group regexps and highlight regexps.
969 This variable uses the same syntax as `gnus-emphasis-alist'."
970   :version "21.1"
971   :type '(repeat (cons (regexp :tag "Group")
972                        (repeat (list (regexp :tag "Highlight regexp")
973                                      (number :tag "Group for entire word" 0)
974                                      (number :tag "Group for displayed part" 0)
975                                      (symbol :tag "Face"
976                                              gnus-emphasis-highlight-words)))))
977   :group 'gnus-summary-visual)
978
979 (defcustom gnus-use-wheel nil
980   "Use Intelli-mouse on summary movement"
981   :type 'boolean
982   :group 'gnus-summary-maneuvering)
983
984 (defcustom gnus-wheel-scroll-amount '(5 . 1)
985   "Amount to scroll messages by spinning the mouse wheel.
986 This is actually a cons cell, where the first item is the amount to scroll
987 on a normal wheel event, and the second is the amount to scroll when the
988 wheel is moved with the shift key depressed."
989   :type '(cons (integer :tag "Shift") integer)
990   :group 'gnus-summary-maneuvering)
991
992 (defcustom gnus-wheel-edge-resistance 2
993   "How hard it should be to change the current article
994 by moving the mouse over the edge of the article window."
995   :type 'integer
996   :group 'gnus-summary-maneuvering)
997
998 (defcustom gnus-summary-show-article-charset-alist
999   nil
1000   "Alist of number and charset.
1001 The article will be shown with the charset corresponding to the
1002 numbered argument.
1003 For example: ((1 . cn-gb-2312) (2 . big5))."
1004   :version "21.1"
1005   :type '(repeat (cons (number :tag "Argument" 1)
1006                        (symbol :tag "Charset")))
1007   :group 'gnus-charset)
1008
1009 (defcustom gnus-preserve-marks t
1010   "Whether marks are preserved when moving, copying and respooling messages."
1011   :version "21.1"
1012   :type 'boolean
1013   :group 'gnus-summary-marks)
1014
1015 (defcustom gnus-alter-articles-to-read-function nil
1016   "Function to be called to alter the list of articles to be selected."
1017   :type '(choice (const nil) function)
1018   :group 'gnus-summary)
1019
1020 (defcustom gnus-orphan-score nil
1021   "*All orphans get this score added.  Set in the score file."
1022   :group 'gnus-score-default
1023   :type '(choice (const nil)
1024                  integer))
1025
1026 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1027   "*A regexp to match MIME parts when saving multiple parts of a message
1028 with gnus-summary-save-parts (X m). This regexp will be used by default
1029 when prompting the user for which type of files to save."
1030   :group 'gnus-summary
1031   :type 'regexp)
1032
1033
1034 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1035   "*A regexp to match MIME parts when saving multiple parts of a message
1036 with gnus-summary-save-parts (X m). This regexp will be used by default
1037 when prompting the user for which type of files to save."
1038   :group 'gnus-summary
1039   :type 'regexp)
1040
1041 (defcustom gnus-read-all-available-headers nil
1042   "Whether Gnus should parse all headers made available to it.
1043 This is mostly relevant for slow backends where the user may
1044 wish to widen the summary buffer to include all headers
1045 that were fetched.  Say, for nnultimate groups."
1046   :group 'gnus-summary
1047   :type '(choice boolean regexp))
1048
1049 ;;; Internal variables
1050
1051 (defvar gnus-summary-display-cache nil)
1052 (defvar gnus-article-mime-handles nil)
1053 (defvar gnus-article-decoded-p nil)
1054 (defvar gnus-article-charset nil)
1055 (defvar gnus-article-ignored-charsets nil)
1056 (defvar gnus-scores-exclude-files nil)
1057 (defvar gnus-page-broken nil)
1058 (defvar gnus-inhibit-mime-unbuttonizing nil)
1059
1060 (defvar gnus-original-article nil)
1061 (defvar gnus-article-internal-prepare-hook nil)
1062 (defvar gnus-newsgroup-process-stack nil)
1063
1064 (defvar gnus-thread-indent-array nil)
1065 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1066 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1067   "Function called to sort the articles within a thread after it has been gathered together.")
1068
1069 (defvar gnus-summary-save-parts-type-history nil)
1070 (defvar gnus-summary-save-parts-last-directory nil)
1071
1072 (defvar gnus-summary-save-parts-type-history nil)
1073 (defvar gnus-summary-save-parts-last-directory nil)
1074
1075 ;; Avoid highlighting in kill files.
1076 (defvar gnus-summary-inhibit-highlight nil)
1077 (defvar gnus-newsgroup-selected-overlay nil)
1078 (defvar gnus-inhibit-limiting nil)
1079 (defvar gnus-newsgroup-adaptive-score-file nil)
1080 (defvar gnus-current-score-file nil)
1081 (defvar gnus-current-move-group nil)
1082 (defvar gnus-current-copy-group nil)
1083 (defvar gnus-current-crosspost-group nil)
1084 (defvar gnus-newsgroup-display nil)
1085
1086 (defvar gnus-newsgroup-dependencies nil)
1087 (defvar gnus-newsgroup-adaptive nil)
1088 (defvar gnus-summary-display-article-function nil)
1089 (defvar gnus-summary-highlight-line-function nil
1090   "Function called after highlighting a summary line.")
1091
1092 (defvar gnus-summary-line-format-alist
1093   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1094     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1095     (?s gnus-tmp-subject-or-nil ?s)
1096     (?n gnus-tmp-name ?s)
1097     (?A (std11-address-string
1098          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1099     (?a (or (std11-full-name-string
1100              (car (mime-entity-read-field gnus-tmp-header 'From)))
1101             gnus-tmp-from) ?s)
1102     (?F gnus-tmp-from ?s)
1103     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1104     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1105     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1106     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1107     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1108     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1109     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1110     (?L gnus-tmp-lines ?s)
1111     (?I gnus-tmp-indentation ?s)
1112     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1113     (?R gnus-tmp-replied ?c)
1114     (?\[ gnus-tmp-opening-bracket ?c)
1115     (?\] gnus-tmp-closing-bracket ?c)
1116     (?\> (make-string gnus-tmp-level ? ) ?s)
1117     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1118     (?i gnus-tmp-score ?d)
1119     (?z gnus-tmp-score-char ?c)
1120     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1121     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1122     (?U gnus-tmp-unread ?c)
1123     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1124     (?t (gnus-summary-number-of-articles-in-thread
1125          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1126         ?d)
1127     (?e (gnus-summary-number-of-articles-in-thread
1128          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1129         ?c)
1130     (?u gnus-tmp-user-defined ?s)
1131     (?P (gnus-pick-line-number) ?d)
1132     (?B gnus-tmp-thread-tree-header-string ?s))
1133   "An alist of format specifications that can appear in summary lines.
1134 These are paired with what variables they correspond with, along with
1135 the type of the variable (string, integer, character, etc).")
1136
1137 (defvar gnus-summary-dummy-line-format-alist
1138   `((?S gnus-tmp-subject ?s)
1139     (?N gnus-tmp-number ?d)
1140     (?u gnus-tmp-user-defined ?s)))
1141
1142 (defvar gnus-summary-mode-line-format-alist
1143   `((?G gnus-tmp-group-name ?s)
1144     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1145     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1146     (?A gnus-tmp-article-number ?d)
1147     (?Z gnus-tmp-unread-and-unselected ?s)
1148     (?V gnus-version ?s)
1149     (?U gnus-tmp-unread-and-unticked ?d)
1150     (?S gnus-tmp-subject ?s)
1151     (?e gnus-tmp-unselected ?d)
1152     (?u gnus-tmp-user-defined ?s)
1153     (?d (length gnus-newsgroup-dormant) ?d)
1154     (?t (length gnus-newsgroup-marked) ?d)
1155     (?r (length gnus-newsgroup-reads) ?d)
1156     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1157     (?E gnus-newsgroup-expunged-tally ?d)
1158     (?s (gnus-current-score-file-nondirectory) ?s)))
1159
1160 (defvar gnus-last-search-regexp nil
1161   "Default regexp for article search command.")
1162
1163 (defvar gnus-summary-search-article-matched-data nil
1164   "Last matched data of article search command.  It is the local variable
1165 in `gnus-article-buffer' which consists of the list of start position,
1166 end position and text.")
1167
1168 (defvar gnus-last-shell-command nil
1169   "Default shell command on article.")
1170
1171 (defvar gnus-newsgroup-begin nil)
1172 (defvar gnus-newsgroup-end nil)
1173 (defvar gnus-newsgroup-last-rmail nil)
1174 (defvar gnus-newsgroup-last-mail nil)
1175 (defvar gnus-newsgroup-last-folder nil)
1176 (defvar gnus-newsgroup-last-file nil)
1177 (defvar gnus-newsgroup-auto-expire nil)
1178 (defvar gnus-newsgroup-active nil)
1179
1180 (defvar gnus-newsgroup-data nil)
1181 (defvar gnus-newsgroup-data-reverse nil)
1182 (defvar gnus-newsgroup-limit nil)
1183 (defvar gnus-newsgroup-limits nil)
1184
1185 (defvar gnus-newsgroup-unreads nil
1186   "List of unread articles in the current newsgroup.")
1187
1188 (defvar gnus-newsgroup-unselected nil
1189   "List of unselected unread articles in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-reads nil
1192   "Alist of read articles and article marks in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-expunged-tally nil)
1195
1196 (defvar gnus-newsgroup-marked nil
1197   "List of ticked articles in the current newsgroup (a subset of unread art).")
1198
1199 (defvar gnus-newsgroup-killed nil
1200   "List of ranges of articles that have been through the scoring process.")
1201
1202 (defvar gnus-newsgroup-cached nil
1203   "List of articles that come from the article cache.")
1204
1205 (defvar gnus-newsgroup-saved nil
1206   "List of articles that have been saved.")
1207
1208 (defvar gnus-newsgroup-kill-headers nil)
1209
1210 (defvar gnus-newsgroup-replied nil
1211   "List of articles that have been replied to in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-forwarded nil
1214   "List of articles that have been forwarded in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-recent nil
1217   "List of articles that have are recent in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-expirable nil
1220   "List of articles in the current newsgroup that can be expired.")
1221
1222 (defvar gnus-newsgroup-processable nil
1223   "List of articles in the current newsgroup that can be processed.")
1224
1225 (defvar gnus-newsgroup-downloadable nil
1226   "List of articles in the current newsgroup that can be processed.")
1227
1228 (defvar gnus-newsgroup-undownloaded nil
1229   "List of articles in the current newsgroup that haven't been downloaded..")
1230
1231 (defvar gnus-newsgroup-unsendable nil
1232   "List of articles in the current newsgroup that won't be sent.")
1233
1234 (defvar gnus-newsgroup-bookmarks nil
1235   "List of articles in the current newsgroup that have bookmarks.")
1236
1237 (defvar gnus-newsgroup-dormant nil
1238   "List of dormant articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-unseen nil
1241   "List of unseen articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-seen nil
1244   "Range of seen articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-articles nil
1247   "List of articles in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-scored nil
1250   "List of scored articles in the current newsgroup.")
1251
1252 (defvar gnus-newsgroup-incorporated nil
1253   "List of incorporated articles in the current newsgroup.")
1254
1255 (defvar gnus-newsgroup-headers nil
1256   "List of article headers in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-threads nil)
1259
1260 (defvar gnus-newsgroup-prepared nil
1261   "Whether the current group has been prepared properly.")
1262
1263 (defvar gnus-newsgroup-ancient nil
1264   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-sparse nil)
1267
1268 (defvar gnus-current-article nil)
1269 (defvar gnus-article-current nil)
1270 (defvar gnus-current-headers nil)
1271 (defvar gnus-have-all-headers nil)
1272 (defvar gnus-last-article nil)
1273 (defvar gnus-newsgroup-history nil)
1274 (defvar gnus-newsgroup-charset nil)
1275 (defvar gnus-newsgroup-ephemeral-charset nil)
1276 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1277
1278 (defvar gnus-article-before-search nil)
1279
1280 (defconst gnus-summary-local-variables
1281   '(gnus-newsgroup-name
1282     gnus-newsgroup-begin gnus-newsgroup-end
1283     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1284     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1285     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1286     gnus-newsgroup-unselected gnus-newsgroup-marked
1287     gnus-newsgroup-reads gnus-newsgroup-saved
1288     gnus-newsgroup-replied gnus-newsgroup-forwarded
1289     gnus-newsgroup-recent
1290     gnus-newsgroup-expirable
1291     gnus-newsgroup-processable gnus-newsgroup-killed
1292     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1293     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1294     gnus-newsgroup-seen gnus-newsgroup-articles
1295     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1296     gnus-newsgroup-headers gnus-newsgroup-threads
1297     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1298     gnus-current-article gnus-current-headers gnus-have-all-headers
1299     gnus-last-article gnus-article-internal-prepare-hook
1300     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1301     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1302     gnus-thread-expunge-below
1303     gnus-score-alist gnus-current-score-file
1304     (gnus-summary-expunge-below . global)
1305     (gnus-summary-mark-below . global)
1306     (gnus-orphan-score . global)
1307     gnus-newsgroup-active gnus-scores-exclude-files
1308     gnus-newsgroup-history gnus-newsgroup-ancient
1309     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1310     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1311     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1312     (gnus-newsgroup-expunged-tally . 0)
1313     gnus-cache-removable-articles gnus-newsgroup-cached
1314     gnus-newsgroup-data gnus-newsgroup-data-reverse
1315     gnus-newsgroup-limit gnus-newsgroup-limits
1316     gnus-newsgroup-charset gnus-newsgroup-display
1317     gnus-newsgroup-incorporated)
1318   "Variables that are buffer-local to the summary buffers.")
1319
1320 (defvar gnus-newsgroup-variables nil
1321   "Variables that have separate values in the newsgroups.")
1322
1323 ;; Byte-compiler warning.
1324 (eval-when-compile (defvar gnus-article-mode-map))
1325
1326 ;; Subject simplification.
1327
1328 (defun gnus-simplify-whitespace (str)
1329   "Remove excessive whitespace from STR."
1330   (let ((mystr str))
1331     ;; Multiple spaces.
1332     (while (string-match "[ \t][ \t]+" mystr)
1333       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1334                           " "
1335                           (substring mystr (match-end 0)))))
1336     ;; Leading spaces.
1337     (when (string-match "^[ \t]+" mystr)
1338       (setq mystr (substring mystr (match-end 0))))
1339     ;; Trailing spaces.
1340     (when (string-match "[ \t]+$" mystr)
1341       (setq mystr (substring mystr 0 (match-beginning 0))))
1342     mystr))
1343
1344 (defsubst gnus-simplify-subject-re (subject)
1345   "Remove \"Re:\" from subject lines."
1346   (if (string-match message-subject-re-regexp subject)
1347       (substring subject (match-end 0))
1348     subject))
1349
1350 (defun gnus-simplify-subject (subject &optional re-only)
1351   "Remove `Re:' and words in parentheses.
1352 If RE-ONLY is non-nil, strip leading `Re:'s only."
1353   (let ((case-fold-search t))           ;Ignore case.
1354     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1355     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1356       (setq subject (substring subject (match-end 0))))
1357     ;; Remove uninteresting prefixes.
1358     (when (and (not re-only)
1359                gnus-simplify-ignored-prefixes
1360                (string-match gnus-simplify-ignored-prefixes subject))
1361       (setq subject (substring subject (match-end 0))))
1362     ;; Remove words in parentheses from end.
1363     (unless re-only
1364       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1365         (setq subject (substring subject 0 (match-beginning 0)))))
1366     ;; Return subject string.
1367     subject))
1368
1369 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1370 ;; all whitespace.
1371 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1372   (goto-char (point-min))
1373   (while (re-search-forward regexp nil t)
1374     (replace-match (or newtext ""))))
1375
1376 (defun gnus-simplify-buffer-fuzzy ()
1377   "Simplify string in the buffer fuzzily.
1378 The string in the accessible portion of the current buffer is simplified.
1379 It is assumed to be a single-line subject.
1380 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1381 matter is removed.  Additional things can be deleted by setting
1382 `gnus-simplify-subject-fuzzy-regexp'."
1383   (let ((case-fold-search t)
1384         (modified-tick))
1385     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1386
1387     (while (not (eq modified-tick (buffer-modified-tick)))
1388       (setq modified-tick (buffer-modified-tick))
1389       (cond
1390        ((listp gnus-simplify-subject-fuzzy-regexp)
1391         (mapcar 'gnus-simplify-buffer-fuzzy-step
1392                 gnus-simplify-subject-fuzzy-regexp))
1393        (gnus-simplify-subject-fuzzy-regexp
1394         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1395       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1396       (gnus-simplify-buffer-fuzzy-step
1397        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1398       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1399
1400     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1401     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1402     (gnus-simplify-buffer-fuzzy-step " $")
1403     (gnus-simplify-buffer-fuzzy-step "^ +")))
1404
1405 (defun gnus-simplify-subject-fuzzy (subject)
1406   "Simplify a subject string fuzzily.
1407 See `gnus-simplify-buffer-fuzzy' for details."
1408   (save-excursion
1409     (gnus-set-work-buffer)
1410     (let ((case-fold-search t))
1411       ;; Remove uninteresting prefixes.
1412       (when (and gnus-simplify-ignored-prefixes
1413                  (string-match gnus-simplify-ignored-prefixes subject))
1414         (setq subject (substring subject (match-end 0))))
1415       (insert subject)
1416       (inline (gnus-simplify-buffer-fuzzy))
1417       (buffer-string))))
1418
1419 (defsubst gnus-simplify-subject-fully (subject)
1420   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1421   (cond
1422    (gnus-simplify-subject-functions
1423     (gnus-map-function gnus-simplify-subject-functions subject))
1424    ((null gnus-summary-gather-subject-limit)
1425     (gnus-simplify-subject-re subject))
1426    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1427     (gnus-simplify-subject-fuzzy subject))
1428    ((numberp gnus-summary-gather-subject-limit)
1429     (gnus-limit-string (gnus-simplify-subject-re subject)
1430                        gnus-summary-gather-subject-limit))
1431    (t
1432     subject)))
1433
1434 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1435   "Check whether two subjects are equal.
1436 If optional argument simple-first is t, first argument is already
1437 simplified."
1438   (cond
1439    ((null simple-first)
1440     (equal (gnus-simplify-subject-fully s1)
1441            (gnus-simplify-subject-fully s2)))
1442    (t
1443     (equal s1
1444            (gnus-simplify-subject-fully s2)))))
1445
1446 (defun gnus-summary-bubble-group ()
1447   "Increase the score of the current group.
1448 This is a handy function to add to `gnus-summary-exit-hook' to
1449 increase the score of each group you read."
1450   (gnus-group-add-score gnus-newsgroup-name))
1451
1452 \f
1453 ;;;
1454 ;;; Gnus summary mode
1455 ;;;
1456
1457 (put 'gnus-summary-mode 'mode-class 'special)
1458
1459 (defvar gnus-article-commands-menu)
1460
1461 (when t
1462   ;; Non-orthogonal keys
1463
1464   (gnus-define-keys gnus-summary-mode-map
1465     " " gnus-summary-next-page
1466     "\177" gnus-summary-prev-page
1467     [delete] gnus-summary-prev-page
1468     [backspace] gnus-summary-prev-page
1469     "\r" gnus-summary-scroll-up
1470     "\M-\r" gnus-summary-scroll-down
1471     "n" gnus-summary-next-unread-article
1472     "p" gnus-summary-prev-unread-article
1473     "N" gnus-summary-next-article
1474     "P" gnus-summary-prev-article
1475     "\M-\C-n" gnus-summary-next-same-subject
1476     "\M-\C-p" gnus-summary-prev-same-subject
1477     "\M-n" gnus-summary-next-unread-subject
1478     "\M-p" gnus-summary-prev-unread-subject
1479     "." gnus-summary-first-unread-article
1480     "," gnus-summary-best-unread-article
1481     "\M-s" gnus-summary-search-article-forward
1482     "\M-r" gnus-summary-search-article-backward
1483     "<" gnus-summary-beginning-of-article
1484     ">" gnus-summary-end-of-article
1485     "j" gnus-summary-goto-article
1486     "^" gnus-summary-refer-parent-article
1487     "\M-^" gnus-summary-refer-article
1488     "u" gnus-summary-tick-article-forward
1489     "!" gnus-summary-tick-article-forward
1490     "U" gnus-summary-tick-article-backward
1491     "d" gnus-summary-mark-as-read-forward
1492     "D" gnus-summary-mark-as-read-backward
1493     "E" gnus-summary-mark-as-expirable
1494     "\M-u" gnus-summary-clear-mark-forward
1495     "\M-U" gnus-summary-clear-mark-backward
1496     "k" gnus-summary-kill-same-subject-and-select
1497     "\C-k" gnus-summary-kill-same-subject
1498     "\M-\C-k" gnus-summary-kill-thread
1499     "\M-\C-l" gnus-summary-lower-thread
1500     "e" gnus-summary-edit-article
1501     "#" gnus-summary-mark-as-processable
1502     "\M-#" gnus-summary-unmark-as-processable
1503     "\M-\C-t" gnus-summary-toggle-threads
1504     "\M-\C-s" gnus-summary-show-thread
1505     "\M-\C-h" gnus-summary-hide-thread
1506     "\M-\C-f" gnus-summary-next-thread
1507     "\M-\C-b" gnus-summary-prev-thread
1508     [(meta down)] gnus-summary-next-thread
1509     [(meta up)] gnus-summary-prev-thread
1510     "\M-\C-u" gnus-summary-up-thread
1511     "\M-\C-d" gnus-summary-down-thread
1512     "&" gnus-summary-execute-command
1513     "c" gnus-summary-catchup-and-exit
1514     "\C-w" gnus-summary-mark-region-as-read
1515     "\C-t" gnus-summary-toggle-truncation
1516     "?" gnus-summary-mark-as-dormant
1517     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1518     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1519     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1520     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1521     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1522     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1523     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1524     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1525     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1526     "=" gnus-summary-expand-window
1527     "\C-x\C-s" gnus-summary-reselect-current-group
1528     "\M-g" gnus-summary-rescan-group
1529     "w" gnus-summary-stop-page-breaking
1530     "\C-c\C-r" gnus-summary-caesar-message
1531     "\M-t" gnus-summary-toggle-mime
1532     "f" gnus-summary-followup
1533     "F" gnus-summary-followup-with-original
1534     "C" gnus-summary-cancel-article
1535     "r" gnus-summary-reply
1536     "R" gnus-summary-reply-with-original
1537     "\C-c\C-f" gnus-summary-mail-forward
1538     "o" gnus-summary-save-article
1539     "\C-o" gnus-summary-save-article-mail
1540     "|" gnus-summary-pipe-output
1541     "\M-k" gnus-summary-edit-local-kill
1542     "\M-K" gnus-summary-edit-global-kill
1543     ;; "V" gnus-version
1544     "\C-c\C-d" gnus-summary-describe-group
1545     "q" gnus-summary-exit
1546     "Q" gnus-summary-exit-no-update
1547     "\C-c\C-i" gnus-info-find-node
1548     gnus-mouse-2 gnus-mouse-pick-article
1549     "m" gnus-summary-mail-other-window
1550     "a" gnus-summary-post-news
1551     "x" gnus-summary-limit-to-unread
1552     "s" gnus-summary-isearch-article
1553     "t" gnus-article-toggle-headers
1554     "g" gnus-summary-show-article
1555     "l" gnus-summary-goto-last-article
1556     "v" gnus-summary-preview-mime-message
1557     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1558     "\C-d" gnus-summary-enter-digest-group
1559     "\M-\C-d" gnus-summary-read-document
1560     "\M-\C-e" gnus-summary-edit-parameters
1561     "\M-\C-a" gnus-summary-customize-parameters
1562     "\C-c\C-b" gnus-bug
1563     "*" gnus-cache-enter-article
1564     "\M-*" gnus-cache-remove-article
1565     "\M-&" gnus-summary-universal-argument
1566     "\C-l" gnus-recenter
1567     "I" gnus-summary-increase-score
1568     "L" gnus-summary-lower-score
1569     "\M-i" gnus-symbolic-argument
1570     "h" gnus-summary-select-article-buffer
1571
1572     "V" gnus-summary-score-map
1573     "X" gnus-uu-extract-map
1574     "S" gnus-summary-send-map)
1575
1576   ;; Sort of orthogonal keymap
1577   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1578     "t" gnus-summary-tick-article-forward
1579     "!" gnus-summary-tick-article-forward
1580     "d" gnus-summary-mark-as-read-forward
1581     "r" gnus-summary-mark-as-read-forward
1582     "c" gnus-summary-clear-mark-forward
1583     " " gnus-summary-clear-mark-forward
1584     "e" gnus-summary-mark-as-expirable
1585     "x" gnus-summary-mark-as-expirable
1586     "?" gnus-summary-mark-as-dormant
1587     "b" gnus-summary-set-bookmark
1588     "B" gnus-summary-remove-bookmark
1589     "#" gnus-summary-mark-as-processable
1590     "\M-#" gnus-summary-unmark-as-processable
1591     "S" gnus-summary-limit-include-expunged
1592     "C" gnus-summary-catchup
1593     "H" gnus-summary-catchup-to-here
1594     "h" gnus-summary-catchup-from-here
1595     "\C-c" gnus-summary-catchup-all
1596     "k" gnus-summary-kill-same-subject-and-select
1597     "K" gnus-summary-kill-same-subject
1598     "P" gnus-uu-mark-map)
1599
1600   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1601     "c" gnus-summary-clear-above
1602     "u" gnus-summary-tick-above
1603     "m" gnus-summary-mark-above
1604     "k" gnus-summary-kill-below)
1605
1606   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1607     "/" gnus-summary-limit-to-subject
1608     "n" gnus-summary-limit-to-articles
1609     "w" gnus-summary-pop-limit
1610     "s" gnus-summary-limit-to-subject
1611     "a" gnus-summary-limit-to-author
1612     "u" gnus-summary-limit-to-unread
1613     "m" gnus-summary-limit-to-marks
1614     "M" gnus-summary-limit-exclude-marks
1615     "v" gnus-summary-limit-to-score
1616     "*" gnus-summary-limit-include-cached
1617     "D" gnus-summary-limit-include-dormant
1618     "T" gnus-summary-limit-include-thread
1619     "d" gnus-summary-limit-exclude-dormant
1620     "t" gnus-summary-limit-to-age
1621     "x" gnus-summary-limit-to-extra
1622     "p" gnus-summary-limit-to-display-predicate
1623     "E" gnus-summary-limit-include-expunged
1624     "c" gnus-summary-limit-exclude-childless-dormant
1625     "C" gnus-summary-limit-mark-excluded-as-read
1626     "o" gnus-summary-insert-old-articles
1627     "N" gnus-summary-insert-new-articles)
1628
1629   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1630     "n" gnus-summary-next-unread-article
1631     "p" gnus-summary-prev-unread-article
1632     "N" gnus-summary-next-article
1633     "P" gnus-summary-prev-article
1634     "\C-n" gnus-summary-next-same-subject
1635     "\C-p" gnus-summary-prev-same-subject
1636     "\M-n" gnus-summary-next-unread-subject
1637     "\M-p" gnus-summary-prev-unread-subject
1638     "f" gnus-summary-first-unread-article
1639     "b" gnus-summary-best-unread-article
1640     "j" gnus-summary-goto-article
1641     "g" gnus-summary-goto-subject
1642     "l" gnus-summary-goto-last-article
1643     "o" gnus-summary-pop-article)
1644
1645   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1646     "k" gnus-summary-kill-thread
1647     "l" gnus-summary-lower-thread
1648     "i" gnus-summary-raise-thread
1649     "T" gnus-summary-toggle-threads
1650     "t" gnus-summary-rethread-current
1651     "^" gnus-summary-reparent-thread
1652     "s" gnus-summary-show-thread
1653     "S" gnus-summary-show-all-threads
1654     "h" gnus-summary-hide-thread
1655     "H" gnus-summary-hide-all-threads
1656     "n" gnus-summary-next-thread
1657     "p" gnus-summary-prev-thread
1658     "u" gnus-summary-up-thread
1659     "o" gnus-summary-top-thread
1660     "d" gnus-summary-down-thread
1661     "#" gnus-uu-mark-thread
1662     "\M-#" gnus-uu-unmark-thread)
1663
1664   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1665     "g" gnus-summary-prepare
1666     "c" gnus-summary-insert-cached-articles)
1667
1668   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1669     "c" gnus-summary-catchup-and-exit
1670     "C" gnus-summary-catchup-all-and-exit
1671     "E" gnus-summary-exit-no-update
1672     "J" gnus-summary-jump-to-other-group
1673     "Q" gnus-summary-exit
1674     "Z" gnus-summary-exit
1675     "n" gnus-summary-catchup-and-goto-next-group
1676     "R" gnus-summary-reselect-current-group
1677     "G" gnus-summary-rescan-group
1678     "N" gnus-summary-next-group
1679     "s" gnus-summary-save-newsrc
1680     "P" gnus-summary-prev-group)
1681
1682   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1683     " " gnus-summary-next-page
1684     "n" gnus-summary-next-page
1685     "\177" gnus-summary-prev-page
1686     [delete] gnus-summary-prev-page
1687     "p" gnus-summary-prev-page
1688     "\r" gnus-summary-scroll-up
1689     "\M-\r" gnus-summary-scroll-down
1690     "<" gnus-summary-beginning-of-article
1691     ">" gnus-summary-end-of-article
1692     "b" gnus-summary-beginning-of-article
1693     "e" gnus-summary-end-of-article
1694     "^" gnus-summary-refer-parent-article
1695     "r" gnus-summary-refer-parent-article
1696     "D" gnus-summary-enter-digest-group
1697     "R" gnus-summary-refer-references
1698     "T" gnus-summary-refer-thread
1699     "g" gnus-summary-show-article
1700     "s" gnus-summary-isearch-article
1701     "P" gnus-summary-print-article
1702     "M" gnus-mailing-list-insinuate
1703     "t" gnus-article-babel)
1704
1705   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1706     "b" gnus-article-add-buttons
1707     "B" gnus-article-add-buttons-to-head
1708     "o" gnus-article-treat-overstrike
1709     "e" gnus-article-emphasize
1710     "w" gnus-article-fill-cited-article
1711     "Q" gnus-article-fill-long-lines
1712     "C" gnus-article-capitalize-sentences
1713     "c" gnus-article-remove-cr
1714     "Z" gnus-article-decode-HZ
1715     "f" gnus-article-display-x-face
1716     "l" gnus-summary-stop-page-breaking
1717     "r" gnus-summary-caesar-message
1718     "t" gnus-article-toggle-headers
1719     "v" gnus-summary-verbose-headers
1720     "m" gnus-summary-toggle-mime
1721     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1722     "p" gnus-article-verify-x-pgp-sig
1723     "d" gnus-article-treat-dumbquotes
1724     "s" gnus-smiley-display)
1725
1726   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1727     "a" gnus-article-hide
1728     "h" gnus-article-toggle-headers
1729     "b" gnus-article-hide-boring-headers
1730     "s" gnus-article-hide-signature
1731     "c" gnus-article-hide-citation
1732     "C" gnus-article-hide-citation-in-followups
1733     "l" gnus-article-hide-list-identifiers
1734     "p" gnus-article-hide-pgp
1735     "B" gnus-article-strip-banner
1736     "P" gnus-article-hide-pem
1737     "\C-c" gnus-article-hide-citation-maybe)
1738
1739   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1740     "a" gnus-article-highlight
1741     "h" gnus-article-highlight-headers
1742     "c" gnus-article-highlight-citation
1743     "s" gnus-article-highlight-signature)
1744
1745   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1746     "z" gnus-article-date-ut
1747     "u" gnus-article-date-ut
1748     "l" gnus-article-date-local
1749     "p" gnus-article-date-english
1750     "e" gnus-article-date-lapsed
1751     "o" gnus-article-date-original
1752     "i" gnus-article-date-iso8601
1753     "s" gnus-article-date-user)
1754
1755   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1756     "t" gnus-article-remove-trailing-blank-lines
1757     "l" gnus-article-strip-leading-blank-lines
1758     "m" gnus-article-strip-multiple-blank-lines
1759     "a" gnus-article-strip-blank-lines
1760     "A" gnus-article-strip-all-blank-lines
1761     "s" gnus-article-strip-leading-space
1762     "e" gnus-article-strip-trailing-space
1763     "w" gnus-article-remove-leading-whitespace)
1764
1765   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1766     "v" gnus-version
1767     "f" gnus-summary-fetch-faq
1768     "d" gnus-summary-describe-group
1769     "h" gnus-summary-describe-briefly
1770     "i" gnus-info-find-node)
1771
1772   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1773     "e" gnus-summary-expire-articles
1774     "\M-\C-e" gnus-summary-expire-articles-now
1775     "\177" gnus-summary-delete-article
1776     [delete] gnus-summary-delete-article
1777     [backspace] gnus-summary-delete-article
1778     "m" gnus-summary-move-article
1779     "r" gnus-summary-respool-article
1780     "w" gnus-summary-edit-article
1781     "c" gnus-summary-copy-article
1782     "B" gnus-summary-crosspost-article
1783     "q" gnus-summary-respool-query
1784     "t" gnus-summary-respool-trace
1785     "i" gnus-summary-import-article
1786     "I" gnus-summary-create-article
1787     "p" gnus-summary-article-posted-p)
1788
1789   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1790     "o" gnus-summary-save-article
1791     "m" gnus-summary-save-article-mail
1792     "F" gnus-summary-write-article-file
1793     "r" gnus-summary-save-article-rmail
1794     "f" gnus-summary-save-article-file
1795     "b" gnus-summary-save-article-body-file
1796     "h" gnus-summary-save-article-folder
1797     "v" gnus-summary-save-article-vm
1798     "p" gnus-summary-pipe-output
1799     "s" gnus-soup-add-article)
1800
1801   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1802     "b" gnus-summary-display-buttonized
1803     "m" gnus-summary-repair-multipart
1804     "v" gnus-article-view-part
1805     "o" gnus-article-save-part
1806     "c" gnus-article-copy-part
1807     "C" gnus-article-view-part-as-charset
1808     "e" gnus-article-externalize-part
1809     "E" gnus-article-encrypt-body
1810     "i" gnus-article-inline-part
1811     "|" gnus-article-pipe-part))
1812
1813 (defvar gnus-article-post-menu nil)
1814
1815 (defun gnus-summary-make-menu-bar ()
1816   (gnus-turn-off-edit-menu 'summary)
1817
1818   (unless (boundp 'gnus-summary-misc-menu)
1819
1820     (easy-menu-define
1821      gnus-summary-kill-menu gnus-summary-mode-map ""
1822      (cons
1823       "Score"
1824       (nconc
1825        (list
1826         ["Customize" gnus-score-customize t])
1827        (gnus-make-score-map 'increase)
1828        (gnus-make-score-map 'lower)
1829        '(("Mark"
1830           ["Kill below" gnus-summary-kill-below t]
1831           ["Mark above" gnus-summary-mark-above t]
1832           ["Tick above" gnus-summary-tick-above t]
1833           ["Clear above" gnus-summary-clear-above t])
1834          ["Current score" gnus-summary-current-score t]
1835          ["Set score" gnus-summary-set-score t]
1836          ["Switch current score file..." gnus-score-change-score-file t]
1837          ["Set mark below..." gnus-score-set-mark-below t]
1838          ["Set expunge below..." gnus-score-set-expunge-below t]
1839          ["Edit current score file" gnus-score-edit-current-scores t]
1840          ["Edit score file" gnus-score-edit-file t]
1841          ["Trace score" gnus-score-find-trace t]
1842          ["Find words" gnus-score-find-favourite-words t]
1843          ["Rescore buffer" gnus-summary-rescore t]
1844          ["Increase score..." gnus-summary-increase-score t]
1845          ["Lower score..." gnus-summary-lower-score t]))))
1846
1847     ;; Define both the Article menu in the summary buffer and the equivalent
1848     ;; Commands menu in the article buffer here for consistency.
1849     (let ((innards
1850            `(("Hide"
1851               ["All" gnus-article-hide t]
1852               ["Headers" gnus-article-toggle-headers t]
1853               ["Signature" gnus-article-hide-signature t]
1854               ["Citation" gnus-article-hide-citation t]
1855               ["List identifiers" gnus-article-hide-list-identifiers t]
1856               ["PGP" gnus-article-hide-pgp t]
1857               ["Banner" gnus-article-strip-banner t]
1858               ["Boring headers" gnus-article-hide-boring-headers t])
1859              ("Highlight"
1860               ["All" gnus-article-highlight t]
1861               ["Headers" gnus-article-highlight-headers t]
1862               ["Signature" gnus-article-highlight-signature t]
1863               ["Citation" gnus-article-highlight-citation t])
1864              ("Date"
1865               ["Local" gnus-article-date-local t]
1866               ["ISO8601" gnus-article-date-iso8601 t]
1867               ["UT" gnus-article-date-ut t]
1868               ["Original" gnus-article-date-original t]
1869               ["Lapsed" gnus-article-date-lapsed t]
1870               ["User-defined" gnus-article-date-user t])
1871              ("Washing"
1872               ("Remove Blanks"
1873                ["Leading" gnus-article-strip-leading-blank-lines t]
1874                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1875                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1876                ["All of the above" gnus-article-strip-blank-lines t]
1877                ["All" gnus-article-strip-all-blank-lines t]
1878                ["Leading space" gnus-article-strip-leading-space t]
1879                ["Trailing space" gnus-article-strip-trailing-space t]
1880                ["Leading space in headers"
1881                 gnus-article-remove-leading-whitespace t])
1882               ["Overstrike" gnus-article-treat-overstrike t]
1883               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1884               ["Emphasis" gnus-article-emphasize t]
1885               ["Word wrap" gnus-article-fill-cited-article t]
1886               ["Fill long lines" gnus-article-fill-long-lines t]
1887               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1888               ["CR" gnus-article-remove-cr t]
1889               ["Show X-Face" gnus-article-display-x-face t]
1890               ["Rot 13" gnus-summary-caesar-message
1891                ,@(if (featurep 'xemacs) '(t)
1892                    '(:help "\"Caesar rotate\" article by 13"))]
1893               ["Unix pipe" gnus-summary-pipe-message t]
1894               ["Add buttons" gnus-article-add-buttons t]
1895               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1896               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1897               ["Toggle MIME" gnus-summary-toggle-mime t]
1898               ["Verbose header" gnus-summary-verbose-headers t]
1899               ["Toggle header" gnus-summary-toggle-header t]
1900               ["Toggle smileys" gnus-smiley-display t]
1901               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1902               ["HZ" gnus-article-decode-HZ t])
1903              ("Output"
1904               ["Save in default format" gnus-summary-save-article
1905                ,@(if (featurep 'xemacs) '(t)
1906                    '(:help "Save article using default method"))]
1907               ["Save in file" gnus-summary-save-article-file
1908                ,@(if (featurep 'xemacs) '(t)
1909                    '(:help "Save article in file"))]
1910               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1911               ["Save in MH folder" gnus-summary-save-article-folder t]
1912               ["Save in VM folder" gnus-summary-save-article-vm t]
1913               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1914               ["Save body in file" gnus-summary-save-article-body-file t]
1915               ["Pipe through a filter" gnus-summary-pipe-output t]
1916               ["Add to SOUP packet" gnus-soup-add-article t]
1917               ["Print" gnus-summary-print-article t])
1918              ("Backend"
1919               ["Respool article..." gnus-summary-respool-article t]
1920               ["Move article..." gnus-summary-move-article
1921                (gnus-check-backend-function
1922                 'request-move-article gnus-newsgroup-name)]
1923               ["Copy article..." gnus-summary-copy-article t]
1924               ["Crosspost article..." gnus-summary-crosspost-article
1925                (gnus-check-backend-function
1926                 'request-replace-article gnus-newsgroup-name)]
1927               ["Import file..." gnus-summary-import-article t]
1928               ["Create article..." gnus-summary-create-article t]
1929               ["Check if posted" gnus-summary-article-posted-p t]
1930               ["Edit article" gnus-summary-edit-article
1931                (not (gnus-group-read-only-p))]
1932               ["Delete article" gnus-summary-delete-article
1933                (gnus-check-backend-function
1934                 'request-expire-articles gnus-newsgroup-name)]
1935               ["Query respool" gnus-summary-respool-query t]
1936               ["Trace respool" gnus-summary-respool-trace t]
1937               ["Delete expirable articles" gnus-summary-expire-articles-now
1938                (gnus-check-backend-function
1939                 'request-expire-articles gnus-newsgroup-name)])
1940              ("Extract"
1941               ["Uudecode" gnus-uu-decode-uu
1942                ,@(if (featurep 'xemacs) '(t)
1943                    '(:help "Decode uuencoded article(s)"))]
1944               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1945               ["Unshar" gnus-uu-decode-unshar t]
1946               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1947               ["Save" gnus-uu-decode-save t]
1948               ["Binhex" gnus-uu-decode-binhex t]
1949               ["Postscript" gnus-uu-decode-postscript t])
1950              ("Cache"
1951               ["Enter article" gnus-cache-enter-article t]
1952               ["Remove article" gnus-cache-remove-article t])
1953              ["Translate" gnus-article-babel t]
1954              ["Select article buffer" gnus-summary-select-article-buffer t]
1955              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1956              ["Isearch article..." gnus-summary-isearch-article t]
1957              ["Beginning of the article" gnus-summary-beginning-of-article t]
1958              ["End of the article" gnus-summary-end-of-article t]
1959              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1960              ["Fetch referenced articles" gnus-summary-refer-references t]
1961              ["Fetch current thread" gnus-summary-refer-thread t]
1962              ["Fetch article with id..." gnus-summary-refer-article t]
1963              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1964              ["Redisplay" gnus-summary-show-article t]
1965              ["Raw article" gnus-summary-show-raw-article t])))
1966       (easy-menu-define
1967        gnus-summary-article-menu gnus-summary-mode-map ""
1968        (cons "Article" innards))
1969
1970       (if (not (keymapp gnus-summary-article-menu))
1971           (easy-menu-define
1972            gnus-article-commands-menu gnus-article-mode-map ""
1973            (cons "Commands" innards))
1974         ;; in Emacs, don't share menu.
1975         (setq gnus-article-commands-menu
1976               (copy-keymap gnus-summary-article-menu))
1977         (define-key gnus-article-mode-map [menu-bar commands]
1978           (cons "Commands" gnus-article-commands-menu))))
1979
1980     (easy-menu-define
1981      gnus-summary-thread-menu gnus-summary-mode-map ""
1982      '("Threads"
1983        ["Toggle threading" gnus-summary-toggle-threads t]
1984        ["Hide threads" gnus-summary-hide-all-threads t]
1985        ["Show threads" gnus-summary-show-all-threads t]
1986        ["Hide thread" gnus-summary-hide-thread t]
1987        ["Show thread" gnus-summary-show-thread t]
1988        ["Go to next thread" gnus-summary-next-thread t]
1989        ["Go to previous thread" gnus-summary-prev-thread t]
1990        ["Go down thread" gnus-summary-down-thread t]
1991        ["Go up thread" gnus-summary-up-thread t]
1992        ["Top of thread" gnus-summary-top-thread t]
1993        ["Mark thread as read" gnus-summary-kill-thread t]
1994        ["Lower thread score" gnus-summary-lower-thread t]
1995        ["Raise thread score" gnus-summary-raise-thread t]
1996        ["Rethread current" gnus-summary-rethread-current t]))
1997
1998     (easy-menu-define
1999      gnus-summary-post-menu gnus-summary-mode-map ""
2000      `("Post"
2001        ["Post an article" gnus-summary-post-news
2002         ,@(if (featurep 'xemacs) '(t)
2003             '(:help "Post an article"))]
2004        ["Followup" gnus-summary-followup
2005         ,@(if (featurep 'xemacs) '(t)
2006             '(:help "Post followup to this article"))]
2007        ["Followup and yank" gnus-summary-followup-with-original
2008         ,@(if (featurep 'xemacs) '(t)
2009             '(:help "Post followup to this article, quoting its contents"))]
2010        ["Supersede article" gnus-summary-supersede-article t]
2011        ["Cancel article" gnus-summary-cancel-article
2012         ,@(if (featurep 'xemacs) '(t)
2013             '(:help "Cancel an article you posted"))]
2014        ["Reply" gnus-summary-reply t]
2015        ["Reply and yank" gnus-summary-reply-with-original t]
2016        ["Wide reply" gnus-summary-wide-reply t]
2017        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2018         ,@(if (featurep 'xemacs) '(t)
2019             '(:help "Mail a reply, quoting this article"))]
2020        ["Mail forward" gnus-summary-mail-forward t]
2021        ["Post forward" gnus-summary-post-forward t]
2022        ["Digest and mail" gnus-summary-digest-mail-forward t]
2023        ["Digest and post" gnus-summary-digest-post-forward t]
2024        ["Resend message" gnus-summary-resend-message t]
2025        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2026        ["Send a mail" gnus-summary-mail-other-window t]
2027        ["Uuencode and post" gnus-uu-post-news
2028         ,@(if (featurep 'xemacs) '(t)
2029             '(:help "Post a uuencoded article"))]
2030        ["Followup via news" gnus-summary-followup-to-mail t]
2031        ["Followup via news and yank"
2032         gnus-summary-followup-to-mail-with-original t]
2033        ;;("Draft"
2034        ;;["Send" gnus-summary-send-draft t]
2035        ;;["Send bounced" gnus-resend-bounced-mail t])
2036        ))
2037
2038     (cond
2039      ((not (keymapp gnus-summary-post-menu))
2040       (setq gnus-article-post-menu gnus-summary-post-menu))
2041      ((not gnus-article-post-menu)
2042       ;; Don't share post menu.
2043       (setq gnus-article-post-menu
2044             (copy-keymap gnus-summary-post-menu))))
2045     (define-key gnus-article-mode-map [menu-bar post]
2046       (cons "Post" gnus-article-post-menu))
2047
2048     (easy-menu-define
2049      gnus-summary-misc-menu gnus-summary-mode-map ""
2050      `("Gnus"
2051        ("Mark Read"
2052         ["Mark as read" gnus-summary-mark-as-read-forward t]
2053         ["Mark same subject and select"
2054          gnus-summary-kill-same-subject-and-select t]
2055         ["Mark same subject" gnus-summary-kill-same-subject t]
2056         ["Catchup" gnus-summary-catchup
2057          ,@(if (featurep 'xemacs) '(t)
2058              '(:help "Mark unread articles in this group as read"))]
2059         ["Catchup all" gnus-summary-catchup-all t]
2060         ["Catchup to here" gnus-summary-catchup-to-here t]
2061         ["Catchup from here" gnus-summary-catchup-from-here t]
2062         ["Catchup region" gnus-summary-mark-region-as-read t]
2063         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2064        ("Mark Various"
2065         ["Tick" gnus-summary-tick-article-forward t]
2066         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2067         ["Remove marks" gnus-summary-clear-mark-forward t]
2068         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2069         ["Set bookmark" gnus-summary-set-bookmark t]
2070         ["Remove bookmark" gnus-summary-remove-bookmark t])
2071        ("Mark Limit"
2072         ["Marks..." gnus-summary-limit-to-marks t]
2073         ["Subject..." gnus-summary-limit-to-subject t]
2074         ["Author..." gnus-summary-limit-to-author t]
2075         ["Age..." gnus-summary-limit-to-age t]
2076         ["Extra..." gnus-summary-limit-to-extra t]
2077         ["Score" gnus-summary-limit-to-score t]
2078         ["Score" gnus-summary-limit-to-display-predicate t]
2079         ["Unread" gnus-summary-limit-to-unread t]
2080         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2081         ["Articles" gnus-summary-limit-to-articles t]
2082         ["Pop limit" gnus-summary-pop-limit t]
2083         ["Show dormant" gnus-summary-limit-include-dormant t]
2084         ["Hide childless dormant"
2085          gnus-summary-limit-exclude-childless-dormant t]
2086         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2087         ["Hide marked" gnus-summary-limit-exclude-marks t]
2088         ["Show expunged" gnus-summary-limit-include-expunged t])
2089        ("Process Mark"
2090         ["Set mark" gnus-summary-mark-as-processable t]
2091         ["Remove mark" gnus-summary-unmark-as-processable t]
2092         ["Remove all marks" gnus-summary-unmark-all-processable t]
2093         ["Mark above" gnus-uu-mark-over t]
2094         ["Mark series" gnus-uu-mark-series t]
2095         ["Mark region" gnus-uu-mark-region t]
2096         ["Unmark region" gnus-uu-unmark-region t]
2097         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2098         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2099         ["Mark all" gnus-uu-mark-all t]
2100         ["Mark buffer" gnus-uu-mark-buffer t]
2101         ["Mark sparse" gnus-uu-mark-sparse t]
2102         ["Mark thread" gnus-uu-mark-thread t]
2103         ["Unmark thread" gnus-uu-unmark-thread t]
2104         ("Process Mark Sets"
2105          ["Kill" gnus-summary-kill-process-mark t]
2106          ["Yank" gnus-summary-yank-process-mark
2107           gnus-newsgroup-process-stack]
2108          ["Save" gnus-summary-save-process-mark t]))
2109        ("Scroll article"
2110         ["Page forward" gnus-summary-next-page
2111          ,@(if (featurep 'xemacs) '(t)
2112              '(:help "Show next page of article"))]
2113         ["Page backward" gnus-summary-prev-page
2114          ,@(if (featurep 'xemacs) '(t)
2115              '(:help "Show previous page of article"))]
2116         ["Line forward" gnus-summary-scroll-up t])
2117        ("Move"
2118         ["Next unread article" gnus-summary-next-unread-article t]
2119         ["Previous unread article" gnus-summary-prev-unread-article t]
2120         ["Next article" gnus-summary-next-article t]
2121         ["Previous article" gnus-summary-prev-article t]
2122         ["Next unread subject" gnus-summary-next-unread-subject t]
2123         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2124         ["Next article same subject" gnus-summary-next-same-subject t]
2125         ["Previous article same subject" gnus-summary-prev-same-subject t]
2126         ["First unread article" gnus-summary-first-unread-article t]
2127         ["Best unread article" gnus-summary-best-unread-article t]
2128         ["Go to subject number..." gnus-summary-goto-subject t]
2129         ["Go to article number..." gnus-summary-goto-article t]
2130         ["Go to the last article" gnus-summary-goto-last-article t]
2131         ["Pop article off history" gnus-summary-pop-article t])
2132        ("Sort"
2133         ["Sort by number" gnus-summary-sort-by-number t]
2134         ["Sort by author" gnus-summary-sort-by-author t]
2135         ["Sort by subject" gnus-summary-sort-by-subject t]
2136         ["Sort by date" gnus-summary-sort-by-date t]
2137         ["Sort by score" gnus-summary-sort-by-score t]
2138         ["Sort by lines" gnus-summary-sort-by-lines t]
2139         ["Sort by characters" gnus-summary-sort-by-chars t]
2140         ["Original sort" gnus-summary-sort-by-original t])
2141        ("Help"
2142         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2143         ["Describe group" gnus-summary-describe-group t]
2144         ["Read manual" gnus-info-find-node t])
2145        ("Modes"
2146         ["Pick and read" gnus-pick-mode t]
2147         ["Binary" gnus-binary-mode t])
2148        ("Regeneration"
2149         ["Regenerate" gnus-summary-prepare t]
2150         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2151         ["Toggle threading" gnus-summary-toggle-threads t])
2152        ["See old articles" gnus-summary-insert-old-articles t]
2153        ["See new articles" gnus-summary-insert-new-articles t]
2154        ["Filter articles..." gnus-summary-execute-command t]
2155        ["Run command on subjects..." gnus-summary-universal-argument t]
2156        ["Search articles forward..." gnus-summary-search-article-forward t]
2157        ["Search articles backward..." gnus-summary-search-article-backward t]
2158        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2159        ["Expand window" gnus-summary-expand-window t]
2160        ["Expire expirable articles" gnus-summary-expire-articles
2161         (gnus-check-backend-function
2162          'request-expire-articles gnus-newsgroup-name)]
2163        ["Edit local kill file" gnus-summary-edit-local-kill t]
2164        ["Edit main kill file" gnus-summary-edit-global-kill t]
2165        ["Edit group parameters" gnus-summary-edit-parameters t]
2166        ["Customize group parameters" gnus-summary-customize-parameters t]
2167        ["Send a bug report" gnus-bug t]
2168        ("Exit"
2169         ["Catchup and exit" gnus-summary-catchup-and-exit
2170          ,@(if (featurep 'xemacs) '(t)
2171              '(:help "Mark unread articles in this group as read, then exit"))]
2172         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2173         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2174         ["Exit group" gnus-summary-exit
2175          ,@(if (featurep 'xemacs) '(t)
2176              '(:help "Exit current group, return to group selection mode"))]
2177         ["Exit group without updating" gnus-summary-exit-no-update t]
2178         ["Exit and goto next group" gnus-summary-next-group t]
2179         ["Exit and goto prev group" gnus-summary-prev-group t]
2180         ["Reselect group" gnus-summary-reselect-current-group t]
2181         ["Rescan group" gnus-summary-rescan-group t]
2182         ["Update dribble" gnus-summary-save-newsrc t])))
2183
2184     (gnus-run-hooks 'gnus-summary-menu-hook)))
2185
2186 (defvar gnus-summary-tool-bar-map nil)
2187
2188 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2189 (defun gnus-summary-make-tool-bar ()
2190   (if (and (fboundp 'tool-bar-add-item-from-menu)
2191            (default-value 'tool-bar-mode)
2192            (not gnus-summary-tool-bar-map))
2193       (setq gnus-summary-tool-bar-map
2194             (let ((tool-bar-map (make-sparse-keymap))
2195                   (load-path (mm-image-load-path)))
2196               (tool-bar-add-item-from-menu
2197                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-post-news "post" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-followup "followup" gnus-summary-mode-map)
2206               (tool-bar-add-item-from-menu
2207                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2208               (tool-bar-add-item-from-menu
2209                'gnus-summary-reply "reply" gnus-summary-mode-map)
2210               (tool-bar-add-item-from-menu
2211                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2226               tool-bar-map)))
2227   (if gnus-summary-tool-bar-map
2228       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2229
2230 (defun gnus-score-set-default (var value)
2231   "A version of set that updates the GNU Emacs menu-bar."
2232   (set var value)
2233   ;; It is the message that forces the active status to be updated.
2234   (message ""))
2235
2236 (defun gnus-make-score-map (type)
2237   "Make a summary score map of type TYPE."
2238   (if t
2239       nil
2240     (let ((headers '(("author" "from" string)
2241                      ("subject" "subject" string)
2242                      ("article body" "body" string)
2243                      ("article head" "head" string)
2244                      ("xref" "xref" string)
2245                      ("extra header" "extra" string)
2246                      ("lines" "lines" number)
2247                      ("followups to author" "followup" string)))
2248           (types '((number ("less than" <)
2249                            ("greater than" >)
2250                            ("equal" =))
2251                    (string ("substring" s)
2252                            ("exact string" e)
2253                            ("fuzzy string" f)
2254                            ("regexp" r))))
2255           (perms '(("temporary" (current-time-string))
2256                    ("permanent" nil)
2257                    ("immediate" now)))
2258           header)
2259       (list
2260        (apply
2261         'nconc
2262         (list
2263          (if (eq type 'lower)
2264              "Lower score"
2265            "Increase score"))
2266         (let (outh)
2267           (while headers
2268             (setq header (car headers))
2269             (setq outh
2270                   (cons
2271                    (apply
2272                     'nconc
2273                     (list (car header))
2274                     (let ((ts (cdr (assoc (nth 2 header) types)))
2275                           outt)
2276                       (while ts
2277                         (setq outt
2278                               (cons
2279                                (apply
2280                                 'nconc
2281                                 (list (caar ts))
2282                                 (let ((ps perms)
2283                                       outp)
2284                                   (while ps
2285                                     (setq outp
2286                                           (cons
2287                                            (vector
2288                                             (caar ps)
2289                                             (list
2290                                              'gnus-summary-score-entry
2291                                              (nth 1 header)
2292                                              (if (or (string= (nth 1 header)
2293                                                               "head")
2294                                                      (string= (nth 1 header)
2295                                                               "body"))
2296                                                  ""
2297                                                (list 'gnus-summary-header
2298                                                      (nth 1 header)))
2299                                              (list 'quote (nth 1 (car ts)))
2300                                              (list 'gnus-score-delta-default
2301                                                    nil)
2302                                              (nth 1 (car ps))
2303                                              t)
2304                                             t)
2305                                            outp))
2306                                     (setq ps (cdr ps)))
2307                                   (list (nreverse outp))))
2308                                outt))
2309                         (setq ts (cdr ts)))
2310                       (list (nreverse outt))))
2311                    outh))
2312             (setq headers (cdr headers)))
2313           (list (nreverse outh))))))))
2314
2315 \f
2316
2317 (defun gnus-summary-mode (&optional group)
2318   "Major mode for reading articles.
2319
2320 All normal editing commands are switched off.
2321 \\<gnus-summary-mode-map>
2322 Each line in this buffer represents one article.  To read an
2323 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2324 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2325 respectively.
2326
2327 You can also post articles and send mail from this buffer.  To
2328 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2329 of an article, type `\\[gnus-summary-reply]'.
2330
2331 There are approx. one gazillion commands you can execute in this
2332 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2333
2334 The following commands are available:
2335
2336 \\{gnus-summary-mode-map}"
2337   (interactive)
2338   (kill-all-local-variables)
2339   (when (gnus-visual-p 'summary-menu 'menu)
2340     (gnus-summary-make-menu-bar)
2341     (gnus-summary-make-tool-bar))
2342   (gnus-summary-make-local-variables)
2343   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2344     (gnus-summary-make-local-variables))
2345   (gnus-make-thread-indent-array)
2346   (gnus-simplify-mode-line)
2347   (setq major-mode 'gnus-summary-mode)
2348   (setq mode-name "Summary")
2349   (make-local-variable 'minor-mode-alist)
2350   (use-local-map gnus-summary-mode-map)
2351   (buffer-disable-undo)
2352   (setq buffer-read-only t)             ;Disable modification
2353   (setq truncate-lines t)
2354   (setq selective-display t)
2355   (setq selective-display-ellipses t)   ;Display `...'
2356   (gnus-summary-set-display-table)
2357   (gnus-set-default-directory)
2358   (setq gnus-newsgroup-name group)
2359   (unless (gnus-news-group-p group)
2360     (setq gnus-newsgroup-incorporated
2361           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2362   (make-local-variable 'gnus-summary-line-format)
2363   (make-local-variable 'gnus-summary-line-format-spec)
2364   (make-local-variable 'gnus-summary-dummy-line-format)
2365   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2366   (make-local-variable 'gnus-summary-mark-positions)
2367   (make-local-hook 'pre-command-hook)
2368   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2369   (gnus-run-hooks 'gnus-summary-mode-hook)
2370   (turn-on-gnus-mailing-list-mode)
2371   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2372   (gnus-update-summary-mark-positions))
2373
2374 (defun gnus-summary-make-local-variables ()
2375   "Make all the local summary buffer variables."
2376   (let (global)
2377     (dolist (local gnus-summary-local-variables)
2378       (if (consp local)
2379           (progn
2380             (if (eq (cdr local) 'global)
2381                 ;; Copy the global value of the variable.
2382                 (setq global (symbol-value (car local)))
2383               ;; Use the value from the list.
2384               (setq global (eval (cdr local))))
2385             (set (make-local-variable (car local)) global))
2386         ;; Simple nil-valued local variable.
2387         (set (make-local-variable local) nil)))))
2388
2389 (defun gnus-summary-clear-local-variables ()
2390   (let ((locals gnus-summary-local-variables))
2391     (while locals
2392       (if (consp (car locals))
2393           (and (vectorp (caar locals))
2394                (set (caar locals) nil))
2395         (and (vectorp (car locals))
2396              (set (car locals) nil)))
2397       (setq locals (cdr locals)))))
2398
2399 ;; Summary data functions.
2400
2401 (defmacro gnus-data-number (data)
2402   `(car ,data))
2403
2404 (defmacro gnus-data-set-number (data number)
2405   `(setcar ,data ,number))
2406
2407 (defmacro gnus-data-mark (data)
2408   `(nth 1 ,data))
2409
2410 (defmacro gnus-data-set-mark (data mark)
2411   `(setcar (nthcdr 1 ,data) ,mark))
2412
2413 (defmacro gnus-data-pos (data)
2414   `(nth 2 ,data))
2415
2416 (defmacro gnus-data-set-pos (data pos)
2417   `(setcar (nthcdr 2 ,data) ,pos))
2418
2419 (defmacro gnus-data-header (data)
2420   `(nth 3 ,data))
2421
2422 (defmacro gnus-data-set-header (data header)
2423   `(setcar (nthcdr 3 ,data) ,header))
2424
2425 (defmacro gnus-data-level (data)
2426   `(nth 4 ,data))
2427
2428 (defmacro gnus-data-unread-p (data)
2429   `(= (nth 1 ,data) gnus-unread-mark))
2430
2431 (defmacro gnus-data-read-p (data)
2432   `(/= (nth 1 ,data) gnus-unread-mark))
2433
2434 (defmacro gnus-data-pseudo-p (data)
2435   `(consp (nth 3 ,data)))
2436
2437 (defmacro gnus-data-find (number)
2438   `(assq ,number gnus-newsgroup-data))
2439
2440 (defmacro gnus-data-find-list (number &optional data)
2441   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2442      (memq (assq ,number bdata)
2443            bdata)))
2444
2445 (defmacro gnus-data-make (number mark pos header level)
2446   `(list ,number ,mark ,pos ,header ,level))
2447
2448 (defun gnus-data-enter (after-article number mark pos header level offset)
2449   (let ((data (gnus-data-find-list after-article)))
2450     (unless data
2451       (error "No such article: %d" after-article))
2452     (setcdr data (cons (gnus-data-make number mark pos header level)
2453                        (cdr data)))
2454     (setq gnus-newsgroup-data-reverse nil)
2455     (gnus-data-update-list (cddr data) offset)))
2456
2457 (defun gnus-data-enter-list (after-article list &optional offset)
2458   (when list
2459     (let ((data (and after-article (gnus-data-find-list after-article)))
2460           (ilist list))
2461       (if (not (or data
2462                    after-article))
2463           (let ((odata gnus-newsgroup-data))
2464             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2465             (when offset
2466               (gnus-data-update-list odata offset)))
2467         ;; Find the last element in the list to be spliced into the main
2468         ;; list.
2469         (while (cdr list)
2470           (setq list (cdr list)))
2471         (if (not data)
2472             (progn
2473               (setcdr list gnus-newsgroup-data)
2474               (setq gnus-newsgroup-data ilist)
2475               (when offset
2476                 (gnus-data-update-list (cdr list) offset)))
2477           (setcdr list (cdr data))
2478           (setcdr data ilist)
2479           (when offset
2480             (gnus-data-update-list (cdr list) offset))))
2481       (setq gnus-newsgroup-data-reverse nil))))
2482
2483 (defun gnus-data-remove (article &optional offset)
2484   (let ((data gnus-newsgroup-data))
2485     (if (= (gnus-data-number (car data)) article)
2486         (progn
2487           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2488                 gnus-newsgroup-data-reverse nil)
2489           (when offset
2490             (gnus-data-update-list gnus-newsgroup-data offset)))
2491       (while (cdr data)
2492         (when (= (gnus-data-number (cadr data)) article)
2493           (setcdr data (cddr data))
2494           (when offset
2495             (gnus-data-update-list (cdr data) offset))
2496           (setq data nil
2497                 gnus-newsgroup-data-reverse nil))
2498         (setq data (cdr data))))))
2499
2500 (defmacro gnus-data-list (backward)
2501   `(if ,backward
2502        (or gnus-newsgroup-data-reverse
2503            (setq gnus-newsgroup-data-reverse
2504                  (reverse gnus-newsgroup-data)))
2505      gnus-newsgroup-data))
2506
2507 (defun gnus-data-update-list (data offset)
2508   "Add OFFSET to the POS of all data entries in DATA."
2509   (setq gnus-newsgroup-data-reverse nil)
2510   (while data
2511     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2512     (setq data (cdr data))))
2513
2514 (defun gnus-summary-article-pseudo-p (article)
2515   "Say whether this article is a pseudo article or not."
2516   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2517
2518 (defmacro gnus-summary-article-sparse-p (article)
2519   "Say whether this article is a sparse article or not."
2520   `(memq ,article gnus-newsgroup-sparse))
2521
2522 (defmacro gnus-summary-article-ancient-p (article)
2523   "Say whether this article is a sparse article or not."
2524   `(memq ,article gnus-newsgroup-ancient))
2525
2526 (defun gnus-article-parent-p (number)
2527   "Say whether this article is a parent or not."
2528   (let ((data (gnus-data-find-list number)))
2529     (and (cdr data)                     ; There has to be an article after...
2530          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2531             (gnus-data-level (nth 1 data))))))
2532
2533 (defun gnus-article-children (number)
2534   "Return a list of all children to NUMBER."
2535   (let* ((data (gnus-data-find-list number))
2536          (level (gnus-data-level (car data)))
2537          children)
2538     (setq data (cdr data))
2539     (while (and data
2540                 (= (gnus-data-level (car data)) (1+ level)))
2541       (push (gnus-data-number (car data)) children)
2542       (setq data (cdr data)))
2543     children))
2544
2545 (defmacro gnus-summary-skip-intangible ()
2546   "If the current article is intangible, then jump to a different article."
2547   '(let ((to (get-text-property (point) 'gnus-intangible)))
2548      (and to (gnus-summary-goto-subject to))))
2549
2550 (defmacro gnus-summary-article-intangible-p ()
2551   "Say whether this article is intangible or not."
2552   '(get-text-property (point) 'gnus-intangible))
2553
2554 (defun gnus-article-read-p (article)
2555   "Say whether ARTICLE is read or not."
2556   (not (or (memq article gnus-newsgroup-marked)
2557            (memq article gnus-newsgroup-unreads)
2558            (memq article gnus-newsgroup-unselected)
2559            (memq article gnus-newsgroup-dormant))))
2560
2561 ;; Some summary mode macros.
2562
2563 (defmacro gnus-summary-article-number ()
2564   "The article number of the article on the current line.
2565 If there isn's an article number here, then we return the current
2566 article number."
2567   '(progn
2568      (gnus-summary-skip-intangible)
2569      (or (get-text-property (point) 'gnus-number)
2570          (gnus-summary-last-subject))))
2571
2572 (defmacro gnus-summary-article-header (&optional number)
2573   "Return the header of article NUMBER."
2574   `(gnus-data-header (gnus-data-find
2575                       ,(or number '(gnus-summary-article-number)))))
2576
2577 (defmacro gnus-summary-thread-level (&optional number)
2578   "Return the level of thread that starts with article NUMBER."
2579   `(if (and (eq gnus-summary-make-false-root 'dummy)
2580             (get-text-property (point) 'gnus-intangible))
2581        0
2582      (gnus-data-level (gnus-data-find
2583                        ,(or number '(gnus-summary-article-number))))))
2584
2585 (defmacro gnus-summary-article-mark (&optional number)
2586   "Return the mark of article NUMBER."
2587   `(gnus-data-mark (gnus-data-find
2588                     ,(or number '(gnus-summary-article-number)))))
2589
2590 (defmacro gnus-summary-article-pos (&optional number)
2591   "Return the position of the line of article NUMBER."
2592   `(gnus-data-pos (gnus-data-find
2593                    ,(or number '(gnus-summary-article-number)))))
2594
2595 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2596 (defmacro gnus-summary-article-subject (&optional number)
2597   "Return current subject string or nil if nothing."
2598   `(let ((headers
2599           ,(if number
2600                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2601              '(gnus-data-header (assq (gnus-summary-article-number)
2602                                       gnus-newsgroup-data)))))
2603      (and headers
2604           (vectorp headers)
2605           (mail-header-subject headers))))
2606
2607 (defmacro gnus-summary-article-score (&optional number)
2608   "Return current article score."
2609   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2610                   gnus-newsgroup-scored))
2611        gnus-summary-default-score 0))
2612
2613 (defun gnus-summary-article-children (&optional number)
2614   "Return a list of article numbers that are children of article NUMBER."
2615   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2616          (level (gnus-data-level (car data)))
2617          l children)
2618     (while (and (setq data (cdr data))
2619                 (> (setq l (gnus-data-level (car data))) level))
2620       (and (= (1+ level) l)
2621            (push (gnus-data-number (car data))
2622                  children)))
2623     (nreverse children)))
2624
2625 (defun gnus-summary-article-parent (&optional number)
2626   "Return the article number of the parent of article NUMBER."
2627   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2628                                     (gnus-data-list t)))
2629          (level (gnus-data-level (car data))))
2630     (if (zerop level)
2631         ()                              ; This is a root.
2632       ;; We search until we find an article with a level less than
2633       ;; this one.  That function has to be the parent.
2634       (while (and (setq data (cdr data))
2635                   (not (< (gnus-data-level (car data)) level))))
2636       (and data (gnus-data-number (car data))))))
2637
2638 (defun gnus-unread-mark-p (mark)
2639   "Say whether MARK is the unread mark."
2640   (= mark gnus-unread-mark))
2641
2642 (defun gnus-read-mark-p (mark)
2643   "Say whether MARK is one of the marks that mark as read.
2644 This is all marks except unread, ticked, dormant, and expirable."
2645   (not (or (= mark gnus-unread-mark)
2646            (= mark gnus-ticked-mark)
2647            (= mark gnus-dormant-mark)
2648            (= mark gnus-expirable-mark))))
2649
2650 (defmacro gnus-article-mark (number)
2651   "Return the MARK of article NUMBER.
2652 This macro should only be used when computing the mark the \"first\"
2653 time; i.e., when generating the summary lines.  After that,
2654 `gnus-summary-article-mark' should be used to examine the
2655 marks of articles."
2656   `(cond
2657     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2658     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2659     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2660     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2661     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2662     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2663     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2664     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2665            gnus-ancient-mark))))
2666
2667 ;; Saving hidden threads.
2668
2669 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2670 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2671
2672 (defmacro gnus-save-hidden-threads (&rest forms)
2673   "Save hidden threads, eval FORMS, and restore the hidden threads."
2674   (let ((config (make-symbol "config")))
2675     `(let ((,config (gnus-hidden-threads-configuration)))
2676        (unwind-protect
2677            (save-excursion
2678              ,@forms)
2679          (gnus-restore-hidden-threads-configuration ,config)))))
2680
2681 (defun gnus-data-compute-positions ()
2682   "Compute the positions of all articles."
2683   (setq gnus-newsgroup-data-reverse nil)
2684   (let ((data gnus-newsgroup-data))
2685     (save-excursion
2686       (gnus-save-hidden-threads
2687         (gnus-summary-show-all-threads)
2688         (goto-char (point-min))
2689         (while data
2690           (while (get-text-property (point) 'gnus-intangible)
2691             (forward-line 1))
2692           (gnus-data-set-pos (car data) (+ (point) 3))
2693           (setq data (cdr data))
2694           (forward-line 1))))))
2695
2696 (defun gnus-hidden-threads-configuration ()
2697   "Return the current hidden threads configuration."
2698   (save-excursion
2699     (let (config)
2700       (goto-char (point-min))
2701       (while (search-forward "\r" nil t)
2702         (push (1- (point)) config))
2703       config)))
2704
2705 (defun gnus-restore-hidden-threads-configuration (config)
2706   "Restore hidden threads configuration from CONFIG."
2707   (save-excursion
2708     (let (point buffer-read-only)
2709       (while (setq point (pop config))
2710         (when (and (< point (point-max))
2711                    (goto-char point)
2712                    (eq (char-after) ?\n))
2713           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2714
2715 ;; Various summary mode internalish functions.
2716
2717 (defun gnus-mouse-pick-article (e)
2718   (interactive "e")
2719   (mouse-set-point e)
2720   (gnus-summary-next-page nil t))
2721
2722 (defun gnus-summary-set-display-table ()
2723   "Change the display table.
2724 Odd characters have a tendency to mess
2725 up nicely formatted displays - we make all possible glyphs
2726 display only a single character."
2727
2728   ;; We start from the standard display table, if any.
2729   (let ((table (or (copy-sequence standard-display-table)
2730                    (make-display-table)))
2731         (i 32))
2732     ;; Nix out all the control chars...
2733     (while (>= (setq i (1- i)) 0)
2734       (aset table i [??]))
2735     ;; ... but not newline and cr, of course.  (cr is necessary for the
2736     ;; selective display).
2737     (aset table ?\n nil)
2738     (aset table ?\r nil)
2739     ;; We keep TAB as well.
2740     (aset table ?\t nil)
2741     ;; We nix out any glyphs over 126 that are not set already.
2742     (let ((i 256))
2743       (while (>= (setq i (1- i)) 127)
2744         ;; Only modify if the entry is nil.
2745         (unless (aref table i)
2746           (aset table i [??]))))
2747     (setq buffer-display-table table)))
2748
2749 (defun gnus-summary-set-article-display-arrow (pos)
2750   "Update the overlay arrow to point to line at position POS."
2751   (when (and gnus-summary-display-arrow
2752              (boundp 'overlay-arrow-position)
2753              (boundp 'overlay-arrow-string))
2754     (save-excursion
2755       (goto-char pos)
2756       (beginning-of-line)
2757       (unless overlay-arrow-position
2758         (setq overlay-arrow-position (make-marker)))
2759       (setq overlay-arrow-string "=>"
2760             overlay-arrow-position (set-marker overlay-arrow-position
2761                                                (point)
2762                                                (current-buffer))))))
2763
2764 (defun gnus-summary-buffer-name (group)
2765   "Return the summary buffer name of GROUP."
2766   (concat "*Summary " group "*"))
2767
2768 (defun gnus-summary-setup-buffer (group)
2769   "Initialize summary buffer."
2770   (let ((buffer (gnus-summary-buffer-name group)))
2771     (if (get-buffer buffer)
2772         (progn
2773           (set-buffer buffer)
2774           (setq gnus-summary-buffer (current-buffer))
2775           (not gnus-newsgroup-prepared))
2776       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2777       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2778       (gnus-summary-mode group)
2779       (when gnus-carpal
2780         (gnus-carpal-setup-buffer 'summary))
2781       (unless gnus-single-article-buffer
2782         (make-local-variable 'gnus-article-buffer)
2783         (make-local-variable 'gnus-article-current)
2784         (make-local-variable 'gnus-original-article-buffer))
2785       (setq gnus-newsgroup-name group)
2786       ;; Set any local variables in the group parameters.
2787       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2788       t)))
2789
2790 (defun gnus-set-global-variables ()
2791   "Set the global equivalents of the buffer-local variables.
2792 They are set to the latest values they had.  These reflect the summary
2793 buffer that was in action when the last article was fetched."
2794   (when (eq major-mode 'gnus-summary-mode)
2795     (setq gnus-summary-buffer (current-buffer))
2796     (let ((name gnus-newsgroup-name)
2797           (marked gnus-newsgroup-marked)
2798           (unread gnus-newsgroup-unreads)
2799           (headers gnus-current-headers)
2800           (data gnus-newsgroup-data)
2801           (summary gnus-summary-buffer)
2802           (article-buffer gnus-article-buffer)
2803           (original gnus-original-article-buffer)
2804           (gac gnus-article-current)
2805           (reffed gnus-reffed-article-number)
2806           (score-file gnus-current-score-file)
2807           (default-charset gnus-newsgroup-charset)
2808           vlist)
2809       (let ((locals gnus-newsgroup-variables))
2810         (while locals
2811           (if (consp (car locals))
2812               (push (eval (caar locals)) vlist)
2813             (push (eval (car locals)) vlist))
2814           (setq locals (cdr locals)))
2815         (setq vlist (nreverse vlist)))
2816       (save-excursion
2817         (set-buffer gnus-group-buffer)
2818         (setq gnus-newsgroup-name name
2819               gnus-newsgroup-marked marked
2820               gnus-newsgroup-unreads unread
2821               gnus-current-headers headers
2822               gnus-newsgroup-data data
2823               gnus-article-current gac
2824               gnus-summary-buffer summary
2825               gnus-article-buffer article-buffer
2826               gnus-original-article-buffer original
2827               gnus-reffed-article-number reffed
2828               gnus-current-score-file score-file
2829               gnus-newsgroup-charset default-charset)
2830         (let ((locals gnus-newsgroup-variables))
2831           (while locals
2832             (if (consp (car locals))
2833                 (set (caar locals) (pop vlist))
2834               (set (car locals) (pop vlist)))
2835             (setq locals (cdr locals))))
2836         ;; The article buffer also has local variables.
2837         (when (gnus-buffer-live-p gnus-article-buffer)
2838           (set-buffer gnus-article-buffer)
2839           (setq gnus-summary-buffer summary))))))
2840
2841 (defun gnus-summary-article-unread-p (article)
2842   "Say whether ARTICLE is unread or not."
2843   (memq article gnus-newsgroup-unreads))
2844
2845 (defun gnus-summary-first-article-p (&optional article)
2846   "Return whether ARTICLE is the first article in the buffer."
2847   (if (not (setq article (or article (gnus-summary-article-number))))
2848       nil
2849     (eq article (caar gnus-newsgroup-data))))
2850
2851 (defun gnus-summary-last-article-p (&optional article)
2852   "Return whether ARTICLE is the last article in the buffer."
2853   (if (not (setq article (or article (gnus-summary-article-number))))
2854       ;; All non-existent numbers are the last article.  :-)
2855       t
2856     (not (cdr (gnus-data-find-list article)))))
2857
2858 (defun gnus-make-thread-indent-array ()
2859   (let ((n 200))
2860     (unless (and gnus-thread-indent-array
2861                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2862       (setq gnus-thread-indent-array (make-vector 201 "")
2863             gnus-thread-indent-array-level gnus-thread-indent-level)
2864       (while (>= n 0)
2865         (aset gnus-thread-indent-array n
2866               (make-string (* n gnus-thread-indent-level) ? ))
2867         (setq n (1- n))))))
2868
2869 (defun gnus-update-summary-mark-positions ()
2870   "Compute where the summary marks are to go."
2871   (save-excursion
2872     (when (gnus-buffer-exists-p gnus-summary-buffer)
2873       (set-buffer gnus-summary-buffer))
2874     (let ((gnus-replied-mark 129)
2875           (gnus-score-below-mark 130)
2876           (gnus-score-over-mark 130)
2877           (gnus-download-mark 131)
2878           (spec gnus-summary-line-format-spec)
2879           gnus-visual pos)
2880       (save-excursion
2881         (gnus-set-work-buffer)
2882         (let ((gnus-summary-line-format-spec spec)
2883               (gnus-newsgroup-downloadable '((0 . t))))
2884           (gnus-summary-insert-line
2885            (make-full-mail-header 0 "" "nobody"
2886                                   "05 Apr 2001 23:33:09 +0400"
2887                                   "" "" 0 0 "" nil)
2888            0 nil 128 t nil "" nil 1)
2889           (goto-char (point-min))
2890           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2891                                              (- (point) 2)))))
2892           (goto-char (point-min))
2893           (push (cons 'replied (and (search-forward "\201" nil t)
2894                                     (- (point) 2)))
2895                 pos)
2896           (goto-char (point-min))
2897           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2898                 pos)
2899           (goto-char (point-min))
2900           (push (cons 'download
2901                       (and (search-forward "\203" nil t) (- (point) 2)))
2902                 pos)))
2903       (setq gnus-summary-mark-positions pos))))
2904
2905 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2906   "Insert a dummy root in the summary buffer."
2907   (beginning-of-line)
2908   (gnus-add-text-properties
2909    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2910    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2911
2912 (defun gnus-summary-from-or-to-or-newsgroups (header)
2913   (let ((to (cdr (assq 'To (mail-header-extra header))))
2914         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2915         (default-mime-charset (with-current-buffer gnus-summary-buffer
2916                                 default-mime-charset)))
2917     (cond
2918      ((and to
2919            gnus-ignored-from-addresses
2920            (string-match gnus-ignored-from-addresses
2921                          (mail-header-from header)))
2922       (concat "-> "
2923               (or (car (funcall gnus-extract-address-components
2924                                 (funcall
2925                                  gnus-decode-encoded-word-function to)))
2926                   (funcall gnus-decode-encoded-word-function to))))
2927      ((and newsgroups
2928            gnus-ignored-from-addresses
2929            (string-match gnus-ignored-from-addresses
2930                          (mail-header-from header)))
2931       (concat "=> " newsgroups))
2932      (t
2933       (or (car (funcall gnus-extract-address-components
2934                         (mail-header-from header)))
2935           (mail-header-from header))))))
2936
2937 (defun gnus-summary-insert-line (gnus-tmp-header
2938                                  gnus-tmp-level gnus-tmp-current
2939                                  gnus-tmp-unread gnus-tmp-replied
2940                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2941                                  &optional gnus-tmp-dummy gnus-tmp-score
2942                                  gnus-tmp-process)
2943   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2944          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2945          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2946          (gnus-tmp-score-char
2947           (if (or (null gnus-summary-default-score)
2948                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2949                       gnus-summary-zcore-fuzz))
2950               ?\ ;;;Whitespace
2951             (if (< gnus-tmp-score gnus-summary-default-score)
2952                 gnus-score-below-mark gnus-score-over-mark)))
2953          (gnus-tmp-replied
2954           (cond (gnus-tmp-process gnus-process-mark)
2955                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2956                  gnus-cached-mark)
2957                 (gnus-tmp-replied gnus-replied-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2959                  gnus-forwarded-mark)
2960                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2961                  gnus-saved-mark)
2962                 ((memq number gnus-newsgroup-recent)
2963                  gnus-recent-mark)
2964                 ((memq number gnus-newsgroup-unseen)
2965                  gnus-unseen-mark)
2966                 (t gnus-no-mark)))
2967          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2968          (gnus-tmp-name
2969           (cond
2970            ((string-match "<[^>]+> *$" gnus-tmp-from)
2971             (let ((beg (match-beginning 0)))
2972               (or (and (string-match "^\".+\"" gnus-tmp-from)
2973                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2974                   (substring gnus-tmp-from 0 beg))))
2975            ((string-match "(.+)" gnus-tmp-from)
2976             (substring gnus-tmp-from
2977                        (1+ (match-beginning 0)) (1- (match-end 0))))
2978            (t gnus-tmp-from)))
2979          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2980          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2981          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2982          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2983          (buffer-read-only nil))
2984     (when (string= gnus-tmp-name "")
2985       (setq gnus-tmp-name gnus-tmp-from))
2986     (unless (numberp gnus-tmp-lines)
2987       (setq gnus-tmp-lines -1))
2988     (if (= gnus-tmp-lines -1)
2989         (setq gnus-tmp-lines "?")
2990       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2991     (gnus-put-text-property-excluding-characters-with-faces
2992      (point)
2993      (progn (eval gnus-summary-line-format-spec) (point))
2994      'gnus-number gnus-tmp-number)
2995     (when (gnus-visual-p 'summary-highlight 'highlight)
2996       (forward-line -1)
2997       (gnus-run-hooks 'gnus-summary-update-hook)
2998       (forward-line 1))))
2999
3000 (defun gnus-summary-update-line (&optional dont-update)
3001   "Update summary line after change."
3002   (when (and gnus-summary-default-score
3003              (not gnus-summary-inhibit-highlight))
3004     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3005            (article (gnus-summary-article-number))
3006            (score (gnus-summary-article-score article)))
3007       (unless dont-update
3008         (if (and gnus-summary-mark-below
3009                  (< (gnus-summary-article-score)
3010                     gnus-summary-mark-below))
3011             ;; This article has a low score, so we mark it as read.
3012             (when (memq article gnus-newsgroup-unreads)
3013               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3014           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3015             ;; This article was previously marked as read on account
3016             ;; of a low score, but now it has risen, so we mark it as
3017             ;; unread.
3018             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3019         (gnus-summary-update-mark
3020          (if (or (null gnus-summary-default-score)
3021                  (<= (abs (- score gnus-summary-default-score))
3022                      gnus-summary-zcore-fuzz))
3023              ?\ ;;;Whitespace
3024            (if (< score gnus-summary-default-score)
3025                gnus-score-below-mark gnus-score-over-mark))
3026          'score))
3027       ;; Do visual highlighting.
3028       (when (gnus-visual-p 'summary-highlight 'highlight)
3029         (gnus-run-hooks 'gnus-summary-update-hook)))))
3030
3031 (defvar gnus-tmp-new-adopts nil)
3032
3033 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3034   "Return the number of articles in THREAD.
3035 This may be 0 in some cases -- if none of the articles in
3036 the thread are to be displayed."
3037   (let* ((number
3038           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3039           (cond
3040            ((not (listp thread))
3041             1)
3042            ((and (consp thread) (cdr thread))
3043             (apply
3044              '+ 1 (mapcar
3045                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3046            ((null thread)
3047             1)
3048            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3049             1)
3050            (t 0))))
3051     (when (and level (zerop level) gnus-tmp-new-adopts)
3052       (incf number
3053             (apply '+ (mapcar
3054                        'gnus-summary-number-of-articles-in-thread
3055                        gnus-tmp-new-adopts))))
3056     (if char
3057         (if (> number 1) gnus-not-empty-thread-mark
3058           gnus-empty-thread-mark)
3059       number)))
3060
3061 (defun gnus-summary-set-local-parameters (group)
3062   "Go through the local params of GROUP and set all variable specs in that list."
3063   (let ((params (gnus-group-find-parameter group))
3064         (vars '(quit-config))           ; Ignore quit-config.
3065         elem)
3066     (while params
3067       (setq elem (car params)
3068             params (cdr params))
3069       (and (consp elem)                 ; Has to be a cons.
3070            (consp (cdr elem))           ; The cdr has to be a list.
3071            (symbolp (car elem))         ; Has to be a symbol in there.
3072            (not (memq (car elem) vars))
3073            (ignore-errors               ; So we set it.
3074              (push (car elem) vars)
3075              (make-local-variable (car elem))
3076              (set (car elem) (eval (nth 1 elem))))))))
3077
3078 (defun gnus-summary-read-group (group &optional show-all no-article
3079                                       kill-buffer no-display backward
3080                                       select-articles)
3081   "Start reading news in newsgroup GROUP.
3082 If SHOW-ALL is non-nil, already read articles are also listed.
3083 If NO-ARTICLE is non-nil, no article is selected initially.
3084 If NO-DISPLAY, don't generate a summary buffer."
3085   (let (result)
3086     (while (and group
3087                 (null (setq result
3088                             (let ((gnus-auto-select-next nil))
3089                               (or (gnus-summary-read-group-1
3090                                    group show-all no-article
3091                                    kill-buffer no-display
3092                                    select-articles)
3093                                   (setq show-all nil
3094                                         select-articles nil)))))
3095                 (eq gnus-auto-select-next 'quietly))
3096       (set-buffer gnus-group-buffer)
3097       ;; The entry function called above goes to the next
3098       ;; group automatically, so we go two groups back
3099       ;; if we are searching for the previous group.
3100       (when backward
3101         (gnus-group-prev-unread-group 2))
3102       (if (not (equal group (gnus-group-group-name)))
3103           (setq group (gnus-group-group-name))
3104         (setq group nil)))
3105     result))
3106
3107 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3108   "Directly jump to the other GROUP from summary buffer.
3109 If SHOW-ALL is non-nil, already read articles are also listed."
3110   (interactive
3111    (if (eq gnus-summary-buffer (current-buffer))
3112        (list (completing-read
3113               "Group: " gnus-active-hashtb nil t
3114               (when (and gnus-newsgroup-name
3115                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3116                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3117               'gnus-group-history)
3118              current-prefix-arg)
3119      (error "%s must be invoked from a gnus summary buffer." this-command)))
3120   (unless (or (zerop (length group))
3121               (and gnus-newsgroup-name
3122                    (string-equal gnus-newsgroup-name group)))
3123     (gnus-summary-exit)
3124     (gnus-summary-read-group group show-all
3125                              gnus-dont-select-after-jump-to-other-group)))
3126
3127 (defun gnus-summary-read-group-1 (group show-all no-article
3128                                         kill-buffer no-display
3129                                         &optional select-articles)
3130   ;; Killed foreign groups can't be entered.
3131   ;;  (when (and (not (gnus-group-native-p group))
3132   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3133   ;;    (error "Dead non-native groups can't be entered"))
3134   (gnus-message 5 "Retrieving newsgroup: %s..."
3135                 (gnus-group-decoded-name group))
3136   (let* ((new-group (gnus-summary-setup-buffer group))
3137          (quit-config (gnus-group-quit-config group))
3138          (did-select (and new-group (gnus-select-newsgroup
3139                                      group show-all select-articles))))
3140     (cond
3141      ;; This summary buffer exists already, so we just select it.
3142      ((not new-group)
3143       (gnus-set-global-variables)
3144       (when kill-buffer
3145         (gnus-kill-or-deaden-summary kill-buffer))
3146       (gnus-configure-windows 'summary 'force)
3147       (gnus-set-mode-line 'summary)
3148       (gnus-summary-position-point)
3149       (message "")
3150       t)
3151      ;; We couldn't select this group.
3152      ((null did-select)
3153       (when (and (eq major-mode 'gnus-summary-mode)
3154                  (not (equal (current-buffer) kill-buffer)))
3155         (kill-buffer (current-buffer))
3156         (if (not quit-config)
3157             (progn
3158               ;; Update the info -- marks might need to be removed,
3159               ;; for instance.
3160               (gnus-summary-update-info)
3161               (set-buffer gnus-group-buffer)
3162               (gnus-group-jump-to-group group)
3163               (gnus-group-next-unread-group 1))
3164           (gnus-handle-ephemeral-exit quit-config)))
3165       (let ((grpinfo (gnus-get-info group)))
3166         (if (null (gnus-info-read grpinfo))
3167             (gnus-message 3 "Group %s contains no messages"
3168                           (gnus-group-decoded-name group))
3169           (gnus-message 3 "Can't select group")))
3170       nil)
3171      ;; The user did a `C-g' while prompting for number of articles,
3172      ;; so we exit this group.
3173      ((eq did-select 'quit)
3174       (and (eq major-mode 'gnus-summary-mode)
3175            (not (equal (current-buffer) kill-buffer))
3176            (kill-buffer (current-buffer)))
3177       (when kill-buffer
3178         (gnus-kill-or-deaden-summary kill-buffer))
3179       (if (not quit-config)
3180           (progn
3181             (set-buffer gnus-group-buffer)
3182             (gnus-group-jump-to-group group)
3183             (gnus-group-next-unread-group 1)
3184             (gnus-configure-windows 'group 'force))
3185         (gnus-handle-ephemeral-exit quit-config))
3186       ;; Finally signal the quit.
3187       (signal 'quit nil))
3188      ;; The group was successfully selected.
3189      (t
3190       (gnus-set-global-variables)
3191       ;; Save the active value in effect when the group was entered.
3192       (setq gnus-newsgroup-active
3193             (gnus-copy-sequence
3194              (gnus-active gnus-newsgroup-name)))
3195       ;; You can change the summary buffer in some way with this hook.
3196       (gnus-run-hooks 'gnus-select-group-hook)
3197       (gnus-update-format-specifications
3198        nil 'summary 'summary-mode 'summary-dummy)
3199       (gnus-update-summary-mark-positions)
3200       ;; Do score processing.
3201       (when gnus-use-scoring
3202         (gnus-possibly-score-headers))
3203       ;; Check whether to fill in the gaps in the threads.
3204       (when gnus-build-sparse-threads
3205         (gnus-build-sparse-threads))
3206       ;; Find the initial limit.
3207       (if gnus-show-threads
3208           (if show-all
3209               (let ((gnus-newsgroup-dormant nil))
3210                 (gnus-summary-initial-limit show-all))
3211             (gnus-summary-initial-limit show-all))
3212         ;; When untreaded, all articles are always shown.
3213         (setq gnus-newsgroup-limit
3214               (mapcar
3215                (lambda (header) (mail-header-number header))
3216                gnus-newsgroup-headers)))
3217       ;; Generate the summary buffer.
3218       (unless no-display
3219         (gnus-summary-prepare))
3220       (when gnus-use-trees
3221         (gnus-tree-open group)
3222         (setq gnus-summary-highlight-line-function
3223               'gnus-tree-highlight-article))
3224       ;; If the summary buffer is empty, but there are some low-scored
3225       ;; articles or some excluded dormants, we include these in the
3226       ;; buffer.
3227       (when (and (zerop (buffer-size))
3228                  (not no-display))
3229         (cond (gnus-newsgroup-dormant
3230                (gnus-summary-limit-include-dormant))
3231               ((and gnus-newsgroup-scored show-all)
3232                (gnus-summary-limit-include-expunged t))))
3233       ;; Function `gnus-apply-kill-file' must be called in this hook.
3234       (gnus-run-hooks 'gnus-apply-kill-hook)
3235       (if (and (zerop (buffer-size))
3236                (not no-display))
3237           (progn
3238             ;; This newsgroup is empty.
3239             (gnus-summary-catchup-and-exit nil t)
3240             (gnus-message 6 "No unread news")
3241             (when kill-buffer
3242               (gnus-kill-or-deaden-summary kill-buffer))
3243             ;; Return nil from this function.
3244             nil)
3245         ;; Hide conversation thread subtrees.  We cannot do this in
3246         ;; gnus-summary-prepare-hook since kill processing may not
3247         ;; work with hidden articles.
3248         (and gnus-show-threads
3249              gnus-thread-hide-subtree
3250              (gnus-summary-hide-all-threads))
3251         (when kill-buffer
3252           (gnus-kill-or-deaden-summary kill-buffer))
3253         ;; Show first unread article if requested.
3254         (if (and (not no-article)
3255                  (not no-display)
3256                  gnus-newsgroup-unreads
3257                  gnus-auto-select-first)
3258             (progn
3259               (gnus-configure-windows 'summary)
3260               (cond
3261                ((eq gnus-auto-select-first 'best)
3262                 (gnus-summary-best-unread-article))
3263                ((eq gnus-auto-select-first t)
3264                 (gnus-summary-first-unread-article))
3265                ((gnus-functionp gnus-auto-select-first)
3266                 (funcall gnus-auto-select-first))))
3267           ;; Don't select any articles, just move point to the first
3268           ;; article in the group.
3269           (goto-char (point-min))
3270           (gnus-summary-position-point)
3271           (gnus-configure-windows 'summary 'force)
3272           (gnus-set-mode-line 'summary))
3273         (when (get-buffer-window gnus-group-buffer t)
3274           ;; Gotta use windows, because recenter does weird stuff if
3275           ;; the current buffer ain't the displayed window.
3276           (let ((owin (selected-window)))
3277             (select-window (get-buffer-window gnus-group-buffer t))
3278             (when (gnus-group-goto-group group)
3279               (recenter))
3280             (select-window owin)))
3281         ;; Mark this buffer as "prepared".
3282         (setq gnus-newsgroup-prepared t)
3283         (gnus-run-hooks 'gnus-summary-prepared-hook)
3284         t)))))
3285
3286 (defun gnus-summary-prepare ()
3287   "Generate the summary buffer."
3288   (interactive)
3289   (let ((buffer-read-only nil))
3290     (erase-buffer)
3291     (setq gnus-newsgroup-data nil
3292           gnus-newsgroup-data-reverse nil)
3293     (gnus-run-hooks 'gnus-summary-generate-hook)
3294     ;; Generate the buffer, either with threads or without.
3295     (when gnus-newsgroup-headers
3296       (gnus-summary-prepare-threads
3297        (if gnus-show-threads
3298            (gnus-sort-gathered-threads
3299             (funcall gnus-summary-thread-gathering-function
3300                      (gnus-sort-threads
3301                       (gnus-cut-threads (gnus-make-threads)))))
3302          ;; Unthreaded display.
3303          (gnus-sort-articles gnus-newsgroup-headers))))
3304     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3305     ;; Call hooks for modifying summary buffer.
3306     (goto-char (point-min))
3307     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3308
3309 (defsubst gnus-general-simplify-subject (subject)
3310   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3311   (setq subject
3312         (cond
3313          ;; Truncate the subject.
3314          (gnus-simplify-subject-functions
3315           (gnus-map-function gnus-simplify-subject-functions subject))
3316          ((numberp gnus-summary-gather-subject-limit)
3317           (setq subject (gnus-simplify-subject-re subject))
3318           (if (> (length subject) gnus-summary-gather-subject-limit)
3319               (substring subject 0 gnus-summary-gather-subject-limit)
3320             subject))
3321          ;; Fuzzily simplify it.
3322          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3323           (gnus-simplify-subject-fuzzy subject))
3324          ;; Just remove the leading "Re:".
3325          (t
3326           (gnus-simplify-subject-re subject))))
3327
3328   (if (and gnus-summary-gather-exclude-subject
3329            (string-match gnus-summary-gather-exclude-subject subject))
3330       nil                               ; This article shouldn't be gathered
3331     subject))
3332
3333 (defun gnus-summary-simplify-subject-query ()
3334   "Query where the respool algorithm would put this article."
3335   (interactive)
3336   (gnus-summary-select-article)
3337   (message "%s"
3338            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3339
3340 (defun gnus-gather-threads-by-subject (threads)
3341   "Gather threads by looking at Subject headers."
3342   (if (not gnus-summary-make-false-root)
3343       threads
3344     (let ((hashtb (gnus-make-hashtable 1024))
3345           (prev threads)
3346           (result threads)
3347           subject hthread whole-subject)
3348       (while threads
3349         (setq subject (gnus-general-simplify-subject
3350                        (setq whole-subject (mail-header-subject
3351                                             (caar threads)))))
3352         (when subject
3353           (if (setq hthread (gnus-gethash subject hashtb))
3354               (progn
3355                 ;; We enter a dummy root into the thread, if we
3356                 ;; haven't done that already.
3357                 (unless (stringp (caar hthread))
3358                   (setcar hthread (list whole-subject (car hthread))))
3359                 ;; We add this new gathered thread to this gathered
3360                 ;; thread.
3361                 (setcdr (car hthread)
3362                         (nconc (cdar hthread) (list (car threads))))
3363                 ;; Remove it from the list of threads.
3364                 (setcdr prev (cdr threads))
3365                 (setq threads prev))
3366             ;; Enter this thread into the hash table.
3367             (gnus-sethash subject threads hashtb)))
3368         (setq prev threads)
3369         (setq threads (cdr threads)))
3370       result)))
3371
3372 (defun gnus-gather-threads-by-references (threads)
3373   "Gather threads by looking at References headers."
3374   (let ((idhashtb (gnus-make-hashtable 1024))
3375         (thhashtb (gnus-make-hashtable 1024))
3376         (prev threads)
3377         (result threads)
3378         ids references id gthread gid entered ref)
3379     (while threads
3380       (when (setq references (mail-header-references (caar threads)))
3381         (setq id (mail-header-id (caar threads))
3382               ids (gnus-split-references references)
3383               entered nil)
3384         (while (setq ref (pop ids))
3385           (setq ids (delete ref ids))
3386           (if (not (setq gid (gnus-gethash ref idhashtb)))
3387               (progn
3388                 (gnus-sethash ref id idhashtb)
3389                 (gnus-sethash id threads thhashtb))
3390             (setq gthread (gnus-gethash gid thhashtb))
3391             (unless entered
3392               ;; We enter a dummy root into the thread, if we
3393               ;; haven't done that already.
3394               (unless (stringp (caar gthread))
3395                 (setcar gthread (list (mail-header-subject (caar gthread))
3396                                       (car gthread))))
3397               ;; We add this new gathered thread to this gathered
3398               ;; thread.
3399               (setcdr (car gthread)
3400                       (nconc (cdar gthread) (list (car threads)))))
3401             ;; Add it into the thread hash table.
3402             (gnus-sethash id gthread thhashtb)
3403             (setq entered t)
3404             ;; Remove it from the list of threads.
3405             (setcdr prev (cdr threads))
3406             (setq threads prev))))
3407       (setq prev threads)
3408       (setq threads (cdr threads)))
3409     result))
3410
3411 (defun gnus-sort-gathered-threads (threads)
3412   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3413   (let ((result threads))
3414     (while threads
3415       (when (stringp (caar threads))
3416         (setcdr (car threads)
3417                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3418       (setq threads (cdr threads)))
3419     result))
3420
3421 (defun gnus-thread-loop-p (root thread)
3422   "Say whether ROOT is in THREAD."
3423   (let ((stack (list thread))
3424         (infloop 0)
3425         th)
3426     (while (setq thread (pop stack))
3427       (setq th (cdr thread))
3428       (while (and th
3429                   (not (eq (caar th) root)))
3430         (pop th))
3431       (if th
3432           ;; We have found a loop.
3433           (let (ref-dep)
3434             (setcdr thread (delq (car th) (cdr thread)))
3435             (if (boundp (setq ref-dep (intern "none"
3436                                               gnus-newsgroup-dependencies)))
3437                 (setcdr (symbol-value ref-dep)
3438                         (nconc (cdr (symbol-value ref-dep))
3439                                (list (car th))))
3440               (set ref-dep (list nil (car th))))
3441             (setq infloop 1
3442                   stack nil))
3443         ;; Push all the subthreads onto the stack.
3444         (push (cdr thread) stack)))
3445     infloop))
3446
3447 (defun gnus-make-threads ()
3448   "Go through the dependency hashtb and find the roots.  Return all threads."
3449   (let (threads)
3450     (while (catch 'infloop
3451              (mapatoms
3452               (lambda (refs)
3453                 ;; Deal with self-referencing References loops.
3454                 (when (and (car (symbol-value refs))
3455                            (not (zerop
3456                                  (apply
3457                                   '+
3458                                   (mapcar
3459                                    (lambda (thread)
3460                                      (gnus-thread-loop-p
3461                                       (car (symbol-value refs)) thread))
3462                                    (cdr (symbol-value refs)))))))
3463                   (setq threads nil)
3464                   (throw 'infloop t))
3465                 (unless (car (symbol-value refs))
3466                   ;; These threads do not refer back to any other articles,
3467                   ;; so they're roots.
3468                   (setq threads (append (cdr (symbol-value refs)) threads))))
3469               gnus-newsgroup-dependencies)))
3470     threads))
3471
3472 ;; Build the thread tree.
3473 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3474   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3475
3476 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3477 if it was already present.
3478
3479 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3480 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3481 Message-IDs will be renamed be renamed to a unique Message-ID before
3482 being entered.
3483
3484 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3485   (let* ((id (mail-header-id header))
3486          (id-dep (and id (intern id dependencies)))
3487          ref ref-dep ref-header)
3488     ;; Enter this `header' in the `dependencies' table.
3489     (cond
3490      ((not id-dep)
3491       (setq header nil))
3492      ;; The first two cases do the normal part: enter a new `header'
3493      ;; in the `dependencies' table.
3494      ((not (boundp id-dep))
3495       (set id-dep (list header)))
3496      ((null (car (symbol-value id-dep)))
3497       (setcar (symbol-value id-dep) header))
3498
3499      ;; From here the `header' was already present in the
3500      ;; `dependencies' table.
3501      (force-new
3502       ;; Overrides an existing entry;
3503       ;; just set the header part of the entry.
3504       (setcar (symbol-value id-dep) header))
3505
3506      ;; Renames the existing `header' to a unique Message-ID.
3507      ((not gnus-summary-ignore-duplicates)
3508       ;; An article with this Message-ID has already been seen.
3509       ;; We rename the Message-ID.
3510       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3511            (list header))
3512       (mail-header-set-id header id))
3513
3514      ;; The last case ignores an existing entry, except it adds any
3515      ;; additional Xrefs (in case the two articles came from different
3516      ;; servers.
3517      ;; Also sets `header' to `nil' meaning that the `dependencies'
3518      ;; table was *not* modified.
3519      (t
3520       (mail-header-set-xref
3521        (car (symbol-value id-dep))
3522        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3523                    "")
3524                (or (mail-header-xref header) "")))
3525       (setq header nil)))
3526
3527     (when header
3528       ;; First check if that we are not creating a References loop.
3529       (setq ref (gnus-parent-id (mail-header-references header)))
3530       (while (and ref
3531                   (setq ref-dep (intern-soft ref dependencies))
3532                   (boundp ref-dep)
3533                   (setq ref-header (car (symbol-value ref-dep))))
3534         (if (string= id ref)
3535             ;; Yuk!  This is a reference loop.  Make the article be a
3536             ;; root article.
3537             (progn
3538               (mail-header-set-references (car (symbol-value id-dep)) "none")
3539               (setq ref nil))
3540           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3541       (setq ref (gnus-parent-id (mail-header-references header)))
3542       (setq ref-dep (intern (or ref "none") dependencies))
3543       (if (boundp ref-dep)
3544           (setcdr (symbol-value ref-dep)
3545                   (nconc (cdr (symbol-value ref-dep))
3546                          (list (symbol-value id-dep))))
3547         (set ref-dep (list nil (symbol-value id-dep)))))
3548     header))
3549
3550 (defun gnus-build-sparse-threads ()
3551   (let ((headers gnus-newsgroup-headers)
3552         (mail-parse-charset gnus-newsgroup-charset)
3553         (gnus-summary-ignore-duplicates t)
3554         header references generation relations
3555         subject child end new-child date)
3556     ;; First we create an alist of generations/relations, where
3557     ;; generations is how much we trust the relation, and the relation
3558     ;; is parent/child.
3559     (gnus-message 7 "Making sparse threads...")
3560     (save-excursion
3561       (nnheader-set-temp-buffer " *gnus sparse threads*")
3562       (while (setq header (pop headers))
3563         (when (and (setq references (mail-header-references header))
3564                    (not (string= references "")))
3565           (insert references)
3566           (setq child (mail-header-id header)
3567                 subject (mail-header-subject header)
3568                 date (mail-header-date header)
3569                 generation 0)
3570           (while (search-backward ">" nil t)
3571             (setq end (1+ (point)))
3572             (when (search-backward "<" nil t)
3573               (setq new-child (buffer-substring (point) end))
3574               (push (list (incf generation)
3575                           child (setq child new-child)
3576                           subject date)
3577                     relations)))
3578           (when child
3579             (push (list (1+ generation) child nil subject) relations))
3580           (erase-buffer)))
3581       (kill-buffer (current-buffer)))
3582     ;; Sort over trustworthiness.
3583     (mapcar
3584      (lambda (relation)
3585        (when (gnus-dependencies-add-header
3586               (make-full-mail-header-from-decoded-header
3587                gnus-reffed-article-number
3588                (nth 3 relation) "" (or (nth 4 relation) "")
3589                (nth 1 relation)
3590                (or (nth 2 relation) "") 0 0 "")
3591               gnus-newsgroup-dependencies nil)
3592          (push gnus-reffed-article-number gnus-newsgroup-limit)
3593          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3594          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3595                gnus-newsgroup-reads)
3596          (decf gnus-reffed-article-number)))
3597      (sort relations 'car-less-than-car))
3598     (gnus-message 7 "Making sparse threads...done")))
3599
3600 (defun gnus-build-old-threads ()
3601   ;; Look at all the articles that refer back to old articles, and
3602   ;; fetch the headers for the articles that aren't there.  This will
3603   ;; build complete threads - if the roots haven't been expired by the
3604   ;; server, that is.
3605   (let ((mail-parse-charset gnus-newsgroup-charset)
3606         id heads)
3607     (mapatoms
3608      (lambda (refs)
3609        (when (not (car (symbol-value refs)))
3610          (setq heads (cdr (symbol-value refs)))
3611          (while heads
3612            (if (memq (mail-header-number (caar heads))
3613                      gnus-newsgroup-dormant)
3614                (setq heads (cdr heads))
3615              (setq id (symbol-name refs))
3616              (while (and (setq id (gnus-build-get-header id))
3617                          (not (car (gnus-id-to-thread id)))))
3618              (setq heads nil)))))
3619      gnus-newsgroup-dependencies)))
3620
3621 ;; This function has to be called with point after the article number
3622 ;; on the beginning of the line.
3623 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3624   (let ((eol (gnus-point-at-eol))
3625         (buffer (current-buffer))
3626         header)
3627
3628     ;; overview: [num subject from date id refs chars lines misc]
3629     (unwind-protect
3630         (progn
3631           (narrow-to-region (point) eol)
3632           (unless (eobp)
3633             (forward-char))
3634
3635           (setq header
3636                 (make-full-mail-header
3637                  number                         ; number
3638                  (nnheader-nov-field)           ; subject
3639                  (nnheader-nov-field)           ; from
3640                  (nnheader-nov-field)           ; date
3641                  (nnheader-nov-read-message-id) ; id
3642                  (nnheader-nov-field)           ; refs
3643                  (nnheader-nov-read-integer)    ; chars
3644                  (nnheader-nov-read-integer)    ; lines
3645                  (unless (eobp)
3646                    (if (looking-at "Xref: ")
3647                        (goto-char (match-end 0)))
3648                    (nnheader-nov-field))        ; Xref
3649                  (nnheader-nov-parse-extra))))  ; extra
3650
3651       (widen))
3652
3653     (when gnus-alter-header-function
3654       (funcall gnus-alter-header-function header))
3655     (gnus-dependencies-add-header header dependencies force-new)))
3656
3657 (defun gnus-build-get-header (id)
3658   "Look through the buffer of NOV lines and find the header to ID.
3659 Enter this line into the dependencies hash table, and return
3660 the id of the parent article (if any)."
3661   (let ((deps gnus-newsgroup-dependencies)
3662         found header)
3663     (prog1
3664         (save-excursion
3665           (set-buffer nntp-server-buffer)
3666           (let ((case-fold-search nil))
3667             (goto-char (point-min))
3668             (while (and (not found)
3669                         (search-forward id nil t))
3670               (beginning-of-line)
3671               (setq found (looking-at
3672                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3673                                    (regexp-quote id))))
3674               (or found (beginning-of-line 2)))
3675             (when found
3676               (beginning-of-line)
3677               (and
3678                (setq header (gnus-nov-parse-line
3679                              (read (current-buffer)) deps))
3680                (gnus-parent-id (mail-header-references header))))))
3681       (when header
3682         (let ((number (mail-header-number header)))
3683           (push number gnus-newsgroup-limit)
3684           (push header gnus-newsgroup-headers)
3685           (if (memq number gnus-newsgroup-unselected)
3686               (progn
3687                 (push number gnus-newsgroup-unreads)
3688                 (setq gnus-newsgroup-unselected
3689                       (delq number gnus-newsgroup-unselected)))
3690             (push number gnus-newsgroup-ancient)))))))
3691
3692 (defun gnus-build-all-threads ()
3693   "Read all the headers."
3694   (let ((gnus-summary-ignore-duplicates t)
3695         (mail-parse-charset gnus-newsgroup-charset)
3696         (dependencies gnus-newsgroup-dependencies)
3697         header article)
3698     (save-excursion
3699       (set-buffer nntp-server-buffer)
3700       (let ((case-fold-search nil))
3701         (goto-char (point-min))
3702         (while (not (eobp))
3703           (ignore-errors
3704             (setq article (read (current-buffer))
3705                   header (gnus-nov-parse-line article dependencies)))
3706           (when header
3707             (save-excursion
3708               (set-buffer gnus-summary-buffer)
3709               (push header gnus-newsgroup-headers)
3710               (if (memq (setq article (mail-header-number header))
3711                         gnus-newsgroup-unselected)
3712                   (progn
3713                     (push article gnus-newsgroup-unreads)
3714                     (setq gnus-newsgroup-unselected
3715                           (delq article gnus-newsgroup-unselected)))
3716                 (push article gnus-newsgroup-ancient)))
3717             (forward-line 1)))))))
3718
3719 (defun gnus-summary-update-article-line (article header)
3720   "Update the line for ARTICLE using HEADERS."
3721   (let* ((id (mail-header-id header))
3722          (thread (gnus-id-to-thread id)))
3723     (unless thread
3724       (error "Article in no thread"))
3725     ;; Update the thread.
3726     (setcar thread header)
3727     (gnus-summary-goto-subject article)
3728     (let* ((datal (gnus-data-find-list article))
3729            (data (car datal))
3730            (length (when (cdr datal)
3731                      (- (gnus-data-pos data)
3732                         (gnus-data-pos (cadr datal)))))
3733            (buffer-read-only nil)
3734            (level (gnus-summary-thread-level)))
3735       (gnus-delete-line)
3736       (gnus-summary-insert-line
3737        header level nil (gnus-article-mark article)
3738        (memq article gnus-newsgroup-replied)
3739        (memq article gnus-newsgroup-expirable)
3740        ;; Only insert the Subject string when it's different
3741        ;; from the previous Subject string.
3742        (if (and
3743             gnus-show-threads
3744             (gnus-subject-equal
3745              (condition-case ()
3746                  (mail-header-subject
3747                   (gnus-data-header
3748                    (cadr
3749                     (gnus-data-find-list
3750                      article
3751                      (gnus-data-list t)))))
3752                ;; Error on the side of excessive subjects.
3753                (error ""))
3754              (mail-header-subject header)))
3755            ""
3756          (mail-header-subject header))
3757        nil (cdr (assq article gnus-newsgroup-scored))
3758        (memq article gnus-newsgroup-processable))
3759       (when length
3760         (gnus-data-update-list
3761          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3762
3763 (defun gnus-summary-update-article (article &optional iheader)
3764   "Update ARTICLE in the summary buffer."
3765   (set-buffer gnus-summary-buffer)
3766   (let* ((header (gnus-summary-article-header article))
3767          (id (mail-header-id header))
3768          (data (gnus-data-find article))
3769          (thread (gnus-id-to-thread id))
3770          (references (mail-header-references header))
3771          (parent
3772           (gnus-id-to-thread
3773            (or (gnus-parent-id
3774                 (when (and references
3775                            (not (equal "" references)))
3776                   references))
3777                "none")))
3778          (buffer-read-only nil)
3779          (old (car thread)))
3780     (when thread
3781       (unless iheader
3782         (setcar thread nil)
3783         (when parent
3784           (delq thread parent)))
3785       (if (gnus-summary-insert-subject id header)
3786           ;; Set the (possibly) new article number in the data structure.
3787           (gnus-data-set-number data (gnus-id-to-article id))
3788         (setcar thread old)
3789         nil))))
3790
3791 (defun gnus-rebuild-thread (id &optional line)
3792   "Rebuild the thread containing ID.
3793 If LINE, insert the rebuilt thread starting on line LINE."
3794   (let ((buffer-read-only nil)
3795         old-pos current thread data)
3796     (if (not gnus-show-threads)
3797         (setq thread (list (car (gnus-id-to-thread id))))
3798       ;; Get the thread this article is part of.
3799       (setq thread (gnus-remove-thread id)))
3800     (setq old-pos (gnus-point-at-bol))
3801     (setq current (save-excursion
3802                     (and (re-search-backward "[\r\n]" nil t)
3803                          (gnus-summary-article-number))))
3804     ;; If this is a gathered thread, we have to go some re-gathering.
3805     (when (stringp (car thread))
3806       (let ((subject (car thread))
3807             roots thr)
3808         (setq thread (cdr thread))
3809         (while thread
3810           (unless (memq (setq thr (gnus-id-to-thread
3811                                    (gnus-root-id
3812                                     (mail-header-id (caar thread)))))
3813                         roots)
3814             (push thr roots))
3815           (setq thread (cdr thread)))
3816         ;; We now have all (unique) roots.
3817         (if (= (length roots) 1)
3818             ;; All the loose roots are now one solid root.
3819             (setq thread (car roots))
3820           (setq thread (cons subject (gnus-sort-threads roots))))))
3821     (let (threads)
3822       ;; We then insert this thread into the summary buffer.
3823       (when line
3824         (goto-char (point-min))
3825         (forward-line (1- line)))
3826       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3827         (if gnus-show-threads
3828             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3829           (gnus-summary-prepare-unthreaded thread))
3830         (setq data (nreverse gnus-newsgroup-data))
3831         (setq threads gnus-newsgroup-threads))
3832       ;; We splice the new data into the data structure.
3833       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3834       ;;!!! then we want to insert at the beginning of the buffer.
3835       ;;!!! That happens to be true with Gnus now, but that may
3836       ;;!!! change in the future.  Perhaps.
3837       (gnus-data-enter-list
3838        (if line nil current) data (- (point) old-pos))
3839       (setq gnus-newsgroup-threads
3840             (nconc threads gnus-newsgroup-threads))
3841       (gnus-data-compute-positions))))
3842
3843 (defun gnus-number-to-header (number)
3844   "Return the header for article NUMBER."
3845   (let ((headers gnus-newsgroup-headers))
3846     (while (and headers
3847                 (not (= number (mail-header-number (car headers)))))
3848       (pop headers))
3849     (when headers
3850       (car headers))))
3851
3852 (defun gnus-parent-headers (in-headers &optional generation)
3853   "Return the headers of the GENERATIONeth parent of HEADERS."
3854   (unless generation
3855     (setq generation 1))
3856   (let ((parent t)
3857         (headers in-headers)
3858         references)
3859     (while (and parent
3860                 (not (zerop generation))
3861                 (setq references (mail-header-references headers)))
3862       (setq headers (if (and references
3863                              (setq parent (gnus-parent-id references)))
3864                         (car (gnus-id-to-thread parent))
3865                       nil))
3866       (decf generation))
3867     (and (not (eq headers in-headers))
3868          headers)))
3869
3870 (defun gnus-id-to-thread (id)
3871   "Return the (sub-)thread where ID appears."
3872   (gnus-gethash id gnus-newsgroup-dependencies))
3873
3874 (defun gnus-id-to-article (id)
3875   "Return the article number of ID."
3876   (let ((thread (gnus-id-to-thread id)))
3877     (when (and thread
3878                (car thread))
3879       (mail-header-number (car thread)))))
3880
3881 (defun gnus-id-to-header (id)
3882   "Return the article headers of ID."
3883   (car (gnus-id-to-thread id)))
3884
3885 (defun gnus-article-displayed-root-p (article)
3886   "Say whether ARTICLE is a root(ish) article."
3887   (let ((level (gnus-summary-thread-level article))
3888         (refs (mail-header-references  (gnus-summary-article-header article)))
3889         particle)
3890     (cond
3891      ((null level) nil)
3892      ((zerop level) t)
3893      ((null refs) t)
3894      ((null (gnus-parent-id refs)) t)
3895      ((and (= 1 level)
3896            (null (setq particle (gnus-id-to-article
3897                                  (gnus-parent-id refs))))
3898            (null (gnus-summary-thread-level particle)))))))
3899
3900 (defun gnus-root-id (id)
3901   "Return the id of the root of the thread where ID appears."
3902   (let (last-id prev)
3903     (while (and id (setq prev (car (gnus-id-to-thread id))))
3904       (setq last-id id
3905             id (gnus-parent-id (mail-header-references prev))))
3906     last-id))
3907
3908 (defun gnus-articles-in-thread (thread)
3909   "Return the list of articles in THREAD."
3910   (cons (mail-header-number (car thread))
3911         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3912
3913 (defun gnus-remove-thread (id &optional dont-remove)
3914   "Remove the thread that has ID in it."
3915   (let (headers thread last-id)
3916     ;; First go up in this thread until we find the root.
3917     (setq last-id (gnus-root-id id)
3918           headers (message-flatten-list (gnus-id-to-thread last-id)))
3919     ;; We have now found the real root of this thread.  It might have
3920     ;; been gathered into some loose thread, so we have to search
3921     ;; through the threads to find the thread we wanted.
3922     (let ((threads gnus-newsgroup-threads)
3923           sub)
3924       (while threads
3925         (setq sub (car threads))
3926         (if (stringp (car sub))
3927             ;; This is a gathered thread, so we look at the roots
3928             ;; below it to find whether this article is in this
3929             ;; gathered root.
3930             (progn
3931               (setq sub (cdr sub))
3932               (while sub
3933                 (when (member (caar sub) headers)
3934                   (setq thread (car threads)
3935                         threads nil
3936                         sub nil))
3937                 (setq sub (cdr sub))))
3938           ;; It's an ordinary thread, so we check it.
3939           (when (eq (car sub) (car headers))
3940             (setq thread sub
3941                   threads nil)))
3942         (setq threads (cdr threads)))
3943       ;; If this article is in no thread, then it's a root.
3944       (if thread
3945           (unless dont-remove
3946             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3947         (setq thread (gnus-id-to-thread last-id)))
3948       (when thread
3949         (prog1
3950             thread                      ; We return this thread.
3951           (unless dont-remove
3952             (if (stringp (car thread))
3953                 (progn
3954                   ;; If we use dummy roots, then we have to remove the
3955                   ;; dummy root as well.
3956                   (when (eq gnus-summary-make-false-root 'dummy)
3957                     ;; We go to the dummy root by going to
3958                     ;; the first sub-"thread", and then one line up.
3959                     (gnus-summary-goto-article
3960                      (mail-header-number (caadr thread)))
3961                     (forward-line -1)
3962                     (gnus-delete-line)
3963                     (gnus-data-compute-positions))
3964                   (setq thread (cdr thread))
3965                   (while thread
3966                     (gnus-remove-thread-1 (car thread))
3967                     (setq thread (cdr thread))))
3968               (gnus-remove-thread-1 thread))))))))
3969
3970 (defun gnus-remove-thread-1 (thread)
3971   "Remove the thread THREAD recursively."
3972   (let ((number (mail-header-number (pop thread)))
3973         d)
3974     (setq thread (reverse thread))
3975     (while thread
3976       (gnus-remove-thread-1 (pop thread)))
3977     (when (setq d (gnus-data-find number))
3978       (goto-char (gnus-data-pos d))
3979       (gnus-summary-show-thread)
3980       (gnus-data-remove
3981        number
3982        (- (gnus-point-at-bol)
3983           (prog1
3984               (1+ (gnus-point-at-eol))
3985             (gnus-delete-line)))))))
3986
3987 (defun gnus-sort-threads-1 (threads func)
3988   (sort (mapcar (lambda (thread)
3989                   (cons (car thread)
3990                         (and (cdr thread)
3991                              (gnus-sort-threads-1 (cdr thread) func))))
3992                 threads) func))
3993
3994 (defun gnus-sort-threads (threads)
3995   "Sort THREADS."
3996   (if (not gnus-thread-sort-functions)
3997       threads
3998     (gnus-message 8 "Sorting threads...")
3999     (prog1
4000         (gnus-sort-threads-1
4001          threads
4002          (gnus-make-sort-function gnus-thread-sort-functions))
4003       (gnus-message 8 "Sorting threads...done"))))
4004
4005 (defun gnus-sort-articles (articles)
4006   "Sort ARTICLES."
4007   (when gnus-article-sort-functions
4008     (gnus-message 7 "Sorting articles...")
4009     (prog1
4010         (setq gnus-newsgroup-headers
4011               (sort articles (gnus-make-sort-function
4012                               gnus-article-sort-functions)))
4013       (gnus-message 7 "Sorting articles...done"))))
4014
4015 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4016 (defmacro gnus-thread-header (thread)
4017   "Return header of first article in THREAD.
4018 Note that THREAD must never, ever be anything else than a variable -
4019 using some other form will lead to serious barfage."
4020   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4021   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4022   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4023         (vector thread) 2))
4024
4025 (defsubst gnus-article-sort-by-number (h1 h2)
4026   "Sort articles by article number."
4027   (< (mail-header-number h1)
4028      (mail-header-number h2)))
4029
4030 (defun gnus-thread-sort-by-number (h1 h2)
4031   "Sort threads by root article number."
4032   (gnus-article-sort-by-number
4033    (gnus-thread-header h1) (gnus-thread-header h2)))
4034
4035 (defsubst gnus-article-sort-by-lines (h1 h2)
4036   "Sort articles by article Lines header."
4037   (< (mail-header-lines h1)
4038      (mail-header-lines h2)))
4039
4040 (defun gnus-thread-sort-by-lines (h1 h2)
4041   "Sort threads by root article Lines header."
4042   (gnus-article-sort-by-lines
4043    (gnus-thread-header h1) (gnus-thread-header h2)))
4044
4045 (defsubst gnus-article-sort-by-chars (h1 h2)
4046   "Sort articles by octet length."
4047   (< (mail-header-chars h1)
4048      (mail-header-chars h2)))
4049
4050 (defun gnus-thread-sort-by-chars (h1 h2)
4051   "Sort threads by root article octet length."
4052   (gnus-article-sort-by-chars
4053    (gnus-thread-header h1) (gnus-thread-header h2)))
4054
4055 (defsubst gnus-article-sort-by-author (h1 h2)
4056   "Sort articles by root author."
4057   (string-lessp
4058    (let ((addr (car (mime-entity-read-field h1 'From))))
4059      (or (std11-full-name-string addr)
4060          (std11-address-string addr)
4061          ""))
4062    (let ((addr (car (mime-entity-read-field h2 'From))))
4063      (or (std11-full-name-string addr)
4064          (std11-address-string addr)
4065          ""))
4066    ))
4067
4068 (defun gnus-thread-sort-by-author (h1 h2)
4069   "Sort threads by root author."
4070   (gnus-article-sort-by-author
4071    (gnus-thread-header h1)  (gnus-thread-header h2)))
4072
4073 (defsubst gnus-article-sort-by-subject (h1 h2)
4074   "Sort articles by root subject."
4075   (string-lessp
4076    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4077    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4078
4079 (defun gnus-thread-sort-by-subject (h1 h2)
4080   "Sort threads by root subject."
4081   (gnus-article-sort-by-subject
4082    (gnus-thread-header h1) (gnus-thread-header h2)))
4083
4084 (defsubst gnus-article-sort-by-date (h1 h2)
4085   "Sort articles by root article date."
4086   (time-less-p
4087    (gnus-date-get-time (mail-header-date h1))
4088    (gnus-date-get-time (mail-header-date h2))))
4089
4090 (defun gnus-thread-sort-by-date (h1 h2)
4091   "Sort threads by root article date."
4092   (gnus-article-sort-by-date
4093    (gnus-thread-header h1) (gnus-thread-header h2)))
4094
4095 (defsubst gnus-article-sort-by-score (h1 h2)
4096   "Sort articles by root article score.
4097 Unscored articles will be counted as having a score of zero."
4098   (> (or (cdr (assq (mail-header-number h1)
4099                     gnus-newsgroup-scored))
4100          gnus-summary-default-score 0)
4101      (or (cdr (assq (mail-header-number h2)
4102                     gnus-newsgroup-scored))
4103          gnus-summary-default-score 0)))
4104
4105 (defun gnus-thread-sort-by-score (h1 h2)
4106   "Sort threads by root article score."
4107   (gnus-article-sort-by-score
4108    (gnus-thread-header h1) (gnus-thread-header h2)))
4109
4110 (defun gnus-thread-sort-by-total-score (h1 h2)
4111   "Sort threads by the sum of all scores in the thread.
4112 Unscored articles will be counted as having a score of zero."
4113   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4114
4115 (defun gnus-thread-total-score (thread)
4116   ;; This function find the total score of THREAD.
4117   (cond ((null thread)
4118          0)
4119         ((consp thread)
4120          (if (stringp (car thread))
4121              (apply gnus-thread-score-function 0
4122                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4123            (gnus-thread-total-score-1 thread)))
4124         (t
4125          (gnus-thread-total-score-1 (list thread)))))
4126
4127 (defun gnus-thread-total-score-1 (root)
4128   ;; This function find the total score of the thread below ROOT.
4129   (setq root (car root))
4130   (apply gnus-thread-score-function
4131          (or (append
4132               (mapcar 'gnus-thread-total-score
4133                       (cdr (gnus-id-to-thread (mail-header-id root))))
4134               (when (> (mail-header-number root) 0)
4135                 (list (or (cdr (assq (mail-header-number root)
4136                                      gnus-newsgroup-scored))
4137                           gnus-summary-default-score 0))))
4138              (list gnus-summary-default-score)
4139              '(0))))
4140
4141 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4142 (defvar gnus-tmp-prev-subject nil)
4143 (defvar gnus-tmp-false-parent nil)
4144 (defvar gnus-tmp-root-expunged nil)
4145 (defvar gnus-tmp-dummy-line nil)
4146
4147 (eval-when-compile (defvar gnus-tmp-header))
4148 (defun gnus-extra-header (type &optional header)
4149   "Return the extra header of TYPE."
4150   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4151       ""))
4152
4153 (defvar gnus-tmp-thread-tree-header-string "")
4154
4155 (defvar gnus-sum-thread-tree-root "> "
4156   "With %B spec, used for the root of a thread.
4157 If nil, use subject instead.")
4158 (defvar gnus-sum-thread-tree-single-indent ""
4159   "With %B spec, used for a thread with just one message.
4160 If nil, use subject instead.")
4161 (defvar gnus-sum-thread-tree-vertical "| "
4162   "With %B spec, used for drawing a vertical line.")
4163 (defvar gnus-sum-thread-tree-indent "  "
4164   "With %B spec, used for indenting.")
4165 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4166   "With %B spec, used for a leaf with brothers.")
4167 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4168   "With %B spec, used for a leaf without brothers.")
4169
4170 (defun gnus-summary-prepare-threads (threads)
4171   "Prepare summary buffer from THREADS and indentation LEVEL.
4172 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4173 or a straight list of headers."
4174   (gnus-message 7 "Generating summary...")
4175
4176   (setq gnus-newsgroup-threads threads)
4177   (beginning-of-line)
4178
4179   (let ((gnus-tmp-level 0)
4180         (default-score (or gnus-summary-default-score 0))
4181         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4182         thread number subject stack state gnus-tmp-gathered beg-match
4183         new-roots gnus-tmp-new-adopts thread-end
4184         gnus-tmp-header gnus-tmp-unread
4185         gnus-tmp-replied gnus-tmp-subject-or-nil
4186         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4187         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4188         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4189         tree-stack)
4190
4191     (setq gnus-tmp-prev-subject nil)
4192
4193     (if (vectorp (car threads))
4194         ;; If this is a straight (sic) list of headers, then a
4195         ;; threaded summary display isn't required, so we just create
4196         ;; an unthreaded one.
4197         (gnus-summary-prepare-unthreaded threads)
4198
4199       ;; Do the threaded display.
4200
4201       (while (or threads stack gnus-tmp-new-adopts new-roots)
4202
4203         (if (and (= gnus-tmp-level 0)
4204                  (or (not stack)
4205                      (= (caar stack) 0))
4206                  (not gnus-tmp-false-parent)
4207                  (or gnus-tmp-new-adopts new-roots))
4208             (if gnus-tmp-new-adopts
4209                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4210                       thread (list (car gnus-tmp-new-adopts))
4211                       gnus-tmp-header (caar thread)
4212                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4213               (when new-roots
4214                 (setq thread (list (car new-roots))
4215                       gnus-tmp-header (caar thread)
4216                       new-roots (cdr new-roots))))
4217
4218           (if threads
4219               ;; If there are some threads, we do them before the
4220               ;; threads on the stack.
4221               (setq thread threads
4222                     gnus-tmp-header (caar thread))
4223             ;; There were no current threads, so we pop something off
4224             ;; the stack.
4225             (setq state (car stack)
4226                   gnus-tmp-level (car state)
4227                   tree-stack (cadr state)
4228                   thread (caddr state)
4229                   stack (cdr stack)
4230                   gnus-tmp-header (caar thread))))
4231
4232         (setq gnus-tmp-false-parent nil)
4233         (setq gnus-tmp-root-expunged nil)
4234         (setq thread-end nil)
4235
4236         (if (stringp gnus-tmp-header)
4237             ;; The header is a dummy root.
4238             (cond
4239              ((eq gnus-summary-make-false-root 'adopt)
4240               ;; We let the first article adopt the rest.
4241               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4242                                                (cddar thread)))
4243               (setq gnus-tmp-gathered
4244                     (nconc (mapcar
4245                             (lambda (h) (mail-header-number (car h)))
4246                             (cddar thread))
4247                            gnus-tmp-gathered))
4248               (setq thread (cons (list (caar thread)
4249                                        (cadar thread))
4250                                  (cdr thread)))
4251               (setq gnus-tmp-level -1
4252                     gnus-tmp-false-parent t))
4253              ((eq gnus-summary-make-false-root 'empty)
4254               ;; We print adopted articles with empty subject fields.
4255               (setq gnus-tmp-gathered
4256                     (nconc (mapcar
4257                             (lambda (h) (mail-header-number (car h)))
4258                             (cddar thread))
4259                            gnus-tmp-gathered))
4260               (setq gnus-tmp-level -1))
4261              ((eq gnus-summary-make-false-root 'dummy)
4262               ;; We remember that we probably want to output a dummy
4263               ;; root.
4264               (setq gnus-tmp-dummy-line gnus-tmp-header)
4265               (setq gnus-tmp-prev-subject gnus-tmp-header))
4266              (t
4267               ;; We do not make a root for the gathered
4268               ;; sub-threads at all.
4269               (setq gnus-tmp-level -1)))
4270
4271           (setq number (mail-header-number gnus-tmp-header)
4272                 subject (mail-header-subject gnus-tmp-header))
4273
4274           (cond
4275            ;; If the thread has changed subject, we might want to make
4276            ;; this subthread into a root.
4277            ((and (null gnus-thread-ignore-subject)
4278                  (not (zerop gnus-tmp-level))
4279                  gnus-tmp-prev-subject
4280                  (not (inline
4281                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4282             (setq new-roots (nconc new-roots (list (car thread)))
4283                   thread-end t
4284                   gnus-tmp-header nil))
4285            ;; If the article lies outside the current limit,
4286            ;; then we do not display it.
4287            ((not (memq number gnus-newsgroup-limit))
4288             (setq gnus-tmp-gathered
4289                   (nconc (mapcar
4290                           (lambda (h) (mail-header-number (car h)))
4291                           (cdar thread))
4292                          gnus-tmp-gathered))
4293             (setq gnus-tmp-new-adopts (if (cdar thread)
4294                                           (append gnus-tmp-new-adopts
4295                                                   (cdar thread))
4296                                         gnus-tmp-new-adopts)
4297                   thread-end t
4298                   gnus-tmp-header nil)
4299             (when (zerop gnus-tmp-level)
4300               (setq gnus-tmp-root-expunged t)))
4301            ;; Perhaps this article is to be marked as read?
4302            ((and gnus-summary-mark-below
4303                  (< (or (cdr (assq number gnus-newsgroup-scored))
4304                         default-score)
4305                     gnus-summary-mark-below)
4306                  ;; Don't touch sparse articles.
4307                  (not (gnus-summary-article-sparse-p number))
4308                  (not (gnus-summary-article-ancient-p number)))
4309             (setq gnus-newsgroup-unreads
4310                   (delq number gnus-newsgroup-unreads))
4311             (if gnus-newsgroup-auto-expire
4312                 (push number gnus-newsgroup-expirable)
4313               (push (cons number gnus-low-score-mark)
4314                     gnus-newsgroup-reads))))
4315
4316           (when gnus-tmp-header
4317             ;; We may have an old dummy line to output before this
4318             ;; article.
4319             (when (and gnus-tmp-dummy-line
4320                        (gnus-subject-equal
4321                         gnus-tmp-dummy-line
4322                         (mail-header-subject gnus-tmp-header)))
4323               (gnus-summary-insert-dummy-line
4324                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4325               (setq gnus-tmp-dummy-line nil))
4326
4327             ;; Compute the mark.
4328             (setq gnus-tmp-unread (gnus-article-mark number))
4329
4330             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4331                                   gnus-tmp-header gnus-tmp-level)
4332                   gnus-newsgroup-data)
4333
4334             ;; Actually insert the line.
4335             (setq
4336              gnus-tmp-subject-or-nil
4337              (cond
4338               ((and gnus-thread-ignore-subject
4339                     gnus-tmp-prev-subject
4340                     (not (inline (gnus-subject-equal
4341                                   gnus-tmp-prev-subject subject))))
4342                subject)
4343               ((zerop gnus-tmp-level)
4344                (if (and (eq gnus-summary-make-false-root 'empty)
4345                         (memq number gnus-tmp-gathered)
4346                         gnus-tmp-prev-subject
4347                         (inline (gnus-subject-equal
4348                                  gnus-tmp-prev-subject subject)))
4349                    gnus-summary-same-subject
4350                  subject))
4351               (t gnus-summary-same-subject)))
4352             (if (and (eq gnus-summary-make-false-root 'adopt)
4353                      (= gnus-tmp-level 1)
4354                      (memq number gnus-tmp-gathered))
4355                 (setq gnus-tmp-opening-bracket ?\<
4356                       gnus-tmp-closing-bracket ?\>)
4357               (setq gnus-tmp-opening-bracket ?\[
4358                     gnus-tmp-closing-bracket ?\]))
4359             (setq
4360              gnus-tmp-indentation
4361              (aref gnus-thread-indent-array gnus-tmp-level)
4362              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4363              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4364                                 gnus-summary-default-score 0)
4365              gnus-tmp-score-char
4366              (if (or (null gnus-summary-default-score)
4367                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4368                          gnus-summary-zcore-fuzz))
4369                  ?\ ;;;Whitespace
4370                (if (< gnus-tmp-score gnus-summary-default-score)
4371                    gnus-score-below-mark gnus-score-over-mark))
4372              gnus-tmp-replied
4373              (cond ((memq number gnus-newsgroup-processable)
4374                     gnus-process-mark)
4375                    ((memq number gnus-newsgroup-cached)
4376                     gnus-cached-mark)
4377                    ((memq number gnus-newsgroup-replied)
4378                     gnus-replied-mark)
4379                    ((memq number gnus-newsgroup-forwarded)
4380                     gnus-forwarded-mark)
4381                    ((memq number gnus-newsgroup-saved)
4382                     gnus-saved-mark)
4383                    ((memq number gnus-newsgroup-recent)
4384                     gnus-recent-mark)
4385                    ((memq number gnus-newsgroup-unseen)
4386                     gnus-unseen-mark)
4387                    (t gnus-no-mark))
4388              gnus-tmp-from (mail-header-from gnus-tmp-header)
4389              gnus-tmp-name
4390              (cond
4391               ((string-match "<[^>]+> *$" gnus-tmp-from)
4392                (setq beg-match (match-beginning 0))
4393                (or (and (string-match "^\".+\"" gnus-tmp-from)
4394                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4395                    (substring gnus-tmp-from 0 beg-match)))
4396               ((string-match "(.+)" gnus-tmp-from)
4397                (substring gnus-tmp-from
4398                           (1+ (match-beginning 0)) (1- (match-end 0))))
4399               (t gnus-tmp-from))
4400              gnus-tmp-thread-tree-header-string
4401              (cond
4402               ((not gnus-show-threads) "")
4403               ((zerop gnus-tmp-level)
4404                (if (cdar thread)
4405                    (or gnus-sum-thread-tree-root subject)
4406                  (or gnus-sum-thread-tree-single-indent subject)))
4407               (t
4408                (concat (apply 'concat
4409                               (mapcar (lambda (item)
4410                                         (if (= item 1)
4411                                             gnus-sum-thread-tree-vertical
4412                                           gnus-sum-thread-tree-indent))
4413                                       (cdr (reverse tree-stack))))
4414                        (if (nth 1 thread)
4415                            gnus-sum-thread-tree-leaf-with-other
4416                          gnus-sum-thread-tree-single-leaf)))))
4417             (when (string= gnus-tmp-name "")
4418               (setq gnus-tmp-name gnus-tmp-from))
4419             (unless (numberp gnus-tmp-lines)
4420               (setq gnus-tmp-lines -1))
4421             (if (= gnus-tmp-lines -1)
4422                 (setq gnus-tmp-lines "?")
4423               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4424             (gnus-put-text-property
4425              (point)
4426              (progn (eval gnus-summary-line-format-spec) (point))
4427              'gnus-number number)
4428             (when gnus-visual-p
4429               (forward-line -1)
4430               (gnus-run-hooks 'gnus-summary-update-hook)
4431               (forward-line 1))
4432
4433             (setq gnus-tmp-prev-subject subject)))
4434
4435         (when (nth 1 thread)
4436           (push (list (max 0 gnus-tmp-level)
4437                       (copy-list tree-stack)
4438                       (nthcdr 1 thread))
4439                 stack))
4440         (push (if (nth 1 thread) 1 0) tree-stack)
4441         (incf gnus-tmp-level)
4442         (setq threads (if thread-end nil (cdar thread)))
4443         (unless threads
4444           (setq gnus-tmp-level 0)))))
4445   (gnus-message 7 "Generating summary...done"))
4446
4447 (defun gnus-summary-prepare-unthreaded (headers)
4448   "Generate an unthreaded summary buffer based on HEADERS."
4449   (let (header number mark)
4450
4451     (beginning-of-line)
4452
4453     (while headers
4454       ;; We may have to root out some bad articles...
4455       (when (memq (setq number (mail-header-number
4456                                 (setq header (pop headers))))
4457                   gnus-newsgroup-limit)
4458         ;; Mark article as read when it has a low score.
4459         (when (and gnus-summary-mark-below
4460                    (< (or (cdr (assq number gnus-newsgroup-scored))
4461                           gnus-summary-default-score 0)
4462                       gnus-summary-mark-below)
4463                    (not (gnus-summary-article-ancient-p number)))
4464           (setq gnus-newsgroup-unreads
4465                 (delq number gnus-newsgroup-unreads))
4466           (if gnus-newsgroup-auto-expire
4467               (push number gnus-newsgroup-expirable)
4468             (push (cons number gnus-low-score-mark)
4469                   gnus-newsgroup-reads)))
4470
4471         (setq mark (gnus-article-mark number))
4472         (push (gnus-data-make number mark (1+ (point)) header 0)
4473               gnus-newsgroup-data)
4474         (gnus-summary-insert-line
4475          header 0 number
4476          mark (memq number gnus-newsgroup-replied)
4477          (memq number gnus-newsgroup-expirable)
4478          (mail-header-subject header) nil
4479          (cdr (assq number gnus-newsgroup-scored))
4480          (memq number gnus-newsgroup-processable))))))
4481
4482 (defun gnus-summary-remove-list-identifiers ()
4483   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4484   (let ((regexp (if (consp gnus-list-identifiers)
4485                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4486                   gnus-list-identifiers))
4487         changed subject)
4488     (when regexp
4489       (dolist (header gnus-newsgroup-headers)
4490         (setq subject (mail-header-subject header)
4491               changed nil)
4492         (while (string-match
4493                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4494                 subject)
4495           (setq subject
4496                 (concat (substring subject 0 (match-beginning 2))
4497                         (substring subject (match-end 0)))
4498                 changed t))
4499         (when (and changed
4500                    (string-match
4501                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4502           (setq subject
4503                 (concat (substring subject 0 (match-beginning 1))
4504                         (substring subject (match-end 1)))))
4505         (when changed
4506           (mail-header-set-subject header subject))))))
4507
4508 (defun gnus-fetch-headers (articles)
4509   "Fetch headers of ARTICLES."
4510   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4511     (gnus-message 5 "Fetching headers for %s..." name)
4512     (prog1
4513         (if (eq 'nov
4514                 (setq gnus-headers-retrieved-by
4515                       (gnus-retrieve-headers
4516                        articles gnus-newsgroup-name
4517                        ;; We might want to fetch old headers, but
4518                        ;; not if there is only 1 article.
4519                        (and (or (and
4520                                  (not (eq gnus-fetch-old-headers 'some))
4521                                  (not (numberp gnus-fetch-old-headers)))
4522                                 (> (length articles) 1))
4523                             gnus-fetch-old-headers))))
4524             (gnus-get-newsgroup-headers-xover
4525              articles nil nil gnus-newsgroup-name t)
4526           (gnus-get-newsgroup-headers))
4527       (gnus-message 5 "Fetching headers for %s...done" name))))
4528
4529 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4530   "Select newsgroup GROUP.
4531 If READ-ALL is non-nil, all articles in the group are selected.
4532 If SELECT-ARTICLES, only select those articles from GROUP."
4533   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4534          ;;!!! Dirty hack; should be removed.
4535          (gnus-summary-ignore-duplicates
4536           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4537               t
4538             gnus-summary-ignore-duplicates))
4539          (info (nth 2 entry))
4540          articles fetched-articles cached)
4541
4542     (unless (gnus-check-server
4543              (setq gnus-current-select-method
4544                    (gnus-find-method-for-group group)))
4545       (error "Couldn't open server"))
4546
4547     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4548         (gnus-activate-group group)     ; Or we can activate it...
4549         (progn                          ; Or we bug out.
4550           (when (equal major-mode 'gnus-summary-mode)
4551             (kill-buffer (current-buffer)))
4552           (error "Couldn't activate group %s: %s"
4553                  group (gnus-status-message group))))
4554
4555     (unless (gnus-request-group group t)
4556       (when (equal major-mode 'gnus-summary-mode)
4557         (kill-buffer (current-buffer)))
4558       (error "Couldn't request group %s: %s"
4559              group (gnus-status-message group)))
4560
4561     (setq gnus-newsgroup-name group
4562           gnus-newsgroup-unselected nil
4563           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4564
4565     (let ((display (gnus-group-find-parameter group 'display)))
4566       (setq gnus-newsgroup-display
4567             (cond
4568              ((eq display 'all)
4569               'gnus-not-ignore)
4570              ((arrayp display)
4571               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4572              (t
4573               nil))))
4574
4575     (gnus-summary-setup-default-charset)
4576
4577     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4578     (when (gnus-virtual-group-p group)
4579       (setq cached gnus-newsgroup-cached))
4580
4581     (setq gnus-newsgroup-unreads
4582           (gnus-set-difference
4583            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4584            gnus-newsgroup-dormant))
4585
4586     (setq gnus-newsgroup-processable nil)
4587
4588     (gnus-update-read-articles group gnus-newsgroup-unreads)
4589
4590     ;; Adjust and set lists of article marks.
4591     (when info
4592       (gnus-adjust-marked-articles info))
4593
4594     (if (setq articles select-articles)
4595         (setq gnus-newsgroup-unselected
4596               (gnus-sorted-intersection
4597                gnus-newsgroup-unreads
4598                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4599       (setq articles (gnus-articles-to-read group read-all)))
4600
4601     (cond
4602      ((null articles)
4603       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4604       'quit)
4605      ((eq articles 0) nil)
4606      (t
4607       ;; Init the dependencies hash table.
4608       (setq gnus-newsgroup-dependencies
4609             (gnus-make-hashtable (length articles)))
4610       (gnus-set-global-variables)
4611       ;; Retrieve the headers and read them in.
4612       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4613
4614       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4615       (when cached
4616         (setq gnus-newsgroup-cached cached))
4617
4618       ;; Suppress duplicates?
4619       (when gnus-suppress-duplicates
4620         (gnus-dup-suppress-articles))
4621
4622       ;; Set the initial limit.
4623       (setq gnus-newsgroup-limit (copy-sequence articles))
4624       ;; Remove canceled articles from the list of unread articles.
4625       (setq fetched-articles
4626             (mapcar (lambda (headers) (mail-header-number headers))
4627                     gnus-newsgroup-headers))
4628       (setq gnus-newsgroup-articles fetched-articles)
4629       (setq gnus-newsgroup-unreads
4630             (gnus-set-sorted-intersection
4631              gnus-newsgroup-unreads fetched-articles))
4632
4633       (let ((marks (assq 'seen (gnus-info-marks info))))
4634         ;; The `seen' marks are treated specially.
4635         (when (setq gnus-newsgroup-seen (cdr marks))
4636           (dolist (article gnus-newsgroup-articles)
4637             (unless (gnus-member-of-range
4638                      article gnus-newsgroup-seen)
4639               (push article gnus-newsgroup-unseen)))))
4640
4641       ;; Removed marked articles that do not exist.
4642       (gnus-update-missing-marks
4643        (gnus-sorted-complement fetched-articles articles))
4644       ;; We might want to build some more threads first.
4645       (when (and gnus-fetch-old-headers
4646                  (eq gnus-headers-retrieved-by 'nov))
4647         (if (eq gnus-fetch-old-headers 'invisible)
4648             (gnus-build-all-threads)
4649           (gnus-build-old-threads)))
4650       ;; Let the Gnus agent mark articles as read.
4651       (when gnus-agent
4652         (gnus-agent-get-undownloaded-list))
4653       ;; Remove list identifiers from subject
4654       (when gnus-list-identifiers
4655         (gnus-summary-remove-list-identifiers))
4656       ;; Check whether auto-expire is to be done in this group.
4657       (setq gnus-newsgroup-auto-expire
4658             (gnus-group-auto-expirable-p group))
4659       ;; Set up the article buffer now, if necessary.
4660       (unless gnus-single-article-buffer
4661         (gnus-article-setup-buffer))
4662       ;; First and last article in this newsgroup.
4663       (when gnus-newsgroup-headers
4664         (setq gnus-newsgroup-begin
4665               (mail-header-number (car gnus-newsgroup-headers))
4666               gnus-newsgroup-end
4667               (mail-header-number
4668                (gnus-last-element gnus-newsgroup-headers))))
4669       ;; GROUP is successfully selected.
4670       (or gnus-newsgroup-headers t)))))
4671
4672 (defun gnus-summary-display-make-predicate (display)
4673   (require 'gnus-agent)
4674   (when (= (length display) 1)
4675     (setq display (car display)))
4676   (unless gnus-summary-display-cache
4677     (dolist (elem (append (list (cons 'read 'read)
4678                                 (cons 'unseen 'unseen))
4679                           gnus-article-mark-lists))
4680       (push (cons (cdr elem)
4681                   (gnus-byte-compile
4682                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4683             gnus-summary-display-cache)))
4684   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4685     (gnus-get-predicate display)))
4686
4687 ;; Uses the dynamically bound `number' variable.
4688 (defvar number)
4689 (defun gnus-article-marked-p (type &optional article)
4690   (let ((article (or article number)))
4691     (cond
4692      ((eq type 'tick)
4693       (memq article gnus-newsgroup-marked))
4694      ((eq type 'unsend)
4695       (memq article gnus-newsgroup-unsendable))
4696      ((eq type 'undownload)
4697       (memq article gnus-newsgroup-undownloaded))
4698      ((eq type 'download)
4699       (memq article gnus-newsgroup-downloadable))
4700      ((eq type 'unread)
4701       (memq article gnus-newsgroup-unreads))
4702      ((eq type 'read)
4703       (memq article gnus-newsgroup-reads))
4704      ((eq type 'dormant)
4705       (memq article gnus-newsgroup-dormant) )
4706      ((eq type 'expire)
4707       (memq article gnus-newsgroup-expirable))
4708      ((eq type 'reply)
4709       (memq article gnus-newsgroup-replied))
4710      ((eq type 'killed)
4711       (memq article gnus-newsgroup-killed))
4712      ((eq type 'bookmark)
4713       (assq article gnus-newsgroup-bookmarks))
4714      ((eq type 'score)
4715       (assq article gnus-newsgroup-scored))
4716      ((eq type 'save)
4717       (memq article gnus-newsgroup-saved))
4718      ((eq type 'cache)
4719       (memq article gnus-newsgroup-cached))
4720      ((eq type 'forward)
4721       (memq article gnus-newsgroup-forwarded))
4722      ((eq type 'seen)
4723       (not (memq article gnus-newsgroup-unseen)))
4724      ((eq type 'recent)
4725       (memq article gnus-newsgroup-recent))
4726      (t t))))
4727
4728 (defun gnus-articles-to-read (group &optional read-all)
4729   "Find out what articles the user wants to read."
4730   (let* ((articles
4731           ;; Select all articles if `read-all' is non-nil, or if there
4732           ;; are no unread articles.
4733           (if (or read-all
4734                   (and (zerop (length gnus-newsgroup-marked))
4735                        (zerop (length gnus-newsgroup-unreads)))
4736                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4737               ;; We want to select the headers for all the articles in
4738               ;; the group, so we select either all the active
4739               ;; articles in the group, or (if that's nil), the
4740               ;; articles in the cache.
4741               (or
4742                (gnus-uncompress-range (gnus-active group))
4743                (gnus-cache-articles-in-group group))
4744             ;; Select only the "normal" subset of articles.
4745             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4746                           (copy-sequence gnus-newsgroup-unreads))
4747                   '<)))
4748          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4749          (scored (length scored-list))
4750          (number (length articles))
4751          (marked (+ (length gnus-newsgroup-marked)
4752                     (length gnus-newsgroup-dormant)))
4753          (select
4754           (cond
4755            ((numberp read-all)
4756             read-all)
4757            (t
4758             (condition-case ()
4759                 (cond
4760                  ((and (or (<= scored marked) (= scored number))
4761                        (natnump gnus-large-newsgroup)
4762                        (> number gnus-large-newsgroup))
4763                   (let* ((cursor-in-echo-area nil)
4764                          (input
4765                           (read-from-minibuffer
4766                            (format
4767                             "How many articles from %s (max %d): "
4768                             (gnus-limit-string
4769                              (gnus-group-decoded-name gnus-newsgroup-name)
4770                              35)
4771                             number)
4772                            (cons (number-to-string gnus-large-newsgroup)
4773                                  0))))
4774                     (if (string-match "^[ \t]*$" input)
4775                         number
4776                       input)))
4777                  ((and (> scored marked) (< scored number)
4778                        (> (- scored number) 20))
4779                   (let ((input
4780                          (read-string
4781                           (format "%s %s (%d scored, %d total): "
4782                                   "How many articles from"
4783                                   (gnus-group-decoded-name group)
4784                                   scored number))))
4785                     (if (string-match "^[ \t]*$" input)
4786                         number input)))
4787                  (t number))
4788               (quit
4789                (message "Quit getting the articles to read")
4790                nil))))))
4791     (setq select (if (stringp select) (string-to-number select) select))
4792     (if (or (null select) (zerop select))
4793         select
4794       (if (and (not (zerop scored)) (<= (abs select) scored))
4795           (progn
4796             (setq articles (sort scored-list '<))
4797             (setq number (length articles)))
4798         (setq articles (copy-sequence articles)))
4799
4800       (when (< (abs select) number)
4801         (if (< select 0)
4802             ;; Select the N oldest articles.
4803             (setcdr (nthcdr (1- (abs select)) articles) nil)
4804           ;; Select the N most recent articles.
4805           (setq articles (nthcdr (- number select) articles))))
4806       (setq gnus-newsgroup-unselected
4807             (gnus-sorted-intersection
4808              gnus-newsgroup-unreads
4809              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4810       (when gnus-alter-articles-to-read-function
4811         (setq gnus-newsgroup-unreads
4812               (sort
4813                (funcall gnus-alter-articles-to-read-function
4814                         gnus-newsgroup-name gnus-newsgroup-unreads)
4815                '<)))
4816       articles)))
4817
4818 (defun gnus-killed-articles (killed articles)
4819   (let (out)
4820     (while articles
4821       (when (inline (gnus-member-of-range (car articles) killed))
4822         (push (car articles) out))
4823       (setq articles (cdr articles)))
4824     out))
4825
4826 (defun gnus-uncompress-marks (marks)
4827   "Uncompress the mark ranges in MARKS."
4828   (let ((uncompressed '(score bookmark))
4829         out)
4830     (while marks
4831       (if (memq (caar marks) uncompressed)
4832           (push (car marks) out)
4833         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4834       (setq marks (cdr marks)))
4835     out))
4836
4837 (defun gnus-article-mark-to-type (mark)
4838   "Return the type of MARK."
4839   (or (cadr (assq mark gnus-article-special-mark-lists))
4840       'list))
4841
4842 (defun gnus-adjust-marked-articles (info)
4843   "Set all article lists and remove all marks that are no longer valid."
4844   (let* ((marked-lists (gnus-info-marks info))
4845          (active (gnus-active (gnus-info-group info)))
4846          (min (car active))
4847          (max (cdr active))
4848          (types gnus-article-mark-lists)
4849          marks var articles article mark mark-type)
4850
4851     (dolist (marks marked-lists)
4852       (setq mark (car marks)
4853             mark-type (gnus-article-mark-to-type mark)
4854             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4855
4856       ;; We set the variable according to the type of the marks list,
4857       ;; and then adjust the marks to a subset of the active articles.
4858       (cond
4859        ;; Adjust "simple" lists.
4860        ((eq mark-type 'list)
4861         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4862         (when (memq mark '(tick dormant expire reply save))
4863           (while articles
4864             (when (or (< (setq article (pop articles)) min) (> article max))
4865               (set var (delq article (symbol-value var)))))))
4866        ;; Adjust assocs.
4867        ((eq mark-type 'tuple)
4868         (set var (setq articles (cdr marks)))
4869         (when (not (listp (cdr (symbol-value var))))
4870           (set var (list (symbol-value var))))
4871         (when (not (listp (cdr articles)))
4872           (setq articles (list articles)))
4873         (while articles
4874           (when (or (not (consp (setq article (pop articles))))
4875                     (< (car article) min)
4876                     (> (car article) max))
4877             (set var (delq article (symbol-value var))))))
4878        ((eq mark-type 'range)
4879         (cond
4880          ((eq mark 'seen))))))))
4881
4882 (defun gnus-update-missing-marks (missing)
4883   "Go through the list of MISSING articles and remove them from the mark lists."
4884   (when missing
4885     (let (var m)
4886       ;; Go through all types.
4887       (dolist (elem gnus-article-mark-lists)
4888         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4889           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4890           (when (symbol-value var)
4891             ;; This list has articles.  So we delete all missing
4892             ;; articles from it.
4893             (setq m missing)
4894             (while m
4895               (set var (delq (pop m) (symbol-value var))))))))))
4896
4897 (defun gnus-update-marks ()
4898   "Enter the various lists of marked articles into the newsgroup info list."
4899   (let ((types gnus-article-mark-lists)
4900         (info (gnus-get-info gnus-newsgroup-name))
4901         (uncompressed '(score bookmark killed seen))
4902         type list newmarked symbol delta-marks)
4903     (when info
4904       ;; Add all marks lists to the list of marks lists.
4905       (while (setq type (pop types))
4906         (setq list (symbol-value
4907                     (setq symbol
4908                           (intern (format "gnus-newsgroup-%s" (car type))))))
4909
4910         (when list
4911           ;; Get rid of the entries of the articles that have the
4912           ;; default score.
4913           (when (and (eq (cdr type) 'score)
4914                      gnus-save-score
4915                      list)
4916             (let* ((arts list)
4917                    (prev (cons nil list))
4918                    (all prev))
4919               (while arts
4920                 (if (or (not (consp (car arts)))
4921                         (= (cdar arts) gnus-summary-default-score))
4922                     (setcdr prev (cdr arts))
4923                   (setq prev arts))
4924                 (setq arts (cdr arts)))
4925               (setq list (cdr all)))))
4926
4927         (when (eq (cdr type) 'seen)
4928           (setq list
4929                 (if list
4930                     (gnus-add-to-range list gnus-newsgroup-unseen)
4931                   (gnus-compress-sequence gnus-newsgroup-articles))))
4932
4933         (unless (memq (cdr type) uncompressed)
4934           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4935
4936         (when (gnus-check-backend-function
4937                'request-set-mark gnus-newsgroup-name)
4938           ;; propagate flags to server, with the following exceptions:
4939           ;; uncompressed:s are not proper flags (they are cons cells)
4940           ;; cache is a internal gnus flag
4941           ;; download are local to one gnus installation (well)
4942           ;; unsend are for nndraft groups only
4943           ;; xxx: generality of this?  this suits nnimap anyway
4944           (unless (memq (cdr type) (append '(cache download unsend)
4945                                            uncompressed))
4946             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4947                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4948                    (add (gnus-remove-from-range
4949                          (gnus-copy-sequence list) old)))
4950               (when add
4951                 (push (list add 'add (list (cdr type))) delta-marks))
4952               (when del
4953                 (push (list del 'del (list (cdr type))) delta-marks)))))
4954
4955         (when list
4956           (push (cons (cdr type) list) newmarked)))
4957
4958       (when delta-marks
4959         (unless (gnus-check-group gnus-newsgroup-name)
4960           (error "Can't open server for %s" gnus-newsgroup-name))
4961         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4962
4963       ;; Enter these new marks into the info of the group.
4964       (if (nthcdr 3 info)
4965           (setcar (nthcdr 3 info) newmarked)
4966         ;; Add the marks lists to the end of the info.
4967         (when newmarked
4968           (setcdr (nthcdr 2 info) (list newmarked))))
4969
4970       ;; Cut off the end of the info if there's nothing else there.
4971       (let ((i 5))
4972         (while (and (> i 2)
4973                     (not (nth i info)))
4974           (when (nthcdr (decf i) info)
4975             (setcdr (nthcdr i info) nil)))))))
4976
4977 (defun gnus-set-mode-line (where)
4978   "Set the mode line of the article or summary buffers.
4979 If WHERE is `summary', the summary mode line format will be used."
4980   ;; Is this mode line one we keep updated?
4981   (when (and (memq where gnus-updated-mode-lines)
4982              (symbol-value
4983               (intern (format "gnus-%s-mode-line-format-spec" where))))
4984     (let (mode-string)
4985       (save-excursion
4986         ;; We evaluate this in the summary buffer since these
4987         ;; variables are buffer-local to that buffer.
4988         (set-buffer gnus-summary-buffer)
4989         ;; We bind all these variables that are used in the `eval' form
4990         ;; below.
4991         (let* ((mformat (symbol-value
4992                          (intern
4993                           (format "gnus-%s-mode-line-format-spec" where))))
4994                (gnus-tmp-group-name (gnus-group-decoded-name
4995                                      gnus-newsgroup-name))
4996                (gnus-tmp-article-number (or gnus-current-article 0))
4997                (gnus-tmp-unread gnus-newsgroup-unreads)
4998                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4999                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5000                (gnus-tmp-unread-and-unselected
5001                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5002                             (zerop gnus-tmp-unselected))
5003                        "")
5004                       ((zerop gnus-tmp-unselected)
5005                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5006                       (t (format "{%d(+%d) more}"
5007                                  gnus-tmp-unread-and-unticked
5008                                  gnus-tmp-unselected))))
5009                (gnus-tmp-subject
5010                 (if (and gnus-current-headers
5011                          (vectorp gnus-current-headers))
5012                     (gnus-mode-string-quote
5013                      (mail-header-subject gnus-current-headers))
5014                   ""))
5015                bufname-length max-len
5016                gnus-tmp-header);; passed as argument to any user-format-funcs
5017           (setq mode-string (eval mformat))
5018           (setq bufname-length (if (string-match "%b" mode-string)
5019                                    (- (length
5020                                        (buffer-name
5021                                         (if (eq where 'summary)
5022                                             nil
5023                                           (get-buffer gnus-article-buffer))))
5024                                       2)
5025                                  0))
5026           (setq max-len (max 4 (if gnus-mode-non-string-length
5027                                    (- (window-width)
5028                                       gnus-mode-non-string-length
5029                                       bufname-length)
5030                                  (length mode-string))))
5031           ;; We might have to chop a bit of the string off...
5032           (when (> (length mode-string) max-len)
5033             (setq mode-string
5034                   (concat (gnus-truncate-string mode-string (- max-len 3))
5035                           "...")))
5036           ;; Pad the mode string a bit.
5037           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5038       ;; Update the mode line.
5039       (setq mode-line-buffer-identification
5040             (gnus-mode-line-buffer-identification (list mode-string)))
5041       (set-buffer-modified-p t))))
5042
5043 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5044   "Go through the HEADERS list and add all Xrefs to a hash table.
5045 The resulting hash table is returned, or nil if no Xrefs were found."
5046   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5047          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5048          (xref-hashtb (gnus-make-hashtable))
5049          start group entry number xrefs header)
5050     (while headers
5051       (setq header (pop headers))
5052       (when (and (setq xrefs (mail-header-xref header))
5053                  (not (memq (setq number (mail-header-number header))
5054                             unreads)))
5055         (setq start 0)
5056         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5057           (setq start (match-end 0))
5058           (setq group (if prefix
5059                           (concat prefix (substring xrefs (match-beginning 1)
5060                                                     (match-end 1)))
5061                         (substring xrefs (match-beginning 1) (match-end 1))))
5062           (setq number
5063                 (string-to-int (substring xrefs (match-beginning 2)
5064                                           (match-end 2))))
5065           (if (setq entry (gnus-gethash group xref-hashtb))
5066               (setcdr entry (cons number (cdr entry)))
5067             (gnus-sethash group (cons number nil) xref-hashtb)))))
5068     (and start xref-hashtb)))
5069
5070 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5071   "Look through all the headers and mark the Xrefs as read."
5072   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5073         name entry info xref-hashtb idlist method nth4)
5074     (save-excursion
5075       (set-buffer gnus-group-buffer)
5076       (when (setq xref-hashtb
5077                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5078         (mapatoms
5079          (lambda (group)
5080            (unless (string= from-newsgroup (setq name (symbol-name group)))
5081              (setq idlist (symbol-value group))
5082              ;; Dead groups are not updated.
5083              (and (prog1
5084                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5085                             info (nth 2 entry))
5086                     (when (stringp (setq nth4 (gnus-info-method info)))
5087                       (setq nth4 (gnus-server-to-method nth4))))
5088                   ;; Only do the xrefs if the group has the same
5089                   ;; select method as the group we have just read.
5090                   (or (gnus-methods-equal-p
5091                        nth4 (gnus-find-method-for-group from-newsgroup))
5092                       virtual
5093                       (equal nth4 (setq method (gnus-find-method-for-group
5094                                                 from-newsgroup)))
5095                       (and (equal (car nth4) (car method))
5096                            (equal (nth 1 nth4) (nth 1 method))))
5097                   gnus-use-cross-reference
5098                   (or (not (eq gnus-use-cross-reference t))
5099                       virtual
5100                       ;; Only do cross-references on subscribed
5101                       ;; groups, if that is what is wanted.
5102                       (<= (gnus-info-level info) gnus-level-subscribed))
5103                   (gnus-group-make-articles-read name idlist))))
5104          xref-hashtb)))))
5105
5106 (defun gnus-compute-read-articles (group articles)
5107   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5108          (info (nth 2 entry))
5109          (active (gnus-active group))
5110          ninfo)
5111     (when entry
5112       ;; First peel off all invalid article numbers.
5113       (when active
5114         (let ((ids articles)
5115               id first)
5116           (while (setq id (pop ids))
5117             (when (and first (> id (cdr active)))
5118               ;; We'll end up in this situation in one particular
5119               ;; obscure situation.  If you re-scan a group and get
5120               ;; a new article that is cross-posted to a different
5121               ;; group that has not been re-scanned, you might get
5122               ;; crossposted article that has a higher number than
5123               ;; Gnus believes possible.  So we re-activate this
5124               ;; group as well.  This might mean doing the
5125               ;; crossposting thingy will *increase* the number
5126               ;; of articles in some groups.  Tsk, tsk.
5127               (setq active (or (gnus-activate-group group) active)))
5128             (when (or (> id (cdr active))
5129                       (< id (car active)))
5130               (setq articles (delq id articles))))))
5131       ;; If the read list is nil, we init it.
5132       (if (and active
5133                (null (gnus-info-read info))
5134                (> (car active) 1))
5135           (setq ninfo (cons 1 (1- (car active))))
5136         (setq ninfo (gnus-info-read info)))
5137       ;; Then we add the read articles to the range.
5138       (gnus-add-to-range
5139        ninfo (setq articles (sort articles '<))))))
5140
5141 (defun gnus-group-make-articles-read (group articles)
5142   "Update the info of GROUP to say that ARTICLES are read."
5143   (let* ((num 0)
5144          (entry (gnus-gethash group gnus-newsrc-hashtb))
5145          (info (nth 2 entry))
5146          (active (gnus-active group))
5147          range)
5148     (when entry
5149       (setq range (gnus-compute-read-articles group articles))
5150       (save-excursion
5151         (set-buffer gnus-group-buffer)
5152         (gnus-undo-register
5153           `(progn
5154              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5155              (gnus-info-set-read ',info ',(gnus-info-read info))
5156              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5157              (gnus-group-update-group ,group t))))
5158       ;; Add the read articles to the range.
5159       (gnus-info-set-read info range)
5160       (gnus-request-set-mark group (list (list range 'add '(read))))
5161       ;; Then we have to re-compute how many unread
5162       ;; articles there are in this group.
5163       (when active
5164         (cond
5165          ((not range)
5166           (setq num (- (1+ (cdr active)) (car active))))
5167          ((not (listp (cdr range)))
5168           (setq num (- (cdr active) (- (1+ (cdr range))
5169                                        (car range)))))
5170          (t
5171           (while range
5172             (if (numberp (car range))
5173                 (setq num (1+ num))
5174               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5175             (setq range (cdr range)))
5176           (setq num (- (cdr active) num))))
5177         ;; Update the number of unread articles.
5178         (setcar entry num)
5179         ;; Update the group buffer.
5180         (gnus-group-update-group group t)))))
5181
5182 (defvar gnus-newsgroup-none-id 0)
5183
5184 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5185   (let ((cur nntp-server-buffer)
5186         (dependencies
5187          (or dependencies
5188              (save-excursion (set-buffer gnus-summary-buffer)
5189                              gnus-newsgroup-dependencies)))
5190         headers id end ref
5191         (mail-parse-charset gnus-newsgroup-charset)
5192         (mail-parse-ignored-charsets
5193          (save-excursion (condition-case nil
5194                              (set-buffer gnus-summary-buffer)
5195                            (error))
5196                          gnus-newsgroup-ignored-charsets)))
5197     (save-excursion
5198       (set-buffer nntp-server-buffer)
5199       ;; Translate all TAB characters into SPACE characters.
5200       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5201       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5202       (gnus-run-hooks 'gnus-parse-headers-hook)
5203       (let ((case-fold-search t)
5204             in-reply-to header p lines chars ctype)
5205         (goto-char (point-min))
5206         ;; Search to the beginning of the next header.  Error messages
5207         ;; do not begin with 2 or 3.
5208         (while (re-search-forward "^[23][0-9]+ " nil t)
5209           (setq id nil
5210                 ref nil)
5211           ;; This implementation of this function, with nine
5212           ;; search-forwards instead of the one re-search-forward and
5213           ;; a case (which basically was the old function) is actually
5214           ;; about twice as fast, even though it looks messier.  You
5215           ;; can't have everything, I guess.  Speed and elegance
5216           ;; doesn't always go hand in hand.
5217           (setq
5218            header
5219            (make-full-mail-header
5220             ;; Number.
5221             (prog1
5222                 (read cur)
5223               (end-of-line)
5224               (setq p (point))
5225               (narrow-to-region (point)
5226                                 (or (and (search-forward "\n.\n" nil t)
5227                                          (- (point) 2))
5228                                     (point))))
5229             ;; Subject.
5230             (progn
5231               (goto-char p)
5232               (if (search-forward "\nsubject:" nil t)
5233                   (nnheader-header-value)
5234                 "(none)"))
5235             ;; From.
5236             (progn
5237               (goto-char p)
5238               (if (search-forward "\nfrom:" nil t)
5239                   (nnheader-header-value)
5240                 "(nobody)"))
5241             ;; Date.
5242             (progn
5243               (goto-char p)
5244               (if (search-forward "\ndate:" nil t)
5245                   (nnheader-header-value) ""))
5246             ;; Message-ID.
5247             (progn
5248               (goto-char p)
5249               (setq id (if (re-search-forward
5250                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5251                            ;; We do it this way to make sure the Message-ID
5252                            ;; is (somewhat) syntactically valid.
5253                            (buffer-substring (match-beginning 1)
5254                                              (match-end 1))
5255                          ;; If there was no message-id, we just fake one
5256                          ;; to make subsequent routines simpler.
5257                          (nnheader-generate-fake-message-id))))
5258             ;; References.
5259             (progn
5260               (goto-char p)
5261               (if (search-forward "\nreferences:" nil t)
5262                   (progn
5263                     (setq end (point))
5264                     (prog1
5265                         (nnheader-header-value)
5266                       (setq ref
5267                             (buffer-substring
5268                              (progn
5269                                ;; (end-of-line)
5270                                (search-backward ">" end t)
5271                                (1+ (point)))
5272                              (progn
5273                                (search-backward "<" end t)
5274                                (point))))))
5275                 ;; Get the references from the in-reply-to header if there
5276                 ;; were no references and the in-reply-to header looks
5277                 ;; promising.
5278                 (if (and (search-forward "\nin-reply-to:" nil t)
5279                          (setq in-reply-to (nnheader-header-value))
5280                          (string-match "<[^>]+>" in-reply-to))
5281                     (let (ref2)
5282                       (setq ref (substring in-reply-to (match-beginning 0)
5283                                            (match-end 0)))
5284                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5285                         (setq ref2 (substring in-reply-to (match-beginning 0)
5286                                               (match-end 0)))
5287                         (when (> (length ref2) (length ref))
5288                           (setq ref ref2)))
5289                       ref)
5290                   (setq ref nil))))
5291             ;; Chars.
5292             (progn
5293               (goto-char p)
5294               (if (search-forward "\nchars: " nil t)
5295                   (if (numberp (setq chars (ignore-errors (read cur))))
5296                       chars -1)
5297                 -1))
5298             ;; Lines.
5299             (progn
5300               (goto-char p)
5301               (if (search-forward "\nlines: " nil t)
5302                   (if (numberp (setq lines (ignore-errors (read cur))))
5303                       lines -1)
5304                 -1))
5305             ;; Xref.
5306             (progn
5307               (goto-char p)
5308               (and (search-forward "\nxref:" nil t)
5309                    (nnheader-header-value)))
5310             ;; Extra.
5311             (when gnus-extra-headers
5312               (let ((extra gnus-extra-headers)
5313                     out)
5314                 (while extra
5315                   (goto-char p)
5316                   (when (search-forward
5317                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5318                     (push (cons (car extra) (nnheader-header-value)) out))
5319                   (pop extra))
5320                 out))))
5321           (goto-char p)
5322           (if (and (search-forward "\ncontent-type: " nil t)
5323                    (setq ctype (nnheader-header-value)))
5324               (mime-entity-set-content-type-internal
5325                header (mime-parse-Content-Type ctype)))
5326           (when (equal id ref)
5327             (setq ref nil))
5328
5329           (when gnus-alter-header-function
5330             (funcall gnus-alter-header-function header)
5331             (setq id (mail-header-id header)
5332                   ref (gnus-parent-id (mail-header-references header))))
5333
5334           (when (setq header
5335                       (gnus-dependencies-add-header
5336                        header dependencies force-new))
5337             (push header headers))
5338           (goto-char (point-max))
5339           (widen))
5340         (nreverse headers)))))
5341
5342 ;; Goes through the xover lines and returns a list of vectors
5343 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5344                                                   force-new dependencies
5345                                                   group also-fetch-heads)
5346   "Parse the news overview data in the server buffer.
5347 Return a list of headers that match SEQUENCE (see
5348 `nntp-retrieve-headers')."
5349   ;; Get the Xref when the users reads the articles since most/some
5350   ;; NNTP servers do not include Xrefs when using XOVER.
5351   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5352   (let ((mail-parse-charset gnus-newsgroup-charset)
5353         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5354         (cur nntp-server-buffer)
5355         (dependencies (or dependencies gnus-newsgroup-dependencies))
5356         (allp (cond
5357                ((eq gnus-read-all-available-headers t)
5358                 t)
5359                ((stringp gnus-read-all-available-headers)
5360                 (string-match gnus-read-all-available-headers group))
5361                (t
5362                 nil)))
5363         number headers header)
5364     (save-excursion
5365       (set-buffer nntp-server-buffer)
5366       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5367       ;; Allow the user to mangle the headers before parsing them.
5368       (gnus-run-hooks 'gnus-parse-headers-hook)
5369       (goto-char (point-min))
5370       (while (not (eobp))
5371         (condition-case ()
5372             (while (and (or sequence allp)
5373                         (not (eobp)))
5374               (setq number (read cur))
5375               (when (not allp)
5376                 (while (and sequence
5377                             (< (car sequence) number))
5378                   (setq sequence (cdr sequence))))
5379               (when (and (or allp
5380                              (and sequence
5381                                   (eq number (car sequence))))
5382                          (progn
5383                            (setq sequence (cdr sequence))
5384                            (setq header (inline
5385                                           (gnus-nov-parse-line
5386                                            number dependencies force-new)))))
5387                 (push header headers))
5388               (forward-line 1))
5389           (error
5390            (gnus-error 4 "Strange nov line (%d)"
5391                        (count-lines (point-min) (point)))))
5392         (forward-line 1))
5393       ;; A common bug in inn is that if you have posted an article and
5394       ;; then retrieves the active file, it will answer correctly --
5395       ;; the new article is included.  However, a NOV entry for the
5396       ;; article may not have been generated yet, so this may fail.
5397       ;; We work around this problem by retrieving the last few
5398       ;; headers using HEAD.
5399       (if (or (not also-fetch-heads)
5400               (not sequence))
5401           ;; We (probably) got all the headers.
5402           (nreverse headers)
5403         (let ((gnus-nov-is-evil t))
5404           (nconc
5405            (nreverse headers)
5406            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5407              (gnus-get-newsgroup-headers))))))))
5408
5409 (defun gnus-article-get-xrefs ()
5410   "Fill in the Xref value in `gnus-current-headers', if necessary.
5411 This is meant to be called in `gnus-article-internal-prepare-hook'."
5412   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5413                                  gnus-current-headers)))
5414     (or (not gnus-use-cross-reference)
5415         (not headers)
5416         (and (mail-header-xref headers)
5417              (not (string= (mail-header-xref headers) "")))
5418         (let ((case-fold-search t)
5419               xref)
5420           (save-restriction
5421             (nnheader-narrow-to-headers)
5422             (goto-char (point-min))
5423             (when (or (and (not (eobp))
5424                            (eq (downcase (char-after)) ?x)
5425                            (looking-at "Xref:"))
5426                       (search-forward "\nXref:" nil t))
5427               (goto-char (1+ (match-end 0)))
5428               (setq xref (buffer-substring (point)
5429                                            (progn (end-of-line) (point))))
5430               (mail-header-set-xref headers xref)))))))
5431
5432 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5433   "Find article ID and insert the summary line for that article.
5434 OLD-HEADER can either be a header or a line number to insert
5435 the subject line on."
5436   (let* ((line (and (numberp old-header) old-header))
5437          (old-header (and (vectorp old-header) old-header))
5438          (header (cond ((and old-header use-old-header)
5439                         old-header)
5440                        ((and (numberp id)
5441                              (gnus-number-to-header id))
5442                         (gnus-number-to-header id))
5443                        (t
5444                         (gnus-read-header id))))
5445          (number (and (numberp id) id))
5446          d)
5447     (when header
5448       ;; Rebuild the thread that this article is part of and go to the
5449       ;; article we have fetched.
5450       (when (and (not gnus-show-threads)
5451                  old-header)
5452         (when (and number
5453                    (setq d (gnus-data-find (mail-header-number old-header))))
5454           (goto-char (gnus-data-pos d))
5455           (gnus-data-remove
5456            number
5457            (- (gnus-point-at-bol)
5458               (prog1
5459                   (1+ (gnus-point-at-eol))
5460                 (gnus-delete-line))))))
5461       (when old-header
5462         (mail-header-set-number header (mail-header-number old-header)))
5463       (setq gnus-newsgroup-sparse
5464             (delq (setq number (mail-header-number header))
5465                   gnus-newsgroup-sparse))
5466       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5467       (push number gnus-newsgroup-limit)
5468       (gnus-rebuild-thread (mail-header-id header) line)
5469       (gnus-summary-goto-subject number nil t))
5470     (when (and (numberp number)
5471                (> number 0))
5472       ;; We have to update the boundaries even if we can't fetch the
5473       ;; article if ID is a number -- so that the next `P' or `N'
5474       ;; command will fetch the previous (or next) article even
5475       ;; if the one we tried to fetch this time has been canceled.
5476       (when (> number gnus-newsgroup-end)
5477         (setq gnus-newsgroup-end number))
5478       (when (< number gnus-newsgroup-begin)
5479         (setq gnus-newsgroup-begin number))
5480       (setq gnus-newsgroup-unselected
5481             (delq number gnus-newsgroup-unselected)))
5482     ;; Report back a success?
5483     (and header (mail-header-number header))))
5484
5485 ;;; Process/prefix in the summary buffer
5486
5487 (defun gnus-summary-work-articles (n)
5488   "Return a list of articles to be worked upon.
5489 The prefix argument, the list of process marked articles, and the
5490 current article will be taken into consideration."
5491   (save-excursion
5492     (set-buffer gnus-summary-buffer)
5493     (cond
5494      (n
5495       ;; A numerical prefix has been given.
5496       (setq n (prefix-numeric-value n))
5497       (let ((backward (< n 0))
5498             (n (abs (prefix-numeric-value n)))
5499             articles article)
5500         (save-excursion
5501           (while
5502               (and (> n 0)
5503                    (push (setq article (gnus-summary-article-number))
5504                          articles)
5505                    (if backward
5506                        (gnus-summary-find-prev nil article)
5507                      (gnus-summary-find-next nil article)))
5508             (decf n)))
5509         (nreverse articles)))
5510      ((and (gnus-region-active-p) (mark))
5511       (message "region active")
5512       ;; Work on the region between point and mark.
5513       (let ((max (max (point) (mark)))
5514             articles article)
5515         (save-excursion
5516           (goto-char (min (point) (mark)))
5517           (while
5518               (and
5519                (push (setq article (gnus-summary-article-number)) articles)
5520                (gnus-summary-find-next nil article)
5521                (< (point) max)))
5522           (nreverse articles))))
5523      (gnus-newsgroup-processable
5524       ;; There are process-marked articles present.
5525       ;; Save current state.
5526       (gnus-summary-save-process-mark)
5527       ;; Return the list.
5528       (reverse gnus-newsgroup-processable))
5529      (t
5530       ;; Just return the current article.
5531       (list (gnus-summary-article-number))))))
5532
5533 (defmacro gnus-summary-iterate (arg &rest forms)
5534   "Iterate over the process/prefixed articles and do FORMS.
5535 ARG is the interactive prefix given to the command.  FORMS will be
5536 executed with point over the summary line of the articles."
5537   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5538     `(let ((,articles (gnus-summary-work-articles ,arg)))
5539        (while ,articles
5540          (gnus-summary-goto-subject (car ,articles))
5541          ,@forms
5542          (pop ,articles)))))
5543
5544 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5545 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5546
5547 (defun gnus-summary-save-process-mark ()
5548   "Push the current set of process marked articles on the stack."
5549   (interactive)
5550   (push (copy-sequence gnus-newsgroup-processable)
5551         gnus-newsgroup-process-stack))
5552
5553 (defun gnus-summary-kill-process-mark ()
5554   "Push the current set of process marked articles on the stack and unmark."
5555   (interactive)
5556   (gnus-summary-save-process-mark)
5557   (gnus-summary-unmark-all-processable))
5558
5559 (defun gnus-summary-yank-process-mark ()
5560   "Pop the last process mark state off the stack and restore it."
5561   (interactive)
5562   (unless gnus-newsgroup-process-stack
5563     (error "Empty mark stack"))
5564   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5565
5566 (defun gnus-summary-process-mark-set (set)
5567   "Make SET into the current process marked articles."
5568   (gnus-summary-unmark-all-processable)
5569   (while set
5570     (gnus-summary-set-process-mark (pop set))))
5571
5572 ;;; Searching and stuff
5573
5574 (defun gnus-summary-search-group (&optional backward use-level)
5575   "Search for next unread newsgroup.
5576 If optional argument BACKWARD is non-nil, search backward instead."
5577   (save-excursion
5578     (set-buffer gnus-group-buffer)
5579     (when (gnus-group-search-forward
5580            backward nil (if use-level (gnus-group-group-level) nil))
5581       (gnus-group-group-name))))
5582
5583 (defun gnus-summary-best-group (&optional exclude-group)
5584   "Find the name of the best unread group.
5585 If EXCLUDE-GROUP, do not go to this group."
5586   (save-excursion
5587     (set-buffer gnus-group-buffer)
5588     (save-excursion
5589       (gnus-group-best-unread-group exclude-group))))
5590
5591 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5592   (if backward (gnus-summary-find-prev)
5593     (let* ((dummy (gnus-summary-article-intangible-p))
5594            (article (or article (gnus-summary-article-number)))
5595            (arts (gnus-data-find-list article))
5596            result)
5597       (when (and (not dummy)
5598                  (or (not gnus-summary-check-current)
5599                      (not unread)
5600                      (not (gnus-data-unread-p (car arts)))))
5601         (setq arts (cdr arts)))
5602       (when (setq result
5603                   (if unread
5604                       (progn
5605                         (while arts
5606                           (when (or (and undownloaded
5607                                          (eq gnus-undownloaded-mark
5608                                              (gnus-data-mark (car arts))))
5609                                     (gnus-data-unread-p (car arts)))
5610                             (setq result (car arts)
5611                                   arts nil))
5612                           (setq arts (cdr arts)))
5613                         result)
5614                     (car arts)))
5615         (goto-char (gnus-data-pos result))
5616         (gnus-data-number result)))))
5617
5618 (defun gnus-summary-find-prev (&optional unread article)
5619   (let* ((eobp (eobp))
5620          (article (or article (gnus-summary-article-number)))
5621          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5622          result)
5623     (when (and (not eobp)
5624                (or (not gnus-summary-check-current)
5625                    (not unread)
5626                    (not (gnus-data-unread-p (car arts)))))
5627       (setq arts (cdr arts)))
5628     (when (setq result
5629                 (if unread
5630                     (progn
5631                       (while arts
5632                         (when (gnus-data-unread-p (car arts))
5633                           (setq result (car arts)
5634                                 arts nil))
5635                         (setq arts (cdr arts)))
5636                       result)
5637                   (car arts)))
5638       (goto-char (gnus-data-pos result))
5639       (gnus-data-number result))))
5640
5641 (defun gnus-summary-find-subject (subject &optional unread backward article)
5642   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5643          (article (or article (gnus-summary-article-number)))
5644          (articles (gnus-data-list backward))
5645          (arts (gnus-data-find-list article articles))
5646          result)
5647     (when (or (not gnus-summary-check-current)
5648               (not unread)
5649               (not (gnus-data-unread-p (car arts))))
5650       (setq arts (cdr arts)))
5651     (while arts
5652       (and (or (not unread)
5653                (gnus-data-unread-p (car arts)))
5654            (vectorp (gnus-data-header (car arts)))
5655            (gnus-subject-equal
5656             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5657            (setq result (car arts)
5658                  arts nil))
5659       (setq arts (cdr arts)))
5660     (and result
5661          (goto-char (gnus-data-pos result))
5662          (gnus-data-number result))))
5663
5664 (defun gnus-summary-search-forward (&optional unread subject backward)
5665   "Search forward for an article.
5666 If UNREAD, look for unread articles.  If SUBJECT, look for
5667 articles with that subject.  If BACKWARD, search backward instead."
5668   (cond (subject (gnus-summary-find-subject subject unread backward))
5669         (backward (gnus-summary-find-prev unread))
5670         (t (gnus-summary-find-next unread))))
5671
5672 (defun gnus-recenter (&optional n)
5673   "Center point in window and redisplay frame.
5674 Also do horizontal recentering."
5675   (interactive "P")
5676   (when (and gnus-auto-center-summary
5677              (not (eq gnus-auto-center-summary 'vertical)))
5678     (gnus-horizontal-recenter))
5679   (recenter n))
5680
5681 (defun gnus-summary-recenter ()
5682   "Center point in the summary window.
5683 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5684 displayed, no centering will be performed."
5685   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5686   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5687   (interactive)
5688   (let* ((top (cond ((< (window-height) 4) 0)
5689                     ((< (window-height) 7) 1)
5690                     (t (if (numberp gnus-auto-center-summary)
5691                            gnus-auto-center-summary
5692                          2))))
5693          (height (1- (window-height)))
5694          (bottom (save-excursion (goto-char (point-max))
5695                                  (forward-line (- height))
5696                                  (point)))
5697          (window (get-buffer-window (current-buffer))))
5698     ;; The user has to want it.
5699     (when gnus-auto-center-summary
5700       (when (get-buffer-window gnus-article-buffer)
5701         ;; Only do recentering when the article buffer is displayed,
5702         ;; Set the window start to either `bottom', which is the biggest
5703         ;; possible valid number, or the second line from the top,
5704         ;; whichever is the least.
5705         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5706           (if (> bottom top-pos)
5707               ;; Keep the second line from the top visible
5708               (set-window-start window top-pos t)
5709             ;; Try to keep the bottom line visible; if it's partially
5710             ;; obscured, either scroll one more line to make it fully
5711             ;; visible, or revert to using TOP-POS.
5712             (save-excursion
5713               (goto-char (point-max))
5714               (forward-line -1)
5715               (let ((last-line-start (point)))
5716                 (goto-char bottom)
5717                 (set-window-start window (point) t)
5718                 (when (not (pos-visible-in-window-p last-line-start window))
5719                   (forward-line 1)
5720                   (set-window-start window (min (point) top-pos) t)))))))
5721       ;; Do horizontal recentering while we're at it.
5722       (when (and (get-buffer-window (current-buffer) t)
5723                  (not (eq gnus-auto-center-summary 'vertical)))
5724         (let ((selected (selected-window)))
5725           (select-window (get-buffer-window (current-buffer) t))
5726           (gnus-summary-position-point)
5727           (gnus-horizontal-recenter)
5728           (select-window selected))))))
5729
5730 (defun gnus-summary-jump-to-group (newsgroup)
5731   "Move point to NEWSGROUP in group mode buffer."
5732   ;; Keep update point of group mode buffer if visible.
5733   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5734       (save-window-excursion
5735         ;; Take care of tree window mode.
5736         (when (get-buffer-window gnus-group-buffer)
5737           (pop-to-buffer gnus-group-buffer))
5738         (gnus-group-jump-to-group newsgroup))
5739     (save-excursion
5740       ;; Take care of tree window mode.
5741       (if (get-buffer-window gnus-group-buffer)
5742           (pop-to-buffer gnus-group-buffer)
5743         (set-buffer gnus-group-buffer))
5744       (gnus-group-jump-to-group newsgroup))))
5745
5746 ;; This function returns a list of article numbers based on the
5747 ;; difference between the ranges of read articles in this group and
5748 ;; the range of active articles.
5749 (defun gnus-list-of-unread-articles (group)
5750   (let* ((read (gnus-info-read (gnus-get-info group)))
5751          (active (or (gnus-active group) (gnus-activate-group group)))
5752          (last (cdr active))
5753          first nlast unread)
5754     ;; If none are read, then all are unread.
5755     (if (not read)
5756         (setq first (car active))
5757       ;; If the range of read articles is a single range, then the
5758       ;; first unread article is the article after the last read
5759       ;; article.  Sounds logical, doesn't it?
5760       (if (and (not (listp (cdr read)))
5761                (or (< (car read) (car active))
5762                    (progn (setq read (list read))
5763                           nil)))
5764           (setq first (max (car active) (1+ (cdr read))))
5765         ;; `read' is a list of ranges.
5766         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5767                                   (caar read)))
5768                   1)
5769           (setq first (car active)))
5770         (while read
5771           (when first
5772             (while (< first nlast)
5773               (push first unread)
5774               (setq first (1+ first))))
5775           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5776           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5777           (setq read (cdr read)))))
5778     ;; And add the last unread articles.
5779     (while (<= first last)
5780       (push first unread)
5781       (setq first (1+ first)))
5782     ;; Return the list of unread articles.
5783     (delq 0 (nreverse unread))))
5784
5785 (defun gnus-list-of-read-articles (group)
5786   "Return a list of unread, unticked and non-dormant articles."
5787   (let* ((info (gnus-get-info group))
5788          (marked (gnus-info-marks info))
5789          (active (gnus-active group)))
5790     (and info active
5791          (gnus-set-difference
5792           (gnus-sorted-complement
5793            (gnus-uncompress-range active)
5794            (gnus-list-of-unread-articles group))
5795           (append
5796            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5797            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5798
5799 ;; Various summary commands
5800
5801 (defun gnus-summary-select-article-buffer ()
5802   "Reconfigure windows to show article buffer."
5803   (interactive)
5804   (if (not (gnus-buffer-live-p gnus-article-buffer))
5805       (error "There is no article buffer for this summary buffer")
5806     (gnus-configure-windows 'article)
5807     (select-window (get-buffer-window gnus-article-buffer))))
5808
5809 (defun gnus-summary-universal-argument (arg)
5810   "Perform any operation on all articles that are process/prefixed."
5811   (interactive "P")
5812   (let ((articles (gnus-summary-work-articles arg))
5813         func article)
5814     (if (eq
5815          (setq
5816           func
5817           (key-binding
5818            (read-key-sequence
5819             (substitute-command-keys
5820              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5821          'undefined)
5822         (gnus-error 1 "Undefined key")
5823       (save-excursion
5824         (while articles
5825           (gnus-summary-goto-subject (setq article (pop articles)))
5826           (let (gnus-newsgroup-processable)
5827             (command-execute func))
5828           (gnus-summary-remove-process-mark article)))))
5829   (gnus-summary-position-point))
5830
5831 (defun gnus-summary-toggle-truncation (&optional arg)
5832   "Toggle truncation of summary lines.
5833 With arg, turn line truncation on iff arg is positive."
5834   (interactive "P")
5835   (setq truncate-lines
5836         (if (null arg) (not truncate-lines)
5837           (> (prefix-numeric-value arg) 0)))
5838   (redraw-display))
5839
5840 (defun gnus-summary-reselect-current-group (&optional all rescan)
5841   "Exit and then reselect the current newsgroup.
5842 The prefix argument ALL means to select all articles."
5843   (interactive "P")
5844   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5845     (error "Ephemeral groups can't be reselected"))
5846   (let ((current-subject (gnus-summary-article-number))
5847         (group gnus-newsgroup-name))
5848     (setq gnus-newsgroup-begin nil)
5849     (gnus-summary-exit)
5850     ;; We have to adjust the point of group mode buffer because
5851     ;; point was moved to the next unread newsgroup by exiting.
5852     (gnus-summary-jump-to-group group)
5853     (when rescan
5854       (save-excursion
5855         (save-window-excursion
5856           ;; Don't show group contents.
5857           (set-window-start (selected-window) (point-max))
5858           (gnus-group-get-new-news-this-group 1))))
5859     (gnus-group-read-group all t)
5860     (gnus-summary-goto-subject current-subject nil t)))
5861
5862 (defun gnus-summary-rescan-group (&optional all)
5863   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5864   (interactive "P")
5865   (gnus-summary-reselect-current-group all t))
5866
5867 (defun gnus-summary-update-info (&optional non-destructive)
5868   (save-excursion
5869     (let ((group gnus-newsgroup-name))
5870       (when group
5871         (when gnus-newsgroup-kill-headers
5872           (setq gnus-newsgroup-killed
5873                 (gnus-compress-sequence
5874                  (nconc
5875                   (gnus-set-sorted-intersection
5876                    (gnus-uncompress-range gnus-newsgroup-killed)
5877                    (setq gnus-newsgroup-unselected
5878                          (sort gnus-newsgroup-unselected '<)))
5879                   (setq gnus-newsgroup-unreads
5880                         (sort gnus-newsgroup-unreads '<)))
5881                  t)))
5882         (unless (listp (cdr gnus-newsgroup-killed))
5883           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5884         (let ((headers gnus-newsgroup-headers))
5885           ;; Set the new ranges of read articles.
5886           (save-excursion
5887             (set-buffer gnus-group-buffer)
5888             (gnus-undo-force-boundary))
5889           (gnus-update-read-articles
5890            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5891           ;; Set the current article marks.
5892           (let ((gnus-newsgroup-scored
5893                  (if (and (not gnus-save-score)
5894                           (not non-destructive))
5895                      nil
5896                    gnus-newsgroup-scored)))
5897             (save-excursion
5898               (gnus-update-marks)))
5899           ;; Do the cross-ref thing.
5900           (when gnus-use-cross-reference
5901             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5902           ;; Do not switch windows but change the buffer to work.
5903           (set-buffer gnus-group-buffer)
5904           (unless (gnus-ephemeral-group-p group)
5905             (gnus-group-update-group group)))))))
5906
5907 (defun gnus-summary-save-newsrc (&optional force)
5908   "Save the current number of read/marked articles in the dribble buffer.
5909 The dribble buffer will then be saved.
5910 If FORCE (the prefix), also save the .newsrc file(s)."
5911   (interactive "P")
5912   (gnus-summary-update-info t)
5913   (if force
5914       (gnus-save-newsrc-file)
5915     (gnus-dribble-save)))
5916
5917 (defun gnus-summary-exit (&optional temporary)
5918   "Exit reading current newsgroup, and then return to group selection mode.
5919 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5920   (interactive)
5921   (gnus-set-global-variables)
5922   (gnus-kill-save-kill-buffer)
5923   (gnus-async-halt-prefetch)
5924   (let* ((group gnus-newsgroup-name)
5925          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5926          (mode major-mode)
5927          (group-point nil)
5928          (buf (current-buffer)))
5929     (unless quit-config
5930       ;; Do adaptive scoring, and possibly save score files.
5931       (when gnus-newsgroup-adaptive
5932         (gnus-score-adaptive))
5933       (when gnus-use-scoring
5934         (gnus-score-save)))
5935     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5936     ;; If we have several article buffers, we kill them at exit.
5937     (unless gnus-single-article-buffer
5938       (gnus-kill-buffer gnus-original-article-buffer)
5939       (setq gnus-article-current nil))
5940     (when gnus-use-cache
5941       (gnus-cache-possibly-remove-articles)
5942       (gnus-cache-save-buffers))
5943     (gnus-async-prefetch-remove-group group)
5944     (when gnus-suppress-duplicates
5945       (gnus-dup-enter-articles))
5946     (when gnus-use-trees
5947       (gnus-tree-close group))
5948     (when gnus-use-cache
5949       (gnus-cache-write-active))
5950     ;; Remove entries for this group.
5951     (nnmail-purge-split-history (gnus-group-real-name group))
5952     ;; Make all changes in this group permanent.
5953     (unless quit-config
5954       (gnus-run-hooks 'gnus-exit-group-hook)
5955       (gnus-summary-update-info))
5956     (gnus-close-group group)
5957     ;; Make sure where we were, and go to next newsgroup.
5958     (set-buffer gnus-group-buffer)
5959     (unless quit-config
5960       (gnus-group-jump-to-group group))
5961     (gnus-run-hooks 'gnus-summary-exit-hook)
5962     (unless (or quit-config
5963                 ;; If this group has disappeared from the summary
5964                 ;; buffer, don't skip forwards.
5965                 (not (string= group (gnus-group-group-name))))
5966       (gnus-group-next-unread-group 1))
5967     (setq group-point (point))
5968     (if temporary
5969         nil                             ;Nothing to do.
5970       ;; If we have several article buffers, we kill them at exit.
5971       (unless gnus-single-article-buffer
5972         (gnus-kill-buffer gnus-article-buffer)
5973         (gnus-kill-buffer gnus-original-article-buffer)
5974         (setq gnus-article-current nil))
5975       (set-buffer buf)
5976       (if (not gnus-kill-summary-on-exit)
5977           (progn
5978             (gnus-deaden-summary)
5979             (setq mode nil))
5980         ;; We set all buffer-local variables to nil.  It is unclear why
5981         ;; this is needed, but if we don't, buffer-local variables are
5982         ;; not garbage-collected, it seems.  This would the lead to en
5983         ;; ever-growing Emacs.
5984         (gnus-summary-clear-local-variables)
5985         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5986           (gnus-summary-clear-local-variables))
5987         (when (get-buffer gnus-article-buffer)
5988           (bury-buffer gnus-article-buffer))
5989         ;; We clear the global counterparts of the buffer-local
5990         ;; variables as well, just to be on the safe side.
5991         (set-buffer gnus-group-buffer)
5992         (gnus-summary-clear-local-variables)
5993         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5994           (gnus-summary-clear-local-variables)))
5995       (setq gnus-current-select-method gnus-select-method)
5996       (pop-to-buffer gnus-group-buffer)
5997       (if (not quit-config)
5998           (progn
5999             (goto-char group-point)
6000             (gnus-configure-windows 'group 'force)
6001             (unless (pos-visible-in-window-p)
6002               (forward-line (/ (static-if (featurep 'xemacs)
6003                                    (window-displayed-height)
6004                                  (1- (window-height)))
6005                                -2))
6006               (set-window-start (selected-window) (point))
6007               (goto-char group-point)))
6008         (gnus-handle-ephemeral-exit quit-config))
6009       ;; Return to group mode buffer.
6010       (when (eq mode 'gnus-summary-mode)
6011         (gnus-kill-buffer buf))
6012       ;; Clear the current group name.
6013       (unless quit-config
6014         (setq gnus-newsgroup-name nil)))))
6015
6016 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6017 (defun gnus-summary-exit-no-update (&optional no-questions)
6018   "Quit reading current newsgroup without updating read article info."
6019   (interactive)
6020   (let* ((group gnus-newsgroup-name)
6021          (quit-config (gnus-group-quit-config group)))
6022     (when (or no-questions
6023               gnus-expert-user
6024               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6025       (gnus-async-halt-prefetch)
6026       (mapcar 'funcall
6027               (delq 'gnus-summary-expire-articles
6028                     (copy-sequence gnus-summary-prepare-exit-hook)))
6029       ;; If we have several article buffers, we kill them at exit.
6030       (unless gnus-single-article-buffer
6031         (gnus-kill-buffer gnus-article-buffer)
6032         (gnus-kill-buffer gnus-original-article-buffer)
6033         (setq gnus-article-current nil))
6034       (if (not gnus-kill-summary-on-exit)
6035           (gnus-deaden-summary)
6036         (gnus-close-group group)
6037         (gnus-summary-clear-local-variables)
6038         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6039           (gnus-summary-clear-local-variables))
6040         (set-buffer gnus-group-buffer)
6041         (gnus-summary-clear-local-variables)
6042         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6043           (gnus-summary-clear-local-variables))
6044         (when (get-buffer gnus-summary-buffer)
6045           (kill-buffer gnus-summary-buffer)))
6046       (unless gnus-single-article-buffer
6047         (setq gnus-article-current nil))
6048       (when gnus-use-trees
6049         (gnus-tree-close group))
6050       (gnus-async-prefetch-remove-group group)
6051       (when (get-buffer gnus-article-buffer)
6052         (bury-buffer gnus-article-buffer))
6053       ;; Return to the group buffer.
6054       (gnus-configure-windows 'group 'force)
6055       ;; Clear the current group name.
6056       (setq gnus-newsgroup-name nil)
6057       (when (equal (gnus-group-group-name) group)
6058         (gnus-group-next-unread-group 1))
6059       (when quit-config
6060         (gnus-handle-ephemeral-exit quit-config)))))
6061
6062 (defun gnus-handle-ephemeral-exit (quit-config)
6063   "Handle movement when leaving an ephemeral group.
6064 The state which existed when entering the ephemeral is reset."
6065   (if (not (buffer-name (car quit-config)))
6066       (gnus-configure-windows 'group 'force)
6067     (set-buffer (car quit-config))
6068     (cond ((eq major-mode 'gnus-summary-mode)
6069            (gnus-set-global-variables))
6070           ((eq major-mode 'gnus-article-mode)
6071            (save-excursion
6072              ;; The `gnus-summary-buffer' variable may point
6073              ;; to the old summary buffer when using a single
6074              ;; article buffer.
6075              (unless (gnus-buffer-live-p gnus-summary-buffer)
6076                (set-buffer gnus-group-buffer))
6077              (set-buffer gnus-summary-buffer)
6078              (gnus-set-global-variables))))
6079     (if (or (eq (cdr quit-config) 'article)
6080             (eq (cdr quit-config) 'pick))
6081         (progn
6082           ;; The current article may be from the ephemeral group
6083           ;; thus it is best that we reload this article
6084           (gnus-summary-show-article)
6085           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6086               (gnus-configure-windows 'pick 'force)
6087             (gnus-configure-windows (cdr quit-config) 'force)))
6088       (gnus-configure-windows (cdr quit-config) 'force))
6089     (when (eq major-mode 'gnus-summary-mode)
6090       (gnus-summary-next-subject 1 nil t)
6091       (gnus-summary-recenter)
6092       (gnus-summary-position-point))))
6093
6094 (defun gnus-summary-preview-mime-message ()
6095   "MIME decode and play this message."
6096   (interactive)
6097   (let ((gnus-break-pages nil)
6098         (gnus-show-mime t))
6099     (gnus-summary-select-article gnus-show-all-headers t))
6100   (select-window (get-buffer-window gnus-article-buffer)))
6101
6102 ;;; Dead summaries.
6103
6104 (defvar gnus-dead-summary-mode-map nil)
6105
6106 (unless gnus-dead-summary-mode-map
6107   (setq gnus-dead-summary-mode-map (make-keymap))
6108   (suppress-keymap gnus-dead-summary-mode-map)
6109   (substitute-key-definition
6110    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6111   (let ((keys '("\C-d" "\r" "\177" [delete])))
6112     (while keys
6113       (define-key gnus-dead-summary-mode-map
6114         (pop keys) 'gnus-summary-wake-up-the-dead))))
6115
6116 (defvar gnus-dead-summary-mode nil
6117   "Minor mode for Gnus summary buffers.")
6118
6119 (defun gnus-dead-summary-mode (&optional arg)
6120   "Minor mode for Gnus summary buffers."
6121   (interactive "P")
6122   (when (eq major-mode 'gnus-summary-mode)
6123     (make-local-variable 'gnus-dead-summary-mode)
6124     (setq gnus-dead-summary-mode
6125           (if (null arg) (not gnus-dead-summary-mode)
6126             (> (prefix-numeric-value arg) 0)))
6127     (when gnus-dead-summary-mode
6128       (gnus-add-minor-mode
6129        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6130
6131 (defun gnus-deaden-summary ()
6132   "Make the current summary buffer into a dead summary buffer."
6133   ;; Kill any previous dead summary buffer.
6134   (when (and gnus-dead-summary
6135              (buffer-name gnus-dead-summary))
6136     (save-excursion
6137       (set-buffer gnus-dead-summary)
6138       (when gnus-dead-summary-mode
6139         (kill-buffer (current-buffer)))))
6140   ;; Make this the current dead summary.
6141   (setq gnus-dead-summary (current-buffer))
6142   (gnus-dead-summary-mode 1)
6143   (let ((name (buffer-name)))
6144     (when (string-match "Summary" name)
6145       (rename-buffer
6146        (concat (substring name 0 (match-beginning 0)) "Dead "
6147                (substring name (match-beginning 0)))
6148        t)
6149       (bury-buffer))))
6150
6151 (defun gnus-kill-or-deaden-summary (buffer)
6152   "Kill or deaden the summary BUFFER."
6153   (save-excursion
6154     (when (and (buffer-name buffer)
6155                (not gnus-single-article-buffer))
6156       (save-excursion
6157         (set-buffer buffer)
6158         (gnus-kill-buffer gnus-article-buffer)
6159         (gnus-kill-buffer gnus-original-article-buffer)))
6160     (cond (gnus-kill-summary-on-exit
6161            (when (and gnus-use-trees
6162                       (gnus-buffer-exists-p buffer))
6163              (save-excursion
6164                (set-buffer buffer)
6165                (gnus-tree-close gnus-newsgroup-name)))
6166            (gnus-kill-buffer buffer))
6167           ((gnus-buffer-exists-p buffer)
6168            (save-excursion
6169              (set-buffer buffer)
6170              (gnus-deaden-summary))))))
6171
6172 (defun gnus-summary-wake-up-the-dead (&rest args)
6173   "Wake up the dead summary buffer."
6174   (interactive)
6175   (gnus-dead-summary-mode -1)
6176   (let ((name (buffer-name)))
6177     (when (string-match "Dead " name)
6178       (rename-buffer
6179        (concat (substring name 0 (match-beginning 0))
6180                (substring name (match-end 0)))
6181        t)))
6182   (gnus-message 3 "This dead summary is now alive again"))
6183
6184 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6185 (defun gnus-summary-fetch-faq (&optional faq-dir)
6186   "Fetch the FAQ for the current group.
6187 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6188 in."
6189   (interactive
6190    (list
6191     (when current-prefix-arg
6192       (completing-read
6193        "Faq dir: " (and (listp gnus-group-faq-directory)
6194                         (mapcar (lambda (file) (list file))
6195                                 gnus-group-faq-directory))))))
6196   (let (gnus-faq-buffer)
6197     (when (setq gnus-faq-buffer
6198                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6199       (gnus-configure-windows 'summary-faq))))
6200
6201 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6202 (defun gnus-summary-describe-group (&optional force)
6203   "Describe the current newsgroup."
6204   (interactive "P")
6205   (gnus-group-describe-group force gnus-newsgroup-name))
6206
6207 (defun gnus-summary-describe-briefly ()
6208   "Describe summary mode commands briefly."
6209   (interactive)
6210   (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")))
6211
6212 ;; Walking around group mode buffer from summary mode.
6213
6214 (defun gnus-summary-next-group (&optional no-article target-group backward)
6215   "Exit current newsgroup and then select next unread newsgroup.
6216 If prefix argument NO-ARTICLE is non-nil, no article is selected
6217 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6218 previous group instead."
6219   (interactive "P")
6220   ;; Stop pre-fetching.
6221   (gnus-async-halt-prefetch)
6222   (let ((current-group gnus-newsgroup-name)
6223         (current-buffer (current-buffer))
6224         entered)
6225     ;; First we semi-exit this group to update Xrefs and all variables.
6226     ;; We can't do a real exit, because the window conf must remain
6227     ;; the same in case the user is prompted for info, and we don't
6228     ;; want the window conf to change before that...
6229     (gnus-summary-exit t)
6230     (while (not entered)
6231       ;; Then we find what group we are supposed to enter.
6232       (set-buffer gnus-group-buffer)
6233       (gnus-group-jump-to-group current-group)
6234       (setq target-group
6235             (or target-group
6236                 (if (eq gnus-keep-same-level 'best)
6237                     (gnus-summary-best-group gnus-newsgroup-name)
6238                   (gnus-summary-search-group backward gnus-keep-same-level))))
6239       (if (not target-group)
6240           ;; There are no further groups, so we return to the group
6241           ;; buffer.
6242           (progn
6243             (gnus-message 5 "Returning to the group buffer")
6244             (setq entered t)
6245             (when (gnus-buffer-live-p current-buffer)
6246               (set-buffer current-buffer)
6247               (gnus-summary-exit))
6248             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6249         ;; We try to enter the target group.
6250         (gnus-group-jump-to-group target-group)
6251         (let ((unreads (gnus-group-group-unread)))
6252           (if (and (or (eq t unreads)
6253                        (and unreads (not (zerop unreads))))
6254                    (gnus-summary-read-group
6255                     target-group nil no-article
6256                     (and (buffer-name current-buffer) current-buffer)
6257                     nil backward))
6258               (setq entered t)
6259             (setq current-group target-group
6260                   target-group nil)))))))
6261
6262 (defun gnus-summary-prev-group (&optional no-article)
6263   "Exit current newsgroup and then select previous unread newsgroup.
6264 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6265   (interactive "P")
6266   (gnus-summary-next-group no-article nil t))
6267
6268 ;; Walking around summary lines.
6269
6270 (defun gnus-summary-first-subject (&optional unread undownloaded)
6271   "Go to the first unread subject.
6272 If UNREAD is non-nil, go to the first unread article.
6273 Returns the article selected or nil if there are no unread articles."
6274   (interactive "P")
6275   (prog1
6276       (cond
6277        ;; Empty summary.
6278        ((null gnus-newsgroup-data)
6279         (gnus-message 3 "No articles in the group")
6280         nil)
6281        ;; Pick the first article.
6282        ((not unread)
6283         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6284         (gnus-data-number (car gnus-newsgroup-data)))
6285        ;; No unread articles.
6286        ((null gnus-newsgroup-unreads)
6287         (gnus-message 3 "No more unread articles")
6288         nil)
6289        ;; Find the first unread article.
6290        (t
6291         (let ((data gnus-newsgroup-data))
6292           (while (and data
6293                       (and (not (and undownloaded
6294                                      (eq gnus-undownloaded-mark
6295                                          (gnus-data-mark (car data)))))
6296                            (not (gnus-data-unread-p (car data)))))
6297             (setq data (cdr data)))
6298           (when data
6299             (goto-char (gnus-data-pos (car data)))
6300             (gnus-data-number (car data))))))
6301     (gnus-summary-position-point)))
6302
6303 (defun gnus-summary-next-subject (n &optional unread dont-display)
6304   "Go to next N'th summary line.
6305 If N is negative, go to the previous N'th subject line.
6306 If UNREAD is non-nil, only unread articles are selected.
6307 The difference between N and the actual number of steps taken is
6308 returned."
6309   (interactive "p")
6310   (let ((backward (< n 0))
6311         (n (abs n)))
6312     (while (and (> n 0)
6313                 (if backward
6314                     (gnus-summary-find-prev unread)
6315                   (gnus-summary-find-next unread)))
6316       (unless (zerop (setq n (1- n)))
6317         (gnus-summary-show-thread)))
6318     (when (/= 0 n)
6319       (gnus-message 7 "No more%s articles"
6320                     (if unread " unread" "")))
6321     (unless dont-display
6322       (gnus-summary-recenter)
6323       (gnus-summary-position-point))
6324     n))
6325
6326 (defun gnus-summary-next-unread-subject (n)
6327   "Go to next N'th unread summary line."
6328   (interactive "p")
6329   (gnus-summary-next-subject n t))
6330
6331 (defun gnus-summary-prev-subject (n &optional unread)
6332   "Go to previous N'th summary line.
6333 If optional argument UNREAD is non-nil, only unread article is selected."
6334   (interactive "p")
6335   (gnus-summary-next-subject (- n) unread))
6336
6337 (defun gnus-summary-prev-unread-subject (n)
6338   "Go to previous N'th unread summary line."
6339   (interactive "p")
6340   (gnus-summary-next-subject (- n) t))
6341
6342 (defun gnus-summary-goto-subject (article &optional force silent)
6343   "Go the subject line of ARTICLE.
6344 If FORCE, also allow jumping to articles not currently shown."
6345   (interactive "nArticle number: ")
6346   (let ((b (point))
6347         (data (gnus-data-find article)))
6348     ;; We read in the article if we have to.
6349     (and (not data)
6350          force
6351          (gnus-summary-insert-subject
6352           article
6353           (if (or (numberp force) (vectorp force)) force)
6354           t)
6355          (setq data (gnus-data-find article)))
6356     (goto-char b)
6357     (if (not data)
6358         (progn
6359           (unless silent
6360             (gnus-message 3 "Can't find article %d" article))
6361           nil)
6362       (let ((pt (gnus-data-pos data)))
6363         (goto-char pt)
6364         (gnus-summary-set-article-display-arrow pt))
6365       (gnus-summary-position-point)
6366       article)))
6367
6368 ;; Walking around summary lines with displaying articles.
6369
6370 (defun gnus-summary-expand-window (&optional arg)
6371   "Make the summary buffer take up the entire Emacs frame.
6372 Given a prefix, will force an `article' buffer configuration."
6373   (interactive "P")
6374   (if arg
6375       (gnus-configure-windows 'article 'force)
6376     (gnus-configure-windows 'summary 'force)))
6377
6378 (defun gnus-summary-display-article (article &optional all-header)
6379   "Display ARTICLE in article buffer."
6380   (when (gnus-buffer-live-p gnus-article-buffer)
6381     (with-current-buffer gnus-article-buffer
6382       (set-buffer-multibyte t)))
6383   (gnus-set-global-variables)
6384   (when (gnus-buffer-live-p gnus-article-buffer)
6385     (with-current-buffer gnus-article-buffer
6386       (setq gnus-article-charset gnus-newsgroup-charset)
6387       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6388   (if (null article)
6389       nil
6390     (prog1
6391         (if gnus-summary-display-article-function
6392             (funcall gnus-summary-display-article-function article all-header)
6393           (gnus-article-prepare article all-header))
6394       (with-current-buffer gnus-article-buffer
6395         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6396              nil))
6397       (gnus-run-hooks 'gnus-select-article-hook)
6398       (when (and gnus-current-article
6399                  (not (zerop gnus-current-article)))
6400         (gnus-summary-goto-subject gnus-current-article))
6401       (gnus-summary-recenter)
6402       (when (and gnus-use-trees gnus-show-threads)
6403         (gnus-possibly-generate-tree article)
6404         (gnus-highlight-selected-tree article))
6405       ;; Successfully display article.
6406       (gnus-article-set-window-start
6407        (cdr (assq article gnus-newsgroup-bookmarks))))))
6408
6409 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6410   "Select the current article.
6411 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6412 non-nil, the article will be re-fetched even if it already present in
6413 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6414 be displayed."
6415   ;; Make sure we are in the summary buffer to work around bbdb bug.
6416   (unless (eq major-mode 'gnus-summary-mode)
6417     (set-buffer gnus-summary-buffer))
6418   (let ((article (or article (gnus-summary-article-number)))
6419         (all-headers (not (not all-headers))) ;Must be T or NIL.
6420         gnus-summary-display-article-function)
6421     (and (not pseudo)
6422          (gnus-summary-article-pseudo-p article)
6423          (error "This is a pseudo-article"))
6424     (save-excursion
6425       (set-buffer gnus-summary-buffer)
6426       (if (or (and gnus-single-article-buffer
6427                    (or (null gnus-current-article)
6428                        (null gnus-article-current)
6429                        (null (get-buffer gnus-article-buffer))
6430                        (not (eq article (cdr gnus-article-current)))
6431                        (not (equal (car gnus-article-current)
6432                                    gnus-newsgroup-name))))
6433               (and (not gnus-single-article-buffer)
6434                    (or (null gnus-current-article)
6435                        (not (eq gnus-current-article article))))
6436               force)
6437           ;; The requested article is different from the current article.
6438           (progn
6439             (gnus-summary-display-article article all-headers)
6440             (when (or all-headers gnus-show-all-headers)
6441               (gnus-article-show-all-headers))
6442             (gnus-article-set-window-start
6443              (cdr (assq article gnus-newsgroup-bookmarks)))
6444             article)
6445         (when (or all-headers gnus-show-all-headers)
6446           (gnus-article-show-all-headers))
6447         'old))))
6448
6449 (defun gnus-summary-force-verify-and-decrypt ()
6450   (interactive)
6451   (let ((mm-verify-option 'known)
6452         (mm-decrypt-option 'known))
6453     (gnus-summary-select-article nil 'force)))
6454
6455 (defun gnus-summary-set-current-mark (&optional current-mark)
6456   "Obsolete function."
6457   nil)
6458
6459 (defun gnus-summary-next-article (&optional unread subject backward push)
6460   "Select the next article.
6461 If UNREAD, only unread articles are selected.
6462 If SUBJECT, only articles with SUBJECT are selected.
6463 If BACKWARD, the previous article is selected instead of the next."
6464   (interactive "P")
6465   (cond
6466    ;; Is there such an article?
6467    ((and (gnus-summary-search-forward unread subject backward)
6468          (or (gnus-summary-display-article (gnus-summary-article-number))
6469              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6470     (gnus-summary-position-point))
6471    ;; If not, we try the first unread, if that is wanted.
6472    ((and subject
6473          gnus-auto-select-same
6474          (gnus-summary-first-unread-article))
6475     (gnus-summary-position-point)
6476     (gnus-message 6 "Wrapped"))
6477    ;; Try to get next/previous article not displayed in this group.
6478    ((and gnus-auto-extend-newsgroup
6479          (not unread) (not subject))
6480     (gnus-summary-goto-article
6481      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6482      nil (count-lines (point-min) (point))))
6483    ;; Go to next/previous group.
6484    (t
6485     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6486       (gnus-summary-jump-to-group gnus-newsgroup-name))
6487     (let ((cmd last-command-char)
6488           (point
6489            (save-excursion
6490              (set-buffer gnus-group-buffer)
6491              (point)))
6492           (group
6493            (if (eq gnus-keep-same-level 'best)
6494                (gnus-summary-best-group gnus-newsgroup-name)
6495              (gnus-summary-search-group backward gnus-keep-same-level))))
6496       ;; For some reason, the group window gets selected.  We change
6497       ;; it back.
6498       (select-window (get-buffer-window (current-buffer)))
6499       ;; Select next unread newsgroup automagically.
6500       (cond
6501        ((or (not gnus-auto-select-next)
6502             (not cmd))
6503         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6504        ((or (eq gnus-auto-select-next 'quietly)
6505             (and (eq gnus-auto-select-next 'slightly-quietly)
6506                  push)
6507             (and (eq gnus-auto-select-next 'almost-quietly)
6508                  (gnus-summary-last-article-p)))
6509         ;; Select quietly.
6510         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6511             (gnus-summary-exit)
6512           (gnus-message 7 "No more%s articles (%s)..."
6513                         (if unread " unread" "")
6514                         (if group (concat "selecting " group)
6515                           "exiting"))
6516           (gnus-summary-next-group nil group backward)))
6517        (t
6518         (when (gnus-key-press-event-p last-input-event)
6519           (gnus-summary-walk-group-buffer
6520            gnus-newsgroup-name cmd unread backward point))))))))
6521
6522 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6523   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6524                       (?\C-p (gnus-group-prev-unread-group 1))))
6525         (cursor-in-echo-area t)
6526         keve key group ended)
6527     (save-excursion
6528       (set-buffer gnus-group-buffer)
6529       (goto-char start)
6530       (setq group
6531             (if (eq gnus-keep-same-level 'best)
6532                 (gnus-summary-best-group gnus-newsgroup-name)
6533               (gnus-summary-search-group backward gnus-keep-same-level))))
6534     (while (not ended)
6535       (gnus-message
6536        5 "No more%s articles%s" (if unread " unread" "")
6537        (if (and group
6538                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6539            (format " (Type %s for %s [%s])"
6540                    (single-key-description cmd) group
6541                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6542          (format " (Type %s to exit %s)"
6543                  (single-key-description cmd)
6544                  gnus-newsgroup-name)))
6545       ;; Confirm auto selection.
6546       (setq key (car (setq keve (gnus-read-event-char))))
6547       (setq ended t)
6548       (cond
6549        ((assq key keystrokes)
6550         (let ((obuf (current-buffer)))
6551           (switch-to-buffer gnus-group-buffer)
6552           (when group
6553             (gnus-group-jump-to-group group))
6554           (eval (cadr (assq key keystrokes)))
6555           (setq group (gnus-group-group-name))
6556           (switch-to-buffer obuf))
6557         (setq ended nil))
6558        ((equal key cmd)
6559         (if (or (not group)
6560                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6561             (gnus-summary-exit)
6562           (gnus-summary-next-group nil group backward)))
6563        (t
6564         (push (cdr keve) unread-command-events))))))
6565
6566 (defun gnus-summary-next-unread-article ()
6567   "Select unread article after current one."
6568   (interactive)
6569   (gnus-summary-next-article
6570    (or (not (eq gnus-summary-goto-unread 'never))
6571        (gnus-summary-last-article-p (gnus-summary-article-number)))
6572    (and gnus-auto-select-same
6573         (gnus-summary-article-subject))))
6574
6575 (defun gnus-summary-prev-article (&optional unread subject)
6576   "Select the article after the current one.
6577 If UNREAD is non-nil, only unread articles are selected."
6578   (interactive "P")
6579   (gnus-summary-next-article unread subject t))
6580
6581 (defun gnus-summary-prev-unread-article ()
6582   "Select unread article before current one."
6583   (interactive)
6584   (gnus-summary-prev-article
6585    (or (not (eq gnus-summary-goto-unread 'never))
6586        (gnus-summary-first-article-p (gnus-summary-article-number)))
6587    (and gnus-auto-select-same
6588         (gnus-summary-article-subject))))
6589
6590 (defun gnus-summary-next-page (&optional lines circular)
6591   "Show next page of the selected article.
6592 If at the end of the current article, select the next article.
6593 LINES says how many lines should be scrolled up.
6594
6595 If CIRCULAR is non-nil, go to the start of the article instead of
6596 selecting the next article when reaching the end of the current
6597 article."
6598   (interactive "P")
6599   (setq gnus-summary-buffer (current-buffer))
6600   (gnus-set-global-variables)
6601   (let ((article (gnus-summary-article-number))
6602         (article-window (get-buffer-window gnus-article-buffer t))
6603         endp)
6604     ;; If the buffer is empty, we have no article.
6605     (unless article
6606       (error "No article to select"))
6607     (gnus-configure-windows 'article)
6608     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6609         (if (and (eq gnus-summary-goto-unread 'never)
6610                  (not (gnus-summary-last-article-p article)))
6611             (gnus-summary-next-article)
6612           (gnus-summary-next-unread-article))
6613       (if (or (null gnus-current-article)
6614               (null gnus-article-current)
6615               (/= article (cdr gnus-article-current))
6616               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6617           ;; Selected subject is different from current article's.
6618           (gnus-summary-display-article article)
6619         (when article-window
6620           (gnus-eval-in-buffer-window gnus-article-buffer
6621             (setq endp (gnus-article-next-page lines)))
6622           (when endp
6623             (cond (circular
6624                    (gnus-summary-beginning-of-article))
6625                   (lines
6626                    (gnus-message 3 "End of message"))
6627                   ((null lines)
6628                    (if (and (eq gnus-summary-goto-unread 'never)
6629                             (not (gnus-summary-last-article-p article)))
6630                        (gnus-summary-next-article)
6631                      (gnus-summary-next-unread-article))))))))
6632     (gnus-summary-recenter)
6633     (gnus-summary-position-point)))
6634
6635 (defun gnus-summary-prev-page (&optional lines move)
6636   "Show previous page of selected article.
6637 Argument LINES specifies lines to be scrolled down.
6638 If MOVE, move to the previous unread article if point is at
6639 the beginning of the buffer."
6640   (interactive "P")
6641   (let ((article (gnus-summary-article-number))
6642         (article-window (get-buffer-window gnus-article-buffer t))
6643         endp)
6644     (gnus-configure-windows 'article)
6645     (if (or (null gnus-current-article)
6646             (null gnus-article-current)
6647             (/= article (cdr gnus-article-current))
6648             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6649         ;; Selected subject is different from current article's.
6650         (gnus-summary-display-article article)
6651       (gnus-summary-recenter)
6652       (when article-window
6653         (gnus-eval-in-buffer-window gnus-article-buffer
6654           (setq endp (gnus-article-prev-page lines)))
6655         (when (and move endp)
6656           (cond (lines
6657                  (gnus-message 3 "Beginning of message"))
6658                 ((null lines)
6659                  (if (and (eq gnus-summary-goto-unread 'never)
6660                           (not (gnus-summary-first-article-p article)))
6661                      (gnus-summary-prev-article)
6662                    (gnus-summary-prev-unread-article))))))))
6663   (gnus-summary-position-point))
6664
6665 (defun gnus-summary-prev-page-or-article (&optional lines)
6666   "Show previous page of selected article.
6667 Argument LINES specifies lines to be scrolled down.
6668 If at the beginning of the article, go to the next article."
6669   (interactive "P")
6670   (gnus-summary-prev-page lines t))
6671
6672 (defun gnus-summary-scroll-up (lines)
6673   "Scroll up (or down) one line current article.
6674 Argument LINES specifies lines to be scrolled up (or down if negative)."
6675   (interactive "p")
6676   (gnus-configure-windows 'article)
6677   (gnus-summary-show-thread)
6678   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6679     (gnus-eval-in-buffer-window gnus-article-buffer
6680       (cond ((> lines 0)
6681              (when (gnus-article-next-page lines)
6682                (gnus-message 3 "End of message")))
6683             ((< lines 0)
6684              (gnus-article-prev-page (- lines))))))
6685   (gnus-summary-recenter)
6686   (gnus-summary-position-point))
6687
6688 (defun gnus-summary-scroll-down (lines)
6689   "Scroll down (or up) one line current article.
6690 Argument LINES specifies lines to be scrolled down (or up if negative)."
6691   (interactive "p")
6692   (gnus-summary-scroll-up (- lines)))
6693
6694 (defun gnus-summary-next-same-subject ()
6695   "Select next article which has the same subject as current one."
6696   (interactive)
6697   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6698
6699 (defun gnus-summary-prev-same-subject ()
6700   "Select previous article which has the same subject as current one."
6701   (interactive)
6702   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6703
6704 (defun gnus-summary-next-unread-same-subject ()
6705   "Select next unread article which has the same subject as current one."
6706   (interactive)
6707   (gnus-summary-next-article t (gnus-summary-article-subject)))
6708
6709 (defun gnus-summary-prev-unread-same-subject ()
6710   "Select previous unread article which has the same subject as current one."
6711   (interactive)
6712   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6713
6714 (defun gnus-summary-first-unread-article ()
6715   "Select the first unread article.
6716 Return nil if there are no unread articles."
6717   (interactive)
6718   (prog1
6719       (when (gnus-summary-first-subject t)
6720         (gnus-summary-show-thread)
6721         (gnus-summary-first-subject t)
6722         (gnus-summary-display-article (gnus-summary-article-number)))
6723     (gnus-summary-position-point)))
6724
6725 (defun gnus-summary-first-unread-subject ()
6726   "Place the point on the subject line of the first unread article.
6727 Return nil if there are no unread articles."
6728   (interactive)
6729   (prog1
6730       (when (gnus-summary-first-subject t)
6731         (gnus-summary-show-thread)
6732         (gnus-summary-first-subject t))
6733     (gnus-summary-position-point)))
6734
6735 (defun gnus-summary-first-article ()
6736   "Select the first article.
6737 Return nil if there are no articles."
6738   (interactive)
6739   (prog1
6740       (when (gnus-summary-first-subject)
6741         (gnus-summary-show-thread)
6742         (gnus-summary-first-subject)
6743         (gnus-summary-display-article (gnus-summary-article-number)))
6744     (gnus-summary-position-point)))
6745
6746 (defun gnus-summary-best-unread-article ()
6747   "Select the unread article with the highest score."
6748   (interactive)
6749   (let ((best -1000000)
6750         (data gnus-newsgroup-data)
6751         article score)
6752     (while data
6753       (and (gnus-data-unread-p (car data))
6754            (> (setq score
6755                     (gnus-summary-article-score (gnus-data-number (car data))))
6756               best)
6757            (setq best score
6758                  article (gnus-data-number (car data))))
6759       (setq data (cdr data)))
6760     (prog1
6761         (if article
6762             (gnus-summary-goto-article article)
6763           (error "No unread articles"))
6764       (gnus-summary-position-point))))
6765
6766 (defun gnus-summary-last-subject ()
6767   "Go to the last displayed subject line in the group."
6768   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6769     (when article
6770       (gnus-summary-goto-subject article))))
6771
6772 (defun gnus-summary-goto-article (article &optional all-headers force)
6773   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6774 If ALL-HEADERS is non-nil, no header lines are hidden.
6775 If FORCE, go to the article even if it isn't displayed.  If FORCE
6776 is a number, it is the line the article is to be displayed on."
6777   (interactive
6778    (list
6779     (completing-read
6780      "Article number or Message-ID: "
6781      (mapcar (lambda (number) (list (int-to-string number)))
6782              gnus-newsgroup-limit))
6783     current-prefix-arg
6784     t))
6785   (prog1
6786       (if (and (stringp article)
6787                (string-match "@" article))
6788           (gnus-summary-refer-article article)
6789         (when (stringp article)
6790           (setq article (string-to-number article)))
6791         (if (gnus-summary-goto-subject article force)
6792             (gnus-summary-display-article article all-headers)
6793           (gnus-message 4 "Couldn't go to article %s" article) nil))
6794     (gnus-summary-position-point)))
6795
6796 (defun gnus-summary-goto-last-article ()
6797   "Go to the previously read article."
6798   (interactive)
6799   (prog1
6800       (when gnus-last-article
6801         (gnus-summary-goto-article gnus-last-article nil t))
6802     (gnus-summary-position-point)))
6803
6804 (defun gnus-summary-pop-article (number)
6805   "Pop one article off the history and go to the previous.
6806 NUMBER articles will be popped off."
6807   (interactive "p")
6808   (let (to)
6809     (setq gnus-newsgroup-history
6810           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6811     (if to
6812         (gnus-summary-goto-article (car to) nil t)
6813       (error "Article history empty")))
6814   (gnus-summary-position-point))
6815
6816 ;; Summary commands and functions for limiting the summary buffer.
6817
6818 (defun gnus-summary-limit-to-articles (n)
6819   "Limit the summary buffer to the next N articles.
6820 If not given a prefix, use the process marked articles instead."
6821   (interactive "P")
6822   (prog1
6823       (let ((articles (gnus-summary-work-articles n)))
6824         (setq gnus-newsgroup-processable nil)
6825         (gnus-summary-limit articles))
6826     (gnus-summary-position-point)))
6827
6828 (defun gnus-summary-pop-limit (&optional total)
6829   "Restore the previous limit.
6830 If given a prefix, remove all limits."
6831   (interactive "P")
6832   (when total
6833     (setq gnus-newsgroup-limits
6834           (list (mapcar (lambda (h) (mail-header-number h))
6835                         gnus-newsgroup-headers))))
6836   (unless gnus-newsgroup-limits
6837     (error "No limit to pop"))
6838   (prog1
6839       (gnus-summary-limit nil 'pop)
6840     (gnus-summary-position-point)))
6841
6842 (defun gnus-summary-limit-to-subject (subject &optional header)
6843   "Limit the summary buffer to articles that have subjects that match a regexp."
6844   (interactive "sLimit to subject (regexp): ")
6845   (unless header
6846     (setq header "subject"))
6847   (when (not (equal "" subject))
6848     (prog1
6849         (let ((articles (gnus-summary-find-matching
6850                          (or header "subject") subject 'all)))
6851           (unless articles
6852             (error "Found no matches for \"%s\"" subject))
6853           (gnus-summary-limit articles))
6854       (gnus-summary-position-point))))
6855
6856 (defun gnus-summary-limit-to-author (from)
6857   "Limit the summary buffer to articles that have authors that match a regexp."
6858   (interactive "sLimit to author (regexp): ")
6859   (gnus-summary-limit-to-subject from "from"))
6860
6861 (defun gnus-summary-limit-to-age (age &optional younger-p)
6862   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6863 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6864 articles that are younger than AGE days."
6865   (interactive
6866    (let ((younger current-prefix-arg)
6867          (days-got nil)
6868          days)
6869      (while (not days-got)
6870        (setq days (if younger
6871                       (read-string "Limit to articles within (in days): ")
6872                     (read-string "Limit to articles older than (in days): ")))
6873        (when (> (length days) 0)
6874          (setq days (read days)))
6875        (if (numberp days)
6876            (setq days-got t)
6877          (message "Please enter a number.")
6878          (sleep-for 1)))
6879      (list days younger)))
6880   (prog1
6881       (let ((data gnus-newsgroup-data)
6882             (cutoff (days-to-time age))
6883             articles d date is-younger)
6884         (while (setq d (pop data))
6885           (when (and (vectorp (gnus-data-header d))
6886                      (setq date (mail-header-date (gnus-data-header d))))
6887             (setq is-younger (time-less-p
6888                               (time-since (condition-case ()
6889                                               (date-to-time date)
6890                                             (error '(0 0))))
6891                               cutoff))
6892             (when (if younger-p
6893                       is-younger
6894                     (not is-younger))
6895               (push (gnus-data-number d) articles))))
6896         (gnus-summary-limit (nreverse articles)))
6897     (gnus-summary-position-point)))
6898
6899 (defun gnus-summary-limit-to-extra (header regexp)
6900   "Limit the summary buffer to articles that match an 'extra' header."
6901   (interactive
6902    (let ((header
6903           (intern
6904            (gnus-completing-read
6905             (symbol-name (car gnus-extra-headers))
6906             "Limit extra header:"
6907             (mapcar (lambda (x)
6908                       (cons (symbol-name x) x))
6909                     gnus-extra-headers)
6910             nil
6911             t))))
6912      (list header
6913            (read-string (format "Limit to header %s (regexp): " header)))))
6914   (when (not (equal "" regexp))
6915     (prog1
6916         (let ((articles (gnus-summary-find-matching
6917                          (cons 'extra header) regexp 'all)))
6918           (unless articles
6919             (error "Found no matches for \"%s\"" regexp))
6920           (gnus-summary-limit articles))
6921       (gnus-summary-position-point))))
6922
6923 (defun gnus-summary-limit-to-display-predicate ()
6924   "Limit the summary buffer to the predicated in the `display' group parameter."
6925   (interactive)
6926   (unless gnus-newsgroup-display
6927     (error "There is no `diplay' group parameter"))
6928   (let (articles)
6929     (dolist (number gnus-newsgroup-articles)
6930       (when (funcall gnus-newsgroup-display)
6931         (push number articles)))
6932     (gnus-summary-limit articles))
6933   (gnus-summary-position-point))
6934
6935 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6936 (make-obsolete
6937  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6938
6939 (defun gnus-summary-limit-to-unread (&optional all)
6940   "Limit the summary buffer to articles that are not marked as read.
6941 If ALL is non-nil, limit strictly to unread articles."
6942   (interactive "P")
6943   (if all
6944       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6945     (gnus-summary-limit-to-marks
6946      ;; Concat all the marks that say that an article is read and have
6947      ;; those removed.
6948      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6949            gnus-killed-mark gnus-kill-file-mark
6950            gnus-low-score-mark gnus-expirable-mark
6951            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6952            gnus-duplicate-mark gnus-souped-mark)
6953      'reverse)))
6954
6955 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6956 (make-obsolete 'gnus-summary-delete-marked-with
6957                'gnus-summary-limit-exlude-marks)
6958
6959 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6960   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6961 If REVERSE, limit the summary buffer to articles that are marked
6962 with MARKS.  MARKS can either be a string of marks or a list of marks.
6963 Returns how many articles were removed."
6964   (interactive "sMarks: ")
6965   (gnus-summary-limit-to-marks marks t))
6966
6967 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6968   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6969 If REVERSE (the prefix), limit the summary buffer to articles that are
6970 not marked with MARKS.  MARKS can either be a string of marks or a
6971 list of marks.
6972 Returns how many articles were removed."
6973   (interactive "sMarks: \nP")
6974   (prog1
6975       (let ((data gnus-newsgroup-data)
6976             (marks (if (listp marks) marks
6977                      (append marks nil))) ; Transform to list.
6978             articles)
6979         (while data
6980           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6981                   (memq (gnus-data-mark (car data)) marks))
6982             (push (gnus-data-number (car data)) articles))
6983           (setq data (cdr data)))
6984         (gnus-summary-limit articles))
6985     (gnus-summary-position-point)))
6986
6987 (defun gnus-summary-limit-to-score (&optional score)
6988   "Limit to articles with score at or above SCORE."
6989   (interactive "P")
6990   (setq score (if score
6991                   (prefix-numeric-value score)
6992                 (or gnus-summary-default-score 0)))
6993   (let ((data gnus-newsgroup-data)
6994         articles)
6995     (while data
6996       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6997                 score)
6998         (push (gnus-data-number (car data)) articles))
6999       (setq data (cdr data)))
7000     (prog1
7001         (gnus-summary-limit articles)
7002       (gnus-summary-position-point))))
7003
7004 (defun gnus-summary-limit-include-thread (id)
7005   "Display all the hidden articles that is in the thread with ID in it.
7006 When called interactively, ID is the Message-ID of the current
7007 article."
7008   (interactive (list (mail-header-id (gnus-summary-article-header))))
7009   (let ((articles (gnus-articles-in-thread
7010                    (gnus-id-to-thread (gnus-root-id id)))))
7011     (prog1
7012         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7013       (gnus-summary-limit-include-matching-articles
7014        "subject"
7015        (regexp-quote (gnus-simplify-subject-re
7016                       (mail-header-subject (gnus-id-to-header id)))))
7017       (gnus-summary-position-point))))
7018
7019 (defun gnus-summary-limit-include-matching-articles (header regexp)
7020   "Display all the hidden articles that have HEADERs that match REGEXP."
7021   (interactive (list (read-string "Match on header: ")
7022                      (read-string "Regexp: ")))
7023   (let ((articles (gnus-find-matching-articles header regexp)))
7024     (prog1
7025         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7026       (gnus-summary-position-point))))
7027
7028 (defun gnus-summary-limit-include-dormant ()
7029   "Display all the hidden articles that are marked as dormant.
7030 Note that this command only works on a subset of the articles currently
7031 fetched for this group."
7032   (interactive)
7033   (unless gnus-newsgroup-dormant
7034     (error "There are no dormant articles in this group"))
7035   (prog1
7036       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7037     (gnus-summary-position-point)))
7038
7039 (defun gnus-summary-limit-exclude-dormant ()
7040   "Hide all dormant articles."
7041   (interactive)
7042   (prog1
7043       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7044     (gnus-summary-position-point)))
7045
7046 (defun gnus-summary-limit-exclude-childless-dormant ()
7047   "Hide all dormant articles that have no children."
7048   (interactive)
7049   (let ((data (gnus-data-list t))
7050         articles d children)
7051     ;; Find all articles that are either not dormant or have
7052     ;; children.
7053     (while (setq d (pop data))
7054       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7055                 (and (setq children
7056                            (gnus-article-children (gnus-data-number d)))
7057                      (let (found)
7058                        (while children
7059                          (when (memq (car children) articles)
7060                            (setq children nil
7061                                  found t))
7062                          (pop children))
7063                        found)))
7064         (push (gnus-data-number d) articles)))
7065     ;; Do the limiting.
7066     (prog1
7067         (gnus-summary-limit articles)
7068       (gnus-summary-position-point))))
7069
7070 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7071   "Mark all unread excluded articles as read.
7072 If ALL, mark even excluded ticked and dormants as read."
7073   (interactive "P")
7074   (let ((articles (gnus-sorted-complement
7075                    (sort
7076                     (mapcar (lambda (h) (mail-header-number h))
7077                             gnus-newsgroup-headers)
7078                     '<)
7079                    (sort gnus-newsgroup-limit '<)))
7080         article)
7081     (setq gnus-newsgroup-unreads
7082           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7083     (if all
7084         (setq gnus-newsgroup-dormant nil
7085               gnus-newsgroup-marked nil
7086               gnus-newsgroup-reads
7087               (nconc
7088                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7089                gnus-newsgroup-reads))
7090       (while (setq article (pop articles))
7091         (unless (or (memq article gnus-newsgroup-dormant)
7092                     (memq article gnus-newsgroup-marked))
7093           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7094
7095 (defun gnus-summary-limit (articles &optional pop)
7096   (if pop
7097       ;; We pop the previous limit off the stack and use that.
7098       (setq articles (car gnus-newsgroup-limits)
7099             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7100     ;; We use the new limit, so we push the old limit on the stack.
7101     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7102   ;; Set the limit.
7103   (setq gnus-newsgroup-limit articles)
7104   (let ((total (length gnus-newsgroup-data))
7105         (data (gnus-data-find-list (gnus-summary-article-number)))
7106         (gnus-summary-mark-below nil)   ; Inhibit this.
7107         found)
7108     ;; This will do all the work of generating the new summary buffer
7109     ;; according to the new limit.
7110     (gnus-summary-prepare)
7111     ;; Hide any threads, possibly.
7112     (and gnus-show-threads
7113          gnus-thread-hide-subtree
7114          (gnus-summary-hide-all-threads))
7115     ;; Try to return to the article you were at, or one in the
7116     ;; neighborhood.
7117     (when data
7118       ;; We try to find some article after the current one.
7119       (while data
7120         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7121           (setq data nil
7122                 found t))
7123         (setq data (cdr data))))
7124     (unless found
7125       ;; If there is no data, that means that we were after the last
7126       ;; article.  The same goes when we can't find any articles
7127       ;; after the current one.
7128       (goto-char (point-max))
7129       (gnus-summary-find-prev))
7130     (gnus-set-mode-line 'summary)
7131     ;; We return how many articles were removed from the summary
7132     ;; buffer as a result of the new limit.
7133     (- total (length gnus-newsgroup-data))))
7134
7135 (defsubst gnus-invisible-cut-children (threads)
7136   (let ((num 0))
7137     (while threads
7138       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7139         (incf num))
7140       (pop threads))
7141     (< num 2)))
7142
7143 (defsubst gnus-cut-thread (thread)
7144   "Go forwards in the thread until we find an article that we want to display."
7145   (when (or (eq gnus-fetch-old-headers 'some)
7146             (eq gnus-fetch-old-headers 'invisible)
7147             (numberp gnus-fetch-old-headers)
7148             (eq gnus-build-sparse-threads 'some)
7149             (eq gnus-build-sparse-threads 'more))
7150     ;; Deal with old-fetched headers and sparse threads.
7151     (while (and
7152             thread
7153             (or
7154              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7155              (gnus-summary-article-ancient-p
7156               (mail-header-number (car thread))))
7157             (if (or (<= (length (cdr thread)) 1)
7158                     (eq gnus-fetch-old-headers 'invisible))
7159                 (setq gnus-newsgroup-limit
7160                       (delq (mail-header-number (car thread))
7161                             gnus-newsgroup-limit)
7162                       thread (cadr thread))
7163               (when (gnus-invisible-cut-children (cdr thread))
7164                 (let ((th (cdr thread)))
7165                   (while th
7166                     (if (memq (mail-header-number (caar th))
7167                               gnus-newsgroup-limit)
7168                         (setq thread (car th)
7169                               th nil)
7170                       (setq th (cdr th))))))))))
7171   thread)
7172
7173 (defun gnus-cut-threads (threads)
7174   "Cut off all uninteresting articles from the beginning of threads."
7175   (when (or (eq gnus-fetch-old-headers 'some)
7176             (eq gnus-fetch-old-headers 'invisible)
7177             (numberp gnus-fetch-old-headers)
7178             (eq gnus-build-sparse-threads 'some)
7179             (eq gnus-build-sparse-threads 'more))
7180     (let ((th threads))
7181       (while th
7182         (setcar th (gnus-cut-thread (car th)))
7183         (setq th (cdr th)))))
7184   ;; Remove nixed out threads.
7185   (delq nil threads))
7186
7187 (defun gnus-summary-initial-limit (&optional show-if-empty)
7188   "Figure out what the initial limit is supposed to be on group entry.
7189 This entails weeding out unwanted dormants, low-scored articles,
7190 fetch-old-headers verbiage, and so on."
7191   ;; Most groups have nothing to remove.
7192   (if (or gnus-inhibit-limiting
7193           (and (null gnus-newsgroup-dormant)
7194                (eq gnus-newsgroup-display 'gnus-not-ignore)
7195                (not (eq gnus-fetch-old-headers 'some))
7196                (not (numberp gnus-fetch-old-headers))
7197                (not (eq gnus-fetch-old-headers 'invisible))
7198                (null gnus-summary-expunge-below)
7199                (not (eq gnus-build-sparse-threads 'some))
7200                (not (eq gnus-build-sparse-threads 'more))
7201                (null gnus-thread-expunge-below)
7202                (not gnus-use-nocem)))
7203       ()                                ; Do nothing.
7204     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7205     (setq gnus-newsgroup-limit nil)
7206     (mapatoms
7207      (lambda (node)
7208        (unless (car (symbol-value node))
7209          ;; These threads have no parents -- they are roots.
7210          (let ((nodes (cdr (symbol-value node)))
7211                thread)
7212            (while nodes
7213              (if (and gnus-thread-expunge-below
7214                       (< (gnus-thread-total-score (car nodes))
7215                          gnus-thread-expunge-below))
7216                  (gnus-expunge-thread (pop nodes))
7217                (setq thread (pop nodes))
7218                (gnus-summary-limit-children thread))))))
7219      gnus-newsgroup-dependencies)
7220     ;; If this limitation resulted in an empty group, we might
7221     ;; pop the previous limit and use it instead.
7222     (when (and (not gnus-newsgroup-limit)
7223                show-if-empty)
7224       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7225     gnus-newsgroup-limit))
7226
7227 (defun gnus-summary-limit-children (thread)
7228   "Return 1 if this subthread is visible and 0 if it is not."
7229   ;; First we get the number of visible children to this thread.  This
7230   ;; is done by recursing down the thread using this function, so this
7231   ;; will really go down to a leaf article first, before slowly
7232   ;; working its way up towards the root.
7233   (when thread
7234     (let ((children
7235            (if (cdr thread)
7236                (apply '+ (mapcar 'gnus-summary-limit-children
7237                                  (cdr thread)))
7238              0))
7239           (number (mail-header-number (car thread)))
7240           score)
7241       (if (and
7242            (not (memq number gnus-newsgroup-marked))
7243            (or
7244             ;; If this article is dormant and has absolutely no visible
7245             ;; children, then this article isn't visible.
7246             (and (memq number gnus-newsgroup-dormant)
7247                  (zerop children))
7248             ;; If this is "fetch-old-headered" and there is no
7249             ;; visible children, then we don't want this article.
7250             (and (or (eq gnus-fetch-old-headers 'some)
7251                      (numberp gnus-fetch-old-headers))
7252                  (gnus-summary-article-ancient-p number)
7253                  (zerop children))
7254             ;; If this is "fetch-old-headered" and `invisible', then
7255             ;; we don't want this article.
7256             (and (eq gnus-fetch-old-headers 'invisible)
7257                  (gnus-summary-article-ancient-p number))
7258             ;; If this is a sparsely inserted article with no children,
7259             ;; we don't want it.
7260             (and (eq gnus-build-sparse-threads 'some)
7261                  (gnus-summary-article-sparse-p number)
7262                  (zerop children))
7263             ;; If we use expunging, and this article is really
7264             ;; low-scored, then we don't want this article.
7265             (when (and gnus-summary-expunge-below
7266                        (< (setq score
7267                                 (or (cdr (assq number gnus-newsgroup-scored))
7268                                     gnus-summary-default-score))
7269                           gnus-summary-expunge-below))
7270               ;; We increase the expunge-tally here, but that has
7271               ;; nothing to do with the limits, really.
7272               (incf gnus-newsgroup-expunged-tally)
7273               ;; We also mark as read here, if that's wanted.
7274               (when (and gnus-summary-mark-below
7275                          (< score gnus-summary-mark-below))
7276                 (setq gnus-newsgroup-unreads
7277                       (delq number gnus-newsgroup-unreads))
7278                 (if gnus-newsgroup-auto-expire
7279                     (push number gnus-newsgroup-expirable)
7280                   (push (cons number gnus-low-score-mark)
7281                         gnus-newsgroup-reads)))
7282               t)
7283             ;; Do the `display' group parameter.
7284             (and gnus-newsgroup-display
7285                  (not (funcall gnus-newsgroup-display)))
7286             ;; Check NoCeM things.
7287             (if (and gnus-use-nocem
7288                      (gnus-nocem-unwanted-article-p
7289                       (mail-header-id (car thread))))
7290                 (progn
7291                   (setq gnus-newsgroup-unreads
7292                         (delq number gnus-newsgroup-unreads))
7293                   t))))
7294           ;; Nope, invisible article.
7295           0
7296         ;; Ok, this article is to be visible, so we add it to the limit
7297         ;; and return 1.
7298         (push number gnus-newsgroup-limit)
7299         1))))
7300
7301 (defun gnus-expunge-thread (thread)
7302   "Mark all articles in THREAD as read."
7303   (let* ((number (mail-header-number (car thread))))
7304     (incf gnus-newsgroup-expunged-tally)
7305     ;; We also mark as read here, if that's wanted.
7306     (setq gnus-newsgroup-unreads
7307           (delq number gnus-newsgroup-unreads))
7308     (if gnus-newsgroup-auto-expire
7309         (push number gnus-newsgroup-expirable)
7310       (push (cons number gnus-low-score-mark)
7311             gnus-newsgroup-reads)))
7312   ;; Go recursively through all subthreads.
7313   (mapcar 'gnus-expunge-thread (cdr thread)))
7314
7315 ;; Summary article oriented commands
7316
7317 (defun gnus-summary-refer-parent-article (n)
7318   "Refer parent article N times.
7319 If N is negative, go to ancestor -N instead.
7320 The difference between N and the number of articles fetched is returned."
7321   (interactive "p")
7322   (let ((skip 1)
7323         error header ref)
7324     (when (not (natnump n))
7325       (setq skip (abs n)
7326             n 1))
7327     (while (and (> n 0)
7328                 (not error))
7329       (setq header (gnus-summary-article-header))
7330       (if (and (eq (mail-header-number header)
7331                    (cdr gnus-article-current))
7332                (equal gnus-newsgroup-name
7333                       (car gnus-article-current)))
7334           ;; If we try to find the parent of the currently
7335           ;; displayed article, then we take a look at the actual
7336           ;; References header, since this is slightly more
7337           ;; reliable than the References field we got from the
7338           ;; server.
7339           (save-excursion
7340             (set-buffer gnus-original-article-buffer)
7341             (nnheader-narrow-to-headers)
7342             (unless (setq ref (message-fetch-field "references"))
7343               (setq ref (message-fetch-field "in-reply-to")))
7344             (widen))
7345         (setq ref
7346               ;; It's not the current article, so we take a bet on
7347               ;; the value we got from the server.
7348               (mail-header-references header)))
7349       (if (and ref
7350                (not (equal ref "")))
7351           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7352             (gnus-message 1 "Couldn't find parent"))
7353         (gnus-message 1 "No references in article %d"
7354                       (gnus-summary-article-number))
7355         (setq error t))
7356       (decf n))
7357     (gnus-summary-position-point)
7358     n))
7359
7360 (defun gnus-summary-refer-references ()
7361   "Fetch all articles mentioned in the References header.
7362 Return the number of articles fetched."
7363   (interactive)
7364   (let ((ref (mail-header-references (gnus-summary-article-header)))
7365         (current (gnus-summary-article-number))
7366         (n 0))
7367     (if (or (not ref)
7368             (equal ref ""))
7369         (error "No References in the current article")
7370       ;; For each Message-ID in the References header...
7371       (while (string-match "<[^>]*>" ref)
7372         (incf n)
7373         ;; ... fetch that article.
7374         (gnus-summary-refer-article
7375          (prog1 (match-string 0 ref)
7376            (setq ref (substring ref (match-end 0))))))
7377       (gnus-summary-goto-subject current)
7378       (gnus-summary-position-point)
7379       n)))
7380
7381 (defun gnus-summary-refer-thread (&optional limit)
7382   "Fetch all articles in the current thread.
7383 If LIMIT (the numerical prefix), fetch that many old headers instead
7384 of what's specified by the `gnus-refer-thread-limit' variable."
7385   (interactive "P")
7386   (let ((id (mail-header-id (gnus-summary-article-header)))
7387         (limit (if limit (prefix-numeric-value limit)
7388                  gnus-refer-thread-limit)))
7389     ;; We want to fetch LIMIT *old* headers, but we also have to
7390     ;; re-fetch all the headers in the current buffer, because many of
7391     ;; them may be undisplayed.  So we adjust LIMIT.
7392     (when (numberp limit)
7393       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7394     (unless (eq gnus-fetch-old-headers 'invisible)
7395       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7396       ;; Retrieve the headers and read them in.
7397       (if (eq (gnus-retrieve-headers
7398                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7399               'nov)
7400           (gnus-build-all-threads)
7401         (error "Can't fetch thread from backends that don't support NOV"))
7402       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7403     (gnus-summary-limit-include-thread id)))
7404
7405 (defun gnus-summary-refer-article (message-id)
7406   "Fetch an article specified by MESSAGE-ID."
7407   (interactive "sMessage-ID: ")
7408   (when (and (stringp message-id)
7409              (not (zerop (length message-id))))
7410     ;; Construct the correct Message-ID if necessary.
7411     ;; Suggested by tale@pawl.rpi.edu.
7412     (unless (string-match "^<" message-id)
7413       (setq message-id (concat "<" message-id)))
7414     (unless (string-match ">$" message-id)
7415       (setq message-id (concat message-id ">")))
7416     (let* ((header (gnus-id-to-header message-id))
7417            (sparse (and header
7418                         (gnus-summary-article-sparse-p
7419                          (mail-header-number header))
7420                         (memq (mail-header-number header)
7421                               gnus-newsgroup-limit)))
7422            number)
7423       (cond
7424        ;; If the article is present in the buffer we just go to it.
7425        ((and header
7426              (or (not (gnus-summary-article-sparse-p
7427                        (mail-header-number header)))
7428                  sparse))
7429         (prog1
7430             (gnus-summary-goto-article
7431              (mail-header-number header) nil t)
7432           (when sparse
7433             (gnus-summary-update-article (mail-header-number header)))))
7434        (t
7435         ;; We fetch the article.
7436         (catch 'found
7437           (dolist (gnus-override-method (gnus-refer-article-methods))
7438             (gnus-check-server gnus-override-method)
7439             ;; Fetch the header, and display the article.
7440             (when (setq number (gnus-summary-insert-subject message-id))
7441               (gnus-summary-select-article nil nil nil number)
7442               (throw 'found t)))
7443           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7444
7445 (defun gnus-refer-article-methods ()
7446   "Return a list of referrable methods."
7447   (cond
7448    ;; No method, so we default to current and native.
7449    ((null gnus-refer-article-method)
7450     (list gnus-current-select-method gnus-select-method))
7451    ;; Current.
7452    ((eq 'current gnus-refer-article-method)
7453     (list gnus-current-select-method))
7454    ;; List of select methods.
7455    ((not (and (symbolp (car gnus-refer-article-method))
7456               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7457     (let (out)
7458       (dolist (method gnus-refer-article-method)
7459         (push (if (eq 'current method)
7460                   gnus-current-select-method
7461                 method)
7462               out))
7463       (nreverse out)))
7464    ;; One single select method.
7465    (t
7466     (list gnus-refer-article-method))))
7467
7468 (defun gnus-summary-edit-parameters ()
7469   "Edit the group parameters of the current group."
7470   (interactive)
7471   (gnus-group-edit-group gnus-newsgroup-name 'params))
7472
7473 (defun gnus-summary-customize-parameters ()
7474   "Customize the group parameters of the current group."
7475   (interactive)
7476   (gnus-group-customize gnus-newsgroup-name))
7477
7478 (defun gnus-summary-enter-digest-group (&optional force)
7479   "Enter an nndoc group based on the current article.
7480 If FORCE, force a digest interpretation.  If not, try
7481 to guess what the document format is."
7482   (interactive "P")
7483   (let ((conf gnus-current-window-configuration))
7484     (save-excursion
7485       (gnus-summary-select-article))
7486     (setq gnus-current-window-configuration conf)
7487     (let* ((name (format "%s-%d"
7488                          (gnus-group-prefixed-name
7489                           gnus-newsgroup-name (list 'nndoc ""))
7490                          (save-excursion
7491                            (set-buffer gnus-summary-buffer)
7492                            gnus-current-article)))
7493            (ogroup gnus-newsgroup-name)
7494            (params (append (gnus-info-params (gnus-get-info ogroup))
7495                            (list (cons 'to-group ogroup))
7496                            (list (cons 'save-article-group ogroup))))
7497            (case-fold-search t)
7498            (buf (current-buffer))
7499            dig to-address)
7500       (save-excursion
7501         (set-buffer gnus-original-article-buffer)
7502         ;; Have the digest group inherit the main mail address of
7503         ;; the parent article.
7504         (when (setq to-address (or (message-fetch-field "reply-to")
7505                                    (message-fetch-field "from")))
7506           (setq params (append
7507                         (list (cons 'to-address
7508                                     (funcall gnus-decode-encoded-word-function
7509                                              to-address))))))
7510         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7511         (insert-buffer-substring gnus-original-article-buffer)
7512         ;; Remove lines that may lead nndoc to misinterpret the
7513         ;; document type.
7514         (narrow-to-region
7515          (goto-char (point-min))
7516          (or (search-forward "\n\n" nil t) (point)))
7517         (goto-char (point-min))
7518         (delete-matching-lines "^Path:\\|^From ")
7519         (widen))
7520       (unwind-protect
7521           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7522                     (gnus-newsgroup-ephemeral-ignored-charsets
7523                      gnus-newsgroup-ignored-charsets))
7524                 (gnus-group-read-ephemeral-group
7525                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7526                               (nndoc-article-type
7527                                ,(if force 'mbox 'guess))) t))
7528               ;; Make all postings to this group go to the parent group.
7529               (nconc (gnus-info-params (gnus-get-info name))
7530                      params)
7531             ;; Couldn't select this doc group.
7532             (switch-to-buffer buf)
7533             (gnus-set-global-variables)
7534             (gnus-configure-windows 'summary)
7535             (gnus-message 3 "Article couldn't be entered?"))
7536         (kill-buffer dig)))))
7537
7538 (defun gnus-summary-read-document (n)
7539   "Open a new group based on the current article(s).
7540 This will allow you to read digests and other similar
7541 documents as newsgroups.
7542 Obeys the standard process/prefix convention."
7543   (interactive "P")
7544   (let* ((articles (gnus-summary-work-articles n))
7545          (ogroup gnus-newsgroup-name)
7546          (params (append (gnus-info-params (gnus-get-info ogroup))
7547                          (list (cons 'to-group ogroup))))
7548          article group egroup groups vgroup)
7549     (while (setq article (pop articles))
7550       (setq group (format "%s-%d" gnus-newsgroup-name article))
7551       (gnus-summary-remove-process-mark article)
7552       (when (gnus-summary-display-article article)
7553         (save-excursion
7554           (with-temp-buffer
7555             (insert-buffer-substring gnus-original-article-buffer)
7556             ;; Remove some headers that may lead nndoc to make
7557             ;; the wrong guess.
7558             (message-narrow-to-head)
7559             (goto-char (point-min))
7560             (delete-matching-lines "^\\(Path\\):\\|^From ")
7561             (widen)
7562             (if (setq egroup
7563                       (gnus-group-read-ephemeral-group
7564                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7565                                      (nndoc-article-type guess))
7566                        t nil t))
7567                 (progn
7568                   ;; Make all postings to this group go to the parent group.
7569                   (nconc (gnus-info-params (gnus-get-info egroup))
7570                          params)
7571                   (push egroup groups))
7572               ;; Couldn't select this doc group.
7573               (gnus-error 3 "Article couldn't be entered"))))))
7574     ;; Now we have selected all the documents.
7575     (cond
7576      ((not groups)
7577       (error "None of the articles could be interpreted as documents"))
7578      ((gnus-group-read-ephemeral-group
7579        (setq vgroup (format
7580                      "nnvirtual:%s-%s" gnus-newsgroup-name
7581                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7582        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7583        t
7584        (cons (current-buffer) 'summary)))
7585      (t
7586       (error "Couldn't select virtual nndoc group")))))
7587
7588 (defun gnus-summary-isearch-article (&optional regexp-p)
7589   "Do incremental search forward on the current article.
7590 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7591   (interactive "P")
7592   (let* ((gnus-inhibit-treatment t)
7593          (old (gnus-summary-select-article)))
7594     (gnus-configure-windows 'article)
7595     (gnus-eval-in-buffer-window gnus-article-buffer
7596       (save-restriction
7597         (widen)
7598         (when (eq 'old old)
7599           (gnus-article-show-all-headers))
7600         (goto-char (point-min))
7601         (isearch-forward regexp-p)))))
7602
7603 (defun gnus-summary-search-article-forward (regexp &optional backward)
7604   "Search for an article containing REGEXP forward.
7605 If BACKWARD, search backward instead."
7606   (interactive
7607    (list (read-string
7608           (format "Search article %s (regexp%s): "
7609                   (if current-prefix-arg "backward" "forward")
7610                   (if gnus-last-search-regexp
7611                       (concat ", default " gnus-last-search-regexp)
7612                     "")))
7613          current-prefix-arg))
7614   (if (string-equal regexp "")
7615       (setq regexp (or gnus-last-search-regexp ""))
7616     (setq gnus-last-search-regexp regexp)
7617     (setq gnus-article-before-search gnus-current-article))
7618   ;; Intentionally set gnus-last-article.
7619   (setq gnus-last-article gnus-article-before-search)
7620   (let ((gnus-last-article gnus-last-article))
7621     (if (gnus-summary-search-article regexp backward)
7622         (gnus-summary-show-thread)
7623       (error "Search failed: \"%s\"" regexp))))
7624
7625 (defun gnus-summary-search-article-backward (regexp)
7626   "Search for an article containing REGEXP backward."
7627   (interactive
7628    (list (read-string
7629           (format "Search article backward (regexp%s): "
7630                   (if gnus-last-search-regexp
7631                       (concat ", default " gnus-last-search-regexp)
7632                     "")))))
7633   (gnus-summary-search-article-forward regexp 'backward))
7634
7635 (eval-when-compile
7636   (defmacro gnus-summary-search-article-position-point (regexp backward)
7637     "Dehighlight the last matched text and goto the beginning position."
7638     (` (if (and gnus-summary-search-article-matched-data
7639                 (let ((text (caddr gnus-summary-search-article-matched-data))
7640                       (inhibit-read-only t)
7641                       buffer-read-only)
7642                   (delete-region
7643                    (goto-char (car gnus-summary-search-article-matched-data))
7644                    (cadr gnus-summary-search-article-matched-data))
7645                   (insert text)
7646                   (string-match (, regexp) text)))
7647            (if (, backward) (beginning-of-line) (end-of-line))
7648          (goto-char (if (, backward) (point-max) (point-min))))))
7649
7650   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7651     "Place point where X-Face image is displayed."
7652     (if (featurep 'xemacs)
7653         (` (let ((end (if (search-forward "\n\n" nil t)
7654                           (goto-char (1- (point)))
7655                         (point-min)))
7656                  extent)
7657              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7658              (unless (and (re-search-forward "^From:" end t)
7659                           (setq extent (extent-at (point)))
7660                           (extent-begin-glyph extent))
7661                (goto-char (, opoint)))))
7662       (` (let ((end (if (search-forward "\n\n" nil t)
7663                         (goto-char (1- (point)))
7664                       (point-min)))
7665                (start (or (search-backward "\n\n" nil t) (point-min))))
7666            (goto-char
7667             (or (text-property-any start end 'x-face-image t);; x-face-e21
7668                 (text-property-any start end 'x-face-mule-bitmap-image t)
7669                 (, opoint)))))))
7670
7671   (defmacro gnus-summary-search-article-highlight-matched-text
7672     (backward treated x-face)
7673     "Highlight matched text in the function `gnus-summary-search-article'."
7674     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7675              (end (set-marker (make-marker) (match-end 0)))
7676              (inhibit-read-only t)
7677              buffer-read-only)
7678          (unless treated
7679            (let ((,@
7680                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7681                     (mapcar
7682                      (lambda (item) (setq items (delq item items)))
7683                      '(gnus-treat-buttonize
7684                        gnus-treat-fill-article
7685                        gnus-treat-fill-long-lines
7686                        gnus-treat-emphasize
7687                        gnus-treat-highlight-headers
7688                        gnus-treat-highlight-citation
7689                        gnus-treat-highlight-signature
7690                        gnus-treat-overstrike
7691                        gnus-treat-display-xface
7692                        gnus-treat-buttonize-head
7693                        gnus-treat-decode-article-as-default-mime-charset))
7694                     (static-if (featurep 'xemacs)
7695                         items
7696                       (cons '(x-face-mule-delete-x-face-field
7697                               (quote never))
7698                             items))))
7699                  (gnus-treat-display-xface
7700                   (when (, x-face) gnus-treat-display-xface)))
7701              (gnus-article-prepare-mime-display)))
7702          (goto-char (if (, backward) start end))
7703          (when (, x-face)
7704            (gnus-summary-search-article-highlight-goto-x-face (point)))
7705          (setq gnus-summary-search-article-matched-data
7706                (list start end (buffer-substring start end)))
7707          (unless (eq start end);; matched text has been deleted. :-<
7708            (put-text-property start end 'face
7709                               (or (find-face 'isearch)
7710                                   'secondary-selection))))))
7711   )
7712
7713 (defun gnus-summary-search-article (regexp &optional backward)
7714   "Search for an article containing REGEXP.
7715 Optional argument BACKWARD means do search for backward.
7716 `gnus-select-article-hook' is not called during the search."
7717   ;; We have to require this here to make sure that the following
7718   ;; dynamic binding isn't shadowed by autoloading.
7719   (require 'gnus-async)
7720   (require 'gnus-art)
7721   (let ((gnus-select-article-hook nil)  ;Disable hook.
7722         (gnus-article-prepare-hook nil)
7723         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7724         (gnus-use-article-prefetch nil)
7725         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7726         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7727         (sum (current-buffer))
7728         (found nil)
7729         point treated)
7730     (gnus-save-hidden-threads
7731       (static-if (featurep 'xemacs)
7732           (let ((gnus-inhibit-treatment t))
7733             (setq treated (eq 'old (gnus-summary-select-article)))
7734             (when (and treated
7735                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7736                                  (window-live-p (get-buffer-window
7737                                                  gnus-article-buffer t)))))
7738               (gnus-summary-select-article nil t)
7739               (setq treated nil)))
7740         (let ((gnus-inhibit-treatment t)
7741               (x-face-mule-delete-x-face-field 'never))
7742           (setq treated (eq 'old (gnus-summary-select-article)))
7743           (when (and treated
7744                      (not
7745                       (and (gnus-buffer-live-p gnus-article-buffer)
7746                            (window-live-p (get-buffer-window
7747                                            gnus-article-buffer t))
7748                            (or (not (string-match "^\\^X-Face:" regexp))
7749                                (with-current-buffer gnus-article-buffer
7750                                  gnus-summary-search-article-matched-data)))))
7751             (gnus-summary-select-article nil t)
7752             (setq treated nil))))
7753       (set-buffer gnus-article-buffer)
7754       (widen)
7755       (if treated
7756           (progn
7757             (gnus-article-show-all-headers)
7758             (gnus-summary-search-article-position-point regexp backward))
7759         (goto-char (if backward (point-max) (point-min))))
7760       (while (not found)
7761         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7762         (if (if backward
7763                 (re-search-backward regexp nil t)
7764               (re-search-forward regexp nil t))
7765             ;; We found the regexp.
7766             (progn
7767               (gnus-summary-search-article-highlight-matched-text
7768                backward treated (string-match "^\\^X-Face:" regexp))
7769               (setq found 'found)
7770               (forward-line
7771                (/ (- 2 (window-height
7772                         (get-buffer-window gnus-article-buffer t)))
7773                   2))
7774               (set-window-start
7775                (get-buffer-window (current-buffer))
7776                (point))
7777               (set-buffer sum)
7778               (setq point (point)))
7779           ;; We didn't find it, so we go to the next article.
7780           (set-buffer sum)
7781           (setq found 'not)
7782           (while (eq found 'not)
7783             (if (not (if backward (gnus-summary-find-prev)
7784                        (gnus-summary-find-next)))
7785                 ;; No more articles.
7786                 (setq found t)
7787               ;; Select the next article and adjust point.
7788               (unless (gnus-summary-article-sparse-p
7789                        (gnus-summary-article-number))
7790                 (setq found nil)
7791                 (let ((gnus-inhibit-treatment t))
7792                   (gnus-summary-select-article))
7793                 (setq treated nil)
7794                 (set-buffer gnus-article-buffer)
7795                 (widen)
7796                 (goto-char (if backward (point-max) (point-min))))))))
7797       (gnus-message 7 ""))
7798     ;; Return whether we found the regexp.
7799     (when (eq found 'found)
7800       (goto-char point)
7801       (gnus-summary-show-thread)
7802       (gnus-summary-goto-subject gnus-current-article)
7803       (gnus-summary-position-point)
7804       t)))
7805
7806 (defun gnus-find-matching-articles (header regexp)
7807   "Return a list of all articles that match REGEXP on HEADER.
7808 This search includes all articles in the current group that Gnus has
7809 fetched headers for, whether they are displayed or not."
7810   (let ((articles nil)
7811         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7812         (case-fold-search t))
7813     (dolist (header gnus-newsgroup-headers)
7814       (when (string-match regexp (funcall func header))
7815         (push (mail-header-number header) articles)))
7816     (nreverse articles)))
7817
7818 (defun gnus-summary-find-matching (header regexp &optional backward unread
7819                                           not-case-fold)
7820   "Return a list of all articles that match REGEXP on HEADER.
7821 The search stars on the current article and goes forwards unless
7822 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7823 If UNREAD is non-nil, only unread articles will
7824 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7825 in the comparisons."
7826   (let ((case-fold-search (not not-case-fold))
7827         articles d func)
7828     (if (consp header)
7829         (if (eq (car header) 'extra)
7830             (setq func
7831                   `(lambda (h)
7832                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7833                          "")))
7834           (error "%s is an invalid header" header))
7835       (unless (fboundp (intern (concat "mail-header-" header)))
7836         (error "%s is not a valid header" header))
7837       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7838     (dolist (d (if (eq backward 'all)
7839                    gnus-newsgroup-data
7840                  (gnus-data-find-list
7841                   (gnus-summary-article-number)
7842                   (gnus-data-list backward))))
7843       (when (and (or (not unread)       ; We want all articles...
7844                      (gnus-data-unread-p d)) ; Or just unreads.
7845                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7846                  (string-match regexp
7847                                (funcall func (gnus-data-header d)))) ; Match.
7848         (push (gnus-data-number d) articles))) ; Success!
7849     (nreverse articles)))
7850
7851 (defun gnus-summary-execute-command (header regexp command &optional backward)
7852   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7853 If HEADER is an empty string (or nil), the match is done on the entire
7854 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7855   (interactive
7856    (list (let ((completion-ignore-case t))
7857            (completing-read
7858             "Header name: "
7859             (mapcar (lambda (header) (list (format "%s" header)))
7860                     (append
7861                      '("Number" "Subject" "From" "Lines" "Date"
7862                        "Message-ID" "Xref" "References" "Body")
7863                      gnus-extra-headers))
7864             nil 'require-match))
7865          (read-string "Regexp: ")
7866          (read-key-sequence "Command: ")
7867          current-prefix-arg))
7868   (when (equal header "Body")
7869     (setq header ""))
7870   ;; Hidden thread subtrees must be searched as well.
7871   (gnus-summary-show-all-threads)
7872   ;; We don't want to change current point nor window configuration.
7873   (save-excursion
7874     (save-window-excursion
7875       (gnus-message 6 "Executing %s..." (key-description command))
7876       ;; We'd like to execute COMMAND interactively so as to give arguments.
7877       (gnus-execute header regexp
7878                     `(call-interactively ',(key-binding command))
7879                     backward)
7880       (gnus-message 6 "Executing %s...done" (key-description command)))))
7881
7882 (defun gnus-summary-beginning-of-article ()
7883   "Scroll the article back to the beginning."
7884   (interactive)
7885   (gnus-summary-select-article)
7886   (gnus-configure-windows 'article)
7887   (gnus-eval-in-buffer-window gnus-article-buffer
7888     (widen)
7889     (goto-char (point-min))
7890     (when gnus-page-broken
7891       (gnus-narrow-to-page))))
7892
7893 (defun gnus-summary-end-of-article ()
7894   "Scroll to the end of the article."
7895   (interactive)
7896   (gnus-summary-select-article)
7897   (gnus-configure-windows 'article)
7898   (gnus-eval-in-buffer-window gnus-article-buffer
7899     (widen)
7900     (goto-char (point-max))
7901     (recenter -3)
7902     (when gnus-page-broken
7903       (gnus-narrow-to-page))))
7904
7905 (defun gnus-summary-print-article (&optional filename n)
7906   "Generate and print a PostScript image of the N next (mail) articles.
7907
7908 If N is negative, print the N previous articles.  If N is nil and articles
7909 have been marked with the process mark, print these instead.
7910
7911 If the optional first argument FILENAME is nil, send the image to the
7912 printer.  If FILENAME is a string, save the PostScript image in a file with
7913 that name.  If FILENAME is a number, prompt the user for the name of the file
7914 to save in."
7915   (interactive (list (ps-print-preprint current-prefix-arg)))
7916   (dolist (article (gnus-summary-work-articles n))
7917     (gnus-summary-select-article nil nil 'pseudo article)
7918     (gnus-eval-in-buffer-window gnus-article-buffer
7919       (let ((buffer (generate-new-buffer " *print*")))
7920         (unwind-protect
7921             (progn
7922               (copy-to-buffer buffer (point-min) (point-max))
7923               (set-buffer buffer)
7924               (gnus-article-delete-invisible-text)
7925               (when (gnus-visual-p 'article-highlight 'highlight)
7926                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7927                 ;; highlight.
7928                 (let ((gnus-article-buffer buffer))
7929                   (gnus-article-highlight-citation t)
7930                   (gnus-article-highlight-signature)))
7931               (let ((ps-left-header
7932                      (list
7933                       (concat "("
7934                               (mail-header-subject gnus-current-headers) ")")
7935                       (concat "("
7936                               (mail-header-from gnus-current-headers) ")")))
7937                     (ps-right-header
7938                      (list
7939                       "/pagenumberstring load"
7940                       (concat "("
7941                               (mail-header-date gnus-current-headers) ")"))))
7942                 (gnus-run-hooks 'gnus-ps-print-hook)
7943                 (save-excursion
7944                   (if window-system
7945                       (ps-spool-buffer-with-faces)
7946                     (ps-spool-buffer)))))
7947           (kill-buffer buffer))))
7948     (gnus-summary-remove-process-mark article))
7949   (ps-despool filename))
7950
7951 (defun gnus-summary-show-article (&optional arg)
7952   "Force re-fetching of the current article.
7953 If ARG (the prefix) is a number, show the article with the charset
7954 defined in `gnus-summary-show-article-charset-alist', or the charset
7955 input.
7956 If ARG (the prefix) is non-nil and not a number, show the raw article
7957 without any article massaging functions being run."
7958   (interactive "P")
7959   (cond
7960    ((numberp arg)
7961     (gnus-summary-show-article t)
7962     (let ((gnus-newsgroup-charset
7963            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7964                (mm-read-coding-system
7965                 "View as charset: "
7966                 (save-excursion
7967                   (set-buffer gnus-article-buffer)
7968                   (detect-coding-region (point) (point-max) t)))))
7969           (gnus-newsgroup-ignored-charsets 'gnus-all))
7970       (gnus-summary-select-article nil 'force)
7971       (let ((deps gnus-newsgroup-dependencies)
7972             head header)
7973         (save-excursion
7974           (set-buffer gnus-original-article-buffer)
7975           (save-restriction
7976             (message-narrow-to-head)
7977             (setq head (buffer-string)))
7978           (with-temp-buffer
7979             (insert (format "211 %d Article retrieved.\n"
7980                             (cdr gnus-article-current)))
7981             (insert head)
7982             (insert ".\n")
7983             (let ((nntp-server-buffer (current-buffer)))
7984               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7985         (gnus-data-set-header
7986          (gnus-data-find (cdr gnus-article-current))
7987          header)
7988         (gnus-summary-update-article-line
7989          (cdr gnus-article-current) header)
7990         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7991           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7992    ((not arg)
7993     ;; Select the article the normal way.
7994     (gnus-summary-select-article nil 'force))
7995    (t
7996     ;; We have to require this here to make sure that the following
7997     ;; dynamic binding isn't shadowed by autoloading.
7998     (require 'gnus-async)
7999     (require 'gnus-art)
8000     ;; Bind the article treatment functions to nil.
8001     (let ((gnus-have-all-headers t)
8002           gnus-article-prepare-hook
8003           gnus-article-decode-hook
8004           gnus-break-pages
8005           gnus-show-mime
8006           (gnus-inhibit-treatment t))
8007       (gnus-summary-select-article nil 'force))))
8008   (gnus-summary-goto-subject gnus-current-article)
8009   (gnus-summary-position-point))
8010
8011 (defun gnus-summary-show-raw-article ()
8012   "Show the raw article without any article massaging functions being run."
8013   (interactive)
8014   (gnus-summary-show-article t))
8015
8016 (defun gnus-summary-verbose-headers (&optional arg)
8017   "Toggle permanent full header display.
8018 If ARG is a positive number, turn header display on.
8019 If ARG is a negative number, turn header display off."
8020   (interactive "P")
8021   (setq gnus-show-all-headers
8022         (cond ((or (not (numberp arg))
8023                    (zerop arg))
8024                (not gnus-show-all-headers))
8025               ((natnump arg)
8026                t)))
8027   (gnus-summary-show-article))
8028
8029 (defun gnus-summary-toggle-header (&optional arg)
8030   "Show the headers if they are hidden, or hide them if they are shown.
8031 If ARG is a positive number, show the entire header.
8032 If ARG is a negative number, hide the unwanted header lines."
8033   (interactive "P")
8034   (save-excursion
8035     (set-buffer gnus-article-buffer)
8036     (save-restriction
8037       (let* ((buffer-read-only nil)
8038              (inhibit-point-motion-hooks t)
8039              hidden e)
8040         (setq hidden
8041               (if (numberp arg)
8042                   (>= arg 0)
8043                 (save-restriction
8044                   (article-narrow-to-head)
8045                   (gnus-article-hidden-text-p 'headers))))
8046         (goto-char (point-min))
8047         (when (search-forward "\n\n" nil t)
8048           (delete-region (point-min) (1- (point))))
8049         (goto-char (point-min))
8050         (save-excursion
8051           (set-buffer gnus-original-article-buffer)
8052           (goto-char (point-min))
8053           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8054         (insert-buffer-substring gnus-original-article-buffer 1 e)
8055         (save-restriction
8056           (narrow-to-region (point-min) (point))
8057           (article-decode-encoded-words)
8058           (if  hidden
8059               (let ((gnus-treat-hide-headers nil)
8060                     (gnus-treat-hide-boring-headers nil))
8061                 (setq gnus-article-wash-types
8062                       (delq 'headers gnus-article-wash-types))
8063                 (gnus-treat-article 'head))
8064             (gnus-treat-article 'head)))
8065         (gnus-set-mode-line 'article)))))
8066
8067 (defun gnus-summary-show-all-headers ()
8068   "Make all header lines visible."
8069   (interactive)
8070   (gnus-summary-toggle-header 1))
8071
8072 (defun gnus-summary-toggle-mime (&optional arg)
8073   "Toggle MIME processing.
8074 If ARG is a positive number, turn MIME processing on."
8075   (interactive "P")
8076   (setq gnus-show-mime
8077         (if (null arg)
8078             (not gnus-show-mime)
8079           (> (prefix-numeric-value arg) 0)))
8080   (gnus-summary-select-article t 'force))
8081
8082 (defun gnus-summary-caesar-message (&optional arg)
8083   "Caesar rotate the current article by 13.
8084 The numerical prefix specifies how many places to rotate each letter
8085 forward."
8086   (interactive "P")
8087   (gnus-summary-select-article)
8088   (let ((mail-header-separator ""))
8089     (gnus-eval-in-buffer-window gnus-article-buffer
8090       (save-restriction
8091         (widen)
8092         (let ((start (window-start))
8093               buffer-read-only)
8094           (message-caesar-buffer-body arg)
8095           (set-window-start (get-buffer-window (current-buffer)) start))))))
8096
8097 (defun gnus-summary-stop-page-breaking ()
8098   "Stop page breaking in the current article."
8099   (interactive)
8100   (gnus-summary-select-article)
8101   (gnus-eval-in-buffer-window gnus-article-buffer
8102     (widen)
8103     (when (gnus-visual-p 'page-marker)
8104       (let ((buffer-read-only nil))
8105         (gnus-remove-text-with-property 'gnus-prev)
8106         (gnus-remove-text-with-property 'gnus-next))
8107       (setq gnus-page-broken nil))))
8108
8109 (defun gnus-summary-move-article (&optional n to-newsgroup
8110                                             select-method action)
8111   "Move the current article to a different newsgroup.
8112 If N is a positive number, move the N next articles.
8113 If N is a negative number, move the N previous articles.
8114 If N is nil and any articles have been marked with the process mark,
8115 move those articles instead.
8116 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8117 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8118 re-spool using this method.
8119
8120 For this function to work, both the current newsgroup and the
8121 newsgroup that you want to move to have to support the `request-move'
8122 and `request-accept' functions.
8123
8124 ACTION can be either `move' (the default), `crosspost' or `copy'."
8125   (interactive "P")
8126   (unless action
8127     (setq action 'move))
8128   ;; Check whether the source group supports the required functions.
8129   (cond ((and (eq action 'move)
8130               (not (gnus-check-backend-function
8131                     'request-move-article gnus-newsgroup-name)))
8132          (error "The current group does not support article moving"))
8133         ((and (eq action 'crosspost)
8134               (not (gnus-check-backend-function
8135                     'request-replace-article gnus-newsgroup-name)))
8136          (error "The current group does not support article editing")))
8137   (let ((articles (gnus-summary-work-articles n))
8138         (prefix (if (gnus-check-backend-function
8139                      'request-move-article gnus-newsgroup-name)
8140                     (gnus-group-real-prefix gnus-newsgroup-name)
8141                   ""))
8142         (names '((move "Move" "Moving")
8143                  (copy "Copy" "Copying")
8144                  (crosspost "Crosspost" "Crossposting")))
8145         (copy-buf (save-excursion
8146                     (nnheader-set-temp-buffer " *copy article*")))
8147         (default-marks gnus-article-mark-lists)
8148         (no-expire-marks (delete '(expirable . expire)
8149                                  (copy-sequence gnus-article-mark-lists)))
8150         art-group to-method new-xref article to-groups)
8151     (unless (assq action names)
8152       (error "Unknown action %s" action))
8153     ;; Read the newsgroup name.
8154     (when (and (not to-newsgroup)
8155                (not select-method))
8156       (setq to-newsgroup
8157             (gnus-read-move-group-name
8158              (cadr (assq action names))
8159              (symbol-value (intern (format "gnus-current-%s-group" action)))
8160              articles prefix))
8161       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8162     (setq to-method (or select-method
8163                         (gnus-server-to-method
8164                          (gnus-group-method to-newsgroup))))
8165     ;; Check the method we are to move this article to...
8166     (unless (gnus-check-backend-function
8167              'request-accept-article (car to-method))
8168       (error "%s does not support article copying" (car to-method)))
8169     (unless (gnus-check-server to-method)
8170       (error "Can't open server %s" (car to-method)))
8171     (gnus-message 6 "%s to %s: %s..."
8172                   (caddr (assq action names))
8173                   (or (car select-method) to-newsgroup) articles)
8174     (while articles
8175       (setq article (pop articles))
8176       (setq
8177        art-group
8178        (cond
8179         ;; Move the article.
8180         ((eq action 'move)
8181          ;; Remove this article from future suppression.
8182          (gnus-dup-unsuppress-article article)
8183          (gnus-request-move-article
8184           article                       ; Article to move
8185           gnus-newsgroup-name           ; From newsgroup
8186           (nth 1 (gnus-find-method-for-group
8187                   gnus-newsgroup-name)) ; Server
8188           (list 'gnus-request-accept-article
8189                 to-newsgroup (list 'quote select-method)
8190                 (not articles) t)       ; Accept form
8191           (not articles)))              ; Only save nov last time
8192         ;; Copy the article.
8193         ((eq action 'copy)
8194          (save-excursion
8195            (set-buffer copy-buf)
8196            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8197              (gnus-request-accept-article
8198               to-newsgroup select-method (not articles) t))))
8199         ;; Crosspost the article.
8200         ((eq action 'crosspost)
8201          (let ((xref (message-tokenize-header
8202                       (mail-header-xref (gnus-summary-article-header article))
8203                       " ")))
8204            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8205                                   ":" article))
8206            (unless xref
8207              (setq xref (list (system-name))))
8208            (setq new-xref
8209                  (concat
8210                   (mapconcat 'identity
8211                              (delete "Xref:" (delete new-xref xref))
8212                              " ")
8213                   " " new-xref))
8214            (save-excursion
8215              (set-buffer copy-buf)
8216              ;; First put the article in the destination group.
8217              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8218              (when (consp (setq art-group
8219                                 (gnus-request-accept-article
8220                                  to-newsgroup select-method (not articles))))
8221                (setq new-xref (concat new-xref " " (car art-group)
8222                                       ":" (cdr art-group)))
8223                ;; Now we have the new Xrefs header, so we insert
8224                ;; it and replace the new article.
8225                (nnheader-replace-header "Xref" new-xref)
8226                (gnus-request-replace-article
8227                 (cdr art-group) to-newsgroup (current-buffer))
8228                art-group))))))
8229       (cond
8230        ((not art-group)
8231         (gnus-message 1 "Couldn't %s article %s: %s"
8232                       (cadr (assq action names)) article
8233                       (nnheader-get-report (car to-method))))
8234        ((eq art-group 'junk)
8235         (when (eq action 'move)
8236           (gnus-summary-mark-article article gnus-canceled-mark)
8237           (gnus-message 4 "Deleted article %s" article)))
8238        (t
8239         (let* ((pto-group (gnus-group-prefixed-name
8240                            (car art-group) to-method))
8241                (entry
8242                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8243                (info (nth 2 entry))
8244                (to-group (gnus-info-group info))
8245                to-marks)
8246           ;; Update the group that has been moved to.
8247           (when (and info
8248                      (memq action '(move copy)))
8249             (unless (member to-group to-groups)
8250               (push to-group to-groups))
8251
8252             (unless (memq article gnus-newsgroup-unreads)
8253               (push 'read to-marks)
8254               (gnus-info-set-read
8255                info (gnus-add-to-range (gnus-info-read info)
8256                                        (list (cdr art-group)))))
8257
8258             ;; See whether the article is to be put in the cache.
8259             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8260                              default-marks
8261                            no-expire-marks))
8262                   (to-article (cdr art-group)))
8263
8264               ;; Enter the article into the cache in the new group,
8265               ;; if that is required.
8266               (when gnus-use-cache
8267                 (gnus-cache-possibly-enter-article
8268                  to-group to-article
8269                  (let ((header (copy-sequence
8270                                 (gnus-summary-article-header article))))
8271                    (mail-header-set-number header to-article)
8272                    header)
8273                  (memq article gnus-newsgroup-marked)
8274                  (memq article gnus-newsgroup-dormant)
8275                  (memq article gnus-newsgroup-unreads)))
8276
8277               (when gnus-preserve-marks
8278                 ;; Copy any marks over to the new group.
8279                 (when (and (equal to-group gnus-newsgroup-name)
8280                            (not (memq article gnus-newsgroup-unreads)))
8281                   ;; Mark this article as read in this group.
8282                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8283                   (setcdr (gnus-active to-group) to-article)
8284                   (setcdr gnus-newsgroup-active to-article))
8285
8286                 (while marks
8287                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8288                     (when (memq article (symbol-value
8289                                          (intern (format "gnus-newsgroup-%s"
8290                                                          (caar marks)))))
8291                       (push (cdar marks) to-marks)
8292                       ;; If the other group is the same as this group,
8293                       ;; then we have to add the mark to the list.
8294                       (when (equal to-group gnus-newsgroup-name)
8295                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8296                              (cons to-article
8297                                    (symbol-value
8298                                     (intern (format "gnus-newsgroup-%s"
8299                                                     (caar marks)))))))
8300                       ;; Copy the marks to other group.
8301                       (gnus-add-marked-articles
8302                        to-group (cdar marks) (list to-article) info)))
8303                   (setq marks (cdr marks)))
8304
8305                 (gnus-request-set-mark to-group (list (list (list to-article)
8306                                                             'add
8307                                                             to-marks))))
8308
8309               (gnus-dribble-enter
8310                (concat "(gnus-group-set-info '"
8311                        (gnus-prin1-to-string (gnus-get-info to-group))
8312                        ")"))))
8313
8314           ;; Update the Xref header in this article to point to
8315           ;; the new crossposted article we have just created.
8316           (when (eq action 'crosspost)
8317             (save-excursion
8318               (set-buffer copy-buf)
8319               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8320               (nnheader-replace-header "Xref" new-xref)
8321               (gnus-request-replace-article
8322                article gnus-newsgroup-name (current-buffer)))))
8323
8324         ;;;!!!Why is this necessary?
8325         (set-buffer gnus-summary-buffer)
8326
8327         (gnus-summary-goto-subject article)
8328         (when (eq action 'move)
8329           (gnus-summary-mark-article article gnus-canceled-mark))))
8330       (gnus-summary-remove-process-mark article))
8331     ;; Re-activate all groups that have been moved to.
8332     (while to-groups
8333       (save-excursion
8334         (set-buffer gnus-group-buffer)
8335         (when (gnus-group-goto-group (car to-groups) t)
8336           (gnus-group-get-new-news-this-group 1 t))
8337         (pop to-groups)))
8338
8339     (gnus-kill-buffer copy-buf)
8340     (gnus-summary-position-point)
8341     (gnus-set-mode-line 'summary)))
8342
8343 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8344   "Move the current article to a different newsgroup.
8345 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8346 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8347 re-spool using this method."
8348   (interactive "P")
8349   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8350
8351 (defun gnus-summary-crosspost-article (&optional n)
8352   "Crosspost the current article to some other group."
8353   (interactive "P")
8354   (gnus-summary-move-article n nil nil 'crosspost))
8355
8356 (defcustom gnus-summary-respool-default-method nil
8357   "Default method for respooling an article.
8358 If nil, use to the current newsgroup method."
8359   :type '(choice (gnus-select-method :value (nnml ""))
8360                  (const nil))
8361   :group 'gnus-summary-mail)
8362
8363 (defun gnus-summary-respool-article (&optional n method)
8364   "Respool the current article.
8365 The article will be squeezed through the mail spooling process again,
8366 which means that it will be put in some mail newsgroup or other
8367 depending on `nnmail-split-methods'.
8368 If N is a positive number, respool the N next articles.
8369 If N is a negative number, respool the N previous articles.
8370 If N is nil and any articles have been marked with the process mark,
8371 respool those articles instead.
8372
8373 Respooling can be done both from mail groups and \"real\" newsgroups.
8374 In the former case, the articles in question will be moved from the
8375 current group into whatever groups they are destined to.  In the
8376 latter case, they will be copied into the relevant groups."
8377   (interactive
8378    (list current-prefix-arg
8379          (let* ((methods (gnus-methods-using 'respool))
8380                 (methname
8381                  (symbol-name (or gnus-summary-respool-default-method
8382                                   (car (gnus-find-method-for-group
8383                                         gnus-newsgroup-name)))))
8384                 (method
8385                  (gnus-completing-read
8386                   methname "What backend do you want to use when respooling?"
8387                   methods nil t nil 'gnus-mail-method-history))
8388                 ms)
8389            (cond
8390             ((zerop (length (setq ms (gnus-servers-using-backend
8391                                       (intern method)))))
8392              (list (intern method) ""))
8393             ((= 1 (length ms))
8394              (car ms))
8395             (t
8396              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8397                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8398                            ms-alist))))))))
8399   (unless method
8400     (error "No method given for respooling"))
8401   (if (assoc (symbol-name
8402               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8403              (gnus-methods-using 'respool))
8404       (gnus-summary-move-article n nil method)
8405     (gnus-summary-copy-article n nil method)))
8406
8407 (defun gnus-summary-import-article (file &optional edit)
8408   "Import an arbitrary file into a mail newsgroup."
8409   (interactive "fImport file: \nP")
8410   (let ((group gnus-newsgroup-name)
8411         (now (current-time))
8412         atts lines group-art)
8413     (unless (gnus-check-backend-function 'request-accept-article group)
8414       (error "%s does not support article importing" group))
8415     (or (file-readable-p file)
8416         (not (file-regular-p file))
8417         (error "Can't read %s" file))
8418     (save-excursion
8419       (set-buffer (gnus-get-buffer-create " *import file*"))
8420       (erase-buffer)
8421       (nnheader-insert-file-contents file)
8422       (goto-char (point-min))
8423       (if (nnheader-article-p)
8424           (save-restriction
8425             (goto-char (point-min))
8426             (search-forward "\n\n" nil t)
8427             (narrow-to-region (point-min) (1- (point)))
8428             (goto-char (point-min))
8429             (unless (re-search-forward "^date:" nil t)
8430               (goto-char (point-max))
8431               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8432         ;; This doesn't look like an article, so we fudge some headers.
8433         (setq atts (file-attributes file)
8434               lines (count-lines (point-min) (point-max)))
8435         (insert "From: " (read-string "From: ") "\n"
8436                 "Subject: " (read-string "Subject: ") "\n"
8437                 "Date: " (message-make-date (nth 5 atts)) "\n"
8438                 "Message-ID: " (message-make-message-id) "\n"
8439                 "Lines: " (int-to-string lines) "\n"
8440                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8441       (setq group-art (gnus-request-accept-article group nil t))
8442       (kill-buffer (current-buffer)))
8443     (setq gnus-newsgroup-active (gnus-activate-group group))
8444     (forward-line 1)
8445     (gnus-summary-goto-article (cdr group-art) nil t)
8446     (when edit
8447       (gnus-summary-edit-article))))
8448
8449 (defun gnus-summary-create-article ()
8450   "Create an article in a mail newsgroup."
8451   (interactive)
8452   (let ((group gnus-newsgroup-name)
8453         (now (current-time))
8454         group-art)
8455     (unless (gnus-check-backend-function 'request-accept-article group)
8456       (error "%s does not support article importing" group))
8457     (save-excursion
8458       (set-buffer (gnus-get-buffer-create " *import file*"))
8459       (erase-buffer)
8460       (goto-char (point-min))
8461       ;; This doesn't look like an article, so we fudge some headers.
8462       (insert "From: " (read-string "From: ") "\n"
8463               "Subject: " (read-string "Subject: ") "\n"
8464               "Date: " (message-make-date now) "\n"
8465               "Message-ID: " (message-make-message-id) "\n")
8466       (setq group-art (gnus-request-accept-article group nil t))
8467       (kill-buffer (current-buffer)))
8468     (setq gnus-newsgroup-active (gnus-activate-group group))
8469     (forward-line 1)
8470     (gnus-summary-goto-article (cdr group-art) nil t)
8471     (gnus-summary-edit-article)))
8472
8473 (defun gnus-summary-article-posted-p ()
8474   "Say whether the current (mail) article is available from news as well.
8475 This will be the case if the article has both been mailed and posted."
8476   (interactive)
8477   (let ((id (mail-header-references (gnus-summary-article-header)))
8478         (gnus-override-method (car (gnus-refer-article-methods))))
8479     (if (gnus-request-head id "")
8480         (gnus-message 2 "The current message was found on %s"
8481                       gnus-override-method)
8482       (gnus-message 2 "The current message couldn't be found on %s"
8483                     gnus-override-method)
8484       nil)))
8485
8486 (defun gnus-summary-expire-articles (&optional now)
8487   "Expire all articles that are marked as expirable in the current group."
8488   (interactive)
8489   (when (gnus-check-backend-function
8490          'request-expire-articles gnus-newsgroup-name)
8491     ;; This backend supports expiry.
8492     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8493            (expirable (if total
8494                           (progn
8495                             ;; We need to update the info for
8496                             ;; this group for `gnus-list-of-read-articles'
8497                             ;; to give us the right answer.
8498                             (gnus-run-hooks 'gnus-exit-group-hook)
8499                             (gnus-summary-update-info)
8500                             (gnus-list-of-read-articles gnus-newsgroup-name))
8501                         (setq gnus-newsgroup-expirable
8502                               (sort gnus-newsgroup-expirable '<))))
8503            (expiry-wait (if now 'immediate
8504                           (gnus-group-find-parameter
8505                            gnus-newsgroup-name 'expiry-wait)))
8506            (nnmail-expiry-target
8507             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8508                 nnmail-expiry-target))
8509            es)
8510       (when expirable
8511         ;; There are expirable articles in this group, so we run them
8512         ;; through the expiry process.
8513         (gnus-message 6 "Expiring articles...")
8514         (unless (gnus-check-group gnus-newsgroup-name)
8515           (error "Can't open server for %s" gnus-newsgroup-name))
8516         ;; The list of articles that weren't expired is returned.
8517         (save-excursion
8518           (if expiry-wait
8519               (let ((nnmail-expiry-wait-function nil)
8520                     (nnmail-expiry-wait expiry-wait))
8521                 (setq es (gnus-request-expire-articles
8522                           expirable gnus-newsgroup-name)))
8523             (setq es (gnus-request-expire-articles
8524                       expirable gnus-newsgroup-name)))
8525           (unless total
8526             (setq gnus-newsgroup-expirable es))
8527           ;; We go through the old list of expirable, and mark all
8528           ;; really expired articles as nonexistent.
8529           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8530             (let ((gnus-use-cache nil))
8531               (while expirable
8532                 (unless (memq (car expirable) es)
8533                   (when (gnus-data-find (car expirable))
8534                     (gnus-summary-mark-article
8535                      (car expirable) gnus-canceled-mark)))
8536                 (setq expirable (cdr expirable))))))
8537         (gnus-message 6 "Expiring articles...done")))))
8538
8539 (defun gnus-summary-expire-articles-now ()
8540   "Expunge all expirable articles in the current group.
8541 This means that *all* articles that are marked as expirable will be
8542 deleted forever, right now."
8543   (interactive)
8544   (or gnus-expert-user
8545       (gnus-yes-or-no-p
8546        "Are you really, really, really sure you want to delete all these messages? ")
8547       (error "Phew!"))
8548   (gnus-summary-expire-articles t))
8549
8550 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8551 (defun gnus-summary-delete-article (&optional n)
8552   "Delete the N next (mail) articles.
8553 This command actually deletes articles.  This is not a marking
8554 command.  The article will disappear forever from your life, never to
8555 return.
8556 If N is negative, delete backwards.
8557 If N is nil and articles have been marked with the process mark,
8558 delete these instead."
8559   (interactive "P")
8560   (unless (gnus-check-backend-function 'request-expire-articles
8561                                        gnus-newsgroup-name)
8562     (error "The current newsgroup does not support article deletion"))
8563   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8564     (error "Couldn't open server"))
8565   ;; Compute the list of articles to delete.
8566   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8567         not-deleted)
8568     (if (and gnus-novice-user
8569              (not (gnus-yes-or-no-p
8570                    (format "Do you really want to delete %s forever? "
8571                            (if (> (length articles) 1)
8572                                (format "these %s articles" (length articles))
8573                              "this article")))))
8574         ()
8575       ;; Delete the articles.
8576       (setq not-deleted (gnus-request-expire-articles
8577                          articles gnus-newsgroup-name 'force))
8578       (while articles
8579         (gnus-summary-remove-process-mark (car articles))
8580         ;; The backend might not have been able to delete the article
8581         ;; after all.
8582         (unless (memq (car articles) not-deleted)
8583           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8584         (setq articles (cdr articles)))
8585       (when not-deleted
8586         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8587     (gnus-summary-position-point)
8588     (gnus-set-mode-line 'summary)
8589     not-deleted))
8590
8591 (defun gnus-summary-edit-article (&optional force)
8592   "Edit the current article.
8593 This will have permanent effect only in mail groups.
8594 If FORCE is non-nil, allow editing of articles even in read-only
8595 groups."
8596   (interactive "P")
8597   (save-excursion
8598     (set-buffer gnus-summary-buffer)
8599     (let ((mail-parse-charset gnus-newsgroup-charset)
8600           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8601       (gnus-set-global-variables)
8602       (when (and (not force)
8603                  (gnus-group-read-only-p))
8604         (error "The current newsgroup does not support article editing"))
8605       (gnus-summary-show-article t)
8606       (gnus-article-edit-article
8607        'ignore
8608        `(lambda (no-highlight)
8609           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8610                 (message-options message-options)
8611                 (message-options-set-recipient)
8612                 (mail-parse-ignored-charsets
8613                  ',gnus-newsgroup-ignored-charsets))
8614             (gnus-summary-edit-article-done
8615              ,(or (mail-header-references gnus-current-headers) "")
8616              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8617
8618 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8619
8620 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8621                                                  no-highlight)
8622   "Make edits to the current article permanent."
8623   (interactive)
8624   (save-excursion
8625     ;; The buffer restriction contains the entire article if it exists.
8626     (when (article-goto-body)
8627       (let ((lines (count-lines (point) (point-max)))
8628             (length (- (point-max) (point)))
8629             (case-fold-search t)
8630             (body (copy-marker (point))))
8631         (goto-char (point-min))
8632         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8633           (delete-region (match-beginning 1) (match-end 1))
8634           (insert (number-to-string length)))
8635         (goto-char (point-min))
8636         (when (re-search-forward
8637                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8638           (delete-region (match-beginning 1) (match-end 1))
8639           (insert (number-to-string length)))
8640         (goto-char (point-min))
8641         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8642           (delete-region (match-beginning 1) (match-end 1))
8643           (insert (number-to-string lines))))))
8644   ;; Replace the article.
8645   (let ((buf (current-buffer)))
8646     (with-temp-buffer
8647       (insert-buffer-substring buf)
8648
8649       (if (and (not read-only)
8650                (not (gnus-request-replace-article
8651                      (cdr gnus-article-current) (car gnus-article-current)
8652                      (current-buffer) t)))
8653           (error "Couldn't replace article")
8654         ;; Update the summary buffer.
8655         (if (and references
8656                  (equal (message-tokenize-header references " ")
8657                         (message-tokenize-header
8658                          (or (message-fetch-field "references") "") " ")))
8659             ;; We only have to update this line.
8660             (save-excursion
8661               (save-restriction
8662                 (message-narrow-to-head)
8663                 (let ((head (buffer-string))
8664                       header)
8665                   (with-temp-buffer
8666                     (insert (format "211 %d Article retrieved.\n"
8667                                     (cdr gnus-article-current)))
8668                     (insert head)
8669                     (insert ".\n")
8670                     (let ((nntp-server-buffer (current-buffer)))
8671                       (setq header (car (gnus-get-newsgroup-headers
8672                                          (save-excursion
8673                                            (set-buffer gnus-summary-buffer)
8674                                            gnus-newsgroup-dependencies)
8675                                          t))))
8676                     (save-excursion
8677                       (set-buffer gnus-summary-buffer)
8678                       (gnus-data-set-header
8679                        (gnus-data-find (cdr gnus-article-current))
8680                        header)
8681                       (gnus-summary-update-article-line
8682                        (cdr gnus-article-current) header)
8683                       (if (gnus-summary-goto-subject
8684                            (cdr gnus-article-current) nil t)
8685                           (gnus-summary-update-secondary-mark
8686                            (cdr gnus-article-current))))))))
8687           ;; Update threads.
8688           (set-buffer (or buffer gnus-summary-buffer))
8689           (gnus-summary-update-article (cdr gnus-article-current))
8690           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8691               (gnus-summary-update-secondary-mark
8692                (cdr gnus-article-current))))
8693         ;; Prettify the article buffer again.
8694         (unless no-highlight
8695           (save-excursion
8696             (set-buffer gnus-article-buffer)
8697             ;;;!!! Fix this -- article should be rehighlighted.
8698             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8699             (set-buffer gnus-original-article-buffer)
8700             (gnus-request-article
8701              (cdr gnus-article-current)
8702              (car gnus-article-current) (current-buffer))))
8703         ;; Prettify the summary buffer line.
8704         (when (gnus-visual-p 'summary-highlight 'highlight)
8705           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8706
8707 (defun gnus-summary-edit-wash (key)
8708   "Perform editing command KEY in the article buffer."
8709   (interactive
8710    (list
8711     (progn
8712       (message "%s" (concat (this-command-keys) "- "))
8713       (read-char))))
8714   (message "")
8715   (gnus-summary-edit-article)
8716   (execute-kbd-macro (concat (this-command-keys) key))
8717   (gnus-article-edit-done))
8718
8719 ;;; Respooling
8720
8721 (defun gnus-summary-respool-query (&optional silent trace)
8722   "Query where the respool algorithm would put this article."
8723   (interactive)
8724   (let (gnus-mark-article-hook)
8725     (gnus-summary-select-article)
8726     (save-excursion
8727       (set-buffer gnus-original-article-buffer)
8728       (save-restriction
8729         (message-narrow-to-head)
8730         (let ((groups (nnmail-article-group 'identity trace)))
8731           (unless silent
8732             (if groups
8733                 (message "This message would go to %s"
8734                          (mapconcat 'car groups ", "))
8735               (message "This message would go to no groups"))
8736             groups))))))
8737
8738 (defun gnus-summary-respool-trace ()
8739   "Trace where the respool algorithm would put this article.
8740 Display a buffer showing all fancy splitting patterns which matched."
8741   (interactive)
8742   (gnus-summary-respool-query nil t))
8743
8744 ;; Summary marking commands.
8745
8746 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8747   "Mark articles which has the same subject as read, and then select the next.
8748 If UNMARK is positive, remove any kind of mark.
8749 If UNMARK is negative, tick articles."
8750   (interactive "P")
8751   (when unmark
8752     (setq unmark (prefix-numeric-value unmark)))
8753   (let ((count
8754          (gnus-summary-mark-same-subject
8755           (gnus-summary-article-subject) unmark)))
8756     ;; Select next unread article.  If auto-select-same mode, should
8757     ;; select the first unread article.
8758     (gnus-summary-next-article t (and gnus-auto-select-same
8759                                       (gnus-summary-article-subject)))
8760     (gnus-message 7 "%d article%s marked as %s"
8761                   count (if (= count 1) " is" "s are")
8762                   (if unmark "unread" "read"))))
8763
8764 (defun gnus-summary-kill-same-subject (&optional unmark)
8765   "Mark articles which has the same subject as read.
8766 If UNMARK is positive, remove any kind of mark.
8767 If UNMARK is negative, tick articles."
8768   (interactive "P")
8769   (when unmark
8770     (setq unmark (prefix-numeric-value unmark)))
8771   (let ((count
8772          (gnus-summary-mark-same-subject
8773           (gnus-summary-article-subject) unmark)))
8774     ;; If marked as read, go to next unread subject.
8775     (when (null unmark)
8776       ;; Go to next unread subject.
8777       (gnus-summary-next-subject 1 t))
8778     (gnus-message 7 "%d articles are marked as %s"
8779                   count (if unmark "unread" "read"))))
8780
8781 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8782   "Mark articles with same SUBJECT as read, and return marked number.
8783 If optional argument UNMARK is positive, remove any kinds of marks.
8784 If optional argument UNMARK is negative, mark articles as unread instead."
8785   (let ((count 1))
8786     (save-excursion
8787       (cond
8788        ((null unmark)                   ; Mark as read.
8789         (while (and
8790                 (progn
8791                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8792                   (gnus-summary-show-thread) t)
8793                 (gnus-summary-find-subject subject))
8794           (setq count (1+ count))))
8795        ((> unmark 0)                    ; Tick.
8796         (while (and
8797                 (progn
8798                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8799                   (gnus-summary-show-thread) t)
8800                 (gnus-summary-find-subject subject))
8801           (setq count (1+ count))))
8802        (t                               ; Mark as unread.
8803         (while (and
8804                 (progn
8805                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8806                   (gnus-summary-show-thread) t)
8807                 (gnus-summary-find-subject subject))
8808           (setq count (1+ count)))))
8809       (gnus-set-mode-line 'summary)
8810       ;; Return the number of marked articles.
8811       count)))
8812
8813 (defun gnus-summary-mark-as-processable (n &optional unmark)
8814   "Set the process mark on the next N articles.
8815 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8816 the process mark instead.  The difference between N and the actual
8817 number of articles marked is returned."
8818   (interactive "P")
8819   (if (and (null n) (gnus-region-active-p))
8820       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8821     (setq n (prefix-numeric-value n))
8822     (let ((backward (< n 0))
8823           (n (abs n)))
8824       (while (and
8825               (> n 0)
8826               (if unmark
8827                   (gnus-summary-remove-process-mark
8828                    (gnus-summary-article-number))
8829                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8830               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8831         (setq n (1- n)))
8832       (when (/= 0 n)
8833         (gnus-message 7 "No more articles"))
8834       (gnus-summary-recenter)
8835       (gnus-summary-position-point)
8836       n)))
8837
8838 (defun gnus-summary-unmark-as-processable (n)
8839   "Remove the process mark from the next N articles.
8840 If N is negative, unmark backward instead.  The difference between N and
8841 the actual number of articles unmarked is returned."
8842   (interactive "P")
8843   (gnus-summary-mark-as-processable n t))
8844
8845 (defun gnus-summary-unmark-all-processable ()
8846   "Remove the process mark from all articles."
8847   (interactive)
8848   (save-excursion
8849     (while gnus-newsgroup-processable
8850       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8851   (gnus-summary-position-point))
8852
8853 (defun gnus-summary-add-mark (article type)
8854   "Mark ARTICLE with a mark of TYPE."
8855   (let ((vtype (car (assq type gnus-article-mark-lists)))
8856         var)
8857     (if (not vtype)
8858         (error "No such mark type: %s" type)
8859       (setq var (intern (format "gnus-newsgroup-%s" type)))
8860       (set var (cons article (symbol-value var)))
8861       (if (memq type '(processable cached replied forwarded recent saved))
8862           (gnus-summary-update-secondary-mark article)
8863         ;;; !!! This is bogus.  We should find out what primary
8864         ;;; !!! mark we want to set.
8865         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8866
8867 (defun gnus-summary-mark-as-expirable (n)
8868   "Mark N articles forward as expirable.
8869 If N is negative, mark backward instead.  The difference between N and
8870 the actual number of articles marked is returned."
8871   (interactive "p")
8872   (gnus-summary-mark-forward n gnus-expirable-mark))
8873
8874 (defun gnus-summary-mark-article-as-replied (article)
8875   "Mark ARTICLE as replied to and update the summary line.
8876 ARTICLE can also be a list of articles."
8877   (interactive (list (gnus-summary-article-number)))
8878   (let ((articles (if (listp article) article (list article))))
8879     (dolist (article articles)
8880       (push article gnus-newsgroup-replied)
8881       (let ((buffer-read-only nil))
8882         (when (gnus-summary-goto-subject article nil t)
8883           (gnus-summary-update-secondary-mark article))))))
8884
8885 (defun gnus-summary-mark-article-as-forwarded (article)
8886   "Mark ARTICLE as forwarded and update the summary line.
8887 ARTICLE can also be a list of articles."
8888   (let ((articles (if (listp article) article (list article))))
8889     (dolist (article articles)
8890       (push article gnus-newsgroup-forwarded)
8891       (let ((buffer-read-only nil))
8892         (when (gnus-summary-goto-subject article nil t)
8893           (gnus-summary-update-secondary-mark article))))))
8894
8895 (defun gnus-summary-set-bookmark (article)
8896   "Set a bookmark in current article."
8897   (interactive (list (gnus-summary-article-number)))
8898   (when (or (not (get-buffer gnus-article-buffer))
8899             (not gnus-current-article)
8900             (not gnus-article-current)
8901             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8902     (error "No current article selected"))
8903   ;; Remove old bookmark, if one exists.
8904   (let ((old (assq article gnus-newsgroup-bookmarks)))
8905     (when old
8906       (setq gnus-newsgroup-bookmarks
8907             (delq old gnus-newsgroup-bookmarks))))
8908   ;; Set the new bookmark, which is on the form
8909   ;; (article-number . line-number-in-body).
8910   (push
8911    (cons article
8912          (save-excursion
8913            (set-buffer gnus-article-buffer)
8914            (count-lines
8915             (min (point)
8916                  (save-excursion
8917                    (goto-char (point-min))
8918                    (search-forward "\n\n" nil t)
8919                    (point)))
8920             (point))))
8921    gnus-newsgroup-bookmarks)
8922   (gnus-message 6 "A bookmark has been added to the current article."))
8923
8924 (defun gnus-summary-remove-bookmark (article)
8925   "Remove the bookmark from the current article."
8926   (interactive (list (gnus-summary-article-number)))
8927   ;; Remove old bookmark, if one exists.
8928   (let ((old (assq article gnus-newsgroup-bookmarks)))
8929     (if old
8930         (progn
8931           (setq gnus-newsgroup-bookmarks
8932                 (delq old gnus-newsgroup-bookmarks))
8933           (gnus-message 6 "Removed bookmark."))
8934       (gnus-message 6 "No bookmark in current article."))))
8935
8936 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8937 (defun gnus-summary-mark-as-dormant (n)
8938   "Mark N articles forward as dormant.
8939 If N is negative, mark backward instead.  The difference between N and
8940 the actual number of articles marked is returned."
8941   (interactive "p")
8942   (gnus-summary-mark-forward n gnus-dormant-mark))
8943
8944 (defun gnus-summary-set-process-mark (article)
8945   "Set the process mark on ARTICLE and update the summary line."
8946   (setq gnus-newsgroup-processable
8947         (cons article
8948               (delq article gnus-newsgroup-processable)))
8949   (when (gnus-summary-goto-subject article)
8950     (gnus-summary-show-thread)
8951     (gnus-summary-goto-subject article)
8952     (gnus-summary-update-secondary-mark article)))
8953
8954 (defun gnus-summary-remove-process-mark (article)
8955   "Remove the process mark from ARTICLE and update the summary line."
8956   (setq gnus-newsgroup-processable (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-set-saved-mark (article)
8963   "Set the process mark on ARTICLE and update the summary line."
8964   (push article gnus-newsgroup-saved)
8965   (when (gnus-summary-goto-subject article)
8966     (gnus-summary-update-secondary-mark article)))
8967
8968 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8969   "Mark N articles as read forwards.
8970 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8971 The difference between N and the actual number of articles marked is
8972 returned.
8973 Iff NO-EXPIRE, auto-expiry will be inhibited."
8974   (interactive "p")
8975   (gnus-summary-show-thread)
8976   (let ((backward (< n 0))
8977         (gnus-summary-goto-unread
8978          (and gnus-summary-goto-unread
8979               (not (eq gnus-summary-goto-unread 'never))
8980               (not (memq mark (list gnus-unread-mark
8981                                     gnus-ticked-mark gnus-dormant-mark)))))
8982         (n (abs n))
8983         (mark (or mark gnus-del-mark)))
8984     (while (and (> n 0)
8985                 (gnus-summary-mark-article nil mark no-expire)
8986                 (zerop (gnus-summary-next-subject
8987                         (if backward -1 1)
8988                         (and gnus-summary-goto-unread
8989                              (not (eq gnus-summary-goto-unread 'never)))
8990                         t)))
8991       (setq n (1- n)))
8992     (when (/= 0 n)
8993       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8994     (gnus-summary-recenter)
8995     (gnus-summary-position-point)
8996     (gnus-set-mode-line 'summary)
8997     n))
8998
8999 (defun gnus-summary-mark-article-as-read (mark)
9000   "Mark the current article quickly as read with MARK."
9001   (let ((article (gnus-summary-article-number)))
9002     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9003     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9004     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9005     (push (cons article mark) gnus-newsgroup-reads)
9006     ;; Possibly remove from cache, if that is used.
9007     (when gnus-use-cache
9008       (gnus-cache-enter-remove-article article))
9009     ;; Allow the backend to change the mark.
9010     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9011     ;; Check for auto-expiry.
9012     (when (and gnus-newsgroup-auto-expire
9013                (memq mark gnus-auto-expirable-marks))
9014       (setq mark gnus-expirable-mark)
9015       ;; Let the backend know about the mark change.
9016       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9017       (push article gnus-newsgroup-expirable))
9018     ;; Set the mark in the buffer.
9019     (gnus-summary-update-mark mark 'unread)
9020     t))
9021
9022 (defun gnus-summary-mark-article-as-unread (mark)
9023   "Mark the current article quickly as unread with MARK."
9024   (let* ((article (gnus-summary-article-number))
9025          (old-mark (gnus-summary-article-mark article)))
9026     ;; Allow the backend to change the mark.
9027     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9028     (if (eq mark old-mark)
9029         t
9030       (if (<= article 0)
9031           (progn
9032             (gnus-error 1 "Can't mark negative article numbers")
9033             nil)
9034         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9035         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9036         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9037         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9038         (cond ((= mark gnus-ticked-mark)
9039                (push article gnus-newsgroup-marked))
9040               ((= mark gnus-dormant-mark)
9041                (push article gnus-newsgroup-dormant))
9042               (t
9043                (push article gnus-newsgroup-unreads)))
9044         (gnus-pull article gnus-newsgroup-reads)
9045
9046         ;; See whether the article is to be put in the cache.
9047         (and gnus-use-cache
9048              (vectorp (gnus-summary-article-header article))
9049              (save-excursion
9050                (gnus-cache-possibly-enter-article
9051                 gnus-newsgroup-name article
9052                 (gnus-summary-article-header article)
9053                 (= mark gnus-ticked-mark)
9054                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9055
9056         ;; Fix the mark.
9057         (gnus-summary-update-mark mark 'unread)
9058         t))))
9059
9060 (defun gnus-summary-mark-article (&optional article mark no-expire)
9061   "Mark ARTICLE with MARK.  MARK can be any character.
9062 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9063 `??' (dormant) and `?E' (expirable).
9064 If MARK is nil, then the default character `?r' is used.
9065 If ARTICLE is nil, then the article on the current line will be
9066 marked.
9067 Iff NO-EXPIRE, auto-expiry will be inhibited."
9068   ;; The mark might be a string.
9069   (when (stringp mark)
9070     (setq mark (aref mark 0)))
9071   ;; If no mark is given, then we check auto-expiring.
9072   (when (null mark)
9073     (setq mark gnus-del-mark))
9074   (when (and (not no-expire)
9075              gnus-newsgroup-auto-expire
9076              (memq mark gnus-auto-expirable-marks))
9077     (setq mark gnus-expirable-mark))
9078   (let ((article (or article (gnus-summary-article-number)))
9079         (old-mark (gnus-summary-article-mark article)))
9080     ;; Allow the backend to change the mark.
9081     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9082     (if (eq mark old-mark)
9083         t
9084       (unless article
9085         (error "No article on current line"))
9086       (if (not (if (or (= mark gnus-unread-mark)
9087                        (= mark gnus-ticked-mark)
9088                        (= mark gnus-dormant-mark))
9089                    (gnus-mark-article-as-unread article mark)
9090                  (gnus-mark-article-as-read article mark)))
9091           t
9092         ;; See whether the article is to be put in the cache.
9093         (and gnus-use-cache
9094              (not (= mark gnus-canceled-mark))
9095              (vectorp (gnus-summary-article-header article))
9096              (save-excursion
9097                (gnus-cache-possibly-enter-article
9098                 gnus-newsgroup-name article
9099                 (gnus-summary-article-header article)
9100                 (= mark gnus-ticked-mark)
9101                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9102
9103         (when (gnus-summary-goto-subject article nil t)
9104           (let ((buffer-read-only nil))
9105             (gnus-summary-show-thread)
9106             ;; Fix the mark.
9107             (gnus-summary-update-mark mark 'unread)
9108             t))))))
9109
9110 (defun gnus-summary-update-secondary-mark (article)
9111   "Update the secondary (read, process, cache) mark."
9112   (gnus-summary-update-mark
9113    (cond ((memq article gnus-newsgroup-processable)
9114           gnus-process-mark)
9115          ((memq article gnus-newsgroup-cached)
9116           gnus-cached-mark)
9117          ((memq article gnus-newsgroup-replied)
9118           gnus-replied-mark)
9119          ((memq article gnus-newsgroup-forwarded)
9120           gnus-forwarded-mark)
9121          ((memq article gnus-newsgroup-saved)
9122           gnus-saved-mark)
9123          ((memq article gnus-newsgroup-recent)
9124           gnus-recent-mark)
9125          ((memq article gnus-newsgroup-unseen)
9126           gnus-unseen-mark)
9127          (t gnus-no-mark))
9128    'replied)
9129   (when (gnus-visual-p 'summary-highlight 'highlight)
9130     (gnus-run-hooks 'gnus-summary-update-hook))
9131   t)
9132
9133 (defun gnus-summary-update-mark (mark type)
9134   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9135         (buffer-read-only nil))
9136     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9137     (when forward
9138       (when (looking-at "\r")
9139         (incf forward))
9140       (when (<= (+ forward (point)) (point-max))
9141         ;; Go to the right position on the line.
9142         (goto-char (+ forward (point)))
9143         ;; Replace the old mark with the new mark.
9144         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9145         ;; Optionally update the marks by some user rule.
9146         (when (eq type 'unread)
9147           (gnus-data-set-mark
9148            (gnus-data-find (gnus-summary-article-number)) mark)
9149           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9150
9151 (defun gnus-mark-article-as-read (article &optional mark)
9152   "Enter ARTICLE in the pertinent lists and remove it from others."
9153   ;; Make the article expirable.
9154   (let ((mark (or mark gnus-del-mark)))
9155     (if (= mark gnus-expirable-mark)
9156         (push article gnus-newsgroup-expirable)
9157       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9158     ;; Remove from unread and marked lists.
9159     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9160     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9161     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9162     (push (cons article mark) gnus-newsgroup-reads)
9163     ;; Possibly remove from cache, if that is used.
9164     (when gnus-use-cache
9165       (gnus-cache-enter-remove-article article))
9166     t))
9167
9168 (defun gnus-mark-article-as-unread (article &optional mark)
9169   "Enter ARTICLE in the pertinent lists and remove it from others."
9170   (let ((mark (or mark gnus-ticked-mark)))
9171     (if (<= article 0)
9172         (progn
9173           (gnus-error 1 "Can't mark negative article numbers")
9174           nil)
9175       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9176             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9177             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9178             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9179
9180       ;; Unsuppress duplicates?
9181       (when gnus-suppress-duplicates
9182         (gnus-dup-unsuppress-article article))
9183
9184       (cond ((= mark gnus-ticked-mark)
9185              (push article gnus-newsgroup-marked))
9186             ((= mark gnus-dormant-mark)
9187              (push article gnus-newsgroup-dormant))
9188             (t
9189              (push article gnus-newsgroup-unreads)))
9190       (gnus-pull article gnus-newsgroup-reads)
9191       t)))
9192
9193 (defalias 'gnus-summary-mark-as-unread-forward
9194   'gnus-summary-tick-article-forward)
9195 (make-obsolete 'gnus-summary-mark-as-unread-forward
9196                'gnus-summary-tick-article-forward)
9197 (defun gnus-summary-tick-article-forward (n)
9198   "Tick N articles forwards.
9199 If N is negative, tick backwards instead.
9200 The difference between N and the number of articles ticked is returned."
9201   (interactive "p")
9202   (gnus-summary-mark-forward n gnus-ticked-mark))
9203
9204 (defalias 'gnus-summary-mark-as-unread-backward
9205   'gnus-summary-tick-article-backward)
9206 (make-obsolete 'gnus-summary-mark-as-unread-backward
9207                'gnus-summary-tick-article-backward)
9208 (defun gnus-summary-tick-article-backward (n)
9209   "Tick N articles backwards.
9210 The difference between N and the number of articles ticked is returned."
9211   (interactive "p")
9212   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9213
9214 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9215 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9216 (defun gnus-summary-tick-article (&optional article clear-mark)
9217   "Mark current article as unread.
9218 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9219 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9220   (interactive)
9221   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9222                                        gnus-ticked-mark)))
9223
9224 (defun gnus-summary-mark-as-read-forward (n)
9225   "Mark N articles as read forwards.
9226 If N is negative, mark backwards instead.
9227 The difference between N and the actual number of articles marked is
9228 returned."
9229   (interactive "p")
9230   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9231
9232 (defun gnus-summary-mark-as-read-backward (n)
9233   "Mark the N articles as read backwards.
9234 The difference between N and the actual number of articles marked is
9235 returned."
9236   (interactive "p")
9237   (gnus-summary-mark-forward
9238    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9239
9240 (defun gnus-summary-mark-as-read (&optional article mark)
9241   "Mark current article as read.
9242 ARTICLE specifies the article to be marked as read.
9243 MARK specifies a string to be inserted at the beginning of the line."
9244   (gnus-summary-mark-article article mark))
9245
9246 (defun gnus-summary-clear-mark-forward (n)
9247   "Clear marks from N articles forward.
9248 If N is negative, clear backward instead.
9249 The difference between N and the number of marks cleared is returned."
9250   (interactive "p")
9251   (gnus-summary-mark-forward n gnus-unread-mark))
9252
9253 (defun gnus-summary-clear-mark-backward (n)
9254   "Clear marks from N articles backward.
9255 The difference between N and the number of marks cleared is returned."
9256   (interactive "p")
9257   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9258
9259 (defun gnus-summary-mark-unread-as-read ()
9260   "Intended to be used by `gnus-summary-mark-article-hook'."
9261   (when (memq gnus-current-article gnus-newsgroup-unreads)
9262     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9263
9264 (defun gnus-summary-mark-read-and-unread-as-read ()
9265   "Intended to be used by `gnus-summary-mark-article-hook'."
9266   (let ((mark (gnus-summary-article-mark)))
9267     (when (or (gnus-unread-mark-p mark)
9268               (gnus-read-mark-p mark))
9269       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9270
9271 (defun gnus-summary-mark-unread-as-ticked ()
9272   "Intended to be used by `gnus-summary-mark-article-hook'."
9273   (when (memq gnus-current-article gnus-newsgroup-unreads)
9274     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9275
9276 (defun gnus-summary-mark-region-as-read (point mark all)
9277   "Mark all unread articles between point and mark as read.
9278 If given a prefix, mark all articles between point and mark as read,
9279 even ticked and dormant ones."
9280   (interactive "r\nP")
9281   (save-excursion
9282     (let (article)
9283       (goto-char point)
9284       (beginning-of-line)
9285       (while (and
9286               (< (point) mark)
9287               (progn
9288                 (when (or all
9289                           (memq (setq article (gnus-summary-article-number))
9290                                 gnus-newsgroup-unreads))
9291                   (gnus-summary-mark-article article gnus-del-mark))
9292                 t)
9293               (gnus-summary-find-next))))))
9294
9295 (defun gnus-summary-mark-below (score mark)
9296   "Mark articles with score less than SCORE with MARK."
9297   (interactive "P\ncMark: ")
9298   (setq score (if score
9299                   (prefix-numeric-value score)
9300                 (or gnus-summary-default-score 0)))
9301   (save-excursion
9302     (set-buffer gnus-summary-buffer)
9303     (goto-char (point-min))
9304     (while
9305         (progn
9306           (and (< (gnus-summary-article-score) score)
9307                (gnus-summary-mark-article nil mark))
9308           (gnus-summary-find-next)))))
9309
9310 (defun gnus-summary-kill-below (&optional score)
9311   "Mark articles with score below SCORE as read."
9312   (interactive "P")
9313   (gnus-summary-mark-below score gnus-killed-mark))
9314
9315 (defun gnus-summary-clear-above (&optional score)
9316   "Clear all marks from articles with score above SCORE."
9317   (interactive "P")
9318   (gnus-summary-mark-above score gnus-unread-mark))
9319
9320 (defun gnus-summary-tick-above (&optional score)
9321   "Tick all articles with score above SCORE."
9322   (interactive "P")
9323   (gnus-summary-mark-above score gnus-ticked-mark))
9324
9325 (defun gnus-summary-mark-above (score mark)
9326   "Mark articles with score over SCORE with MARK."
9327   (interactive "P\ncMark: ")
9328   (setq score (if score
9329                   (prefix-numeric-value score)
9330                 (or gnus-summary-default-score 0)))
9331   (save-excursion
9332     (set-buffer gnus-summary-buffer)
9333     (goto-char (point-min))
9334     (while (and (progn
9335                   (when (> (gnus-summary-article-score) score)
9336                     (gnus-summary-mark-article nil mark))
9337                   t)
9338                 (gnus-summary-find-next)))))
9339
9340 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9341 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9342 (defun gnus-summary-limit-include-expunged (&optional no-error)
9343   "Display all the hidden articles that were expunged for low scores."
9344   (interactive)
9345   (let ((buffer-read-only nil))
9346     (let ((scored gnus-newsgroup-scored)
9347           headers h)
9348       (while scored
9349         (unless (gnus-summary-article-header (caar scored))
9350           (and (setq h (gnus-number-to-header (caar scored)))
9351                (< (cdar scored) gnus-summary-expunge-below)
9352                (push h headers)))
9353         (setq scored (cdr scored)))
9354       (if (not headers)
9355           (when (not no-error)
9356             (error "No expunged articles hidden"))
9357         (goto-char (point-min))
9358         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9359         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9360         (mapcar (lambda (x) (push (mail-header-number x)
9361                                   gnus-newsgroup-limit))
9362                 headers)
9363         (gnus-summary-prepare-unthreaded (nreverse headers))
9364         (goto-char (point-min))
9365         (gnus-summary-position-point)
9366         t))))
9367
9368 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9369   "Mark all unread articles in this newsgroup as read.
9370 If prefix argument ALL is non-nil, ticked and dormant articles will
9371 also be marked as read.
9372 If QUIETLY is non-nil, no questions will be asked.
9373 If TO-HERE is non-nil, it should be a point in the buffer.  All
9374 articles before (after, if REVERSE is set) this point will be marked as read.
9375 Note that this function will only catch up the unread article
9376 in the current summary buffer limitation.
9377 The number of articles marked as read is returned."
9378   (interactive "P")
9379   (prog1
9380       (save-excursion
9381         (when (or quietly
9382                   (not gnus-interactive-catchup) ;Without confirmation?
9383                   gnus-expert-user
9384                   (gnus-y-or-n-p
9385                    (if all
9386                        "Mark absolutely all articles as read? "
9387                      "Mark all unread articles as read? ")))
9388           (if (and not-mark
9389                    (not gnus-newsgroup-adaptive)
9390                    (not gnus-newsgroup-auto-expire)
9391                    (not gnus-suppress-duplicates)
9392                    (or (not gnus-use-cache)
9393                        (eq gnus-use-cache 'passive)))
9394               (progn
9395                 (when all
9396                   (setq gnus-newsgroup-marked nil
9397                         gnus-newsgroup-dormant nil))
9398                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9399             ;; We actually mark all articles as canceled, which we
9400             ;; have to do when using auto-expiry or adaptive scoring.
9401             (gnus-summary-show-all-threads)
9402             (if (and to-here reverse)
9403                 (progn
9404                   (goto-char to-here)
9405                   (while (and
9406                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9407                           (gnus-summary-find-next (not all) nil nil t))))
9408               (when (gnus-summary-first-subject (not all) t)
9409                 (while (and
9410                         (if to-here (< (point) to-here) t)
9411                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9412                         (gnus-summary-find-next (not all) nil nil t)))))
9413             (gnus-set-mode-line 'summary))
9414           t))
9415     (gnus-summary-position-point)))
9416
9417 (defun gnus-summary-catchup-to-here (&optional all)
9418   "Mark all unticked articles before the current one as read.
9419 If ALL is non-nil, also mark ticked and dormant articles as read."
9420   (interactive "P")
9421   (save-excursion
9422     (gnus-save-hidden-threads
9423       (let ((beg (point)))
9424         ;; We check that there are unread articles.
9425         (when (or all (gnus-summary-find-prev))
9426           (gnus-summary-catchup all t beg)))))
9427   (gnus-summary-position-point))
9428
9429 (defun gnus-summary-catchup-from-here (&optional all)
9430   "Mark all unticked articles after the current one as read.
9431 If ALL is non-nil, also mark ticked and dormant articles as read."
9432   (interactive "P")
9433   (save-excursion
9434     (gnus-save-hidden-threads
9435       (let ((beg (point)))
9436         ;; We check that there are unread articles.
9437         (when (or all (gnus-summary-find-next))
9438           (gnus-summary-catchup all t beg nil t)))))
9439   (gnus-summary-position-point))
9440
9441 (defun gnus-summary-catchup-all (&optional quietly)
9442   "Mark all articles in this newsgroup as read."
9443   (interactive "P")
9444   (gnus-summary-catchup t quietly))
9445
9446 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9447   "Mark all unread articles in this group as read, then exit.
9448 If prefix argument ALL is non-nil, all articles are marked as read.
9449 If QUIETLY is non-nil, no questions will be asked."
9450   (interactive "P")
9451   (when (gnus-summary-catchup all quietly nil 'fast)
9452     ;; Select next newsgroup or exit.
9453     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9454              (eq gnus-auto-select-next 'quietly))
9455         (gnus-summary-next-group nil)
9456       (gnus-summary-exit))))
9457
9458 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9459   "Mark all articles in this newsgroup as read, and then exit."
9460   (interactive "P")
9461   (gnus-summary-catchup-and-exit t quietly))
9462
9463 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9464   "Mark all articles in this group as read and select the next group.
9465 If given a prefix, mark all articles, unread as well as ticked, as
9466 read."
9467   (interactive "P")
9468   (save-excursion
9469     (gnus-summary-catchup all))
9470   (gnus-summary-next-group))
9471
9472 ;;;
9473 ;;; with article
9474 ;;;
9475
9476 (defmacro gnus-with-article (article &rest forms)
9477   "Select ARTICLE and perform FORMS in the original article buffer.
9478 Then replace the article with the result."
9479   `(progn
9480      ;; We don't want the article to be marked as read.
9481      (let (gnus-mark-article-hook)
9482        (gnus-summary-select-article t t nil ,article))
9483      (set-buffer gnus-original-article-buffer)
9484      ,@forms
9485      (if (not (gnus-check-backend-function
9486                'request-replace-article (car gnus-article-current)))
9487          (gnus-message 5 "Read-only group; not replacing")
9488        (unless (gnus-request-replace-article
9489                 ,article (car gnus-article-current)
9490                 (current-buffer) t)
9491          (error "Couldn't replace article")))
9492      ;; The cache and backlog have to be flushed somewhat.
9493      (when gnus-keep-backlog
9494        (gnus-backlog-remove-article
9495         (car gnus-article-current) (cdr gnus-article-current)))
9496      (when gnus-use-cache
9497        (gnus-cache-update-article
9498         (car gnus-article-current) (cdr gnus-article-current)))))
9499
9500 (put 'gnus-with-article 'lisp-indent-function 1)
9501 (put 'gnus-with-article 'edebug-form-spec '(form body))
9502
9503 ;; Thread-based commands.
9504
9505 (defun gnus-summary-articles-in-thread (&optional article)
9506   "Return a list of all articles in the current thread.
9507 If ARTICLE is non-nil, return all articles in the thread that starts
9508 with that article."
9509   (let* ((article (or article (gnus-summary-article-number)))
9510          (data (gnus-data-find-list article))
9511          (top-level (gnus-data-level (car data)))
9512          (top-subject
9513           (cond ((null gnus-thread-operation-ignore-subject)
9514                  (gnus-simplify-subject-re
9515                   (mail-header-subject (gnus-data-header (car data)))))
9516                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9517                  (gnus-simplify-subject-fuzzy
9518                   (mail-header-subject (gnus-data-header (car data)))))
9519                 (t nil)))
9520          (end-point (save-excursion
9521                       (if (gnus-summary-go-to-next-thread)
9522                           (point) (point-max))))
9523          articles)
9524     (while (and data
9525                 (< (gnus-data-pos (car data)) end-point))
9526       (when (or (not top-subject)
9527                 (string= top-subject
9528                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9529                              (gnus-simplify-subject-fuzzy
9530                               (mail-header-subject
9531                                (gnus-data-header (car data))))
9532                            (gnus-simplify-subject-re
9533                             (mail-header-subject
9534                              (gnus-data-header (car data)))))))
9535         (push (gnus-data-number (car data)) articles))
9536       (unless (and (setq data (cdr data))
9537                    (> (gnus-data-level (car data)) top-level))
9538         (setq data nil)))
9539     ;; Return the list of articles.
9540     (nreverse articles)))
9541
9542 (defun gnus-summary-rethread-current ()
9543   "Rethread the thread the current article is part of."
9544   (interactive)
9545   (let* ((gnus-show-threads t)
9546          (article (gnus-summary-article-number))
9547          (id (mail-header-id (gnus-summary-article-header)))
9548          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9549     (unless id
9550       (error "No article on the current line"))
9551     (gnus-rebuild-thread id)
9552     (gnus-summary-goto-subject article)))
9553
9554 (defun gnus-summary-reparent-thread ()
9555   "Make the current article child of the marked (or previous) article.
9556
9557 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9558 is non-nil or the Subject: of both articles are the same."
9559   (interactive)
9560   (unless (not (gnus-group-read-only-p))
9561     (error "The current newsgroup does not support article editing"))
9562   (unless (<= (length gnus-newsgroup-processable) 1)
9563     (error "No more than one article may be marked"))
9564   (save-window-excursion
9565     (let ((gnus-article-buffer " *reparent*")
9566           (current-article (gnus-summary-article-number))
9567           ;; First grab the marked article, otherwise one line up.
9568           (parent-article (if (not (null gnus-newsgroup-processable))
9569                               (car gnus-newsgroup-processable)
9570                             (save-excursion
9571                               (if (eq (forward-line -1) 0)
9572                                   (gnus-summary-article-number)
9573                                 (error "Beginning of summary buffer"))))))
9574       (unless (not (eq current-article parent-article))
9575         (error "An article may not be self-referential"))
9576       (let ((message-id (mail-header-id
9577                          (gnus-summary-article-header parent-article))))
9578         (unless (and message-id (not (equal message-id "")))
9579           (error "No message-id in desired parent"))
9580         (gnus-with-article current-article
9581           (save-restriction
9582             (goto-char (point-min))
9583             (message-narrow-to-head)
9584             (if (re-search-forward "^References: " nil t)
9585                 (progn
9586                   (re-search-forward "^[^ \t]" nil t)
9587                   (forward-line -1)
9588                   (end-of-line)
9589                   (insert " " message-id))
9590               (insert "References: " message-id "\n"))))
9591         (set-buffer gnus-summary-buffer)
9592         (gnus-summary-unmark-all-processable)
9593         (gnus-summary-update-article current-article)
9594         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9595             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9596         (gnus-summary-rethread-current)
9597         (gnus-message 3 "Article %d is now the child of article %d"
9598                       current-article parent-article)))))
9599
9600 (defun gnus-summary-toggle-threads (&optional arg)
9601   "Toggle showing conversation threads.
9602 If ARG is positive number, turn showing conversation threads on."
9603   (interactive "P")
9604   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9605     (setq gnus-show-threads
9606           (if (null arg) (not gnus-show-threads)
9607             (> (prefix-numeric-value arg) 0)))
9608     (gnus-summary-prepare)
9609     (gnus-summary-goto-subject current)
9610     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9611     (gnus-summary-position-point)))
9612
9613 (defun gnus-summary-show-all-threads ()
9614   "Show all threads."
9615   (interactive)
9616   (save-excursion
9617     (let ((buffer-read-only nil))
9618       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9619   (gnus-summary-position-point))
9620
9621 (defun gnus-summary-show-thread ()
9622   "Show thread subtrees.
9623 Returns nil if no thread was there to be shown."
9624   (interactive)
9625   (let ((buffer-read-only nil)
9626         (orig (point))
9627         ;; first goto end then to beg, to have point at beg after let
9628         (end (progn (end-of-line) (point)))
9629         (beg (progn (beginning-of-line) (point))))
9630     (prog1
9631         ;; Any hidden lines here?
9632         (search-forward "\r" end t)
9633       (subst-char-in-region beg end ?\^M ?\n t)
9634       (goto-char orig)
9635       (gnus-summary-position-point))))
9636
9637 (defun gnus-summary-hide-all-threads ()
9638   "Hide all thread subtrees."
9639   (interactive)
9640   (save-excursion
9641     (goto-char (point-min))
9642     (gnus-summary-hide-thread)
9643     (while (zerop (gnus-summary-next-thread 1 t))
9644       (gnus-summary-hide-thread)))
9645   (gnus-summary-position-point))
9646
9647 (defun gnus-summary-hide-thread ()
9648   "Hide thread subtrees.
9649 Returns nil if no threads were there to be hidden."
9650   (interactive)
9651   (let ((buffer-read-only nil)
9652         (start (point))
9653         (article (gnus-summary-article-number)))
9654     (goto-char start)
9655     ;; Go forward until either the buffer ends or the subthread
9656     ;; ends.
9657     (when (and (not (eobp))
9658                (or (zerop (gnus-summary-next-thread 1 t))
9659                    (goto-char (point-max))))
9660       (prog1
9661           (if (and (> (point) start)
9662                    (search-backward "\n" start t))
9663               (progn
9664                 (subst-char-in-region start (point) ?\n ?\^M)
9665                 (gnus-summary-goto-subject article))
9666             (goto-char start)
9667             nil)))))
9668
9669 (defun gnus-summary-go-to-next-thread (&optional previous)
9670   "Go to the same level (or less) next thread.
9671 If PREVIOUS is non-nil, go to previous thread instead.
9672 Return the article number moved to, or nil if moving was impossible."
9673   (let ((level (gnus-summary-thread-level))
9674         (way (if previous -1 1))
9675         (beg (point)))
9676     (forward-line way)
9677     (while (and (not (eobp))
9678                 (< level (gnus-summary-thread-level)))
9679       (forward-line way))
9680     (if (eobp)
9681         (progn
9682           (goto-char beg)
9683           nil)
9684       (setq beg (point))
9685       (prog1
9686           (gnus-summary-article-number)
9687         (goto-char beg)))))
9688
9689 (defun gnus-summary-next-thread (n &optional silent)
9690   "Go to the same level next N'th thread.
9691 If N is negative, search backward instead.
9692 Returns the difference between N and the number of skips actually
9693 done.
9694
9695 If SILENT, don't output messages."
9696   (interactive "p")
9697   (let ((backward (< n 0))
9698         (n (abs n)))
9699     (while (and (> n 0)
9700                 (gnus-summary-go-to-next-thread backward))
9701       (decf n))
9702     (unless silent
9703       (gnus-summary-position-point))
9704     (when (and (not silent) (/= 0 n))
9705       (gnus-message 7 "No more threads"))
9706     n))
9707
9708 (defun gnus-summary-prev-thread (n)
9709   "Go to the same level previous N'th thread.
9710 Returns the difference between N and the number of skips actually
9711 done."
9712   (interactive "p")
9713   (gnus-summary-next-thread (- n)))
9714
9715 (defun gnus-summary-go-down-thread ()
9716   "Go down one level in the current thread."
9717   (let ((children (gnus-summary-article-children)))
9718     (when children
9719       (gnus-summary-goto-subject (car children)))))
9720
9721 (defun gnus-summary-go-up-thread ()
9722   "Go up one level in the current thread."
9723   (let ((parent (gnus-summary-article-parent)))
9724     (when parent
9725       (gnus-summary-goto-subject parent))))
9726
9727 (defun gnus-summary-down-thread (n)
9728   "Go down thread N steps.
9729 If N is negative, go up instead.
9730 Returns the difference between N and how many steps down that were
9731 taken."
9732   (interactive "p")
9733   (let ((up (< n 0))
9734         (n (abs n)))
9735     (while (and (> n 0)
9736                 (if up (gnus-summary-go-up-thread)
9737                   (gnus-summary-go-down-thread)))
9738       (setq n (1- n)))
9739     (gnus-summary-position-point)
9740     (when (/= 0 n)
9741       (gnus-message 7 "Can't go further"))
9742     n))
9743
9744 (defun gnus-summary-up-thread (n)
9745   "Go up thread N steps.
9746 If N is negative, go down instead.
9747 Returns the difference between N and how many steps down that were
9748 taken."
9749   (interactive "p")
9750   (gnus-summary-down-thread (- n)))
9751
9752 (defun gnus-summary-top-thread ()
9753   "Go to the top of the thread."
9754   (interactive)
9755   (while (gnus-summary-go-up-thread))
9756   (gnus-summary-article-number))
9757
9758 (defun gnus-summary-kill-thread (&optional unmark)
9759   "Mark articles under current thread as read.
9760 If the prefix argument is positive, remove any kinds of marks.
9761 If the prefix argument is negative, tick articles instead."
9762   (interactive "P")
9763   (when unmark
9764     (setq unmark (prefix-numeric-value unmark)))
9765   (let ((articles (gnus-summary-articles-in-thread)))
9766     (save-excursion
9767       ;; Expand the thread.
9768       (gnus-summary-show-thread)
9769       ;; Mark all the articles.
9770       (while articles
9771         (gnus-summary-goto-subject (car articles))
9772         (cond ((null unmark)
9773                (gnus-summary-mark-article-as-read gnus-killed-mark))
9774               ((> unmark 0)
9775                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9776               (t
9777                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9778         (setq articles (cdr articles))))
9779     ;; Hide killed subtrees.
9780     (and (null unmark)
9781          gnus-thread-hide-killed
9782          (gnus-summary-hide-thread))
9783     ;; If marked as read, go to next unread subject.
9784     (when (null unmark)
9785       ;; Go to next unread subject.
9786       (gnus-summary-next-subject 1 t)))
9787   (gnus-set-mode-line 'summary))
9788
9789 ;; Summary sorting commands
9790
9791 (defun gnus-summary-sort-by-number (&optional reverse)
9792   "Sort the summary buffer by article number.
9793 Argument REVERSE means reverse order."
9794   (interactive "P")
9795   (gnus-summary-sort 'number reverse))
9796
9797 (defun gnus-summary-sort-by-author (&optional reverse)
9798   "Sort the summary buffer by author name alphabetically.
9799 If `case-fold-search' is non-nil, case of letters is ignored.
9800 Argument REVERSE means reverse order."
9801   (interactive "P")
9802   (gnus-summary-sort 'author reverse))
9803
9804 (defun gnus-summary-sort-by-subject (&optional reverse)
9805   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9806 If `case-fold-search' is non-nil, case of letters is ignored.
9807 Argument REVERSE means reverse order."
9808   (interactive "P")
9809   (gnus-summary-sort 'subject reverse))
9810
9811 (defun gnus-summary-sort-by-date (&optional reverse)
9812   "Sort the summary buffer by date.
9813 Argument REVERSE means reverse order."
9814   (interactive "P")
9815   (gnus-summary-sort 'date reverse))
9816
9817 (defun gnus-summary-sort-by-score (&optional reverse)
9818   "Sort the summary buffer by score.
9819 Argument REVERSE means reverse order."
9820   (interactive "P")
9821   (gnus-summary-sort 'score reverse))
9822
9823 (defun gnus-summary-sort-by-lines (&optional reverse)
9824   "Sort the summary buffer by the number of lines.
9825 Argument REVERSE means reverse order."
9826   (interactive "P")
9827   (gnus-summary-sort 'lines reverse))
9828
9829 (defun gnus-summary-sort-by-chars (&optional reverse)
9830   "Sort the summary buffer by article length.
9831 Argument REVERSE means reverse order."
9832   (interactive "P")
9833   (gnus-summary-sort 'chars reverse))
9834
9835 (defun gnus-summary-sort-by-original (&optional reverse)
9836   "Sort the summary buffer using the default sorting method.
9837 Argument REVERSE means reverse order."
9838   (interactive "P")
9839   (let* ((buffer-read-only)
9840          (gnus-summary-prepare-hook nil))
9841     ;; We do the sorting by regenerating the threads.
9842     (gnus-summary-prepare)
9843     ;; Hide subthreads if needed.
9844     (when (and gnus-show-threads gnus-thread-hide-subtree)
9845       (gnus-summary-hide-all-threads))))
9846
9847 (defun gnus-summary-sort (predicate reverse)
9848   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9849   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9850          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9851          (gnus-thread-sort-functions
9852           (if (not reverse)
9853               thread
9854             `(lambda (t1 t2)
9855                (,thread t2 t1))))
9856          (gnus-sort-gathered-threads-function
9857           gnus-thread-sort-functions)
9858          (gnus-article-sort-functions
9859           (if (not reverse)
9860               article
9861             `(lambda (t1 t2)
9862                (,article t2 t1))))
9863          (buffer-read-only)
9864          (gnus-summary-prepare-hook nil))
9865     ;; We do the sorting by regenerating the threads.
9866     (gnus-summary-prepare)
9867     ;; Hide subthreads if needed.
9868     (when (and gnus-show-threads gnus-thread-hide-subtree)
9869       (gnus-summary-hide-all-threads))))
9870
9871 ;; Summary saving commands.
9872
9873 (defun gnus-summary-save-article (&optional n not-saved)
9874   "Save the current article using the default saver function.
9875 If N is a positive number, save the N next articles.
9876 If N is a negative number, save the N previous articles.
9877 If N is nil and any articles have been marked with the process mark,
9878 save those articles instead.
9879 The variable `gnus-default-article-saver' specifies the saver function."
9880   (interactive "P")
9881   (let* ((articles (gnus-summary-work-articles n))
9882          (save-buffer (save-excursion
9883                         (nnheader-set-temp-buffer " *Gnus Save*")))
9884          (num (length articles))
9885          header file)
9886     (dolist (article articles)
9887       (setq header (gnus-summary-article-header article))
9888       (if (not (vectorp header))
9889           ;; This is a pseudo-article.
9890           (if (assq 'name header)
9891               (gnus-copy-file (cdr (assq 'name header)))
9892             (gnus-message 1 "Article %d is unsaveable" article))
9893         ;; This is a real article.
9894         (save-window-excursion
9895           (gnus-summary-select-article t nil nil article))
9896         (save-excursion
9897           (set-buffer save-buffer)
9898           (erase-buffer)
9899           (insert-buffer-substring gnus-original-article-buffer))
9900         (setq file (gnus-article-save save-buffer file num))
9901         (gnus-summary-remove-process-mark article)
9902         (unless not-saved
9903           (gnus-summary-set-saved-mark article))))
9904     (gnus-kill-buffer save-buffer)
9905     (gnus-summary-position-point)
9906     (gnus-set-mode-line 'summary)
9907     n))
9908
9909 (defun gnus-summary-pipe-output (&optional arg)
9910   "Pipe the current article to a subprocess.
9911 If N is a positive number, pipe the N next articles.
9912 If N is a negative number, pipe the N previous articles.
9913 If N is nil and any articles have been marked with the process mark,
9914 pipe those articles instead."
9915   (interactive "P")
9916   (require 'gnus-art)
9917   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9918     (gnus-summary-save-article arg t))
9919   (let ((buffer (get-buffer "*Shell Command Output*")))
9920     (if (and buffer
9921              (with-current-buffer buffer (> (point-max) (point-min))))
9922         (gnus-configure-windows 'pipe))))
9923
9924 (defun gnus-summary-save-article-mail (&optional arg)
9925   "Append the current article to an mail file.
9926 If N is a positive number, save the N next articles.
9927 If N is a negative number, save the N previous articles.
9928 If N is nil and any articles have been marked with the process mark,
9929 save those articles instead."
9930   (interactive "P")
9931   (require 'gnus-art)
9932   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9933     (gnus-summary-save-article arg)))
9934
9935 (defun gnus-summary-save-article-rmail (&optional arg)
9936   "Append the current article to an rmail file.
9937 If N is a positive number, save the N next articles.
9938 If N is a negative number, save the N previous articles.
9939 If N is nil and any articles have been marked with the process mark,
9940 save those articles instead."
9941   (interactive "P")
9942   (require 'gnus-art)
9943   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9944     (gnus-summary-save-article arg)))
9945
9946 (defun gnus-summary-save-article-file (&optional arg)
9947   "Append the current article to a file.
9948 If N is a positive number, save the N next articles.
9949 If N is a negative number, save the N previous articles.
9950 If N is nil and any articles have been marked with the process mark,
9951 save those articles instead."
9952   (interactive "P")
9953   (require 'gnus-art)
9954   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9955     (gnus-summary-save-article arg)))
9956
9957 (defun gnus-summary-write-article-file (&optional arg)
9958   "Write the current article to a file, deleting the previous file.
9959 If N is a positive number, save the N next articles.
9960 If N is a negative number, save the N previous articles.
9961 If N is nil and any articles have been marked with the process mark,
9962 save those articles instead."
9963   (interactive "P")
9964   (require 'gnus-art)
9965   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9966     (gnus-summary-save-article arg)))
9967
9968 (defun gnus-summary-save-article-body-file (&optional arg)
9969   "Append the current article body to a file.
9970 If N is a positive number, save the N next articles.
9971 If N is a negative number, save the N previous articles.
9972 If N is nil and any articles have been marked with the process mark,
9973 save those articles instead."
9974   (interactive "P")
9975   (require 'gnus-art)
9976   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9977     (gnus-summary-save-article arg)))
9978
9979 (defun gnus-summary-pipe-message (program)
9980   "Pipe the current article through PROGRAM."
9981   (interactive "sProgram: ")
9982   (gnus-summary-select-article)
9983   (let ((mail-header-separator ""))
9984     (gnus-eval-in-buffer-window gnus-article-buffer
9985       (save-restriction
9986         (widen)
9987         (let ((start (window-start))
9988               buffer-read-only)
9989           (message-pipe-buffer-body program)
9990           (set-window-start (get-buffer-window (current-buffer)) start))))))
9991
9992 (defun gnus-get-split-value (methods)
9993   "Return a value based on the split METHODS."
9994   (let (split-name method result match)
9995     (when methods
9996       (save-excursion
9997         (set-buffer gnus-original-article-buffer)
9998         (save-restriction
9999           (nnheader-narrow-to-headers)
10000           (while (and methods (not split-name))
10001             (goto-char (point-min))
10002             (setq method (pop methods))
10003             (setq match (car method))
10004             (when (cond
10005                    ((stringp match)
10006                     ;; Regular expression.
10007                     (ignore-errors
10008                       (re-search-forward match nil t)))
10009                    ((gnus-functionp match)
10010                     ;; Function.
10011                     (save-restriction
10012                       (widen)
10013                       (setq result (funcall match gnus-newsgroup-name))))
10014                    ((consp match)
10015                     ;; Form.
10016                     (save-restriction
10017                       (widen)
10018                       (setq result (eval match)))))
10019               (setq split-name (cdr method))
10020               (cond ((stringp result)
10021                      (push (expand-file-name
10022                             result gnus-article-save-directory)
10023                            split-name))
10024                     ((consp result)
10025                      (setq split-name (append result split-name)))))))))
10026     (nreverse split-name)))
10027
10028 (defun gnus-valid-move-group-p (group)
10029   (and (boundp group)
10030        (symbol-name group)
10031        (symbol-value group)
10032        (gnus-get-function (gnus-find-method-for-group
10033                            (symbol-name group)) 'request-accept-article t)))
10034
10035 (defun gnus-read-move-group-name (prompt default articles prefix)
10036   "Read a group name."
10037   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10038          (minibuffer-confirm-incomplete nil) ; XEmacs
10039          (prom
10040           (format "%s %s to:"
10041                   prompt
10042                   (if (> (length articles) 1)
10043                       (format "these %d articles" (length articles))
10044                     "this article")))
10045          (to-newsgroup
10046           (cond
10047            ((null split-name)
10048             (gnus-completing-read default prom
10049                                   gnus-active-hashtb
10050                                   'gnus-valid-move-group-p
10051                                   nil prefix
10052                                   'gnus-group-history))
10053            ((= 1 (length split-name))
10054             (gnus-completing-read (car split-name) prom
10055                                   gnus-active-hashtb
10056                                   'gnus-valid-move-group-p
10057                                   nil nil
10058                                   'gnus-group-history))
10059            (t
10060             (gnus-completing-read nil prom
10061                                   (mapcar (lambda (el) (list el))
10062                                           (nreverse split-name))
10063                                   nil nil nil
10064                                   'gnus-group-history))))
10065          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10066     (when to-newsgroup
10067       (if (or (string= to-newsgroup "")
10068               (string= to-newsgroup prefix))
10069           (setq to-newsgroup default))
10070       (unless to-newsgroup
10071         (error "No group name entered"))
10072       (or (gnus-active to-newsgroup)
10073           (gnus-activate-group to-newsgroup nil nil to-method)
10074           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10075                                      to-newsgroup))
10076               (or (and (gnus-request-create-group to-newsgroup to-method)
10077                        (gnus-activate-group
10078                         to-newsgroup nil nil to-method)
10079                        (gnus-subscribe-group to-newsgroup))
10080                   (error "Couldn't create group %s" to-newsgroup)))
10081           (error "No such group: %s" to-newsgroup)))
10082     to-newsgroup))
10083
10084 (defun gnus-summary-save-parts (type dir n &optional reverse)
10085   "Save parts matching TYPE to DIR.
10086 If REVERSE, save parts that do not match TYPE."
10087   (interactive
10088    (list (read-string "Save parts of type: "
10089                       (or (car gnus-summary-save-parts-type-history)
10090                           gnus-summary-save-parts-default-mime)
10091                       'gnus-summary-save-parts-type-history)
10092          (setq gnus-summary-save-parts-last-directory
10093                (read-file-name "Save to directory: "
10094                                gnus-summary-save-parts-last-directory
10095                                nil t))
10096          current-prefix-arg))
10097   (gnus-summary-iterate n
10098     (let ((gnus-display-mime-function nil)
10099           (gnus-inhibit-treatment t))
10100       (gnus-summary-select-article))
10101     (save-excursion
10102       (set-buffer gnus-article-buffer)
10103       (let ((handles (or gnus-article-mime-handles
10104                          (mm-dissect-buffer) (mm-uu-dissect))))
10105         (when handles
10106           (gnus-summary-save-parts-1 type dir handles reverse)
10107           (unless gnus-article-mime-handles ;; Don't destroy this case.
10108             (mm-destroy-parts handles)))))))
10109
10110 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10111   (if (stringp (car handle))
10112       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10113               (cdr handle))
10114     (when (if reverse
10115               (not (string-match type (mm-handle-media-type handle)))
10116             (string-match type (mm-handle-media-type handle)))
10117       (let ((file (expand-file-name
10118                    (file-name-nondirectory
10119                     (or
10120                      (mail-content-type-get
10121                       (mm-handle-disposition handle) 'filename)
10122                      (concat gnus-newsgroup-name
10123                              "." (number-to-string
10124                                   (cdr gnus-article-current)))))
10125                    dir)))
10126         (unless (file-exists-p file)
10127           (mm-save-part-to-file handle file))))))
10128
10129 ;; Summary extract commands
10130
10131 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10132   (let ((buffer-read-only nil)
10133         (article (gnus-summary-article-number))
10134         after-article b e)
10135     (unless (gnus-summary-goto-subject article)
10136       (error "No such article: %d" article))
10137     (gnus-summary-position-point)
10138     ;; If all commands are to be bunched up on one line, we collect
10139     ;; them here.
10140     (unless gnus-view-pseudos-separately
10141       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10142             files action)
10143         (while ps
10144           (setq action (cdr (assq 'action (car ps))))
10145           (setq files (list (cdr (assq 'name (car ps)))))
10146           (while (and ps (cdr ps)
10147                       (string= (or action "1")
10148                                (or (cdr (assq 'action (cadr ps))) "2")))
10149             (push (cdr (assq 'name (cadr ps))) files)
10150             (setcdr ps (cddr ps)))
10151           (when files
10152             (when (not (string-match "%s" action))
10153               (push " " files))
10154             (push " " files)
10155             (when (assq 'execute (car ps))
10156               (setcdr (assq 'execute (car ps))
10157                       (funcall (if (string-match "%s" action)
10158                                    'format 'concat)
10159                                action
10160                                (mapconcat
10161                                 (lambda (f)
10162                                   (if (equal f " ")
10163                                       f
10164                                     (gnus-quote-arg-for-sh-or-csh f)))
10165                                 files " ")))))
10166           (setq ps (cdr ps)))))
10167     (if (and gnus-view-pseudos (not not-view))
10168         (while pslist
10169           (when (assq 'execute (car pslist))
10170             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10171                                   (eq gnus-view-pseudos 'not-confirm)))
10172           (setq pslist (cdr pslist)))
10173       (save-excursion
10174         (while pslist
10175           (setq after-article (or (cdr (assq 'article (car pslist)))
10176                                   (gnus-summary-article-number)))
10177           (gnus-summary-goto-subject after-article)
10178           (forward-line 1)
10179           (setq b (point))
10180           (insert "    " (file-name-nondirectory
10181                           (cdr (assq 'name (car pslist))))
10182                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10183           (setq e (point))
10184           (forward-line -1)             ; back to `b'
10185           (gnus-add-text-properties
10186            b (1- e) (list 'gnus-number gnus-reffed-article-number
10187                           gnus-mouse-face-prop gnus-mouse-face))
10188           (gnus-data-enter
10189            after-article gnus-reffed-article-number
10190            gnus-unread-mark b (car pslist) 0 (- e b))
10191           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10192           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10193           (setq pslist (cdr pslist)))))))
10194
10195 (defun gnus-pseudos< (p1 p2)
10196   (let ((c1 (cdr (assq 'action p1)))
10197         (c2 (cdr (assq 'action p2))))
10198     (and c1 c2 (string< c1 c2))))
10199
10200 (defun gnus-request-pseudo-article (props)
10201   (cond ((assq 'execute props)
10202          (gnus-execute-command (cdr (assq 'execute props)))))
10203   (let ((gnus-current-article (gnus-summary-article-number)))
10204     (gnus-run-hooks 'gnus-mark-article-hook)))
10205
10206 (defun gnus-execute-command (command &optional automatic)
10207   (save-excursion
10208     (gnus-article-setup-buffer)
10209     (set-buffer gnus-article-buffer)
10210     (setq buffer-read-only nil)
10211     (let ((command (if automatic command
10212                      (read-string "Command: " (cons command 0)))))
10213       (erase-buffer)
10214       (insert "$ " command "\n\n")
10215       (if gnus-view-pseudo-asynchronously
10216           (start-process "gnus-execute" (current-buffer) shell-file-name
10217                          shell-command-switch command)
10218         (call-process shell-file-name nil t nil
10219                       shell-command-switch command)))))
10220
10221 ;; Summary kill commands.
10222
10223 (defun gnus-summary-edit-global-kill (article)
10224   "Edit the \"global\" kill file."
10225   (interactive (list (gnus-summary-article-number)))
10226   (gnus-group-edit-global-kill article))
10227
10228 (defun gnus-summary-edit-local-kill ()
10229   "Edit a local kill file applied to the current newsgroup."
10230   (interactive)
10231   (setq gnus-current-headers (gnus-summary-article-header))
10232   (gnus-group-edit-local-kill
10233    (gnus-summary-article-number) gnus-newsgroup-name))
10234
10235 ;;; Header reading.
10236
10237 (defun gnus-read-header (id &optional header)
10238   "Read the headers of article ID and enter them into the Gnus system."
10239   (let ((group gnus-newsgroup-name)
10240         (gnus-override-method
10241          (or
10242           gnus-override-method
10243           (and (gnus-news-group-p gnus-newsgroup-name)
10244                (car (gnus-refer-article-methods)))))
10245         where)
10246     ;; First we check to see whether the header in question is already
10247     ;; fetched.
10248     (if (stringp id)
10249         ;; This is a Message-ID.
10250         (setq header (or header (gnus-id-to-header id)))
10251       ;; This is an article number.
10252       (setq header (or header (gnus-summary-article-header id))))
10253     (if (and header
10254              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10255         ;; We have found the header.
10256         header
10257       ;; If this is a sparse article, we have to nix out its
10258       ;; previous entry in the thread hashtb.
10259       (when (and header
10260                  (gnus-summary-article-sparse-p (mail-header-number header)))
10261         (let* ((parent (gnus-parent-id (mail-header-references header)))
10262                (thread (and parent (gnus-id-to-thread parent))))
10263           (when thread
10264             (delq (assq header thread) thread))))
10265       ;; We have to really fetch the header to this article.
10266       (save-excursion
10267         (set-buffer nntp-server-buffer)
10268         (when (setq where (gnus-request-head id group))
10269           (nnheader-fold-continuation-lines)
10270           (goto-char (point-max))
10271           (insert ".\n")
10272           (goto-char (point-min))
10273           (insert "211 ")
10274           (princ (cond
10275                   ((numberp id) id)
10276                   ((cdr where) (cdr where))
10277                   (header (mail-header-number header))
10278                   (t gnus-reffed-article-number))
10279                  (current-buffer))
10280           (insert " Article retrieved.\n"))
10281         (if (or (not where)
10282                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10283             ()                          ; Malformed head.
10284           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10285             (when (and (stringp id)
10286                        (not (string= (gnus-group-real-name group)
10287                                      (car where))))
10288               ;; If we fetched by Message-ID and the article came
10289               ;; from a different group, we fudge some bogus article
10290               ;; numbers for this article.
10291               (mail-header-set-number header gnus-reffed-article-number))
10292             (save-excursion
10293               (set-buffer gnus-summary-buffer)
10294               (decf gnus-reffed-article-number)
10295               (gnus-remove-header (mail-header-number header))
10296               (push header gnus-newsgroup-headers)
10297               (setq gnus-current-headers header)
10298               (push (mail-header-number header) gnus-newsgroup-limit)))
10299           header)))))
10300
10301 (defun gnus-remove-header (number)
10302   "Remove header NUMBER from `gnus-newsgroup-headers'."
10303   (if (and gnus-newsgroup-headers
10304            (= number (mail-header-number (car gnus-newsgroup-headers))))
10305       (pop gnus-newsgroup-headers)
10306     (let ((headers gnus-newsgroup-headers))
10307       (while (and (cdr headers)
10308                   (not (= number (mail-header-number (cadr headers)))))
10309         (pop headers))
10310       (when (cdr headers)
10311         (setcdr headers (cddr headers))))))
10312
10313 ;;;
10314 ;;; summary highlights
10315 ;;;
10316
10317 (defun gnus-highlight-selected-summary ()
10318   "Highlight selected article in summary buffer."
10319   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10320   (when gnus-summary-selected-face
10321     (save-excursion
10322       (let* ((beg (progn (beginning-of-line) (point)))
10323              (end (progn (end-of-line) (point)))
10324              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10325              (from (if (get-text-property beg gnus-mouse-face-prop)
10326                        beg
10327                      (or (next-single-property-change
10328                           beg gnus-mouse-face-prop nil end)
10329                          beg)))
10330              (to
10331               (if (= from end)
10332                   (- from 2)
10333                 (or (next-single-property-change
10334                      from gnus-mouse-face-prop nil end)
10335                     end))))
10336         ;; If no mouse-face prop on line we will have to = from = end,
10337         ;; so we highlight the entire line instead.
10338         (when (= (+ to 2) from)
10339           (setq from beg)
10340           (setq to end))
10341         (if gnus-newsgroup-selected-overlay
10342             ;; Move old overlay.
10343             (gnus-move-overlay
10344              gnus-newsgroup-selected-overlay from to (current-buffer))
10345           ;; Create new overlay.
10346           (gnus-overlay-put
10347            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10348            'face gnus-summary-selected-face))))))
10349
10350 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10351 (defun gnus-summary-highlight-line ()
10352   "Highlight current line according to `gnus-summary-highlight'."
10353   (let* ((list gnus-summary-highlight)
10354          (p (point))
10355          (end (progn (end-of-line) (point)))
10356          ;; now find out where the line starts and leave point there.
10357          (beg (progn (beginning-of-line) (point)))
10358          (article (gnus-summary-article-number))
10359          (score (or (cdr (assq (or article gnus-current-article)
10360                                gnus-newsgroup-scored))
10361                     gnus-summary-default-score 0))
10362          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10363          (inhibit-read-only t))
10364     ;; Eval the cars of the lists until we find a match.
10365     (let ((default gnus-summary-default-score)
10366           (default-high gnus-summary-default-high-score)
10367           (default-low gnus-summary-default-low-score))
10368       (while (and list
10369                   (not (eval (caar list))))
10370         (setq list (cdr list))))
10371     (let ((face (cdar list)))
10372       (unless (eq face (get-text-property beg 'face))
10373         (gnus-put-text-property-excluding-characters-with-faces
10374          beg end 'face
10375          (setq face (if (boundp face) (symbol-value face) face)))
10376         (when gnus-summary-highlight-line-function
10377           (funcall gnus-summary-highlight-line-function article face))))
10378     (goto-char p)))
10379
10380 (defun gnus-update-read-articles (group unread &optional compute)
10381   "Update the list of read articles in GROUP."
10382   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10383          (entry (gnus-gethash group gnus-newsrc-hashtb))
10384          (info (nth 2 entry))
10385          (prev 1)
10386          (unread (sort (copy-sequence unread) '<))
10387          read)
10388     (if (or (not info) (not active))
10389         ;; There is no info on this group if it was, in fact,
10390         ;; killed.  Gnus stores no information on killed groups, so
10391         ;; there's nothing to be done.
10392         ;; One could store the information somewhere temporarily,
10393         ;; perhaps...  Hmmm...
10394         ()
10395       ;; Remove any negative articles numbers.
10396       (while (and unread (< (car unread) 0))
10397         (setq unread (cdr unread)))
10398       ;; Remove any expired article numbers
10399       (while (and unread (< (car unread) (car active)))
10400         (setq unread (cdr unread)))
10401       ;; Compute the ranges of read articles by looking at the list of
10402       ;; unread articles.
10403       (while unread
10404         (when (/= (car unread) prev)
10405           (push (if (= prev (1- (car unread))) prev
10406                   (cons prev (1- (car unread))))
10407                 read))
10408         (setq prev (1+ (car unread)))
10409         (setq unread (cdr unread)))
10410       (when (<= prev (cdr active))
10411         (push (cons prev (cdr active)) read))
10412       (setq read (if (> (length read) 1) (nreverse read) read))
10413       (if compute
10414           read
10415         (save-excursion
10416           (let (setmarkundo)
10417             ;; Propagate the read marks to the backend.
10418             (when (gnus-check-backend-function 'request-set-mark group)
10419               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10420                     (add (gnus-remove-from-range read (gnus-info-read info))))
10421                 (when (or add del)
10422                   (unless (gnus-check-group group)
10423                     (error "Can't open server for %s" group))
10424                   (gnus-request-set-mark
10425                    group (delq nil (list (if add (list add 'add '(read)))
10426                                          (if del (list del 'del '(read))))))
10427                   (setq setmarkundo
10428                         `(gnus-request-set-mark
10429                           ,group
10430                           ',(delq nil (list
10431                                        (if del (list del 'add '(read)))
10432                                        (if add (list add 'del '(read))))))))))
10433             (set-buffer gnus-group-buffer)
10434             (gnus-undo-register
10435               `(progn
10436                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10437                  (gnus-info-set-read ',info ',(gnus-info-read info))
10438                  (gnus-get-unread-articles-in-group ',info
10439                                                     (gnus-active ,group))
10440                  (gnus-group-update-group ,group t)
10441                  ,setmarkundo))))
10442         ;; Enter this list into the group info.
10443         (gnus-info-set-read info read)
10444         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10445         (gnus-get-unread-articles-in-group info (gnus-active group))
10446         t))))
10447
10448 (defun gnus-offer-save-summaries ()
10449   "Offer to save all active summary buffers."
10450   (let (buffers)
10451     ;; Go through all buffers and find all summaries.
10452     (dolist (buffer (buffer-list))
10453       (when (and (setq buffer (buffer-name buffer))
10454                  (string-match "Summary" buffer)
10455                  (save-excursion
10456                    (set-buffer buffer)
10457                    ;; We check that this is, indeed, a summary buffer.
10458                    (and (eq major-mode 'gnus-summary-mode)
10459                         ;; Also make sure this isn't bogus.
10460                         gnus-newsgroup-prepared
10461                         ;; Also make sure that this isn't a
10462                         ;; dead summary buffer.
10463                         (not gnus-dead-summary-mode))))
10464         (push buffer buffers)))
10465     ;; Go through all these summary buffers and offer to save them.
10466     (when buffers
10467       (save-excursion
10468         (map-y-or-n-p
10469          "Update summary buffer %s? "
10470          (lambda (buf)
10471            (switch-to-buffer buf)
10472            (gnus-summary-exit))
10473          buffers)))))
10474
10475
10476 ;;; @ for mime-partial
10477 ;;;
10478
10479 (defun gnus-request-partial-message ()
10480   (save-excursion
10481     (let ((number (gnus-summary-article-number))
10482           (group gnus-newsgroup-name)
10483           (mother gnus-article-buffer))
10484       (set-buffer (get-buffer-create " *Partial Article*"))
10485       (erase-buffer)
10486       (setq mime-preview-buffer mother)
10487       (gnus-request-article-this-buffer number group)
10488       (mime-parse-buffer)
10489       )))
10490
10491 (autoload 'mime-combine-message/partial-pieces-automatically
10492   "mime-partial"
10493   "Internal method to combine message/partial messages automatically.")
10494
10495 (mime-add-condition
10496  'action '((type . message)(subtype . partial)
10497            (major-mode . gnus-original-article-mode)
10498            (method . mime-combine-message/partial-pieces-automatically)
10499            (summary-buffer-exp . gnus-summary-buffer)
10500            (request-partial-message-method . gnus-request-partial-message)
10501            ))
10502
10503
10504 ;;; @ for message/rfc822
10505 ;;;
10506
10507 (defun gnus-mime-extract-message/rfc822 (entity situation)
10508   (let (group article num cwin swin cur)
10509     (with-temp-buffer
10510       (mime-insert-entity-content entity)
10511       (setq group (or (cdr (assq 'group situation))
10512                       (completing-read "Group: "
10513                                        gnus-active-hashtb
10514                                        nil
10515                                        (gnus-read-active-file-p)
10516                                        gnus-newsgroup-name))
10517             article (gnus-request-accept-article group)))
10518     (when (and (consp article)
10519                (numberp (setq article (cdr article))))
10520       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10521             cwin (get-buffer-window (current-buffer) t))
10522       (save-window-excursion
10523         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10524             (select-window swin)
10525           (set-buffer gnus-summary-buffer))
10526         (setq cur gnus-current-article)
10527         (forward-line num)
10528         (let (gnus-show-threads)
10529           (gnus-summary-goto-subject article t))
10530         (gnus-summary-clear-mark-forward 1)
10531         (gnus-summary-goto-subject cur))
10532       (when (and cwin (window-frame cwin))
10533         (select-frame (window-frame cwin)))
10534       (when (boundp 'mime-acting-situation-to-override)
10535         (set-alist 'mime-acting-situation-to-override
10536                    'group
10537                    group)
10538         (set-alist 'mime-acting-situation-to-override
10539                    'after-method
10540                    `(progn
10541                       (save-current-buffer
10542                         (set-buffer gnus-group-buffer)
10543                         (gnus-activate-group ,group))
10544                       (gnus-summary-goto-article ,cur
10545                                                  gnus-show-all-headers)))
10546         (set-alist 'mime-acting-situation-to-override
10547                    'number num)))))
10548
10549 (mime-add-condition
10550  'action '((type . message)(subtype . rfc822)
10551            (major-mode . gnus-original-article-mode)
10552            (method . gnus-mime-extract-message/rfc822)
10553            (mode . "extract")
10554            ))
10555
10556 (mime-add-condition
10557  'action '((type . message)(subtype . news)
10558            (major-mode . gnus-original-article-mode)
10559            (method . gnus-mime-extract-message/rfc822)
10560            (mode . "extract")
10561            ))
10562
10563 (defun gnus-mime-extract-multipart (entity situation)
10564   (let ((children (mime-entity-children entity))
10565         mime-acting-situation-to-override
10566         f)
10567     (while children
10568       (mime-play-entity (car children)
10569                         (cons (assq 'mode situation)
10570                               mime-acting-situation-to-override))
10571       (setq children (cdr children)))
10572     (if (setq f (cdr (assq 'after-method
10573                            mime-acting-situation-to-override)))
10574         (eval f)
10575       )))
10576
10577 (mime-add-condition
10578  'action '((type . multipart)
10579            (method . gnus-mime-extract-multipart)
10580            (mode . "extract")
10581            )
10582  'with-default)
10583
10584
10585 ;;; @ end
10586 ;;;
10587
10588 (defun gnus-summary-setup-default-charset ()
10589   "Setup newsgroup default charset."
10590   (if (equal gnus-newsgroup-name "nndraft:drafts")
10591       (setq gnus-newsgroup-charset nil)
10592     (let* ((ignored-charsets
10593             (or gnus-newsgroup-ephemeral-ignored-charsets
10594                 (append
10595                  (and gnus-newsgroup-name
10596                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10597                  gnus-newsgroup-ignored-charsets))))
10598       (setq gnus-newsgroup-charset
10599             (or gnus-newsgroup-ephemeral-charset
10600                 (and gnus-newsgroup-name
10601                      (gnus-parameter-charset gnus-newsgroup-name))
10602                 gnus-default-charset))
10603       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10604            ignored-charsets))))
10605
10606 ;;;
10607 ;;; Mime Commands
10608 ;;;
10609
10610 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10611   "Display the current article buffer fully MIME-buttonized.
10612 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10613 treated as multipart/mixed."
10614   (interactive "P")
10615   (require 'gnus-art)
10616   (let ((gnus-unbuttonized-mime-types nil)
10617         (gnus-mime-display-multipart-as-mixed show-all-parts))
10618     (gnus-summary-show-article)))
10619
10620 (defun gnus-summary-repair-multipart (article)
10621   "Add a Content-Type header to a multipart article without one."
10622   (interactive (list (gnus-summary-article-number)))
10623   (gnus-with-article article
10624     (message-narrow-to-head)
10625     (message-remove-header "Mime-Version")
10626     (goto-char (point-max))
10627     (insert "Mime-Version: 1.0\n")
10628     (widen)
10629     (when (search-forward "\n--" nil t)
10630       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10631         (message-narrow-to-head)
10632         (message-remove-header "Content-Type")
10633         (goto-char (point-max))
10634         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10635                         separator))
10636         (widen))))
10637   (let (gnus-mark-article-hook)
10638     (gnus-summary-select-article t t nil article)))
10639
10640 (defun gnus-summary-toggle-display-buttonized ()
10641   "Toggle the buttonizing of the article buffer."
10642   (interactive)
10643   (require 'gnus-art)
10644   (if (setq gnus-inhibit-mime-unbuttonizing
10645             (not gnus-inhibit-mime-unbuttonizing))
10646       (let ((gnus-unbuttonized-mime-types nil))
10647         (gnus-summary-show-article))
10648     (gnus-summary-show-article)))
10649
10650 ;;;
10651 ;;; Intelli-mouse commmands
10652 ;;;
10653
10654 (defun gnus-wheel-summary-scroll (event)
10655   (interactive "e")
10656   (let ((amount (if (memq 'shift (event-modifiers event))
10657                     (car gnus-wheel-scroll-amount)
10658                   (cdr gnus-wheel-scroll-amount)))
10659         (direction (- (* (static-if (featurep 'xemacs)
10660                              (event-button event)
10661                            (cond ((eq 'mouse-4 (event-basic-type event))
10662                                   4)
10663                                  ((eq 'mouse-5 (event-basic-type event))
10664                                   5)))
10665                          2) 9))
10666         edge)
10667     (gnus-summary-scroll-up (* amount direction))
10668     (when (gnus-eval-in-buffer-window gnus-article-buffer
10669             (save-restriction
10670               (widen)
10671               (and (if (< 0 direction)
10672                        (gnus-article-next-page 0)
10673                      (gnus-article-prev-page 0)
10674                      (bobp))
10675                    (if (setq edge (get-text-property
10676                                    (point-min) 'gnus-wheel-edge))
10677                        (setq edge (* edge direction))
10678                      (setq edge -1))
10679                    (or (plusp edge)
10680                        (let ((buffer-read-only nil)
10681                              (inhibit-read-only t))
10682                          (put-text-property (point-min) (point-max)
10683                                             'gnus-wheel-edge direction)
10684                          nil))
10685                    (or (> edge gnus-wheel-edge-resistance)
10686                        (let ((buffer-read-only nil)
10687                              (inhibit-read-only t))
10688                          (put-text-property (point-min) (point-max)
10689                                             'gnus-wheel-edge
10690                                             (* (1+ edge) direction))
10691                          nil))
10692                    (eq last-command 'gnus-wheel-summary-scroll))))
10693       (gnus-summary-next-article nil nil (minusp direction)))))
10694
10695 (defun gnus-wheel-install ()
10696   "Enable mouse wheel support on summary window."
10697   (when gnus-use-wheel
10698     (let ((keys
10699            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10700       (dolist (key keys)
10701         (define-key gnus-summary-mode-map key
10702           'gnus-wheel-summary-scroll)))))
10703
10704 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10705
10706 ;;;
10707 ;;; Traditional PGP commmands
10708 ;;;
10709
10710 (defun gnus-summary-decrypt-article (&optional force)
10711   "Decrypt the current article in traditional PGP way.
10712 This will have permanent effect only in mail groups.
10713 If FORCE is non-nil, allow editing of articles even in read-only
10714 groups."
10715   (interactive "P")
10716   (gnus-summary-select-article t)
10717   (gnus-eval-in-buffer-window gnus-article-buffer
10718     (save-excursion
10719       (save-restriction
10720         (widen)
10721         (goto-char (point-min))
10722         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10723           (error "Not a traditional PGP message!"))
10724         (let ((armor-start (match-beginning 0)))
10725           (if (and (pgg-decrypt-region armor-start (point-max))
10726                    (or force (not (gnus-group-read-only-p))))
10727               (let ((inhibit-read-only t)
10728                     buffer-read-only)
10729                 (delete-region armor-start
10730                                (progn
10731                                  (re-search-forward "^-+END PGP" nil t)
10732                                  (beginning-of-line 2)
10733                                  (point)))
10734                 (insert-buffer-substring pgg-output-buffer))))))))
10735
10736 (defun gnus-summary-verify-article ()
10737   "Verify the current article in traditional PGP way."
10738   (interactive)
10739   (save-excursion
10740     (set-buffer gnus-original-article-buffer)
10741     (goto-char (point-min))
10742     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10743       (error "Not a traditional PGP message!"))
10744     (re-search-forward "^-+END PGP" nil t)
10745     (beginning-of-line 2)
10746     (call-interactively (function pgg-verify-region))))
10747
10748 ;;;
10749 ;;; Generic summary marking commands
10750 ;;;
10751
10752 (defvar gnus-summary-marking-alist
10753   '((read gnus-del-mark "d")
10754     (unread gnus-unread-mark "u")
10755     (ticked gnus-ticked-mark "!")
10756     (dormant gnus-dormant-mark "?")
10757     (expirable gnus-expirable-mark "e"))
10758   "An alist of names/marks/keystrokes.")
10759
10760 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10761 (defvar gnus-summary-mark-map)
10762
10763 (defun gnus-summary-make-all-marking-commands ()
10764   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10765   (dolist (elem gnus-summary-marking-alist)
10766     (apply 'gnus-summary-make-marking-command elem)))
10767
10768 (defun gnus-summary-make-marking-command (name mark keystroke)
10769   (let ((map (make-sparse-keymap)))
10770     (define-key gnus-summary-generic-mark-map keystroke map)
10771     (dolist (lway `((next "next" next nil "n")
10772                     (next-unread "next unread" next t "N")
10773                     (prev "previous" prev nil "p")
10774                     (prev-unread "previous unread" prev t "P")
10775                     (nomove "" nil nil ,keystroke)))
10776       (let ((func (gnus-summary-make-marking-command-1
10777                    mark (car lway) lway name)))
10778         (setq func (eval func))
10779         (define-key map (nth 4 lway) func)))))
10780
10781 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10782   `(defun ,(intern
10783             (format "gnus-summary-put-mark-as-%s%s"
10784                     name (if (eq way 'nomove)
10785                              ""
10786                            (concat "-" (symbol-name way)))))
10787      (n)
10788      ,(format
10789        "Mark the current article as %s%s.
10790 If N, the prefix, then repeat N times.
10791 If N is negative, move in reverse order.
10792 The difference between N and the actual number of articles marked is
10793 returned."
10794        name (car (cdr lway)))
10795      (interactive "p")
10796      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10797
10798 (defun gnus-summary-generic-mark (n mark move unread)
10799   "Mark N articles with MARK."
10800   (unless (eq major-mode 'gnus-summary-mode)
10801     (error "This command can only be used in the summary buffer"))
10802   (gnus-summary-show-thread)
10803   (let ((nummove
10804          (cond
10805           ((eq move 'next) 1)
10806           ((eq move 'prev) -1)
10807           (t 0))))
10808     (if (zerop nummove)
10809         (setq n 1)
10810       (when (< n 0)
10811         (setq n (abs n)
10812               nummove (* -1 nummove))))
10813     (while (and (> n 0)
10814                 (gnus-summary-mark-article nil mark)
10815                 (zerop (gnus-summary-next-subject nummove unread t)))
10816       (setq n (1- n)))
10817     (when (/= 0 n)
10818       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10819     (gnus-summary-recenter)
10820     (gnus-summary-position-point)
10821     (gnus-set-mode-line 'summary)
10822     n))
10823
10824 (defun gnus-summary-insert-articles (articles)
10825   (when (setq articles
10826               (gnus-set-difference articles
10827                                    (mapcar (lambda (h) (mail-header-number h))
10828                                            gnus-newsgroup-headers)))
10829     (setq gnus-newsgroup-headers
10830           (merge 'list
10831                  gnus-newsgroup-headers
10832                  (gnus-fetch-headers articles)
10833                  'gnus-article-sort-by-number))
10834     ;; Suppress duplicates?
10835     (when gnus-suppress-duplicates
10836       (gnus-dup-suppress-articles))
10837
10838     ;; We might want to build some more threads first.
10839     (when (and gnus-fetch-old-headers
10840                (eq gnus-headers-retrieved-by 'nov))
10841       (if (eq gnus-fetch-old-headers 'invisible)
10842           (gnus-build-all-threads)
10843         (gnus-build-old-threads)))
10844     ;; Let the Gnus agent mark articles as read.
10845     (when gnus-agent
10846       (gnus-agent-get-undownloaded-list))
10847     ;; Remove list identifiers from subject
10848     (when gnus-list-identifiers
10849       (gnus-summary-remove-list-identifiers))
10850     ;; First and last article in this newsgroup.
10851     (when gnus-newsgroup-headers
10852       (setq gnus-newsgroup-begin
10853             (mail-header-number (car gnus-newsgroup-headers))
10854             gnus-newsgroup-end
10855             (mail-header-number
10856              (gnus-last-element gnus-newsgroup-headers))))
10857     (when gnus-use-scoring
10858       (gnus-possibly-score-headers))))
10859
10860 (defun gnus-summary-insert-old-articles (&optional all)
10861   "Insert all old articles in this group.
10862 If ALL is non-nil, already read articles become readable.
10863 If ALL is a number, fetch this number of articles."
10864   (interactive "P")
10865   (prog1
10866       (let ((old (mapcar 'car gnus-newsgroup-data))
10867             (i (car gnus-newsgroup-active))
10868             older len)
10869         (while (<= i (cdr gnus-newsgroup-active))
10870           (or (memq i old) (push i older))
10871           (incf i))
10872         (setq len (length older))
10873         (cond
10874          ((null older) nil)
10875          ((numberp all)
10876           (if (< all len)
10877               (setq older (subseq older 0 all))))
10878          (all nil)
10879          (t
10880           (if (and (numberp gnus-large-newsgroup)
10881                    (> len gnus-large-newsgroup))
10882               (let ((input
10883                      (read-string
10884                       (format
10885                        "How many articles from %s (default %d): "
10886                        (gnus-limit-string
10887                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10888                        len))))
10889                 (unless (string-match "^[ \t]*$" input)
10890                   (setq all (string-to-number input))
10891                   (if (< all len)
10892                       (setq older (subseq older 0 all))))))))
10893         (if (not older)
10894             (message "No old news.")
10895           (gnus-summary-insert-articles older)
10896           (gnus-summary-limit (gnus-union older old))))
10897     (gnus-summary-position-point)))
10898
10899 (defun gnus-summary-insert-new-articles ()
10900   "Insert all new articles in this group."
10901   (interactive)
10902   (prog1
10903       (let ((old (mapcar 'car gnus-newsgroup-data))
10904             (old-active gnus-newsgroup-active)
10905             (nnmail-fetched-sources (list t))
10906             i new)
10907         (setq gnus-newsgroup-active
10908               (gnus-activate-group gnus-newsgroup-name 'scan))
10909         (setq i (1+ (cdr old-active)))
10910         (while (<= i (cdr gnus-newsgroup-active))
10911           (push i new)
10912           (incf i))
10913         (if (not new)
10914             (message "No gnus is bad news.")
10915           (setq new (nreverse new))
10916           (gnus-summary-insert-articles new)
10917           (setq gnus-newsgroup-unreads
10918                 (append gnus-newsgroup-unreads new))
10919           (gnus-summary-limit (gnus-union old new))))
10920     (gnus-summary-position-point)))
10921
10922 (gnus-summary-make-all-marking-commands)
10923
10924 (gnus-ems-redefine)
10925
10926 (provide 'gnus-sum)
10927
10928 (run-hooks 'gnus-sum-load-hook)
10929
10930 ;;; gnus-sum.el ends here