Add nnir-1.68.
[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-saved)
2959                  gnus-saved-mark)
2960                 (t gnus-no-mark)))
2961          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2962          (gnus-tmp-name
2963           (cond
2964            ((string-match "<[^>]+> *$" gnus-tmp-from)
2965             (let ((beg (match-beginning 0)))
2966               (or (and (string-match "^\".+\"" gnus-tmp-from)
2967                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2968                   (substring gnus-tmp-from 0 beg))))
2969            ((string-match "(.+)" gnus-tmp-from)
2970             (substring gnus-tmp-from
2971                        (1+ (match-beginning 0)) (1- (match-end 0))))
2972            (t gnus-tmp-from)))
2973          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2974          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2975          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2976          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2977          (buffer-read-only nil))
2978     (when (string= gnus-tmp-name "")
2979       (setq gnus-tmp-name gnus-tmp-from))
2980     (unless (numberp gnus-tmp-lines)
2981       (setq gnus-tmp-lines -1))
2982     (if (= gnus-tmp-lines -1)
2983         (setq gnus-tmp-lines "?")
2984       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2985     (gnus-put-text-property-excluding-characters-with-faces
2986      (point)
2987      (progn (eval gnus-summary-line-format-spec) (point))
2988      'gnus-number gnus-tmp-number)
2989     (when (gnus-visual-p 'summary-highlight 'highlight)
2990       (forward-line -1)
2991       (gnus-run-hooks 'gnus-summary-update-hook)
2992       (forward-line 1))))
2993
2994 (defun gnus-summary-update-line (&optional dont-update)
2995   "Update summary line after change."
2996   (when (and gnus-summary-default-score
2997              (not gnus-summary-inhibit-highlight))
2998     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2999            (article (gnus-summary-article-number))
3000            (score (gnus-summary-article-score article)))
3001       (unless dont-update
3002         (if (and gnus-summary-mark-below
3003                  (< (gnus-summary-article-score)
3004                     gnus-summary-mark-below))
3005             ;; This article has a low score, so we mark it as read.
3006             (when (memq article gnus-newsgroup-unreads)
3007               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3008           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3009             ;; This article was previously marked as read on account
3010             ;; of a low score, but now it has risen, so we mark it as
3011             ;; unread.
3012             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3013         (gnus-summary-update-mark
3014          (if (or (null gnus-summary-default-score)
3015                  (<= (abs (- score gnus-summary-default-score))
3016                      gnus-summary-zcore-fuzz))
3017              ?\ ;;;Whitespace
3018            (if (< score gnus-summary-default-score)
3019                gnus-score-below-mark gnus-score-over-mark))
3020          'score))
3021       ;; Do visual highlighting.
3022       (when (gnus-visual-p 'summary-highlight 'highlight)
3023         (gnus-run-hooks 'gnus-summary-update-hook)))))
3024
3025 (defvar gnus-tmp-new-adopts nil)
3026
3027 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3028   "Return the number of articles in THREAD.
3029 This may be 0 in some cases -- if none of the articles in
3030 the thread are to be displayed."
3031   (let* ((number
3032           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3033           (cond
3034            ((not (listp thread))
3035             1)
3036            ((and (consp thread) (cdr thread))
3037             (apply
3038              '+ 1 (mapcar
3039                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3040            ((null thread)
3041             1)
3042            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3043             1)
3044            (t 0))))
3045     (when (and level (zerop level) gnus-tmp-new-adopts)
3046       (incf number
3047             (apply '+ (mapcar
3048                        'gnus-summary-number-of-articles-in-thread
3049                        gnus-tmp-new-adopts))))
3050     (if char
3051         (if (> number 1) gnus-not-empty-thread-mark
3052           gnus-empty-thread-mark)
3053       number)))
3054
3055 (defun gnus-summary-set-local-parameters (group)
3056   "Go through the local params of GROUP and set all variable specs in that list."
3057   (let ((params (gnus-group-find-parameter group))
3058         (vars '(quit-config))           ; Ignore quit-config.
3059         elem)
3060     (while params
3061       (setq elem (car params)
3062             params (cdr params))
3063       (and (consp elem)                 ; Has to be a cons.
3064            (consp (cdr elem))           ; The cdr has to be a list.
3065            (symbolp (car elem))         ; Has to be a symbol in there.
3066            (not (memq (car elem) vars))
3067            (ignore-errors               ; So we set it.
3068              (push (car elem) vars)
3069              (make-local-variable (car elem))
3070              (set (car elem) (eval (nth 1 elem))))))))
3071
3072 (defun gnus-summary-read-group (group &optional show-all no-article
3073                                       kill-buffer no-display backward
3074                                       select-articles)
3075   "Start reading news in newsgroup GROUP.
3076 If SHOW-ALL is non-nil, already read articles are also listed.
3077 If NO-ARTICLE is non-nil, no article is selected initially.
3078 If NO-DISPLAY, don't generate a summary buffer."
3079   (let (result)
3080     (while (and group
3081                 (null (setq result
3082                             (let ((gnus-auto-select-next nil))
3083                               (or (gnus-summary-read-group-1
3084                                    group show-all no-article
3085                                    kill-buffer no-display
3086                                    select-articles)
3087                                   (setq show-all nil
3088                                         select-articles nil)))))
3089                 (eq gnus-auto-select-next 'quietly))
3090       (set-buffer gnus-group-buffer)
3091       ;; The entry function called above goes to the next
3092       ;; group automatically, so we go two groups back
3093       ;; if we are searching for the previous group.
3094       (when backward
3095         (gnus-group-prev-unread-group 2))
3096       (if (not (equal group (gnus-group-group-name)))
3097           (setq group (gnus-group-group-name))
3098         (setq group nil)))
3099     result))
3100
3101 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3102   "Directly jump to the other GROUP from summary buffer.
3103 If SHOW-ALL is non-nil, already read articles are also listed."
3104   (interactive
3105    (if (eq gnus-summary-buffer (current-buffer))
3106        (list (completing-read
3107               "Group: " gnus-active-hashtb nil t
3108               (when (and gnus-newsgroup-name
3109                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3110                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3111               'gnus-group-history)
3112              current-prefix-arg)
3113      (error "%s must be invoked from a gnus summary buffer." this-command)))
3114   (unless (or (zerop (length group))
3115               (and gnus-newsgroup-name
3116                    (string-equal gnus-newsgroup-name group)))
3117     (gnus-summary-exit)
3118     (gnus-summary-read-group group show-all
3119                              gnus-dont-select-after-jump-to-other-group)))
3120
3121 (defun gnus-summary-read-group-1 (group show-all no-article
3122                                         kill-buffer no-display
3123                                         &optional select-articles)
3124   ;; Killed foreign groups can't be entered.
3125   ;;  (when (and (not (gnus-group-native-p group))
3126   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3127   ;;    (error "Dead non-native groups can't be entered"))
3128   (gnus-message 5 "Retrieving newsgroup: %s..."
3129                 (gnus-group-decoded-name group))
3130   (let* ((new-group (gnus-summary-setup-buffer group))
3131          (quit-config (gnus-group-quit-config group))
3132          (did-select (and new-group (gnus-select-newsgroup
3133                                      group show-all select-articles))))
3134     (cond
3135      ;; This summary buffer exists already, so we just select it.
3136      ((not new-group)
3137       (gnus-set-global-variables)
3138       (when kill-buffer
3139         (gnus-kill-or-deaden-summary kill-buffer))
3140       (gnus-configure-windows 'summary 'force)
3141       (gnus-set-mode-line 'summary)
3142       (gnus-summary-position-point)
3143       (message "")
3144       t)
3145      ;; We couldn't select this group.
3146      ((null did-select)
3147       (when (and (eq major-mode 'gnus-summary-mode)
3148                  (not (equal (current-buffer) kill-buffer)))
3149         (kill-buffer (current-buffer))
3150         (if (not quit-config)
3151             (progn
3152               ;; Update the info -- marks might need to be removed,
3153               ;; for instance.
3154               (gnus-summary-update-info)
3155               (set-buffer gnus-group-buffer)
3156               (gnus-group-jump-to-group group)
3157               (gnus-group-next-unread-group 1))
3158           (gnus-handle-ephemeral-exit quit-config)))
3159       (let ((grpinfo (gnus-get-info group)))
3160         (if (null (gnus-info-read grpinfo))
3161             (gnus-message 3 "Group %s contains no messages"
3162                           (gnus-group-decoded-name group))
3163           (gnus-message 3 "Can't select group")))
3164       nil)
3165      ;; The user did a `C-g' while prompting for number of articles,
3166      ;; so we exit this group.
3167      ((eq did-select 'quit)
3168       (and (eq major-mode 'gnus-summary-mode)
3169            (not (equal (current-buffer) kill-buffer))
3170            (kill-buffer (current-buffer)))
3171       (when kill-buffer
3172         (gnus-kill-or-deaden-summary kill-buffer))
3173       (if (not quit-config)
3174           (progn
3175             (set-buffer gnus-group-buffer)
3176             (gnus-group-jump-to-group group)
3177             (gnus-group-next-unread-group 1)
3178             (gnus-configure-windows 'group 'force))
3179         (gnus-handle-ephemeral-exit quit-config))
3180       ;; Finally signal the quit.
3181       (signal 'quit nil))
3182      ;; The group was successfully selected.
3183      (t
3184       (gnus-set-global-variables)
3185       ;; Save the active value in effect when the group was entered.
3186       (setq gnus-newsgroup-active
3187             (gnus-copy-sequence
3188              (gnus-active gnus-newsgroup-name)))
3189       ;; You can change the summary buffer in some way with this hook.
3190       (gnus-run-hooks 'gnus-select-group-hook)
3191       (gnus-update-format-specifications
3192        nil 'summary 'summary-mode 'summary-dummy)
3193       (gnus-update-summary-mark-positions)
3194       ;; Do score processing.
3195       (when gnus-use-scoring
3196         (gnus-possibly-score-headers))
3197       ;; Check whether to fill in the gaps in the threads.
3198       (when gnus-build-sparse-threads
3199         (gnus-build-sparse-threads))
3200       ;; Find the initial limit.
3201       (if gnus-show-threads
3202           (if show-all
3203               (let ((gnus-newsgroup-dormant nil))
3204                 (gnus-summary-initial-limit show-all))
3205             (gnus-summary-initial-limit show-all))
3206         ;; When untreaded, all articles are always shown.
3207         (setq gnus-newsgroup-limit
3208               (mapcar
3209                (lambda (header) (mail-header-number header))
3210                gnus-newsgroup-headers)))
3211       ;; Generate the summary buffer.
3212       (unless no-display
3213         (gnus-summary-prepare))
3214       (when gnus-use-trees
3215         (gnus-tree-open group)
3216         (setq gnus-summary-highlight-line-function
3217               'gnus-tree-highlight-article))
3218       ;; If the summary buffer is empty, but there are some low-scored
3219       ;; articles or some excluded dormants, we include these in the
3220       ;; buffer.
3221       (when (and (zerop (buffer-size))
3222                  (not no-display))
3223         (cond (gnus-newsgroup-dormant
3224                (gnus-summary-limit-include-dormant))
3225               ((and gnus-newsgroup-scored show-all)
3226                (gnus-summary-limit-include-expunged t))))
3227       ;; Function `gnus-apply-kill-file' must be called in this hook.
3228       (gnus-run-hooks 'gnus-apply-kill-hook)
3229       (if (and (zerop (buffer-size))
3230                (not no-display))
3231           (progn
3232             ;; This newsgroup is empty.
3233             (gnus-summary-catchup-and-exit nil t)
3234             (gnus-message 6 "No unread news")
3235             (when kill-buffer
3236               (gnus-kill-or-deaden-summary kill-buffer))
3237             ;; Return nil from this function.
3238             nil)
3239         ;; Hide conversation thread subtrees.  We cannot do this in
3240         ;; gnus-summary-prepare-hook since kill processing may not
3241         ;; work with hidden articles.
3242         (and gnus-show-threads
3243              gnus-thread-hide-subtree
3244              (gnus-summary-hide-all-threads))
3245         (when kill-buffer
3246           (gnus-kill-or-deaden-summary kill-buffer))
3247         ;; Show first unread article if requested.
3248         (if (and (not no-article)
3249                  (not no-display)
3250                  gnus-newsgroup-unreads
3251                  gnus-auto-select-first)
3252             (progn
3253               (gnus-configure-windows 'summary)
3254               (cond
3255                ((eq gnus-auto-select-first 'best)
3256                 (gnus-summary-best-unread-article))
3257                ((eq gnus-auto-select-first t)
3258                 (gnus-summary-first-unread-article))
3259                ((gnus-functionp gnus-auto-select-first)
3260                 (funcall gnus-auto-select-first))))
3261           ;; Don't select any articles, just move point to the first
3262           ;; article in the group.
3263           (goto-char (point-min))
3264           (gnus-summary-position-point)
3265           (gnus-configure-windows 'summary 'force)
3266           (gnus-set-mode-line 'summary))
3267         (when (get-buffer-window gnus-group-buffer t)
3268           ;; Gotta use windows, because recenter does weird stuff if
3269           ;; the current buffer ain't the displayed window.
3270           (let ((owin (selected-window)))
3271             (select-window (get-buffer-window gnus-group-buffer t))
3272             (when (gnus-group-goto-group group)
3273               (recenter))
3274             (select-window owin)))
3275         ;; Mark this buffer as "prepared".
3276         (setq gnus-newsgroup-prepared t)
3277         (gnus-run-hooks 'gnus-summary-prepared-hook)
3278         t)))))
3279
3280 (defun gnus-summary-prepare ()
3281   "Generate the summary buffer."
3282   (interactive)
3283   (let ((buffer-read-only nil))
3284     (erase-buffer)
3285     (setq gnus-newsgroup-data nil
3286           gnus-newsgroup-data-reverse nil)
3287     (gnus-run-hooks 'gnus-summary-generate-hook)
3288     ;; Generate the buffer, either with threads or without.
3289     (when gnus-newsgroup-headers
3290       (gnus-summary-prepare-threads
3291        (if gnus-show-threads
3292            (gnus-sort-gathered-threads
3293             (funcall gnus-summary-thread-gathering-function
3294                      (gnus-sort-threads
3295                       (gnus-cut-threads (gnus-make-threads)))))
3296          ;; Unthreaded display.
3297          (gnus-sort-articles gnus-newsgroup-headers))))
3298     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3299     ;; Call hooks for modifying summary buffer.
3300     (goto-char (point-min))
3301     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3302
3303 (defsubst gnus-general-simplify-subject (subject)
3304   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3305   (setq subject
3306         (cond
3307          ;; Truncate the subject.
3308          (gnus-simplify-subject-functions
3309           (gnus-map-function gnus-simplify-subject-functions subject))
3310          ((numberp gnus-summary-gather-subject-limit)
3311           (setq subject (gnus-simplify-subject-re subject))
3312           (if (> (length subject) gnus-summary-gather-subject-limit)
3313               (substring subject 0 gnus-summary-gather-subject-limit)
3314             subject))
3315          ;; Fuzzily simplify it.
3316          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3317           (gnus-simplify-subject-fuzzy subject))
3318          ;; Just remove the leading "Re:".
3319          (t
3320           (gnus-simplify-subject-re subject))))
3321
3322   (if (and gnus-summary-gather-exclude-subject
3323            (string-match gnus-summary-gather-exclude-subject subject))
3324       nil                               ; This article shouldn't be gathered
3325     subject))
3326
3327 (defun gnus-summary-simplify-subject-query ()
3328   "Query where the respool algorithm would put this article."
3329   (interactive)
3330   (gnus-summary-select-article)
3331   (message "%s"
3332            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3333
3334 (defun gnus-gather-threads-by-subject (threads)
3335   "Gather threads by looking at Subject headers."
3336   (if (not gnus-summary-make-false-root)
3337       threads
3338     (let ((hashtb (gnus-make-hashtable 1024))
3339           (prev threads)
3340           (result threads)
3341           subject hthread whole-subject)
3342       (while threads
3343         (setq subject (gnus-general-simplify-subject
3344                        (setq whole-subject (mail-header-subject
3345                                             (caar threads)))))
3346         (when subject
3347           (if (setq hthread (gnus-gethash subject hashtb))
3348               (progn
3349                 ;; We enter a dummy root into the thread, if we
3350                 ;; haven't done that already.
3351                 (unless (stringp (caar hthread))
3352                   (setcar hthread (list whole-subject (car hthread))))
3353                 ;; We add this new gathered thread to this gathered
3354                 ;; thread.
3355                 (setcdr (car hthread)
3356                         (nconc (cdar hthread) (list (car threads))))
3357                 ;; Remove it from the list of threads.
3358                 (setcdr prev (cdr threads))
3359                 (setq threads prev))
3360             ;; Enter this thread into the hash table.
3361             (gnus-sethash subject threads hashtb)))
3362         (setq prev threads)
3363         (setq threads (cdr threads)))
3364       result)))
3365
3366 (defun gnus-gather-threads-by-references (threads)
3367   "Gather threads by looking at References headers."
3368   (let ((idhashtb (gnus-make-hashtable 1024))
3369         (thhashtb (gnus-make-hashtable 1024))
3370         (prev threads)
3371         (result threads)
3372         ids references id gthread gid entered ref)
3373     (while threads
3374       (when (setq references (mail-header-references (caar threads)))
3375         (setq id (mail-header-id (caar threads))
3376               ids (gnus-split-references references)
3377               entered nil)
3378         (while (setq ref (pop ids))
3379           (setq ids (delete ref ids))
3380           (if (not (setq gid (gnus-gethash ref idhashtb)))
3381               (progn
3382                 (gnus-sethash ref id idhashtb)
3383                 (gnus-sethash id threads thhashtb))
3384             (setq gthread (gnus-gethash gid thhashtb))
3385             (unless entered
3386               ;; We enter a dummy root into the thread, if we
3387               ;; haven't done that already.
3388               (unless (stringp (caar gthread))
3389                 (setcar gthread (list (mail-header-subject (caar gthread))
3390                                       (car gthread))))
3391               ;; We add this new gathered thread to this gathered
3392               ;; thread.
3393               (setcdr (car gthread)
3394                       (nconc (cdar gthread) (list (car threads)))))
3395             ;; Add it into the thread hash table.
3396             (gnus-sethash id gthread thhashtb)
3397             (setq entered t)
3398             ;; Remove it from the list of threads.
3399             (setcdr prev (cdr threads))
3400             (setq threads prev))))
3401       (setq prev threads)
3402       (setq threads (cdr threads)))
3403     result))
3404
3405 (defun gnus-sort-gathered-threads (threads)
3406   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3407   (let ((result threads))
3408     (while threads
3409       (when (stringp (caar threads))
3410         (setcdr (car threads)
3411                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3412       (setq threads (cdr threads)))
3413     result))
3414
3415 (defun gnus-thread-loop-p (root thread)
3416   "Say whether ROOT is in THREAD."
3417   (let ((stack (list thread))
3418         (infloop 0)
3419         th)
3420     (while (setq thread (pop stack))
3421       (setq th (cdr thread))
3422       (while (and th
3423                   (not (eq (caar th) root)))
3424         (pop th))
3425       (if th
3426           ;; We have found a loop.
3427           (let (ref-dep)
3428             (setcdr thread (delq (car th) (cdr thread)))
3429             (if (boundp (setq ref-dep (intern "none"
3430                                               gnus-newsgroup-dependencies)))
3431                 (setcdr (symbol-value ref-dep)
3432                         (nconc (cdr (symbol-value ref-dep))
3433                                (list (car th))))
3434               (set ref-dep (list nil (car th))))
3435             (setq infloop 1
3436                   stack nil))
3437         ;; Push all the subthreads onto the stack.
3438         (push (cdr thread) stack)))
3439     infloop))
3440
3441 (defun gnus-make-threads ()
3442   "Go through the dependency hashtb and find the roots.  Return all threads."
3443   (let (threads)
3444     (while (catch 'infloop
3445              (mapatoms
3446               (lambda (refs)
3447                 ;; Deal with self-referencing References loops.
3448                 (when (and (car (symbol-value refs))
3449                            (not (zerop
3450                                  (apply
3451                                   '+
3452                                   (mapcar
3453                                    (lambda (thread)
3454                                      (gnus-thread-loop-p
3455                                       (car (symbol-value refs)) thread))
3456                                    (cdr (symbol-value refs)))))))
3457                   (setq threads nil)
3458                   (throw 'infloop t))
3459                 (unless (car (symbol-value refs))
3460                   ;; These threads do not refer back to any other articles,
3461                   ;; so they're roots.
3462                   (setq threads (append (cdr (symbol-value refs)) threads))))
3463               gnus-newsgroup-dependencies)))
3464     threads))
3465
3466 ;; Build the thread tree.
3467 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3468   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3469
3470 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3471 if it was already present.
3472
3473 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3474 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3475 Message-IDs will be renamed be renamed to a unique Message-ID before
3476 being entered.
3477
3478 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3479   (let* ((id (mail-header-id header))
3480          (id-dep (and id (intern id dependencies)))
3481          ref ref-dep ref-header)
3482     ;; Enter this `header' in the `dependencies' table.
3483     (cond
3484      ((not id-dep)
3485       (setq header nil))
3486      ;; The first two cases do the normal part: enter a new `header'
3487      ;; in the `dependencies' table.
3488      ((not (boundp id-dep))
3489       (set id-dep (list header)))
3490      ((null (car (symbol-value id-dep)))
3491       (setcar (symbol-value id-dep) header))
3492
3493      ;; From here the `header' was already present in the
3494      ;; `dependencies' table.
3495      (force-new
3496       ;; Overrides an existing entry;
3497       ;; just set the header part of the entry.
3498       (setcar (symbol-value id-dep) header))
3499
3500      ;; Renames the existing `header' to a unique Message-ID.
3501      ((not gnus-summary-ignore-duplicates)
3502       ;; An article with this Message-ID has already been seen.
3503       ;; We rename the Message-ID.
3504       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3505            (list header))
3506       (mail-header-set-id header id))
3507
3508      ;; The last case ignores an existing entry, except it adds any
3509      ;; additional Xrefs (in case the two articles came from different
3510      ;; servers.
3511      ;; Also sets `header' to `nil' meaning that the `dependencies'
3512      ;; table was *not* modified.
3513      (t
3514       (mail-header-set-xref
3515        (car (symbol-value id-dep))
3516        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3517                    "")
3518                (or (mail-header-xref header) "")))
3519       (setq header nil)))
3520
3521     (when header
3522       ;; First check if that we are not creating a References loop.
3523       (setq ref (gnus-parent-id (mail-header-references header)))
3524       (while (and ref
3525                   (setq ref-dep (intern-soft ref dependencies))
3526                   (boundp ref-dep)
3527                   (setq ref-header (car (symbol-value ref-dep))))
3528         (if (string= id ref)
3529             ;; Yuk!  This is a reference loop.  Make the article be a
3530             ;; root article.
3531             (progn
3532               (mail-header-set-references (car (symbol-value id-dep)) "none")
3533               (setq ref nil))
3534           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3535       (setq ref (gnus-parent-id (mail-header-references header)))
3536       (setq ref-dep (intern (or ref "none") dependencies))
3537       (if (boundp ref-dep)
3538           (setcdr (symbol-value ref-dep)
3539                   (nconc (cdr (symbol-value ref-dep))
3540                          (list (symbol-value id-dep))))
3541         (set ref-dep (list nil (symbol-value id-dep)))))
3542     header))
3543
3544 (defun gnus-build-sparse-threads ()
3545   (let ((headers gnus-newsgroup-headers)
3546         (mail-parse-charset gnus-newsgroup-charset)
3547         (gnus-summary-ignore-duplicates t)
3548         header references generation relations
3549         subject child end new-child date)
3550     ;; First we create an alist of generations/relations, where
3551     ;; generations is how much we trust the relation, and the relation
3552     ;; is parent/child.
3553     (gnus-message 7 "Making sparse threads...")
3554     (save-excursion
3555       (nnheader-set-temp-buffer " *gnus sparse threads*")
3556       (while (setq header (pop headers))
3557         (when (and (setq references (mail-header-references header))
3558                    (not (string= references "")))
3559           (insert references)
3560           (setq child (mail-header-id header)
3561                 subject (mail-header-subject header)
3562                 date (mail-header-date header)
3563                 generation 0)
3564           (while (search-backward ">" nil t)
3565             (setq end (1+ (point)))
3566             (when (search-backward "<" nil t)
3567               (setq new-child (buffer-substring (point) end))
3568               (push (list (incf generation)
3569                           child (setq child new-child)
3570                           subject date)
3571                     relations)))
3572           (when child
3573             (push (list (1+ generation) child nil subject) relations))
3574           (erase-buffer)))
3575       (kill-buffer (current-buffer)))
3576     ;; Sort over trustworthiness.
3577     (mapcar
3578      (lambda (relation)
3579        (when (gnus-dependencies-add-header
3580               (make-full-mail-header-from-decoded-header
3581                gnus-reffed-article-number
3582                (nth 3 relation) "" (or (nth 4 relation) "")
3583                (nth 1 relation)
3584                (or (nth 2 relation) "") 0 0 "")
3585               gnus-newsgroup-dependencies nil)
3586          (push gnus-reffed-article-number gnus-newsgroup-limit)
3587          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3588          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3589                gnus-newsgroup-reads)
3590          (decf gnus-reffed-article-number)))
3591      (sort relations 'car-less-than-car))
3592     (gnus-message 7 "Making sparse threads...done")))
3593
3594 (defun gnus-build-old-threads ()
3595   ;; Look at all the articles that refer back to old articles, and
3596   ;; fetch the headers for the articles that aren't there.  This will
3597   ;; build complete threads - if the roots haven't been expired by the
3598   ;; server, that is.
3599   (let ((mail-parse-charset gnus-newsgroup-charset)
3600         id heads)
3601     (mapatoms
3602      (lambda (refs)
3603        (when (not (car (symbol-value refs)))
3604          (setq heads (cdr (symbol-value refs)))
3605          (while heads
3606            (if (memq (mail-header-number (caar heads))
3607                      gnus-newsgroup-dormant)
3608                (setq heads (cdr heads))
3609              (setq id (symbol-name refs))
3610              (while (and (setq id (gnus-build-get-header id))
3611                          (not (car (gnus-id-to-thread id)))))
3612              (setq heads nil)))))
3613      gnus-newsgroup-dependencies)))
3614
3615 ;; This function has to be called with point after the article number
3616 ;; on the beginning of the line.
3617 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3618   (let ((eol (gnus-point-at-eol))
3619         (buffer (current-buffer))
3620         header)
3621
3622     ;; overview: [num subject from date id refs chars lines misc]
3623     (unwind-protect
3624         (progn
3625           (narrow-to-region (point) eol)
3626           (unless (eobp)
3627             (forward-char))
3628
3629           (setq header
3630                 (make-full-mail-header
3631                  number                         ; number
3632                  (nnheader-nov-field)           ; subject
3633                  (nnheader-nov-field)           ; from
3634                  (nnheader-nov-field)           ; date
3635                  (nnheader-nov-read-message-id) ; id
3636                  (nnheader-nov-field)           ; refs
3637                  (nnheader-nov-read-integer)    ; chars
3638                  (nnheader-nov-read-integer)    ; lines
3639                  (unless (eobp)
3640                    (if (looking-at "Xref: ")
3641                        (goto-char (match-end 0)))
3642                    (nnheader-nov-field))        ; Xref
3643                  (nnheader-nov-parse-extra))))  ; extra
3644
3645       (widen))
3646
3647     (when gnus-alter-header-function
3648       (funcall gnus-alter-header-function header))
3649     (gnus-dependencies-add-header header dependencies force-new)))
3650
3651 (defun gnus-build-get-header (id)
3652   "Look through the buffer of NOV lines and find the header to ID.
3653 Enter this line into the dependencies hash table, and return
3654 the id of the parent article (if any)."
3655   (let ((deps gnus-newsgroup-dependencies)
3656         found header)
3657     (prog1
3658         (save-excursion
3659           (set-buffer nntp-server-buffer)
3660           (let ((case-fold-search nil))
3661             (goto-char (point-min))
3662             (while (and (not found)
3663                         (search-forward id nil t))
3664               (beginning-of-line)
3665               (setq found (looking-at
3666                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3667                                    (regexp-quote id))))
3668               (or found (beginning-of-line 2)))
3669             (when found
3670               (beginning-of-line)
3671               (and
3672                (setq header (gnus-nov-parse-line
3673                              (read (current-buffer)) deps))
3674                (gnus-parent-id (mail-header-references header))))))
3675       (when header
3676         (let ((number (mail-header-number header)))
3677           (push number gnus-newsgroup-limit)
3678           (push header gnus-newsgroup-headers)
3679           (if (memq number gnus-newsgroup-unselected)
3680               (progn
3681                 (push number gnus-newsgroup-unreads)
3682                 (setq gnus-newsgroup-unselected
3683                       (delq number gnus-newsgroup-unselected)))
3684             (push number gnus-newsgroup-ancient)))))))
3685
3686 (defun gnus-build-all-threads ()
3687   "Read all the headers."
3688   (let ((gnus-summary-ignore-duplicates t)
3689         (mail-parse-charset gnus-newsgroup-charset)
3690         (dependencies gnus-newsgroup-dependencies)
3691         header article)
3692     (save-excursion
3693       (set-buffer nntp-server-buffer)
3694       (let ((case-fold-search nil))
3695         (goto-char (point-min))
3696         (while (not (eobp))
3697           (ignore-errors
3698             (setq article (read (current-buffer))
3699                   header (gnus-nov-parse-line article dependencies)))
3700           (when header
3701             (save-excursion
3702               (set-buffer gnus-summary-buffer)
3703               (push header gnus-newsgroup-headers)
3704               (if (memq (setq article (mail-header-number header))
3705                         gnus-newsgroup-unselected)
3706                   (progn
3707                     (push article gnus-newsgroup-unreads)
3708                     (setq gnus-newsgroup-unselected
3709                           (delq article gnus-newsgroup-unselected)))
3710                 (push article gnus-newsgroup-ancient)))
3711             (forward-line 1)))))))
3712
3713 (defun gnus-summary-update-article-line (article header)
3714   "Update the line for ARTICLE using HEADERS."
3715   (let* ((id (mail-header-id header))
3716          (thread (gnus-id-to-thread id)))
3717     (unless thread
3718       (error "Article in no thread"))
3719     ;; Update the thread.
3720     (setcar thread header)
3721     (gnus-summary-goto-subject article)
3722     (let* ((datal (gnus-data-find-list article))
3723            (data (car datal))
3724            (length (when (cdr datal)
3725                      (- (gnus-data-pos data)
3726                         (gnus-data-pos (cadr datal)))))
3727            (buffer-read-only nil)
3728            (level (gnus-summary-thread-level)))
3729       (gnus-delete-line)
3730       (gnus-summary-insert-line
3731        header level nil (gnus-article-mark article)
3732        (memq article gnus-newsgroup-replied)
3733        (memq article gnus-newsgroup-expirable)
3734        ;; Only insert the Subject string when it's different
3735        ;; from the previous Subject string.
3736        (if (and
3737             gnus-show-threads
3738             (gnus-subject-equal
3739              (condition-case ()
3740                  (mail-header-subject
3741                   (gnus-data-header
3742                    (cadr
3743                     (gnus-data-find-list
3744                      article
3745                      (gnus-data-list t)))))
3746                ;; Error on the side of excessive subjects.
3747                (error ""))
3748              (mail-header-subject header)))
3749            ""
3750          (mail-header-subject header))
3751        nil (cdr (assq article gnus-newsgroup-scored))
3752        (memq article gnus-newsgroup-processable))
3753       (when length
3754         (gnus-data-update-list
3755          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3756
3757 (defun gnus-summary-update-article (article &optional iheader)
3758   "Update ARTICLE in the summary buffer."
3759   (set-buffer gnus-summary-buffer)
3760   (let* ((header (gnus-summary-article-header article))
3761          (id (mail-header-id header))
3762          (data (gnus-data-find article))
3763          (thread (gnus-id-to-thread id))
3764          (references (mail-header-references header))
3765          (parent
3766           (gnus-id-to-thread
3767            (or (gnus-parent-id
3768                 (when (and references
3769                            (not (equal "" references)))
3770                   references))
3771                "none")))
3772          (buffer-read-only nil)
3773          (old (car thread)))
3774     (when thread
3775       (unless iheader
3776         (setcar thread nil)
3777         (when parent
3778           (delq thread parent)))
3779       (if (gnus-summary-insert-subject id header)
3780           ;; Set the (possibly) new article number in the data structure.
3781           (gnus-data-set-number data (gnus-id-to-article id))
3782         (setcar thread old)
3783         nil))))
3784
3785 (defun gnus-rebuild-thread (id &optional line)
3786   "Rebuild the thread containing ID.
3787 If LINE, insert the rebuilt thread starting on line LINE."
3788   (let ((buffer-read-only nil)
3789         old-pos current thread data)
3790     (if (not gnus-show-threads)
3791         (setq thread (list (car (gnus-id-to-thread id))))
3792       ;; Get the thread this article is part of.
3793       (setq thread (gnus-remove-thread id)))
3794     (setq old-pos (gnus-point-at-bol))
3795     (setq current (save-excursion
3796                     (and (re-search-backward "[\r\n]" nil t)
3797                          (gnus-summary-article-number))))
3798     ;; If this is a gathered thread, we have to go some re-gathering.
3799     (when (stringp (car thread))
3800       (let ((subject (car thread))
3801             roots thr)
3802         (setq thread (cdr thread))
3803         (while thread
3804           (unless (memq (setq thr (gnus-id-to-thread
3805                                    (gnus-root-id
3806                                     (mail-header-id (caar thread)))))
3807                         roots)
3808             (push thr roots))
3809           (setq thread (cdr thread)))
3810         ;; We now have all (unique) roots.
3811         (if (= (length roots) 1)
3812             ;; All the loose roots are now one solid root.
3813             (setq thread (car roots))
3814           (setq thread (cons subject (gnus-sort-threads roots))))))
3815     (let (threads)
3816       ;; We then insert this thread into the summary buffer.
3817       (when line
3818         (goto-char (point-min))
3819         (forward-line (1- line)))
3820       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3821         (if gnus-show-threads
3822             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3823           (gnus-summary-prepare-unthreaded thread))
3824         (setq data (nreverse gnus-newsgroup-data))
3825         (setq threads gnus-newsgroup-threads))
3826       ;; We splice the new data into the data structure.
3827       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3828       ;;!!! then we want to insert at the beginning of the buffer.
3829       ;;!!! That happens to be true with Gnus now, but that may
3830       ;;!!! change in the future.  Perhaps.
3831       (gnus-data-enter-list
3832        (if line nil current) data (- (point) old-pos))
3833       (setq gnus-newsgroup-threads
3834             (nconc threads gnus-newsgroup-threads))
3835       (gnus-data-compute-positions))))
3836
3837 (defun gnus-number-to-header (number)
3838   "Return the header for article NUMBER."
3839   (let ((headers gnus-newsgroup-headers))
3840     (while (and headers
3841                 (not (= number (mail-header-number (car headers)))))
3842       (pop headers))
3843     (when headers
3844       (car headers))))
3845
3846 (defun gnus-parent-headers (in-headers &optional generation)
3847   "Return the headers of the GENERATIONeth parent of HEADERS."
3848   (unless generation
3849     (setq generation 1))
3850   (let ((parent t)
3851         (headers in-headers)
3852         references)
3853     (while (and parent
3854                 (not (zerop generation))
3855                 (setq references (mail-header-references headers)))
3856       (setq headers (if (and references
3857                              (setq parent (gnus-parent-id references)))
3858                         (car (gnus-id-to-thread parent))
3859                       nil))
3860       (decf generation))
3861     (and (not (eq headers in-headers))
3862          headers)))
3863
3864 (defun gnus-id-to-thread (id)
3865   "Return the (sub-)thread where ID appears."
3866   (gnus-gethash id gnus-newsgroup-dependencies))
3867
3868 (defun gnus-id-to-article (id)
3869   "Return the article number of ID."
3870   (let ((thread (gnus-id-to-thread id)))
3871     (when (and thread
3872                (car thread))
3873       (mail-header-number (car thread)))))
3874
3875 (defun gnus-id-to-header (id)
3876   "Return the article headers of ID."
3877   (car (gnus-id-to-thread id)))
3878
3879 (defun gnus-article-displayed-root-p (article)
3880   "Say whether ARTICLE is a root(ish) article."
3881   (let ((level (gnus-summary-thread-level article))
3882         (refs (mail-header-references  (gnus-summary-article-header article)))
3883         particle)
3884     (cond
3885      ((null level) nil)
3886      ((zerop level) t)
3887      ((null refs) t)
3888      ((null (gnus-parent-id refs)) t)
3889      ((and (= 1 level)
3890            (null (setq particle (gnus-id-to-article
3891                                  (gnus-parent-id refs))))
3892            (null (gnus-summary-thread-level particle)))))))
3893
3894 (defun gnus-root-id (id)
3895   "Return the id of the root of the thread where ID appears."
3896   (let (last-id prev)
3897     (while (and id (setq prev (car (gnus-id-to-thread id))))
3898       (setq last-id id
3899             id (gnus-parent-id (mail-header-references prev))))
3900     last-id))
3901
3902 (defun gnus-articles-in-thread (thread)
3903   "Return the list of articles in THREAD."
3904   (cons (mail-header-number (car thread))
3905         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3906
3907 (defun gnus-remove-thread (id &optional dont-remove)
3908   "Remove the thread that has ID in it."
3909   (let (headers thread last-id)
3910     ;; First go up in this thread until we find the root.
3911     (setq last-id (gnus-root-id id)
3912           headers (message-flatten-list (gnus-id-to-thread last-id)))
3913     ;; We have now found the real root of this thread.  It might have
3914     ;; been gathered into some loose thread, so we have to search
3915     ;; through the threads to find the thread we wanted.
3916     (let ((threads gnus-newsgroup-threads)
3917           sub)
3918       (while threads
3919         (setq sub (car threads))
3920         (if (stringp (car sub))
3921             ;; This is a gathered thread, so we look at the roots
3922             ;; below it to find whether this article is in this
3923             ;; gathered root.
3924             (progn
3925               (setq sub (cdr sub))
3926               (while sub
3927                 (when (member (caar sub) headers)
3928                   (setq thread (car threads)
3929                         threads nil
3930                         sub nil))
3931                 (setq sub (cdr sub))))
3932           ;; It's an ordinary thread, so we check it.
3933           (when (eq (car sub) (car headers))
3934             (setq thread sub
3935                   threads nil)))
3936         (setq threads (cdr threads)))
3937       ;; If this article is in no thread, then it's a root.
3938       (if thread
3939           (unless dont-remove
3940             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3941         (setq thread (gnus-id-to-thread last-id)))
3942       (when thread
3943         (prog1
3944             thread                      ; We return this thread.
3945           (unless dont-remove
3946             (if (stringp (car thread))
3947                 (progn
3948                   ;; If we use dummy roots, then we have to remove the
3949                   ;; dummy root as well.
3950                   (when (eq gnus-summary-make-false-root 'dummy)
3951                     ;; We go to the dummy root by going to
3952                     ;; the first sub-"thread", and then one line up.
3953                     (gnus-summary-goto-article
3954                      (mail-header-number (caadr thread)))
3955                     (forward-line -1)
3956                     (gnus-delete-line)
3957                     (gnus-data-compute-positions))
3958                   (setq thread (cdr thread))
3959                   (while thread
3960                     (gnus-remove-thread-1 (car thread))
3961                     (setq thread (cdr thread))))
3962               (gnus-remove-thread-1 thread))))))))
3963
3964 (defun gnus-remove-thread-1 (thread)
3965   "Remove the thread THREAD recursively."
3966   (let ((number (mail-header-number (pop thread)))
3967         d)
3968     (setq thread (reverse thread))
3969     (while thread
3970       (gnus-remove-thread-1 (pop thread)))
3971     (when (setq d (gnus-data-find number))
3972       (goto-char (gnus-data-pos d))
3973       (gnus-summary-show-thread)
3974       (gnus-data-remove
3975        number
3976        (- (gnus-point-at-bol)
3977           (prog1
3978               (1+ (gnus-point-at-eol))
3979             (gnus-delete-line)))))))
3980
3981 (defun gnus-sort-threads-1 (threads func)
3982   (sort (mapcar (lambda (thread)
3983                   (cons (car thread)
3984                         (and (cdr thread)
3985                              (gnus-sort-threads-1 (cdr thread) func))))
3986                 threads) func))
3987
3988 (defun gnus-sort-threads (threads)
3989   "Sort THREADS."
3990   (if (not gnus-thread-sort-functions)
3991       threads
3992     (gnus-message 8 "Sorting threads...")
3993     (prog1
3994         (gnus-sort-threads-1
3995          threads
3996          (gnus-make-sort-function gnus-thread-sort-functions))
3997       (gnus-message 8 "Sorting threads...done"))))
3998
3999 (defun gnus-sort-articles (articles)
4000   "Sort ARTICLES."
4001   (when gnus-article-sort-functions
4002     (gnus-message 7 "Sorting articles...")
4003     (prog1
4004         (setq gnus-newsgroup-headers
4005               (sort articles (gnus-make-sort-function
4006                               gnus-article-sort-functions)))
4007       (gnus-message 7 "Sorting articles...done"))))
4008
4009 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4010 (defmacro gnus-thread-header (thread)
4011   "Return header of first article in THREAD.
4012 Note that THREAD must never, ever be anything else than a variable -
4013 using some other form will lead to serious barfage."
4014   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4015   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4016   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4017         (vector thread) 2))
4018
4019 (defsubst gnus-article-sort-by-number (h1 h2)
4020   "Sort articles by article number."
4021   (< (mail-header-number h1)
4022      (mail-header-number h2)))
4023
4024 (defun gnus-thread-sort-by-number (h1 h2)
4025   "Sort threads by root article number."
4026   (gnus-article-sort-by-number
4027    (gnus-thread-header h1) (gnus-thread-header h2)))
4028
4029 (defsubst gnus-article-sort-by-lines (h1 h2)
4030   "Sort articles by article Lines header."
4031   (< (mail-header-lines h1)
4032      (mail-header-lines h2)))
4033
4034 (defun gnus-thread-sort-by-lines (h1 h2)
4035   "Sort threads by root article Lines header."
4036   (gnus-article-sort-by-lines
4037    (gnus-thread-header h1) (gnus-thread-header h2)))
4038
4039 (defsubst gnus-article-sort-by-chars (h1 h2)
4040   "Sort articles by octet length."
4041   (< (mail-header-chars h1)
4042      (mail-header-chars h2)))
4043
4044 (defun gnus-thread-sort-by-chars (h1 h2)
4045   "Sort threads by root article octet length."
4046   (gnus-article-sort-by-chars
4047    (gnus-thread-header h1) (gnus-thread-header h2)))
4048
4049 (defsubst gnus-article-sort-by-author (h1 h2)
4050   "Sort articles by root author."
4051   (string-lessp
4052    (let ((addr (car (mime-entity-read-field h1 'From))))
4053      (or (std11-full-name-string addr)
4054          (std11-address-string addr)
4055          ""))
4056    (let ((addr (car (mime-entity-read-field h2 'From))))
4057      (or (std11-full-name-string addr)
4058          (std11-address-string addr)
4059          ""))
4060    ))
4061
4062 (defun gnus-thread-sort-by-author (h1 h2)
4063   "Sort threads by root author."
4064   (gnus-article-sort-by-author
4065    (gnus-thread-header h1)  (gnus-thread-header h2)))
4066
4067 (defsubst gnus-article-sort-by-subject (h1 h2)
4068   "Sort articles by root subject."
4069   (string-lessp
4070    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4071    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4072
4073 (defun gnus-thread-sort-by-subject (h1 h2)
4074   "Sort threads by root subject."
4075   (gnus-article-sort-by-subject
4076    (gnus-thread-header h1) (gnus-thread-header h2)))
4077
4078 (defsubst gnus-article-sort-by-date (h1 h2)
4079   "Sort articles by root article date."
4080   (time-less-p
4081    (gnus-date-get-time (mail-header-date h1))
4082    (gnus-date-get-time (mail-header-date h2))))
4083
4084 (defun gnus-thread-sort-by-date (h1 h2)
4085   "Sort threads by root article date."
4086   (gnus-article-sort-by-date
4087    (gnus-thread-header h1) (gnus-thread-header h2)))
4088
4089 (defsubst gnus-article-sort-by-score (h1 h2)
4090   "Sort articles by root article score.
4091 Unscored articles will be counted as having a score of zero."
4092   (> (or (cdr (assq (mail-header-number h1)
4093                     gnus-newsgroup-scored))
4094          gnus-summary-default-score 0)
4095      (or (cdr (assq (mail-header-number h2)
4096                     gnus-newsgroup-scored))
4097          gnus-summary-default-score 0)))
4098
4099 (defun gnus-thread-sort-by-score (h1 h2)
4100   "Sort threads by root article score."
4101   (gnus-article-sort-by-score
4102    (gnus-thread-header h1) (gnus-thread-header h2)))
4103
4104 (defun gnus-thread-sort-by-total-score (h1 h2)
4105   "Sort threads by the sum of all scores in the thread.
4106 Unscored articles will be counted as having a score of zero."
4107   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4108
4109 (defun gnus-thread-total-score (thread)
4110   ;; This function find the total score of THREAD.
4111   (cond ((null thread)
4112          0)
4113         ((consp thread)
4114          (if (stringp (car thread))
4115              (apply gnus-thread-score-function 0
4116                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4117            (gnus-thread-total-score-1 thread)))
4118         (t
4119          (gnus-thread-total-score-1 (list thread)))))
4120
4121 (defun gnus-thread-total-score-1 (root)
4122   ;; This function find the total score of the thread below ROOT.
4123   (setq root (car root))
4124   (apply gnus-thread-score-function
4125          (or (append
4126               (mapcar 'gnus-thread-total-score
4127                       (cdr (gnus-id-to-thread (mail-header-id root))))
4128               (when (> (mail-header-number root) 0)
4129                 (list (or (cdr (assq (mail-header-number root)
4130                                      gnus-newsgroup-scored))
4131                           gnus-summary-default-score 0))))
4132              (list gnus-summary-default-score)
4133              '(0))))
4134
4135 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4136 (defvar gnus-tmp-prev-subject nil)
4137 (defvar gnus-tmp-false-parent nil)
4138 (defvar gnus-tmp-root-expunged nil)
4139 (defvar gnus-tmp-dummy-line nil)
4140
4141 (eval-when-compile (defvar gnus-tmp-header))
4142 (defun gnus-extra-header (type &optional header)
4143   "Return the extra header of TYPE."
4144   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4145       ""))
4146
4147 (defvar gnus-tmp-thread-tree-header-string "")
4148
4149 (defvar gnus-sum-thread-tree-root "> "
4150   "With %B spec, used for the root of a thread.
4151 If nil, use subject instead.")
4152 (defvar gnus-sum-thread-tree-single-indent ""
4153   "With %B spec, used for a thread with just one message.
4154 If nil, use subject instead.")
4155 (defvar gnus-sum-thread-tree-vertical "| "
4156   "With %B spec, used for drawing a vertical line.")
4157 (defvar gnus-sum-thread-tree-indent "  "
4158   "With %B spec, used for indenting.")
4159 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4160   "With %B spec, used for a leaf with brothers.")
4161 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4162   "With %B spec, used for a leaf without brothers.")
4163
4164 (defun gnus-summary-prepare-threads (threads)
4165   "Prepare summary buffer from THREADS and indentation LEVEL.
4166 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4167 or a straight list of headers."
4168   (gnus-message 7 "Generating summary...")
4169
4170   (setq gnus-newsgroup-threads threads)
4171   (beginning-of-line)
4172
4173   (let ((gnus-tmp-level 0)
4174         (default-score (or gnus-summary-default-score 0))
4175         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4176         thread number subject stack state gnus-tmp-gathered beg-match
4177         new-roots gnus-tmp-new-adopts thread-end
4178         gnus-tmp-header gnus-tmp-unread
4179         gnus-tmp-replied gnus-tmp-subject-or-nil
4180         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4181         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4182         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4183         tree-stack)
4184
4185     (setq gnus-tmp-prev-subject nil)
4186
4187     (if (vectorp (car threads))
4188         ;; If this is a straight (sic) list of headers, then a
4189         ;; threaded summary display isn't required, so we just create
4190         ;; an unthreaded one.
4191         (gnus-summary-prepare-unthreaded threads)
4192
4193       ;; Do the threaded display.
4194
4195       (while (or threads stack gnus-tmp-new-adopts new-roots)
4196
4197         (if (and (= gnus-tmp-level 0)
4198                  (or (not stack)
4199                      (= (caar stack) 0))
4200                  (not gnus-tmp-false-parent)
4201                  (or gnus-tmp-new-adopts new-roots))
4202             (if gnus-tmp-new-adopts
4203                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4204                       thread (list (car gnus-tmp-new-adopts))
4205                       gnus-tmp-header (caar thread)
4206                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4207               (when new-roots
4208                 (setq thread (list (car new-roots))
4209                       gnus-tmp-header (caar thread)
4210                       new-roots (cdr new-roots))))
4211
4212           (if threads
4213               ;; If there are some threads, we do them before the
4214               ;; threads on the stack.
4215               (setq thread threads
4216                     gnus-tmp-header (caar thread))
4217             ;; There were no current threads, so we pop something off
4218             ;; the stack.
4219             (setq state (car stack)
4220                   gnus-tmp-level (car state)
4221                   tree-stack (cadr state)
4222                   thread (caddr state)
4223                   stack (cdr stack)
4224                   gnus-tmp-header (caar thread))))
4225
4226         (setq gnus-tmp-false-parent nil)
4227         (setq gnus-tmp-root-expunged nil)
4228         (setq thread-end nil)
4229
4230         (if (stringp gnus-tmp-header)
4231             ;; The header is a dummy root.
4232             (cond
4233              ((eq gnus-summary-make-false-root 'adopt)
4234               ;; We let the first article adopt the rest.
4235               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4236                                                (cddar thread)))
4237               (setq gnus-tmp-gathered
4238                     (nconc (mapcar
4239                             (lambda (h) (mail-header-number (car h)))
4240                             (cddar thread))
4241                            gnus-tmp-gathered))
4242               (setq thread (cons (list (caar thread)
4243                                        (cadar thread))
4244                                  (cdr thread)))
4245               (setq gnus-tmp-level -1
4246                     gnus-tmp-false-parent t))
4247              ((eq gnus-summary-make-false-root 'empty)
4248               ;; We print adopted articles with empty subject fields.
4249               (setq gnus-tmp-gathered
4250                     (nconc (mapcar
4251                             (lambda (h) (mail-header-number (car h)))
4252                             (cddar thread))
4253                            gnus-tmp-gathered))
4254               (setq gnus-tmp-level -1))
4255              ((eq gnus-summary-make-false-root 'dummy)
4256               ;; We remember that we probably want to output a dummy
4257               ;; root.
4258               (setq gnus-tmp-dummy-line gnus-tmp-header)
4259               (setq gnus-tmp-prev-subject gnus-tmp-header))
4260              (t
4261               ;; We do not make a root for the gathered
4262               ;; sub-threads at all.
4263               (setq gnus-tmp-level -1)))
4264
4265           (setq number (mail-header-number gnus-tmp-header)
4266                 subject (mail-header-subject gnus-tmp-header))
4267
4268           (cond
4269            ;; If the thread has changed subject, we might want to make
4270            ;; this subthread into a root.
4271            ((and (null gnus-thread-ignore-subject)
4272                  (not (zerop gnus-tmp-level))
4273                  gnus-tmp-prev-subject
4274                  (not (inline
4275                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4276             (setq new-roots (nconc new-roots (list (car thread)))
4277                   thread-end t
4278                   gnus-tmp-header nil))
4279            ;; If the article lies outside the current limit,
4280            ;; then we do not display it.
4281            ((not (memq number gnus-newsgroup-limit))
4282             (setq gnus-tmp-gathered
4283                   (nconc (mapcar
4284                           (lambda (h) (mail-header-number (car h)))
4285                           (cdar thread))
4286                          gnus-tmp-gathered))
4287             (setq gnus-tmp-new-adopts (if (cdar thread)
4288                                           (append gnus-tmp-new-adopts
4289                                                   (cdar thread))
4290                                         gnus-tmp-new-adopts)
4291                   thread-end t
4292                   gnus-tmp-header nil)
4293             (when (zerop gnus-tmp-level)
4294               (setq gnus-tmp-root-expunged t)))
4295            ;; Perhaps this article is to be marked as read?
4296            ((and gnus-summary-mark-below
4297                  (< (or (cdr (assq number gnus-newsgroup-scored))
4298                         default-score)
4299                     gnus-summary-mark-below)
4300                  ;; Don't touch sparse articles.
4301                  (not (gnus-summary-article-sparse-p number))
4302                  (not (gnus-summary-article-ancient-p number)))
4303             (setq gnus-newsgroup-unreads
4304                   (delq number gnus-newsgroup-unreads))
4305             (if gnus-newsgroup-auto-expire
4306                 (push number gnus-newsgroup-expirable)
4307               (push (cons number gnus-low-score-mark)
4308                     gnus-newsgroup-reads))))
4309
4310           (when gnus-tmp-header
4311             ;; We may have an old dummy line to output before this
4312             ;; article.
4313             (when (and gnus-tmp-dummy-line
4314                        (gnus-subject-equal
4315                         gnus-tmp-dummy-line
4316                         (mail-header-subject gnus-tmp-header)))
4317               (gnus-summary-insert-dummy-line
4318                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4319               (setq gnus-tmp-dummy-line nil))
4320
4321             ;; Compute the mark.
4322             (setq gnus-tmp-unread (gnus-article-mark number))
4323
4324             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4325                                   gnus-tmp-header gnus-tmp-level)
4326                   gnus-newsgroup-data)
4327
4328             ;; Actually insert the line.
4329             (setq
4330              gnus-tmp-subject-or-nil
4331              (cond
4332               ((and gnus-thread-ignore-subject
4333                     gnus-tmp-prev-subject
4334                     (not (inline (gnus-subject-equal
4335                                   gnus-tmp-prev-subject subject))))
4336                subject)
4337               ((zerop gnus-tmp-level)
4338                (if (and (eq gnus-summary-make-false-root 'empty)
4339                         (memq number gnus-tmp-gathered)
4340                         gnus-tmp-prev-subject
4341                         (inline (gnus-subject-equal
4342                                  gnus-tmp-prev-subject subject)))
4343                    gnus-summary-same-subject
4344                  subject))
4345               (t gnus-summary-same-subject)))
4346             (if (and (eq gnus-summary-make-false-root 'adopt)
4347                      (= gnus-tmp-level 1)
4348                      (memq number gnus-tmp-gathered))
4349                 (setq gnus-tmp-opening-bracket ?\<
4350                       gnus-tmp-closing-bracket ?\>)
4351               (setq gnus-tmp-opening-bracket ?\[
4352                     gnus-tmp-closing-bracket ?\]))
4353             (setq
4354              gnus-tmp-indentation
4355              (aref gnus-thread-indent-array gnus-tmp-level)
4356              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4357              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4358                                 gnus-summary-default-score 0)
4359              gnus-tmp-score-char
4360              (if (or (null gnus-summary-default-score)
4361                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4362                          gnus-summary-zcore-fuzz))
4363                  ?\ ;;;Whitespace
4364                (if (< gnus-tmp-score gnus-summary-default-score)
4365                    gnus-score-below-mark gnus-score-over-mark))
4366              gnus-tmp-replied
4367              (cond ((memq number gnus-newsgroup-processable)
4368                     gnus-process-mark)
4369                    ((memq number gnus-newsgroup-cached)
4370                     gnus-cached-mark)
4371                    ((memq number gnus-newsgroup-replied)
4372                     gnus-replied-mark)
4373                    ((memq number gnus-newsgroup-forwarded)
4374                     gnus-forwarded-mark)
4375                    ((memq number gnus-newsgroup-saved)
4376                     gnus-saved-mark)
4377                    ((memq number gnus-newsgroup-recent)
4378                     gnus-recent-mark)
4379                    ((memq number gnus-newsgroup-unseen)
4380                     gnus-unseen-mark)
4381                    (t gnus-no-mark))
4382              gnus-tmp-from (mail-header-from gnus-tmp-header)
4383              gnus-tmp-name
4384              (cond
4385               ((string-match "<[^>]+> *$" gnus-tmp-from)
4386                (setq beg-match (match-beginning 0))
4387                (or (and (string-match "^\".+\"" gnus-tmp-from)
4388                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4389                    (substring gnus-tmp-from 0 beg-match)))
4390               ((string-match "(.+)" gnus-tmp-from)
4391                (substring gnus-tmp-from
4392                           (1+ (match-beginning 0)) (1- (match-end 0))))
4393               (t gnus-tmp-from))
4394              gnus-tmp-thread-tree-header-string
4395              (cond
4396               ((not gnus-show-threads) "")
4397               ((zerop gnus-tmp-level)
4398                (if (cdar thread)
4399                    (or gnus-sum-thread-tree-root subject)
4400                  (or gnus-sum-thread-tree-single-indent subject)))
4401               (t
4402                (concat (apply 'concat
4403                               (mapcar (lambda (item)
4404                                         (if (= item 1)
4405                                             gnus-sum-thread-tree-vertical
4406                                           gnus-sum-thread-tree-indent))
4407                                       (cdr (reverse tree-stack))))
4408                        (if (nth 1 thread)
4409                            gnus-sum-thread-tree-leaf-with-other
4410                          gnus-sum-thread-tree-single-leaf)))))
4411             (when (string= gnus-tmp-name "")
4412               (setq gnus-tmp-name gnus-tmp-from))
4413             (unless (numberp gnus-tmp-lines)
4414               (setq gnus-tmp-lines -1))
4415             (if (= gnus-tmp-lines -1)
4416                 (setq gnus-tmp-lines "?")
4417               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4418             (gnus-put-text-property
4419              (point)
4420              (progn (eval gnus-summary-line-format-spec) (point))
4421              'gnus-number number)
4422             (when gnus-visual-p
4423               (forward-line -1)
4424               (gnus-run-hooks 'gnus-summary-update-hook)
4425               (forward-line 1))
4426
4427             (setq gnus-tmp-prev-subject subject)))
4428
4429         (when (nth 1 thread)
4430           (push (list (max 0 gnus-tmp-level)
4431                       (copy-list tree-stack)
4432                       (nthcdr 1 thread))
4433                 stack))
4434         (push (if (nth 1 thread) 1 0) tree-stack)
4435         (incf gnus-tmp-level)
4436         (setq threads (if thread-end nil (cdar thread)))
4437         (unless threads
4438           (setq gnus-tmp-level 0)))))
4439   (gnus-message 7 "Generating summary...done"))
4440
4441 (defun gnus-summary-prepare-unthreaded (headers)
4442   "Generate an unthreaded summary buffer based on HEADERS."
4443   (let (header number mark)
4444
4445     (beginning-of-line)
4446
4447     (while headers
4448       ;; We may have to root out some bad articles...
4449       (when (memq (setq number (mail-header-number
4450                                 (setq header (pop headers))))
4451                   gnus-newsgroup-limit)
4452         ;; Mark article as read when it has a low score.
4453         (when (and gnus-summary-mark-below
4454                    (< (or (cdr (assq number gnus-newsgroup-scored))
4455                           gnus-summary-default-score 0)
4456                       gnus-summary-mark-below)
4457                    (not (gnus-summary-article-ancient-p number)))
4458           (setq gnus-newsgroup-unreads
4459                 (delq number gnus-newsgroup-unreads))
4460           (if gnus-newsgroup-auto-expire
4461               (push number gnus-newsgroup-expirable)
4462             (push (cons number gnus-low-score-mark)
4463                   gnus-newsgroup-reads)))
4464
4465         (setq mark (gnus-article-mark number))
4466         (push (gnus-data-make number mark (1+ (point)) header 0)
4467               gnus-newsgroup-data)
4468         (gnus-summary-insert-line
4469          header 0 number
4470          mark (memq number gnus-newsgroup-replied)
4471          (memq number gnus-newsgroup-expirable)
4472          (mail-header-subject header) nil
4473          (cdr (assq number gnus-newsgroup-scored))
4474          (memq number gnus-newsgroup-processable))))))
4475
4476 (defun gnus-summary-remove-list-identifiers ()
4477   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4478   (let ((regexp (if (consp gnus-list-identifiers)
4479                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4480                   gnus-list-identifiers))
4481         changed subject)
4482     (when regexp
4483       (dolist (header gnus-newsgroup-headers)
4484         (setq subject (mail-header-subject header)
4485               changed nil)
4486         (while (string-match
4487                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4488                 subject)
4489           (setq subject
4490                 (concat (substring subject 0 (match-beginning 2))
4491                         (substring subject (match-end 0)))
4492                 changed t))
4493         (when (and changed
4494                    (string-match
4495                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4496           (setq subject
4497                 (concat (substring subject 0 (match-beginning 1))
4498                         (substring subject (match-end 1)))))
4499         (when changed
4500           (mail-header-set-subject header subject))))))
4501
4502 (defun gnus-fetch-headers (articles)
4503   "Fetch headers of ARTICLES."
4504   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4505     (gnus-message 5 "Fetching headers for %s..." name)
4506     (prog1
4507         (if (eq 'nov
4508                 (setq gnus-headers-retrieved-by
4509                       (gnus-retrieve-headers
4510                        articles gnus-newsgroup-name
4511                        ;; We might want to fetch old headers, but
4512                        ;; not if there is only 1 article.
4513                        (and (or (and
4514                                  (not (eq gnus-fetch-old-headers 'some))
4515                                  (not (numberp gnus-fetch-old-headers)))
4516                                 (> (length articles) 1))
4517                             gnus-fetch-old-headers))))
4518             (gnus-get-newsgroup-headers-xover
4519              articles nil nil gnus-newsgroup-name t)
4520           (gnus-get-newsgroup-headers))
4521       (gnus-message 5 "Fetching headers for %s...done" name))))
4522
4523 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4524   "Select newsgroup GROUP.
4525 If READ-ALL is non-nil, all articles in the group are selected.
4526 If SELECT-ARTICLES, only select those articles from GROUP."
4527   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4528          ;;!!! Dirty hack; should be removed.
4529          (gnus-summary-ignore-duplicates
4530           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4531               t
4532             gnus-summary-ignore-duplicates))
4533          (info (nth 2 entry))
4534          articles fetched-articles cached)
4535
4536     (unless (gnus-check-server
4537              (setq gnus-current-select-method
4538                    (gnus-find-method-for-group group)))
4539       (error "Couldn't open server"))
4540
4541     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4542         (gnus-activate-group group)     ; Or we can activate it...
4543         (progn                          ; Or we bug out.
4544           (when (equal major-mode 'gnus-summary-mode)
4545             (kill-buffer (current-buffer)))
4546           (error "Couldn't activate group %s: %s"
4547                  group (gnus-status-message group))))
4548
4549     (unless (gnus-request-group group t)
4550       (when (equal major-mode 'gnus-summary-mode)
4551         (kill-buffer (current-buffer)))
4552       (error "Couldn't request group %s: %s"
4553              group (gnus-status-message group)))
4554
4555     (setq gnus-newsgroup-name group
4556           gnus-newsgroup-unselected nil
4557           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4558
4559     (let ((display (gnus-group-find-parameter group 'display)))
4560       (setq gnus-newsgroup-display
4561             (cond
4562              ((eq display 'all)
4563               'gnus-not-ignore)
4564              ((arrayp display)
4565               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4566              (t
4567               nil))))
4568
4569     (gnus-summary-setup-default-charset)
4570
4571     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4572     (when (gnus-virtual-group-p group)
4573       (setq cached gnus-newsgroup-cached))
4574
4575     (setq gnus-newsgroup-unreads
4576           (gnus-set-difference
4577            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4578            gnus-newsgroup-dormant))
4579
4580     (setq gnus-newsgroup-processable nil)
4581
4582     (gnus-update-read-articles group gnus-newsgroup-unreads)
4583
4584     ;; Adjust and set lists of article marks.
4585     (when info
4586       (gnus-adjust-marked-articles info))
4587
4588     (if (setq articles select-articles)
4589         (setq gnus-newsgroup-unselected
4590               (gnus-sorted-intersection
4591                gnus-newsgroup-unreads
4592                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4593       (setq articles (gnus-articles-to-read group read-all)))
4594
4595     (cond
4596      ((null articles)
4597       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4598       'quit)
4599      ((eq articles 0) nil)
4600      (t
4601       ;; Init the dependencies hash table.
4602       (setq gnus-newsgroup-dependencies
4603             (gnus-make-hashtable (length articles)))
4604       (gnus-set-global-variables)
4605       ;; Retrieve the headers and read them in.
4606       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4607
4608       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4609       (when cached
4610         (setq gnus-newsgroup-cached cached))
4611
4612       ;; Suppress duplicates?
4613       (when gnus-suppress-duplicates
4614         (gnus-dup-suppress-articles))
4615
4616       ;; Set the initial limit.
4617       (setq gnus-newsgroup-limit (copy-sequence articles))
4618       ;; Remove canceled articles from the list of unread articles.
4619       (setq fetched-articles
4620             (mapcar (lambda (headers) (mail-header-number headers))
4621                     gnus-newsgroup-headers))
4622       (setq gnus-newsgroup-articles fetched-articles)
4623       (setq gnus-newsgroup-unreads
4624             (gnus-set-sorted-intersection
4625              gnus-newsgroup-unreads fetched-articles))
4626
4627       (let ((marks (assq 'seen (gnus-info-marks info))))
4628         ;; The `seen' marks are treated specially.
4629         (when (setq gnus-newsgroup-seen (cdr marks))
4630           (dolist (article gnus-newsgroup-articles)
4631             (unless (gnus-member-of-range
4632                      article gnus-newsgroup-seen)
4633               (push article gnus-newsgroup-unseen)))))
4634
4635       ;; Removed marked articles that do not exist.
4636       (gnus-update-missing-marks
4637        (gnus-sorted-complement fetched-articles articles))
4638       ;; We might want to build some more threads first.
4639       (when (and gnus-fetch-old-headers
4640                  (eq gnus-headers-retrieved-by 'nov))
4641         (if (eq gnus-fetch-old-headers 'invisible)
4642             (gnus-build-all-threads)
4643           (gnus-build-old-threads)))
4644       ;; Let the Gnus agent mark articles as read.
4645       (when gnus-agent
4646         (gnus-agent-get-undownloaded-list))
4647       ;; Remove list identifiers from subject
4648       (when gnus-list-identifiers
4649         (gnus-summary-remove-list-identifiers))
4650       ;; Check whether auto-expire is to be done in this group.
4651       (setq gnus-newsgroup-auto-expire
4652             (gnus-group-auto-expirable-p group))
4653       ;; Set up the article buffer now, if necessary.
4654       (unless gnus-single-article-buffer
4655         (gnus-article-setup-buffer))
4656       ;; First and last article in this newsgroup.
4657       (when gnus-newsgroup-headers
4658         (setq gnus-newsgroup-begin
4659               (mail-header-number (car gnus-newsgroup-headers))
4660               gnus-newsgroup-end
4661               (mail-header-number
4662                (gnus-last-element gnus-newsgroup-headers))))
4663       ;; GROUP is successfully selected.
4664       (or gnus-newsgroup-headers t)))))
4665
4666 (defun gnus-summary-display-make-predicate (display)
4667   (require 'gnus-agent)
4668   (when (= (length display) 1)
4669     (setq display (car display)))
4670   (unless gnus-summary-display-cache
4671     (dolist (elem (append (list (cons 'read 'read)
4672                                 (cons 'unseen 'unseen))
4673                           gnus-article-mark-lists))
4674       (push (cons (cdr elem)
4675                   (gnus-byte-compile
4676                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4677             gnus-summary-display-cache)))
4678   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4679     (gnus-get-predicate display)))
4680
4681 ;; Uses the dynamically bound `number' variable.
4682 (defvar number)
4683 (defun gnus-article-marked-p (type &optional article)
4684   (let ((article (or article number)))
4685     (cond
4686      ((eq type 'tick)
4687       (memq article gnus-newsgroup-marked))
4688      ((eq type 'unsend)
4689       (memq article gnus-newsgroup-unsendable))
4690      ((eq type 'undownload)
4691       (memq article gnus-newsgroup-undownloaded))
4692      ((eq type 'download)
4693       (memq article gnus-newsgroup-downloadable))
4694      ((eq type 'unread)
4695       (memq article gnus-newsgroup-unreads))
4696      ((eq type 'read)
4697       (memq article gnus-newsgroup-reads))
4698      ((eq type 'dormant)
4699       (memq article gnus-newsgroup-dormant) )
4700      ((eq type 'expire)
4701       (memq article gnus-newsgroup-expirable))
4702      ((eq type 'reply)
4703       (memq article gnus-newsgroup-replied))
4704      ((eq type 'killed)
4705       (memq article gnus-newsgroup-killed))
4706      ((eq type 'bookmark)
4707       (assq article gnus-newsgroup-bookmarks))
4708      ((eq type 'score)
4709       (assq article gnus-newsgroup-scored))
4710      ((eq type 'save)
4711       (memq article gnus-newsgroup-saved))
4712      ((eq type 'cache)
4713       (memq article gnus-newsgroup-cached))
4714      ((eq type 'forward)
4715       (memq article gnus-newsgroup-forwarded))
4716      ((eq type 'seen)
4717       (not (memq article gnus-newsgroup-unseen)))
4718      ((eq type 'recent)
4719       (memq article gnus-newsgroup-recent))
4720      (t t))))
4721
4722 (defun gnus-articles-to-read (group &optional read-all)
4723   "Find out what articles the user wants to read."
4724   (let* ((articles
4725           ;; Select all articles if `read-all' is non-nil, or if there
4726           ;; are no unread articles.
4727           (if (or read-all
4728                   (and (zerop (length gnus-newsgroup-marked))
4729                        (zerop (length gnus-newsgroup-unreads)))
4730                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4731               ;; We want to select the headers for all the articles in
4732               ;; the group, so we select either all the active
4733               ;; articles in the group, or (if that's nil), the
4734               ;; articles in the cache.
4735               (or
4736                (gnus-uncompress-range (gnus-active group))
4737                (gnus-cache-articles-in-group group))
4738             ;; Select only the "normal" subset of articles.
4739             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4740                           (copy-sequence gnus-newsgroup-unreads))
4741                   '<)))
4742          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4743          (scored (length scored-list))
4744          (number (length articles))
4745          (marked (+ (length gnus-newsgroup-marked)
4746                     (length gnus-newsgroup-dormant)))
4747          (select
4748           (cond
4749            ((numberp read-all)
4750             read-all)
4751            (t
4752             (condition-case ()
4753                 (cond
4754                  ((and (or (<= scored marked) (= scored number))
4755                        (natnump gnus-large-newsgroup)
4756                        (> number gnus-large-newsgroup))
4757                   (let* ((cursor-in-echo-area nil)
4758                          (input
4759                           (read-from-minibuffer
4760                            (format
4761                             "How many articles from %s (max %d): "
4762                             (gnus-limit-string
4763                              (gnus-group-decoded-name gnus-newsgroup-name)
4764                              35)
4765                             number)
4766                            (cons (number-to-string gnus-large-newsgroup)
4767                                  0))))
4768                     (if (string-match "^[ \t]*$" input)
4769                         number
4770                       input)))
4771                  ((and (> scored marked) (< scored number)
4772                        (> (- scored number) 20))
4773                   (let ((input
4774                          (read-string
4775                           (format "%s %s (%d scored, %d total): "
4776                                   "How many articles from"
4777                                   (gnus-group-decoded-name group)
4778                                   scored number))))
4779                     (if (string-match "^[ \t]*$" input)
4780                         number input)))
4781                  (t number))
4782               (quit
4783                (message "Quit getting the articles to read")
4784                nil))))))
4785     (setq select (if (stringp select) (string-to-number select) select))
4786     (if (or (null select) (zerop select))
4787         select
4788       (if (and (not (zerop scored)) (<= (abs select) scored))
4789           (progn
4790             (setq articles (sort scored-list '<))
4791             (setq number (length articles)))
4792         (setq articles (copy-sequence articles)))
4793
4794       (when (< (abs select) number)
4795         (if (< select 0)
4796             ;; Select the N oldest articles.
4797             (setcdr (nthcdr (1- (abs select)) articles) nil)
4798           ;; Select the N most recent articles.
4799           (setq articles (nthcdr (- number select) articles))))
4800       (setq gnus-newsgroup-unselected
4801             (gnus-sorted-intersection
4802              gnus-newsgroup-unreads
4803              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4804       (when gnus-alter-articles-to-read-function
4805         (setq gnus-newsgroup-unreads
4806               (sort
4807                (funcall gnus-alter-articles-to-read-function
4808                         gnus-newsgroup-name gnus-newsgroup-unreads)
4809                '<)))
4810       articles)))
4811
4812 (defun gnus-killed-articles (killed articles)
4813   (let (out)
4814     (while articles
4815       (when (inline (gnus-member-of-range (car articles) killed))
4816         (push (car articles) out))
4817       (setq articles (cdr articles)))
4818     out))
4819
4820 (defun gnus-uncompress-marks (marks)
4821   "Uncompress the mark ranges in MARKS."
4822   (let ((uncompressed '(score bookmark))
4823         out)
4824     (while marks
4825       (if (memq (caar marks) uncompressed)
4826           (push (car marks) out)
4827         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4828       (setq marks (cdr marks)))
4829     out))
4830
4831 (defun gnus-article-mark-to-type (mark)
4832   "Return the type of MARK."
4833   (or (cadr (assq mark gnus-article-special-mark-lists))
4834       'list))
4835
4836 (defun gnus-adjust-marked-articles (info)
4837   "Set all article lists and remove all marks that are no longer valid."
4838   (let* ((marked-lists (gnus-info-marks info))
4839          (active (gnus-active (gnus-info-group info)))
4840          (min (car active))
4841          (max (cdr active))
4842          (types gnus-article-mark-lists)
4843          marks var articles article mark mark-type)
4844
4845     (dolist (marks marked-lists)
4846       (setq mark (car marks)
4847             mark-type (gnus-article-mark-to-type mark)
4848             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4849
4850       ;; We set the variable according to the type of the marks list,
4851       ;; and then adjust the marks to a subset of the active articles.
4852       (cond
4853        ;; Adjust "simple" lists.
4854        ((eq mark-type 'list)
4855         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4856         (when (memq mark '(tick dormant expire reply save))
4857           (while articles
4858             (when (or (< (setq article (pop articles)) min) (> article max))
4859               (set var (delq article (symbol-value var)))))))
4860        ;; Adjust assocs.
4861        ((eq mark-type 'tuple)
4862         (set var (setq articles (cdr marks)))
4863         (when (not (listp (cdr (symbol-value var))))
4864           (set var (list (symbol-value var))))
4865         (when (not (listp (cdr articles)))
4866           (setq articles (list articles)))
4867         (while articles
4868           (when (or (not (consp (setq article (pop articles))))
4869                     (< (car article) min)
4870                     (> (car article) max))
4871             (set var (delq article (symbol-value var))))))
4872        ((eq mark-type 'range)
4873         (cond
4874          ((eq mark 'seen))))))))
4875
4876 (defun gnus-update-missing-marks (missing)
4877   "Go through the list of MISSING articles and remove them from the mark lists."
4878   (when missing
4879     (let (var m)
4880       ;; Go through all types.
4881       (dolist (elem gnus-article-mark-lists)
4882         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4883           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4884           (when (symbol-value var)
4885             ;; This list has articles.  So we delete all missing
4886             ;; articles from it.
4887             (setq m missing)
4888             (while m
4889               (set var (delq (pop m) (symbol-value var))))))))))
4890
4891 (defun gnus-update-marks ()
4892   "Enter the various lists of marked articles into the newsgroup info list."
4893   (let ((types gnus-article-mark-lists)
4894         (info (gnus-get-info gnus-newsgroup-name))
4895         (uncompressed '(score bookmark killed seen))
4896         type list newmarked symbol delta-marks)
4897     (when info
4898       ;; Add all marks lists to the list of marks lists.
4899       (while (setq type (pop types))
4900         (setq list (symbol-value
4901                     (setq symbol
4902                           (intern (format "gnus-newsgroup-%s" (car type))))))
4903
4904         (when list
4905           ;; Get rid of the entries of the articles that have the
4906           ;; default score.
4907           (when (and (eq (cdr type) 'score)
4908                      gnus-save-score
4909                      list)
4910             (let* ((arts list)
4911                    (prev (cons nil list))
4912                    (all prev))
4913               (while arts
4914                 (if (or (not (consp (car arts)))
4915                         (= (cdar arts) gnus-summary-default-score))
4916                     (setcdr prev (cdr arts))
4917                   (setq prev arts))
4918                 (setq arts (cdr arts)))
4919               (setq list (cdr all)))))
4920
4921         (when (eq (cdr type) 'seen)
4922           (setq list
4923                 (if list
4924                     (gnus-add-to-range list gnus-newsgroup-unseen)
4925                   (gnus-compress-sequence gnus-newsgroup-articles))))
4926
4927         (unless (memq (cdr type) uncompressed)
4928           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4929
4930         (when (gnus-check-backend-function
4931                'request-set-mark gnus-newsgroup-name)
4932           ;; propagate flags to server, with the following exceptions:
4933           ;; uncompressed:s are not proper flags (they are cons cells)
4934           ;; cache is a internal gnus flag
4935           ;; download are local to one gnus installation (well)
4936           ;; unsend are for nndraft groups only
4937           ;; xxx: generality of this?  this suits nnimap anyway
4938           (unless (memq (cdr type) (append '(cache download unsend)
4939                                            uncompressed))
4940             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4941                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4942                    (add (gnus-remove-from-range
4943                          (gnus-copy-sequence list) old)))
4944               (when add
4945                 (push (list add 'add (list (cdr type))) delta-marks))
4946               (when del
4947                 (push (list del 'del (list (cdr type))) delta-marks)))))
4948
4949         (when list
4950           (push (cons (cdr type) list) newmarked)))
4951
4952       (when delta-marks
4953         (unless (gnus-check-group gnus-newsgroup-name)
4954           (error "Can't open server for %s" gnus-newsgroup-name))
4955         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4956
4957       ;; Enter these new marks into the info of the group.
4958       (if (nthcdr 3 info)
4959           (setcar (nthcdr 3 info) newmarked)
4960         ;; Add the marks lists to the end of the info.
4961         (when newmarked
4962           (setcdr (nthcdr 2 info) (list newmarked))))
4963
4964       ;; Cut off the end of the info if there's nothing else there.
4965       (let ((i 5))
4966         (while (and (> i 2)
4967                     (not (nth i info)))
4968           (when (nthcdr (decf i) info)
4969             (setcdr (nthcdr i info) nil)))))))
4970
4971 (defun gnus-set-mode-line (where)
4972   "Set the mode line of the article or summary buffers.
4973 If WHERE is `summary', the summary mode line format will be used."
4974   ;; Is this mode line one we keep updated?
4975   (when (and (memq where gnus-updated-mode-lines)
4976              (symbol-value
4977               (intern (format "gnus-%s-mode-line-format-spec" where))))
4978     (let (mode-string)
4979       (save-excursion
4980         ;; We evaluate this in the summary buffer since these
4981         ;; variables are buffer-local to that buffer.
4982         (set-buffer gnus-summary-buffer)
4983         ;; We bind all these variables that are used in the `eval' form
4984         ;; below.
4985         (let* ((mformat (symbol-value
4986                          (intern
4987                           (format "gnus-%s-mode-line-format-spec" where))))
4988                (gnus-tmp-group-name (gnus-group-decoded-name
4989                                      gnus-newsgroup-name))
4990                (gnus-tmp-article-number (or gnus-current-article 0))
4991                (gnus-tmp-unread gnus-newsgroup-unreads)
4992                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4993                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4994                (gnus-tmp-unread-and-unselected
4995                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4996                             (zerop gnus-tmp-unselected))
4997                        "")
4998                       ((zerop gnus-tmp-unselected)
4999                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5000                       (t (format "{%d(+%d) more}"
5001                                  gnus-tmp-unread-and-unticked
5002                                  gnus-tmp-unselected))))
5003                (gnus-tmp-subject
5004                 (if (and gnus-current-headers
5005                          (vectorp gnus-current-headers))
5006                     (gnus-mode-string-quote
5007                      (mail-header-subject gnus-current-headers))
5008                   ""))
5009                bufname-length max-len
5010                gnus-tmp-header);; passed as argument to any user-format-funcs
5011           (setq mode-string (eval mformat))
5012           (setq bufname-length (if (string-match "%b" mode-string)
5013                                    (- (length
5014                                        (buffer-name
5015                                         (if (eq where 'summary)
5016                                             nil
5017                                           (get-buffer gnus-article-buffer))))
5018                                       2)
5019                                  0))
5020           (setq max-len (max 4 (if gnus-mode-non-string-length
5021                                    (- (window-width)
5022                                       gnus-mode-non-string-length
5023                                       bufname-length)
5024                                  (length mode-string))))
5025           ;; We might have to chop a bit of the string off...
5026           (when (> (length mode-string) max-len)
5027             (setq mode-string
5028                   (concat (gnus-truncate-string mode-string (- max-len 3))
5029                           "...")))
5030           ;; Pad the mode string a bit.
5031           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5032       ;; Update the mode line.
5033       (setq mode-line-buffer-identification
5034             (gnus-mode-line-buffer-identification (list mode-string)))
5035       (set-buffer-modified-p t))))
5036
5037 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5038   "Go through the HEADERS list and add all Xrefs to a hash table.
5039 The resulting hash table is returned, or nil if no Xrefs were found."
5040   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5041          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5042          (xref-hashtb (gnus-make-hashtable))
5043          start group entry number xrefs header)
5044     (while headers
5045       (setq header (pop headers))
5046       (when (and (setq xrefs (mail-header-xref header))
5047                  (not (memq (setq number (mail-header-number header))
5048                             unreads)))
5049         (setq start 0)
5050         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5051           (setq start (match-end 0))
5052           (setq group (if prefix
5053                           (concat prefix (substring xrefs (match-beginning 1)
5054                                                     (match-end 1)))
5055                         (substring xrefs (match-beginning 1) (match-end 1))))
5056           (setq number
5057                 (string-to-int (substring xrefs (match-beginning 2)
5058                                           (match-end 2))))
5059           (if (setq entry (gnus-gethash group xref-hashtb))
5060               (setcdr entry (cons number (cdr entry)))
5061             (gnus-sethash group (cons number nil) xref-hashtb)))))
5062     (and start xref-hashtb)))
5063
5064 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5065   "Look through all the headers and mark the Xrefs as read."
5066   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5067         name entry info xref-hashtb idlist method nth4)
5068     (save-excursion
5069       (set-buffer gnus-group-buffer)
5070       (when (setq xref-hashtb
5071                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5072         (mapatoms
5073          (lambda (group)
5074            (unless (string= from-newsgroup (setq name (symbol-name group)))
5075              (setq idlist (symbol-value group))
5076              ;; Dead groups are not updated.
5077              (and (prog1
5078                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5079                             info (nth 2 entry))
5080                     (when (stringp (setq nth4 (gnus-info-method info)))
5081                       (setq nth4 (gnus-server-to-method nth4))))
5082                   ;; Only do the xrefs if the group has the same
5083                   ;; select method as the group we have just read.
5084                   (or (gnus-methods-equal-p
5085                        nth4 (gnus-find-method-for-group from-newsgroup))
5086                       virtual
5087                       (equal nth4 (setq method (gnus-find-method-for-group
5088                                                 from-newsgroup)))
5089                       (and (equal (car nth4) (car method))
5090                            (equal (nth 1 nth4) (nth 1 method))))
5091                   gnus-use-cross-reference
5092                   (or (not (eq gnus-use-cross-reference t))
5093                       virtual
5094                       ;; Only do cross-references on subscribed
5095                       ;; groups, if that is what is wanted.
5096                       (<= (gnus-info-level info) gnus-level-subscribed))
5097                   (gnus-group-make-articles-read name idlist))))
5098          xref-hashtb)))))
5099
5100 (defun gnus-compute-read-articles (group articles)
5101   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5102          (info (nth 2 entry))
5103          (active (gnus-active group))
5104          ninfo)
5105     (when entry
5106       ;; First peel off all invalid article numbers.
5107       (when active
5108         (let ((ids articles)
5109               id first)
5110           (while (setq id (pop ids))
5111             (when (and first (> id (cdr active)))
5112               ;; We'll end up in this situation in one particular
5113               ;; obscure situation.  If you re-scan a group and get
5114               ;; a new article that is cross-posted to a different
5115               ;; group that has not been re-scanned, you might get
5116               ;; crossposted article that has a higher number than
5117               ;; Gnus believes possible.  So we re-activate this
5118               ;; group as well.  This might mean doing the
5119               ;; crossposting thingy will *increase* the number
5120               ;; of articles in some groups.  Tsk, tsk.
5121               (setq active (or (gnus-activate-group group) active)))
5122             (when (or (> id (cdr active))
5123                       (< id (car active)))
5124               (setq articles (delq id articles))))))
5125       ;; If the read list is nil, we init it.
5126       (if (and active
5127                (null (gnus-info-read info))
5128                (> (car active) 1))
5129           (setq ninfo (cons 1 (1- (car active))))
5130         (setq ninfo (gnus-info-read info)))
5131       ;; Then we add the read articles to the range.
5132       (gnus-add-to-range
5133        ninfo (setq articles (sort articles '<))))))
5134
5135 (defun gnus-group-make-articles-read (group articles)
5136   "Update the info of GROUP to say that ARTICLES are read."
5137   (let* ((num 0)
5138          (entry (gnus-gethash group gnus-newsrc-hashtb))
5139          (info (nth 2 entry))
5140          (active (gnus-active group))
5141          range)
5142     (when entry
5143       (setq range (gnus-compute-read-articles group articles))
5144       (save-excursion
5145         (set-buffer gnus-group-buffer)
5146         (gnus-undo-register
5147           `(progn
5148              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5149              (gnus-info-set-read ',info ',(gnus-info-read info))
5150              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5151              (gnus-group-update-group ,group t))))
5152       ;; Add the read articles to the range.
5153       (gnus-info-set-read info range)
5154       (gnus-request-set-mark group (list (list range 'add '(read))))
5155       ;; Then we have to re-compute how many unread
5156       ;; articles there are in this group.
5157       (when active
5158         (cond
5159          ((not range)
5160           (setq num (- (1+ (cdr active)) (car active))))
5161          ((not (listp (cdr range)))
5162           (setq num (- (cdr active) (- (1+ (cdr range))
5163                                        (car range)))))
5164          (t
5165           (while range
5166             (if (numberp (car range))
5167                 (setq num (1+ num))
5168               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5169             (setq range (cdr range)))
5170           (setq num (- (cdr active) num))))
5171         ;; Update the number of unread articles.
5172         (setcar entry num)
5173         ;; Update the group buffer.
5174         (gnus-group-update-group group t)))))
5175
5176 (defvar gnus-newsgroup-none-id 0)
5177
5178 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5179   (let ((cur nntp-server-buffer)
5180         (dependencies
5181          (or dependencies
5182              (save-excursion (set-buffer gnus-summary-buffer)
5183                              gnus-newsgroup-dependencies)))
5184         headers id end ref
5185         (mail-parse-charset gnus-newsgroup-charset)
5186         (mail-parse-ignored-charsets
5187          (save-excursion (condition-case nil
5188                              (set-buffer gnus-summary-buffer)
5189                            (error))
5190                          gnus-newsgroup-ignored-charsets)))
5191     (save-excursion
5192       (set-buffer nntp-server-buffer)
5193       ;; Translate all TAB characters into SPACE characters.
5194       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5195       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5196       (gnus-run-hooks 'gnus-parse-headers-hook)
5197       (let ((case-fold-search t)
5198             in-reply-to header p lines chars ctype)
5199         (goto-char (point-min))
5200         ;; Search to the beginning of the next header.  Error messages
5201         ;; do not begin with 2 or 3.
5202         (while (re-search-forward "^[23][0-9]+ " nil t)
5203           (setq id nil
5204                 ref nil)
5205           ;; This implementation of this function, with nine
5206           ;; search-forwards instead of the one re-search-forward and
5207           ;; a case (which basically was the old function) is actually
5208           ;; about twice as fast, even though it looks messier.  You
5209           ;; can't have everything, I guess.  Speed and elegance
5210           ;; doesn't always go hand in hand.
5211           (setq
5212            header
5213            (make-full-mail-header
5214             ;; Number.
5215             (prog1
5216                 (read cur)
5217               (end-of-line)
5218               (setq p (point))
5219               (narrow-to-region (point)
5220                                 (or (and (search-forward "\n.\n" nil t)
5221                                          (- (point) 2))
5222                                     (point))))
5223             ;; Subject.
5224             (progn
5225               (goto-char p)
5226               (if (search-forward "\nsubject:" nil t)
5227                   (nnheader-header-value)
5228                 "(none)"))
5229             ;; From.
5230             (progn
5231               (goto-char p)
5232               (if (search-forward "\nfrom:" nil t)
5233                   (nnheader-header-value)
5234                 "(nobody)"))
5235             ;; Date.
5236             (progn
5237               (goto-char p)
5238               (if (search-forward "\ndate:" nil t)
5239                   (nnheader-header-value) ""))
5240             ;; Message-ID.
5241             (progn
5242               (goto-char p)
5243               (setq id (if (re-search-forward
5244                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5245                            ;; We do it this way to make sure the Message-ID
5246                            ;; is (somewhat) syntactically valid.
5247                            (buffer-substring (match-beginning 1)
5248                                              (match-end 1))
5249                          ;; If there was no message-id, we just fake one
5250                          ;; to make subsequent routines simpler.
5251                          (nnheader-generate-fake-message-id))))
5252             ;; References.
5253             (progn
5254               (goto-char p)
5255               (if (search-forward "\nreferences:" nil t)
5256                   (progn
5257                     (setq end (point))
5258                     (prog1
5259                         (nnheader-header-value)
5260                       (setq ref
5261                             (buffer-substring
5262                              (progn
5263                                ;; (end-of-line)
5264                                (search-backward ">" end t)
5265                                (1+ (point)))
5266                              (progn
5267                                (search-backward "<" end t)
5268                                (point))))))
5269                 ;; Get the references from the in-reply-to header if there
5270                 ;; were no references and the in-reply-to header looks
5271                 ;; promising.
5272                 (if (and (search-forward "\nin-reply-to:" nil t)
5273                          (setq in-reply-to (nnheader-header-value))
5274                          (string-match "<[^>]+>" in-reply-to))
5275                     (let (ref2)
5276                       (setq ref (substring in-reply-to (match-beginning 0)
5277                                            (match-end 0)))
5278                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5279                         (setq ref2 (substring in-reply-to (match-beginning 0)
5280                                               (match-end 0)))
5281                         (when (> (length ref2) (length ref))
5282                           (setq ref ref2)))
5283                       ref)
5284                   (setq ref nil))))
5285             ;; Chars.
5286             (progn
5287               (goto-char p)
5288               (if (search-forward "\nchars: " nil t)
5289                   (if (numberp (setq chars (ignore-errors (read cur))))
5290                       chars -1)
5291                 -1))
5292             ;; Lines.
5293             (progn
5294               (goto-char p)
5295               (if (search-forward "\nlines: " nil t)
5296                   (if (numberp (setq lines (ignore-errors (read cur))))
5297                       lines -1)
5298                 -1))
5299             ;; Xref.
5300             (progn
5301               (goto-char p)
5302               (and (search-forward "\nxref:" nil t)
5303                    (nnheader-header-value)))
5304             ;; Extra.
5305             (when gnus-extra-headers
5306               (let ((extra gnus-extra-headers)
5307                     out)
5308                 (while extra
5309                   (goto-char p)
5310                   (when (search-forward
5311                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5312                     (push (cons (car extra) (nnheader-header-value)) out))
5313                   (pop extra))
5314                 out))))
5315           (goto-char p)
5316           (if (and (search-forward "\ncontent-type: " nil t)
5317                    (setq ctype (nnheader-header-value)))
5318               (mime-entity-set-content-type-internal
5319                header (mime-parse-Content-Type ctype)))
5320           (when (equal id ref)
5321             (setq ref nil))
5322
5323           (when gnus-alter-header-function
5324             (funcall gnus-alter-header-function header)
5325             (setq id (mail-header-id header)
5326                   ref (gnus-parent-id (mail-header-references header))))
5327
5328           (when (setq header
5329                       (gnus-dependencies-add-header
5330                        header dependencies force-new))
5331             (push header headers))
5332           (goto-char (point-max))
5333           (widen))
5334         (nreverse headers)))))
5335
5336 ;; Goes through the xover lines and returns a list of vectors
5337 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5338                                                   force-new dependencies
5339                                                   group also-fetch-heads)
5340   "Parse the news overview data in the server buffer.
5341 Return a list of headers that match SEQUENCE (see
5342 `nntp-retrieve-headers')."
5343   ;; Get the Xref when the users reads the articles since most/some
5344   ;; NNTP servers do not include Xrefs when using XOVER.
5345   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5346   (let ((mail-parse-charset gnus-newsgroup-charset)
5347         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5348         (cur nntp-server-buffer)
5349         (dependencies (or dependencies gnus-newsgroup-dependencies))
5350         (allp (cond
5351                ((eq gnus-read-all-available-headers t)
5352                 t)
5353                ((stringp gnus-read-all-available-headers)
5354                 (string-match gnus-read-all-available-headers group))
5355                (t
5356                 nil)))
5357         number headers header)
5358     (save-excursion
5359       (set-buffer nntp-server-buffer)
5360       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5361       ;; Allow the user to mangle the headers before parsing them.
5362       (gnus-run-hooks 'gnus-parse-headers-hook)
5363       (goto-char (point-min))
5364       (while (not (eobp))
5365         (condition-case ()
5366             (while (and (or sequence allp)
5367                         (not (eobp)))
5368               (setq number (read cur))
5369               (when (not allp)
5370                 (while (and sequence
5371                             (< (car sequence) number))
5372                   (setq sequence (cdr sequence))))
5373               (when (and (or allp
5374                              (and sequence
5375                                   (eq number (car sequence))))
5376                          (progn
5377                            (setq sequence (cdr sequence))
5378                            (setq header (inline
5379                                           (gnus-nov-parse-line
5380                                            number dependencies force-new)))))
5381                 (push header headers))
5382               (forward-line 1))
5383           (error
5384            (gnus-error 4 "Strange nov line (%d)"
5385                        (count-lines (point-min) (point)))))
5386         (forward-line 1))
5387       ;; A common bug in inn is that if you have posted an article and
5388       ;; then retrieves the active file, it will answer correctly --
5389       ;; the new article is included.  However, a NOV entry for the
5390       ;; article may not have been generated yet, so this may fail.
5391       ;; We work around this problem by retrieving the last few
5392       ;; headers using HEAD.
5393       (if (or (not also-fetch-heads)
5394               (not sequence))
5395           ;; We (probably) got all the headers.
5396           (nreverse headers)
5397         (let ((gnus-nov-is-evil t))
5398           (nconc
5399            (nreverse headers)
5400            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5401              (gnus-get-newsgroup-headers))))))))
5402
5403 (defun gnus-article-get-xrefs ()
5404   "Fill in the Xref value in `gnus-current-headers', if necessary.
5405 This is meant to be called in `gnus-article-internal-prepare-hook'."
5406   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5407                                  gnus-current-headers)))
5408     (or (not gnus-use-cross-reference)
5409         (not headers)
5410         (and (mail-header-xref headers)
5411              (not (string= (mail-header-xref headers) "")))
5412         (let ((case-fold-search t)
5413               xref)
5414           (save-restriction
5415             (nnheader-narrow-to-headers)
5416             (goto-char (point-min))
5417             (when (or (and (not (eobp))
5418                            (eq (downcase (char-after)) ?x)
5419                            (looking-at "Xref:"))
5420                       (search-forward "\nXref:" nil t))
5421               (goto-char (1+ (match-end 0)))
5422               (setq xref (buffer-substring (point)
5423                                            (progn (end-of-line) (point))))
5424               (mail-header-set-xref headers xref)))))))
5425
5426 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5427   "Find article ID and insert the summary line for that article.
5428 OLD-HEADER can either be a header or a line number to insert
5429 the subject line on."
5430   (let* ((line (and (numberp old-header) old-header))
5431          (old-header (and (vectorp old-header) old-header))
5432          (header (cond ((and old-header use-old-header)
5433                         old-header)
5434                        ((and (numberp id)
5435                              (gnus-number-to-header id))
5436                         (gnus-number-to-header id))
5437                        (t
5438                         (gnus-read-header id))))
5439          (number (and (numberp id) id))
5440          d)
5441     (when header
5442       ;; Rebuild the thread that this article is part of and go to the
5443       ;; article we have fetched.
5444       (when (and (not gnus-show-threads)
5445                  old-header)
5446         (when (and number
5447                    (setq d (gnus-data-find (mail-header-number old-header))))
5448           (goto-char (gnus-data-pos d))
5449           (gnus-data-remove
5450            number
5451            (- (gnus-point-at-bol)
5452               (prog1
5453                   (1+ (gnus-point-at-eol))
5454                 (gnus-delete-line))))))
5455       (when old-header
5456         (mail-header-set-number header (mail-header-number old-header)))
5457       (setq gnus-newsgroup-sparse
5458             (delq (setq number (mail-header-number header))
5459                   gnus-newsgroup-sparse))
5460       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5461       (push number gnus-newsgroup-limit)
5462       (gnus-rebuild-thread (mail-header-id header) line)
5463       (gnus-summary-goto-subject number nil t))
5464     (when (and (numberp number)
5465                (> number 0))
5466       ;; We have to update the boundaries even if we can't fetch the
5467       ;; article if ID is a number -- so that the next `P' or `N'
5468       ;; command will fetch the previous (or next) article even
5469       ;; if the one we tried to fetch this time has been canceled.
5470       (when (> number gnus-newsgroup-end)
5471         (setq gnus-newsgroup-end number))
5472       (when (< number gnus-newsgroup-begin)
5473         (setq gnus-newsgroup-begin number))
5474       (setq gnus-newsgroup-unselected
5475             (delq number gnus-newsgroup-unselected)))
5476     ;; Report back a success?
5477     (and header (mail-header-number header))))
5478
5479 ;;; Process/prefix in the summary buffer
5480
5481 (defun gnus-summary-work-articles (n)
5482   "Return a list of articles to be worked upon.
5483 The prefix argument, the list of process marked articles, and the
5484 current article will be taken into consideration."
5485   (save-excursion
5486     (set-buffer gnus-summary-buffer)
5487     (cond
5488      (n
5489       ;; A numerical prefix has been given.
5490       (setq n (prefix-numeric-value n))
5491       (let ((backward (< n 0))
5492             (n (abs (prefix-numeric-value n)))
5493             articles article)
5494         (save-excursion
5495           (while
5496               (and (> n 0)
5497                    (push (setq article (gnus-summary-article-number))
5498                          articles)
5499                    (if backward
5500                        (gnus-summary-find-prev nil article)
5501                      (gnus-summary-find-next nil article)))
5502             (decf n)))
5503         (nreverse articles)))
5504      ((and (gnus-region-active-p) (mark))
5505       (message "region active")
5506       ;; Work on the region between point and mark.
5507       (let ((max (max (point) (mark)))
5508             articles article)
5509         (save-excursion
5510           (goto-char (min (point) (mark)))
5511           (while
5512               (and
5513                (push (setq article (gnus-summary-article-number)) articles)
5514                (gnus-summary-find-next nil article)
5515                (< (point) max)))
5516           (nreverse articles))))
5517      (gnus-newsgroup-processable
5518       ;; There are process-marked articles present.
5519       ;; Save current state.
5520       (gnus-summary-save-process-mark)
5521       ;; Return the list.
5522       (reverse gnus-newsgroup-processable))
5523      (t
5524       ;; Just return the current article.
5525       (list (gnus-summary-article-number))))))
5526
5527 (defmacro gnus-summary-iterate (arg &rest forms)
5528   "Iterate over the process/prefixed articles and do FORMS.
5529 ARG is the interactive prefix given to the command.  FORMS will be
5530 executed with point over the summary line of the articles."
5531   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5532     `(let ((,articles (gnus-summary-work-articles ,arg)))
5533        (while ,articles
5534          (gnus-summary-goto-subject (car ,articles))
5535          ,@forms
5536          (pop ,articles)))))
5537
5538 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5539 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5540
5541 (defun gnus-summary-save-process-mark ()
5542   "Push the current set of process marked articles on the stack."
5543   (interactive)
5544   (push (copy-sequence gnus-newsgroup-processable)
5545         gnus-newsgroup-process-stack))
5546
5547 (defun gnus-summary-kill-process-mark ()
5548   "Push the current set of process marked articles on the stack and unmark."
5549   (interactive)
5550   (gnus-summary-save-process-mark)
5551   (gnus-summary-unmark-all-processable))
5552
5553 (defun gnus-summary-yank-process-mark ()
5554   "Pop the last process mark state off the stack and restore it."
5555   (interactive)
5556   (unless gnus-newsgroup-process-stack
5557     (error "Empty mark stack"))
5558   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5559
5560 (defun gnus-summary-process-mark-set (set)
5561   "Make SET into the current process marked articles."
5562   (gnus-summary-unmark-all-processable)
5563   (while set
5564     (gnus-summary-set-process-mark (pop set))))
5565
5566 ;;; Searching and stuff
5567
5568 (defun gnus-summary-search-group (&optional backward use-level)
5569   "Search for next unread newsgroup.
5570 If optional argument BACKWARD is non-nil, search backward instead."
5571   (save-excursion
5572     (set-buffer gnus-group-buffer)
5573     (when (gnus-group-search-forward
5574            backward nil (if use-level (gnus-group-group-level) nil))
5575       (gnus-group-group-name))))
5576
5577 (defun gnus-summary-best-group (&optional exclude-group)
5578   "Find the name of the best unread group.
5579 If EXCLUDE-GROUP, do not go to this group."
5580   (save-excursion
5581     (set-buffer gnus-group-buffer)
5582     (save-excursion
5583       (gnus-group-best-unread-group exclude-group))))
5584
5585 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5586   (if backward (gnus-summary-find-prev)
5587     (let* ((dummy (gnus-summary-article-intangible-p))
5588            (article (or article (gnus-summary-article-number)))
5589            (arts (gnus-data-find-list article))
5590            result)
5591       (when (and (not dummy)
5592                  (or (not gnus-summary-check-current)
5593                      (not unread)
5594                      (not (gnus-data-unread-p (car arts)))))
5595         (setq arts (cdr arts)))
5596       (when (setq result
5597                   (if unread
5598                       (progn
5599                         (while arts
5600                           (when (or (and undownloaded
5601                                          (eq gnus-undownloaded-mark
5602                                              (gnus-data-mark (car arts))))
5603                                     (gnus-data-unread-p (car arts)))
5604                             (setq result (car arts)
5605                                   arts nil))
5606                           (setq arts (cdr arts)))
5607                         result)
5608                     (car arts)))
5609         (goto-char (gnus-data-pos result))
5610         (gnus-data-number result)))))
5611
5612 (defun gnus-summary-find-prev (&optional unread article)
5613   (let* ((eobp (eobp))
5614          (article (or article (gnus-summary-article-number)))
5615          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5616          result)
5617     (when (and (not eobp)
5618                (or (not gnus-summary-check-current)
5619                    (not unread)
5620                    (not (gnus-data-unread-p (car arts)))))
5621       (setq arts (cdr arts)))
5622     (when (setq result
5623                 (if unread
5624                     (progn
5625                       (while arts
5626                         (when (gnus-data-unread-p (car arts))
5627                           (setq result (car arts)
5628                                 arts nil))
5629                         (setq arts (cdr arts)))
5630                       result)
5631                   (car arts)))
5632       (goto-char (gnus-data-pos result))
5633       (gnus-data-number result))))
5634
5635 (defun gnus-summary-find-subject (subject &optional unread backward article)
5636   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5637          (article (or article (gnus-summary-article-number)))
5638          (articles (gnus-data-list backward))
5639          (arts (gnus-data-find-list article articles))
5640          result)
5641     (when (or (not gnus-summary-check-current)
5642               (not unread)
5643               (not (gnus-data-unread-p (car arts))))
5644       (setq arts (cdr arts)))
5645     (while arts
5646       (and (or (not unread)
5647                (gnus-data-unread-p (car arts)))
5648            (vectorp (gnus-data-header (car arts)))
5649            (gnus-subject-equal
5650             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5651            (setq result (car arts)
5652                  arts nil))
5653       (setq arts (cdr arts)))
5654     (and result
5655          (goto-char (gnus-data-pos result))
5656          (gnus-data-number result))))
5657
5658 (defun gnus-summary-search-forward (&optional unread subject backward)
5659   "Search forward for an article.
5660 If UNREAD, look for unread articles.  If SUBJECT, look for
5661 articles with that subject.  If BACKWARD, search backward instead."
5662   (cond (subject (gnus-summary-find-subject subject unread backward))
5663         (backward (gnus-summary-find-prev unread))
5664         (t (gnus-summary-find-next unread))))
5665
5666 (defun gnus-recenter (&optional n)
5667   "Center point in window and redisplay frame.
5668 Also do horizontal recentering."
5669   (interactive "P")
5670   (when (and gnus-auto-center-summary
5671              (not (eq gnus-auto-center-summary 'vertical)))
5672     (gnus-horizontal-recenter))
5673   (recenter n))
5674
5675 (defun gnus-summary-recenter ()
5676   "Center point in the summary window.
5677 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5678 displayed, no centering will be performed."
5679   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5680   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5681   (interactive)
5682   (let* ((top (cond ((< (window-height) 4) 0)
5683                     ((< (window-height) 7) 1)
5684                     (t (if (numberp gnus-auto-center-summary)
5685                            gnus-auto-center-summary
5686                          2))))
5687          (height (1- (window-height)))
5688          (bottom (save-excursion (goto-char (point-max))
5689                                  (forward-line (- height))
5690                                  (point)))
5691          (window (get-buffer-window (current-buffer))))
5692     ;; The user has to want it.
5693     (when gnus-auto-center-summary
5694       (when (get-buffer-window gnus-article-buffer)
5695         ;; Only do recentering when the article buffer is displayed,
5696         ;; Set the window start to either `bottom', which is the biggest
5697         ;; possible valid number, or the second line from the top,
5698         ;; whichever is the least.
5699         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5700           (if (> bottom top-pos)
5701               ;; Keep the second line from the top visible
5702               (set-window-start window top-pos t)
5703             ;; Try to keep the bottom line visible; if it's partially
5704             ;; obscured, either scroll one more line to make it fully
5705             ;; visible, or revert to using TOP-POS.
5706             (save-excursion
5707               (goto-char (point-max))
5708               (forward-line -1)
5709               (let ((last-line-start (point)))
5710                 (goto-char bottom)
5711                 (set-window-start window (point) t)
5712                 (when (not (pos-visible-in-window-p last-line-start window))
5713                   (forward-line 1)
5714                   (set-window-start window (min (point) top-pos) t)))))))
5715       ;; Do horizontal recentering while we're at it.
5716       (when (and (get-buffer-window (current-buffer) t)
5717                  (not (eq gnus-auto-center-summary 'vertical)))
5718         (let ((selected (selected-window)))
5719           (select-window (get-buffer-window (current-buffer) t))
5720           (gnus-summary-position-point)
5721           (gnus-horizontal-recenter)
5722           (select-window selected))))))
5723
5724 (defun gnus-summary-jump-to-group (newsgroup)
5725   "Move point to NEWSGROUP in group mode buffer."
5726   ;; Keep update point of group mode buffer if visible.
5727   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5728       (save-window-excursion
5729         ;; Take care of tree window mode.
5730         (when (get-buffer-window gnus-group-buffer)
5731           (pop-to-buffer gnus-group-buffer))
5732         (gnus-group-jump-to-group newsgroup))
5733     (save-excursion
5734       ;; Take care of tree window mode.
5735       (if (get-buffer-window gnus-group-buffer)
5736           (pop-to-buffer gnus-group-buffer)
5737         (set-buffer gnus-group-buffer))
5738       (gnus-group-jump-to-group newsgroup))))
5739
5740 ;; This function returns a list of article numbers based on the
5741 ;; difference between the ranges of read articles in this group and
5742 ;; the range of active articles.
5743 (defun gnus-list-of-unread-articles (group)
5744   (let* ((read (gnus-info-read (gnus-get-info group)))
5745          (active (or (gnus-active group) (gnus-activate-group group)))
5746          (last (cdr active))
5747          first nlast unread)
5748     ;; If none are read, then all are unread.
5749     (if (not read)
5750         (setq first (car active))
5751       ;; If the range of read articles is a single range, then the
5752       ;; first unread article is the article after the last read
5753       ;; article.  Sounds logical, doesn't it?
5754       (if (and (not (listp (cdr read)))
5755                (or (< (car read) (car active))
5756                    (progn (setq read (list read))
5757                           nil)))
5758           (setq first (max (car active) (1+ (cdr read))))
5759         ;; `read' is a list of ranges.
5760         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5761                                   (caar read)))
5762                   1)
5763           (setq first (car active)))
5764         (while read
5765           (when first
5766             (while (< first nlast)
5767               (push first unread)
5768               (setq first (1+ first))))
5769           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5770           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5771           (setq read (cdr read)))))
5772     ;; And add the last unread articles.
5773     (while (<= first last)
5774       (push first unread)
5775       (setq first (1+ first)))
5776     ;; Return the list of unread articles.
5777     (delq 0 (nreverse unread))))
5778
5779 (defun gnus-list-of-read-articles (group)
5780   "Return a list of unread, unticked and non-dormant articles."
5781   (let* ((info (gnus-get-info group))
5782          (marked (gnus-info-marks info))
5783          (active (gnus-active group)))
5784     (and info active
5785          (gnus-set-difference
5786           (gnus-sorted-complement
5787            (gnus-uncompress-range active)
5788            (gnus-list-of-unread-articles group))
5789           (append
5790            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5791            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5792
5793 ;; Various summary commands
5794
5795 (defun gnus-summary-select-article-buffer ()
5796   "Reconfigure windows to show article buffer."
5797   (interactive)
5798   (if (not (gnus-buffer-live-p gnus-article-buffer))
5799       (error "There is no article buffer for this summary buffer")
5800     (gnus-configure-windows 'article)
5801     (select-window (get-buffer-window gnus-article-buffer))))
5802
5803 (defun gnus-summary-universal-argument (arg)
5804   "Perform any operation on all articles that are process/prefixed."
5805   (interactive "P")
5806   (let ((articles (gnus-summary-work-articles arg))
5807         func article)
5808     (if (eq
5809          (setq
5810           func
5811           (key-binding
5812            (read-key-sequence
5813             (substitute-command-keys
5814              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5815          'undefined)
5816         (gnus-error 1 "Undefined key")
5817       (save-excursion
5818         (while articles
5819           (gnus-summary-goto-subject (setq article (pop articles)))
5820           (let (gnus-newsgroup-processable)
5821             (command-execute func))
5822           (gnus-summary-remove-process-mark article)))))
5823   (gnus-summary-position-point))
5824
5825 (defun gnus-summary-toggle-truncation (&optional arg)
5826   "Toggle truncation of summary lines.
5827 With arg, turn line truncation on iff arg is positive."
5828   (interactive "P")
5829   (setq truncate-lines
5830         (if (null arg) (not truncate-lines)
5831           (> (prefix-numeric-value arg) 0)))
5832   (redraw-display))
5833
5834 (defun gnus-summary-reselect-current-group (&optional all rescan)
5835   "Exit and then reselect the current newsgroup.
5836 The prefix argument ALL means to select all articles."
5837   (interactive "P")
5838   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5839     (error "Ephemeral groups can't be reselected"))
5840   (let ((current-subject (gnus-summary-article-number))
5841         (group gnus-newsgroup-name))
5842     (setq gnus-newsgroup-begin nil)
5843     (gnus-summary-exit)
5844     ;; We have to adjust the point of group mode buffer because
5845     ;; point was moved to the next unread newsgroup by exiting.
5846     (gnus-summary-jump-to-group group)
5847     (when rescan
5848       (save-excursion
5849         (save-window-excursion
5850           ;; Don't show group contents.
5851           (set-window-start (selected-window) (point-max))
5852           (gnus-group-get-new-news-this-group 1))))
5853     (gnus-group-read-group all t)
5854     (gnus-summary-goto-subject current-subject nil t)))
5855
5856 (defun gnus-summary-rescan-group (&optional all)
5857   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5858   (interactive "P")
5859   (gnus-summary-reselect-current-group all t))
5860
5861 (defun gnus-summary-update-info (&optional non-destructive)
5862   (save-excursion
5863     (let ((group gnus-newsgroup-name))
5864       (when group
5865         (when gnus-newsgroup-kill-headers
5866           (setq gnus-newsgroup-killed
5867                 (gnus-compress-sequence
5868                  (nconc
5869                   (gnus-set-sorted-intersection
5870                    (gnus-uncompress-range gnus-newsgroup-killed)
5871                    (setq gnus-newsgroup-unselected
5872                          (sort gnus-newsgroup-unselected '<)))
5873                   (setq gnus-newsgroup-unreads
5874                         (sort gnus-newsgroup-unreads '<)))
5875                  t)))
5876         (unless (listp (cdr gnus-newsgroup-killed))
5877           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5878         (let ((headers gnus-newsgroup-headers))
5879           ;; Set the new ranges of read articles.
5880           (save-excursion
5881             (set-buffer gnus-group-buffer)
5882             (gnus-undo-force-boundary))
5883           (gnus-update-read-articles
5884            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5885           ;; Set the current article marks.
5886           (let ((gnus-newsgroup-scored
5887                  (if (and (not gnus-save-score)
5888                           (not non-destructive))
5889                      nil
5890                    gnus-newsgroup-scored)))
5891             (save-excursion
5892               (gnus-update-marks)))
5893           ;; Do the cross-ref thing.
5894           (when gnus-use-cross-reference
5895             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5896           ;; Do not switch windows but change the buffer to work.
5897           (set-buffer gnus-group-buffer)
5898           (unless (gnus-ephemeral-group-p group)
5899             (gnus-group-update-group group)))))))
5900
5901 (defun gnus-summary-save-newsrc (&optional force)
5902   "Save the current number of read/marked articles in the dribble buffer.
5903 The dribble buffer will then be saved.
5904 If FORCE (the prefix), also save the .newsrc file(s)."
5905   (interactive "P")
5906   (gnus-summary-update-info t)
5907   (if force
5908       (gnus-save-newsrc-file)
5909     (gnus-dribble-save)))
5910
5911 (defun gnus-summary-exit (&optional temporary)
5912   "Exit reading current newsgroup, and then return to group selection mode.
5913 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5914   (interactive)
5915   (gnus-set-global-variables)
5916   (gnus-kill-save-kill-buffer)
5917   (gnus-async-halt-prefetch)
5918   (let* ((group gnus-newsgroup-name)
5919          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5920          (mode major-mode)
5921          (group-point nil)
5922          (buf (current-buffer)))
5923     (unless quit-config
5924       ;; Do adaptive scoring, and possibly save score files.
5925       (when gnus-newsgroup-adaptive
5926         (gnus-score-adaptive))
5927       (when gnus-use-scoring
5928         (gnus-score-save)))
5929     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5930     ;; If we have several article buffers, we kill them at exit.
5931     (unless gnus-single-article-buffer
5932       (gnus-kill-buffer gnus-original-article-buffer)
5933       (setq gnus-article-current nil))
5934     (when gnus-use-cache
5935       (gnus-cache-possibly-remove-articles)
5936       (gnus-cache-save-buffers))
5937     (gnus-async-prefetch-remove-group group)
5938     (when gnus-suppress-duplicates
5939       (gnus-dup-enter-articles))
5940     (when gnus-use-trees
5941       (gnus-tree-close group))
5942     (when gnus-use-cache
5943       (gnus-cache-write-active))
5944     ;; Remove entries for this group.
5945     (nnmail-purge-split-history (gnus-group-real-name group))
5946     ;; Make all changes in this group permanent.
5947     (unless quit-config
5948       (gnus-run-hooks 'gnus-exit-group-hook)
5949       (gnus-summary-update-info))
5950     (gnus-close-group group)
5951     ;; Make sure where we were, and go to next newsgroup.
5952     (set-buffer gnus-group-buffer)
5953     (unless quit-config
5954       (gnus-group-jump-to-group group))
5955     (gnus-run-hooks 'gnus-summary-exit-hook)
5956     (unless (or quit-config
5957                 ;; If this group has disappeared from the summary
5958                 ;; buffer, don't skip forwards.
5959                 (not (string= group (gnus-group-group-name))))
5960       (gnus-group-next-unread-group 1))
5961     (setq group-point (point))
5962     (if temporary
5963         nil                             ;Nothing to do.
5964       ;; If we have several article buffers, we kill them at exit.
5965       (unless gnus-single-article-buffer
5966         (gnus-kill-buffer gnus-article-buffer)
5967         (gnus-kill-buffer gnus-original-article-buffer)
5968         (setq gnus-article-current nil))
5969       (set-buffer buf)
5970       (if (not gnus-kill-summary-on-exit)
5971           (progn
5972             (gnus-deaden-summary)
5973             (setq mode nil))
5974         ;; We set all buffer-local variables to nil.  It is unclear why
5975         ;; this is needed, but if we don't, buffer-local variables are
5976         ;; not garbage-collected, it seems.  This would the lead to en
5977         ;; ever-growing Emacs.
5978         (gnus-summary-clear-local-variables)
5979         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5980           (gnus-summary-clear-local-variables))
5981         (when (get-buffer gnus-article-buffer)
5982           (bury-buffer gnus-article-buffer))
5983         ;; We clear the global counterparts of the buffer-local
5984         ;; variables as well, just to be on the safe side.
5985         (set-buffer gnus-group-buffer)
5986         (gnus-summary-clear-local-variables)
5987         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5988           (gnus-summary-clear-local-variables)))
5989       (setq gnus-current-select-method gnus-select-method)
5990       (pop-to-buffer gnus-group-buffer)
5991       (if (not quit-config)
5992           (progn
5993             (goto-char group-point)
5994             (gnus-configure-windows 'group 'force)
5995             (unless (pos-visible-in-window-p)
5996               (forward-line (/ (static-if (featurep 'xemacs)
5997                                    (window-displayed-height)
5998                                  (1- (window-height)))
5999                                -2))
6000               (set-window-start (selected-window) (point))
6001               (goto-char group-point)))
6002         (gnus-handle-ephemeral-exit quit-config))
6003       ;; Return to group mode buffer.
6004       (when (eq mode 'gnus-summary-mode)
6005         (gnus-kill-buffer buf))
6006       ;; Clear the current group name.
6007       (unless quit-config
6008         (setq gnus-newsgroup-name nil)))))
6009
6010 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6011 (defun gnus-summary-exit-no-update (&optional no-questions)
6012   "Quit reading current newsgroup without updating read article info."
6013   (interactive)
6014   (let* ((group gnus-newsgroup-name)
6015          (quit-config (gnus-group-quit-config group)))
6016     (when (or no-questions
6017               gnus-expert-user
6018               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6019       (gnus-async-halt-prefetch)
6020       (mapcar 'funcall
6021               (delq 'gnus-summary-expire-articles
6022                     (copy-sequence gnus-summary-prepare-exit-hook)))
6023       ;; If we have several article buffers, we kill them at exit.
6024       (unless gnus-single-article-buffer
6025         (gnus-kill-buffer gnus-article-buffer)
6026         (gnus-kill-buffer gnus-original-article-buffer)
6027         (setq gnus-article-current nil))
6028       (if (not gnus-kill-summary-on-exit)
6029           (gnus-deaden-summary)
6030         (gnus-close-group group)
6031         (gnus-summary-clear-local-variables)
6032         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6033           (gnus-summary-clear-local-variables))
6034         (set-buffer gnus-group-buffer)
6035         (gnus-summary-clear-local-variables)
6036         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6037           (gnus-summary-clear-local-variables))
6038         (when (get-buffer gnus-summary-buffer)
6039           (kill-buffer gnus-summary-buffer)))
6040       (unless gnus-single-article-buffer
6041         (setq gnus-article-current nil))
6042       (when gnus-use-trees
6043         (gnus-tree-close group))
6044       (gnus-async-prefetch-remove-group group)
6045       (when (get-buffer gnus-article-buffer)
6046         (bury-buffer gnus-article-buffer))
6047       ;; Return to the group buffer.
6048       (gnus-configure-windows 'group 'force)
6049       ;; Clear the current group name.
6050       (setq gnus-newsgroup-name nil)
6051       (when (equal (gnus-group-group-name) group)
6052         (gnus-group-next-unread-group 1))
6053       (when quit-config
6054         (gnus-handle-ephemeral-exit quit-config)))))
6055
6056 (defun gnus-handle-ephemeral-exit (quit-config)
6057   "Handle movement when leaving an ephemeral group.
6058 The state which existed when entering the ephemeral is reset."
6059   (if (not (buffer-name (car quit-config)))
6060       (gnus-configure-windows 'group 'force)
6061     (set-buffer (car quit-config))
6062     (cond ((eq major-mode 'gnus-summary-mode)
6063            (gnus-set-global-variables))
6064           ((eq major-mode 'gnus-article-mode)
6065            (save-excursion
6066              ;; The `gnus-summary-buffer' variable may point
6067              ;; to the old summary buffer when using a single
6068              ;; article buffer.
6069              (unless (gnus-buffer-live-p gnus-summary-buffer)
6070                (set-buffer gnus-group-buffer))
6071              (set-buffer gnus-summary-buffer)
6072              (gnus-set-global-variables))))
6073     (if (or (eq (cdr quit-config) 'article)
6074             (eq (cdr quit-config) 'pick))
6075         (progn
6076           ;; The current article may be from the ephemeral group
6077           ;; thus it is best that we reload this article
6078           (gnus-summary-show-article)
6079           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6080               (gnus-configure-windows 'pick 'force)
6081             (gnus-configure-windows (cdr quit-config) 'force)))
6082       (gnus-configure-windows (cdr quit-config) 'force))
6083     (when (eq major-mode 'gnus-summary-mode)
6084       (gnus-summary-next-subject 1 nil t)
6085       (gnus-summary-recenter)
6086       (gnus-summary-position-point))))
6087
6088 (defun gnus-summary-preview-mime-message ()
6089   "MIME decode and play this message."
6090   (interactive)
6091   (let ((gnus-break-pages nil)
6092         (gnus-show-mime t))
6093     (gnus-summary-select-article gnus-show-all-headers t))
6094   (select-window (get-buffer-window gnus-article-buffer)))
6095
6096 ;;; Dead summaries.
6097
6098 (defvar gnus-dead-summary-mode-map nil)
6099
6100 (unless gnus-dead-summary-mode-map
6101   (setq gnus-dead-summary-mode-map (make-keymap))
6102   (suppress-keymap gnus-dead-summary-mode-map)
6103   (substitute-key-definition
6104    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6105   (let ((keys '("\C-d" "\r" "\177" [delete])))
6106     (while keys
6107       (define-key gnus-dead-summary-mode-map
6108         (pop keys) 'gnus-summary-wake-up-the-dead))))
6109
6110 (defvar gnus-dead-summary-mode nil
6111   "Minor mode for Gnus summary buffers.")
6112
6113 (defun gnus-dead-summary-mode (&optional arg)
6114   "Minor mode for Gnus summary buffers."
6115   (interactive "P")
6116   (when (eq major-mode 'gnus-summary-mode)
6117     (make-local-variable 'gnus-dead-summary-mode)
6118     (setq gnus-dead-summary-mode
6119           (if (null arg) (not gnus-dead-summary-mode)
6120             (> (prefix-numeric-value arg) 0)))
6121     (when gnus-dead-summary-mode
6122       (gnus-add-minor-mode
6123        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6124
6125 (defun gnus-deaden-summary ()
6126   "Make the current summary buffer into a dead summary buffer."
6127   ;; Kill any previous dead summary buffer.
6128   (when (and gnus-dead-summary
6129              (buffer-name gnus-dead-summary))
6130     (save-excursion
6131       (set-buffer gnus-dead-summary)
6132       (when gnus-dead-summary-mode
6133         (kill-buffer (current-buffer)))))
6134   ;; Make this the current dead summary.
6135   (setq gnus-dead-summary (current-buffer))
6136   (gnus-dead-summary-mode 1)
6137   (let ((name (buffer-name)))
6138     (when (string-match "Summary" name)
6139       (rename-buffer
6140        (concat (substring name 0 (match-beginning 0)) "Dead "
6141                (substring name (match-beginning 0)))
6142        t)
6143       (bury-buffer))))
6144
6145 (defun gnus-kill-or-deaden-summary (buffer)
6146   "Kill or deaden the summary BUFFER."
6147   (save-excursion
6148     (when (and (buffer-name buffer)
6149                (not gnus-single-article-buffer))
6150       (save-excursion
6151         (set-buffer buffer)
6152         (gnus-kill-buffer gnus-article-buffer)
6153         (gnus-kill-buffer gnus-original-article-buffer)))
6154     (cond (gnus-kill-summary-on-exit
6155            (when (and gnus-use-trees
6156                       (gnus-buffer-exists-p buffer))
6157              (save-excursion
6158                (set-buffer buffer)
6159                (gnus-tree-close gnus-newsgroup-name)))
6160            (gnus-kill-buffer buffer))
6161           ((gnus-buffer-exists-p buffer)
6162            (save-excursion
6163              (set-buffer buffer)
6164              (gnus-deaden-summary))))))
6165
6166 (defun gnus-summary-wake-up-the-dead (&rest args)
6167   "Wake up the dead summary buffer."
6168   (interactive)
6169   (gnus-dead-summary-mode -1)
6170   (let ((name (buffer-name)))
6171     (when (string-match "Dead " name)
6172       (rename-buffer
6173        (concat (substring name 0 (match-beginning 0))
6174                (substring name (match-end 0)))
6175        t)))
6176   (gnus-message 3 "This dead summary is now alive again"))
6177
6178 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6179 (defun gnus-summary-fetch-faq (&optional faq-dir)
6180   "Fetch the FAQ for the current group.
6181 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6182 in."
6183   (interactive
6184    (list
6185     (when current-prefix-arg
6186       (completing-read
6187        "Faq dir: " (and (listp gnus-group-faq-directory)
6188                         (mapcar (lambda (file) (list file))
6189                                 gnus-group-faq-directory))))))
6190   (let (gnus-faq-buffer)
6191     (when (setq gnus-faq-buffer
6192                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6193       (gnus-configure-windows 'summary-faq))))
6194
6195 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6196 (defun gnus-summary-describe-group (&optional force)
6197   "Describe the current newsgroup."
6198   (interactive "P")
6199   (gnus-group-describe-group force gnus-newsgroup-name))
6200
6201 (defun gnus-summary-describe-briefly ()
6202   "Describe summary mode commands briefly."
6203   (interactive)
6204   (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")))
6205
6206 ;; Walking around group mode buffer from summary mode.
6207
6208 (defun gnus-summary-next-group (&optional no-article target-group backward)
6209   "Exit current newsgroup and then select next unread newsgroup.
6210 If prefix argument NO-ARTICLE is non-nil, no article is selected
6211 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6212 previous group instead."
6213   (interactive "P")
6214   ;; Stop pre-fetching.
6215   (gnus-async-halt-prefetch)
6216   (let ((current-group gnus-newsgroup-name)
6217         (current-buffer (current-buffer))
6218         entered)
6219     ;; First we semi-exit this group to update Xrefs and all variables.
6220     ;; We can't do a real exit, because the window conf must remain
6221     ;; the same in case the user is prompted for info, and we don't
6222     ;; want the window conf to change before that...
6223     (gnus-summary-exit t)
6224     (while (not entered)
6225       ;; Then we find what group we are supposed to enter.
6226       (set-buffer gnus-group-buffer)
6227       (gnus-group-jump-to-group current-group)
6228       (setq target-group
6229             (or target-group
6230                 (if (eq gnus-keep-same-level 'best)
6231                     (gnus-summary-best-group gnus-newsgroup-name)
6232                   (gnus-summary-search-group backward gnus-keep-same-level))))
6233       (if (not target-group)
6234           ;; There are no further groups, so we return to the group
6235           ;; buffer.
6236           (progn
6237             (gnus-message 5 "Returning to the group buffer")
6238             (setq entered t)
6239             (when (gnus-buffer-live-p current-buffer)
6240               (set-buffer current-buffer)
6241               (gnus-summary-exit))
6242             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6243         ;; We try to enter the target group.
6244         (gnus-group-jump-to-group target-group)
6245         (let ((unreads (gnus-group-group-unread)))
6246           (if (and (or (eq t unreads)
6247                        (and unreads (not (zerop unreads))))
6248                    (gnus-summary-read-group
6249                     target-group nil no-article
6250                     (and (buffer-name current-buffer) current-buffer)
6251                     nil backward))
6252               (setq entered t)
6253             (setq current-group target-group
6254                   target-group nil)))))))
6255
6256 (defun gnus-summary-prev-group (&optional no-article)
6257   "Exit current newsgroup and then select previous unread newsgroup.
6258 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6259   (interactive "P")
6260   (gnus-summary-next-group no-article nil t))
6261
6262 ;; Walking around summary lines.
6263
6264 (defun gnus-summary-first-subject (&optional unread undownloaded)
6265   "Go to the first unread subject.
6266 If UNREAD is non-nil, go to the first unread article.
6267 Returns the article selected or nil if there are no unread articles."
6268   (interactive "P")
6269   (prog1
6270       (cond
6271        ;; Empty summary.
6272        ((null gnus-newsgroup-data)
6273         (gnus-message 3 "No articles in the group")
6274         nil)
6275        ;; Pick the first article.
6276        ((not unread)
6277         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6278         (gnus-data-number (car gnus-newsgroup-data)))
6279        ;; No unread articles.
6280        ((null gnus-newsgroup-unreads)
6281         (gnus-message 3 "No more unread articles")
6282         nil)
6283        ;; Find the first unread article.
6284        (t
6285         (let ((data gnus-newsgroup-data))
6286           (while (and data
6287                       (and (not (and undownloaded
6288                                      (eq gnus-undownloaded-mark
6289                                          (gnus-data-mark (car data)))))
6290                            (not (gnus-data-unread-p (car data)))))
6291             (setq data (cdr data)))
6292           (when data
6293             (goto-char (gnus-data-pos (car data)))
6294             (gnus-data-number (car data))))))
6295     (gnus-summary-position-point)))
6296
6297 (defun gnus-summary-next-subject (n &optional unread dont-display)
6298   "Go to next N'th summary line.
6299 If N is negative, go to the previous N'th subject line.
6300 If UNREAD is non-nil, only unread articles are selected.
6301 The difference between N and the actual number of steps taken is
6302 returned."
6303   (interactive "p")
6304   (let ((backward (< n 0))
6305         (n (abs n)))
6306     (while (and (> n 0)
6307                 (if backward
6308                     (gnus-summary-find-prev unread)
6309                   (gnus-summary-find-next unread)))
6310       (unless (zerop (setq n (1- n)))
6311         (gnus-summary-show-thread)))
6312     (when (/= 0 n)
6313       (gnus-message 7 "No more%s articles"
6314                     (if unread " unread" "")))
6315     (unless dont-display
6316       (gnus-summary-recenter)
6317       (gnus-summary-position-point))
6318     n))
6319
6320 (defun gnus-summary-next-unread-subject (n)
6321   "Go to next N'th unread summary line."
6322   (interactive "p")
6323   (gnus-summary-next-subject n t))
6324
6325 (defun gnus-summary-prev-subject (n &optional unread)
6326   "Go to previous N'th summary line.
6327 If optional argument UNREAD is non-nil, only unread article is selected."
6328   (interactive "p")
6329   (gnus-summary-next-subject (- n) unread))
6330
6331 (defun gnus-summary-prev-unread-subject (n)
6332   "Go to previous N'th unread summary line."
6333   (interactive "p")
6334   (gnus-summary-next-subject (- n) t))
6335
6336 (defun gnus-summary-goto-subject (article &optional force silent)
6337   "Go the subject line of ARTICLE.
6338 If FORCE, also allow jumping to articles not currently shown."
6339   (interactive "nArticle number: ")
6340   (let ((b (point))
6341         (data (gnus-data-find article)))
6342     ;; We read in the article if we have to.
6343     (and (not data)
6344          force
6345          (gnus-summary-insert-subject
6346           article
6347           (if (or (numberp force) (vectorp force)) force)
6348           t)
6349          (setq data (gnus-data-find article)))
6350     (goto-char b)
6351     (if (not data)
6352         (progn
6353           (unless silent
6354             (gnus-message 3 "Can't find article %d" article))
6355           nil)
6356       (let ((pt (gnus-data-pos data)))
6357         (goto-char pt)
6358         (gnus-summary-set-article-display-arrow pt))
6359       (gnus-summary-position-point)
6360       article)))
6361
6362 ;; Walking around summary lines with displaying articles.
6363
6364 (defun gnus-summary-expand-window (&optional arg)
6365   "Make the summary buffer take up the entire Emacs frame.
6366 Given a prefix, will force an `article' buffer configuration."
6367   (interactive "P")
6368   (if arg
6369       (gnus-configure-windows 'article 'force)
6370     (gnus-configure-windows 'summary 'force)))
6371
6372 (defun gnus-summary-display-article (article &optional all-header)
6373   "Display ARTICLE in article buffer."
6374   (when (gnus-buffer-live-p gnus-article-buffer)
6375     (with-current-buffer gnus-article-buffer
6376       (set-buffer-multibyte t)))
6377   (gnus-set-global-variables)
6378   (when (gnus-buffer-live-p gnus-article-buffer)
6379     (with-current-buffer gnus-article-buffer
6380       (setq gnus-article-charset gnus-newsgroup-charset)
6381       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6382   (if (null article)
6383       nil
6384     (prog1
6385         (if gnus-summary-display-article-function
6386             (funcall gnus-summary-display-article-function article all-header)
6387           (gnus-article-prepare article all-header))
6388       (with-current-buffer gnus-article-buffer
6389         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6390              nil))
6391       (gnus-run-hooks 'gnus-select-article-hook)
6392       (when (and gnus-current-article
6393                  (not (zerop gnus-current-article)))
6394         (gnus-summary-goto-subject gnus-current-article))
6395       (gnus-summary-recenter)
6396       (when (and gnus-use-trees gnus-show-threads)
6397         (gnus-possibly-generate-tree article)
6398         (gnus-highlight-selected-tree article))
6399       ;; Successfully display article.
6400       (gnus-article-set-window-start
6401        (cdr (assq article gnus-newsgroup-bookmarks))))))
6402
6403 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6404   "Select the current article.
6405 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6406 non-nil, the article will be re-fetched even if it already present in
6407 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6408 be displayed."
6409   ;; Make sure we are in the summary buffer to work around bbdb bug.
6410   (unless (eq major-mode 'gnus-summary-mode)
6411     (set-buffer gnus-summary-buffer))
6412   (let ((article (or article (gnus-summary-article-number)))
6413         (all-headers (not (not all-headers))) ;Must be T or NIL.
6414         gnus-summary-display-article-function)
6415     (and (not pseudo)
6416          (gnus-summary-article-pseudo-p article)
6417          (error "This is a pseudo-article"))
6418     (save-excursion
6419       (set-buffer gnus-summary-buffer)
6420       (if (or (and gnus-single-article-buffer
6421                    (or (null gnus-current-article)
6422                        (null gnus-article-current)
6423                        (null (get-buffer gnus-article-buffer))
6424                        (not (eq article (cdr gnus-article-current)))
6425                        (not (equal (car gnus-article-current)
6426                                    gnus-newsgroup-name))))
6427               (and (not gnus-single-article-buffer)
6428                    (or (null gnus-current-article)
6429                        (not (eq gnus-current-article article))))
6430               force)
6431           ;; The requested article is different from the current article.
6432           (progn
6433             (gnus-summary-display-article article all-headers)
6434             (when (or all-headers gnus-show-all-headers)
6435               (gnus-article-show-all-headers))
6436             (gnus-article-set-window-start
6437              (cdr (assq article gnus-newsgroup-bookmarks)))
6438             article)
6439         (when (or all-headers gnus-show-all-headers)
6440           (gnus-article-show-all-headers))
6441         'old))))
6442
6443 (defun gnus-summary-force-verify-and-decrypt ()
6444   (interactive)
6445   (let ((mm-verify-option 'known)
6446         (mm-decrypt-option 'known))
6447     (gnus-summary-select-article nil 'force)))
6448
6449 (defun gnus-summary-set-current-mark (&optional current-mark)
6450   "Obsolete function."
6451   nil)
6452
6453 (defun gnus-summary-next-article (&optional unread subject backward push)
6454   "Select the next article.
6455 If UNREAD, only unread articles are selected.
6456 If SUBJECT, only articles with SUBJECT are selected.
6457 If BACKWARD, the previous article is selected instead of the next."
6458   (interactive "P")
6459   (cond
6460    ;; Is there such an article?
6461    ((and (gnus-summary-search-forward unread subject backward)
6462          (or (gnus-summary-display-article (gnus-summary-article-number))
6463              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6464     (gnus-summary-position-point))
6465    ;; If not, we try the first unread, if that is wanted.
6466    ((and subject
6467          gnus-auto-select-same
6468          (gnus-summary-first-unread-article))
6469     (gnus-summary-position-point)
6470     (gnus-message 6 "Wrapped"))
6471    ;; Try to get next/previous article not displayed in this group.
6472    ((and gnus-auto-extend-newsgroup
6473          (not unread) (not subject))
6474     (gnus-summary-goto-article
6475      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6476      nil (count-lines (point-min) (point))))
6477    ;; Go to next/previous group.
6478    (t
6479     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6480       (gnus-summary-jump-to-group gnus-newsgroup-name))
6481     (let ((cmd last-command-char)
6482           (point
6483            (save-excursion
6484              (set-buffer gnus-group-buffer)
6485              (point)))
6486           (group
6487            (if (eq gnus-keep-same-level 'best)
6488                (gnus-summary-best-group gnus-newsgroup-name)
6489              (gnus-summary-search-group backward gnus-keep-same-level))))
6490       ;; For some reason, the group window gets selected.  We change
6491       ;; it back.
6492       (select-window (get-buffer-window (current-buffer)))
6493       ;; Select next unread newsgroup automagically.
6494       (cond
6495        ((or (not gnus-auto-select-next)
6496             (not cmd))
6497         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6498        ((or (eq gnus-auto-select-next 'quietly)
6499             (and (eq gnus-auto-select-next 'slightly-quietly)
6500                  push)
6501             (and (eq gnus-auto-select-next 'almost-quietly)
6502                  (gnus-summary-last-article-p)))
6503         ;; Select quietly.
6504         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6505             (gnus-summary-exit)
6506           (gnus-message 7 "No more%s articles (%s)..."
6507                         (if unread " unread" "")
6508                         (if group (concat "selecting " group)
6509                           "exiting"))
6510           (gnus-summary-next-group nil group backward)))
6511        (t
6512         (when (gnus-key-press-event-p last-input-event)
6513           (gnus-summary-walk-group-buffer
6514            gnus-newsgroup-name cmd unread backward point))))))))
6515
6516 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6517   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6518                       (?\C-p (gnus-group-prev-unread-group 1))))
6519         (cursor-in-echo-area t)
6520         keve key group ended)
6521     (save-excursion
6522       (set-buffer gnus-group-buffer)
6523       (goto-char start)
6524       (setq group
6525             (if (eq gnus-keep-same-level 'best)
6526                 (gnus-summary-best-group gnus-newsgroup-name)
6527               (gnus-summary-search-group backward gnus-keep-same-level))))
6528     (while (not ended)
6529       (gnus-message
6530        5 "No more%s articles%s" (if unread " unread" "")
6531        (if (and group
6532                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6533            (format " (Type %s for %s [%s])"
6534                    (single-key-description cmd) group
6535                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6536          (format " (Type %s to exit %s)"
6537                  (single-key-description cmd)
6538                  gnus-newsgroup-name)))
6539       ;; Confirm auto selection.
6540       (setq key (car (setq keve (gnus-read-event-char))))
6541       (setq ended t)
6542       (cond
6543        ((assq key keystrokes)
6544         (let ((obuf (current-buffer)))
6545           (switch-to-buffer gnus-group-buffer)
6546           (when group
6547             (gnus-group-jump-to-group group))
6548           (eval (cadr (assq key keystrokes)))
6549           (setq group (gnus-group-group-name))
6550           (switch-to-buffer obuf))
6551         (setq ended nil))
6552        ((equal key cmd)
6553         (if (or (not group)
6554                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6555             (gnus-summary-exit)
6556           (gnus-summary-next-group nil group backward)))
6557        (t
6558         (push (cdr keve) unread-command-events))))))
6559
6560 (defun gnus-summary-next-unread-article ()
6561   "Select unread article after current one."
6562   (interactive)
6563   (gnus-summary-next-article
6564    (or (not (eq gnus-summary-goto-unread 'never))
6565        (gnus-summary-last-article-p (gnus-summary-article-number)))
6566    (and gnus-auto-select-same
6567         (gnus-summary-article-subject))))
6568
6569 (defun gnus-summary-prev-article (&optional unread subject)
6570   "Select the article after the current one.
6571 If UNREAD is non-nil, only unread articles are selected."
6572   (interactive "P")
6573   (gnus-summary-next-article unread subject t))
6574
6575 (defun gnus-summary-prev-unread-article ()
6576   "Select unread article before current one."
6577   (interactive)
6578   (gnus-summary-prev-article
6579    (or (not (eq gnus-summary-goto-unread 'never))
6580        (gnus-summary-first-article-p (gnus-summary-article-number)))
6581    (and gnus-auto-select-same
6582         (gnus-summary-article-subject))))
6583
6584 (defun gnus-summary-next-page (&optional lines circular)
6585   "Show next page of the selected article.
6586 If at the end of the current article, select the next article.
6587 LINES says how many lines should be scrolled up.
6588
6589 If CIRCULAR is non-nil, go to the start of the article instead of
6590 selecting the next article when reaching the end of the current
6591 article."
6592   (interactive "P")
6593   (setq gnus-summary-buffer (current-buffer))
6594   (gnus-set-global-variables)
6595   (let ((article (gnus-summary-article-number))
6596         (article-window (get-buffer-window gnus-article-buffer t))
6597         endp)
6598     ;; If the buffer is empty, we have no article.
6599     (unless article
6600       (error "No article to select"))
6601     (gnus-configure-windows 'article)
6602     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6603         (if (and (eq gnus-summary-goto-unread 'never)
6604                  (not (gnus-summary-last-article-p article)))
6605             (gnus-summary-next-article)
6606           (gnus-summary-next-unread-article))
6607       (if (or (null gnus-current-article)
6608               (null gnus-article-current)
6609               (/= article (cdr gnus-article-current))
6610               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6611           ;; Selected subject is different from current article's.
6612           (gnus-summary-display-article article)
6613         (when article-window
6614           (gnus-eval-in-buffer-window gnus-article-buffer
6615             (setq endp (gnus-article-next-page lines)))
6616           (when endp
6617             (cond (circular
6618                    (gnus-summary-beginning-of-article))
6619                   (lines
6620                    (gnus-message 3 "End of message"))
6621                   ((null lines)
6622                    (if (and (eq gnus-summary-goto-unread 'never)
6623                             (not (gnus-summary-last-article-p article)))
6624                        (gnus-summary-next-article)
6625                      (gnus-summary-next-unread-article))))))))
6626     (gnus-summary-recenter)
6627     (gnus-summary-position-point)))
6628
6629 (defun gnus-summary-prev-page (&optional lines move)
6630   "Show previous page of selected article.
6631 Argument LINES specifies lines to be scrolled down.
6632 If MOVE, move to the previous unread article if point is at
6633 the beginning of the buffer."
6634   (interactive "P")
6635   (let ((article (gnus-summary-article-number))
6636         (article-window (get-buffer-window gnus-article-buffer t))
6637         endp)
6638     (gnus-configure-windows 'article)
6639     (if (or (null gnus-current-article)
6640             (null gnus-article-current)
6641             (/= article (cdr gnus-article-current))
6642             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6643         ;; Selected subject is different from current article's.
6644         (gnus-summary-display-article article)
6645       (gnus-summary-recenter)
6646       (when article-window
6647         (gnus-eval-in-buffer-window gnus-article-buffer
6648           (setq endp (gnus-article-prev-page lines)))
6649         (when (and move endp)
6650           (cond (lines
6651                  (gnus-message 3 "Beginning of message"))
6652                 ((null lines)
6653                  (if (and (eq gnus-summary-goto-unread 'never)
6654                           (not (gnus-summary-first-article-p article)))
6655                      (gnus-summary-prev-article)
6656                    (gnus-summary-prev-unread-article))))))))
6657   (gnus-summary-position-point))
6658
6659 (defun gnus-summary-prev-page-or-article (&optional lines)
6660   "Show previous page of selected article.
6661 Argument LINES specifies lines to be scrolled down.
6662 If at the beginning of the article, go to the next article."
6663   (interactive "P")
6664   (gnus-summary-prev-page lines t))
6665
6666 (defun gnus-summary-scroll-up (lines)
6667   "Scroll up (or down) one line current article.
6668 Argument LINES specifies lines to be scrolled up (or down if negative)."
6669   (interactive "p")
6670   (gnus-configure-windows 'article)
6671   (gnus-summary-show-thread)
6672   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6673     (gnus-eval-in-buffer-window gnus-article-buffer
6674       (cond ((> lines 0)
6675              (when (gnus-article-next-page lines)
6676                (gnus-message 3 "End of message")))
6677             ((< lines 0)
6678              (gnus-article-prev-page (- lines))))))
6679   (gnus-summary-recenter)
6680   (gnus-summary-position-point))
6681
6682 (defun gnus-summary-scroll-down (lines)
6683   "Scroll down (or up) one line current article.
6684 Argument LINES specifies lines to be scrolled down (or up if negative)."
6685   (interactive "p")
6686   (gnus-summary-scroll-up (- lines)))
6687
6688 (defun gnus-summary-next-same-subject ()
6689   "Select next article which has the same subject as current one."
6690   (interactive)
6691   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6692
6693 (defun gnus-summary-prev-same-subject ()
6694   "Select previous article which has the same subject as current one."
6695   (interactive)
6696   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6697
6698 (defun gnus-summary-next-unread-same-subject ()
6699   "Select next unread article which has the same subject as current one."
6700   (interactive)
6701   (gnus-summary-next-article t (gnus-summary-article-subject)))
6702
6703 (defun gnus-summary-prev-unread-same-subject ()
6704   "Select previous unread article which has the same subject as current one."
6705   (interactive)
6706   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6707
6708 (defun gnus-summary-first-unread-article ()
6709   "Select the first unread article.
6710 Return nil if there are no unread articles."
6711   (interactive)
6712   (prog1
6713       (when (gnus-summary-first-subject t)
6714         (gnus-summary-show-thread)
6715         (gnus-summary-first-subject t)
6716         (gnus-summary-display-article (gnus-summary-article-number)))
6717     (gnus-summary-position-point)))
6718
6719 (defun gnus-summary-first-unread-subject ()
6720   "Place the point on the subject line of the first unread article.
6721 Return nil if there are no unread articles."
6722   (interactive)
6723   (prog1
6724       (when (gnus-summary-first-subject t)
6725         (gnus-summary-show-thread)
6726         (gnus-summary-first-subject t))
6727     (gnus-summary-position-point)))
6728
6729 (defun gnus-summary-first-article ()
6730   "Select the first article.
6731 Return nil if there are no articles."
6732   (interactive)
6733   (prog1
6734       (when (gnus-summary-first-subject)
6735         (gnus-summary-show-thread)
6736         (gnus-summary-first-subject)
6737         (gnus-summary-display-article (gnus-summary-article-number)))
6738     (gnus-summary-position-point)))
6739
6740 (defun gnus-summary-best-unread-article ()
6741   "Select the unread article with the highest score."
6742   (interactive)
6743   (let ((best -1000000)
6744         (data gnus-newsgroup-data)
6745         article score)
6746     (while data
6747       (and (gnus-data-unread-p (car data))
6748            (> (setq score
6749                     (gnus-summary-article-score (gnus-data-number (car data))))
6750               best)
6751            (setq best score
6752                  article (gnus-data-number (car data))))
6753       (setq data (cdr data)))
6754     (prog1
6755         (if article
6756             (gnus-summary-goto-article article)
6757           (error "No unread articles"))
6758       (gnus-summary-position-point))))
6759
6760 (defun gnus-summary-last-subject ()
6761   "Go to the last displayed subject line in the group."
6762   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6763     (when article
6764       (gnus-summary-goto-subject article))))
6765
6766 (defun gnus-summary-goto-article (article &optional all-headers force)
6767   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6768 If ALL-HEADERS is non-nil, no header lines are hidden.
6769 If FORCE, go to the article even if it isn't displayed.  If FORCE
6770 is a number, it is the line the article is to be displayed on."
6771   (interactive
6772    (list
6773     (completing-read
6774      "Article number or Message-ID: "
6775      (mapcar (lambda (number) (list (int-to-string number)))
6776              gnus-newsgroup-limit))
6777     current-prefix-arg
6778     t))
6779   (prog1
6780       (if (and (stringp article)
6781                (string-match "@" article))
6782           (gnus-summary-refer-article article)
6783         (when (stringp article)
6784           (setq article (string-to-number article)))
6785         (if (gnus-summary-goto-subject article force)
6786             (gnus-summary-display-article article all-headers)
6787           (gnus-message 4 "Couldn't go to article %s" article) nil))
6788     (gnus-summary-position-point)))
6789
6790 (defun gnus-summary-goto-last-article ()
6791   "Go to the previously read article."
6792   (interactive)
6793   (prog1
6794       (when gnus-last-article
6795         (gnus-summary-goto-article gnus-last-article nil t))
6796     (gnus-summary-position-point)))
6797
6798 (defun gnus-summary-pop-article (number)
6799   "Pop one article off the history and go to the previous.
6800 NUMBER articles will be popped off."
6801   (interactive "p")
6802   (let (to)
6803     (setq gnus-newsgroup-history
6804           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6805     (if to
6806         (gnus-summary-goto-article (car to) nil t)
6807       (error "Article history empty")))
6808   (gnus-summary-position-point))
6809
6810 ;; Summary commands and functions for limiting the summary buffer.
6811
6812 (defun gnus-summary-limit-to-articles (n)
6813   "Limit the summary buffer to the next N articles.
6814 If not given a prefix, use the process marked articles instead."
6815   (interactive "P")
6816   (prog1
6817       (let ((articles (gnus-summary-work-articles n)))
6818         (setq gnus-newsgroup-processable nil)
6819         (gnus-summary-limit articles))
6820     (gnus-summary-position-point)))
6821
6822 (defun gnus-summary-pop-limit (&optional total)
6823   "Restore the previous limit.
6824 If given a prefix, remove all limits."
6825   (interactive "P")
6826   (when total
6827     (setq gnus-newsgroup-limits
6828           (list (mapcar (lambda (h) (mail-header-number h))
6829                         gnus-newsgroup-headers))))
6830   (unless gnus-newsgroup-limits
6831     (error "No limit to pop"))
6832   (prog1
6833       (gnus-summary-limit nil 'pop)
6834     (gnus-summary-position-point)))
6835
6836 (defun gnus-summary-limit-to-subject (subject &optional header)
6837   "Limit the summary buffer to articles that have subjects that match a regexp."
6838   (interactive "sLimit to subject (regexp): ")
6839   (unless header
6840     (setq header "subject"))
6841   (when (not (equal "" subject))
6842     (prog1
6843         (let ((articles (gnus-summary-find-matching
6844                          (or header "subject") subject 'all)))
6845           (unless articles
6846             (error "Found no matches for \"%s\"" subject))
6847           (gnus-summary-limit articles))
6848       (gnus-summary-position-point))))
6849
6850 (defun gnus-summary-limit-to-author (from)
6851   "Limit the summary buffer to articles that have authors that match a regexp."
6852   (interactive "sLimit to author (regexp): ")
6853   (gnus-summary-limit-to-subject from "from"))
6854
6855 (defun gnus-summary-limit-to-age (age &optional younger-p)
6856   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6857 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6858 articles that are younger than AGE days."
6859   (interactive
6860    (let ((younger current-prefix-arg)
6861          (days-got nil)
6862          days)
6863      (while (not days-got)
6864        (setq days (if younger
6865                       (read-string "Limit to articles within (in days): ")
6866                     (read-string "Limit to articles older than (in days): ")))
6867        (when (> (length days) 0)
6868          (setq days (read days)))
6869        (if (numberp days)
6870            (setq days-got t)
6871          (message "Please enter a number.")
6872          (sleep-for 1)))
6873      (list days younger)))
6874   (prog1
6875       (let ((data gnus-newsgroup-data)
6876             (cutoff (days-to-time age))
6877             articles d date is-younger)
6878         (while (setq d (pop data))
6879           (when (and (vectorp (gnus-data-header d))
6880                      (setq date (mail-header-date (gnus-data-header d))))
6881             (setq is-younger (time-less-p
6882                               (time-since (condition-case ()
6883                                               (date-to-time date)
6884                                             (error '(0 0))))
6885                               cutoff))
6886             (when (if younger-p
6887                       is-younger
6888                     (not is-younger))
6889               (push (gnus-data-number d) articles))))
6890         (gnus-summary-limit (nreverse articles)))
6891     (gnus-summary-position-point)))
6892
6893 (defun gnus-summary-limit-to-extra (header regexp)
6894   "Limit the summary buffer to articles that match an 'extra' header."
6895   (interactive
6896    (let ((header
6897           (intern
6898            (gnus-completing-read
6899             (symbol-name (car gnus-extra-headers))
6900             "Limit extra header:"
6901             (mapcar (lambda (x)
6902                       (cons (symbol-name x) x))
6903                     gnus-extra-headers)
6904             nil
6905             t))))
6906      (list header
6907            (read-string (format "Limit to header %s (regexp): " header)))))
6908   (when (not (equal "" regexp))
6909     (prog1
6910         (let ((articles (gnus-summary-find-matching
6911                          (cons 'extra header) regexp 'all)))
6912           (unless articles
6913             (error "Found no matches for \"%s\"" regexp))
6914           (gnus-summary-limit articles))
6915       (gnus-summary-position-point))))
6916
6917 (defun gnus-summary-limit-to-display-predicate ()
6918   "Limit the summary buffer to the predicated in the `display' group parameter."
6919   (interactive)
6920   (unless gnus-newsgroup-display
6921     (error "There is no `diplay' group parameter"))
6922   (let (articles)
6923     (dolist (number gnus-newsgroup-articles)
6924       (when (funcall gnus-newsgroup-display)
6925         (push number articles)))
6926     (gnus-summary-limit articles))
6927   (gnus-summary-position-point))
6928
6929 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6930 (make-obsolete
6931  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6932
6933 (defun gnus-summary-limit-to-unread (&optional all)
6934   "Limit the summary buffer to articles that are not marked as read.
6935 If ALL is non-nil, limit strictly to unread articles."
6936   (interactive "P")
6937   (if all
6938       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6939     (gnus-summary-limit-to-marks
6940      ;; Concat all the marks that say that an article is read and have
6941      ;; those removed.
6942      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6943            gnus-killed-mark gnus-kill-file-mark
6944            gnus-low-score-mark gnus-expirable-mark
6945            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6946            gnus-duplicate-mark gnus-souped-mark)
6947      'reverse)))
6948
6949 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6950 (make-obsolete 'gnus-summary-delete-marked-with
6951                'gnus-summary-limit-exlude-marks)
6952
6953 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6954   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6955 If REVERSE, limit the summary buffer to articles that are marked
6956 with MARKS.  MARKS can either be a string of marks or a list of marks.
6957 Returns how many articles were removed."
6958   (interactive "sMarks: ")
6959   (gnus-summary-limit-to-marks marks t))
6960
6961 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6962   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6963 If REVERSE (the prefix), limit the summary buffer to articles that are
6964 not marked with MARKS.  MARKS can either be a string of marks or a
6965 list of marks.
6966 Returns how many articles were removed."
6967   (interactive "sMarks: \nP")
6968   (prog1
6969       (let ((data gnus-newsgroup-data)
6970             (marks (if (listp marks) marks
6971                      (append marks nil))) ; Transform to list.
6972             articles)
6973         (while data
6974           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6975                   (memq (gnus-data-mark (car data)) marks))
6976             (push (gnus-data-number (car data)) articles))
6977           (setq data (cdr data)))
6978         (gnus-summary-limit articles))
6979     (gnus-summary-position-point)))
6980
6981 (defun gnus-summary-limit-to-score (&optional score)
6982   "Limit to articles with score at or above SCORE."
6983   (interactive "P")
6984   (setq score (if score
6985                   (prefix-numeric-value score)
6986                 (or gnus-summary-default-score 0)))
6987   (let ((data gnus-newsgroup-data)
6988         articles)
6989     (while data
6990       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6991                 score)
6992         (push (gnus-data-number (car data)) articles))
6993       (setq data (cdr data)))
6994     (prog1
6995         (gnus-summary-limit articles)
6996       (gnus-summary-position-point))))
6997
6998 (defun gnus-summary-limit-include-thread (id)
6999   "Display all the hidden articles that is in the thread with ID in it.
7000 When called interactively, ID is the Message-ID of the current
7001 article."
7002   (interactive (list (mail-header-id (gnus-summary-article-header))))
7003   (let ((articles (gnus-articles-in-thread
7004                    (gnus-id-to-thread (gnus-root-id id)))))
7005     (prog1
7006         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7007       (gnus-summary-limit-include-matching-articles
7008        "subject"
7009        (regexp-quote (gnus-simplify-subject-re
7010                       (mail-header-subject (gnus-id-to-header id)))))
7011       (gnus-summary-position-point))))
7012
7013 (defun gnus-summary-limit-include-matching-articles (header regexp)
7014   "Display all the hidden articles that have HEADERs that match REGEXP."
7015   (interactive (list (read-string "Match on header: ")
7016                      (read-string "Regexp: ")))
7017   (let ((articles (gnus-find-matching-articles header regexp)))
7018     (prog1
7019         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7020       (gnus-summary-position-point))))
7021
7022 (defun gnus-summary-limit-include-dormant ()
7023   "Display all the hidden articles that are marked as dormant.
7024 Note that this command only works on a subset of the articles currently
7025 fetched for this group."
7026   (interactive)
7027   (unless gnus-newsgroup-dormant
7028     (error "There are no dormant articles in this group"))
7029   (prog1
7030       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7031     (gnus-summary-position-point)))
7032
7033 (defun gnus-summary-limit-exclude-dormant ()
7034   "Hide all dormant articles."
7035   (interactive)
7036   (prog1
7037       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7038     (gnus-summary-position-point)))
7039
7040 (defun gnus-summary-limit-exclude-childless-dormant ()
7041   "Hide all dormant articles that have no children."
7042   (interactive)
7043   (let ((data (gnus-data-list t))
7044         articles d children)
7045     ;; Find all articles that are either not dormant or have
7046     ;; children.
7047     (while (setq d (pop data))
7048       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7049                 (and (setq children
7050                            (gnus-article-children (gnus-data-number d)))
7051                      (let (found)
7052                        (while children
7053                          (when (memq (car children) articles)
7054                            (setq children nil
7055                                  found t))
7056                          (pop children))
7057                        found)))
7058         (push (gnus-data-number d) articles)))
7059     ;; Do the limiting.
7060     (prog1
7061         (gnus-summary-limit articles)
7062       (gnus-summary-position-point))))
7063
7064 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7065   "Mark all unread excluded articles as read.
7066 If ALL, mark even excluded ticked and dormants as read."
7067   (interactive "P")
7068   (let ((articles (gnus-sorted-complement
7069                    (sort
7070                     (mapcar (lambda (h) (mail-header-number h))
7071                             gnus-newsgroup-headers)
7072                     '<)
7073                    (sort gnus-newsgroup-limit '<)))
7074         article)
7075     (setq gnus-newsgroup-unreads
7076           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7077     (if all
7078         (setq gnus-newsgroup-dormant nil
7079               gnus-newsgroup-marked nil
7080               gnus-newsgroup-reads
7081               (nconc
7082                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7083                gnus-newsgroup-reads))
7084       (while (setq article (pop articles))
7085         (unless (or (memq article gnus-newsgroup-dormant)
7086                     (memq article gnus-newsgroup-marked))
7087           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7088
7089 (defun gnus-summary-limit (articles &optional pop)
7090   (if pop
7091       ;; We pop the previous limit off the stack and use that.
7092       (setq articles (car gnus-newsgroup-limits)
7093             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7094     ;; We use the new limit, so we push the old limit on the stack.
7095     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7096   ;; Set the limit.
7097   (setq gnus-newsgroup-limit articles)
7098   (let ((total (length gnus-newsgroup-data))
7099         (data (gnus-data-find-list (gnus-summary-article-number)))
7100         (gnus-summary-mark-below nil)   ; Inhibit this.
7101         found)
7102     ;; This will do all the work of generating the new summary buffer
7103     ;; according to the new limit.
7104     (gnus-summary-prepare)
7105     ;; Hide any threads, possibly.
7106     (and gnus-show-threads
7107          gnus-thread-hide-subtree
7108          (gnus-summary-hide-all-threads))
7109     ;; Try to return to the article you were at, or one in the
7110     ;; neighborhood.
7111     (when data
7112       ;; We try to find some article after the current one.
7113       (while data
7114         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7115           (setq data nil
7116                 found t))
7117         (setq data (cdr data))))
7118     (unless found
7119       ;; If there is no data, that means that we were after the last
7120       ;; article.  The same goes when we can't find any articles
7121       ;; after the current one.
7122       (goto-char (point-max))
7123       (gnus-summary-find-prev))
7124     (gnus-set-mode-line 'summary)
7125     ;; We return how many articles were removed from the summary
7126     ;; buffer as a result of the new limit.
7127     (- total (length gnus-newsgroup-data))))
7128
7129 (defsubst gnus-invisible-cut-children (threads)
7130   (let ((num 0))
7131     (while threads
7132       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7133         (incf num))
7134       (pop threads))
7135     (< num 2)))
7136
7137 (defsubst gnus-cut-thread (thread)
7138   "Go forwards in the thread until we find an article that we want to display."
7139   (when (or (eq gnus-fetch-old-headers 'some)
7140             (eq gnus-fetch-old-headers 'invisible)
7141             (numberp gnus-fetch-old-headers)
7142             (eq gnus-build-sparse-threads 'some)
7143             (eq gnus-build-sparse-threads 'more))
7144     ;; Deal with old-fetched headers and sparse threads.
7145     (while (and
7146             thread
7147             (or
7148              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7149              (gnus-summary-article-ancient-p
7150               (mail-header-number (car thread))))
7151             (if (or (<= (length (cdr thread)) 1)
7152                     (eq gnus-fetch-old-headers 'invisible))
7153                 (setq gnus-newsgroup-limit
7154                       (delq (mail-header-number (car thread))
7155                             gnus-newsgroup-limit)
7156                       thread (cadr thread))
7157               (when (gnus-invisible-cut-children (cdr thread))
7158                 (let ((th (cdr thread)))
7159                   (while th
7160                     (if (memq (mail-header-number (caar th))
7161                               gnus-newsgroup-limit)
7162                         (setq thread (car th)
7163                               th nil)
7164                       (setq th (cdr th))))))))))
7165   thread)
7166
7167 (defun gnus-cut-threads (threads)
7168   "Cut off all uninteresting articles from the beginning of threads."
7169   (when (or (eq gnus-fetch-old-headers 'some)
7170             (eq gnus-fetch-old-headers 'invisible)
7171             (numberp gnus-fetch-old-headers)
7172             (eq gnus-build-sparse-threads 'some)
7173             (eq gnus-build-sparse-threads 'more))
7174     (let ((th threads))
7175       (while th
7176         (setcar th (gnus-cut-thread (car th)))
7177         (setq th (cdr th)))))
7178   ;; Remove nixed out threads.
7179   (delq nil threads))
7180
7181 (defun gnus-summary-initial-limit (&optional show-if-empty)
7182   "Figure out what the initial limit is supposed to be on group entry.
7183 This entails weeding out unwanted dormants, low-scored articles,
7184 fetch-old-headers verbiage, and so on."
7185   ;; Most groups have nothing to remove.
7186   (if (or gnus-inhibit-limiting
7187           (and (null gnus-newsgroup-dormant)
7188                (eq gnus-newsgroup-display 'gnus-not-ignore)
7189                (not (eq gnus-fetch-old-headers 'some))
7190                (not (numberp gnus-fetch-old-headers))
7191                (not (eq gnus-fetch-old-headers 'invisible))
7192                (null gnus-summary-expunge-below)
7193                (not (eq gnus-build-sparse-threads 'some))
7194                (not (eq gnus-build-sparse-threads 'more))
7195                (null gnus-thread-expunge-below)
7196                (not gnus-use-nocem)))
7197       ()                                ; Do nothing.
7198     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7199     (setq gnus-newsgroup-limit nil)
7200     (mapatoms
7201      (lambda (node)
7202        (unless (car (symbol-value node))
7203          ;; These threads have no parents -- they are roots.
7204          (let ((nodes (cdr (symbol-value node)))
7205                thread)
7206            (while nodes
7207              (if (and gnus-thread-expunge-below
7208                       (< (gnus-thread-total-score (car nodes))
7209                          gnus-thread-expunge-below))
7210                  (gnus-expunge-thread (pop nodes))
7211                (setq thread (pop nodes))
7212                (gnus-summary-limit-children thread))))))
7213      gnus-newsgroup-dependencies)
7214     ;; If this limitation resulted in an empty group, we might
7215     ;; pop the previous limit and use it instead.
7216     (when (and (not gnus-newsgroup-limit)
7217                show-if-empty)
7218       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7219     gnus-newsgroup-limit))
7220
7221 (defun gnus-summary-limit-children (thread)
7222   "Return 1 if this subthread is visible and 0 if it is not."
7223   ;; First we get the number of visible children to this thread.  This
7224   ;; is done by recursing down the thread using this function, so this
7225   ;; will really go down to a leaf article first, before slowly
7226   ;; working its way up towards the root.
7227   (when thread
7228     (let ((children
7229            (if (cdr thread)
7230                (apply '+ (mapcar 'gnus-summary-limit-children
7231                                  (cdr thread)))
7232              0))
7233           (number (mail-header-number (car thread)))
7234           score)
7235       (if (and
7236            (not (memq number gnus-newsgroup-marked))
7237            (or
7238             ;; If this article is dormant and has absolutely no visible
7239             ;; children, then this article isn't visible.
7240             (and (memq number gnus-newsgroup-dormant)
7241                  (zerop children))
7242             ;; If this is "fetch-old-headered" and there is no
7243             ;; visible children, then we don't want this article.
7244             (and (or (eq gnus-fetch-old-headers 'some)
7245                      (numberp gnus-fetch-old-headers))
7246                  (gnus-summary-article-ancient-p number)
7247                  (zerop children))
7248             ;; If this is "fetch-old-headered" and `invisible', then
7249             ;; we don't want this article.
7250             (and (eq gnus-fetch-old-headers 'invisible)
7251                  (gnus-summary-article-ancient-p number))
7252             ;; If this is a sparsely inserted article with no children,
7253             ;; we don't want it.
7254             (and (eq gnus-build-sparse-threads 'some)
7255                  (gnus-summary-article-sparse-p number)
7256                  (zerop children))
7257             ;; If we use expunging, and this article is really
7258             ;; low-scored, then we don't want this article.
7259             (when (and gnus-summary-expunge-below
7260                        (< (setq score
7261                                 (or (cdr (assq number gnus-newsgroup-scored))
7262                                     gnus-summary-default-score))
7263                           gnus-summary-expunge-below))
7264               ;; We increase the expunge-tally here, but that has
7265               ;; nothing to do with the limits, really.
7266               (incf gnus-newsgroup-expunged-tally)
7267               ;; We also mark as read here, if that's wanted.
7268               (when (and gnus-summary-mark-below
7269                          (< score gnus-summary-mark-below))
7270                 (setq gnus-newsgroup-unreads
7271                       (delq number gnus-newsgroup-unreads))
7272                 (if gnus-newsgroup-auto-expire
7273                     (push number gnus-newsgroup-expirable)
7274                   (push (cons number gnus-low-score-mark)
7275                         gnus-newsgroup-reads)))
7276               t)
7277             ;; Do the `display' group parameter.
7278             (and gnus-newsgroup-display
7279                  (not (funcall gnus-newsgroup-display)))
7280             ;; Check NoCeM things.
7281             (if (and gnus-use-nocem
7282                      (gnus-nocem-unwanted-article-p
7283                       (mail-header-id (car thread))))
7284                 (progn
7285                   (setq gnus-newsgroup-unreads
7286                         (delq number gnus-newsgroup-unreads))
7287                   t))))
7288           ;; Nope, invisible article.
7289           0
7290         ;; Ok, this article is to be visible, so we add it to the limit
7291         ;; and return 1.
7292         (push number gnus-newsgroup-limit)
7293         1))))
7294
7295 (defun gnus-expunge-thread (thread)
7296   "Mark all articles in THREAD as read."
7297   (let* ((number (mail-header-number (car thread))))
7298     (incf gnus-newsgroup-expunged-tally)
7299     ;; We also mark as read here, if that's wanted.
7300     (setq gnus-newsgroup-unreads
7301           (delq number gnus-newsgroup-unreads))
7302     (if gnus-newsgroup-auto-expire
7303         (push number gnus-newsgroup-expirable)
7304       (push (cons number gnus-low-score-mark)
7305             gnus-newsgroup-reads)))
7306   ;; Go recursively through all subthreads.
7307   (mapcar 'gnus-expunge-thread (cdr thread)))
7308
7309 ;; Summary article oriented commands
7310
7311 (defun gnus-summary-refer-parent-article (n)
7312   "Refer parent article N times.
7313 If N is negative, go to ancestor -N instead.
7314 The difference between N and the number of articles fetched is returned."
7315   (interactive "p")
7316   (let ((skip 1)
7317         error header ref)
7318     (when (not (natnump n))
7319       (setq skip (abs n)
7320             n 1))
7321     (while (and (> n 0)
7322                 (not error))
7323       (setq header (gnus-summary-article-header))
7324       (if (and (eq (mail-header-number header)
7325                    (cdr gnus-article-current))
7326                (equal gnus-newsgroup-name
7327                       (car gnus-article-current)))
7328           ;; If we try to find the parent of the currently
7329           ;; displayed article, then we take a look at the actual
7330           ;; References header, since this is slightly more
7331           ;; reliable than the References field we got from the
7332           ;; server.
7333           (save-excursion
7334             (set-buffer gnus-original-article-buffer)
7335             (nnheader-narrow-to-headers)
7336             (unless (setq ref (message-fetch-field "references"))
7337               (setq ref (message-fetch-field "in-reply-to")))
7338             (widen))
7339         (setq ref
7340               ;; It's not the current article, so we take a bet on
7341               ;; the value we got from the server.
7342               (mail-header-references header)))
7343       (if (and ref
7344                (not (equal ref "")))
7345           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7346             (gnus-message 1 "Couldn't find parent"))
7347         (gnus-message 1 "No references in article %d"
7348                       (gnus-summary-article-number))
7349         (setq error t))
7350       (decf n))
7351     (gnus-summary-position-point)
7352     n))
7353
7354 (defun gnus-summary-refer-references ()
7355   "Fetch all articles mentioned in the References header.
7356 Return the number of articles fetched."
7357   (interactive)
7358   (let ((ref (mail-header-references (gnus-summary-article-header)))
7359         (current (gnus-summary-article-number))
7360         (n 0))
7361     (if (or (not ref)
7362             (equal ref ""))
7363         (error "No References in the current article")
7364       ;; For each Message-ID in the References header...
7365       (while (string-match "<[^>]*>" ref)
7366         (incf n)
7367         ;; ... fetch that article.
7368         (gnus-summary-refer-article
7369          (prog1 (match-string 0 ref)
7370            (setq ref (substring ref (match-end 0))))))
7371       (gnus-summary-goto-subject current)
7372       (gnus-summary-position-point)
7373       n)))
7374
7375 (defun gnus-summary-refer-thread (&optional limit)
7376   "Fetch all articles in the current thread.
7377 If LIMIT (the numerical prefix), fetch that many old headers instead
7378 of what's specified by the `gnus-refer-thread-limit' variable."
7379   (interactive "P")
7380   (let ((id (mail-header-id (gnus-summary-article-header)))
7381         (limit (if limit (prefix-numeric-value limit)
7382                  gnus-refer-thread-limit)))
7383     ;; We want to fetch LIMIT *old* headers, but we also have to
7384     ;; re-fetch all the headers in the current buffer, because many of
7385     ;; them may be undisplayed.  So we adjust LIMIT.
7386     (when (numberp limit)
7387       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7388     (unless (eq gnus-fetch-old-headers 'invisible)
7389       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7390       ;; Retrieve the headers and read them in.
7391       (if (eq (gnus-retrieve-headers
7392                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7393               'nov)
7394           (gnus-build-all-threads)
7395         (error "Can't fetch thread from backends that don't support NOV"))
7396       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7397     (gnus-summary-limit-include-thread id)))
7398
7399 (defun gnus-summary-refer-article (message-id)
7400   "Fetch an article specified by MESSAGE-ID."
7401   (interactive "sMessage-ID: ")
7402   (when (and (stringp message-id)
7403              (not (zerop (length message-id))))
7404     ;; Construct the correct Message-ID if necessary.
7405     ;; Suggested by tale@pawl.rpi.edu.
7406     (unless (string-match "^<" message-id)
7407       (setq message-id (concat "<" message-id)))
7408     (unless (string-match ">$" message-id)
7409       (setq message-id (concat message-id ">")))
7410     (let* ((header (gnus-id-to-header message-id))
7411            (sparse (and header
7412                         (gnus-summary-article-sparse-p
7413                          (mail-header-number header))
7414                         (memq (mail-header-number header)
7415                               gnus-newsgroup-limit)))
7416            number)
7417       (cond
7418        ;; If the article is present in the buffer we just go to it.
7419        ((and header
7420              (or (not (gnus-summary-article-sparse-p
7421                        (mail-header-number header)))
7422                  sparse))
7423         (prog1
7424             (gnus-summary-goto-article
7425              (mail-header-number header) nil t)
7426           (when sparse
7427             (gnus-summary-update-article (mail-header-number header)))))
7428        (t
7429         ;; We fetch the article.
7430         (catch 'found
7431           (dolist (gnus-override-method (gnus-refer-article-methods))
7432             (gnus-check-server gnus-override-method)
7433             ;; Fetch the header, and display the article.
7434             (when (setq number (gnus-summary-insert-subject message-id))
7435               (gnus-summary-select-article nil nil nil number)
7436               (throw 'found t)))
7437           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7438
7439 (defun gnus-refer-article-methods ()
7440   "Return a list of referrable methods."
7441   (cond
7442    ;; No method, so we default to current and native.
7443    ((null gnus-refer-article-method)
7444     (list gnus-current-select-method gnus-select-method))
7445    ;; Current.
7446    ((eq 'current gnus-refer-article-method)
7447     (list gnus-current-select-method))
7448    ;; List of select methods.
7449    ((not (and (symbolp (car gnus-refer-article-method))
7450               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7451     (let (out)
7452       (dolist (method gnus-refer-article-method)
7453         (push (if (eq 'current method)
7454                   gnus-current-select-method
7455                 method)
7456               out))
7457       (nreverse out)))
7458    ;; One single select method.
7459    (t
7460     (list gnus-refer-article-method))))
7461
7462 (defun gnus-summary-edit-parameters ()
7463   "Edit the group parameters of the current group."
7464   (interactive)
7465   (gnus-group-edit-group gnus-newsgroup-name 'params))
7466
7467 (defun gnus-summary-customize-parameters ()
7468   "Customize the group parameters of the current group."
7469   (interactive)
7470   (gnus-group-customize gnus-newsgroup-name))
7471
7472 (defun gnus-summary-enter-digest-group (&optional force)
7473   "Enter an nndoc group based on the current article.
7474 If FORCE, force a digest interpretation.  If not, try
7475 to guess what the document format is."
7476   (interactive "P")
7477   (let ((conf gnus-current-window-configuration))
7478     (save-excursion
7479       (gnus-summary-select-article))
7480     (setq gnus-current-window-configuration conf)
7481     (let* ((name (format "%s-%d"
7482                          (gnus-group-prefixed-name
7483                           gnus-newsgroup-name (list 'nndoc ""))
7484                          (save-excursion
7485                            (set-buffer gnus-summary-buffer)
7486                            gnus-current-article)))
7487            (ogroup gnus-newsgroup-name)
7488            (params (append (gnus-info-params (gnus-get-info ogroup))
7489                            (list (cons 'to-group ogroup))
7490                            (list (cons 'save-article-group ogroup))))
7491            (case-fold-search t)
7492            (buf (current-buffer))
7493            dig to-address)
7494       (save-excursion
7495         (set-buffer gnus-original-article-buffer)
7496         ;; Have the digest group inherit the main mail address of
7497         ;; the parent article.
7498         (when (setq to-address (or (message-fetch-field "reply-to")
7499                                    (message-fetch-field "from")))
7500           (setq params (append
7501                         (list (cons 'to-address
7502                                     (funcall gnus-decode-encoded-word-function
7503                                              to-address))))))
7504         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7505         (insert-buffer-substring gnus-original-article-buffer)
7506         ;; Remove lines that may lead nndoc to misinterpret the
7507         ;; document type.
7508         (narrow-to-region
7509          (goto-char (point-min))
7510          (or (search-forward "\n\n" nil t) (point)))
7511         (goto-char (point-min))
7512         (delete-matching-lines "^Path:\\|^From ")
7513         (widen))
7514       (unwind-protect
7515           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7516                     (gnus-newsgroup-ephemeral-ignored-charsets
7517                      gnus-newsgroup-ignored-charsets))
7518                 (gnus-group-read-ephemeral-group
7519                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7520                               (nndoc-article-type
7521                                ,(if force 'mbox 'guess))) t))
7522               ;; Make all postings to this group go to the parent group.
7523               (nconc (gnus-info-params (gnus-get-info name))
7524                      params)
7525             ;; Couldn't select this doc group.
7526             (switch-to-buffer buf)
7527             (gnus-set-global-variables)
7528             (gnus-configure-windows 'summary)
7529             (gnus-message 3 "Article couldn't be entered?"))
7530         (kill-buffer dig)))))
7531
7532 (defun gnus-summary-read-document (n)
7533   "Open a new group based on the current article(s).
7534 This will allow you to read digests and other similar
7535 documents as newsgroups.
7536 Obeys the standard process/prefix convention."
7537   (interactive "P")
7538   (let* ((articles (gnus-summary-work-articles n))
7539          (ogroup gnus-newsgroup-name)
7540          (params (append (gnus-info-params (gnus-get-info ogroup))
7541                          (list (cons 'to-group ogroup))))
7542          article group egroup groups vgroup)
7543     (while (setq article (pop articles))
7544       (setq group (format "%s-%d" gnus-newsgroup-name article))
7545       (gnus-summary-remove-process-mark article)
7546       (when (gnus-summary-display-article article)
7547         (save-excursion
7548           (with-temp-buffer
7549             (insert-buffer-substring gnus-original-article-buffer)
7550             ;; Remove some headers that may lead nndoc to make
7551             ;; the wrong guess.
7552             (message-narrow-to-head)
7553             (goto-char (point-min))
7554             (delete-matching-lines "^\\(Path\\):\\|^From ")
7555             (widen)
7556             (if (setq egroup
7557                       (gnus-group-read-ephemeral-group
7558                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7559                                      (nndoc-article-type guess))
7560                        t nil t))
7561                 (progn
7562                   ;; Make all postings to this group go to the parent group.
7563                   (nconc (gnus-info-params (gnus-get-info egroup))
7564                          params)
7565                   (push egroup groups))
7566               ;; Couldn't select this doc group.
7567               (gnus-error 3 "Article couldn't be entered"))))))
7568     ;; Now we have selected all the documents.
7569     (cond
7570      ((not groups)
7571       (error "None of the articles could be interpreted as documents"))
7572      ((gnus-group-read-ephemeral-group
7573        (setq vgroup (format
7574                      "nnvirtual:%s-%s" gnus-newsgroup-name
7575                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7576        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7577        t
7578        (cons (current-buffer) 'summary)))
7579      (t
7580       (error "Couldn't select virtual nndoc group")))))
7581
7582 (defun gnus-summary-isearch-article (&optional regexp-p)
7583   "Do incremental search forward on the current article.
7584 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7585   (interactive "P")
7586   (let* ((gnus-inhibit-treatment t)
7587          (old (gnus-summary-select-article)))
7588     (gnus-configure-windows 'article)
7589     (gnus-eval-in-buffer-window gnus-article-buffer
7590       (save-restriction
7591         (widen)
7592         (when (eq 'old old)
7593           (gnus-article-show-all-headers))
7594         (goto-char (point-min))
7595         (isearch-forward regexp-p)))))
7596
7597 (defun gnus-summary-search-article-forward (regexp &optional backward)
7598   "Search for an article containing REGEXP forward.
7599 If BACKWARD, search backward instead."
7600   (interactive
7601    (list (read-string
7602           (format "Search article %s (regexp%s): "
7603                   (if current-prefix-arg "backward" "forward")
7604                   (if gnus-last-search-regexp
7605                       (concat ", default " gnus-last-search-regexp)
7606                     "")))
7607          current-prefix-arg))
7608   (if (string-equal regexp "")
7609       (setq regexp (or gnus-last-search-regexp ""))
7610     (setq gnus-last-search-regexp regexp)
7611     (setq gnus-article-before-search gnus-current-article))
7612   ;; Intentionally set gnus-last-article.
7613   (setq gnus-last-article gnus-article-before-search)
7614   (let ((gnus-last-article gnus-last-article))
7615     (if (gnus-summary-search-article regexp backward)
7616         (gnus-summary-show-thread)
7617       (error "Search failed: \"%s\"" regexp))))
7618
7619 (defun gnus-summary-search-article-backward (regexp)
7620   "Search for an article containing REGEXP backward."
7621   (interactive
7622    (list (read-string
7623           (format "Search article backward (regexp%s): "
7624                   (if gnus-last-search-regexp
7625                       (concat ", default " gnus-last-search-regexp)
7626                     "")))))
7627   (gnus-summary-search-article-forward regexp 'backward))
7628
7629 (eval-when-compile
7630   (defmacro gnus-summary-search-article-position-point (regexp backward)
7631     "Dehighlight the last matched text and goto the beginning position."
7632     (` (if (and gnus-summary-search-article-matched-data
7633                 (let ((text (caddr gnus-summary-search-article-matched-data))
7634                       (inhibit-read-only t)
7635                       buffer-read-only)
7636                   (delete-region
7637                    (goto-char (car gnus-summary-search-article-matched-data))
7638                    (cadr gnus-summary-search-article-matched-data))
7639                   (insert text)
7640                   (string-match (, regexp) text)))
7641            (if (, backward) (beginning-of-line) (end-of-line))
7642          (goto-char (if (, backward) (point-max) (point-min))))))
7643
7644   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7645     "Place point where X-Face image is displayed."
7646     (if (featurep 'xemacs)
7647         (` (let ((end (if (search-forward "\n\n" nil t)
7648                           (goto-char (1- (point)))
7649                         (point-min)))
7650                  extent)
7651              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7652              (unless (and (re-search-forward "^From:" end t)
7653                           (setq extent (extent-at (point)))
7654                           (extent-begin-glyph extent))
7655                (goto-char (, opoint)))))
7656       (` (let ((end (if (search-forward "\n\n" nil t)
7657                         (goto-char (1- (point)))
7658                       (point-min)))
7659                (start (or (search-backward "\n\n" nil t) (point-min))))
7660            (goto-char
7661             (or (text-property-any start end 'x-face-image t);; x-face-e21
7662                 (text-property-any start end 'x-face-mule-bitmap-image t)
7663                 (, opoint)))))))
7664
7665   (defmacro gnus-summary-search-article-highlight-matched-text
7666     (backward treated x-face)
7667     "Highlight matched text in the function `gnus-summary-search-article'."
7668     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7669              (end (set-marker (make-marker) (match-end 0)))
7670              (inhibit-read-only t)
7671              buffer-read-only)
7672          (unless treated
7673            (let ((,@
7674                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7675                     (mapcar
7676                      (lambda (item) (setq items (delq item items)))
7677                      '(gnus-treat-buttonize
7678                        gnus-treat-fill-article
7679                        gnus-treat-fill-long-lines
7680                        gnus-treat-emphasize
7681                        gnus-treat-highlight-headers
7682                        gnus-treat-highlight-citation
7683                        gnus-treat-highlight-signature
7684                        gnus-treat-overstrike
7685                        gnus-treat-display-xface
7686                        gnus-treat-buttonize-head
7687                        gnus-treat-decode-article-as-default-mime-charset))
7688                     (static-if (featurep 'xemacs)
7689                         items
7690                       (cons '(x-face-mule-delete-x-face-field
7691                               (quote never))
7692                             items))))
7693                  (gnus-treat-display-xface
7694                   (when (, x-face) gnus-treat-display-xface)))
7695              (gnus-article-prepare-mime-display)))
7696          (goto-char (if (, backward) start end))
7697          (when (, x-face)
7698            (gnus-summary-search-article-highlight-goto-x-face (point)))
7699          (setq gnus-summary-search-article-matched-data
7700                (list start end (buffer-substring start end)))
7701          (unless (eq start end);; matched text has been deleted. :-<
7702            (put-text-property start end 'face
7703                               (or (find-face 'isearch)
7704                                   'secondary-selection))))))
7705   )
7706
7707 (defun gnus-summary-search-article (regexp &optional backward)
7708   "Search for an article containing REGEXP.
7709 Optional argument BACKWARD means do search for backward.
7710 `gnus-select-article-hook' is not called during the search."
7711   ;; We have to require this here to make sure that the following
7712   ;; dynamic binding isn't shadowed by autoloading.
7713   (require 'gnus-async)
7714   (require 'gnus-art)
7715   (let ((gnus-select-article-hook nil)  ;Disable hook.
7716         (gnus-article-prepare-hook nil)
7717         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7718         (gnus-use-article-prefetch nil)
7719         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7720         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7721         (sum (current-buffer))
7722         (found nil)
7723         point treated)
7724     (gnus-save-hidden-threads
7725       (static-if (featurep 'xemacs)
7726           (let ((gnus-inhibit-treatment t))
7727             (setq treated (eq 'old (gnus-summary-select-article)))
7728             (when (and treated
7729                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7730                                  (window-live-p (get-buffer-window
7731                                                  gnus-article-buffer t)))))
7732               (gnus-summary-select-article nil t)
7733               (setq treated nil)))
7734         (let ((gnus-inhibit-treatment t)
7735               (x-face-mule-delete-x-face-field 'never))
7736           (setq treated (eq 'old (gnus-summary-select-article)))
7737           (when (and treated
7738                      (not
7739                       (and (gnus-buffer-live-p gnus-article-buffer)
7740                            (window-live-p (get-buffer-window
7741                                            gnus-article-buffer t))
7742                            (or (not (string-match "^\\^X-Face:" regexp))
7743                                (with-current-buffer gnus-article-buffer
7744                                  gnus-summary-search-article-matched-data)))))
7745             (gnus-summary-select-article nil t)
7746             (setq treated nil))))
7747       (set-buffer gnus-article-buffer)
7748       (widen)
7749       (if treated
7750           (progn
7751             (gnus-article-show-all-headers)
7752             (gnus-summary-search-article-position-point regexp backward))
7753         (goto-char (if backward (point-max) (point-min))))
7754       (while (not found)
7755         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7756         (if (if backward
7757                 (re-search-backward regexp nil t)
7758               (re-search-forward regexp nil t))
7759             ;; We found the regexp.
7760             (progn
7761               (gnus-summary-search-article-highlight-matched-text
7762                backward treated (string-match "^\\^X-Face:" regexp))
7763               (setq found 'found)
7764               (forward-line
7765                (/ (- 2 (window-height
7766                         (get-buffer-window gnus-article-buffer t)))
7767                   2))
7768               (set-window-start
7769                (get-buffer-window (current-buffer))
7770                (point))
7771               (set-buffer sum)
7772               (setq point (point)))
7773           ;; We didn't find it, so we go to the next article.
7774           (set-buffer sum)
7775           (setq found 'not)
7776           (while (eq found 'not)
7777             (if (not (if backward (gnus-summary-find-prev)
7778                        (gnus-summary-find-next)))
7779                 ;; No more articles.
7780                 (setq found t)
7781               ;; Select the next article and adjust point.
7782               (unless (gnus-summary-article-sparse-p
7783                        (gnus-summary-article-number))
7784                 (setq found nil)
7785                 (let ((gnus-inhibit-treatment t))
7786                   (gnus-summary-select-article))
7787                 (setq treated nil)
7788                 (set-buffer gnus-article-buffer)
7789                 (widen)
7790                 (goto-char (if backward (point-max) (point-min))))))))
7791       (gnus-message 7 ""))
7792     ;; Return whether we found the regexp.
7793     (when (eq found 'found)
7794       (goto-char point)
7795       (gnus-summary-show-thread)
7796       (gnus-summary-goto-subject gnus-current-article)
7797       (gnus-summary-position-point)
7798       t)))
7799
7800 (defun gnus-find-matching-articles (header regexp)
7801   "Return a list of all articles that match REGEXP on HEADER.
7802 This search includes all articles in the current group that Gnus has
7803 fetched headers for, whether they are displayed or not."
7804   (let ((articles nil)
7805         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7806         (case-fold-search t))
7807     (dolist (header gnus-newsgroup-headers)
7808       (when (string-match regexp (funcall func header))
7809         (push (mail-header-number header) articles)))
7810     (nreverse articles)))
7811
7812 (defun gnus-summary-find-matching (header regexp &optional backward unread
7813                                           not-case-fold)
7814   "Return a list of all articles that match REGEXP on HEADER.
7815 The search stars on the current article and goes forwards unless
7816 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7817 If UNREAD is non-nil, only unread articles will
7818 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7819 in the comparisons."
7820   (let ((case-fold-search (not not-case-fold))
7821         articles d func)
7822     (if (consp header)
7823         (if (eq (car header) 'extra)
7824             (setq func
7825                   `(lambda (h)
7826                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7827                          "")))
7828           (error "%s is an invalid header" header))
7829       (unless (fboundp (intern (concat "mail-header-" header)))
7830         (error "%s is not a valid header" header))
7831       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7832     (dolist (d (if (eq backward 'all)
7833                    gnus-newsgroup-data
7834                  (gnus-data-find-list
7835                   (gnus-summary-article-number)
7836                   (gnus-data-list backward))))
7837       (when (and (or (not unread)       ; We want all articles...
7838                      (gnus-data-unread-p d)) ; Or just unreads.
7839                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7840                  (string-match regexp
7841                                (funcall func (gnus-data-header d)))) ; Match.
7842         (push (gnus-data-number d) articles))) ; Success!
7843     (nreverse articles)))
7844
7845 (defun gnus-summary-execute-command (header regexp command &optional backward)
7846   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7847 If HEADER is an empty string (or nil), the match is done on the entire
7848 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7849   (interactive
7850    (list (let ((completion-ignore-case t))
7851            (completing-read
7852             "Header name: "
7853             (mapcar (lambda (header) (list (format "%s" header)))
7854                     (append
7855                      '("Number" "Subject" "From" "Lines" "Date"
7856                        "Message-ID" "Xref" "References" "Body")
7857                      gnus-extra-headers))
7858             nil 'require-match))
7859          (read-string "Regexp: ")
7860          (read-key-sequence "Command: ")
7861          current-prefix-arg))
7862   (when (equal header "Body")
7863     (setq header ""))
7864   ;; Hidden thread subtrees must be searched as well.
7865   (gnus-summary-show-all-threads)
7866   ;; We don't want to change current point nor window configuration.
7867   (save-excursion
7868     (save-window-excursion
7869       (gnus-message 6 "Executing %s..." (key-description command))
7870       ;; We'd like to execute COMMAND interactively so as to give arguments.
7871       (gnus-execute header regexp
7872                     `(call-interactively ',(key-binding command))
7873                     backward)
7874       (gnus-message 6 "Executing %s...done" (key-description command)))))
7875
7876 (defun gnus-summary-beginning-of-article ()
7877   "Scroll the article back to the beginning."
7878   (interactive)
7879   (gnus-summary-select-article)
7880   (gnus-configure-windows 'article)
7881   (gnus-eval-in-buffer-window gnus-article-buffer
7882     (widen)
7883     (goto-char (point-min))
7884     (when gnus-page-broken
7885       (gnus-narrow-to-page))))
7886
7887 (defun gnus-summary-end-of-article ()
7888   "Scroll to the end of the article."
7889   (interactive)
7890   (gnus-summary-select-article)
7891   (gnus-configure-windows 'article)
7892   (gnus-eval-in-buffer-window gnus-article-buffer
7893     (widen)
7894     (goto-char (point-max))
7895     (recenter -3)
7896     (when gnus-page-broken
7897       (gnus-narrow-to-page))))
7898
7899 (defun gnus-summary-print-article (&optional filename n)
7900   "Generate and print a PostScript image of the N next (mail) articles.
7901
7902 If N is negative, print the N previous articles.  If N is nil and articles
7903 have been marked with the process mark, print these instead.
7904
7905 If the optional first argument FILENAME is nil, send the image to the
7906 printer.  If FILENAME is a string, save the PostScript image in a file with
7907 that name.  If FILENAME is a number, prompt the user for the name of the file
7908 to save in."
7909   (interactive (list (ps-print-preprint current-prefix-arg)))
7910   (dolist (article (gnus-summary-work-articles n))
7911     (gnus-summary-select-article nil nil 'pseudo article)
7912     (gnus-eval-in-buffer-window gnus-article-buffer
7913       (let ((buffer (generate-new-buffer " *print*")))
7914         (unwind-protect
7915             (progn
7916               (copy-to-buffer buffer (point-min) (point-max))
7917               (set-buffer buffer)
7918               (gnus-article-delete-invisible-text)
7919               (when (gnus-visual-p 'article-highlight 'highlight)
7920                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7921                 ;; highlight.
7922                 (let ((gnus-article-buffer buffer))
7923                   (gnus-article-highlight-citation t)
7924                   (gnus-article-highlight-signature)))
7925               (let ((ps-left-header
7926                      (list
7927                       (concat "("
7928                               (mail-header-subject gnus-current-headers) ")")
7929                       (concat "("
7930                               (mail-header-from gnus-current-headers) ")")))
7931                     (ps-right-header
7932                      (list
7933                       "/pagenumberstring load"
7934                       (concat "("
7935                               (mail-header-date gnus-current-headers) ")"))))
7936                 (gnus-run-hooks 'gnus-ps-print-hook)
7937                 (save-excursion
7938                   (if window-system
7939                       (ps-spool-buffer-with-faces)
7940                     (ps-spool-buffer)))))
7941           (kill-buffer buffer))))
7942     (gnus-summary-remove-process-mark article))
7943   (ps-despool filename))
7944
7945 (defun gnus-summary-show-article (&optional arg)
7946   "Force re-fetching of the current article.
7947 If ARG (the prefix) is a number, show the article with the charset
7948 defined in `gnus-summary-show-article-charset-alist', or the charset
7949 input.
7950 If ARG (the prefix) is non-nil and not a number, show the raw article
7951 without any article massaging functions being run."
7952   (interactive "P")
7953   (cond
7954    ((numberp arg)
7955     (gnus-summary-show-article t)
7956     (let ((gnus-newsgroup-charset
7957            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7958                (mm-read-coding-system
7959                 "View as charset: "
7960                 (save-excursion
7961                   (set-buffer gnus-article-buffer)
7962                   (detect-coding-region (point) (point-max) t)))))
7963           (gnus-newsgroup-ignored-charsets 'gnus-all))
7964       (gnus-summary-select-article nil 'force)
7965       (let ((deps gnus-newsgroup-dependencies)
7966             head header)
7967         (save-excursion
7968           (set-buffer gnus-original-article-buffer)
7969           (save-restriction
7970             (message-narrow-to-head)
7971             (setq head (buffer-string)))
7972           (with-temp-buffer
7973             (insert (format "211 %d Article retrieved.\n"
7974                             (cdr gnus-article-current)))
7975             (insert head)
7976             (insert ".\n")
7977             (let ((nntp-server-buffer (current-buffer)))
7978               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7979         (gnus-data-set-header
7980          (gnus-data-find (cdr gnus-article-current))
7981          header)
7982         (gnus-summary-update-article-line
7983          (cdr gnus-article-current) header)
7984         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7985           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7986    ((not arg)
7987     ;; Select the article the normal way.
7988     (gnus-summary-select-article nil 'force))
7989    (t
7990     ;; We have to require this here to make sure that the following
7991     ;; dynamic binding isn't shadowed by autoloading.
7992     (require 'gnus-async)
7993     (require 'gnus-art)
7994     ;; Bind the article treatment functions to nil.
7995     (let ((gnus-have-all-headers t)
7996           gnus-article-prepare-hook
7997           gnus-article-decode-hook
7998           gnus-break-pages
7999           gnus-show-mime
8000           (gnus-inhibit-treatment t))
8001       (gnus-summary-select-article nil 'force))))
8002   (gnus-summary-goto-subject gnus-current-article)
8003   (gnus-summary-position-point))
8004
8005 (defun gnus-summary-show-raw-article ()
8006   "Show the raw article without any article massaging functions being run."
8007   (interactive)
8008   (gnus-summary-show-article t))
8009
8010 (defun gnus-summary-verbose-headers (&optional arg)
8011   "Toggle permanent full header display.
8012 If ARG is a positive number, turn header display on.
8013 If ARG is a negative number, turn header display off."
8014   (interactive "P")
8015   (setq gnus-show-all-headers
8016         (cond ((or (not (numberp arg))
8017                    (zerop arg))
8018                (not gnus-show-all-headers))
8019               ((natnump arg)
8020                t)))
8021   (gnus-summary-show-article))
8022
8023 (defun gnus-summary-toggle-header (&optional arg)
8024   "Show the headers if they are hidden, or hide them if they are shown.
8025 If ARG is a positive number, show the entire header.
8026 If ARG is a negative number, hide the unwanted header lines."
8027   (interactive "P")
8028   (save-excursion
8029     (set-buffer gnus-article-buffer)
8030     (save-restriction
8031       (let* ((buffer-read-only nil)
8032              (inhibit-point-motion-hooks t)
8033              hidden e)
8034         (setq hidden
8035               (if (numberp arg)
8036                   (>= arg 0)
8037                 (save-restriction
8038                   (article-narrow-to-head)
8039                   (gnus-article-hidden-text-p 'headers))))
8040         (goto-char (point-min))
8041         (when (search-forward "\n\n" nil t)
8042           (delete-region (point-min) (1- (point))))
8043         (goto-char (point-min))
8044         (save-excursion
8045           (set-buffer gnus-original-article-buffer)
8046           (goto-char (point-min))
8047           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8048         (insert-buffer-substring gnus-original-article-buffer 1 e)
8049         (save-restriction
8050           (narrow-to-region (point-min) (point))
8051           (article-decode-encoded-words)
8052           (if  hidden
8053               (let ((gnus-treat-hide-headers nil)
8054                     (gnus-treat-hide-boring-headers nil))
8055                 (setq gnus-article-wash-types
8056                       (delq 'headers gnus-article-wash-types))
8057                 (gnus-treat-article 'head))
8058             (gnus-treat-article 'head)))
8059         (gnus-set-mode-line 'article)))))
8060
8061 (defun gnus-summary-show-all-headers ()
8062   "Make all header lines visible."
8063   (interactive)
8064   (gnus-summary-toggle-header 1))
8065
8066 (defun gnus-summary-toggle-mime (&optional arg)
8067   "Toggle MIME processing.
8068 If ARG is a positive number, turn MIME processing on."
8069   (interactive "P")
8070   (setq gnus-show-mime
8071         (if (null arg)
8072             (not gnus-show-mime)
8073           (> (prefix-numeric-value arg) 0)))
8074   (gnus-summary-select-article t 'force))
8075
8076 (defun gnus-summary-caesar-message (&optional arg)
8077   "Caesar rotate the current article by 13.
8078 The numerical prefix specifies how many places to rotate each letter
8079 forward."
8080   (interactive "P")
8081   (gnus-summary-select-article)
8082   (let ((mail-header-separator ""))
8083     (gnus-eval-in-buffer-window gnus-article-buffer
8084       (save-restriction
8085         (widen)
8086         (let ((start (window-start))
8087               buffer-read-only)
8088           (message-caesar-buffer-body arg)
8089           (set-window-start (get-buffer-window (current-buffer)) start))))))
8090
8091 (defun gnus-summary-stop-page-breaking ()
8092   "Stop page breaking in the current article."
8093   (interactive)
8094   (gnus-summary-select-article)
8095   (gnus-eval-in-buffer-window gnus-article-buffer
8096     (widen)
8097     (when (gnus-visual-p 'page-marker)
8098       (let ((buffer-read-only nil))
8099         (gnus-remove-text-with-property 'gnus-prev)
8100         (gnus-remove-text-with-property 'gnus-next))
8101       (setq gnus-page-broken nil))))
8102
8103 (defun gnus-summary-move-article (&optional n to-newsgroup
8104                                             select-method action)
8105   "Move the current article to a different newsgroup.
8106 If N is a positive number, move the N next articles.
8107 If N is a negative number, move the N previous articles.
8108 If N is nil and any articles have been marked with the process mark,
8109 move those articles instead.
8110 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8111 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8112 re-spool using this method.
8113
8114 For this function to work, both the current newsgroup and the
8115 newsgroup that you want to move to have to support the `request-move'
8116 and `request-accept' functions.
8117
8118 ACTION can be either `move' (the default), `crosspost' or `copy'."
8119   (interactive "P")
8120   (unless action
8121     (setq action 'move))
8122   ;; Check whether the source group supports the required functions.
8123   (cond ((and (eq action 'move)
8124               (not (gnus-check-backend-function
8125                     'request-move-article gnus-newsgroup-name)))
8126          (error "The current group does not support article moving"))
8127         ((and (eq action 'crosspost)
8128               (not (gnus-check-backend-function
8129                     'request-replace-article gnus-newsgroup-name)))
8130          (error "The current group does not support article editing")))
8131   (let ((articles (gnus-summary-work-articles n))
8132         (prefix (if (gnus-check-backend-function
8133                      'request-move-article gnus-newsgroup-name)
8134                     (gnus-group-real-prefix gnus-newsgroup-name)
8135                   ""))
8136         (names '((move "Move" "Moving")
8137                  (copy "Copy" "Copying")
8138                  (crosspost "Crosspost" "Crossposting")))
8139         (copy-buf (save-excursion
8140                     (nnheader-set-temp-buffer " *copy article*")))
8141         (default-marks gnus-article-mark-lists)
8142         (no-expire-marks (delete '(expirable . expire)
8143                                  (copy-sequence gnus-article-mark-lists)))
8144         art-group to-method new-xref article to-groups)
8145     (unless (assq action names)
8146       (error "Unknown action %s" action))
8147     ;; Read the newsgroup name.
8148     (when (and (not to-newsgroup)
8149                (not select-method))
8150       (setq to-newsgroup
8151             (gnus-read-move-group-name
8152              (cadr (assq action names))
8153              (symbol-value (intern (format "gnus-current-%s-group" action)))
8154              articles prefix))
8155       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8156     (setq to-method (or select-method
8157                         (gnus-server-to-method
8158                          (gnus-group-method to-newsgroup))))
8159     ;; Check the method we are to move this article to...
8160     (unless (gnus-check-backend-function
8161              'request-accept-article (car to-method))
8162       (error "%s does not support article copying" (car to-method)))
8163     (unless (gnus-check-server to-method)
8164       (error "Can't open server %s" (car to-method)))
8165     (gnus-message 6 "%s to %s: %s..."
8166                   (caddr (assq action names))
8167                   (or (car select-method) to-newsgroup) articles)
8168     (while articles
8169       (setq article (pop articles))
8170       (setq
8171        art-group
8172        (cond
8173         ;; Move the article.
8174         ((eq action 'move)
8175          ;; Remove this article from future suppression.
8176          (gnus-dup-unsuppress-article article)
8177          (gnus-request-move-article
8178           article                       ; Article to move
8179           gnus-newsgroup-name           ; From newsgroup
8180           (nth 1 (gnus-find-method-for-group
8181                   gnus-newsgroup-name)) ; Server
8182           (list 'gnus-request-accept-article
8183                 to-newsgroup (list 'quote select-method)
8184                 (not articles) t)       ; Accept form
8185           (not articles)))              ; Only save nov last time
8186         ;; Copy the article.
8187         ((eq action 'copy)
8188          (save-excursion
8189            (set-buffer copy-buf)
8190            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8191              (gnus-request-accept-article
8192               to-newsgroup select-method (not articles) t))))
8193         ;; Crosspost the article.
8194         ((eq action 'crosspost)
8195          (let ((xref (message-tokenize-header
8196                       (mail-header-xref (gnus-summary-article-header article))
8197                       " ")))
8198            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8199                                   ":" article))
8200            (unless xref
8201              (setq xref (list (system-name))))
8202            (setq new-xref
8203                  (concat
8204                   (mapconcat 'identity
8205                              (delete "Xref:" (delete new-xref xref))
8206                              " ")
8207                   " " new-xref))
8208            (save-excursion
8209              (set-buffer copy-buf)
8210              ;; First put the article in the destination group.
8211              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8212              (when (consp (setq art-group
8213                                 (gnus-request-accept-article
8214                                  to-newsgroup select-method (not articles))))
8215                (setq new-xref (concat new-xref " " (car art-group)
8216                                       ":" (cdr art-group)))
8217                ;; Now we have the new Xrefs header, so we insert
8218                ;; it and replace the new article.
8219                (nnheader-replace-header "Xref" new-xref)
8220                (gnus-request-replace-article
8221                 (cdr art-group) to-newsgroup (current-buffer))
8222                art-group))))))
8223       (cond
8224        ((not art-group)
8225         (gnus-message 1 "Couldn't %s article %s: %s"
8226                       (cadr (assq action names)) article
8227                       (nnheader-get-report (car to-method))))
8228        ((eq art-group 'junk)
8229         (when (eq action 'move)
8230           (gnus-summary-mark-article article gnus-canceled-mark)
8231           (gnus-message 4 "Deleted article %s" article)))
8232        (t
8233         (let* ((pto-group (gnus-group-prefixed-name
8234                            (car art-group) to-method))
8235                (entry
8236                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8237                (info (nth 2 entry))
8238                (to-group (gnus-info-group info))
8239                to-marks)
8240           ;; Update the group that has been moved to.
8241           (when (and info
8242                      (memq action '(move copy)))
8243             (unless (member to-group to-groups)
8244               (push to-group to-groups))
8245
8246             (unless (memq article gnus-newsgroup-unreads)
8247               (push 'read to-marks)
8248               (gnus-info-set-read
8249                info (gnus-add-to-range (gnus-info-read info)
8250                                        (list (cdr art-group)))))
8251
8252             ;; See whether the article is to be put in the cache.
8253             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8254                              default-marks
8255                            no-expire-marks))
8256                   (to-article (cdr art-group)))
8257
8258               ;; Enter the article into the cache in the new group,
8259               ;; if that is required.
8260               (when gnus-use-cache
8261                 (gnus-cache-possibly-enter-article
8262                  to-group to-article
8263                  (let ((header (copy-sequence
8264                                 (gnus-summary-article-header article))))
8265                    (mail-header-set-number header to-article)
8266                    header)
8267                  (memq article gnus-newsgroup-marked)
8268                  (memq article gnus-newsgroup-dormant)
8269                  (memq article gnus-newsgroup-unreads)))
8270
8271               (when gnus-preserve-marks
8272                 ;; Copy any marks over to the new group.
8273                 (when (and (equal to-group gnus-newsgroup-name)
8274                            (not (memq article gnus-newsgroup-unreads)))
8275                   ;; Mark this article as read in this group.
8276                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8277                   (setcdr (gnus-active to-group) to-article)
8278                   (setcdr gnus-newsgroup-active to-article))
8279
8280                 (while marks
8281                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8282                     (when (memq article (symbol-value
8283                                          (intern (format "gnus-newsgroup-%s"
8284                                                          (caar marks)))))
8285                       (push (cdar marks) to-marks)
8286                       ;; If the other group is the same as this group,
8287                       ;; then we have to add the mark to the list.
8288                       (when (equal to-group gnus-newsgroup-name)
8289                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8290                              (cons to-article
8291                                    (symbol-value
8292                                     (intern (format "gnus-newsgroup-%s"
8293                                                     (caar marks)))))))
8294                       ;; Copy the marks to other group.
8295                       (gnus-add-marked-articles
8296                        to-group (cdar marks) (list to-article) info)))
8297                   (setq marks (cdr marks)))
8298
8299                 (gnus-request-set-mark to-group (list (list (list to-article)
8300                                                             'add
8301                                                             to-marks))))
8302
8303               (gnus-dribble-enter
8304                (concat "(gnus-group-set-info '"
8305                        (gnus-prin1-to-string (gnus-get-info to-group))
8306                        ")"))))
8307
8308           ;; Update the Xref header in this article to point to
8309           ;; the new crossposted article we have just created.
8310           (when (eq action 'crosspost)
8311             (save-excursion
8312               (set-buffer copy-buf)
8313               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8314               (nnheader-replace-header "Xref" new-xref)
8315               (gnus-request-replace-article
8316                article gnus-newsgroup-name (current-buffer)))))
8317
8318         ;;;!!!Why is this necessary?
8319         (set-buffer gnus-summary-buffer)
8320
8321         (gnus-summary-goto-subject article)
8322         (when (eq action 'move)
8323           (gnus-summary-mark-article article gnus-canceled-mark))))
8324       (gnus-summary-remove-process-mark article))
8325     ;; Re-activate all groups that have been moved to.
8326     (while to-groups
8327       (save-excursion
8328         (set-buffer gnus-group-buffer)
8329         (when (gnus-group-goto-group (car to-groups) t)
8330           (gnus-group-get-new-news-this-group 1 t))
8331         (pop to-groups)))
8332
8333     (gnus-kill-buffer copy-buf)
8334     (gnus-summary-position-point)
8335     (gnus-set-mode-line 'summary)))
8336
8337 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8338   "Move the current article to a different newsgroup.
8339 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8340 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8341 re-spool using this method."
8342   (interactive "P")
8343   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8344
8345 (defun gnus-summary-crosspost-article (&optional n)
8346   "Crosspost the current article to some other group."
8347   (interactive "P")
8348   (gnus-summary-move-article n nil nil 'crosspost))
8349
8350 (defcustom gnus-summary-respool-default-method nil
8351   "Default method for respooling an article.
8352 If nil, use to the current newsgroup method."
8353   :type '(choice (gnus-select-method :value (nnml ""))
8354                  (const nil))
8355   :group 'gnus-summary-mail)
8356
8357 (defun gnus-summary-respool-article (&optional n method)
8358   "Respool the current article.
8359 The article will be squeezed through the mail spooling process again,
8360 which means that it will be put in some mail newsgroup or other
8361 depending on `nnmail-split-methods'.
8362 If N is a positive number, respool the N next articles.
8363 If N is a negative number, respool the N previous articles.
8364 If N is nil and any articles have been marked with the process mark,
8365 respool those articles instead.
8366
8367 Respooling can be done both from mail groups and \"real\" newsgroups.
8368 In the former case, the articles in question will be moved from the
8369 current group into whatever groups they are destined to.  In the
8370 latter case, they will be copied into the relevant groups."
8371   (interactive
8372    (list current-prefix-arg
8373          (let* ((methods (gnus-methods-using 'respool))
8374                 (methname
8375                  (symbol-name (or gnus-summary-respool-default-method
8376                                   (car (gnus-find-method-for-group
8377                                         gnus-newsgroup-name)))))
8378                 (method
8379                  (gnus-completing-read
8380                   methname "What backend do you want to use when respooling?"
8381                   methods nil t nil 'gnus-mail-method-history))
8382                 ms)
8383            (cond
8384             ((zerop (length (setq ms (gnus-servers-using-backend
8385                                       (intern method)))))
8386              (list (intern method) ""))
8387             ((= 1 (length ms))
8388              (car ms))
8389             (t
8390              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8391                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8392                            ms-alist))))))))
8393   (unless method
8394     (error "No method given for respooling"))
8395   (if (assoc (symbol-name
8396               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8397              (gnus-methods-using 'respool))
8398       (gnus-summary-move-article n nil method)
8399     (gnus-summary-copy-article n nil method)))
8400
8401 (defun gnus-summary-import-article (file &optional edit)
8402   "Import an arbitrary file into a mail newsgroup."
8403   (interactive "fImport file: \nP")
8404   (let ((group gnus-newsgroup-name)
8405         (now (current-time))
8406         atts lines group-art)
8407     (unless (gnus-check-backend-function 'request-accept-article group)
8408       (error "%s does not support article importing" group))
8409     (or (file-readable-p file)
8410         (not (file-regular-p file))
8411         (error "Can't read %s" file))
8412     (save-excursion
8413       (set-buffer (gnus-get-buffer-create " *import file*"))
8414       (erase-buffer)
8415       (nnheader-insert-file-contents file)
8416       (goto-char (point-min))
8417       (if (nnheader-article-p)
8418           (save-restriction
8419             (goto-char (point-min))
8420             (search-forward "\n\n" nil t)
8421             (narrow-to-region (point-min) (1- (point)))
8422             (goto-char (point-min))
8423             (unless (re-search-forward "^date:" nil t)
8424               (goto-char (point-max))
8425               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8426         ;; This doesn't look like an article, so we fudge some headers.
8427         (setq atts (file-attributes file)
8428               lines (count-lines (point-min) (point-max)))
8429         (insert "From: " (read-string "From: ") "\n"
8430                 "Subject: " (read-string "Subject: ") "\n"
8431                 "Date: " (message-make-date (nth 5 atts)) "\n"
8432                 "Message-ID: " (message-make-message-id) "\n"
8433                 "Lines: " (int-to-string lines) "\n"
8434                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8435       (setq group-art (gnus-request-accept-article group nil t))
8436       (kill-buffer (current-buffer)))
8437     (setq gnus-newsgroup-active (gnus-activate-group group))
8438     (forward-line 1)
8439     (gnus-summary-goto-article (cdr group-art) nil t)
8440     (when edit
8441       (gnus-summary-edit-article))))
8442
8443 (defun gnus-summary-create-article ()
8444   "Create an article in a mail newsgroup."
8445   (interactive)
8446   (let ((group gnus-newsgroup-name)
8447         (now (current-time))
8448         group-art)
8449     (unless (gnus-check-backend-function 'request-accept-article group)
8450       (error "%s does not support article importing" group))
8451     (save-excursion
8452       (set-buffer (gnus-get-buffer-create " *import file*"))
8453       (erase-buffer)
8454       (goto-char (point-min))
8455       ;; This doesn't look like an article, so we fudge some headers.
8456       (insert "From: " (read-string "From: ") "\n"
8457               "Subject: " (read-string "Subject: ") "\n"
8458               "Date: " (message-make-date now) "\n"
8459               "Message-ID: " (message-make-message-id) "\n")
8460       (setq group-art (gnus-request-accept-article group nil t))
8461       (kill-buffer (current-buffer)))
8462     (setq gnus-newsgroup-active (gnus-activate-group group))
8463     (forward-line 1)
8464     (gnus-summary-goto-article (cdr group-art) nil t)
8465     (gnus-summary-edit-article)))
8466
8467 (defun gnus-summary-article-posted-p ()
8468   "Say whether the current (mail) article is available from news as well.
8469 This will be the case if the article has both been mailed and posted."
8470   (interactive)
8471   (let ((id (mail-header-references (gnus-summary-article-header)))
8472         (gnus-override-method (car (gnus-refer-article-methods))))
8473     (if (gnus-request-head id "")
8474         (gnus-message 2 "The current message was found on %s"
8475                       gnus-override-method)
8476       (gnus-message 2 "The current message couldn't be found on %s"
8477                     gnus-override-method)
8478       nil)))
8479
8480 (defun gnus-summary-expire-articles (&optional now)
8481   "Expire all articles that are marked as expirable in the current group."
8482   (interactive)
8483   (when (gnus-check-backend-function
8484          'request-expire-articles gnus-newsgroup-name)
8485     ;; This backend supports expiry.
8486     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8487            (expirable (if total
8488                           (progn
8489                             ;; We need to update the info for
8490                             ;; this group for `gnus-list-of-read-articles'
8491                             ;; to give us the right answer.
8492                             (gnus-run-hooks 'gnus-exit-group-hook)
8493                             (gnus-summary-update-info)
8494                             (gnus-list-of-read-articles gnus-newsgroup-name))
8495                         (setq gnus-newsgroup-expirable
8496                               (sort gnus-newsgroup-expirable '<))))
8497            (expiry-wait (if now 'immediate
8498                           (gnus-group-find-parameter
8499                            gnus-newsgroup-name 'expiry-wait)))
8500            (nnmail-expiry-target
8501             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8502                 nnmail-expiry-target))
8503            es)
8504       (when expirable
8505         ;; There are expirable articles in this group, so we run them
8506         ;; through the expiry process.
8507         (gnus-message 6 "Expiring articles...")
8508         (unless (gnus-check-group gnus-newsgroup-name)
8509           (error "Can't open server for %s" gnus-newsgroup-name))
8510         ;; The list of articles that weren't expired is returned.
8511         (save-excursion
8512           (if expiry-wait
8513               (let ((nnmail-expiry-wait-function nil)
8514                     (nnmail-expiry-wait expiry-wait))
8515                 (setq es (gnus-request-expire-articles
8516                           expirable gnus-newsgroup-name)))
8517             (setq es (gnus-request-expire-articles
8518                       expirable gnus-newsgroup-name)))
8519           (unless total
8520             (setq gnus-newsgroup-expirable es))
8521           ;; We go through the old list of expirable, and mark all
8522           ;; really expired articles as nonexistent.
8523           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8524             (let ((gnus-use-cache nil))
8525               (while expirable
8526                 (unless (memq (car expirable) es)
8527                   (when (gnus-data-find (car expirable))
8528                     (gnus-summary-mark-article
8529                      (car expirable) gnus-canceled-mark)))
8530                 (setq expirable (cdr expirable))))))
8531         (gnus-message 6 "Expiring articles...done")))))
8532
8533 (defun gnus-summary-expire-articles-now ()
8534   "Expunge all expirable articles in the current group.
8535 This means that *all* articles that are marked as expirable will be
8536 deleted forever, right now."
8537   (interactive)
8538   (or gnus-expert-user
8539       (gnus-yes-or-no-p
8540        "Are you really, really, really sure you want to delete all these messages? ")
8541       (error "Phew!"))
8542   (gnus-summary-expire-articles t))
8543
8544 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8545 (defun gnus-summary-delete-article (&optional n)
8546   "Delete the N next (mail) articles.
8547 This command actually deletes articles.  This is not a marking
8548 command.  The article will disappear forever from your life, never to
8549 return.
8550 If N is negative, delete backwards.
8551 If N is nil and articles have been marked with the process mark,
8552 delete these instead."
8553   (interactive "P")
8554   (unless (gnus-check-backend-function 'request-expire-articles
8555                                        gnus-newsgroup-name)
8556     (error "The current newsgroup does not support article deletion"))
8557   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8558     (error "Couldn't open server"))
8559   ;; Compute the list of articles to delete.
8560   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8561         not-deleted)
8562     (if (and gnus-novice-user
8563              (not (gnus-yes-or-no-p
8564                    (format "Do you really want to delete %s forever? "
8565                            (if (> (length articles) 1)
8566                                (format "these %s articles" (length articles))
8567                              "this article")))))
8568         ()
8569       ;; Delete the articles.
8570       (setq not-deleted (gnus-request-expire-articles
8571                          articles gnus-newsgroup-name 'force))
8572       (while articles
8573         (gnus-summary-remove-process-mark (car articles))
8574         ;; The backend might not have been able to delete the article
8575         ;; after all.
8576         (unless (memq (car articles) not-deleted)
8577           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8578         (setq articles (cdr articles)))
8579       (when not-deleted
8580         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8581     (gnus-summary-position-point)
8582     (gnus-set-mode-line 'summary)
8583     not-deleted))
8584
8585 (defun gnus-summary-edit-article (&optional force)
8586   "Edit the current article.
8587 This will have permanent effect only in mail groups.
8588 If FORCE is non-nil, allow editing of articles even in read-only
8589 groups."
8590   (interactive "P")
8591   (save-excursion
8592     (set-buffer gnus-summary-buffer)
8593     (let ((mail-parse-charset gnus-newsgroup-charset)
8594           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8595       (gnus-set-global-variables)
8596       (when (and (not force)
8597                  (gnus-group-read-only-p))
8598         (error "The current newsgroup does not support article editing"))
8599       (gnus-summary-show-article t)
8600       (gnus-article-edit-article
8601        'ignore
8602        `(lambda (no-highlight)
8603           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8604                 (message-options message-options)
8605                 (message-options-set-recipient)
8606                 (mail-parse-ignored-charsets
8607                  ',gnus-newsgroup-ignored-charsets))
8608             (gnus-summary-edit-article-done
8609              ,(or (mail-header-references gnus-current-headers) "")
8610              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8611
8612 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8613
8614 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8615                                                  no-highlight)
8616   "Make edits to the current article permanent."
8617   (interactive)
8618   (save-excursion
8619     ;; The buffer restriction contains the entire article if it exists.
8620     (when (article-goto-body)
8621       (let ((lines (count-lines (point) (point-max)))
8622             (length (- (point-max) (point)))
8623             (case-fold-search t)
8624             (body (copy-marker (point))))
8625         (goto-char (point-min))
8626         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8627           (delete-region (match-beginning 1) (match-end 1))
8628           (insert (number-to-string length)))
8629         (goto-char (point-min))
8630         (when (re-search-forward
8631                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8632           (delete-region (match-beginning 1) (match-end 1))
8633           (insert (number-to-string length)))
8634         (goto-char (point-min))
8635         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8636           (delete-region (match-beginning 1) (match-end 1))
8637           (insert (number-to-string lines))))))
8638   ;; Replace the article.
8639   (let ((buf (current-buffer)))
8640     (with-temp-buffer
8641       (insert-buffer-substring buf)
8642
8643       (if (and (not read-only)
8644                (not (gnus-request-replace-article
8645                      (cdr gnus-article-current) (car gnus-article-current)
8646                      (current-buffer) t)))
8647           (error "Couldn't replace article")
8648         ;; Update the summary buffer.
8649         (if (and references
8650                  (equal (message-tokenize-header references " ")
8651                         (message-tokenize-header
8652                          (or (message-fetch-field "references") "") " ")))
8653             ;; We only have to update this line.
8654             (save-excursion
8655               (save-restriction
8656                 (message-narrow-to-head)
8657                 (let ((head (buffer-string))
8658                       header)
8659                   (with-temp-buffer
8660                     (insert (format "211 %d Article retrieved.\n"
8661                                     (cdr gnus-article-current)))
8662                     (insert head)
8663                     (insert ".\n")
8664                     (let ((nntp-server-buffer (current-buffer)))
8665                       (setq header (car (gnus-get-newsgroup-headers
8666                                          (save-excursion
8667                                            (set-buffer gnus-summary-buffer)
8668                                            gnus-newsgroup-dependencies)
8669                                          t))))
8670                     (save-excursion
8671                       (set-buffer gnus-summary-buffer)
8672                       (gnus-data-set-header
8673                        (gnus-data-find (cdr gnus-article-current))
8674                        header)
8675                       (gnus-summary-update-article-line
8676                        (cdr gnus-article-current) header)
8677                       (if (gnus-summary-goto-subject
8678                            (cdr gnus-article-current) nil t)
8679                           (gnus-summary-update-secondary-mark
8680                            (cdr gnus-article-current))))))))
8681           ;; Update threads.
8682           (set-buffer (or buffer gnus-summary-buffer))
8683           (gnus-summary-update-article (cdr gnus-article-current))
8684           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8685               (gnus-summary-update-secondary-mark
8686                (cdr gnus-article-current))))
8687         ;; Prettify the article buffer again.
8688         (unless no-highlight
8689           (save-excursion
8690             (set-buffer gnus-article-buffer)
8691             ;;;!!! Fix this -- article should be rehighlighted.
8692             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8693             (set-buffer gnus-original-article-buffer)
8694             (gnus-request-article
8695              (cdr gnus-article-current)
8696              (car gnus-article-current) (current-buffer))))
8697         ;; Prettify the summary buffer line.
8698         (when (gnus-visual-p 'summary-highlight 'highlight)
8699           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8700
8701 (defun gnus-summary-edit-wash (key)
8702   "Perform editing command KEY in the article buffer."
8703   (interactive
8704    (list
8705     (progn
8706       (message "%s" (concat (this-command-keys) "- "))
8707       (read-char))))
8708   (message "")
8709   (gnus-summary-edit-article)
8710   (execute-kbd-macro (concat (this-command-keys) key))
8711   (gnus-article-edit-done))
8712
8713 ;;; Respooling
8714
8715 (defun gnus-summary-respool-query (&optional silent trace)
8716   "Query where the respool algorithm would put this article."
8717   (interactive)
8718   (let (gnus-mark-article-hook)
8719     (gnus-summary-select-article)
8720     (save-excursion
8721       (set-buffer gnus-original-article-buffer)
8722       (save-restriction
8723         (message-narrow-to-head)
8724         (let ((groups (nnmail-article-group 'identity trace)))
8725           (unless silent
8726             (if groups
8727                 (message "This message would go to %s"
8728                          (mapconcat 'car groups ", "))
8729               (message "This message would go to no groups"))
8730             groups))))))
8731
8732 (defun gnus-summary-respool-trace ()
8733   "Trace where the respool algorithm would put this article.
8734 Display a buffer showing all fancy splitting patterns which matched."
8735   (interactive)
8736   (gnus-summary-respool-query nil t))
8737
8738 ;; Summary marking commands.
8739
8740 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8741   "Mark articles which has the same subject as read, and then select the next.
8742 If UNMARK is positive, remove any kind of mark.
8743 If UNMARK is negative, tick articles."
8744   (interactive "P")
8745   (when unmark
8746     (setq unmark (prefix-numeric-value unmark)))
8747   (let ((count
8748          (gnus-summary-mark-same-subject
8749           (gnus-summary-article-subject) unmark)))
8750     ;; Select next unread article.  If auto-select-same mode, should
8751     ;; select the first unread article.
8752     (gnus-summary-next-article t (and gnus-auto-select-same
8753                                       (gnus-summary-article-subject)))
8754     (gnus-message 7 "%d article%s marked as %s"
8755                   count (if (= count 1) " is" "s are")
8756                   (if unmark "unread" "read"))))
8757
8758 (defun gnus-summary-kill-same-subject (&optional unmark)
8759   "Mark articles which has the same subject as read.
8760 If UNMARK is positive, remove any kind of mark.
8761 If UNMARK is negative, tick articles."
8762   (interactive "P")
8763   (when unmark
8764     (setq unmark (prefix-numeric-value unmark)))
8765   (let ((count
8766          (gnus-summary-mark-same-subject
8767           (gnus-summary-article-subject) unmark)))
8768     ;; If marked as read, go to next unread subject.
8769     (when (null unmark)
8770       ;; Go to next unread subject.
8771       (gnus-summary-next-subject 1 t))
8772     (gnus-message 7 "%d articles are marked as %s"
8773                   count (if unmark "unread" "read"))))
8774
8775 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8776   "Mark articles with same SUBJECT as read, and return marked number.
8777 If optional argument UNMARK is positive, remove any kinds of marks.
8778 If optional argument UNMARK is negative, mark articles as unread instead."
8779   (let ((count 1))
8780     (save-excursion
8781       (cond
8782        ((null unmark)                   ; Mark as read.
8783         (while (and
8784                 (progn
8785                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8786                   (gnus-summary-show-thread) t)
8787                 (gnus-summary-find-subject subject))
8788           (setq count (1+ count))))
8789        ((> unmark 0)                    ; Tick.
8790         (while (and
8791                 (progn
8792                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8793                   (gnus-summary-show-thread) t)
8794                 (gnus-summary-find-subject subject))
8795           (setq count (1+ count))))
8796        (t                               ; Mark as unread.
8797         (while (and
8798                 (progn
8799                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8800                   (gnus-summary-show-thread) t)
8801                 (gnus-summary-find-subject subject))
8802           (setq count (1+ count)))))
8803       (gnus-set-mode-line 'summary)
8804       ;; Return the number of marked articles.
8805       count)))
8806
8807 (defun gnus-summary-mark-as-processable (n &optional unmark)
8808   "Set the process mark on the next N articles.
8809 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8810 the process mark instead.  The difference between N and the actual
8811 number of articles marked is returned."
8812   (interactive "P")
8813   (if (and (null n) (gnus-region-active-p))
8814       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8815     (setq n (prefix-numeric-value n))
8816     (let ((backward (< n 0))
8817           (n (abs n)))
8818       (while (and
8819               (> n 0)
8820               (if unmark
8821                   (gnus-summary-remove-process-mark
8822                    (gnus-summary-article-number))
8823                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8824               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8825         (setq n (1- n)))
8826       (when (/= 0 n)
8827         (gnus-message 7 "No more articles"))
8828       (gnus-summary-recenter)
8829       (gnus-summary-position-point)
8830       n)))
8831
8832 (defun gnus-summary-unmark-as-processable (n)
8833   "Remove the process mark from the next N articles.
8834 If N is negative, unmark backward instead.  The difference between N and
8835 the actual number of articles unmarked is returned."
8836   (interactive "P")
8837   (gnus-summary-mark-as-processable n t))
8838
8839 (defun gnus-summary-unmark-all-processable ()
8840   "Remove the process mark from all articles."
8841   (interactive)
8842   (save-excursion
8843     (while gnus-newsgroup-processable
8844       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8845   (gnus-summary-position-point))
8846
8847 (defun gnus-summary-add-mark (article type)
8848   "Mark ARTICLE with a mark of TYPE."
8849   (let ((vtype (car (assq type gnus-article-mark-lists)))
8850         var)
8851     (if (not vtype)
8852         (error "No such mark type: %s" type)
8853       (setq var (intern (format "gnus-newsgroup-%s" type)))
8854       (set var (cons article (symbol-value var)))
8855       (if (memq type '(processable cached replied forwarded recent saved))
8856           (gnus-summary-update-secondary-mark article)
8857         ;;; !!! This is bogus.  We should find out what primary
8858         ;;; !!! mark we want to set.
8859         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8860
8861 (defun gnus-summary-mark-as-expirable (n)
8862   "Mark N articles forward as expirable.
8863 If N is negative, mark backward instead.  The difference between N and
8864 the actual number of articles marked is returned."
8865   (interactive "p")
8866   (gnus-summary-mark-forward n gnus-expirable-mark))
8867
8868 (defun gnus-summary-mark-article-as-replied (article)
8869   "Mark ARTICLE as replied to and update the summary line.
8870 ARTICLE can also be a list of articles."
8871   (interactive (list (gnus-summary-article-number)))
8872   (let ((articles (if (listp article) article (list article))))
8873     (dolist (article articles)
8874       (push article gnus-newsgroup-replied)
8875       (let ((buffer-read-only nil))
8876         (when (gnus-summary-goto-subject article nil t)
8877           (gnus-summary-update-secondary-mark article))))))
8878
8879 (defun gnus-summary-mark-article-as-forwarded (article)
8880   "Mark ARTICLE as forwarded and update the summary line.
8881 ARTICLE can also be a list of articles."
8882   (let ((articles (if (listp article) article (list article))))
8883     (dolist (article articles)
8884       (push article gnus-newsgroup-forwarded)
8885       (let ((buffer-read-only nil))
8886         (when (gnus-summary-goto-subject article nil t)
8887           (gnus-summary-update-secondary-mark article))))))
8888
8889 (defun gnus-summary-set-bookmark (article)
8890   "Set a bookmark in current article."
8891   (interactive (list (gnus-summary-article-number)))
8892   (when (or (not (get-buffer gnus-article-buffer))
8893             (not gnus-current-article)
8894             (not gnus-article-current)
8895             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8896     (error "No current article selected"))
8897   ;; Remove old bookmark, if one exists.
8898   (let ((old (assq article gnus-newsgroup-bookmarks)))
8899     (when old
8900       (setq gnus-newsgroup-bookmarks
8901             (delq old gnus-newsgroup-bookmarks))))
8902   ;; Set the new bookmark, which is on the form
8903   ;; (article-number . line-number-in-body).
8904   (push
8905    (cons article
8906          (save-excursion
8907            (set-buffer gnus-article-buffer)
8908            (count-lines
8909             (min (point)
8910                  (save-excursion
8911                    (goto-char (point-min))
8912                    (search-forward "\n\n" nil t)
8913                    (point)))
8914             (point))))
8915    gnus-newsgroup-bookmarks)
8916   (gnus-message 6 "A bookmark has been added to the current article."))
8917
8918 (defun gnus-summary-remove-bookmark (article)
8919   "Remove the bookmark from the current article."
8920   (interactive (list (gnus-summary-article-number)))
8921   ;; Remove old bookmark, if one exists.
8922   (let ((old (assq article gnus-newsgroup-bookmarks)))
8923     (if old
8924         (progn
8925           (setq gnus-newsgroup-bookmarks
8926                 (delq old gnus-newsgroup-bookmarks))
8927           (gnus-message 6 "Removed bookmark."))
8928       (gnus-message 6 "No bookmark in current article."))))
8929
8930 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8931 (defun gnus-summary-mark-as-dormant (n)
8932   "Mark N articles forward as dormant.
8933 If N is negative, mark backward instead.  The difference between N and
8934 the actual number of articles marked is returned."
8935   (interactive "p")
8936   (gnus-summary-mark-forward n gnus-dormant-mark))
8937
8938 (defun gnus-summary-set-process-mark (article)
8939   "Set the process mark on ARTICLE and update the summary line."
8940   (setq gnus-newsgroup-processable
8941         (cons article
8942               (delq article gnus-newsgroup-processable)))
8943   (when (gnus-summary-goto-subject article)
8944     (gnus-summary-show-thread)
8945     (gnus-summary-goto-subject article)
8946     (gnus-summary-update-secondary-mark article)))
8947
8948 (defun gnus-summary-remove-process-mark (article)
8949   "Remove the process mark from ARTICLE and update the summary line."
8950   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8951   (when (gnus-summary-goto-subject article)
8952     (gnus-summary-show-thread)
8953     (gnus-summary-goto-subject article)
8954     (gnus-summary-update-secondary-mark article)))
8955
8956 (defun gnus-summary-set-saved-mark (article)
8957   "Set the process mark on ARTICLE and update the summary line."
8958   (push article gnus-newsgroup-saved)
8959   (when (gnus-summary-goto-subject article)
8960     (gnus-summary-update-secondary-mark article)))
8961
8962 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8963   "Mark N articles as read forwards.
8964 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8965 The difference between N and the actual number of articles marked is
8966 returned.
8967 Iff NO-EXPIRE, auto-expiry will be inhibited."
8968   (interactive "p")
8969   (gnus-summary-show-thread)
8970   (let ((backward (< n 0))
8971         (gnus-summary-goto-unread
8972          (and gnus-summary-goto-unread
8973               (not (eq gnus-summary-goto-unread 'never))
8974               (not (memq mark (list gnus-unread-mark
8975                                     gnus-ticked-mark gnus-dormant-mark)))))
8976         (n (abs n))
8977         (mark (or mark gnus-del-mark)))
8978     (while (and (> n 0)
8979                 (gnus-summary-mark-article nil mark no-expire)
8980                 (zerop (gnus-summary-next-subject
8981                         (if backward -1 1)
8982                         (and gnus-summary-goto-unread
8983                              (not (eq gnus-summary-goto-unread 'never)))
8984                         t)))
8985       (setq n (1- n)))
8986     (when (/= 0 n)
8987       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8988     (gnus-summary-recenter)
8989     (gnus-summary-position-point)
8990     (gnus-set-mode-line 'summary)
8991     n))
8992
8993 (defun gnus-summary-mark-article-as-read (mark)
8994   "Mark the current article quickly as read with MARK."
8995   (let ((article (gnus-summary-article-number)))
8996     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8997     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8998     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8999     (push (cons article mark) gnus-newsgroup-reads)
9000     ;; Possibly remove from cache, if that is used.
9001     (when gnus-use-cache
9002       (gnus-cache-enter-remove-article article))
9003     ;; Allow the backend to change the mark.
9004     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9005     ;; Check for auto-expiry.
9006     (when (and gnus-newsgroup-auto-expire
9007                (memq mark gnus-auto-expirable-marks))
9008       (setq mark gnus-expirable-mark)
9009       ;; Let the backend know about the mark change.
9010       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9011       (push article gnus-newsgroup-expirable))
9012     ;; Set the mark in the buffer.
9013     (gnus-summary-update-mark mark 'unread)
9014     t))
9015
9016 (defun gnus-summary-mark-article-as-unread (mark)
9017   "Mark the current article quickly as unread with MARK."
9018   (let* ((article (gnus-summary-article-number))
9019          (old-mark (gnus-summary-article-mark article)))
9020     ;; Allow the backend to change the mark.
9021     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9022     (if (eq mark old-mark)
9023         t
9024       (if (<= article 0)
9025           (progn
9026             (gnus-error 1 "Can't mark negative article numbers")
9027             nil)
9028         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9029         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9030         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9031         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9032         (cond ((= mark gnus-ticked-mark)
9033                (push article gnus-newsgroup-marked))
9034               ((= mark gnus-dormant-mark)
9035                (push article gnus-newsgroup-dormant))
9036               (t
9037                (push article gnus-newsgroup-unreads)))
9038         (gnus-pull article gnus-newsgroup-reads)
9039
9040         ;; See whether the article is to be put in the cache.
9041         (and gnus-use-cache
9042              (vectorp (gnus-summary-article-header article))
9043              (save-excursion
9044                (gnus-cache-possibly-enter-article
9045                 gnus-newsgroup-name article
9046                 (gnus-summary-article-header article)
9047                 (= mark gnus-ticked-mark)
9048                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9049
9050         ;; Fix the mark.
9051         (gnus-summary-update-mark mark 'unread)
9052         t))))
9053
9054 (defun gnus-summary-mark-article (&optional article mark no-expire)
9055   "Mark ARTICLE with MARK.  MARK can be any character.
9056 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9057 `??' (dormant) and `?E' (expirable).
9058 If MARK is nil, then the default character `?r' is used.
9059 If ARTICLE is nil, then the article on the current line will be
9060 marked.
9061 Iff NO-EXPIRE, auto-expiry will be inhibited."
9062   ;; The mark might be a string.
9063   (when (stringp mark)
9064     (setq mark (aref mark 0)))
9065   ;; If no mark is given, then we check auto-expiring.
9066   (when (null mark)
9067     (setq mark gnus-del-mark))
9068   (when (and (not no-expire)
9069              gnus-newsgroup-auto-expire
9070              (memq mark gnus-auto-expirable-marks))
9071     (setq mark gnus-expirable-mark))
9072   (let ((article (or article (gnus-summary-article-number)))
9073         (old-mark (gnus-summary-article-mark article)))
9074     ;; Allow the backend to change the mark.
9075     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9076     (if (eq mark old-mark)
9077         t
9078       (unless article
9079         (error "No article on current line"))
9080       (if (not (if (or (= mark gnus-unread-mark)
9081                        (= mark gnus-ticked-mark)
9082                        (= mark gnus-dormant-mark))
9083                    (gnus-mark-article-as-unread article mark)
9084                  (gnus-mark-article-as-read article mark)))
9085           t
9086         ;; See whether the article is to be put in the cache.
9087         (and gnus-use-cache
9088              (not (= mark gnus-canceled-mark))
9089              (vectorp (gnus-summary-article-header article))
9090              (save-excursion
9091                (gnus-cache-possibly-enter-article
9092                 gnus-newsgroup-name article
9093                 (gnus-summary-article-header article)
9094                 (= mark gnus-ticked-mark)
9095                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9096
9097         (when (gnus-summary-goto-subject article nil t)
9098           (let ((buffer-read-only nil))
9099             (gnus-summary-show-thread)
9100             ;; Fix the mark.
9101             (gnus-summary-update-mark mark 'unread)
9102             t))))))
9103
9104 (defun gnus-summary-update-secondary-mark (article)
9105   "Update the secondary (read, process, cache) mark."
9106   (gnus-summary-update-mark
9107    (cond ((memq article gnus-newsgroup-processable)
9108           gnus-process-mark)
9109          ((memq article gnus-newsgroup-cached)
9110           gnus-cached-mark)
9111          ((memq article gnus-newsgroup-replied)
9112           gnus-replied-mark)
9113          ((memq article gnus-newsgroup-forwarded)
9114           gnus-forwarded-mark)
9115          ((memq article gnus-newsgroup-saved)
9116           gnus-saved-mark)
9117          ((memq article gnus-newsgroup-recent)
9118           gnus-recent-mark)
9119          ((memq article gnus-newsgroup-unseen)
9120           gnus-unseen-mark)
9121          (t gnus-no-mark))
9122    'replied)
9123   (when (gnus-visual-p 'summary-highlight 'highlight)
9124     (gnus-run-hooks 'gnus-summary-update-hook))
9125   t)
9126
9127 (defun gnus-summary-update-mark (mark type)
9128   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9129         (buffer-read-only nil))
9130     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9131     (when forward
9132       (when (looking-at "\r")
9133         (incf forward))
9134       (when (<= (+ forward (point)) (point-max))
9135         ;; Go to the right position on the line.
9136         (goto-char (+ forward (point)))
9137         ;; Replace the old mark with the new mark.
9138         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9139         ;; Optionally update the marks by some user rule.
9140         (when (eq type 'unread)
9141           (gnus-data-set-mark
9142            (gnus-data-find (gnus-summary-article-number)) mark)
9143           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9144
9145 (defun gnus-mark-article-as-read (article &optional mark)
9146   "Enter ARTICLE in the pertinent lists and remove it from others."
9147   ;; Make the article expirable.
9148   (let ((mark (or mark gnus-del-mark)))
9149     (if (= mark gnus-expirable-mark)
9150         (push article gnus-newsgroup-expirable)
9151       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9152     ;; Remove from unread and marked lists.
9153     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9154     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9155     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9156     (push (cons article mark) gnus-newsgroup-reads)
9157     ;; Possibly remove from cache, if that is used.
9158     (when gnus-use-cache
9159       (gnus-cache-enter-remove-article article))
9160     t))
9161
9162 (defun gnus-mark-article-as-unread (article &optional mark)
9163   "Enter ARTICLE in the pertinent lists and remove it from others."
9164   (let ((mark (or mark gnus-ticked-mark)))
9165     (if (<= article 0)
9166         (progn
9167           (gnus-error 1 "Can't mark negative article numbers")
9168           nil)
9169       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9170             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9171             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9172             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9173
9174       ;; Unsuppress duplicates?
9175       (when gnus-suppress-duplicates
9176         (gnus-dup-unsuppress-article article))
9177
9178       (cond ((= mark gnus-ticked-mark)
9179              (push article gnus-newsgroup-marked))
9180             ((= mark gnus-dormant-mark)
9181              (push article gnus-newsgroup-dormant))
9182             (t
9183              (push article gnus-newsgroup-unreads)))
9184       (gnus-pull article gnus-newsgroup-reads)
9185       t)))
9186
9187 (defalias 'gnus-summary-mark-as-unread-forward
9188   'gnus-summary-tick-article-forward)
9189 (make-obsolete 'gnus-summary-mark-as-unread-forward
9190                'gnus-summary-tick-article-forward)
9191 (defun gnus-summary-tick-article-forward (n)
9192   "Tick N articles forwards.
9193 If N is negative, tick backwards instead.
9194 The difference between N and the number of articles ticked is returned."
9195   (interactive "p")
9196   (gnus-summary-mark-forward n gnus-ticked-mark))
9197
9198 (defalias 'gnus-summary-mark-as-unread-backward
9199   'gnus-summary-tick-article-backward)
9200 (make-obsolete 'gnus-summary-mark-as-unread-backward
9201                'gnus-summary-tick-article-backward)
9202 (defun gnus-summary-tick-article-backward (n)
9203   "Tick N articles backwards.
9204 The difference between N and the number of articles ticked is returned."
9205   (interactive "p")
9206   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9207
9208 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9209 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9210 (defun gnus-summary-tick-article (&optional article clear-mark)
9211   "Mark current article as unread.
9212 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9213 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9214   (interactive)
9215   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9216                                        gnus-ticked-mark)))
9217
9218 (defun gnus-summary-mark-as-read-forward (n)
9219   "Mark N articles as read forwards.
9220 If N is negative, mark backwards instead.
9221 The difference between N and the actual number of articles marked is
9222 returned."
9223   (interactive "p")
9224   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9225
9226 (defun gnus-summary-mark-as-read-backward (n)
9227   "Mark the N articles as read backwards.
9228 The difference between N and the actual number of articles marked is
9229 returned."
9230   (interactive "p")
9231   (gnus-summary-mark-forward
9232    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9233
9234 (defun gnus-summary-mark-as-read (&optional article mark)
9235   "Mark current article as read.
9236 ARTICLE specifies the article to be marked as read.
9237 MARK specifies a string to be inserted at the beginning of the line."
9238   (gnus-summary-mark-article article mark))
9239
9240 (defun gnus-summary-clear-mark-forward (n)
9241   "Clear marks from N articles forward.
9242 If N is negative, clear backward instead.
9243 The difference between N and the number of marks cleared is returned."
9244   (interactive "p")
9245   (gnus-summary-mark-forward n gnus-unread-mark))
9246
9247 (defun gnus-summary-clear-mark-backward (n)
9248   "Clear marks from N articles backward.
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-mark-unread-as-read ()
9254   "Intended to be used by `gnus-summary-mark-article-hook'."
9255   (when (memq gnus-current-article gnus-newsgroup-unreads)
9256     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9257
9258 (defun gnus-summary-mark-read-and-unread-as-read ()
9259   "Intended to be used by `gnus-summary-mark-article-hook'."
9260   (let ((mark (gnus-summary-article-mark)))
9261     (when (or (gnus-unread-mark-p mark)
9262               (gnus-read-mark-p mark))
9263       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9264
9265 (defun gnus-summary-mark-unread-as-ticked ()
9266   "Intended to be used by `gnus-summary-mark-article-hook'."
9267   (when (memq gnus-current-article gnus-newsgroup-unreads)
9268     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9269
9270 (defun gnus-summary-mark-region-as-read (point mark all)
9271   "Mark all unread articles between point and mark as read.
9272 If given a prefix, mark all articles between point and mark as read,
9273 even ticked and dormant ones."
9274   (interactive "r\nP")
9275   (save-excursion
9276     (let (article)
9277       (goto-char point)
9278       (beginning-of-line)
9279       (while (and
9280               (< (point) mark)
9281               (progn
9282                 (when (or all
9283                           (memq (setq article (gnus-summary-article-number))
9284                                 gnus-newsgroup-unreads))
9285                   (gnus-summary-mark-article article gnus-del-mark))
9286                 t)
9287               (gnus-summary-find-next))))))
9288
9289 (defun gnus-summary-mark-below (score mark)
9290   "Mark articles with score less than SCORE with MARK."
9291   (interactive "P\ncMark: ")
9292   (setq score (if score
9293                   (prefix-numeric-value score)
9294                 (or gnus-summary-default-score 0)))
9295   (save-excursion
9296     (set-buffer gnus-summary-buffer)
9297     (goto-char (point-min))
9298     (while
9299         (progn
9300           (and (< (gnus-summary-article-score) score)
9301                (gnus-summary-mark-article nil mark))
9302           (gnus-summary-find-next)))))
9303
9304 (defun gnus-summary-kill-below (&optional score)
9305   "Mark articles with score below SCORE as read."
9306   (interactive "P")
9307   (gnus-summary-mark-below score gnus-killed-mark))
9308
9309 (defun gnus-summary-clear-above (&optional score)
9310   "Clear all marks from articles with score above SCORE."
9311   (interactive "P")
9312   (gnus-summary-mark-above score gnus-unread-mark))
9313
9314 (defun gnus-summary-tick-above (&optional score)
9315   "Tick all articles with score above SCORE."
9316   (interactive "P")
9317   (gnus-summary-mark-above score gnus-ticked-mark))
9318
9319 (defun gnus-summary-mark-above (score mark)
9320   "Mark articles with score over SCORE with MARK."
9321   (interactive "P\ncMark: ")
9322   (setq score (if score
9323                   (prefix-numeric-value score)
9324                 (or gnus-summary-default-score 0)))
9325   (save-excursion
9326     (set-buffer gnus-summary-buffer)
9327     (goto-char (point-min))
9328     (while (and (progn
9329                   (when (> (gnus-summary-article-score) score)
9330                     (gnus-summary-mark-article nil mark))
9331                   t)
9332                 (gnus-summary-find-next)))))
9333
9334 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9335 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9336 (defun gnus-summary-limit-include-expunged (&optional no-error)
9337   "Display all the hidden articles that were expunged for low scores."
9338   (interactive)
9339   (let ((buffer-read-only nil))
9340     (let ((scored gnus-newsgroup-scored)
9341           headers h)
9342       (while scored
9343         (unless (gnus-summary-article-header (caar scored))
9344           (and (setq h (gnus-number-to-header (caar scored)))
9345                (< (cdar scored) gnus-summary-expunge-below)
9346                (push h headers)))
9347         (setq scored (cdr scored)))
9348       (if (not headers)
9349           (when (not no-error)
9350             (error "No expunged articles hidden"))
9351         (goto-char (point-min))
9352         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9353         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9354         (mapcar (lambda (x) (push (mail-header-number x)
9355                                   gnus-newsgroup-limit))
9356                 headers)
9357         (gnus-summary-prepare-unthreaded (nreverse headers))
9358         (goto-char (point-min))
9359         (gnus-summary-position-point)
9360         t))))
9361
9362 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9363   "Mark all unread articles in this newsgroup as read.
9364 If prefix argument ALL is non-nil, ticked and dormant articles will
9365 also be marked as read.
9366 If QUIETLY is non-nil, no questions will be asked.
9367 If TO-HERE is non-nil, it should be a point in the buffer.  All
9368 articles before (after, if REVERSE is set) this point will be marked as read.
9369 Note that this function will only catch up the unread article
9370 in the current summary buffer limitation.
9371 The number of articles marked as read is returned."
9372   (interactive "P")
9373   (prog1
9374       (save-excursion
9375         (when (or quietly
9376                   (not gnus-interactive-catchup) ;Without confirmation?
9377                   gnus-expert-user
9378                   (gnus-y-or-n-p
9379                    (if all
9380                        "Mark absolutely all articles as read? "
9381                      "Mark all unread articles as read? ")))
9382           (if (and not-mark
9383                    (not gnus-newsgroup-adaptive)
9384                    (not gnus-newsgroup-auto-expire)
9385                    (not gnus-suppress-duplicates)
9386                    (or (not gnus-use-cache)
9387                        (eq gnus-use-cache 'passive)))
9388               (progn
9389                 (when all
9390                   (setq gnus-newsgroup-marked nil
9391                         gnus-newsgroup-dormant nil))
9392                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9393             ;; We actually mark all articles as canceled, which we
9394             ;; have to do when using auto-expiry or adaptive scoring.
9395             (gnus-summary-show-all-threads)
9396             (if (and to-here reverse)
9397                 (progn
9398                   (goto-char to-here)
9399                   (while (and
9400                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9401                           (gnus-summary-find-next (not all) nil nil t))))
9402               (when (gnus-summary-first-subject (not all) t)
9403                 (while (and
9404                         (if to-here (< (point) to-here) t)
9405                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9406                         (gnus-summary-find-next (not all) nil nil t)))))
9407             (gnus-set-mode-line 'summary))
9408           t))
9409     (gnus-summary-position-point)))
9410
9411 (defun gnus-summary-catchup-to-here (&optional all)
9412   "Mark all unticked articles before the current one as read.
9413 If ALL is non-nil, also mark ticked and dormant articles as read."
9414   (interactive "P")
9415   (save-excursion
9416     (gnus-save-hidden-threads
9417       (let ((beg (point)))
9418         ;; We check that there are unread articles.
9419         (when (or all (gnus-summary-find-prev))
9420           (gnus-summary-catchup all t beg)))))
9421   (gnus-summary-position-point))
9422
9423 (defun gnus-summary-catchup-from-here (&optional all)
9424   "Mark all unticked articles after the current one as read.
9425 If ALL is non-nil, also mark ticked and dormant articles as read."
9426   (interactive "P")
9427   (save-excursion
9428     (gnus-save-hidden-threads
9429       (let ((beg (point)))
9430         ;; We check that there are unread articles.
9431         (when (or all (gnus-summary-find-next))
9432           (gnus-summary-catchup all t beg nil t)))))
9433   (gnus-summary-position-point))
9434
9435 (defun gnus-summary-catchup-all (&optional quietly)
9436   "Mark all articles in this newsgroup as read."
9437   (interactive "P")
9438   (gnus-summary-catchup t quietly))
9439
9440 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9441   "Mark all unread articles in this group as read, then exit.
9442 If prefix argument ALL is non-nil, all articles are marked as read.
9443 If QUIETLY is non-nil, no questions will be asked."
9444   (interactive "P")
9445   (when (gnus-summary-catchup all quietly nil 'fast)
9446     ;; Select next newsgroup or exit.
9447     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9448              (eq gnus-auto-select-next 'quietly))
9449         (gnus-summary-next-group nil)
9450       (gnus-summary-exit))))
9451
9452 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9453   "Mark all articles in this newsgroup as read, and then exit."
9454   (interactive "P")
9455   (gnus-summary-catchup-and-exit t quietly))
9456
9457 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9458   "Mark all articles in this group as read and select the next group.
9459 If given a prefix, mark all articles, unread as well as ticked, as
9460 read."
9461   (interactive "P")
9462   (save-excursion
9463     (gnus-summary-catchup all))
9464   (gnus-summary-next-group))
9465
9466 ;;;
9467 ;;; with article
9468 ;;;
9469
9470 (defmacro gnus-with-article (article &rest forms)
9471   "Select ARTICLE and perform FORMS in the original article buffer.
9472 Then replace the article with the result."
9473   `(progn
9474      ;; We don't want the article to be marked as read.
9475      (let (gnus-mark-article-hook)
9476        (gnus-summary-select-article t t nil ,article))
9477      (set-buffer gnus-original-article-buffer)
9478      ,@forms
9479      (if (not (gnus-check-backend-function
9480                'request-replace-article (car gnus-article-current)))
9481          (gnus-message 5 "Read-only group; not replacing")
9482        (unless (gnus-request-replace-article
9483                 ,article (car gnus-article-current)
9484                 (current-buffer) t)
9485          (error "Couldn't replace article")))
9486      ;; The cache and backlog have to be flushed somewhat.
9487      (when gnus-keep-backlog
9488        (gnus-backlog-remove-article
9489         (car gnus-article-current) (cdr gnus-article-current)))
9490      (when gnus-use-cache
9491        (gnus-cache-update-article
9492         (car gnus-article-current) (cdr gnus-article-current)))))
9493
9494 (put 'gnus-with-article 'lisp-indent-function 1)
9495 (put 'gnus-with-article 'edebug-form-spec '(form body))
9496
9497 ;; Thread-based commands.
9498
9499 (defun gnus-summary-articles-in-thread (&optional article)
9500   "Return a list of all articles in the current thread.
9501 If ARTICLE is non-nil, return all articles in the thread that starts
9502 with that article."
9503   (let* ((article (or article (gnus-summary-article-number)))
9504          (data (gnus-data-find-list article))
9505          (top-level (gnus-data-level (car data)))
9506          (top-subject
9507           (cond ((null gnus-thread-operation-ignore-subject)
9508                  (gnus-simplify-subject-re
9509                   (mail-header-subject (gnus-data-header (car data)))))
9510                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9511                  (gnus-simplify-subject-fuzzy
9512                   (mail-header-subject (gnus-data-header (car data)))))
9513                 (t nil)))
9514          (end-point (save-excursion
9515                       (if (gnus-summary-go-to-next-thread)
9516                           (point) (point-max))))
9517          articles)
9518     (while (and data
9519                 (< (gnus-data-pos (car data)) end-point))
9520       (when (or (not top-subject)
9521                 (string= top-subject
9522                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9523                              (gnus-simplify-subject-fuzzy
9524                               (mail-header-subject
9525                                (gnus-data-header (car data))))
9526                            (gnus-simplify-subject-re
9527                             (mail-header-subject
9528                              (gnus-data-header (car data)))))))
9529         (push (gnus-data-number (car data)) articles))
9530       (unless (and (setq data (cdr data))
9531                    (> (gnus-data-level (car data)) top-level))
9532         (setq data nil)))
9533     ;; Return the list of articles.
9534     (nreverse articles)))
9535
9536 (defun gnus-summary-rethread-current ()
9537   "Rethread the thread the current article is part of."
9538   (interactive)
9539   (let* ((gnus-show-threads t)
9540          (article (gnus-summary-article-number))
9541          (id (mail-header-id (gnus-summary-article-header)))
9542          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9543     (unless id
9544       (error "No article on the current line"))
9545     (gnus-rebuild-thread id)
9546     (gnus-summary-goto-subject article)))
9547
9548 (defun gnus-summary-reparent-thread ()
9549   "Make the current article child of the marked (or previous) article.
9550
9551 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9552 is non-nil or the Subject: of both articles are the same."
9553   (interactive)
9554   (unless (not (gnus-group-read-only-p))
9555     (error "The current newsgroup does not support article editing"))
9556   (unless (<= (length gnus-newsgroup-processable) 1)
9557     (error "No more than one article may be marked"))
9558   (save-window-excursion
9559     (let ((gnus-article-buffer " *reparent*")
9560           (current-article (gnus-summary-article-number))
9561           ;; First grab the marked article, otherwise one line up.
9562           (parent-article (if (not (null gnus-newsgroup-processable))
9563                               (car gnus-newsgroup-processable)
9564                             (save-excursion
9565                               (if (eq (forward-line -1) 0)
9566                                   (gnus-summary-article-number)
9567                                 (error "Beginning of summary buffer"))))))
9568       (unless (not (eq current-article parent-article))
9569         (error "An article may not be self-referential"))
9570       (let ((message-id (mail-header-id
9571                          (gnus-summary-article-header parent-article))))
9572         (unless (and message-id (not (equal message-id "")))
9573           (error "No message-id in desired parent"))
9574         (gnus-with-article current-article
9575           (save-restriction
9576             (goto-char (point-min))
9577             (message-narrow-to-head)
9578             (if (re-search-forward "^References: " nil t)
9579                 (progn
9580                   (re-search-forward "^[^ \t]" nil t)
9581                   (forward-line -1)
9582                   (end-of-line)
9583                   (insert " " message-id))
9584               (insert "References: " message-id "\n"))))
9585         (set-buffer gnus-summary-buffer)
9586         (gnus-summary-unmark-all-processable)
9587         (gnus-summary-update-article current-article)
9588         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9589             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9590         (gnus-summary-rethread-current)
9591         (gnus-message 3 "Article %d is now the child of article %d"
9592                       current-article parent-article)))))
9593
9594 (defun gnus-summary-toggle-threads (&optional arg)
9595   "Toggle showing conversation threads.
9596 If ARG is positive number, turn showing conversation threads on."
9597   (interactive "P")
9598   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9599     (setq gnus-show-threads
9600           (if (null arg) (not gnus-show-threads)
9601             (> (prefix-numeric-value arg) 0)))
9602     (gnus-summary-prepare)
9603     (gnus-summary-goto-subject current)
9604     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9605     (gnus-summary-position-point)))
9606
9607 (defun gnus-summary-show-all-threads ()
9608   "Show all threads."
9609   (interactive)
9610   (save-excursion
9611     (let ((buffer-read-only nil))
9612       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9613   (gnus-summary-position-point))
9614
9615 (defun gnus-summary-show-thread ()
9616   "Show thread subtrees.
9617 Returns nil if no thread was there to be shown."
9618   (interactive)
9619   (let ((buffer-read-only nil)
9620         (orig (point))
9621         ;; first goto end then to beg, to have point at beg after let
9622         (end (progn (end-of-line) (point)))
9623         (beg (progn (beginning-of-line) (point))))
9624     (prog1
9625         ;; Any hidden lines here?
9626         (search-forward "\r" end t)
9627       (subst-char-in-region beg end ?\^M ?\n t)
9628       (goto-char orig)
9629       (gnus-summary-position-point))))
9630
9631 (defun gnus-summary-hide-all-threads ()
9632   "Hide all thread subtrees."
9633   (interactive)
9634   (save-excursion
9635     (goto-char (point-min))
9636     (gnus-summary-hide-thread)
9637     (while (zerop (gnus-summary-next-thread 1 t))
9638       (gnus-summary-hide-thread)))
9639   (gnus-summary-position-point))
9640
9641 (defun gnus-summary-hide-thread ()
9642   "Hide thread subtrees.
9643 Returns nil if no threads were there to be hidden."
9644   (interactive)
9645   (let ((buffer-read-only nil)
9646         (start (point))
9647         (article (gnus-summary-article-number)))
9648     (goto-char start)
9649     ;; Go forward until either the buffer ends or the subthread
9650     ;; ends.
9651     (when (and (not (eobp))
9652                (or (zerop (gnus-summary-next-thread 1 t))
9653                    (goto-char (point-max))))
9654       (prog1
9655           (if (and (> (point) start)
9656                    (search-backward "\n" start t))
9657               (progn
9658                 (subst-char-in-region start (point) ?\n ?\^M)
9659                 (gnus-summary-goto-subject article))
9660             (goto-char start)
9661             nil)))))
9662
9663 (defun gnus-summary-go-to-next-thread (&optional previous)
9664   "Go to the same level (or less) next thread.
9665 If PREVIOUS is non-nil, go to previous thread instead.
9666 Return the article number moved to, or nil if moving was impossible."
9667   (let ((level (gnus-summary-thread-level))
9668         (way (if previous -1 1))
9669         (beg (point)))
9670     (forward-line way)
9671     (while (and (not (eobp))
9672                 (< level (gnus-summary-thread-level)))
9673       (forward-line way))
9674     (if (eobp)
9675         (progn
9676           (goto-char beg)
9677           nil)
9678       (setq beg (point))
9679       (prog1
9680           (gnus-summary-article-number)
9681         (goto-char beg)))))
9682
9683 (defun gnus-summary-next-thread (n &optional silent)
9684   "Go to the same level next N'th thread.
9685 If N is negative, search backward instead.
9686 Returns the difference between N and the number of skips actually
9687 done.
9688
9689 If SILENT, don't output messages."
9690   (interactive "p")
9691   (let ((backward (< n 0))
9692         (n (abs n)))
9693     (while (and (> n 0)
9694                 (gnus-summary-go-to-next-thread backward))
9695       (decf n))
9696     (unless silent
9697       (gnus-summary-position-point))
9698     (when (and (not silent) (/= 0 n))
9699       (gnus-message 7 "No more threads"))
9700     n))
9701
9702 (defun gnus-summary-prev-thread (n)
9703   "Go to the same level previous N'th thread.
9704 Returns the difference between N and the number of skips actually
9705 done."
9706   (interactive "p")
9707   (gnus-summary-next-thread (- n)))
9708
9709 (defun gnus-summary-go-down-thread ()
9710   "Go down one level in the current thread."
9711   (let ((children (gnus-summary-article-children)))
9712     (when children
9713       (gnus-summary-goto-subject (car children)))))
9714
9715 (defun gnus-summary-go-up-thread ()
9716   "Go up one level in the current thread."
9717   (let ((parent (gnus-summary-article-parent)))
9718     (when parent
9719       (gnus-summary-goto-subject parent))))
9720
9721 (defun gnus-summary-down-thread (n)
9722   "Go down thread N steps.
9723 If N is negative, go up instead.
9724 Returns the difference between N and how many steps down that were
9725 taken."
9726   (interactive "p")
9727   (let ((up (< n 0))
9728         (n (abs n)))
9729     (while (and (> n 0)
9730                 (if up (gnus-summary-go-up-thread)
9731                   (gnus-summary-go-down-thread)))
9732       (setq n (1- n)))
9733     (gnus-summary-position-point)
9734     (when (/= 0 n)
9735       (gnus-message 7 "Can't go further"))
9736     n))
9737
9738 (defun gnus-summary-up-thread (n)
9739   "Go up thread N steps.
9740 If N is negative, go down instead.
9741 Returns the difference between N and how many steps down that were
9742 taken."
9743   (interactive "p")
9744   (gnus-summary-down-thread (- n)))
9745
9746 (defun gnus-summary-top-thread ()
9747   "Go to the top of the thread."
9748   (interactive)
9749   (while (gnus-summary-go-up-thread))
9750   (gnus-summary-article-number))
9751
9752 (defun gnus-summary-kill-thread (&optional unmark)
9753   "Mark articles under current thread as read.
9754 If the prefix argument is positive, remove any kinds of marks.
9755 If the prefix argument is negative, tick articles instead."
9756   (interactive "P")
9757   (when unmark
9758     (setq unmark (prefix-numeric-value unmark)))
9759   (let ((articles (gnus-summary-articles-in-thread)))
9760     (save-excursion
9761       ;; Expand the thread.
9762       (gnus-summary-show-thread)
9763       ;; Mark all the articles.
9764       (while articles
9765         (gnus-summary-goto-subject (car articles))
9766         (cond ((null unmark)
9767                (gnus-summary-mark-article-as-read gnus-killed-mark))
9768               ((> unmark 0)
9769                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9770               (t
9771                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9772         (setq articles (cdr articles))))
9773     ;; Hide killed subtrees.
9774     (and (null unmark)
9775          gnus-thread-hide-killed
9776          (gnus-summary-hide-thread))
9777     ;; If marked as read, go to next unread subject.
9778     (when (null unmark)
9779       ;; Go to next unread subject.
9780       (gnus-summary-next-subject 1 t)))
9781   (gnus-set-mode-line 'summary))
9782
9783 ;; Summary sorting commands
9784
9785 (defun gnus-summary-sort-by-number (&optional reverse)
9786   "Sort the summary buffer by article number.
9787 Argument REVERSE means reverse order."
9788   (interactive "P")
9789   (gnus-summary-sort 'number reverse))
9790
9791 (defun gnus-summary-sort-by-author (&optional reverse)
9792   "Sort the summary buffer by author name alphabetically.
9793 If `case-fold-search' is non-nil, case of letters is ignored.
9794 Argument REVERSE means reverse order."
9795   (interactive "P")
9796   (gnus-summary-sort 'author reverse))
9797
9798 (defun gnus-summary-sort-by-subject (&optional reverse)
9799   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9800 If `case-fold-search' is non-nil, case of letters is ignored.
9801 Argument REVERSE means reverse order."
9802   (interactive "P")
9803   (gnus-summary-sort 'subject reverse))
9804
9805 (defun gnus-summary-sort-by-date (&optional reverse)
9806   "Sort the summary buffer by date.
9807 Argument REVERSE means reverse order."
9808   (interactive "P")
9809   (gnus-summary-sort 'date reverse))
9810
9811 (defun gnus-summary-sort-by-score (&optional reverse)
9812   "Sort the summary buffer by score.
9813 Argument REVERSE means reverse order."
9814   (interactive "P")
9815   (gnus-summary-sort 'score reverse))
9816
9817 (defun gnus-summary-sort-by-lines (&optional reverse)
9818   "Sort the summary buffer by the number of lines.
9819 Argument REVERSE means reverse order."
9820   (interactive "P")
9821   (gnus-summary-sort 'lines reverse))
9822
9823 (defun gnus-summary-sort-by-chars (&optional reverse)
9824   "Sort the summary buffer by article length.
9825 Argument REVERSE means reverse order."
9826   (interactive "P")
9827   (gnus-summary-sort 'chars reverse))
9828
9829 (defun gnus-summary-sort-by-original (&optional reverse)
9830   "Sort the summary buffer using the default sorting method.
9831 Argument REVERSE means reverse order."
9832   (interactive "P")
9833   (let* ((buffer-read-only)
9834          (gnus-summary-prepare-hook nil))
9835     ;; We do the sorting by regenerating the threads.
9836     (gnus-summary-prepare)
9837     ;; Hide subthreads if needed.
9838     (when (and gnus-show-threads gnus-thread-hide-subtree)
9839       (gnus-summary-hide-all-threads))))
9840
9841 (defun gnus-summary-sort (predicate reverse)
9842   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9843   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9844          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9845          (gnus-thread-sort-functions
9846           (if (not reverse)
9847               thread
9848             `(lambda (t1 t2)
9849                (,thread t2 t1))))
9850          (gnus-sort-gathered-threads-function
9851           gnus-thread-sort-functions)
9852          (gnus-article-sort-functions
9853           (if (not reverse)
9854               article
9855             `(lambda (t1 t2)
9856                (,article t2 t1))))
9857          (buffer-read-only)
9858          (gnus-summary-prepare-hook nil))
9859     ;; We do the sorting by regenerating the threads.
9860     (gnus-summary-prepare)
9861     ;; Hide subthreads if needed.
9862     (when (and gnus-show-threads gnus-thread-hide-subtree)
9863       (gnus-summary-hide-all-threads))))
9864
9865 ;; Summary saving commands.
9866
9867 (defun gnus-summary-save-article (&optional n not-saved)
9868   "Save the current article using the default saver function.
9869 If N is a positive number, save the N next articles.
9870 If N is a negative number, save the N previous articles.
9871 If N is nil and any articles have been marked with the process mark,
9872 save those articles instead.
9873 The variable `gnus-default-article-saver' specifies the saver function."
9874   (interactive "P")
9875   (let* ((articles (gnus-summary-work-articles n))
9876          (save-buffer (save-excursion
9877                         (nnheader-set-temp-buffer " *Gnus Save*")))
9878          (num (length articles))
9879          header file)
9880     (dolist (article articles)
9881       (setq header (gnus-summary-article-header article))
9882       (if (not (vectorp header))
9883           ;; This is a pseudo-article.
9884           (if (assq 'name header)
9885               (gnus-copy-file (cdr (assq 'name header)))
9886             (gnus-message 1 "Article %d is unsaveable" article))
9887         ;; This is a real article.
9888         (save-window-excursion
9889           (gnus-summary-select-article t nil nil article))
9890         (save-excursion
9891           (set-buffer save-buffer)
9892           (erase-buffer)
9893           (insert-buffer-substring gnus-original-article-buffer))
9894         (setq file (gnus-article-save save-buffer file num))
9895         (gnus-summary-remove-process-mark article)
9896         (unless not-saved
9897           (gnus-summary-set-saved-mark article))))
9898     (gnus-kill-buffer save-buffer)
9899     (gnus-summary-position-point)
9900     (gnus-set-mode-line 'summary)
9901     n))
9902
9903 (defun gnus-summary-pipe-output (&optional arg)
9904   "Pipe the current article to a subprocess.
9905 If N is a positive number, pipe the N next articles.
9906 If N is a negative number, pipe the N previous articles.
9907 If N is nil and any articles have been marked with the process mark,
9908 pipe those articles instead."
9909   (interactive "P")
9910   (require 'gnus-art)
9911   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9912     (gnus-summary-save-article arg t))
9913   (let ((buffer (get-buffer "*Shell Command Output*")))
9914     (if (and buffer
9915              (with-current-buffer buffer (> (point-max) (point-min))))
9916         (gnus-configure-windows 'pipe))))
9917
9918 (defun gnus-summary-save-article-mail (&optional arg)
9919   "Append the current article to an mail file.
9920 If N is a positive number, save the N next articles.
9921 If N is a negative number, save the N previous articles.
9922 If N is nil and any articles have been marked with the process mark,
9923 save those articles instead."
9924   (interactive "P")
9925   (require 'gnus-art)
9926   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9927     (gnus-summary-save-article arg)))
9928
9929 (defun gnus-summary-save-article-rmail (&optional arg)
9930   "Append the current article to an rmail file.
9931 If N is a positive number, save the N next articles.
9932 If N is a negative number, save the N previous articles.
9933 If N is nil and any articles have been marked with the process mark,
9934 save those articles instead."
9935   (interactive "P")
9936   (require 'gnus-art)
9937   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9938     (gnus-summary-save-article arg)))
9939
9940 (defun gnus-summary-save-article-file (&optional arg)
9941   "Append the current article to a file.
9942 If N is a positive number, save the N next articles.
9943 If N is a negative number, save the N previous articles.
9944 If N is nil and any articles have been marked with the process mark,
9945 save those articles instead."
9946   (interactive "P")
9947   (require 'gnus-art)
9948   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9949     (gnus-summary-save-article arg)))
9950
9951 (defun gnus-summary-write-article-file (&optional arg)
9952   "Write the current article to a file, deleting the previous file.
9953 If N is a positive number, save the N next articles.
9954 If N is a negative number, save the N previous articles.
9955 If N is nil and any articles have been marked with the process mark,
9956 save those articles instead."
9957   (interactive "P")
9958   (require 'gnus-art)
9959   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9960     (gnus-summary-save-article arg)))
9961
9962 (defun gnus-summary-save-article-body-file (&optional arg)
9963   "Append the current article body to a file.
9964 If N is a positive number, save the N next articles.
9965 If N is a negative number, save the N previous articles.
9966 If N is nil and any articles have been marked with the process mark,
9967 save those articles instead."
9968   (interactive "P")
9969   (require 'gnus-art)
9970   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9971     (gnus-summary-save-article arg)))
9972
9973 (defun gnus-summary-pipe-message (program)
9974   "Pipe the current article through PROGRAM."
9975   (interactive "sProgram: ")
9976   (gnus-summary-select-article)
9977   (let ((mail-header-separator ""))
9978     (gnus-eval-in-buffer-window gnus-article-buffer
9979       (save-restriction
9980         (widen)
9981         (let ((start (window-start))
9982               buffer-read-only)
9983           (message-pipe-buffer-body program)
9984           (set-window-start (get-buffer-window (current-buffer)) start))))))
9985
9986 (defun gnus-get-split-value (methods)
9987   "Return a value based on the split METHODS."
9988   (let (split-name method result match)
9989     (when methods
9990       (save-excursion
9991         (set-buffer gnus-original-article-buffer)
9992         (save-restriction
9993           (nnheader-narrow-to-headers)
9994           (while (and methods (not split-name))
9995             (goto-char (point-min))
9996             (setq method (pop methods))
9997             (setq match (car method))
9998             (when (cond
9999                    ((stringp match)
10000                     ;; Regular expression.
10001                     (ignore-errors
10002                       (re-search-forward match nil t)))
10003                    ((gnus-functionp match)
10004                     ;; Function.
10005                     (save-restriction
10006                       (widen)
10007                       (setq result (funcall match gnus-newsgroup-name))))
10008                    ((consp match)
10009                     ;; Form.
10010                     (save-restriction
10011                       (widen)
10012                       (setq result (eval match)))))
10013               (setq split-name (cdr method))
10014               (cond ((stringp result)
10015                      (push (expand-file-name
10016                             result gnus-article-save-directory)
10017                            split-name))
10018                     ((consp result)
10019                      (setq split-name (append result split-name)))))))))
10020     (nreverse split-name)))
10021
10022 (defun gnus-valid-move-group-p (group)
10023   (and (boundp group)
10024        (symbol-name group)
10025        (symbol-value group)
10026        (gnus-get-function (gnus-find-method-for-group
10027                            (symbol-name group)) 'request-accept-article t)))
10028
10029 (defun gnus-read-move-group-name (prompt default articles prefix)
10030   "Read a group name."
10031   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10032          (minibuffer-confirm-incomplete nil) ; XEmacs
10033          (prom
10034           (format "%s %s to:"
10035                   prompt
10036                   (if (> (length articles) 1)
10037                       (format "these %d articles" (length articles))
10038                     "this article")))
10039          (to-newsgroup
10040           (cond
10041            ((null split-name)
10042             (gnus-completing-read default prom
10043                                   gnus-active-hashtb
10044                                   'gnus-valid-move-group-p
10045                                   nil prefix
10046                                   'gnus-group-history))
10047            ((= 1 (length split-name))
10048             (gnus-completing-read (car split-name) prom
10049                                   gnus-active-hashtb
10050                                   'gnus-valid-move-group-p
10051                                   nil nil
10052                                   'gnus-group-history))
10053            (t
10054             (gnus-completing-read nil prom
10055                                   (mapcar (lambda (el) (list el))
10056                                           (nreverse split-name))
10057                                   nil nil nil
10058                                   'gnus-group-history))))
10059          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10060     (when to-newsgroup
10061       (if (or (string= to-newsgroup "")
10062               (string= to-newsgroup prefix))
10063           (setq to-newsgroup default))
10064       (unless to-newsgroup
10065         (error "No group name entered"))
10066       (or (gnus-active to-newsgroup)
10067           (gnus-activate-group to-newsgroup nil nil to-method)
10068           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10069                                      to-newsgroup))
10070               (or (and (gnus-request-create-group to-newsgroup to-method)
10071                        (gnus-activate-group
10072                         to-newsgroup nil nil to-method)
10073                        (gnus-subscribe-group to-newsgroup))
10074                   (error "Couldn't create group %s" to-newsgroup)))
10075           (error "No such group: %s" to-newsgroup)))
10076     to-newsgroup))
10077
10078 (defun gnus-summary-save-parts (type dir n &optional reverse)
10079   "Save parts matching TYPE to DIR.
10080 If REVERSE, save parts that do not match TYPE."
10081   (interactive
10082    (list (read-string "Save parts of type: "
10083                       (or (car gnus-summary-save-parts-type-history)
10084                           gnus-summary-save-parts-default-mime)
10085                       'gnus-summary-save-parts-type-history)
10086          (setq gnus-summary-save-parts-last-directory
10087                (read-file-name "Save to directory: "
10088                                gnus-summary-save-parts-last-directory
10089                                nil t))
10090          current-prefix-arg))
10091   (gnus-summary-iterate n
10092     (let ((gnus-display-mime-function nil)
10093           (gnus-inhibit-treatment t))
10094       (gnus-summary-select-article))
10095     (save-excursion
10096       (set-buffer gnus-article-buffer)
10097       (let ((handles (or gnus-article-mime-handles
10098                          (mm-dissect-buffer) (mm-uu-dissect))))
10099         (when handles
10100           (gnus-summary-save-parts-1 type dir handles reverse)
10101           (unless gnus-article-mime-handles ;; Don't destroy this case.
10102             (mm-destroy-parts handles)))))))
10103
10104 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10105   (if (stringp (car handle))
10106       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10107               (cdr handle))
10108     (when (if reverse
10109               (not (string-match type (mm-handle-media-type handle)))
10110             (string-match type (mm-handle-media-type handle)))
10111       (let ((file (expand-file-name
10112                    (file-name-nondirectory
10113                     (or
10114                      (mail-content-type-get
10115                       (mm-handle-disposition handle) 'filename)
10116                      (concat gnus-newsgroup-name
10117                              "." (number-to-string
10118                                   (cdr gnus-article-current)))))
10119                    dir)))
10120         (unless (file-exists-p file)
10121           (mm-save-part-to-file handle file))))))
10122
10123 ;; Summary extract commands
10124
10125 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10126   (let ((buffer-read-only nil)
10127         (article (gnus-summary-article-number))
10128         after-article b e)
10129     (unless (gnus-summary-goto-subject article)
10130       (error "No such article: %d" article))
10131     (gnus-summary-position-point)
10132     ;; If all commands are to be bunched up on one line, we collect
10133     ;; them here.
10134     (unless gnus-view-pseudos-separately
10135       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10136             files action)
10137         (while ps
10138           (setq action (cdr (assq 'action (car ps))))
10139           (setq files (list (cdr (assq 'name (car ps)))))
10140           (while (and ps (cdr ps)
10141                       (string= (or action "1")
10142                                (or (cdr (assq 'action (cadr ps))) "2")))
10143             (push (cdr (assq 'name (cadr ps))) files)
10144             (setcdr ps (cddr ps)))
10145           (when files
10146             (when (not (string-match "%s" action))
10147               (push " " files))
10148             (push " " files)
10149             (when (assq 'execute (car ps))
10150               (setcdr (assq 'execute (car ps))
10151                       (funcall (if (string-match "%s" action)
10152                                    'format 'concat)
10153                                action
10154                                (mapconcat
10155                                 (lambda (f)
10156                                   (if (equal f " ")
10157                                       f
10158                                     (gnus-quote-arg-for-sh-or-csh f)))
10159                                 files " ")))))
10160           (setq ps (cdr ps)))))
10161     (if (and gnus-view-pseudos (not not-view))
10162         (while pslist
10163           (when (assq 'execute (car pslist))
10164             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10165                                   (eq gnus-view-pseudos 'not-confirm)))
10166           (setq pslist (cdr pslist)))
10167       (save-excursion
10168         (while pslist
10169           (setq after-article (or (cdr (assq 'article (car pslist)))
10170                                   (gnus-summary-article-number)))
10171           (gnus-summary-goto-subject after-article)
10172           (forward-line 1)
10173           (setq b (point))
10174           (insert "    " (file-name-nondirectory
10175                           (cdr (assq 'name (car pslist))))
10176                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10177           (setq e (point))
10178           (forward-line -1)             ; back to `b'
10179           (gnus-add-text-properties
10180            b (1- e) (list 'gnus-number gnus-reffed-article-number
10181                           gnus-mouse-face-prop gnus-mouse-face))
10182           (gnus-data-enter
10183            after-article gnus-reffed-article-number
10184            gnus-unread-mark b (car pslist) 0 (- e b))
10185           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10186           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10187           (setq pslist (cdr pslist)))))))
10188
10189 (defun gnus-pseudos< (p1 p2)
10190   (let ((c1 (cdr (assq 'action p1)))
10191         (c2 (cdr (assq 'action p2))))
10192     (and c1 c2 (string< c1 c2))))
10193
10194 (defun gnus-request-pseudo-article (props)
10195   (cond ((assq 'execute props)
10196          (gnus-execute-command (cdr (assq 'execute props)))))
10197   (let ((gnus-current-article (gnus-summary-article-number)))
10198     (gnus-run-hooks 'gnus-mark-article-hook)))
10199
10200 (defun gnus-execute-command (command &optional automatic)
10201   (save-excursion
10202     (gnus-article-setup-buffer)
10203     (set-buffer gnus-article-buffer)
10204     (setq buffer-read-only nil)
10205     (let ((command (if automatic command
10206                      (read-string "Command: " (cons command 0)))))
10207       (erase-buffer)
10208       (insert "$ " command "\n\n")
10209       (if gnus-view-pseudo-asynchronously
10210           (start-process "gnus-execute" (current-buffer) shell-file-name
10211                          shell-command-switch command)
10212         (call-process shell-file-name nil t nil
10213                       shell-command-switch command)))))
10214
10215 ;; Summary kill commands.
10216
10217 (defun gnus-summary-edit-global-kill (article)
10218   "Edit the \"global\" kill file."
10219   (interactive (list (gnus-summary-article-number)))
10220   (gnus-group-edit-global-kill article))
10221
10222 (defun gnus-summary-edit-local-kill ()
10223   "Edit a local kill file applied to the current newsgroup."
10224   (interactive)
10225   (setq gnus-current-headers (gnus-summary-article-header))
10226   (gnus-group-edit-local-kill
10227    (gnus-summary-article-number) gnus-newsgroup-name))
10228
10229 ;;; Header reading.
10230
10231 (defun gnus-read-header (id &optional header)
10232   "Read the headers of article ID and enter them into the Gnus system."
10233   (let ((group gnus-newsgroup-name)
10234         (gnus-override-method
10235          (or
10236           gnus-override-method
10237           (and (gnus-news-group-p gnus-newsgroup-name)
10238                (car (gnus-refer-article-methods)))))
10239         where)
10240     ;; First we check to see whether the header in question is already
10241     ;; fetched.
10242     (if (stringp id)
10243         ;; This is a Message-ID.
10244         (setq header (or header (gnus-id-to-header id)))
10245       ;; This is an article number.
10246       (setq header (or header (gnus-summary-article-header id))))
10247     (if (and header
10248              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10249         ;; We have found the header.
10250         header
10251       ;; If this is a sparse article, we have to nix out its
10252       ;; previous entry in the thread hashtb.
10253       (when (and header
10254                  (gnus-summary-article-sparse-p (mail-header-number header)))
10255         (let* ((parent (gnus-parent-id (mail-header-references header)))
10256                (thread (and parent (gnus-id-to-thread parent))))
10257           (when thread
10258             (delq (assq header thread) thread))))
10259       ;; We have to really fetch the header to this article.
10260       (save-excursion
10261         (set-buffer nntp-server-buffer)
10262         (when (setq where (gnus-request-head id group))
10263           (nnheader-fold-continuation-lines)
10264           (goto-char (point-max))
10265           (insert ".\n")
10266           (goto-char (point-min))
10267           (insert "211 ")
10268           (princ (cond
10269                   ((numberp id) id)
10270                   ((cdr where) (cdr where))
10271                   (header (mail-header-number header))
10272                   (t gnus-reffed-article-number))
10273                  (current-buffer))
10274           (insert " Article retrieved.\n"))
10275         (if (or (not where)
10276                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10277             ()                          ; Malformed head.
10278           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10279             (when (and (stringp id)
10280                        (not (string= (gnus-group-real-name group)
10281                                      (car where))))
10282               ;; If we fetched by Message-ID and the article came
10283               ;; from a different group, we fudge some bogus article
10284               ;; numbers for this article.
10285               (mail-header-set-number header gnus-reffed-article-number))
10286             (save-excursion
10287               (set-buffer gnus-summary-buffer)
10288               (decf gnus-reffed-article-number)
10289               (gnus-remove-header (mail-header-number header))
10290               (push header gnus-newsgroup-headers)
10291               (setq gnus-current-headers header)
10292               (push (mail-header-number header) gnus-newsgroup-limit)))
10293           header)))))
10294
10295 (defun gnus-remove-header (number)
10296   "Remove header NUMBER from `gnus-newsgroup-headers'."
10297   (if (and gnus-newsgroup-headers
10298            (= number (mail-header-number (car gnus-newsgroup-headers))))
10299       (pop gnus-newsgroup-headers)
10300     (let ((headers gnus-newsgroup-headers))
10301       (while (and (cdr headers)
10302                   (not (= number (mail-header-number (cadr headers)))))
10303         (pop headers))
10304       (when (cdr headers)
10305         (setcdr headers (cddr headers))))))
10306
10307 ;;;
10308 ;;; summary highlights
10309 ;;;
10310
10311 (defun gnus-highlight-selected-summary ()
10312   "Highlight selected article in summary buffer."
10313   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10314   (when gnus-summary-selected-face
10315     (save-excursion
10316       (let* ((beg (progn (beginning-of-line) (point)))
10317              (end (progn (end-of-line) (point)))
10318              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10319              (from (if (get-text-property beg gnus-mouse-face-prop)
10320                        beg
10321                      (or (next-single-property-change
10322                           beg gnus-mouse-face-prop nil end)
10323                          beg)))
10324              (to
10325               (if (= from end)
10326                   (- from 2)
10327                 (or (next-single-property-change
10328                      from gnus-mouse-face-prop nil end)
10329                     end))))
10330         ;; If no mouse-face prop on line we will have to = from = end,
10331         ;; so we highlight the entire line instead.
10332         (when (= (+ to 2) from)
10333           (setq from beg)
10334           (setq to end))
10335         (if gnus-newsgroup-selected-overlay
10336             ;; Move old overlay.
10337             (gnus-move-overlay
10338              gnus-newsgroup-selected-overlay from to (current-buffer))
10339           ;; Create new overlay.
10340           (gnus-overlay-put
10341            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10342            'face gnus-summary-selected-face))))))
10343
10344 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10345 (defun gnus-summary-highlight-line ()
10346   "Highlight current line according to `gnus-summary-highlight'."
10347   (let* ((list gnus-summary-highlight)
10348          (p (point))
10349          (end (progn (end-of-line) (point)))
10350          ;; now find out where the line starts and leave point there.
10351          (beg (progn (beginning-of-line) (point)))
10352          (article (gnus-summary-article-number))
10353          (score (or (cdr (assq (or article gnus-current-article)
10354                                gnus-newsgroup-scored))
10355                     gnus-summary-default-score 0))
10356          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10357          (inhibit-read-only t))
10358     ;; Eval the cars of the lists until we find a match.
10359     (let ((default gnus-summary-default-score)
10360           (default-high gnus-summary-default-high-score)
10361           (default-low gnus-summary-default-low-score))
10362       (while (and list
10363                   (not (eval (caar list))))
10364         (setq list (cdr list))))
10365     (let ((face (cdar list)))
10366       (unless (eq face (get-text-property beg 'face))
10367         (gnus-put-text-property-excluding-characters-with-faces
10368          beg end 'face
10369          (setq face (if (boundp face) (symbol-value face) face)))
10370         (when gnus-summary-highlight-line-function
10371           (funcall gnus-summary-highlight-line-function article face))))
10372     (goto-char p)))
10373
10374 (defun gnus-update-read-articles (group unread &optional compute)
10375   "Update the list of read articles in GROUP."
10376   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10377          (entry (gnus-gethash group gnus-newsrc-hashtb))
10378          (info (nth 2 entry))
10379          (prev 1)
10380          (unread (sort (copy-sequence unread) '<))
10381          read)
10382     (if (or (not info) (not active))
10383         ;; There is no info on this group if it was, in fact,
10384         ;; killed.  Gnus stores no information on killed groups, so
10385         ;; there's nothing to be done.
10386         ;; One could store the information somewhere temporarily,
10387         ;; perhaps...  Hmmm...
10388         ()
10389       ;; Remove any negative articles numbers.
10390       (while (and unread (< (car unread) 0))
10391         (setq unread (cdr unread)))
10392       ;; Remove any expired article numbers
10393       (while (and unread (< (car unread) (car active)))
10394         (setq unread (cdr unread)))
10395       ;; Compute the ranges of read articles by looking at the list of
10396       ;; unread articles.
10397       (while unread
10398         (when (/= (car unread) prev)
10399           (push (if (= prev (1- (car unread))) prev
10400                   (cons prev (1- (car unread))))
10401                 read))
10402         (setq prev (1+ (car unread)))
10403         (setq unread (cdr unread)))
10404       (when (<= prev (cdr active))
10405         (push (cons prev (cdr active)) read))
10406       (setq read (if (> (length read) 1) (nreverse read) read))
10407       (if compute
10408           read
10409         (save-excursion
10410           (let (setmarkundo)
10411             ;; Propagate the read marks to the backend.
10412             (when (gnus-check-backend-function 'request-set-mark group)
10413               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10414                     (add (gnus-remove-from-range read (gnus-info-read info))))
10415                 (when (or add del)
10416                   (unless (gnus-check-group group)
10417                     (error "Can't open server for %s" group))
10418                   (gnus-request-set-mark
10419                    group (delq nil (list (if add (list add 'add '(read)))
10420                                          (if del (list del 'del '(read))))))
10421                   (setq setmarkundo
10422                         `(gnus-request-set-mark
10423                           ,group
10424                           ',(delq nil (list
10425                                        (if del (list del 'add '(read)))
10426                                        (if add (list add 'del '(read))))))))))
10427             (set-buffer gnus-group-buffer)
10428             (gnus-undo-register
10429               `(progn
10430                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10431                  (gnus-info-set-read ',info ',(gnus-info-read info))
10432                  (gnus-get-unread-articles-in-group ',info
10433                                                     (gnus-active ,group))
10434                  (gnus-group-update-group ,group t)
10435                  ,setmarkundo))))
10436         ;; Enter this list into the group info.
10437         (gnus-info-set-read info read)
10438         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10439         (gnus-get-unread-articles-in-group info (gnus-active group))
10440         t))))
10441
10442 (defun gnus-offer-save-summaries ()
10443   "Offer to save all active summary buffers."
10444   (let (buffers)
10445     ;; Go through all buffers and find all summaries.
10446     (dolist (buffer (buffer-list))
10447       (when (and (setq buffer (buffer-name buffer))
10448                  (string-match "Summary" buffer)
10449                  (save-excursion
10450                    (set-buffer buffer)
10451                    ;; We check that this is, indeed, a summary buffer.
10452                    (and (eq major-mode 'gnus-summary-mode)
10453                         ;; Also make sure this isn't bogus.
10454                         gnus-newsgroup-prepared
10455                         ;; Also make sure that this isn't a
10456                         ;; dead summary buffer.
10457                         (not gnus-dead-summary-mode))))
10458         (push buffer buffers)))
10459     ;; Go through all these summary buffers and offer to save them.
10460     (when buffers
10461       (save-excursion
10462         (map-y-or-n-p
10463          "Update summary buffer %s? "
10464          (lambda (buf)
10465            (switch-to-buffer buf)
10466            (gnus-summary-exit))
10467          buffers)))))
10468
10469
10470 ;;; @ for mime-partial
10471 ;;;
10472
10473 (defun gnus-request-partial-message ()
10474   (save-excursion
10475     (let ((number (gnus-summary-article-number))
10476           (group gnus-newsgroup-name)
10477           (mother gnus-article-buffer))
10478       (set-buffer (get-buffer-create " *Partial Article*"))
10479       (erase-buffer)
10480       (setq mime-preview-buffer mother)
10481       (gnus-request-article-this-buffer number group)
10482       (mime-parse-buffer)
10483       )))
10484
10485 (autoload 'mime-combine-message/partial-pieces-automatically
10486   "mime-partial"
10487   "Internal method to combine message/partial messages automatically.")
10488
10489 (mime-add-condition
10490  'action '((type . message)(subtype . partial)
10491            (major-mode . gnus-original-article-mode)
10492            (method . mime-combine-message/partial-pieces-automatically)
10493            (summary-buffer-exp . gnus-summary-buffer)
10494            (request-partial-message-method . gnus-request-partial-message)
10495            ))
10496
10497
10498 ;;; @ for message/rfc822
10499 ;;;
10500
10501 (defun gnus-mime-extract-message/rfc822 (entity situation)
10502   (let (group article num cwin swin cur)
10503     (with-temp-buffer
10504       (mime-insert-entity-content entity)
10505       (setq group (or (cdr (assq 'group situation))
10506                       (completing-read "Group: "
10507                                        gnus-active-hashtb
10508                                        nil
10509                                        (gnus-read-active-file-p)
10510                                        gnus-newsgroup-name))
10511             article (gnus-request-accept-article group)))
10512     (when (and (consp article)
10513                (numberp (setq article (cdr article))))
10514       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10515             cwin (get-buffer-window (current-buffer) t))
10516       (save-window-excursion
10517         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10518             (select-window swin)
10519           (set-buffer gnus-summary-buffer))
10520         (setq cur gnus-current-article)
10521         (forward-line num)
10522         (let (gnus-show-threads)
10523           (gnus-summary-goto-subject article t))
10524         (gnus-summary-clear-mark-forward 1)
10525         (gnus-summary-goto-subject cur))
10526       (when (and cwin (window-frame cwin))
10527         (select-frame (window-frame cwin)))
10528       (when (boundp 'mime-acting-situation-to-override)
10529         (set-alist 'mime-acting-situation-to-override
10530                    'group
10531                    group)
10532         (set-alist 'mime-acting-situation-to-override
10533                    'after-method
10534                    `(progn
10535                       (save-current-buffer
10536                         (set-buffer gnus-group-buffer)
10537                         (gnus-activate-group ,group))
10538                       (gnus-summary-goto-article ,cur
10539                                                  gnus-show-all-headers)))
10540         (set-alist 'mime-acting-situation-to-override
10541                    'number num)))))
10542
10543 (mime-add-condition
10544  'action '((type . message)(subtype . rfc822)
10545            (major-mode . gnus-original-article-mode)
10546            (method . gnus-mime-extract-message/rfc822)
10547            (mode . "extract")
10548            ))
10549
10550 (mime-add-condition
10551  'action '((type . message)(subtype . news)
10552            (major-mode . gnus-original-article-mode)
10553            (method . gnus-mime-extract-message/rfc822)
10554            (mode . "extract")
10555            ))
10556
10557 (defun gnus-mime-extract-multipart (entity situation)
10558   (let ((children (mime-entity-children entity))
10559         mime-acting-situation-to-override
10560         f)
10561     (while children
10562       (mime-play-entity (car children)
10563                         (cons (assq 'mode situation)
10564                               mime-acting-situation-to-override))
10565       (setq children (cdr children)))
10566     (if (setq f (cdr (assq 'after-method
10567                            mime-acting-situation-to-override)))
10568         (eval f)
10569       )))
10570
10571 (mime-add-condition
10572  'action '((type . multipart)
10573            (method . gnus-mime-extract-multipart)
10574            (mode . "extract")
10575            )
10576  'with-default)
10577
10578
10579 ;;; @ end
10580 ;;;
10581
10582 (defun gnus-summary-setup-default-charset ()
10583   "Setup newsgroup default charset."
10584   (if (equal gnus-newsgroup-name "nndraft:drafts")
10585       (setq gnus-newsgroup-charset nil)
10586     (let* ((ignored-charsets
10587             (or gnus-newsgroup-ephemeral-ignored-charsets
10588                 (append
10589                  (and gnus-newsgroup-name
10590                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10591                  gnus-newsgroup-ignored-charsets))))
10592       (setq gnus-newsgroup-charset
10593             (or gnus-newsgroup-ephemeral-charset
10594                 (and gnus-newsgroup-name
10595                      (gnus-parameter-charset gnus-newsgroup-name))
10596                 gnus-default-charset))
10597       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10598            ignored-charsets))))
10599
10600 ;;;
10601 ;;; Mime Commands
10602 ;;;
10603
10604 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10605   "Display the current article buffer fully MIME-buttonized.
10606 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10607 treated as multipart/mixed."
10608   (interactive "P")
10609   (require 'gnus-art)
10610   (let ((gnus-unbuttonized-mime-types nil)
10611         (gnus-mime-display-multipart-as-mixed show-all-parts))
10612     (gnus-summary-show-article)))
10613
10614 (defun gnus-summary-repair-multipart (article)
10615   "Add a Content-Type header to a multipart article without one."
10616   (interactive (list (gnus-summary-article-number)))
10617   (gnus-with-article article
10618     (message-narrow-to-head)
10619     (message-remove-header "Mime-Version")
10620     (goto-char (point-max))
10621     (insert "Mime-Version: 1.0\n")
10622     (widen)
10623     (when (search-forward "\n--" nil t)
10624       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10625         (message-narrow-to-head)
10626         (message-remove-header "Content-Type")
10627         (goto-char (point-max))
10628         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10629                         separator))
10630         (widen))))
10631   (let (gnus-mark-article-hook)
10632     (gnus-summary-select-article t t nil article)))
10633
10634 (defun gnus-summary-toggle-display-buttonized ()
10635   "Toggle the buttonizing of the article buffer."
10636   (interactive)
10637   (require 'gnus-art)
10638   (if (setq gnus-inhibit-mime-unbuttonizing
10639             (not gnus-inhibit-mime-unbuttonizing))
10640       (let ((gnus-unbuttonized-mime-types nil))
10641         (gnus-summary-show-article))
10642     (gnus-summary-show-article)))
10643
10644 ;;;
10645 ;;; Intelli-mouse commmands
10646 ;;;
10647
10648 (defun gnus-wheel-summary-scroll (event)
10649   (interactive "e")
10650   (let ((amount (if (memq 'shift (event-modifiers event))
10651                     (car gnus-wheel-scroll-amount)
10652                   (cdr gnus-wheel-scroll-amount)))
10653         (direction (- (* (static-if (featurep 'xemacs)
10654                              (event-button event)
10655                            (cond ((eq 'mouse-4 (event-basic-type event))
10656                                   4)
10657                                  ((eq 'mouse-5 (event-basic-type event))
10658                                   5)))
10659                          2) 9))
10660         edge)
10661     (gnus-summary-scroll-up (* amount direction))
10662     (when (gnus-eval-in-buffer-window gnus-article-buffer
10663             (save-restriction
10664               (widen)
10665               (and (if (< 0 direction)
10666                        (gnus-article-next-page 0)
10667                      (gnus-article-prev-page 0)
10668                      (bobp))
10669                    (if (setq edge (get-text-property
10670                                    (point-min) 'gnus-wheel-edge))
10671                        (setq edge (* edge direction))
10672                      (setq edge -1))
10673                    (or (plusp edge)
10674                        (let ((buffer-read-only nil)
10675                              (inhibit-read-only t))
10676                          (put-text-property (point-min) (point-max)
10677                                             'gnus-wheel-edge direction)
10678                          nil))
10679                    (or (> edge gnus-wheel-edge-resistance)
10680                        (let ((buffer-read-only nil)
10681                              (inhibit-read-only t))
10682                          (put-text-property (point-min) (point-max)
10683                                             'gnus-wheel-edge
10684                                             (* (1+ edge) direction))
10685                          nil))
10686                    (eq last-command 'gnus-wheel-summary-scroll))))
10687       (gnus-summary-next-article nil nil (minusp direction)))))
10688
10689 (defun gnus-wheel-install ()
10690   "Enable mouse wheel support on summary window."
10691   (when gnus-use-wheel
10692     (let ((keys
10693            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10694       (dolist (key keys)
10695         (define-key gnus-summary-mode-map key
10696           'gnus-wheel-summary-scroll)))))
10697
10698 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10699
10700 ;;;
10701 ;;; Traditional PGP commmands
10702 ;;;
10703
10704 (defun gnus-summary-decrypt-article (&optional force)
10705   "Decrypt the current article in traditional PGP way.
10706 This will have permanent effect only in mail groups.
10707 If FORCE is non-nil, allow editing of articles even in read-only
10708 groups."
10709   (interactive "P")
10710   (gnus-summary-select-article t)
10711   (gnus-eval-in-buffer-window gnus-article-buffer
10712     (save-excursion
10713       (save-restriction
10714         (widen)
10715         (goto-char (point-min))
10716         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10717           (error "Not a traditional PGP message!"))
10718         (let ((armor-start (match-beginning 0)))
10719           (if (and (pgg-decrypt-region armor-start (point-max))
10720                    (or force (not (gnus-group-read-only-p))))
10721               (let ((inhibit-read-only t)
10722                     buffer-read-only)
10723                 (delete-region armor-start
10724                                (progn
10725                                  (re-search-forward "^-+END PGP" nil t)
10726                                  (beginning-of-line 2)
10727                                  (point)))
10728                 (insert-buffer-substring pgg-output-buffer))))))))
10729
10730 (defun gnus-summary-verify-article ()
10731   "Verify the current article in traditional PGP way."
10732   (interactive)
10733   (save-excursion
10734     (set-buffer gnus-original-article-buffer)
10735     (goto-char (point-min))
10736     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10737       (error "Not a traditional PGP message!"))
10738     (re-search-forward "^-+END PGP" nil t)
10739     (beginning-of-line 2)
10740     (call-interactively (function pgg-verify-region))))
10741
10742 ;;;
10743 ;;; Generic summary marking commands
10744 ;;;
10745
10746 (defvar gnus-summary-marking-alist
10747   '((read gnus-del-mark "d")
10748     (unread gnus-unread-mark "u")
10749     (ticked gnus-ticked-mark "!")
10750     (dormant gnus-dormant-mark "?")
10751     (expirable gnus-expirable-mark "e"))
10752   "An alist of names/marks/keystrokes.")
10753
10754 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10755 (defvar gnus-summary-mark-map)
10756
10757 (defun gnus-summary-make-all-marking-commands ()
10758   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10759   (dolist (elem gnus-summary-marking-alist)
10760     (apply 'gnus-summary-make-marking-command elem)))
10761
10762 (defun gnus-summary-make-marking-command (name mark keystroke)
10763   (let ((map (make-sparse-keymap)))
10764     (define-key gnus-summary-generic-mark-map keystroke map)
10765     (dolist (lway `((next "next" next nil "n")
10766                     (next-unread "next unread" next t "N")
10767                     (prev "previous" prev nil "p")
10768                     (prev-unread "previous unread" prev t "P")
10769                     (nomove "" nil nil ,keystroke)))
10770       (let ((func (gnus-summary-make-marking-command-1
10771                    mark (car lway) lway name)))
10772         (setq func (eval func))
10773         (define-key map (nth 4 lway) func)))))
10774
10775 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10776   `(defun ,(intern
10777             (format "gnus-summary-put-mark-as-%s%s"
10778                     name (if (eq way 'nomove)
10779                              ""
10780                            (concat "-" (symbol-name way)))))
10781      (n)
10782      ,(format
10783        "Mark the current article as %s%s.
10784 If N, the prefix, then repeat N times.
10785 If N is negative, move in reverse order.
10786 The difference between N and the actual number of articles marked is
10787 returned."
10788        name (car (cdr lway)))
10789      (interactive "p")
10790      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10791
10792 (defun gnus-summary-generic-mark (n mark move unread)
10793   "Mark N articles with MARK."
10794   (unless (eq major-mode 'gnus-summary-mode)
10795     (error "This command can only be used in the summary buffer"))
10796   (gnus-summary-show-thread)
10797   (let ((nummove
10798          (cond
10799           ((eq move 'next) 1)
10800           ((eq move 'prev) -1)
10801           (t 0))))
10802     (if (zerop nummove)
10803         (setq n 1)
10804       (when (< n 0)
10805         (setq n (abs n)
10806               nummove (* -1 nummove))))
10807     (while (and (> n 0)
10808                 (gnus-summary-mark-article nil mark)
10809                 (zerop (gnus-summary-next-subject nummove unread t)))
10810       (setq n (1- n)))
10811     (when (/= 0 n)
10812       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10813     (gnus-summary-recenter)
10814     (gnus-summary-position-point)
10815     (gnus-set-mode-line 'summary)
10816     n))
10817
10818 (defun gnus-summary-insert-articles (articles)
10819   (when (setq articles
10820               (gnus-set-difference articles
10821                                    (mapcar (lambda (h) (mail-header-number h))
10822                                            gnus-newsgroup-headers)))
10823     (setq gnus-newsgroup-headers
10824           (merge 'list
10825                  gnus-newsgroup-headers
10826                  (gnus-fetch-headers articles)
10827                  'gnus-article-sort-by-number))
10828     ;; Suppress duplicates?
10829     (when gnus-suppress-duplicates
10830       (gnus-dup-suppress-articles))
10831
10832     ;; We might want to build some more threads first.
10833     (when (and gnus-fetch-old-headers
10834                (eq gnus-headers-retrieved-by 'nov))
10835       (if (eq gnus-fetch-old-headers 'invisible)
10836           (gnus-build-all-threads)
10837         (gnus-build-old-threads)))
10838     ;; Let the Gnus agent mark articles as read.
10839     (when gnus-agent
10840       (gnus-agent-get-undownloaded-list))
10841     ;; Remove list identifiers from subject
10842     (when gnus-list-identifiers
10843       (gnus-summary-remove-list-identifiers))
10844     ;; First and last article in this newsgroup.
10845     (when gnus-newsgroup-headers
10846       (setq gnus-newsgroup-begin
10847             (mail-header-number (car gnus-newsgroup-headers))
10848             gnus-newsgroup-end
10849             (mail-header-number
10850              (gnus-last-element gnus-newsgroup-headers))))
10851     (when gnus-use-scoring
10852       (gnus-possibly-score-headers))))
10853
10854 (defun gnus-summary-insert-old-articles (&optional all)
10855   "Insert all old articles in this group.
10856 If ALL is non-nil, already read articles become readable.
10857 If ALL is a number, fetch this number of articles."
10858   (interactive "P")
10859   (prog1
10860       (let ((old (mapcar 'car gnus-newsgroup-data))
10861             (i (car gnus-newsgroup-active))
10862             older len)
10863         (while (<= i (cdr gnus-newsgroup-active))
10864           (or (memq i old) (push i older))
10865           (incf i))
10866         (setq len (length older))
10867         (cond
10868          ((null older) nil)
10869          ((numberp all)
10870           (if (< all len)
10871               (setq older (subseq older 0 all))))
10872          (all nil)
10873          (t
10874           (if (and (numberp gnus-large-newsgroup)
10875                    (> len gnus-large-newsgroup))
10876               (let ((input
10877                      (read-string
10878                       (format
10879                        "How many articles from %s (default %d): "
10880                        (gnus-limit-string
10881                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10882                        len))))
10883                 (unless (string-match "^[ \t]*$" input)
10884                   (setq all (string-to-number input))
10885                   (if (< all len)
10886                       (setq older (subseq older 0 all))))))))
10887         (if (not older)
10888             (message "No old news.")
10889           (gnus-summary-insert-articles older)
10890           (gnus-summary-limit (gnus-union older old))))
10891     (gnus-summary-position-point)))
10892
10893 (defun gnus-summary-insert-new-articles ()
10894   "Insert all new articles in this group."
10895   (interactive)
10896   (prog1
10897       (let ((old (mapcar 'car gnus-newsgroup-data))
10898             (old-active gnus-newsgroup-active)
10899             (nnmail-fetched-sources (list t))
10900             i new)
10901         (setq gnus-newsgroup-active
10902               (gnus-activate-group gnus-newsgroup-name 'scan))
10903         (setq i (1+ (cdr old-active)))
10904         (while (<= i (cdr gnus-newsgroup-active))
10905           (push i new)
10906           (incf i))
10907         (if (not new)
10908             (message "No gnus is bad news.")
10909           (setq new (nreverse new))
10910           (gnus-summary-insert-articles new)
10911           (setq gnus-newsgroup-unreads
10912                 (append gnus-newsgroup-unreads new))
10913           (gnus-summary-limit (gnus-union old new))))
10914     (gnus-summary-position-point)))
10915
10916 (gnus-summary-make-all-marking-commands)
10917
10918 (gnus-ems-redefine)
10919
10920 (provide 'gnus-sum)
10921
10922 (run-hooks 'gnus-sum-load-hook)
10923
10924 ;;; gnus-sum.el ends here