2001-10-18 Simon Josefsson <jas@extundo.com>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for 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 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-default-high-score 0
203   "*Default threshold for a high scored article.
204 An article will be highlighted as high scored if its score is greater
205 than this score."
206   :group 'gnus-score-default
207   :type 'integer)
208
209 (defcustom gnus-summary-default-low-score 0
210   "*Default threshold for a low scored article.
211 An article will be highlighted as low scored if its score is smaller
212 than this score."
213   :group 'gnus-score-default
214   :type 'integer)
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-move-split-methods nil
368   "*Variable used to suggest where articles are to be moved to.
369 It uses the same syntax as the `gnus-split-methods' variable.
370 However, whereas `gnus-split-methods' specifies file names as targets,
371 this variable specifies group names."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?           ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for souped articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-forwarded-mark ?F
438   "*Mark used for articles that have been forwarded."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-recent-mark ?N
443   "*Mark used for articles that are recent."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-cached-mark ?*
448   "*Mark used for articles that are in the cache."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-saved-mark ?S
453   "*Mark used for articles that have been saved."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-unseen-mark ?.
458   "*Mark used for articles that haven't been seen."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-no-mark ?               ;Whitespace
463   "*Mark used for articles that have no other secondary mark."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-ancient-mark ?O
468   "*Mark used for ancient articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-sparse-mark ?Q
473   "*Mark used for sparsely reffed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-canceled-mark ?G
478   "*Mark used for canceled articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-duplicate-mark ?M
483   "*Mark used for duplicate articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-undownloaded-mark ?@
488   "*Mark used for articles that weren't downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-downloadable-mark ?%
493   "*Mark used for articles that are to be downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-unsendable-mark ?=
498   "*Mark used for articles that won't be sent."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-over-mark ?+
503   "*Score mark used for articles with high scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-below-mark ?-
508   "*Score mark used for articles with low scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-empty-thread-mark ?     ;Whitespace
513   "*There is no thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-not-empty-thread-mark ?=
518   "*There is a thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-view-pseudo-asynchronously nil
523   "*If non-nil, Gnus will view pseudo-articles asynchronously."
524   :group 'gnus-extract-view
525   :type 'boolean)
526
527 (defcustom gnus-auto-expirable-marks
528   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
529         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
530         gnus-souped-mark gnus-duplicate-mark)
531   "*The list of marks converted into expiration if a group is auto-expirable."
532   :version "21.1"
533   :group 'gnus-summary
534   :type '(repeat character))
535
536 (defcustom gnus-inhibit-user-auto-expire t
537   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
538   :version "21.1"
539   :group 'gnus-summary
540   :type 'boolean)
541
542 (defcustom gnus-view-pseudos nil
543   "*If `automatic', pseudo-articles will be viewed automatically.
544 If `not-confirm', pseudos will be viewed automatically, and the user
545 will not be asked to confirm the command."
546   :group 'gnus-extract-view
547   :type '(choice (const :tag "off" nil)
548                  (const automatic)
549                  (const not-confirm)))
550
551 (defcustom gnus-view-pseudos-separately t
552   "*If non-nil, one pseudo-article will be created for each file to be viewed.
553 If nil, all files that use the same viewing command will be given as a
554 list of parameters to that command."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-insert-pseudo-articles t
559   "*If non-nil, insert pseudo-articles when decoding articles."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-summary-dummy-line-format
564   "  %(:                          :%) %S\n"
565   "*The format specification for the dummy roots in the summary buffer.
566 It works along the same lines as a normal formatting string,
567 with some simple extensions.
568
569 %S  The subject"
570   :group 'gnus-threading
571   :type 'string)
572
573 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
574   "*The format specification for the summary mode line.
575 It works along the same lines as a normal formatting string,
576 with some simple extensions:
577
578 %G  Group name
579 %p  Unprefixed group name
580 %A  Current article number
581 %z  Current article score
582 %V  Gnus version
583 %U  Number of unread articles in the group
584 %e  Number of unselected articles in the group
585 %Z  A string with unread/unselected article counts
586 %g  Shortish group name
587 %S  Subject of the current article
588 %u  User-defined spec
589 %s  Current score file name
590 %d  Number of dormant articles
591 %r  Number of articles that have been marked as read in this session
592 %E  Number of articles expunged by the score files"
593   :group 'gnus-summary-format
594   :type 'string)
595
596 (defcustom gnus-list-identifiers nil
597   "Regexp that matches list identifiers to be removed from subject.
598 This can also be a list of regexps."
599   :version "21.1"
600   :group 'gnus-summary-format
601   :group 'gnus-article-hiding
602   :type '(choice (const :tag "none" nil)
603                  (regexp :value ".*")
604                  (repeat :value (".*") regexp)))
605
606 (defcustom gnus-summary-mark-below 0
607   "*Mark all articles with a score below this variable as read.
608 This variable is local to each summary buffer and usually set by the
609 score file."
610   :group 'gnus-score-default
611   :type 'integer)
612
613 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
614   "*List of functions used for sorting articles in the summary buffer.
615
616 Each function takes two articles and returns non-nil if the first
617 article should be sorted before the other.  If you use more than one
618 function, the primary sort function should be the last.  You should
619 probably always include `gnus-article-sort-by-number' in the list of
620 sorting functions -- preferably first.  Also note that sorting by date
621 is often much slower than sorting by number, and the sorting order is
622 very similar.  (Sorting by date means sorting by the time the message
623 was sent, sorting by number means sorting by arrival time.)
624
625 Ready-made functions include `gnus-article-sort-by-number',
626 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
627 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
628
629 When threading is turned on, the variable `gnus-thread-sort-functions'
630 controls how articles are sorted."
631   :group 'gnus-summary-sort
632   :type '(repeat (choice (function-item gnus-article-sort-by-number)
633                          (function-item gnus-article-sort-by-author)
634                          (function-item gnus-article-sort-by-subject)
635                          (function-item gnus-article-sort-by-date)
636                          (function-item gnus-article-sort-by-score)
637                          (function :tag "other"))))
638
639 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
640   "*List of functions used for sorting threads in the summary buffer.
641 By default, threads are sorted by article number.
642
643 Each function takes two threads and returns non-nil if the first
644 thread should be sorted before the other.  If you use more than one
645 function, the primary sort function should be the last.  You should
646 probably always include `gnus-thread-sort-by-number' in the list of
647 sorting functions -- preferably first.  Also note that sorting by date
648 is often much slower than sorting by number, and the sorting order is
649 very similar.  (Sorting by date means sorting by the time the message
650 was sent, sorting by number means sorting by arrival time.)
651
652 Ready-made functions include `gnus-thread-sort-by-number',
653 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
654 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
655 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
656
657 When threading is turned off, the variable
658 `gnus-article-sort-functions' controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
661                          (function-item gnus-thread-sort-by-author)
662                          (function-item gnus-thread-sort-by-subject)
663                          (function-item gnus-thread-sort-by-date)
664                          (function-item gnus-thread-sort-by-score)
665                          (function-item gnus-thread-sort-by-total-score)
666                          (function :tag "other"))))
667
668 (defcustom gnus-thread-score-function '+
669   "*Function used for calculating the total score of a thread.
670
671 The function is called with the scores of the article and each
672 subthread and should then return the score of the thread.
673
674 Some functions you can use are `+', `max', or `min'."
675   :group 'gnus-summary-sort
676   :type 'function)
677
678 (defcustom gnus-summary-expunge-below nil
679   "All articles that have a score less than this variable will be expunged.
680 This variable is local to the summary buffers."
681   :group 'gnus-score-default
682   :type '(choice (const :tag "off" nil)
683                  integer))
684
685 (defcustom gnus-thread-expunge-below nil
686   "All threads that have a total score less than this variable will be expunged.
687 See `gnus-thread-score-function' for en explanation of what a
688 \"thread score\" is.
689
690 This variable is local to the summary buffers."
691   :group 'gnus-threading
692   :group 'gnus-score-default
693   :type '(choice (const :tag "off" nil)
694                  integer))
695
696 (defcustom gnus-summary-mode-hook nil
697   "*A hook for Gnus summary mode.
698 This hook is run before any variables are set in the summary buffer."
699   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
704 (when (featurep 'xemacs)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
706   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
707   (add-hook 'gnus-summary-mode-hook
708             'gnus-xmas-switch-horizontal-scrollbar-off))
709
710 (defcustom gnus-summary-menu-hook nil
711   "*Hook run after the creation of the summary mode menu."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-summary-exit-hook nil
716   "*A hook called on exit from the summary buffer.
717 It will be called with point in the group buffer."
718   :group 'gnus-summary-exit
719   :type 'hook)
720
721 (defcustom gnus-summary-prepare-hook nil
722   "*A hook called after the summary buffer has been generated.
723 If you want to modify the summary buffer, you can use this hook."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-prepared-hook nil
728   "*A hook called as the last thing after the summary buffer has been generated."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-generate-hook nil
733   "*A hook run just before generating the summary buffer.
734 This hook is commonly used to customize threading variables and the
735 like."
736   :group 'gnus-summary-various
737   :type 'hook)
738
739 (defcustom gnus-select-group-hook nil
740   "*A hook called when a newsgroup is selected.
741
742 If you'd like to simplify subjects like the
743 `gnus-summary-next-same-subject' command does, you can use the
744 following hook:
745
746  (setq gnus-select-group-hook
747       (list
748         (lambda ()
749           (mapcar (lambda (header)
750                      (mail-header-set-subject
751                       header
752                       (gnus-simplify-subject
753                        (mail-header-subject header) 're-only)))
754                   gnus-newsgroup-headers))))"
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-select-article-hook nil
759   "*A hook called when an article is selected."
760   :group 'gnus-summary-choose
761   :type 'hook)
762
763 (defcustom gnus-visual-mark-article-hook
764   (list 'gnus-highlight-selected-summary)
765   "*Hook run after selecting an article in the summary buffer.
766 It is meant to be used for highlighting the article in some way.  It
767 is not run if `gnus-visual' is nil."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-parse-headers-hook nil
772   "*A hook called before parsing the headers."
773   :group 'gnus-various
774   :type 'hook)
775
776 (defcustom gnus-exit-group-hook nil
777   "*A hook called when exiting summary mode.
778 This hook is not called from the non-updating exit commands like `Q'."
779   :group 'gnus-various
780   :type 'hook)
781
782 (defcustom gnus-summary-update-hook
783   (list 'gnus-summary-highlight-line)
784   "*A hook called when a summary line is changed.
785 The hook will not be called if `gnus-visual' is nil.
786
787 The default function `gnus-summary-highlight-line' will
788 highlight the line according to the `gnus-summary-highlight'
789 variable."
790   :group 'gnus-summary-visual
791   :type 'hook)
792
793 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
794   "*A hook called when an article is selected for the first time.
795 The hook is intended to mark an article as read (or unread)
796 automatically when it is selected."
797   :group 'gnus-summary-choose
798   :type 'hook)
799
800 (defcustom gnus-group-no-more-groups-hook nil
801   "*A hook run when returning to group mode having no more (unread) groups."
802   :group 'gnus-group-select
803   :type 'hook)
804
805 (defcustom gnus-ps-print-hook nil
806   "*A hook run before ps-printing something from Gnus."
807   :group 'gnus-summary
808   :type 'hook)
809
810 (defcustom gnus-summary-display-arrow
811   (and (fboundp 'display-graphic-p)
812        (display-graphic-p))
813   "*If non-nil, display an arrow highlighting the current article."
814   :version "21.1"
815   :group 'gnus-summary
816   :type 'boolean)
817
818 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
819   "Face used for highlighting the current article in the summary buffer."
820   :group 'gnus-summary-visual
821   :type 'face)
822
823 (defcustom gnus-summary-highlight
824   '(((= mark gnus-canceled-mark)
825      . gnus-summary-cancelled-face)
826     ((and (> score default-high)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-high-ticked-face)
830     ((and (< score default-low)
831           (or (= mark gnus-dormant-mark)
832               (= mark gnus-ticked-mark)))
833      . gnus-summary-low-ticked-face)
834     ((or (= mark gnus-dormant-mark)
835          (= mark gnus-ticked-mark))
836      . gnus-summary-normal-ticked-face)
837     ((and (> score default-high) (= mark gnus-ancient-mark))
838      . gnus-summary-high-ancient-face)
839     ((and (< score default-low) (= mark gnus-ancient-mark))
840      . gnus-summary-low-ancient-face)
841     ((= mark gnus-ancient-mark)
842      . gnus-summary-normal-ancient-face)
843     ((and (> score default-high) (= mark gnus-unread-mark))
844      . gnus-summary-high-unread-face)
845     ((and (< score default-low) (= mark gnus-unread-mark))
846      . gnus-summary-low-unread-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
850                                                   gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
853                                                  gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default-high)
861      . gnus-summary-high-read-face)
862     ((< score default-low)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:        The article's score
876 default:      The default article score.
877 default-high: The default score for high scored articles.
878 default-low:  The default score for low scored articles.
879 below:        The score below which articles are automatically marked as read.
880 mark:         The articles mark."
881   :group 'gnus-summary-visual
882   :type '(repeat (cons (sexp :tag "Form" nil)
883                        face)))
884
885 (defcustom gnus-alter-header-function nil
886   "Function called to allow alteration of article header structures.
887 The function is called with one parameter, the article header vector,
888 which it may alter in any way.")
889
890 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
891   "Variable that says which function should be used to decode a string with encoded words.")
892
893 (defcustom gnus-extra-headers nil
894   "*Extra headers to parse."
895   :version "21.1"
896   :group 'gnus-summary
897   :type '(repeat symbol))
898
899 (defcustom gnus-ignored-from-addresses
900   (and user-mail-address (regexp-quote user-mail-address))
901   "*Regexp of From headers that may be suppressed in favor of To headers."
902   :version "21.1"
903   :group 'gnus-summary
904   :type 'regexp)
905
906 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
907   "List of charsets that should be ignored.
908 When these charsets are used in the \"charset\" parameter, the
909 default charset will be used instead."
910   :version "21.1"
911   :type '(repeat symbol)
912   :group 'gnus-charset)
913
914 (gnus-define-group-parameter
915  ignored-charsets
916  :type list
917  :function-document
918  "Return the ignored charsets of GROUP."
919  :variable gnus-group-ignored-charsets-alist
920  :variable-default
921  '(("alt\\.chinese\\.text" iso-8859-1))
922  :variable-document
923  "Alist of regexps (to match group names) and charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926  :variable-group gnus-charset
927  :variable-type '(repeat (cons (regexp :tag "Group")
928                                (repeat symbol)))
929  :parameter-type '(choice :tag "Ignored charsets"
930                           :value nil
931                           (repeat (symbol)))
932  :parameter-document       "\
933 List of charsets that should be ignored.
934
935 When these charsets are used in the \"charset\" parameter, the
936 default charset will be used instead.")
937
938 (defcustom gnus-group-highlight-words-alist nil
939   "Alist of group regexps and highlight regexps.
940 This variable uses the same syntax as `gnus-emphasis-alist'."
941   :version "21.1"
942   :type '(repeat (cons (regexp :tag "Group")
943                        (repeat (list (regexp :tag "Highlight regexp")
944                                      (number :tag "Group for entire word" 0)
945                                      (number :tag "Group for displayed part" 0)
946                                      (symbol :tag "Face"
947                                              gnus-emphasis-highlight-words)))))
948   :group 'gnus-summary-visual)
949
950 (defcustom gnus-summary-show-article-charset-alist
951   nil
952   "Alist of number and charset.
953 The article will be shown with the charset corresponding to the
954 numbered argument.
955 For example: ((1 . cn-gb-2312) (2 . big5))."
956   :version "21.1"
957   :type '(repeat (cons (number :tag "Argument" 1)
958                        (symbol :tag "Charset")))
959   :group 'gnus-charset)
960
961 (defcustom gnus-preserve-marks t
962   "Whether marks are preserved when moving, copying and respooling messages."
963   :version "21.1"
964   :type 'boolean
965   :group 'gnus-summary-marks)
966
967 (defcustom gnus-alter-articles-to-read-function nil
968   "Function to be called to alter the list of articles to be selected."
969   :type '(choice (const nil) function)
970   :group 'gnus-summary)
971
972 (defcustom gnus-orphan-score nil
973   "*All orphans get this score added.  Set in the score file."
974   :group 'gnus-score-default
975   :type '(choice (const nil)
976                  integer))
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 (defcustom gnus-summary-save-parts-default-mime "image/.*"
987   "*A regexp to match MIME parts when saving multiple parts of a message
988 with gnus-summary-save-parts (X m). This regexp will be used by default
989 when prompting the user for which type of files to save."
990   :group 'gnus-summary
991   :type 'regexp)
992
993 (defcustom gnus-read-all-available-headers nil
994   "Whether Gnus should parse all headers made available to it.
995 This is mostly relevant for slow backends where the user may
996 wish to widen the summary buffer to include all headers
997 that were fetched.  Say, for nnultimate groups."
998   :group 'gnus-summary
999   :type '(choice boolean regexp))
1000
1001 ;;; Internal variables
1002
1003 (defvar gnus-summary-display-cache nil)
1004 (defvar gnus-article-mime-handles nil)
1005 (defvar gnus-article-decoded-p nil)
1006 (defvar gnus-article-charset nil)
1007 (defvar gnus-article-ignored-charsets nil)
1008 (defvar gnus-scores-exclude-files nil)
1009 (defvar gnus-page-broken nil)
1010 (defvar gnus-inhibit-mime-unbuttonizing nil)
1011
1012 (defvar gnus-original-article nil)
1013 (defvar gnus-article-internal-prepare-hook nil)
1014 (defvar gnus-newsgroup-process-stack nil)
1015
1016 (defvar gnus-thread-indent-array nil)
1017 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1018 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1019   "Function called to sort the articles within a thread after it has been gathered together.")
1020
1021 (defvar gnus-summary-save-parts-type-history nil)
1022 (defvar gnus-summary-save-parts-last-directory nil)
1023
1024 (defvar gnus-summary-save-parts-type-history nil)
1025 (defvar gnus-summary-save-parts-last-directory nil)
1026
1027 ;; Avoid highlighting in kill files.
1028 (defvar gnus-summary-inhibit-highlight nil)
1029 (defvar gnus-newsgroup-selected-overlay nil)
1030 (defvar gnus-inhibit-limiting nil)
1031 (defvar gnus-newsgroup-adaptive-score-file nil)
1032 (defvar gnus-current-score-file nil)
1033 (defvar gnus-current-move-group nil)
1034 (defvar gnus-current-copy-group nil)
1035 (defvar gnus-current-crosspost-group nil)
1036 (defvar gnus-newsgroup-display nil)
1037
1038 (defvar gnus-newsgroup-dependencies nil)
1039 (defvar gnus-newsgroup-adaptive nil)
1040 (defvar gnus-summary-display-article-function nil)
1041 (defvar gnus-summary-highlight-line-function nil
1042   "Function called after highlighting a summary line.")
1043
1044 (defvar gnus-summary-line-format-alist
1045   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1046     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1047     (?s gnus-tmp-subject-or-nil ?s)
1048     (?n gnus-tmp-name ?s)
1049     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1050         ?s)
1051     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1052             gnus-tmp-from) ?s)
1053     (?F gnus-tmp-from ?s)
1054     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1055     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1056     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1057     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1058     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1059     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1060     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1061     (?L gnus-tmp-lines ?s)
1062     (?I gnus-tmp-indentation ?s)
1063     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1064     (?R gnus-tmp-replied ?c)
1065     (?\[ gnus-tmp-opening-bracket ?c)
1066     (?\] gnus-tmp-closing-bracket ?c)
1067     (?\> (make-string gnus-tmp-level ? ) ?s)
1068     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1069     (?i gnus-tmp-score ?d)
1070     (?z gnus-tmp-score-char ?c)
1071     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1072     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1073     (?U gnus-tmp-unread ?c)
1074     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1075         ?s)
1076     (?t (gnus-summary-number-of-articles-in-thread
1077          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1078         ?d)
1079     (?e (gnus-summary-number-of-articles-in-thread
1080          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1081         ?c)
1082     (?u gnus-tmp-user-defined ?s)
1083     (?P (gnus-pick-line-number) ?d)
1084     (?B gnus-tmp-thread-tree-header-string ?s)
1085     (user-date (gnus-user-date 
1086                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1087   "An alist of format specifications that can appear in summary lines.
1088 These are paired with what variables they correspond with, along with
1089 the type of the variable (string, integer, character, etc).")
1090
1091 (defvar gnus-summary-dummy-line-format-alist
1092   `((?S gnus-tmp-subject ?s)
1093     (?N gnus-tmp-number ?d)
1094     (?u gnus-tmp-user-defined ?s)))
1095
1096 (defvar gnus-summary-mode-line-format-alist
1097   `((?G gnus-tmp-group-name ?s)
1098     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1099     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1100     (?A gnus-tmp-article-number ?d)
1101     (?Z gnus-tmp-unread-and-unselected ?s)
1102     (?V gnus-version ?s)
1103     (?U gnus-tmp-unread-and-unticked ?d)
1104     (?S gnus-tmp-subject ?s)
1105     (?e gnus-tmp-unselected ?d)
1106     (?u gnus-tmp-user-defined ?s)
1107     (?d (length gnus-newsgroup-dormant) ?d)
1108     (?t (length gnus-newsgroup-marked) ?d)
1109     (?r (length gnus-newsgroup-reads) ?d)
1110     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1111     (?E gnus-newsgroup-expunged-tally ?d)
1112     (?s (gnus-current-score-file-nondirectory) ?s)))
1113
1114 (defvar gnus-last-search-regexp nil
1115   "Default regexp for article search command.")
1116
1117 (defvar gnus-last-shell-command nil
1118   "Default shell command on article.")
1119
1120 (defvar gnus-newsgroup-begin nil)
1121 (defvar gnus-newsgroup-end nil)
1122 (defvar gnus-newsgroup-last-rmail nil)
1123 (defvar gnus-newsgroup-last-mail nil)
1124 (defvar gnus-newsgroup-last-folder nil)
1125 (defvar gnus-newsgroup-last-file nil)
1126 (defvar gnus-newsgroup-auto-expire nil)
1127 (defvar gnus-newsgroup-active nil)
1128
1129 (defvar gnus-newsgroup-data nil)
1130 (defvar gnus-newsgroup-data-reverse nil)
1131 (defvar gnus-newsgroup-limit nil)
1132 (defvar gnus-newsgroup-limits nil)
1133
1134 (defvar gnus-newsgroup-unreads nil
1135   "List of unread articles in the current newsgroup.")
1136
1137 (defvar gnus-newsgroup-unselected nil
1138   "List of unselected unread articles in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-reads nil
1141   "Alist of read articles and article marks in the current newsgroup.")
1142
1143 (defvar gnus-newsgroup-expunged-tally nil)
1144
1145 (defvar gnus-newsgroup-marked nil
1146   "List of ticked articles in the current newsgroup (a subset of unread art).")
1147
1148 (defvar gnus-newsgroup-killed nil
1149   "List of ranges of articles that have been through the scoring process.")
1150
1151 (defvar gnus-newsgroup-cached nil
1152   "List of articles that come from the article cache.")
1153
1154 (defvar gnus-newsgroup-saved nil
1155   "List of articles that have been saved.")
1156
1157 (defvar gnus-newsgroup-kill-headers nil)
1158
1159 (defvar gnus-newsgroup-replied nil
1160   "List of articles that have been replied to in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-forwarded nil
1163   "List of articles that have been forwarded in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-recent nil
1166   "List of articles that have are recent in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-expirable nil
1169   "List of articles in the current newsgroup that can be expired.")
1170
1171 (defvar gnus-newsgroup-processable nil
1172   "List of articles in the current newsgroup that can be processed.")
1173
1174 (defvar gnus-newsgroup-downloadable nil
1175   "List of articles in the current newsgroup that can be processed.")
1176
1177 (defvar gnus-newsgroup-undownloaded nil
1178   "List of articles in the current newsgroup that haven't been downloaded..")
1179
1180 (defvar gnus-newsgroup-unsendable nil
1181   "List of articles in the current newsgroup that won't be sent.")
1182
1183 (defvar gnus-newsgroup-bookmarks nil
1184   "List of articles in the current newsgroup that have bookmarks.")
1185
1186 (defvar gnus-newsgroup-dormant nil
1187   "List of dormant articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-unseen nil
1190   "List of unseen articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-seen nil
1193   "Range of seen articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-articles nil
1196   "List of articles in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-scored nil
1199   "List of scored articles in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-headers nil
1202   "List of article headers in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-threads nil)
1205
1206 (defvar gnus-newsgroup-prepared nil
1207   "Whether the current group has been prepared properly.")
1208
1209 (defvar gnus-newsgroup-ancient nil
1210   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-sparse nil)
1213
1214 (defvar gnus-current-article nil)
1215 (defvar gnus-article-current nil)
1216 (defvar gnus-current-headers nil)
1217 (defvar gnus-have-all-headers nil)
1218 (defvar gnus-last-article nil)
1219 (defvar gnus-newsgroup-history nil)
1220 (defvar gnus-newsgroup-charset nil)
1221 (defvar gnus-newsgroup-ephemeral-charset nil)
1222 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1223
1224 (defvar gnus-article-before-search nil)
1225
1226 (defconst gnus-summary-local-variables
1227   '(gnus-newsgroup-name
1228     gnus-newsgroup-begin gnus-newsgroup-end
1229     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1230     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1231     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1232     gnus-newsgroup-unselected gnus-newsgroup-marked
1233     gnus-newsgroup-reads gnus-newsgroup-saved
1234     gnus-newsgroup-replied gnus-newsgroup-forwarded
1235     gnus-newsgroup-recent
1236     gnus-newsgroup-expirable
1237     gnus-newsgroup-processable gnus-newsgroup-killed
1238     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1239     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1240     gnus-newsgroup-seen gnus-newsgroup-articles
1241     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1242     gnus-newsgroup-headers gnus-newsgroup-threads
1243     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1244     gnus-current-article gnus-current-headers gnus-have-all-headers
1245     gnus-last-article gnus-article-internal-prepare-hook
1246     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1247     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1248     gnus-thread-expunge-below
1249     gnus-score-alist gnus-current-score-file
1250     (gnus-summary-expunge-below . global)
1251     (gnus-summary-mark-below . global)
1252     (gnus-orphan-score . global)
1253     gnus-newsgroup-active gnus-scores-exclude-files
1254     gnus-newsgroup-history gnus-newsgroup-ancient
1255     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1256     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1257     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1258     (gnus-newsgroup-expunged-tally . 0)
1259     gnus-cache-removable-articles gnus-newsgroup-cached
1260     gnus-newsgroup-data gnus-newsgroup-data-reverse
1261     gnus-newsgroup-limit gnus-newsgroup-limits
1262     gnus-newsgroup-charset gnus-newsgroup-display)
1263   "Variables that are buffer-local to the summary buffers.")
1264
1265 (defvar gnus-newsgroup-variables nil
1266   "Variables that have separate values in the newsgroups.")
1267
1268 ;; Byte-compiler warning.
1269 (eval-when-compile (defvar gnus-article-mode-map))
1270
1271 ;; MIME stuff.
1272
1273 (defvar gnus-decode-encoded-word-methods
1274   '(mail-decode-encoded-word-string)
1275   "List of methods used to decode encoded words.
1276
1277 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1278 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1279 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1280 whose names match REGEXP.
1281
1282 For example:
1283 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1284  mail-decode-encoded-word-string
1285  (\"chinese\" . rfc1843-decode-string))")
1286
1287 (defvar gnus-decode-encoded-word-methods-cache nil)
1288
1289 (defun gnus-multi-decode-encoded-word-string (string)
1290   "Apply the functions from `gnus-encoded-word-methods' that match."
1291   (unless (and gnus-decode-encoded-word-methods-cache
1292                (eq gnus-newsgroup-name
1293                    (car gnus-decode-encoded-word-methods-cache)))
1294     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1295     (mapcar (lambda (x)
1296               (if (symbolp x)
1297                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1298                 (if (and gnus-newsgroup-name
1299                          (string-match (car x) gnus-newsgroup-name))
1300                     (nconc gnus-decode-encoded-word-methods-cache
1301                            (list (cdr x))))))
1302             gnus-decode-encoded-word-methods))
1303   (let ((xlist gnus-decode-encoded-word-methods-cache))
1304     (pop xlist)
1305     (while xlist
1306       (setq string (funcall (pop xlist) string))))
1307   string)
1308
1309 ;; Subject simplification.
1310
1311 (defun gnus-simplify-whitespace (str)
1312   "Remove excessive whitespace from STR."
1313   (let ((mystr str))
1314     ;; Multiple spaces.
1315     (while (string-match "[ \t][ \t]+" mystr)
1316       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1317                           " "
1318                           (substring mystr (match-end 0)))))
1319     ;; Leading spaces.
1320     (when (string-match "^[ \t]+" mystr)
1321       (setq mystr (substring mystr (match-end 0))))
1322     ;; Trailing spaces.
1323     (when (string-match "[ \t]+$" mystr)
1324       (setq mystr (substring mystr 0 (match-beginning 0))))
1325     mystr))
1326
1327 (defsubst gnus-simplify-subject-re (subject)
1328   "Remove \"Re:\" from subject lines."
1329   (if (string-match message-subject-re-regexp subject)
1330       (substring subject (match-end 0))
1331     subject))
1332
1333 (defun gnus-simplify-subject (subject &optional re-only)
1334   "Remove `Re:' and words in parentheses.
1335 If RE-ONLY is non-nil, strip leading `Re:'s only."
1336   (let ((case-fold-search t))           ;Ignore case.
1337     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1338     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1339       (setq subject (substring subject (match-end 0))))
1340     ;; Remove uninteresting prefixes.
1341     (when (and (not re-only)
1342                gnus-simplify-ignored-prefixes
1343                (string-match gnus-simplify-ignored-prefixes subject))
1344       (setq subject (substring subject (match-end 0))))
1345     ;; Remove words in parentheses from end.
1346     (unless re-only
1347       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1348         (setq subject (substring subject 0 (match-beginning 0)))))
1349     ;; Return subject string.
1350     subject))
1351
1352 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1353 ;; all whitespace.
1354 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1355   (goto-char (point-min))
1356   (while (re-search-forward regexp nil t)
1357     (replace-match (or newtext ""))))
1358
1359 (defun gnus-simplify-buffer-fuzzy ()
1360   "Simplify string in the buffer fuzzily.
1361 The string in the accessible portion of the current buffer is simplified.
1362 It is assumed to be a single-line subject.
1363 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1364 matter is removed.  Additional things can be deleted by setting
1365 `gnus-simplify-subject-fuzzy-regexp'."
1366   (let ((case-fold-search t)
1367         (modified-tick))
1368     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1369
1370     (while (not (eq modified-tick (buffer-modified-tick)))
1371       (setq modified-tick (buffer-modified-tick))
1372       (cond
1373        ((listp gnus-simplify-subject-fuzzy-regexp)
1374         (mapcar 'gnus-simplify-buffer-fuzzy-step
1375                 gnus-simplify-subject-fuzzy-regexp))
1376        (gnus-simplify-subject-fuzzy-regexp
1377         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1378       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1379       (gnus-simplify-buffer-fuzzy-step
1380        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1381       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1382
1383     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1384     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1385     (gnus-simplify-buffer-fuzzy-step " $")
1386     (gnus-simplify-buffer-fuzzy-step "^ +")))
1387
1388 (defun gnus-simplify-subject-fuzzy (subject)
1389   "Simplify a subject string fuzzily.
1390 See `gnus-simplify-buffer-fuzzy' for details."
1391   (save-excursion
1392     (gnus-set-work-buffer)
1393     (let ((case-fold-search t))
1394       ;; Remove uninteresting prefixes.
1395       (when (and gnus-simplify-ignored-prefixes
1396                  (string-match gnus-simplify-ignored-prefixes subject))
1397         (setq subject (substring subject (match-end 0))))
1398       (insert subject)
1399       (inline (gnus-simplify-buffer-fuzzy))
1400       (buffer-string))))
1401
1402 (defsubst gnus-simplify-subject-fully (subject)
1403   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1404   (cond
1405    (gnus-simplify-subject-functions
1406     (gnus-map-function gnus-simplify-subject-functions subject))
1407    ((null gnus-summary-gather-subject-limit)
1408     (gnus-simplify-subject-re subject))
1409    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1410     (gnus-simplify-subject-fuzzy subject))
1411    ((numberp gnus-summary-gather-subject-limit)
1412     (gnus-limit-string (gnus-simplify-subject-re subject)
1413                        gnus-summary-gather-subject-limit))
1414    (t
1415     subject)))
1416
1417 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1418   "Check whether two subjects are equal.
1419 If optional argument simple-first is t, first argument is already
1420 simplified."
1421   (cond
1422    ((null simple-first)
1423     (equal (gnus-simplify-subject-fully s1)
1424            (gnus-simplify-subject-fully s2)))
1425    (t
1426     (equal s1
1427            (gnus-simplify-subject-fully s2)))))
1428
1429 (defun gnus-summary-bubble-group ()
1430   "Increase the score of the current group.
1431 This is a handy function to add to `gnus-summary-exit-hook' to
1432 increase the score of each group you read."
1433   (gnus-group-add-score gnus-newsgroup-name))
1434
1435 \f
1436 ;;;
1437 ;;; Gnus summary mode
1438 ;;;
1439
1440 (put 'gnus-summary-mode 'mode-class 'special)
1441
1442 (defvar gnus-article-commands-menu)
1443
1444 (when t
1445   ;; Non-orthogonal keys
1446
1447   (gnus-define-keys gnus-summary-mode-map
1448     " " gnus-summary-next-page
1449     "\177" gnus-summary-prev-page
1450     [delete] gnus-summary-prev-page
1451     [backspace] gnus-summary-prev-page
1452     "\r" gnus-summary-scroll-up
1453     "\M-\r" gnus-summary-scroll-down
1454     "n" gnus-summary-next-unread-article
1455     "p" gnus-summary-prev-unread-article
1456     "N" gnus-summary-next-article
1457     "P" gnus-summary-prev-article
1458     "\M-\C-n" gnus-summary-next-same-subject
1459     "\M-\C-p" gnus-summary-prev-same-subject
1460     "\M-n" gnus-summary-next-unread-subject
1461     "\M-p" gnus-summary-prev-unread-subject
1462     "." gnus-summary-first-unread-article
1463     "," gnus-summary-best-unread-article
1464     "\M-s" gnus-summary-search-article-forward
1465     "\M-r" gnus-summary-search-article-backward
1466     "<" gnus-summary-beginning-of-article
1467     ">" gnus-summary-end-of-article
1468     "j" gnus-summary-goto-article
1469     "^" gnus-summary-refer-parent-article
1470     "\M-^" gnus-summary-refer-article
1471     "u" gnus-summary-tick-article-forward
1472     "!" gnus-summary-tick-article-forward
1473     "U" gnus-summary-tick-article-backward
1474     "d" gnus-summary-mark-as-read-forward
1475     "D" gnus-summary-mark-as-read-backward
1476     "E" gnus-summary-mark-as-expirable
1477     "\M-u" gnus-summary-clear-mark-forward
1478     "\M-U" gnus-summary-clear-mark-backward
1479     "k" gnus-summary-kill-same-subject-and-select
1480     "\C-k" gnus-summary-kill-same-subject
1481     "\M-\C-k" gnus-summary-kill-thread
1482     "\M-\C-l" gnus-summary-lower-thread
1483     "e" gnus-summary-edit-article
1484     "#" gnus-summary-mark-as-processable
1485     "\M-#" gnus-summary-unmark-as-processable
1486     "\M-\C-t" gnus-summary-toggle-threads
1487     "\M-\C-s" gnus-summary-show-thread
1488     "\M-\C-h" gnus-summary-hide-thread
1489     "\M-\C-f" gnus-summary-next-thread
1490     "\M-\C-b" gnus-summary-prev-thread
1491     [(meta down)] gnus-summary-next-thread
1492     [(meta up)] gnus-summary-prev-thread
1493     "\M-\C-u" gnus-summary-up-thread
1494     "\M-\C-d" gnus-summary-down-thread
1495     "&" gnus-summary-execute-command
1496     "c" gnus-summary-catchup-and-exit
1497     "\C-w" gnus-summary-mark-region-as-read
1498     "\C-t" gnus-summary-toggle-truncation
1499     "?" gnus-summary-mark-as-dormant
1500     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1501     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1502     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1503     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1504     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1505     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1506     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1507     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1508     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1509     "=" gnus-summary-expand-window
1510     "\C-x\C-s" gnus-summary-reselect-current-group
1511     "\M-g" gnus-summary-rescan-group
1512     "w" gnus-summary-stop-page-breaking
1513     "\C-c\C-r" gnus-summary-caesar-message
1514     "f" gnus-summary-followup
1515     "F" gnus-summary-followup-with-original
1516     "C" gnus-summary-cancel-article
1517     "r" gnus-summary-reply
1518     "R" gnus-summary-reply-with-original
1519     "\C-c\C-f" gnus-summary-mail-forward
1520     "o" gnus-summary-save-article
1521     "\C-o" gnus-summary-save-article-mail
1522     "|" gnus-summary-pipe-output
1523     "\M-k" gnus-summary-edit-local-kill
1524     "\M-K" gnus-summary-edit-global-kill
1525     ;; "V" gnus-version
1526     "\C-c\C-d" gnus-summary-describe-group
1527     "q" gnus-summary-exit
1528     "Q" gnus-summary-exit-no-update
1529     "\C-c\C-i" gnus-info-find-node
1530     gnus-mouse-2 gnus-mouse-pick-article
1531     "m" gnus-summary-mail-other-window
1532     "a" gnus-summary-post-news
1533     "x" gnus-summary-limit-to-unread
1534     "s" gnus-summary-isearch-article
1535     "t" gnus-summary-toggle-header
1536     "g" gnus-summary-show-article
1537     "l" gnus-summary-goto-last-article
1538     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1539     "\C-d" gnus-summary-enter-digest-group
1540     "\M-\C-d" gnus-summary-read-document
1541     "\M-\C-e" gnus-summary-edit-parameters
1542     "\M-\C-a" gnus-summary-customize-parameters
1543     "\C-c\C-b" gnus-bug
1544     "*" gnus-cache-enter-article
1545     "\M-*" gnus-cache-remove-article
1546     "\M-&" gnus-summary-universal-argument
1547     "\C-l" gnus-recenter
1548     "I" gnus-summary-increase-score
1549     "L" gnus-summary-lower-score
1550     "\M-i" gnus-symbolic-argument
1551     "h" gnus-summary-select-article-buffer
1552
1553     "b" gnus-article-view-part
1554     "\M-t" gnus-summary-toggle-display-buttonized
1555
1556     "V" gnus-summary-score-map
1557     "X" gnus-uu-extract-map
1558     "S" gnus-summary-send-map)
1559
1560   ;; Sort of orthogonal keymap
1561   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1562     "t" gnus-summary-tick-article-forward
1563     "!" gnus-summary-tick-article-forward
1564     "d" gnus-summary-mark-as-read-forward
1565     "r" gnus-summary-mark-as-read-forward
1566     "c" gnus-summary-clear-mark-forward
1567     " " gnus-summary-clear-mark-forward
1568     "e" gnus-summary-mark-as-expirable
1569     "x" gnus-summary-mark-as-expirable
1570     "?" gnus-summary-mark-as-dormant
1571     "b" gnus-summary-set-bookmark
1572     "B" gnus-summary-remove-bookmark
1573     "#" gnus-summary-mark-as-processable
1574     "\M-#" gnus-summary-unmark-as-processable
1575     "S" gnus-summary-limit-include-expunged
1576     "C" gnus-summary-catchup
1577     "H" gnus-summary-catchup-to-here
1578     "h" gnus-summary-catchup-from-here
1579     "\C-c" gnus-summary-catchup-all
1580     "k" gnus-summary-kill-same-subject-and-select
1581     "K" gnus-summary-kill-same-subject
1582     "P" gnus-uu-mark-map)
1583
1584   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1585     "c" gnus-summary-clear-above
1586     "u" gnus-summary-tick-above
1587     "m" gnus-summary-mark-above
1588     "k" gnus-summary-kill-below)
1589
1590   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1591     "/" gnus-summary-limit-to-subject
1592     "n" gnus-summary-limit-to-articles
1593     "w" gnus-summary-pop-limit
1594     "s" gnus-summary-limit-to-subject
1595     "a" gnus-summary-limit-to-author
1596     "u" gnus-summary-limit-to-unread
1597     "m" gnus-summary-limit-to-marks
1598     "M" gnus-summary-limit-exclude-marks
1599     "v" gnus-summary-limit-to-score
1600     "*" gnus-summary-limit-include-cached
1601     "D" gnus-summary-limit-include-dormant
1602     "T" gnus-summary-limit-include-thread
1603     "d" gnus-summary-limit-exclude-dormant
1604     "t" gnus-summary-limit-to-age
1605     "x" gnus-summary-limit-to-extra
1606     "p" gnus-summary-limit-to-display-predicate
1607     "E" gnus-summary-limit-include-expunged
1608     "c" gnus-summary-limit-exclude-childless-dormant
1609     "C" gnus-summary-limit-mark-excluded-as-read
1610     "o" gnus-summary-insert-old-articles
1611     "N" gnus-summary-insert-new-articles)
1612
1613   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1614     "n" gnus-summary-next-unread-article
1615     "p" gnus-summary-prev-unread-article
1616     "N" gnus-summary-next-article
1617     "P" gnus-summary-prev-article
1618     "\C-n" gnus-summary-next-same-subject
1619     "\C-p" gnus-summary-prev-same-subject
1620     "\M-n" gnus-summary-next-unread-subject
1621     "\M-p" gnus-summary-prev-unread-subject
1622     "f" gnus-summary-first-unread-article
1623     "b" gnus-summary-best-unread-article
1624     "j" gnus-summary-goto-article
1625     "g" gnus-summary-goto-subject
1626     "l" gnus-summary-goto-last-article
1627     "o" gnus-summary-pop-article)
1628
1629   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1630     "k" gnus-summary-kill-thread
1631     "l" gnus-summary-lower-thread
1632     "i" gnus-summary-raise-thread
1633     "T" gnus-summary-toggle-threads
1634     "t" gnus-summary-rethread-current
1635     "^" gnus-summary-reparent-thread
1636     "s" gnus-summary-show-thread
1637     "S" gnus-summary-show-all-threads
1638     "h" gnus-summary-hide-thread
1639     "H" gnus-summary-hide-all-threads
1640     "n" gnus-summary-next-thread
1641     "p" gnus-summary-prev-thread
1642     "u" gnus-summary-up-thread
1643     "o" gnus-summary-top-thread
1644     "d" gnus-summary-down-thread
1645     "#" gnus-uu-mark-thread
1646     "\M-#" gnus-uu-unmark-thread)
1647
1648   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1649     "g" gnus-summary-prepare
1650     "c" gnus-summary-insert-cached-articles)
1651
1652   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1653     "c" gnus-summary-catchup-and-exit
1654     "C" gnus-summary-catchup-all-and-exit
1655     "E" gnus-summary-exit-no-update
1656     "Q" gnus-summary-exit
1657     "Z" gnus-summary-exit
1658     "n" gnus-summary-catchup-and-goto-next-group
1659     "R" gnus-summary-reselect-current-group
1660     "G" gnus-summary-rescan-group
1661     "N" gnus-summary-next-group
1662     "s" gnus-summary-save-newsrc
1663     "P" gnus-summary-prev-group)
1664
1665   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1666     " " gnus-summary-next-page
1667     "n" gnus-summary-next-page
1668     "\177" gnus-summary-prev-page
1669     [delete] gnus-summary-prev-page
1670     "p" gnus-summary-prev-page
1671     "\r" gnus-summary-scroll-up
1672     "\M-\r" gnus-summary-scroll-down
1673     "<" gnus-summary-beginning-of-article
1674     ">" gnus-summary-end-of-article
1675     "b" gnus-summary-beginning-of-article
1676     "e" gnus-summary-end-of-article
1677     "^" gnus-summary-refer-parent-article
1678     "r" gnus-summary-refer-parent-article
1679     "D" gnus-summary-enter-digest-group
1680     "R" gnus-summary-refer-references
1681     "T" gnus-summary-refer-thread
1682     "g" gnus-summary-show-article
1683     "s" gnus-summary-isearch-article
1684     "P" gnus-summary-print-article
1685     "M" gnus-mailing-list-insinuate
1686     "t" gnus-article-babel)
1687
1688   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1689     "b" gnus-article-add-buttons
1690     "B" gnus-article-add-buttons-to-head
1691     "o" gnus-article-treat-overstrike
1692     "e" gnus-article-emphasize
1693     "w" gnus-article-fill-cited-article
1694     "Q" gnus-article-fill-long-lines
1695     "C" gnus-article-capitalize-sentences
1696     "c" gnus-article-remove-cr
1697     "q" gnus-article-de-quoted-unreadable
1698     "6" gnus-article-de-base64-unreadable
1699     "Z" gnus-article-decode-HZ
1700     "h" gnus-article-wash-html
1701     "s" gnus-summary-force-verify-and-decrypt
1702     "f" gnus-article-display-x-face
1703     "l" gnus-summary-stop-page-breaking
1704     "r" gnus-summary-caesar-message
1705     "t" gnus-summary-toggle-header
1706     "g" gnus-summary-toggle-smiley
1707     "v" gnus-summary-verbose-headers
1708     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1709     "p" gnus-article-verify-x-pgp-sig
1710     "d" gnus-article-treat-dumbquotes)
1711
1712   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1713     "a" gnus-article-hide
1714     "h" gnus-article-hide-headers
1715     "b" gnus-article-hide-boring-headers
1716     "s" gnus-article-hide-signature
1717     "c" gnus-article-hide-citation
1718     "C" gnus-article-hide-citation-in-followups
1719     "l" gnus-article-hide-list-identifiers
1720     "p" gnus-article-hide-pgp
1721     "B" gnus-article-strip-banner
1722     "P" gnus-article-hide-pem
1723     "\C-c" gnus-article-hide-citation-maybe)
1724
1725   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1726     "a" gnus-article-highlight
1727     "h" gnus-article-highlight-headers
1728     "c" gnus-article-highlight-citation
1729     "s" gnus-article-highlight-signature)
1730
1731   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1732     "w" gnus-article-decode-mime-words
1733     "c" gnus-article-decode-charset
1734     "v" gnus-mime-view-all-parts
1735     "b" gnus-article-view-part)
1736
1737   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1738     "z" gnus-article-date-ut
1739     "u" gnus-article-date-ut
1740     "l" gnus-article-date-local
1741     "p" gnus-article-date-english
1742     "e" gnus-article-date-lapsed
1743     "o" gnus-article-date-original
1744     "i" gnus-article-date-iso8601
1745     "s" gnus-article-date-user)
1746
1747   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1748     "t" gnus-article-remove-trailing-blank-lines
1749     "l" gnus-article-strip-leading-blank-lines
1750     "m" gnus-article-strip-multiple-blank-lines
1751     "a" gnus-article-strip-blank-lines
1752     "A" gnus-article-strip-all-blank-lines
1753     "s" gnus-article-strip-leading-space
1754     "e" gnus-article-strip-trailing-space
1755     "w" gnus-article-remove-leading-whitespace)
1756
1757   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1758     "v" gnus-version
1759     "f" gnus-summary-fetch-faq
1760     "d" gnus-summary-describe-group
1761     "h" gnus-summary-describe-briefly
1762     "i" gnus-info-find-node)
1763
1764   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1765     "e" gnus-summary-expire-articles
1766     "\M-\C-e" gnus-summary-expire-articles-now
1767     "\177" gnus-summary-delete-article
1768     [delete] gnus-summary-delete-article
1769     [backspace] gnus-summary-delete-article
1770     "m" gnus-summary-move-article
1771     "r" gnus-summary-respool-article
1772     "w" gnus-summary-edit-article
1773     "c" gnus-summary-copy-article
1774     "B" gnus-summary-crosspost-article
1775     "q" gnus-summary-respool-query
1776     "t" gnus-summary-respool-trace
1777     "i" gnus-summary-import-article
1778     "I" gnus-summary-create-article
1779     "p" gnus-summary-article-posted-p)
1780
1781   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1782     "o" gnus-summary-save-article
1783     "m" gnus-summary-save-article-mail
1784     "F" gnus-summary-write-article-file
1785     "r" gnus-summary-save-article-rmail
1786     "f" gnus-summary-save-article-file
1787     "b" gnus-summary-save-article-body-file
1788     "h" gnus-summary-save-article-folder
1789     "v" gnus-summary-save-article-vm
1790     "p" gnus-summary-pipe-output
1791     "s" gnus-soup-add-article)
1792
1793   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1794     "b" gnus-summary-display-buttonized
1795     "m" gnus-summary-repair-multipart
1796     "v" gnus-article-view-part
1797     "o" gnus-article-save-part
1798     "c" gnus-article-copy-part
1799     "C" gnus-article-view-part-as-charset
1800     "e" gnus-article-externalize-part
1801     "E" gnus-article-encrypt-body
1802     "i" gnus-article-inline-part
1803     "|" gnus-article-pipe-part))
1804
1805 (defvar gnus-article-post-menu nil)
1806
1807 (defun gnus-summary-make-menu-bar ()
1808   (gnus-turn-off-edit-menu 'summary)
1809
1810   (unless (boundp 'gnus-summary-misc-menu)
1811
1812     (easy-menu-define
1813       gnus-summary-kill-menu gnus-summary-mode-map ""
1814       (cons
1815        "Score"
1816        (nconc
1817         (list
1818          ["Customize" gnus-score-customize t])
1819         (gnus-make-score-map 'increase)
1820         (gnus-make-score-map 'lower)
1821         '(("Mark"
1822            ["Kill below" gnus-summary-kill-below t]
1823            ["Mark above" gnus-summary-mark-above t]
1824            ["Tick above" gnus-summary-tick-above t]
1825            ["Clear above" gnus-summary-clear-above t])
1826           ["Current score" gnus-summary-current-score t]
1827           ["Set score" gnus-summary-set-score t]
1828           ["Switch current score file..." gnus-score-change-score-file t]
1829           ["Set mark below..." gnus-score-set-mark-below t]
1830           ["Set expunge below..." gnus-score-set-expunge-below t]
1831           ["Edit current score file" gnus-score-edit-current-scores t]
1832           ["Edit score file" gnus-score-edit-file t]
1833           ["Trace score" gnus-score-find-trace t]
1834           ["Find words" gnus-score-find-favourite-words t]
1835           ["Rescore buffer" gnus-summary-rescore t]
1836           ["Increase score..." gnus-summary-increase-score t]
1837           ["Lower score..." gnus-summary-lower-score t]))))
1838
1839 ;; Define both the Article menu in the summary buffer and the equivalent
1840     ;; Commands menu in the article buffer here for consistency.
1841     (let ((innards
1842            `(("Hide"
1843               ["All" gnus-article-hide t]
1844               ["Headers" gnus-article-hide-headers t]
1845               ["Signature" gnus-article-hide-signature t]
1846               ["Citation" gnus-article-hide-citation t]
1847               ["List identifiers" gnus-article-hide-list-identifiers t]
1848               ["PGP" gnus-article-hide-pgp t]
1849               ["Banner" gnus-article-strip-banner t]
1850               ["Boring headers" gnus-article-hide-boring-headers t])
1851              ("Highlight"
1852               ["All" gnus-article-highlight t]
1853               ["Headers" gnus-article-highlight-headers t]
1854               ["Signature" gnus-article-highlight-signature t]
1855               ["Citation" gnus-article-highlight-citation t])
1856              ("MIME"
1857               ["Words" gnus-article-decode-mime-words t]
1858               ["Charset" gnus-article-decode-charset t]
1859               ["QP" gnus-article-de-quoted-unreadable t]
1860               ["Base64" gnus-article-de-base64-unreadable t]
1861               ["View all" gnus-mime-view-all-parts t]
1862               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1863               ["Encrypt body" gnus-article-encrypt-body 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               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1891               ["Base64" gnus-article-de-base64-unreadable t]
1892               ["Rot 13" gnus-summary-caesar-message
1893                ,@(if (featurep 'xemacs) '(t)
1894                    '(:help "\"Caesar rotate\" article by 13"))]
1895               ["Unix pipe" gnus-summary-pipe-message t]
1896               ["Add buttons" gnus-article-add-buttons t]
1897               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1898               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1899               ["Verbose header" gnus-summary-verbose-headers t]
1900               ["Toggle header" gnus-summary-toggle-header t]
1901               ["Toggle smiley" gnus-summary-toggle-smiley t]
1902               ["Html" gnus-article-wash-html t]
1903               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1904               ["HZ" gnus-article-decode-HZ t])
1905              ("Output"
1906               ["Save in default format" gnus-summary-save-article
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Save article using default method"))]
1909               ["Save in file" gnus-summary-save-article-file
1910                ,@(if (featurep 'xemacs) '(t)
1911                    '(:help "Save article in file"))]
1912               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1913               ["Save in MH folder" gnus-summary-save-article-folder t]
1914               ["Save in VM folder" gnus-summary-save-article-vm t]
1915               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1916               ["Save body in file" gnus-summary-save-article-body-file t]
1917               ["Pipe through a filter" gnus-summary-pipe-output t]
1918               ["Add to SOUP packet" gnus-soup-add-article t]
1919               ["Print" gnus-summary-print-article t])
1920              ("Backend"
1921               ["Respool article..." gnus-summary-respool-article t]
1922               ["Move article..." gnus-summary-move-article
1923                (gnus-check-backend-function
1924                 'request-move-article gnus-newsgroup-name)]
1925               ["Copy article..." gnus-summary-copy-article t]
1926               ["Crosspost article..." gnus-summary-crosspost-article
1927                (gnus-check-backend-function
1928                 'request-replace-article gnus-newsgroup-name)]
1929               ["Import file..." gnus-summary-import-article t]
1930               ["Create article..." gnus-summary-create-article t]
1931               ["Check if posted" gnus-summary-article-posted-p t]
1932               ["Edit article" gnus-summary-edit-article
1933                (not (gnus-group-read-only-p))]
1934               ["Delete article" gnus-summary-delete-article
1935                (gnus-check-backend-function
1936                 'request-expire-articles gnus-newsgroup-name)]
1937               ["Query respool" gnus-summary-respool-query t]
1938               ["Trace respool" gnus-summary-respool-trace t]
1939               ["Delete expirable articles" gnus-summary-expire-articles-now
1940                (gnus-check-backend-function
1941                 'request-expire-articles gnus-newsgroup-name)])
1942              ("Extract"
1943               ["Uudecode" gnus-uu-decode-uu
1944                ,@(if (featurep 'xemacs) '(t)
1945                    '(:help "Decode uuencoded article(s)"))]
1946               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1947               ["Unshar" gnus-uu-decode-unshar t]
1948               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1949               ["Save" gnus-uu-decode-save t]
1950               ["Binhex" gnus-uu-decode-binhex t]
1951               ["Postscript" gnus-uu-decode-postscript t])
1952              ("Cache"
1953               ["Enter article" gnus-cache-enter-article t]
1954               ["Remove article" gnus-cache-remove-article t])
1955              ["Translate" gnus-article-babel t]
1956              ["Select article buffer" gnus-summary-select-article-buffer t]
1957              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1958              ["Isearch article..." gnus-summary-isearch-article t]
1959              ["Beginning of the article" gnus-summary-beginning-of-article t]
1960              ["End of the article" gnus-summary-end-of-article t]
1961              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1962              ["Fetch referenced articles" gnus-summary-refer-references t]
1963              ["Fetch current thread" gnus-summary-refer-thread t]
1964              ["Fetch article with id..." gnus-summary-refer-article t]
1965              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1966              ["Redisplay" gnus-summary-show-article t]
1967              ["Raw article" gnus-summary-show-raw-article t])))
1968       (easy-menu-define
1969         gnus-summary-article-menu gnus-summary-mode-map ""
1970         (cons "Article" innards))
1971
1972       (if (not (keymapp gnus-summary-article-menu))
1973           (easy-menu-define
1974             gnus-article-commands-menu gnus-article-mode-map ""
1975             (cons "Commands" innards))
1976         ;; in Emacs, don't share menu.
1977         (setq gnus-article-commands-menu
1978               (copy-keymap gnus-summary-article-menu))
1979         (define-key gnus-article-mode-map [menu-bar commands]
1980           (cons "Commands" gnus-article-commands-menu))))
1981
1982     (easy-menu-define
1983       gnus-summary-thread-menu gnus-summary-mode-map ""
1984       '("Threads"
1985         ["Toggle threading" gnus-summary-toggle-threads t]
1986         ["Hide threads" gnus-summary-hide-all-threads t]
1987         ["Show threads" gnus-summary-show-all-threads t]
1988         ["Hide thread" gnus-summary-hide-thread t]
1989         ["Show thread" gnus-summary-show-thread t]
1990         ["Go to next thread" gnus-summary-next-thread t]
1991         ["Go to previous thread" gnus-summary-prev-thread t]
1992         ["Go down thread" gnus-summary-down-thread t]
1993         ["Go up thread" gnus-summary-up-thread t]
1994         ["Top of thread" gnus-summary-top-thread t]
1995         ["Mark thread as read" gnus-summary-kill-thread t]
1996         ["Lower thread score" gnus-summary-lower-thread t]
1997         ["Raise thread score" gnus-summary-raise-thread t]
1998         ["Rethread current" gnus-summary-rethread-current t]))
1999
2000     (easy-menu-define
2001       gnus-summary-post-menu gnus-summary-mode-map ""
2002       `("Post"
2003         ["Post an article" gnus-summary-post-news
2004          ,@(if (featurep 'xemacs) '(t)
2005              '(:help "Post an article"))]
2006         ["Followup" gnus-summary-followup
2007          ,@(if (featurep 'xemacs) '(t)
2008              '(:help "Post followup to this article"))]
2009         ["Followup and yank" gnus-summary-followup-with-original
2010          ,@(if (featurep 'xemacs) '(t)
2011              '(:help "Post followup to this article, quoting its contents"))]
2012         ["Supersede article" gnus-summary-supersede-article t]
2013         ["Cancel article" gnus-summary-cancel-article
2014          ,@(if (featurep 'xemacs) '(t)
2015              '(:help "Cancel an article you posted"))]
2016         ["Reply" gnus-summary-reply t]
2017         ["Reply and yank" gnus-summary-reply-with-original t]
2018         ["Wide reply" gnus-summary-wide-reply t]
2019         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2020          ,@(if (featurep 'xemacs) '(t)
2021              '(:help "Mail a reply, quoting this article"))]
2022         ["Mail forward" gnus-summary-mail-forward t]
2023         ["Post forward" gnus-summary-post-forward t]
2024         ["Digest and mail" gnus-uu-digest-mail-forward t]
2025         ["Digest and post" gnus-uu-digest-post-forward t]
2026         ["Resend message" gnus-summary-resend-message t]
2027         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2028         ["Send a mail" gnus-summary-mail-other-window t]
2029         ["Uuencode and post" gnus-uu-post-news
2030          ,@(if (featurep 'xemacs) '(t)
2031              '(:help "Post a uuencoded article"))]
2032         ["Followup via news" gnus-summary-followup-to-mail t]
2033         ["Followup via news and yank"
2034          gnus-summary-followup-to-mail-with-original t]
2035         ;;("Draft"
2036         ;;["Send" gnus-summary-send-draft t]
2037         ;;["Send bounced" gnus-resend-bounced-mail t])
2038         ))
2039
2040     (cond
2041      ((not (keymapp gnus-summary-post-menu))
2042       (setq gnus-article-post-menu gnus-summary-post-menu))
2043      ((not gnus-article-post-menu)
2044       ;; Don't share post menu.
2045       (setq gnus-article-post-menu
2046             (copy-keymap gnus-summary-post-menu))))
2047     (define-key gnus-article-mode-map [menu-bar post]
2048       (cons "Post" gnus-article-post-menu))
2049
2050     (easy-menu-define
2051       gnus-summary-misc-menu gnus-summary-mode-map ""
2052       `("Gnus"
2053         ("Mark Read"
2054          ["Mark as read" gnus-summary-mark-as-read-forward t]
2055          ["Mark same subject and select"
2056           gnus-summary-kill-same-subject-and-select t]
2057          ["Mark same subject" gnus-summary-kill-same-subject t]
2058          ["Catchup" gnus-summary-catchup
2059           ,@(if (featurep 'xemacs) '(t)
2060               '(:help "Mark unread articles in this group as read"))]
2061          ["Catchup all" gnus-summary-catchup-all t]
2062          ["Catchup to here" gnus-summary-catchup-to-here t]
2063          ["Catchup from here" gnus-summary-catchup-from-here t]
2064          ["Catchup region" gnus-summary-mark-region-as-read t]
2065          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2066         ("Mark Various"
2067          ["Tick" gnus-summary-tick-article-forward t]
2068          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2069          ["Remove marks" gnus-summary-clear-mark-forward t]
2070          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2071          ["Set bookmark" gnus-summary-set-bookmark t]
2072          ["Remove bookmark" gnus-summary-remove-bookmark t])
2073         ("Mark Limit"
2074          ["Marks..." gnus-summary-limit-to-marks t]
2075          ["Subject..." gnus-summary-limit-to-subject t]
2076          ["Author..." gnus-summary-limit-to-author t]
2077          ["Age..." gnus-summary-limit-to-age t]
2078          ["Extra..." gnus-summary-limit-to-extra t]
2079          ["Score" gnus-summary-limit-to-score t]
2080          ["Score" gnus-summary-limit-to-display-predicate t]
2081          ["Unread" gnus-summary-limit-to-unread t]
2082          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2083          ["Articles" gnus-summary-limit-to-articles t]
2084          ["Pop limit" gnus-summary-pop-limit t]
2085          ["Show dormant" gnus-summary-limit-include-dormant t]
2086          ["Hide childless dormant"
2087           gnus-summary-limit-exclude-childless-dormant t]
2088          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2089          ["Hide marked" gnus-summary-limit-exclude-marks t]
2090          ["Show expunged" gnus-summary-limit-include-expunged t])
2091         ("Process Mark"
2092          ["Set mark" gnus-summary-mark-as-processable t]
2093          ["Remove mark" gnus-summary-unmark-as-processable t]
2094          ["Remove all marks" gnus-summary-unmark-all-processable t]
2095          ["Mark above" gnus-uu-mark-over t]
2096          ["Mark series" gnus-uu-mark-series t]
2097          ["Mark region" gnus-uu-mark-region t]
2098          ["Unmark region" gnus-uu-unmark-region t]
2099          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2100          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2101          ["Mark all" gnus-uu-mark-all t]
2102          ["Mark buffer" gnus-uu-mark-buffer t]
2103          ["Mark sparse" gnus-uu-mark-sparse t]
2104          ["Mark thread" gnus-uu-mark-thread t]
2105          ["Unmark thread" gnus-uu-unmark-thread t]
2106          ("Process Mark Sets"
2107           ["Kill" gnus-summary-kill-process-mark t]
2108           ["Yank" gnus-summary-yank-process-mark
2109            gnus-newsgroup-process-stack]
2110           ["Save" gnus-summary-save-process-mark t]))
2111         ("Scroll article"
2112          ["Page forward" gnus-summary-next-page
2113           ,@(if (featurep 'xemacs) '(t)
2114               '(:help "Show next page of article"))]
2115          ["Page backward" gnus-summary-prev-page
2116           ,@(if (featurep 'xemacs) '(t)
2117               '(:help "Show previous page of article"))]
2118          ["Line forward" gnus-summary-scroll-up t])
2119         ("Move"
2120          ["Next unread article" gnus-summary-next-unread-article t]
2121          ["Previous unread article" gnus-summary-prev-unread-article t]
2122          ["Next article" gnus-summary-next-article t]
2123          ["Previous article" gnus-summary-prev-article t]
2124          ["Next unread subject" gnus-summary-next-unread-subject t]
2125          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2126          ["Next article same subject" gnus-summary-next-same-subject t]
2127          ["Previous article same subject" gnus-summary-prev-same-subject t]
2128          ["First unread article" gnus-summary-first-unread-article t]
2129          ["Best unread article" gnus-summary-best-unread-article t]
2130          ["Go to subject number..." gnus-summary-goto-subject t]
2131          ["Go to article number..." gnus-summary-goto-article t]
2132          ["Go to the last article" gnus-summary-goto-last-article t]
2133          ["Pop article off history" gnus-summary-pop-article t])
2134         ("Sort"
2135          ["Sort by number" gnus-summary-sort-by-number t]
2136          ["Sort by author" gnus-summary-sort-by-author t]
2137          ["Sort by subject" gnus-summary-sort-by-subject t]
2138          ["Sort by date" gnus-summary-sort-by-date t]
2139          ["Sort by score" gnus-summary-sort-by-score t]
2140          ["Sort by lines" gnus-summary-sort-by-lines t]
2141          ["Sort by characters" gnus-summary-sort-by-chars t]
2142          ["Original sort" gnus-summary-sort-by-original t])
2143         ("Help"
2144          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2145          ["Describe group" gnus-summary-describe-group t]
2146          ["Read manual" gnus-info-find-node t])
2147         ("Modes"
2148          ["Pick and read" gnus-pick-mode t]
2149          ["Binary" gnus-binary-mode t])
2150         ("Regeneration"
2151          ["Regenerate" gnus-summary-prepare t]
2152          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2153          ["Toggle threading" gnus-summary-toggle-threads t])
2154         ["See old articles" gnus-summary-insert-old-articles t]
2155         ["See new articles" gnus-summary-insert-new-articles t]
2156         ["Filter articles..." gnus-summary-execute-command t]
2157         ["Run command on subjects..." gnus-summary-universal-argument t]
2158         ["Search articles forward..." gnus-summary-search-article-forward t]
2159         ["Search articles backward..." gnus-summary-search-article-backward t]
2160         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2161         ["Expand window" gnus-summary-expand-window t]
2162         ["Expire expirable articles" gnus-summary-expire-articles
2163          (gnus-check-backend-function
2164           'request-expire-articles gnus-newsgroup-name)]
2165         ["Edit local kill file" gnus-summary-edit-local-kill t]
2166         ["Edit main kill file" gnus-summary-edit-global-kill t]
2167         ["Edit group parameters" gnus-summary-edit-parameters t]
2168         ["Customize group parameters" gnus-summary-customize-parameters t]
2169         ["Send a bug report" gnus-bug t]
2170         ("Exit"
2171          ["Catchup and exit" gnus-summary-catchup-and-exit
2172           ,@(if (featurep 'xemacs) '(t)
2173               '(:help "Mark unread articles in this group as read, then exit"))]
2174          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2175          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2176          ["Exit group" gnus-summary-exit
2177           ,@(if (featurep 'xemacs) '(t)
2178               '(:help "Exit current group, return to group selection mode"))]
2179          ["Exit group without updating" gnus-summary-exit-no-update t]
2180          ["Exit and goto next group" gnus-summary-next-group t]
2181          ["Exit and goto prev group" gnus-summary-prev-group t]
2182          ["Reselect group" gnus-summary-reselect-current-group t]
2183          ["Rescan group" gnus-summary-rescan-group t]
2184          ["Update dribble" gnus-summary-save-newsrc t])))
2185
2186     (gnus-run-hooks 'gnus-summary-menu-hook)))
2187
2188 (defvar gnus-summary-tool-bar-map nil)
2189
2190 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2191 (defun gnus-summary-make-tool-bar ()
2192   (if (and (fboundp 'tool-bar-add-item-from-menu)
2193            (default-value 'tool-bar-mode)
2194            (not gnus-summary-tool-bar-map))
2195       (setq gnus-summary-tool-bar-map
2196             (let ((tool-bar-map (make-sparse-keymap))
2197                   (load-path (mm-image-load-path)))
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-post-news "post" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2206               (tool-bar-add-item-from-menu
2207                'gnus-summary-followup "followup" gnus-summary-mode-map)
2208               (tool-bar-add-item-from-menu
2209                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2210               (tool-bar-add-item-from-menu
2211                'gnus-summary-reply "reply" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2226               (tool-bar-add-item-from-menu
2227                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2228               tool-bar-map)))
2229   (if gnus-summary-tool-bar-map
2230       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2231
2232 (defun gnus-score-set-default (var value)
2233   "A version of set that updates the GNU Emacs menu-bar."
2234   (set var value)
2235   ;; It is the message that forces the active status to be updated.
2236   (message ""))
2237
2238 (defun gnus-make-score-map (type)
2239   "Make a summary score map of type TYPE."
2240   (if t
2241       nil
2242     (let ((headers '(("author" "from" string)
2243                      ("subject" "subject" string)
2244                      ("article body" "body" string)
2245                      ("article head" "head" string)
2246                      ("xref" "xref" string)
2247                      ("extra header" "extra" string)
2248                      ("lines" "lines" number)
2249                      ("followups to author" "followup" string)))
2250           (types '((number ("less than" <)
2251                            ("greater than" >)
2252                            ("equal" =))
2253                    (string ("substring" s)
2254                            ("exact string" e)
2255                            ("fuzzy string" f)
2256                            ("regexp" r))))
2257           (perms '(("temporary" (current-time-string))
2258                    ("permanent" nil)
2259                    ("immediate" now)))
2260           header)
2261       (list
2262        (apply
2263         'nconc
2264         (list
2265          (if (eq type 'lower)
2266              "Lower score"
2267            "Increase score"))
2268         (let (outh)
2269           (while headers
2270             (setq header (car headers))
2271             (setq outh
2272                   (cons
2273                    (apply
2274                     'nconc
2275                     (list (car header))
2276                     (let ((ts (cdr (assoc (nth 2 header) types)))
2277                           outt)
2278                       (while ts
2279                         (setq outt
2280                               (cons
2281                                (apply
2282                                 'nconc
2283                                 (list (caar ts))
2284                                 (let ((ps perms)
2285                                       outp)
2286                                   (while ps
2287                                     (setq outp
2288                                           (cons
2289                                            (vector
2290                                             (caar ps)
2291                                             (list
2292                                              'gnus-summary-score-entry
2293                                              (nth 1 header)
2294                                              (if (or (string= (nth 1 header)
2295                                                               "head")
2296                                                      (string= (nth 1 header)
2297                                                               "body"))
2298                                                  ""
2299                                                (list 'gnus-summary-header
2300                                                      (nth 1 header)))
2301                                              (list 'quote (nth 1 (car ts)))
2302                                              (list 'gnus-score-delta-default
2303                                                    nil)
2304                                              (nth 1 (car ps))
2305                                              t)
2306                                             t)
2307                                            outp))
2308                                     (setq ps (cdr ps)))
2309                                   (list (nreverse outp))))
2310                                outt))
2311                         (setq ts (cdr ts)))
2312                       (list (nreverse outt))))
2313                    outh))
2314             (setq headers (cdr headers)))
2315           (list (nreverse outh))))))))
2316
2317 \f
2318
2319 (defun gnus-summary-mode (&optional group)
2320   "Major mode for reading articles.
2321
2322 All normal editing commands are switched off.
2323 \\<gnus-summary-mode-map>
2324 Each line in this buffer represents one article.  To read an
2325 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2326 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2327 respectively.
2328
2329 You can also post articles and send mail from this buffer.  To
2330 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2331 of an article, type `\\[gnus-summary-reply]'.
2332
2333 There are approx. one gazillion commands you can execute in this
2334 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2335
2336 The following commands are available:
2337
2338 \\{gnus-summary-mode-map}"
2339   (interactive)
2340   (kill-all-local-variables)
2341   (when (gnus-visual-p 'summary-menu 'menu)
2342     (gnus-summary-make-menu-bar)
2343     (gnus-summary-make-tool-bar))
2344   (gnus-summary-make-local-variables)
2345   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2346     (gnus-summary-make-local-variables))
2347   (gnus-make-thread-indent-array)
2348   (gnus-simplify-mode-line)
2349   (setq major-mode 'gnus-summary-mode)
2350   (setq mode-name "Summary")
2351   (make-local-variable 'minor-mode-alist)
2352   (use-local-map gnus-summary-mode-map)
2353   (buffer-disable-undo)
2354   (setq buffer-read-only t)             ;Disable modification
2355   (setq truncate-lines t)
2356   (setq selective-display t)
2357   (setq selective-display-ellipses t)   ;Display `...'
2358   (gnus-summary-set-display-table)
2359   (gnus-set-default-directory)
2360   (setq gnus-newsgroup-name group)
2361   (make-local-variable 'gnus-summary-line-format)
2362   (make-local-variable 'gnus-summary-line-format-spec)
2363   (make-local-variable 'gnus-summary-dummy-line-format)
2364   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2365   (make-local-variable 'gnus-summary-mark-positions)
2366   (make-local-hook 'pre-command-hook)
2367   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2368   (gnus-run-hooks 'gnus-summary-mode-hook)
2369   (turn-on-gnus-mailing-list-mode)
2370   (mm-enable-multibyte-mule4)
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   `(setf (nth 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            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2886            0 nil 128 t nil "" nil 1)
2887           (goto-char (point-min))
2888           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2889                                              (- (point) 2)))))
2890           (goto-char (point-min))
2891           (push (cons 'replied (and (search-forward "\201" nil t)
2892                                     (- (point) 2)))
2893                 pos)
2894           (goto-char (point-min))
2895           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2896                 pos)
2897           (goto-char (point-min))
2898           (push (cons 'download
2899                       (and (search-forward "\203" nil t) (- (point) 2)))
2900                 pos)))
2901       (setq gnus-summary-mark-positions pos))))
2902
2903 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2904   "Insert a dummy root in the summary buffer."
2905   (beginning-of-line)
2906   (gnus-add-text-properties
2907    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2908    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2909
2910 (defun gnus-summary-extract-address-component (from)
2911   (or (car (funcall gnus-extract-address-components from))
2912       from))
2913
2914 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2915   (let ((mail-parse-charset gnus-newsgroup-charset)
2916         ; Is it really necessary to do this next part for each summary line?
2917         ; Luckily, doesn't seem to slow things down much.
2918         (mail-parse-ignored-charsets
2919          (save-excursion (set-buffer gnus-summary-buffer)
2920                          gnus-newsgroup-ignored-charsets)))
2921     (or
2922      (and gnus-ignored-from-addresses
2923           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2924           (let ((extra-headers (mail-header-extra header))
2925                 to
2926                 newsgroups)
2927             (cond
2928              ((setq to (cdr (assq 'To extra-headers)))
2929               (concat "-> "
2930                       (gnus-summary-extract-address-component
2931                        (funcall gnus-decode-encoded-word-function to))))
2932              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2933               (concat "=> " newsgroups)))))
2934      (gnus-summary-extract-address-component gnus-tmp-from))))
2935
2936 (defun gnus-summary-insert-line (gnus-tmp-header
2937                                  gnus-tmp-level gnus-tmp-current
2938                                  gnus-tmp-unread gnus-tmp-replied
2939                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2940                                  &optional gnus-tmp-dummy gnus-tmp-score
2941                                  gnus-tmp-process)
2942   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2943          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2944          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2945          (gnus-tmp-score-char
2946           (if (or (null gnus-summary-default-score)
2947                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2948                       gnus-summary-zcore-fuzz))
2949               ?                         ;Whitespace
2950             (if (< gnus-tmp-score gnus-summary-default-score)
2951                 gnus-score-below-mark gnus-score-over-mark)))
2952          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2953          (gnus-tmp-replied
2954           (cond (gnus-tmp-process gnus-process-mark)
2955                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2956                  gnus-cached-mark)
2957                 (gnus-tmp-replied gnus-replied-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2959                  gnus-forwarded-mark)
2960                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2961                  gnus-saved-mark)
2962                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2963                  gnus-recent-mark)
2964                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2965                  gnus-unseen-mark)
2966                 (t gnus-no-mark)))
2967          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2968          (gnus-tmp-name
2969           (cond
2970            ((string-match "<[^>]+> *$" gnus-tmp-from)
2971             (let ((beg (match-beginning 0)))
2972               (or (and (string-match "^\".+\"" gnus-tmp-from)
2973                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2974                   (substring gnus-tmp-from 0 beg))))
2975            ((string-match "(.+)" gnus-tmp-from)
2976             (substring gnus-tmp-from
2977                        (1+ (match-beginning 0)) (1- (match-end 0))))
2978            (t gnus-tmp-from)))
2979          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2980          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2981          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2982          (buffer-read-only nil))
2983     (when (string= gnus-tmp-name "")
2984       (setq gnus-tmp-name gnus-tmp-from))
2985     (unless (numberp gnus-tmp-lines)
2986       (setq gnus-tmp-lines -1))
2987     (if (= gnus-tmp-lines -1)
2988         (setq gnus-tmp-lines "?")
2989       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2990     (gnus-put-text-property
2991      (point)
2992      (progn (eval gnus-summary-line-format-spec) (point))
2993      'gnus-number gnus-tmp-number)
2994     (when (gnus-visual-p 'summary-highlight 'highlight)
2995       (forward-line -1)
2996       (gnus-run-hooks 'gnus-summary-update-hook)
2997       (forward-line 1))))
2998
2999 (defun gnus-summary-update-line (&optional dont-update)
3000   "Update summary line after change."
3001   (when (and gnus-summary-default-score
3002              (not gnus-summary-inhibit-highlight))
3003     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3004            (article (gnus-summary-article-number))
3005            (score (gnus-summary-article-score article)))
3006       (unless dont-update
3007         (if (and gnus-summary-mark-below
3008                  (< (gnus-summary-article-score)
3009                     gnus-summary-mark-below))
3010             ;; This article has a low score, so we mark it as read.
3011             (when (memq article gnus-newsgroup-unreads)
3012               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3013           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3014             ;; This article was previously marked as read on account
3015             ;; of a low score, but now it has risen, so we mark it as
3016             ;; unread.
3017             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3018         (gnus-summary-update-mark
3019          (if (or (null gnus-summary-default-score)
3020                  (<= (abs (- score gnus-summary-default-score))
3021                      gnus-summary-zcore-fuzz))
3022              ?                          ;Whitespace
3023            (if (< score gnus-summary-default-score)
3024                gnus-score-below-mark gnus-score-over-mark))
3025          'score))
3026       ;; Do visual highlighting.
3027       (when (gnus-visual-p 'summary-highlight 'highlight)
3028         (gnus-run-hooks 'gnus-summary-update-hook)))))
3029
3030 (defvar gnus-tmp-new-adopts nil)
3031
3032 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3033   "Return the number of articles in THREAD.
3034 This may be 0 in some cases -- if none of the articles in
3035 the thread are to be displayed."
3036   (let* ((number
3037          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3038           (cond
3039            ((not (listp thread))
3040             1)
3041            ((and (consp thread) (cdr thread))
3042             (apply
3043              '+ 1 (mapcar
3044                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3045            ((null thread)
3046             1)
3047            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3048             1)
3049            (t 0))))
3050     (when (and level (zerop level) gnus-tmp-new-adopts)
3051       (incf number
3052             (apply '+ (mapcar
3053                        'gnus-summary-number-of-articles-in-thread
3054                        gnus-tmp-new-adopts))))
3055     (if char
3056         (if (> number 1) gnus-not-empty-thread-mark
3057           gnus-empty-thread-mark)
3058       number)))
3059
3060 (defun gnus-summary-set-local-parameters (group)
3061   "Go through the local params of GROUP and set all variable specs in that list."
3062   (let ((params (gnus-group-find-parameter group))
3063         (vars '(quit-config))           ; Ignore quit-config.
3064         elem)
3065     (while params
3066       (setq elem (car params)
3067             params (cdr params))
3068       (and (consp elem)                 ; Has to be a cons.
3069            (consp (cdr elem))           ; The cdr has to be a list.
3070            (symbolp (car elem))         ; Has to be a symbol in there.
3071            (not (memq (car elem) vars))
3072            (ignore-errors               ; So we set it.
3073              (push (car elem) vars)
3074              (make-local-variable (car elem))
3075              (set (car elem) (eval (nth 1 elem))))))))
3076
3077 (defun gnus-summary-read-group (group &optional show-all no-article
3078                                       kill-buffer no-display backward
3079                                       select-articles)
3080   "Start reading news in newsgroup GROUP.
3081 If SHOW-ALL is non-nil, already read articles are also listed.
3082 If NO-ARTICLE is non-nil, no article is selected initially.
3083 If NO-DISPLAY, don't generate a summary buffer."
3084   (let (result)
3085     (while (and group
3086                 (null (setq result
3087                             (let ((gnus-auto-select-next nil))
3088                               (or (gnus-summary-read-group-1
3089                                    group show-all no-article
3090                                    kill-buffer no-display
3091                                    select-articles)
3092                                   (setq show-all nil
3093                                         select-articles nil)))))
3094                 (eq gnus-auto-select-next 'quietly))
3095       (set-buffer gnus-group-buffer)
3096       ;; The entry function called above goes to the next
3097       ;; group automatically, so we go two groups back
3098       ;; if we are searching for the previous group.
3099       (when backward
3100         (gnus-group-prev-unread-group 2))
3101       (if (not (equal group (gnus-group-group-name)))
3102           (setq group (gnus-group-group-name))
3103         (setq group nil)))
3104     result))
3105
3106 (defun gnus-summary-read-group-1 (group show-all no-article
3107                                         kill-buffer no-display
3108                                         &optional select-articles)
3109   ;; Killed foreign groups can't be entered.
3110   ;;  (when (and (not (gnus-group-native-p group))
3111   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3112   ;;    (error "Dead non-native groups can't be entered"))
3113   (gnus-message 5 "Retrieving newsgroup: %s..."
3114                 (gnus-group-decoded-name group))
3115   (let* ((new-group (gnus-summary-setup-buffer group))
3116          (quit-config (gnus-group-quit-config group))
3117          (did-select (and new-group (gnus-select-newsgroup
3118                                      group show-all select-articles))))
3119     (cond
3120      ;; This summary buffer exists already, so we just select it.
3121      ((not new-group)
3122       (gnus-set-global-variables)
3123       (when kill-buffer
3124         (gnus-kill-or-deaden-summary kill-buffer))
3125       (gnus-configure-windows 'summary 'force)
3126       (gnus-set-mode-line 'summary)
3127       (gnus-summary-position-point)
3128       (message "")
3129       t)
3130      ;; We couldn't select this group.
3131      ((null did-select)
3132       (when (and (eq major-mode 'gnus-summary-mode)
3133                  (not (equal (current-buffer) kill-buffer)))
3134         (kill-buffer (current-buffer))
3135         (if (not quit-config)
3136             (progn
3137               ;; Update the info -- marks might need to be removed,
3138               ;; for instance.
3139               (gnus-summary-update-info)
3140               (set-buffer gnus-group-buffer)
3141               (gnus-group-jump-to-group group)
3142               (gnus-group-next-unread-group 1))
3143           (gnus-handle-ephemeral-exit quit-config)))
3144       (let ((grpinfo (gnus-get-info group)))
3145         (if (null (gnus-info-read grpinfo))
3146             (gnus-message 3 "Group %s contains no messages"
3147                           (gnus-group-decoded-name group))
3148           (gnus-message 3 "Can't select group")))
3149       nil)
3150      ;; The user did a `C-g' while prompting for number of articles,
3151      ;; so we exit this group.
3152      ((eq did-select 'quit)
3153       (and (eq major-mode 'gnus-summary-mode)
3154            (not (equal (current-buffer) kill-buffer))
3155            (kill-buffer (current-buffer)))
3156       (when kill-buffer
3157         (gnus-kill-or-deaden-summary kill-buffer))
3158       (if (not quit-config)
3159           (progn
3160             (set-buffer gnus-group-buffer)
3161             (gnus-group-jump-to-group group)
3162             (gnus-group-next-unread-group 1)
3163             (gnus-configure-windows 'group 'force))
3164         (gnus-handle-ephemeral-exit quit-config))
3165       ;; Finally signal the quit.
3166       (signal 'quit nil))
3167      ;; The group was successfully selected.
3168      (t
3169       (gnus-set-global-variables)
3170       ;; Save the active value in effect when the group was entered.
3171       (setq gnus-newsgroup-active
3172             (gnus-copy-sequence
3173              (gnus-active gnus-newsgroup-name)))
3174       ;; You can change the summary buffer in some way with this hook.
3175       (gnus-run-hooks 'gnus-select-group-hook)
3176       (gnus-update-format-specifications
3177        nil 'summary 'summary-mode 'summary-dummy)
3178       (gnus-update-summary-mark-positions)
3179       ;; Do score processing.
3180       (when gnus-use-scoring
3181         (gnus-possibly-score-headers))
3182       ;; Check whether to fill in the gaps in the threads.
3183       (when gnus-build-sparse-threads
3184         (gnus-build-sparse-threads))
3185       ;; Find the initial limit.
3186       (if gnus-show-threads
3187           (if show-all
3188               (let ((gnus-newsgroup-dormant nil))
3189                 (gnus-summary-initial-limit show-all))
3190             (gnus-summary-initial-limit show-all))
3191         ;; When untreaded, all articles are always shown.
3192         (setq gnus-newsgroup-limit
3193               (mapcar
3194                (lambda (header) (mail-header-number header))
3195                gnus-newsgroup-headers)))
3196       ;; Generate the summary buffer.
3197       (unless no-display
3198         (gnus-summary-prepare))
3199       (when gnus-use-trees
3200         (gnus-tree-open group)
3201         (setq gnus-summary-highlight-line-function
3202               'gnus-tree-highlight-article))
3203       ;; If the summary buffer is empty, but there are some low-scored
3204       ;; articles or some excluded dormants, we include these in the
3205       ;; buffer.
3206       (when (and (zerop (buffer-size))
3207                  (not no-display))
3208         (cond (gnus-newsgroup-dormant
3209                (gnus-summary-limit-include-dormant))
3210               ((and gnus-newsgroup-scored show-all)
3211                (gnus-summary-limit-include-expunged t))))
3212       ;; Function `gnus-apply-kill-file' must be called in this hook.
3213       (gnus-run-hooks 'gnus-apply-kill-hook)
3214       (if (and (zerop (buffer-size))
3215                (not no-display))
3216           (progn
3217             ;; This newsgroup is empty.
3218             (gnus-summary-catchup-and-exit nil t)
3219             (gnus-message 6 "No unread news")
3220             (when kill-buffer
3221               (gnus-kill-or-deaden-summary kill-buffer))
3222             ;; Return nil from this function.
3223             nil)
3224         ;; Hide conversation thread subtrees.  We cannot do this in
3225         ;; gnus-summary-prepare-hook since kill processing may not
3226         ;; work with hidden articles.
3227         (and gnus-show-threads
3228              gnus-thread-hide-subtree
3229              (gnus-summary-hide-all-threads))
3230         (when kill-buffer
3231           (gnus-kill-or-deaden-summary kill-buffer))
3232         ;; Show first unread article if requested.
3233         (if (and (not no-article)
3234                  (not no-display)
3235                  gnus-newsgroup-unreads
3236                  gnus-auto-select-first)
3237             (progn
3238               (gnus-configure-windows 'summary)
3239               (cond
3240                ((eq gnus-auto-select-first 'best)
3241                 (gnus-summary-best-unread-article))
3242                ((eq gnus-auto-select-first t)
3243                 (gnus-summary-first-unread-article))
3244                ((gnus-functionp gnus-auto-select-first)
3245                 (funcall gnus-auto-select-first))))
3246           ;; Don't select any articles, just move point to the first
3247           ;; article in the group.
3248           (goto-char (point-min))
3249           (gnus-summary-position-point)
3250           (gnus-configure-windows 'summary 'force)
3251           (gnus-set-mode-line 'summary))
3252         (when (get-buffer-window gnus-group-buffer t)
3253           ;; Gotta use windows, because recenter does weird stuff if
3254           ;; the current buffer ain't the displayed window.
3255           (let ((owin (selected-window)))
3256             (select-window (get-buffer-window gnus-group-buffer t))
3257             (when (gnus-group-goto-group group)
3258               (recenter))
3259             (select-window owin)))
3260         ;; Mark this buffer as "prepared".
3261         (setq gnus-newsgroup-prepared t)
3262         (gnus-run-hooks 'gnus-summary-prepared-hook)
3263         t)))))
3264
3265 (defun gnus-summary-prepare ()
3266   "Generate the summary buffer."
3267   (interactive)
3268   (let ((buffer-read-only nil))
3269     (erase-buffer)
3270     (setq gnus-newsgroup-data nil
3271           gnus-newsgroup-data-reverse nil)
3272     (gnus-run-hooks 'gnus-summary-generate-hook)
3273     ;; Generate the buffer, either with threads or without.
3274     (when gnus-newsgroup-headers
3275       (gnus-summary-prepare-threads
3276        (if gnus-show-threads
3277            (gnus-sort-gathered-threads
3278             (funcall gnus-summary-thread-gathering-function
3279                      (gnus-sort-threads
3280                       (gnus-cut-threads (gnus-make-threads)))))
3281          ;; Unthreaded display.
3282          (gnus-sort-articles gnus-newsgroup-headers))))
3283     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3284     ;; Call hooks for modifying summary buffer.
3285     (goto-char (point-min))
3286     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3287
3288 (defsubst gnus-general-simplify-subject (subject)
3289   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3290   (setq subject
3291         (cond
3292          ;; Truncate the subject.
3293          (gnus-simplify-subject-functions
3294           (gnus-map-function gnus-simplify-subject-functions subject))
3295          ((numberp gnus-summary-gather-subject-limit)
3296           (setq subject (gnus-simplify-subject-re subject))
3297           (if (> (length subject) gnus-summary-gather-subject-limit)
3298               (substring subject 0 gnus-summary-gather-subject-limit)
3299             subject))
3300          ;; Fuzzily simplify it.
3301          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3302           (gnus-simplify-subject-fuzzy subject))
3303          ;; Just remove the leading "Re:".
3304          (t
3305           (gnus-simplify-subject-re subject))))
3306
3307   (if (and gnus-summary-gather-exclude-subject
3308            (string-match gnus-summary-gather-exclude-subject subject))
3309       nil                         ; This article shouldn't be gathered
3310     subject))
3311
3312 (defun gnus-summary-simplify-subject-query ()
3313   "Query where the respool algorithm would put this article."
3314   (interactive)
3315   (gnus-summary-select-article)
3316   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3317
3318 (defun gnus-gather-threads-by-subject (threads)
3319   "Gather threads by looking at Subject headers."
3320   (if (not gnus-summary-make-false-root)
3321       threads
3322     (let ((hashtb (gnus-make-hashtable 1024))
3323           (prev threads)
3324           (result threads)
3325           subject hthread whole-subject)
3326       (while threads
3327         (setq subject (gnus-general-simplify-subject
3328                        (setq whole-subject (mail-header-subject
3329                                             (caar threads)))))
3330         (when subject
3331           (if (setq hthread (gnus-gethash subject hashtb))
3332               (progn
3333                 ;; We enter a dummy root into the thread, if we
3334                 ;; haven't done that already.
3335                 (unless (stringp (caar hthread))
3336                   (setcar hthread (list whole-subject (car hthread))))
3337                 ;; We add this new gathered thread to this gathered
3338                 ;; thread.
3339                 (setcdr (car hthread)
3340                         (nconc (cdar hthread) (list (car threads))))
3341                 ;; Remove it from the list of threads.
3342                 (setcdr prev (cdr threads))
3343                 (setq threads prev))
3344             ;; Enter this thread into the hash table.
3345             (gnus-sethash subject threads hashtb)))
3346         (setq prev threads)
3347         (setq threads (cdr threads)))
3348       result)))
3349
3350 (defun gnus-gather-threads-by-references (threads)
3351   "Gather threads by looking at References headers."
3352   (let ((idhashtb (gnus-make-hashtable 1024))
3353         (thhashtb (gnus-make-hashtable 1024))
3354         (prev threads)
3355         (result threads)
3356         ids references id gthread gid entered ref)
3357     (while threads
3358       (when (setq references (mail-header-references (caar threads)))
3359         (setq id (mail-header-id (caar threads))
3360               ids (gnus-split-references references)
3361               entered nil)
3362         (while (setq ref (pop ids))
3363           (setq ids (delete ref ids))
3364           (if (not (setq gid (gnus-gethash ref idhashtb)))
3365               (progn
3366                 (gnus-sethash ref id idhashtb)
3367                 (gnus-sethash id threads thhashtb))
3368             (setq gthread (gnus-gethash gid thhashtb))
3369             (unless entered
3370               ;; We enter a dummy root into the thread, if we
3371               ;; haven't done that already.
3372               (unless (stringp (caar gthread))
3373                 (setcar gthread (list (mail-header-subject (caar gthread))
3374                                       (car gthread))))
3375               ;; We add this new gathered thread to this gathered
3376               ;; thread.
3377               (setcdr (car gthread)
3378                       (nconc (cdar gthread) (list (car threads)))))
3379             ;; Add it into the thread hash table.
3380             (gnus-sethash id gthread thhashtb)
3381             (setq entered t)
3382             ;; Remove it from the list of threads.
3383             (setcdr prev (cdr threads))
3384             (setq threads prev))))
3385       (setq prev threads)
3386       (setq threads (cdr threads)))
3387     result))
3388
3389 (defun gnus-sort-gathered-threads (threads)
3390   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3391   (let ((result threads))
3392     (while threads
3393       (when (stringp (caar threads))
3394         (setcdr (car threads)
3395                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3396       (setq threads (cdr threads)))
3397     result))
3398
3399 (defun gnus-thread-loop-p (root thread)
3400   "Say whether ROOT is in THREAD."
3401   (let ((stack (list thread))
3402         (infloop 0)
3403         th)
3404     (while (setq thread (pop stack))
3405       (setq th (cdr thread))
3406       (while (and th
3407                   (not (eq (caar th) root)))
3408         (pop th))
3409       (if th
3410           ;; We have found a loop.
3411           (let (ref-dep)
3412             (setcdr thread (delq (car th) (cdr thread)))
3413             (if (boundp (setq ref-dep (intern "none"
3414                                               gnus-newsgroup-dependencies)))
3415                 (setcdr (symbol-value ref-dep)
3416                         (nconc (cdr (symbol-value ref-dep))
3417                                (list (car th))))
3418               (set ref-dep (list nil (car th))))
3419             (setq infloop 1
3420                   stack nil))
3421         ;; Push all the subthreads onto the stack.
3422         (push (cdr thread) stack)))
3423     infloop))
3424
3425 (defun gnus-make-threads ()
3426   "Go through the dependency hashtb and find the roots.  Return all threads."
3427   (let (threads)
3428     (while (catch 'infloop
3429              (mapatoms
3430               (lambda (refs)
3431                 ;; Deal with self-referencing References loops.
3432                 (when (and (car (symbol-value refs))
3433                            (not (zerop
3434                                  (apply
3435                                   '+
3436                                   (mapcar
3437                                    (lambda (thread)
3438                                      (gnus-thread-loop-p
3439                                       (car (symbol-value refs)) thread))
3440                                    (cdr (symbol-value refs)))))))
3441                   (setq threads nil)
3442                   (throw 'infloop t))
3443                 (unless (car (symbol-value refs))
3444              ;; These threads do not refer back to any other articles,
3445                   ;; so they're roots.
3446                   (setq threads (append (cdr (symbol-value refs)) threads))))
3447               gnus-newsgroup-dependencies)))
3448     threads))
3449
3450 ;; Build the thread tree.
3451 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3452   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3453
3454 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3455 if it was already present.
3456
3457 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3458 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3459 Message-IDs will be renamed be renamed to a unique Message-ID before
3460 being entered.
3461
3462 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3463   (let* ((id (mail-header-id header))
3464          (id-dep (and id (intern id dependencies)))
3465          ref ref-dep ref-header)
3466     ;; Enter this `header' in the `dependencies' table.
3467     (cond
3468      ((not id-dep)
3469       (setq header nil))
3470      ;; The first two cases do the normal part: enter a new `header'
3471      ;; in the `dependencies' table.
3472      ((not (boundp id-dep))
3473       (set id-dep (list header)))
3474      ((null (car (symbol-value id-dep)))
3475       (setcar (symbol-value id-dep) header))
3476
3477      ;; From here the `header' was already present in the
3478      ;; `dependencies' table.
3479      (force-new
3480       ;; Overrides an existing entry;
3481       ;; just set the header part of the entry.
3482       (setcar (symbol-value id-dep) header))
3483
3484      ;; Renames the existing `header' to a unique Message-ID.
3485      ((not gnus-summary-ignore-duplicates)
3486       ;; An article with this Message-ID has already been seen.
3487       ;; We rename the Message-ID.
3488       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3489            (list header))
3490       (mail-header-set-id header id))
3491
3492      ;; The last case ignores an existing entry, except it adds any
3493      ;; additional Xrefs (in case the two articles came from different
3494      ;; servers.
3495      ;; Also sets `header' to `nil' meaning that the `dependencies'
3496      ;; table was *not* modified.
3497      (t
3498       (mail-header-set-xref
3499        (car (symbol-value id-dep))
3500        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3501                    "")
3502                (or (mail-header-xref header) "")))
3503       (setq header nil)))
3504
3505     (when header
3506       ;; First check if that we are not creating a References loop.
3507       (setq ref (gnus-parent-id (mail-header-references header)))
3508       (while (and ref
3509                   (setq ref-dep (intern-soft ref dependencies))
3510                   (boundp ref-dep)
3511                   (setq ref-header (car (symbol-value ref-dep))))
3512         (if (string= id ref)
3513             ;; Yuk!  This is a reference loop.  Make the article be a
3514             ;; root article.
3515             (progn
3516               (mail-header-set-references (car (symbol-value id-dep)) "none")
3517               (setq ref nil))
3518           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3519       (setq ref (gnus-parent-id (mail-header-references header)))
3520       (setq ref-dep (intern (or ref "none") dependencies))
3521       (if (boundp ref-dep)
3522           (setcdr (symbol-value ref-dep)
3523                   (nconc (cdr (symbol-value ref-dep))
3524                          (list (symbol-value id-dep))))
3525         (set ref-dep (list nil (symbol-value id-dep)))))
3526     header))
3527
3528 (defun gnus-build-sparse-threads ()
3529   (let ((headers gnus-newsgroup-headers)
3530         (mail-parse-charset gnus-newsgroup-charset)
3531         (gnus-summary-ignore-duplicates t)
3532         header references generation relations
3533         subject child end new-child date)
3534     ;; First we create an alist of generations/relations, where
3535     ;; generations is how much we trust the relation, and the relation
3536     ;; is parent/child.
3537     (gnus-message 7 "Making sparse threads...")
3538     (save-excursion
3539       (nnheader-set-temp-buffer " *gnus sparse threads*")
3540       (while (setq header (pop headers))
3541         (when (and (setq references (mail-header-references header))
3542                    (not (string= references "")))
3543           (insert references)
3544           (setq child (mail-header-id header)
3545                 subject (mail-header-subject header)
3546                 date (mail-header-date header)
3547                 generation 0)
3548           (while (search-backward ">" nil t)
3549             (setq end (1+ (point)))
3550             (when (search-backward "<" nil t)
3551               (setq new-child (buffer-substring (point) end))
3552               (push (list (incf generation)
3553                           child (setq child new-child)
3554                           subject date)
3555                     relations)))
3556           (when child
3557             (push (list (1+ generation) child nil subject) relations))
3558           (erase-buffer)))
3559       (kill-buffer (current-buffer)))
3560     ;; Sort over trustworthiness.
3561     (mapcar
3562      (lambda (relation)
3563        (when (gnus-dependencies-add-header
3564               (make-full-mail-header
3565                gnus-reffed-article-number
3566                (nth 3 relation) "" (or (nth 4 relation) "")
3567                (nth 1 relation)
3568                (or (nth 2 relation) "") 0 0 "")
3569               gnus-newsgroup-dependencies nil)
3570          (push gnus-reffed-article-number gnus-newsgroup-limit)
3571          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3572          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3573                gnus-newsgroup-reads)
3574          (decf gnus-reffed-article-number)))
3575      (sort relations 'car-less-than-car))
3576     (gnus-message 7 "Making sparse threads...done")))
3577
3578 (defun gnus-build-old-threads ()
3579   ;; Look at all the articles that refer back to old articles, and
3580   ;; fetch the headers for the articles that aren't there.  This will
3581   ;; build complete threads - if the roots haven't been expired by the
3582   ;; server, that is.
3583   (let ((mail-parse-charset gnus-newsgroup-charset)
3584         id heads)
3585     (mapatoms
3586      (lambda (refs)
3587        (when (not (car (symbol-value refs)))
3588          (setq heads (cdr (symbol-value refs)))
3589          (while heads
3590            (if (memq (mail-header-number (caar heads))
3591                      gnus-newsgroup-dormant)
3592                (setq heads (cdr heads))
3593              (setq id (symbol-name refs))
3594              (while (and (setq id (gnus-build-get-header id))
3595                          (not (car (gnus-id-to-thread id)))))
3596              (setq heads nil)))))
3597      gnus-newsgroup-dependencies)))
3598
3599 ;; This function has to be called with point after the article number
3600 ;; on the beginning of the line.
3601 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3602   (let ((eol (gnus-point-at-eol))
3603         (buffer (current-buffer))
3604         header)
3605
3606     ;; overview: [num subject from date id refs chars lines misc]
3607     (unwind-protect
3608         (progn
3609           (narrow-to-region (point) eol)
3610           (unless (eobp)
3611             (forward-char))
3612
3613           (setq header
3614                 (make-full-mail-header
3615                  number                 ; number
3616                  (funcall gnus-decode-encoded-word-function
3617                           (nnheader-nov-field)) ; subject
3618                  (funcall gnus-decode-encoded-word-function
3619                           (nnheader-nov-field)) ; from
3620                  (nnheader-nov-field)   ; date
3621                  (nnheader-nov-read-message-id) ; id
3622                  (nnheader-nov-field)   ; refs
3623                  (nnheader-nov-read-integer) ; chars
3624                  (nnheader-nov-read-integer) ; lines
3625                  (unless (eobp)
3626                    (if (looking-at "Xref: ")
3627                        (goto-char (match-end 0)))
3628                    (nnheader-nov-field)) ; Xref
3629                  (nnheader-nov-parse-extra)))) ; extra
3630
3631       (widen))
3632
3633     (when gnus-alter-header-function
3634       (funcall gnus-alter-header-function header))
3635     (gnus-dependencies-add-header header dependencies force-new)))
3636
3637 (defun gnus-build-get-header (id)
3638   "Look through the buffer of NOV lines and find the header to ID.
3639 Enter this line into the dependencies hash table, and return
3640 the id of the parent article (if any)."
3641   (let ((deps gnus-newsgroup-dependencies)
3642         found header)
3643     (prog1
3644         (save-excursion
3645           (set-buffer nntp-server-buffer)
3646           (let ((case-fold-search nil))
3647             (goto-char (point-min))
3648             (while (and (not found)
3649                         (search-forward id nil t))
3650               (beginning-of-line)
3651               (setq found (looking-at
3652                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3653                                    (regexp-quote id))))
3654               (or found (beginning-of-line 2)))
3655             (when found
3656               (beginning-of-line)
3657               (and
3658                (setq header (gnus-nov-parse-line
3659                              (read (current-buffer)) deps))
3660                (gnus-parent-id (mail-header-references header))))))
3661       (when header
3662         (let ((number (mail-header-number header)))
3663           (push number gnus-newsgroup-limit)
3664           (push header gnus-newsgroup-headers)
3665           (if (memq number gnus-newsgroup-unselected)
3666               (progn
3667                 (push number gnus-newsgroup-unreads)
3668                 (setq gnus-newsgroup-unselected
3669                       (delq number gnus-newsgroup-unselected)))
3670             (push number gnus-newsgroup-ancient)))))))
3671
3672 (defun gnus-build-all-threads ()
3673   "Read all the headers."
3674   (let ((gnus-summary-ignore-duplicates t)
3675         (mail-parse-charset gnus-newsgroup-charset)
3676         (dependencies gnus-newsgroup-dependencies)
3677         header article)
3678     (save-excursion
3679       (set-buffer nntp-server-buffer)
3680       (let ((case-fold-search nil))
3681         (goto-char (point-min))
3682         (while (not (eobp))
3683           (ignore-errors
3684             (setq article (read (current-buffer))
3685                   header (gnus-nov-parse-line article dependencies)))
3686           (when header
3687             (save-excursion
3688               (set-buffer gnus-summary-buffer)
3689               (push header gnus-newsgroup-headers)
3690               (if (memq (setq article (mail-header-number header))
3691                         gnus-newsgroup-unselected)
3692                   (progn
3693                     (push article gnus-newsgroup-unreads)
3694                     (setq gnus-newsgroup-unselected
3695                           (delq article gnus-newsgroup-unselected)))
3696                 (push article gnus-newsgroup-ancient)))
3697             (forward-line 1)))))))
3698
3699 (defun gnus-summary-update-article-line (article header)
3700   "Update the line for ARTICLE using HEADERS."
3701   (let* ((id (mail-header-id header))
3702          (thread (gnus-id-to-thread id)))
3703     (unless thread
3704       (error "Article in no thread"))
3705     ;; Update the thread.
3706     (setcar thread header)
3707     (gnus-summary-goto-subject article)
3708     (let* ((datal (gnus-data-find-list article))
3709            (data (car datal))
3710            (length (when (cdr datal)
3711                      (- (gnus-data-pos data)
3712                         (gnus-data-pos (cadr datal)))))
3713            (buffer-read-only nil)
3714            (level (gnus-summary-thread-level)))
3715       (gnus-delete-line)
3716       (gnus-summary-insert-line
3717        header level nil (gnus-article-mark article)
3718        (memq article gnus-newsgroup-replied)
3719        (memq article gnus-newsgroup-expirable)
3720        ;; Only insert the Subject string when it's different
3721        ;; from the previous Subject string.
3722        (if (and
3723             gnus-show-threads
3724             (gnus-subject-equal
3725              (condition-case ()
3726                  (mail-header-subject
3727                   (gnus-data-header
3728                    (cadr
3729                     (gnus-data-find-list
3730                      article
3731                      (gnus-data-list t)))))
3732                ;; Error on the side of excessive subjects.
3733                (error ""))
3734              (mail-header-subject header)))
3735            ""
3736          (mail-header-subject header))
3737        nil (cdr (assq article gnus-newsgroup-scored))
3738        (memq article gnus-newsgroup-processable))
3739       (when length
3740         (gnus-data-update-list
3741          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3742
3743 (defun gnus-summary-update-article (article &optional iheader)
3744   "Update ARTICLE in the summary buffer."
3745   (set-buffer gnus-summary-buffer)
3746   (let* ((header (gnus-summary-article-header article))
3747          (id (mail-header-id header))
3748          (data (gnus-data-find article))
3749          (thread (gnus-id-to-thread id))
3750          (references (mail-header-references header))
3751          (parent
3752           (gnus-id-to-thread
3753            (or (gnus-parent-id
3754                 (when (and references
3755                            (not (equal "" references)))
3756                   references))
3757                "none")))
3758          (buffer-read-only nil)
3759          (old (car thread)))
3760     (when thread
3761       (unless iheader
3762         (setcar thread nil)
3763         (when parent
3764           (delq thread parent)))
3765       (if (gnus-summary-insert-subject id header)
3766        ;; Set the (possibly) new article number in the data structure.
3767           (gnus-data-set-number data (gnus-id-to-article id))
3768         (setcar thread old)
3769         nil))))
3770
3771 (defun gnus-rebuild-thread (id &optional line)
3772   "Rebuild the thread containing ID.
3773 If LINE, insert the rebuilt thread starting on line LINE."
3774   (let ((buffer-read-only nil)
3775         old-pos current thread data)
3776     (if (not gnus-show-threads)
3777         (setq thread (list (car (gnus-id-to-thread id))))
3778       ;; Get the thread this article is part of.
3779       (setq thread (gnus-remove-thread id)))
3780     (setq old-pos (gnus-point-at-bol))
3781     (setq current (save-excursion
3782                     (and (re-search-backward "[\r\n]" nil t)
3783                          (gnus-summary-article-number))))
3784     ;; If this is a gathered thread, we have to go some re-gathering.
3785     (when (stringp (car thread))
3786       (let ((subject (car thread))
3787             roots thr)
3788         (setq thread (cdr thread))
3789         (while thread
3790           (unless (memq (setq thr (gnus-id-to-thread
3791                                    (gnus-root-id
3792                                     (mail-header-id (caar thread)))))
3793                         roots)
3794             (push thr roots))
3795           (setq thread (cdr thread)))
3796         ;; We now have all (unique) roots.
3797         (if (= (length roots) 1)
3798             ;; All the loose roots are now one solid root.
3799             (setq thread (car roots))
3800           (setq thread (cons subject (gnus-sort-threads roots))))))
3801     (let (threads)
3802       ;; We then insert this thread into the summary buffer.
3803       (when line
3804         (goto-char (point-min))
3805         (forward-line (1- line)))
3806       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3807         (if gnus-show-threads
3808             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3809           (gnus-summary-prepare-unthreaded thread))
3810         (setq data (nreverse gnus-newsgroup-data))
3811         (setq threads gnus-newsgroup-threads))
3812       ;; We splice the new data into the data structure.
3813       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3814       ;;!!! then we want to insert at the beginning of the buffer.
3815       ;;!!! That happens to be true with Gnus now, but that may
3816       ;;!!! change in the future.  Perhaps.
3817       (gnus-data-enter-list
3818        (if line nil current) data (- (point) old-pos))
3819       (setq gnus-newsgroup-threads
3820             (nconc threads gnus-newsgroup-threads))
3821       (gnus-data-compute-positions))))
3822
3823 (defun gnus-number-to-header (number)
3824   "Return the header for article NUMBER."
3825   (let ((headers gnus-newsgroup-headers))
3826     (while (and headers
3827                 (not (= number (mail-header-number (car headers)))))
3828       (pop headers))
3829     (when headers
3830       (car headers))))
3831
3832 (defun gnus-parent-headers (in-headers &optional generation)
3833   "Return the headers of the GENERATIONeth parent of HEADERS."
3834   (unless generation
3835     (setq generation 1))
3836   (let ((parent t)
3837         (headers in-headers)
3838         references)
3839     (while (and parent
3840                 (not (zerop generation))
3841                 (setq references (mail-header-references headers)))
3842       (setq headers (if (and references
3843                              (setq parent (gnus-parent-id references)))
3844                         (car (gnus-id-to-thread parent))
3845                       nil))
3846       (decf generation))
3847     (and (not (eq headers in-headers))
3848          headers)))
3849
3850 (defun gnus-id-to-thread (id)
3851   "Return the (sub-)thread where ID appears."
3852   (gnus-gethash id gnus-newsgroup-dependencies))
3853
3854 (defun gnus-id-to-article (id)
3855   "Return the article number of ID."
3856   (let ((thread (gnus-id-to-thread id)))
3857     (when (and thread
3858                (car thread))
3859       (mail-header-number (car thread)))))
3860
3861 (defun gnus-id-to-header (id)
3862   "Return the article headers of ID."
3863   (car (gnus-id-to-thread id)))
3864
3865 (defun gnus-article-displayed-root-p (article)
3866   "Say whether ARTICLE is a root(ish) article."
3867   (let ((level (gnus-summary-thread-level article))
3868         (refs (mail-header-references  (gnus-summary-article-header article)))
3869         particle)
3870     (cond
3871      ((null level) nil)
3872      ((zerop level) t)
3873      ((null refs) t)
3874      ((null (gnus-parent-id refs)) t)
3875      ((and (= 1 level)
3876            (null (setq particle (gnus-id-to-article
3877                                  (gnus-parent-id refs))))
3878            (null (gnus-summary-thread-level particle)))))))
3879
3880 (defun gnus-root-id (id)
3881   "Return the id of the root of the thread where ID appears."
3882   (let (last-id prev)
3883     (while (and id (setq prev (car (gnus-id-to-thread id))))
3884       (setq last-id id
3885             id (gnus-parent-id (mail-header-references prev))))
3886     last-id))
3887
3888 (defun gnus-articles-in-thread (thread)
3889   "Return the list of articles in THREAD."
3890   (cons (mail-header-number (car thread))
3891         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3892
3893 (defun gnus-remove-thread (id &optional dont-remove)
3894   "Remove the thread that has ID in it."
3895   (let (headers thread last-id)
3896     ;; First go up in this thread until we find the root.
3897     (setq last-id (gnus-root-id id)
3898           headers (message-flatten-list (gnus-id-to-thread last-id)))
3899     ;; We have now found the real root of this thread.  It might have
3900     ;; been gathered into some loose thread, so we have to search
3901     ;; through the threads to find the thread we wanted.
3902     (let ((threads gnus-newsgroup-threads)
3903           sub)
3904       (while threads
3905         (setq sub (car threads))
3906         (if (stringp (car sub))
3907             ;; This is a gathered thread, so we look at the roots
3908             ;; below it to find whether this article is in this
3909             ;; gathered root.
3910             (progn
3911               (setq sub (cdr sub))
3912               (while sub
3913                 (when (member (caar sub) headers)
3914                   (setq thread (car threads)
3915                         threads nil
3916                         sub nil))
3917                 (setq sub (cdr sub))))
3918           ;; It's an ordinary thread, so we check it.
3919           (when (eq (car sub) (car headers))
3920             (setq thread sub
3921                   threads nil)))
3922         (setq threads (cdr threads)))
3923       ;; If this article is in no thread, then it's a root.
3924       (if thread
3925           (unless dont-remove
3926             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3927         (setq thread (gnus-id-to-thread last-id)))
3928       (when thread
3929         (prog1
3930             thread                      ; We return this thread.
3931           (unless dont-remove
3932             (if (stringp (car thread))
3933                 (progn
3934                   ;; If we use dummy roots, then we have to remove the
3935                   ;; dummy root as well.
3936                   (when (eq gnus-summary-make-false-root 'dummy)
3937                     ;; We go to the dummy root by going to
3938                     ;; the first sub-"thread", and then one line up.
3939                     (gnus-summary-goto-article
3940                      (mail-header-number (caadr thread)))
3941                     (forward-line -1)
3942                     (gnus-delete-line)
3943                     (gnus-data-compute-positions))
3944                   (setq thread (cdr thread))
3945                   (while thread
3946                     (gnus-remove-thread-1 (car thread))
3947                     (setq thread (cdr thread))))
3948               (gnus-remove-thread-1 thread))))))))
3949
3950 (defun gnus-remove-thread-1 (thread)
3951   "Remove the thread THREAD recursively."
3952   (let ((number (mail-header-number (pop thread)))
3953         d)
3954     (setq thread (reverse thread))
3955     (while thread
3956       (gnus-remove-thread-1 (pop thread)))
3957     (when (setq d (gnus-data-find number))
3958       (goto-char (gnus-data-pos d))
3959       (gnus-summary-show-thread)
3960       (gnus-data-remove
3961        number
3962        (- (gnus-point-at-bol)
3963           (prog1
3964               (1+ (gnus-point-at-eol))
3965             (gnus-delete-line)))))))
3966
3967 (defun gnus-sort-threads-1 (threads func)
3968   (sort (mapcar (lambda (thread)
3969                   (cons (car thread)
3970                         (and (cdr thread)
3971                              (gnus-sort-threads-1 (cdr thread) func))))
3972                 threads) func))
3973
3974 (defun gnus-sort-threads (threads)
3975   "Sort THREADS."
3976   (if (not gnus-thread-sort-functions)
3977       threads
3978     (gnus-message 8 "Sorting threads...")
3979     (prog1
3980         (gnus-sort-threads-1
3981          threads
3982          (gnus-make-sort-function gnus-thread-sort-functions))
3983       (gnus-message 8 "Sorting threads...done"))))
3984
3985 (defun gnus-sort-articles (articles)
3986   "Sort ARTICLES."
3987   (when gnus-article-sort-functions
3988     (gnus-message 7 "Sorting articles...")
3989     (prog1
3990         (setq gnus-newsgroup-headers
3991               (sort articles (gnus-make-sort-function
3992                               gnus-article-sort-functions)))
3993       (gnus-message 7 "Sorting articles...done"))))
3994
3995 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3996 (defmacro gnus-thread-header (thread)
3997   "Return header of first article in THREAD.
3998 Note that THREAD must never, ever be anything else than a variable -
3999 using some other form will lead to serious barfage."
4000   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4001   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4002   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4003         (vector thread) 2))
4004
4005 (defsubst gnus-article-sort-by-number (h1 h2)
4006   "Sort articles by article number."
4007   (< (mail-header-number h1)
4008      (mail-header-number h2)))
4009
4010 (defun gnus-thread-sort-by-number (h1 h2)
4011   "Sort threads by root article number."
4012   (gnus-article-sort-by-number
4013    (gnus-thread-header h1) (gnus-thread-header h2)))
4014
4015 (defsubst gnus-article-sort-by-lines (h1 h2)
4016   "Sort articles by article Lines header."
4017   (< (mail-header-lines h1)
4018      (mail-header-lines h2)))
4019
4020 (defun gnus-thread-sort-by-lines (h1 h2)
4021   "Sort threads by root article Lines header."
4022   (gnus-article-sort-by-lines
4023    (gnus-thread-header h1) (gnus-thread-header h2)))
4024
4025 (defsubst gnus-article-sort-by-chars (h1 h2)
4026   "Sort articles by octet length."
4027   (< (mail-header-chars h1)
4028      (mail-header-chars h2)))
4029
4030 (defun gnus-thread-sort-by-chars (h1 h2)
4031   "Sort threads by root article octet length."
4032   (gnus-article-sort-by-chars
4033    (gnus-thread-header h1) (gnus-thread-header h2)))
4034
4035 (defsubst gnus-article-sort-by-author (h1 h2)
4036   "Sort articles by root author."
4037   (string-lessp
4038    (let ((extract (funcall
4039                    gnus-extract-address-components
4040                    (mail-header-from h1))))
4041      (or (car extract) (cadr extract) ""))
4042    (let ((extract (funcall
4043                    gnus-extract-address-components
4044                    (mail-header-from h2))))
4045      (or (car extract) (cadr extract) ""))))
4046
4047 (defun gnus-thread-sort-by-author (h1 h2)
4048   "Sort threads by root author."
4049   (gnus-article-sort-by-author
4050    (gnus-thread-header h1)  (gnus-thread-header h2)))
4051
4052 (defsubst gnus-article-sort-by-subject (h1 h2)
4053   "Sort articles by root subject."
4054   (string-lessp
4055    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4056    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4057
4058 (defun gnus-thread-sort-by-subject (h1 h2)
4059   "Sort threads by root subject."
4060   (gnus-article-sort-by-subject
4061    (gnus-thread-header h1) (gnus-thread-header h2)))
4062
4063 (defsubst gnus-article-sort-by-date (h1 h2)
4064   "Sort articles by root article date."
4065   (time-less-p
4066    (gnus-date-get-time (mail-header-date h1))
4067    (gnus-date-get-time (mail-header-date h2))))
4068
4069 (defun gnus-thread-sort-by-date (h1 h2)
4070   "Sort threads by root article date."
4071   (gnus-article-sort-by-date
4072    (gnus-thread-header h1) (gnus-thread-header h2)))
4073
4074 (defsubst gnus-article-sort-by-score (h1 h2)
4075   "Sort articles by root article score.
4076 Unscored articles will be counted as having a score of zero."
4077   (> (or (cdr (assq (mail-header-number h1)
4078                     gnus-newsgroup-scored))
4079          gnus-summary-default-score 0)
4080      (or (cdr (assq (mail-header-number h2)
4081                     gnus-newsgroup-scored))
4082          gnus-summary-default-score 0)))
4083
4084 (defun gnus-thread-sort-by-score (h1 h2)
4085   "Sort threads by root article score."
4086   (gnus-article-sort-by-score
4087    (gnus-thread-header h1) (gnus-thread-header h2)))
4088
4089 (defun gnus-thread-sort-by-total-score (h1 h2)
4090   "Sort threads by the sum of all scores in the thread.
4091 Unscored articles will be counted as having a score of zero."
4092   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4093
4094 (defun gnus-thread-total-score (thread)
4095   ;; This function find the total score of THREAD.
4096   (cond ((null thread)
4097          0)
4098         ((consp thread)
4099          (if (stringp (car thread))
4100              (apply gnus-thread-score-function 0
4101                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4102            (gnus-thread-total-score-1 thread)))
4103         (t
4104          (gnus-thread-total-score-1 (list thread)))))
4105
4106 (defun gnus-thread-total-score-1 (root)
4107   ;; This function find the total score of the thread below ROOT.
4108   (setq root (car root))
4109   (apply gnus-thread-score-function
4110          (or (append
4111               (mapcar 'gnus-thread-total-score
4112                       (cdr (gnus-id-to-thread (mail-header-id root))))
4113               (when (> (mail-header-number root) 0)
4114                 (list (or (cdr (assq (mail-header-number root)
4115                                      gnus-newsgroup-scored))
4116                           gnus-summary-default-score 0))))
4117              (list gnus-summary-default-score)
4118              '(0))))
4119
4120 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4121 (defvar gnus-tmp-prev-subject nil)
4122 (defvar gnus-tmp-false-parent nil)
4123 (defvar gnus-tmp-root-expunged nil)
4124 (defvar gnus-tmp-dummy-line nil)
4125
4126 (eval-when-compile (defvar gnus-tmp-header))
4127 (defun gnus-extra-header (type &optional header)
4128   "Return the extra header of TYPE."
4129   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4130       ""))
4131
4132 (defvar gnus-tmp-thread-tree-header-string "")
4133
4134 (defvar gnus-sum-thread-tree-root "> "
4135   "With %B spec, used for the root of a thread.
4136 If nil, use subject instead.")
4137 (defvar gnus-sum-thread-tree-single-indent ""
4138   "With %B spec, used for a thread with just one message.
4139 If nil, use subject instead.")
4140 (defvar gnus-sum-thread-tree-vertical "| "
4141   "With %B spec, used for drawing a vertical line.")
4142 (defvar gnus-sum-thread-tree-indent "  "
4143   "With %B spec, used for indenting.")
4144 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4145   "With %B spec, used for a leaf with brothers.")
4146 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4147   "With %B spec, used for a leaf without brothers.")
4148
4149 (defun gnus-summary-prepare-threads (threads)
4150   "Prepare summary buffer from THREADS and indentation LEVEL.
4151 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4152 or a straight list of headers."
4153   (gnus-message 7 "Generating summary...")
4154
4155   (setq gnus-newsgroup-threads threads)
4156   (beginning-of-line)
4157
4158   (let ((gnus-tmp-level 0)
4159         (default-score (or gnus-summary-default-score 0))
4160         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4161         thread number subject stack state gnus-tmp-gathered beg-match
4162         new-roots gnus-tmp-new-adopts thread-end
4163         gnus-tmp-header gnus-tmp-unread
4164         gnus-tmp-replied gnus-tmp-subject-or-nil
4165         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4166         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4167         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4168         tree-stack)
4169
4170     (setq gnus-tmp-prev-subject nil)
4171
4172     (if (vectorp (car threads))
4173         ;; If this is a straight (sic) list of headers, then a
4174         ;; threaded summary display isn't required, so we just create
4175         ;; an unthreaded one.
4176         (gnus-summary-prepare-unthreaded threads)
4177
4178       ;; Do the threaded display.
4179
4180       (while (or threads stack gnus-tmp-new-adopts new-roots)
4181
4182         (if (and (= gnus-tmp-level 0)
4183                  (or (not stack)
4184                      (= (caar stack) 0))
4185                  (not gnus-tmp-false-parent)
4186                  (or gnus-tmp-new-adopts new-roots))
4187             (if gnus-tmp-new-adopts
4188                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4189                       thread (list (car gnus-tmp-new-adopts))
4190                       gnus-tmp-header (caar thread)
4191                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4192               (when new-roots
4193                 (setq thread (list (car new-roots))
4194                       gnus-tmp-header (caar thread)
4195                       new-roots (cdr new-roots))))
4196
4197           (if threads
4198               ;; If there are some threads, we do them before the
4199               ;; threads on the stack.
4200               (setq thread threads
4201                     gnus-tmp-header (caar thread))
4202             ;; There were no current threads, so we pop something off
4203             ;; the stack.
4204             (setq state (car stack)
4205                   gnus-tmp-level (car state)
4206                   tree-stack (cadr state)
4207                   thread (caddr state)
4208                   stack (cdr stack)
4209                   gnus-tmp-header (caar thread))))
4210
4211         (setq gnus-tmp-false-parent nil)
4212         (setq gnus-tmp-root-expunged nil)
4213         (setq thread-end nil)
4214
4215         (if (stringp gnus-tmp-header)
4216             ;; The header is a dummy root.
4217             (cond
4218              ((eq gnus-summary-make-false-root 'adopt)
4219               ;; We let the first article adopt the rest.
4220               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4221                                                (cddar thread)))
4222               (setq gnus-tmp-gathered
4223                     (nconc (mapcar
4224                             (lambda (h) (mail-header-number (car h)))
4225                             (cddar thread))
4226                            gnus-tmp-gathered))
4227               (setq thread (cons (list (caar thread)
4228                                        (cadar thread))
4229                                  (cdr thread)))
4230               (setq gnus-tmp-level -1
4231                     gnus-tmp-false-parent t))
4232              ((eq gnus-summary-make-false-root 'empty)
4233               ;; We print adopted articles with empty subject fields.
4234               (setq gnus-tmp-gathered
4235                     (nconc (mapcar
4236                             (lambda (h) (mail-header-number (car h)))
4237                             (cddar thread))
4238                            gnus-tmp-gathered))
4239               (setq gnus-tmp-level -1))
4240              ((eq gnus-summary-make-false-root 'dummy)
4241               ;; We remember that we probably want to output a dummy
4242               ;; root.
4243               (setq gnus-tmp-dummy-line gnus-tmp-header)
4244               (setq gnus-tmp-prev-subject gnus-tmp-header))
4245              (t
4246               ;; We do not make a root for the gathered
4247               ;; sub-threads at all.
4248               (setq gnus-tmp-level -1)))
4249
4250           (setq number (mail-header-number gnus-tmp-header)
4251                 subject (mail-header-subject gnus-tmp-header))
4252
4253           (cond
4254            ;; If the thread has changed subject, we might want to make
4255            ;; this subthread into a root.
4256            ((and (null gnus-thread-ignore-subject)
4257                  (not (zerop gnus-tmp-level))
4258                  gnus-tmp-prev-subject
4259                  (not (inline
4260                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4261             (setq new-roots (nconc new-roots (list (car thread)))
4262                   thread-end t
4263                   gnus-tmp-header nil))
4264            ;; If the article lies outside the current limit,
4265            ;; then we do not display it.
4266            ((not (memq number gnus-newsgroup-limit))
4267             (setq gnus-tmp-gathered
4268                   (nconc (mapcar
4269                           (lambda (h) (mail-header-number (car h)))
4270                           (cdar thread))
4271                          gnus-tmp-gathered))
4272             (setq gnus-tmp-new-adopts (if (cdar thread)
4273                                           (append gnus-tmp-new-adopts
4274                                                   (cdar thread))
4275                                         gnus-tmp-new-adopts)
4276                   thread-end t
4277                   gnus-tmp-header nil)
4278             (when (zerop gnus-tmp-level)
4279               (setq gnus-tmp-root-expunged t)))
4280            ;; Perhaps this article is to be marked as read?
4281            ((and gnus-summary-mark-below
4282                  (< (or (cdr (assq number gnus-newsgroup-scored))
4283                         default-score)
4284                     gnus-summary-mark-below)
4285                  ;; Don't touch sparse articles.
4286                  (not (gnus-summary-article-sparse-p number))
4287                  (not (gnus-summary-article-ancient-p number)))
4288             (setq gnus-newsgroup-unreads
4289                   (delq number gnus-newsgroup-unreads))
4290             (if gnus-newsgroup-auto-expire
4291                 (push number gnus-newsgroup-expirable)
4292               (push (cons number gnus-low-score-mark)
4293                     gnus-newsgroup-reads))))
4294
4295           (when gnus-tmp-header
4296             ;; We may have an old dummy line to output before this
4297             ;; article.
4298             (when (and gnus-tmp-dummy-line
4299                        (gnus-subject-equal
4300                         gnus-tmp-dummy-line
4301                         (mail-header-subject gnus-tmp-header)))
4302               (gnus-summary-insert-dummy-line
4303                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4304               (setq gnus-tmp-dummy-line nil))
4305
4306             ;; Compute the mark.
4307             (setq gnus-tmp-unread (gnus-article-mark number))
4308
4309             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4310                                   gnus-tmp-header gnus-tmp-level)
4311                   gnus-newsgroup-data)
4312
4313             ;; Actually insert the line.
4314             (setq
4315              gnus-tmp-subject-or-nil
4316              (cond
4317               ((and gnus-thread-ignore-subject
4318                     gnus-tmp-prev-subject
4319                     (not (inline (gnus-subject-equal
4320                                   gnus-tmp-prev-subject subject))))
4321                subject)
4322               ((zerop gnus-tmp-level)
4323                (if (and (eq gnus-summary-make-false-root 'empty)
4324                         (memq number gnus-tmp-gathered)
4325                         gnus-tmp-prev-subject
4326                         (inline (gnus-subject-equal
4327                                  gnus-tmp-prev-subject subject)))
4328                    gnus-summary-same-subject
4329                  subject))
4330               (t gnus-summary-same-subject)))
4331             (if (and (eq gnus-summary-make-false-root 'adopt)
4332                      (= gnus-tmp-level 1)
4333                      (memq number gnus-tmp-gathered))
4334                 (setq gnus-tmp-opening-bracket ?\<
4335                       gnus-tmp-closing-bracket ?\>)
4336               (setq gnus-tmp-opening-bracket ?\[
4337                     gnus-tmp-closing-bracket ?\]))
4338             (setq
4339              gnus-tmp-indentation
4340              (aref gnus-thread-indent-array gnus-tmp-level)
4341              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4342              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4343                                 gnus-summary-default-score 0)
4344              gnus-tmp-score-char
4345              (if (or (null gnus-summary-default-score)
4346                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4347                          gnus-summary-zcore-fuzz))
4348                  ?                      ;Whitespace
4349                (if (< gnus-tmp-score gnus-summary-default-score)
4350                    gnus-score-below-mark gnus-score-over-mark))
4351              gnus-tmp-replied
4352              (cond ((memq number gnus-newsgroup-processable)
4353                     gnus-process-mark)
4354                    ((memq number gnus-newsgroup-cached)
4355                     gnus-cached-mark)
4356                    ((memq number gnus-newsgroup-replied)
4357                     gnus-replied-mark)
4358                    ((memq number gnus-newsgroup-forwarded)
4359                     gnus-forwarded-mark)
4360                    ((memq number gnus-newsgroup-saved)
4361                     gnus-saved-mark)
4362                    ((memq number gnus-newsgroup-recent)
4363                     gnus-recent-mark)
4364                    ((memq number gnus-newsgroup-unseen)
4365                     gnus-unseen-mark)
4366                    (t gnus-no-mark))
4367              gnus-tmp-from (mail-header-from gnus-tmp-header)
4368              gnus-tmp-name
4369              (cond
4370               ((string-match "<[^>]+> *$" gnus-tmp-from)
4371                (setq beg-match (match-beginning 0))
4372                (or (and (string-match "^\".+\"" gnus-tmp-from)
4373                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4374                    (substring gnus-tmp-from 0 beg-match)))
4375               ((string-match "(.+)" gnus-tmp-from)
4376                (substring gnus-tmp-from
4377                           (1+ (match-beginning 0)) (1- (match-end 0))))
4378               (t gnus-tmp-from))
4379              gnus-tmp-thread-tree-header-string
4380              (cond 
4381               ((not gnus-show-threads) "")
4382               ((zerop gnus-tmp-level)
4383                (if (cdar thread) 
4384                    (or gnus-sum-thread-tree-root subject)
4385                  (or gnus-sum-thread-tree-single-indent subject)))
4386               (t
4387                (concat (apply 'concat
4388                               (mapcar (lambda (item) 
4389                                         (if (= item 1) 
4390                                             gnus-sum-thread-tree-vertical
4391                                           gnus-sum-thread-tree-indent))
4392                                       (cdr (reverse tree-stack))))
4393                        (if (nth 1 thread) 
4394                            gnus-sum-thread-tree-leaf-with-other
4395                          gnus-sum-thread-tree-single-leaf)))))
4396             (when (string= gnus-tmp-name "")
4397               (setq gnus-tmp-name gnus-tmp-from))
4398             (unless (numberp gnus-tmp-lines)
4399               (setq gnus-tmp-lines -1))
4400             (if (= gnus-tmp-lines -1)
4401                 (setq gnus-tmp-lines "?")
4402               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4403             (gnus-put-text-property
4404              (point)
4405              (progn (eval gnus-summary-line-format-spec) (point))
4406              'gnus-number number)
4407             (when gnus-visual-p
4408               (forward-line -1)
4409               (gnus-run-hooks 'gnus-summary-update-hook)
4410               (forward-line 1))
4411
4412             (setq gnus-tmp-prev-subject subject)))
4413
4414         (when (nth 1 thread)
4415           (push (list (max 0 gnus-tmp-level)
4416                       (copy-list tree-stack)
4417                       (nthcdr 1 thread))
4418                 stack))
4419         (push (if (nth 1 thread) 1 0) tree-stack)
4420         (incf gnus-tmp-level)
4421         (setq threads (if thread-end nil (cdar thread)))
4422         (unless threads
4423           (setq gnus-tmp-level 0)))))
4424   (gnus-message 7 "Generating summary...done"))
4425
4426 (defun gnus-summary-prepare-unthreaded (headers)
4427   "Generate an unthreaded summary buffer based on HEADERS."
4428   (let (header number mark)
4429
4430     (beginning-of-line)
4431
4432     (while headers
4433       ;; We may have to root out some bad articles...
4434       (when (memq (setq number (mail-header-number
4435                                 (setq header (pop headers))))
4436                   gnus-newsgroup-limit)
4437         ;; Mark article as read when it has a low score.
4438         (when (and gnus-summary-mark-below
4439                    (< (or (cdr (assq number gnus-newsgroup-scored))
4440                           gnus-summary-default-score 0)
4441                       gnus-summary-mark-below)
4442                    (not (gnus-summary-article-ancient-p number)))
4443           (setq gnus-newsgroup-unreads
4444                 (delq number gnus-newsgroup-unreads))
4445           (if gnus-newsgroup-auto-expire
4446               (push number gnus-newsgroup-expirable)
4447             (push (cons number gnus-low-score-mark)
4448                   gnus-newsgroup-reads)))
4449
4450         (setq mark (gnus-article-mark number))
4451         (push (gnus-data-make number mark (1+ (point)) header 0)
4452               gnus-newsgroup-data)
4453         (gnus-summary-insert-line
4454          header 0 number
4455          mark (memq number gnus-newsgroup-replied)
4456          (memq number gnus-newsgroup-expirable)
4457          (mail-header-subject header) nil
4458          (cdr (assq number gnus-newsgroup-scored))
4459          (memq number gnus-newsgroup-processable))))))
4460
4461 (defun gnus-summary-remove-list-identifiers ()
4462   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4463   (let ((regexp (if (consp gnus-list-identifiers)
4464                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4465                   gnus-list-identifiers))
4466         changed subject)
4467     (when regexp
4468       (dolist (header gnus-newsgroup-headers)
4469         (setq subject (mail-header-subject header)
4470               changed nil)
4471         (while (string-match
4472                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4473                 subject)
4474           (setq subject
4475                 (concat (substring subject 0 (match-beginning 2))
4476                         (substring subject (match-end 0)))
4477                 changed t))
4478         (when (and changed
4479                    (string-match
4480                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4481           (setq subject
4482                 (concat (substring subject 0 (match-beginning 1))
4483                         (substring subject (match-end 1)))))
4484         (when changed
4485           (mail-header-set-subject header subject))))))
4486
4487 (defun gnus-fetch-headers (articles)
4488   "Fetch headers of ARTICLES."
4489   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4490     (gnus-message 5 "Fetching headers for %s..." name)
4491     (prog1
4492         (if (eq 'nov
4493                 (setq gnus-headers-retrieved-by
4494                       (gnus-retrieve-headers
4495                        articles gnus-newsgroup-name
4496                        ;; We might want to fetch old headers, but
4497                        ;; not if there is only 1 article.
4498                        (and (or (and
4499                                  (not (eq gnus-fetch-old-headers 'some))
4500                                  (not (numberp gnus-fetch-old-headers)))
4501                                 (> (length articles) 1))
4502                             gnus-fetch-old-headers))))
4503             (gnus-get-newsgroup-headers-xover
4504              articles nil nil gnus-newsgroup-name t)
4505           (gnus-get-newsgroup-headers))
4506       (gnus-message 5 "Fetching headers for %s...done" name))))
4507
4508 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4509   "Select newsgroup GROUP.
4510 If READ-ALL is non-nil, all articles in the group are selected.
4511 If SELECT-ARTICLES, only select those articles from GROUP."
4512   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4513          ;;!!! Dirty hack; should be removed.
4514          (gnus-summary-ignore-duplicates
4515           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4516               t
4517             gnus-summary-ignore-duplicates))
4518          (info (nth 2 entry))
4519          articles fetched-articles cached)
4520
4521     (unless (gnus-check-server
4522              (set (make-local-variable 'gnus-current-select-method)
4523                   (gnus-find-method-for-group group)))
4524       (error "Couldn't open server"))
4525
4526     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4527         (gnus-activate-group group)     ; Or we can activate it...
4528         (progn                          ; Or we bug out.
4529           (when (equal major-mode 'gnus-summary-mode)
4530             (kill-buffer (current-buffer)))
4531           (error "Couldn't activate group %s: %s"
4532                  group (gnus-status-message group))))
4533
4534     (unless (gnus-request-group group t)
4535       (when (equal major-mode 'gnus-summary-mode)
4536         (kill-buffer (current-buffer)))
4537       (error "Couldn't request group %s: %s"
4538              group (gnus-status-message group)))
4539
4540     (setq gnus-newsgroup-name group
4541           gnus-newsgroup-unselected nil
4542           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4543     
4544     (let ((display (gnus-group-find-parameter group 'display)))
4545       (setq gnus-newsgroup-display
4546             (cond
4547              ((not (zerop (or (car-safe read-all) 0)))
4548               ;; The user entered the group with C-u SPC/RET, let's show
4549               ;; all articles.
4550               'gnus-not-ignore)
4551              ((eq display 'all)
4552               'gnus-not-ignore)
4553              ((arrayp display)
4554               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4555              (t
4556               nil))))
4557       
4558     (gnus-summary-setup-default-charset)
4559
4560     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4561     (when (gnus-virtual-group-p group)
4562       (setq cached gnus-newsgroup-cached))
4563
4564     (setq gnus-newsgroup-unreads
4565           (gnus-set-difference
4566            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4567            gnus-newsgroup-dormant))
4568
4569     (setq gnus-newsgroup-processable nil)
4570
4571     (gnus-update-read-articles group gnus-newsgroup-unreads)
4572
4573     ;; Adjust and set lists of article marks.
4574     (when info
4575       (gnus-adjust-marked-articles info))
4576     
4577     (if (setq articles select-articles)
4578         (setq gnus-newsgroup-unselected
4579               (gnus-sorted-intersection
4580                gnus-newsgroup-unreads
4581                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4582       (setq articles (gnus-articles-to-read group read-all)))
4583
4584     (cond
4585      ((null articles)
4586       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4587       'quit)
4588      ((eq articles 0) nil)
4589      (t
4590       ;; Init the dependencies hash table.
4591       (setq gnus-newsgroup-dependencies
4592             (gnus-make-hashtable (length articles)))
4593       (gnus-set-global-variables)
4594       ;; Retrieve the headers and read them in.
4595       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4596
4597       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4598       (when cached
4599         (setq gnus-newsgroup-cached cached))
4600
4601       ;; Suppress duplicates?
4602       (when gnus-suppress-duplicates
4603         (gnus-dup-suppress-articles))
4604
4605       ;; Set the initial limit.
4606       (setq gnus-newsgroup-limit (copy-sequence articles))
4607       ;; Remove canceled articles from the list of unread articles.
4608       (setq fetched-articles
4609             (mapcar (lambda (headers) (mail-header-number headers))
4610                     gnus-newsgroup-headers))
4611       (setq gnus-newsgroup-articles fetched-articles)
4612       (setq gnus-newsgroup-unreads
4613             (gnus-set-sorted-intersection
4614              gnus-newsgroup-unreads fetched-articles))
4615
4616       (let ((marks (assq 'seen (gnus-info-marks info))))
4617         ;; The `seen' marks are treated specially.
4618         (when (setq gnus-newsgroup-seen (cdr marks))
4619           (dolist (article gnus-newsgroup-articles)
4620             (unless (gnus-member-of-range
4621                      article gnus-newsgroup-seen)
4622               (push article gnus-newsgroup-unseen)))))
4623
4624       ;; Removed marked articles that do not exist.
4625       (gnus-update-missing-marks
4626        (gnus-sorted-complement fetched-articles articles))
4627       ;; We might want to build some more threads first.
4628       (when (and gnus-fetch-old-headers
4629                  (eq gnus-headers-retrieved-by 'nov))
4630         (if (eq gnus-fetch-old-headers 'invisible)
4631             (gnus-build-all-threads)
4632           (gnus-build-old-threads)))
4633       ;; Let the Gnus agent mark articles as read.
4634       (when gnus-agent
4635         (gnus-agent-get-undownloaded-list))
4636       ;; Remove list identifiers from subject
4637       (when gnus-list-identifiers
4638         (gnus-summary-remove-list-identifiers))
4639       ;; Check whether auto-expire is to be done in this group.
4640       (setq gnus-newsgroup-auto-expire
4641             (gnus-group-auto-expirable-p group))
4642       ;; Set up the article buffer now, if necessary.
4643       (unless gnus-single-article-buffer
4644         (gnus-article-setup-buffer))
4645       ;; First and last article in this newsgroup.
4646       (when gnus-newsgroup-headers
4647         (setq gnus-newsgroup-begin
4648               (mail-header-number (car gnus-newsgroup-headers))
4649               gnus-newsgroup-end
4650               (mail-header-number
4651                (gnus-last-element gnus-newsgroup-headers))))
4652       ;; GROUP is successfully selected.
4653       (or gnus-newsgroup-headers t)))))
4654
4655 (defun gnus-summary-display-make-predicate (display)
4656   (require 'gnus-agent)
4657   (when (= (length display) 1)
4658     (setq display (car display)))
4659   (unless gnus-summary-display-cache
4660     (dolist (elem (append (list (cons 'read 'read)
4661                                 (cons 'unseen 'unseen))
4662                           gnus-article-mark-lists))
4663       (push (cons (cdr elem)
4664                   (gnus-byte-compile
4665                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4666             gnus-summary-display-cache)))
4667   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4668     (gnus-get-predicate display)))
4669
4670 ;; Uses the dynamically bound `number' variable.
4671 (defvar number)
4672 (defun gnus-article-marked-p (type &optional article)
4673   (let ((article (or article number)))
4674     (cond
4675      ((eq type 'tick)
4676       (memq article gnus-newsgroup-marked))
4677      ((eq type 'unsend)
4678       (memq article gnus-newsgroup-unsendable))
4679      ((eq type 'undownload)
4680       (memq article gnus-newsgroup-undownloaded))
4681      ((eq type 'download)
4682       (memq article gnus-newsgroup-downloadable))
4683      ((eq type 'unread)
4684       (memq article gnus-newsgroup-unreads))
4685      ((eq type 'read)
4686       (memq article gnus-newsgroup-reads))
4687      ((eq type 'dormant)
4688       (memq article gnus-newsgroup-dormant) )
4689      ((eq type 'expire)
4690       (memq article gnus-newsgroup-expirable))
4691      ((eq type 'reply)
4692       (memq article gnus-newsgroup-replied))
4693      ((eq type 'killed)
4694       (memq article gnus-newsgroup-killed))
4695      ((eq type 'bookmark)
4696       (assq article gnus-newsgroup-bookmarks))
4697      ((eq type 'score)
4698       (assq article gnus-newsgroup-scored))
4699      ((eq type 'save)
4700       (memq article gnus-newsgroup-saved))
4701      ((eq type 'cache)
4702       (memq article gnus-newsgroup-cached))
4703      ((eq type 'forward)
4704       (memq article gnus-newsgroup-forwarded))
4705      ((eq type 'seen)
4706       (not (memq article gnus-newsgroup-unseen)))
4707      ((eq type 'recent)
4708       (memq article gnus-newsgroup-recent))
4709      (t t))))
4710
4711 (defun gnus-articles-to-read (group &optional read-all)
4712   "Find out what articles the user wants to read."
4713   (let* ((articles
4714           ;; Select all articles if `read-all' is non-nil, or if there
4715           ;; are no unread articles.
4716           (if (or read-all
4717                   (and (zerop (length gnus-newsgroup-marked))
4718                        (zerop (length gnus-newsgroup-unreads)))
4719                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4720               ;; We want to select the headers for all the articles in
4721               ;; the group, so we select either all the active
4722               ;; articles in the group, or (if that's nil), the
4723               ;; articles in the cache.
4724               (or
4725                (gnus-uncompress-range (gnus-active group))
4726                (gnus-cache-articles-in-group group))
4727             ;; Select only the "normal" subset of articles.
4728             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4729                           (copy-sequence gnus-newsgroup-unreads))
4730                   '<)))
4731          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4732          (scored (length scored-list))
4733          (number (length articles))
4734          (marked (+ (length gnus-newsgroup-marked)
4735                     (length gnus-newsgroup-dormant)))
4736          (select
4737           (cond
4738            ((numberp read-all)
4739             read-all)
4740            (t
4741             (condition-case ()
4742                 (cond
4743                  ((and (or (<= scored marked) (= scored number))
4744                        (numberp gnus-large-newsgroup)
4745                        (> number gnus-large-newsgroup))
4746                   (let ((input
4747                          (read-string
4748                           (format
4749                            "How many articles from %s (default %d): "
4750                            (gnus-limit-string
4751                             (gnus-group-decoded-name gnus-newsgroup-name)
4752                             35)
4753                            number))))
4754                     (if (string-match "^[ \t]*$" input) number input)))
4755                  ((and (> scored marked) (< scored number)
4756                        (> (- scored number) 20))
4757                   (let ((input
4758                          (read-string
4759                           (format "%s %s (%d scored, %d total): "
4760                                   "How many articles from"
4761                                   (gnus-group-decoded-name group)
4762                                   scored number))))
4763                     (if (string-match "^[ \t]*$" input)
4764                         number input)))
4765                  (t number))
4766               (quit
4767                (message "Quit getting the articles to read")
4768                nil))))))
4769     (setq select (if (stringp select) (string-to-number select) select))
4770     (if (or (null select) (zerop select))
4771         select
4772       (if (and (not (zerop scored)) (<= (abs select) scored))
4773           (progn
4774             (setq articles (sort scored-list '<))
4775             (setq number (length articles)))
4776         (setq articles (copy-sequence articles)))
4777
4778       (when (< (abs select) number)
4779         (if (< select 0)
4780             ;; Select the N oldest articles.
4781             (setcdr (nthcdr (1- (abs select)) articles) nil)
4782           ;; Select the N most recent articles.
4783           (setq articles (nthcdr (- number select) articles))))
4784       (setq gnus-newsgroup-unselected
4785             (gnus-sorted-intersection
4786              gnus-newsgroup-unreads
4787              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4788       (when gnus-alter-articles-to-read-function
4789         (setq gnus-newsgroup-unreads
4790               (sort
4791                (funcall gnus-alter-articles-to-read-function
4792                         gnus-newsgroup-name gnus-newsgroup-unreads)
4793                '<)))
4794       articles)))
4795
4796 (defun gnus-killed-articles (killed articles)
4797   (let (out)
4798     (while articles
4799       (when (inline (gnus-member-of-range (car articles) killed))
4800         (push (car articles) out))
4801       (setq articles (cdr articles)))
4802     out))
4803
4804 (defun gnus-uncompress-marks (marks)
4805   "Uncompress the mark ranges in MARKS."
4806   (let ((uncompressed '(score bookmark))
4807         out)
4808     (while marks
4809       (if (memq (caar marks) uncompressed)
4810           (push (car marks) out)
4811         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4812       (setq marks (cdr marks)))
4813     out))
4814
4815 (defun gnus-article-mark-to-type (mark)
4816   "Return the type of MARK."
4817   (or (cadr (assq mark gnus-article-special-mark-lists))
4818       'list))
4819
4820 (defun gnus-article-unpropagatable-p (mark)
4821   "Return whether MARK should be propagated to backend."
4822   (memq mark gnus-article-unpropagated-mark-lists))
4823
4824 (defun gnus-adjust-marked-articles (info)
4825   "Set all article lists and remove all marks that are no longer valid."
4826   (let* ((marked-lists (gnus-info-marks info))
4827          (active (gnus-active (gnus-info-group info)))
4828          (min (car active))
4829          (max (cdr active))
4830          (types gnus-article-mark-lists)
4831          marks var articles article mark mark-type)
4832
4833     (dolist (marks marked-lists)
4834       (setq mark (car marks)
4835             mark-type (gnus-article-mark-to-type mark)
4836             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4837
4838       ;; We set the variable according to the type of the marks list,
4839       ;; and then adjust the marks to a subset of the active articles.
4840       (cond
4841        ;; Adjust "simple" lists.
4842        ((eq mark-type 'list)
4843         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4844         (when (memq mark '(tick dormant expire reply save))
4845           (while articles
4846             (when (or (< (setq article (pop articles)) min) (> article max))
4847               (set var (delq article (symbol-value var)))))))
4848        ;; Adjust assocs.
4849        ((eq mark-type 'tuple)
4850         (set var (setq articles (cdr marks)))
4851         (when (not (listp (cdr (symbol-value var))))
4852           (set var (list (symbol-value var))))
4853         (when (not (listp (cdr articles)))
4854           (setq articles (list articles)))
4855         (while articles
4856           (when (or (not (consp (setq article (pop articles))))
4857                     (< (car article) min)
4858                     (> (car article) max))
4859             (set var (delq article (symbol-value var))))))
4860        ((eq mark-type 'range)
4861         (cond
4862          ((eq mark 'seen))))))))
4863
4864 (defun gnus-update-missing-marks (missing)
4865   "Go through the list of MISSING articles and remove them from the mark lists."
4866   (when missing
4867     (let (var m)
4868       ;; Go through all types.
4869       (dolist (elem gnus-article-mark-lists)
4870         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4871           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4872           (when (symbol-value var)
4873             ;; This list has articles.  So we delete all missing
4874             ;; articles from it.
4875             (setq m missing)
4876             (while m
4877               (set var (delq (pop m) (symbol-value var))))))))))
4878
4879 (defun gnus-update-marks ()
4880   "Enter the various lists of marked articles into the newsgroup info list."
4881   (let ((types gnus-article-mark-lists)
4882         (info (gnus-get-info gnus-newsgroup-name))
4883         type list newmarked symbol delta-marks)
4884     (when info
4885       ;; Add all marks lists to the list of marks lists.
4886       (while (setq type (pop types))
4887         (setq list (symbol-value
4888                     (setq symbol
4889                           (intern (format "gnus-newsgroup-%s" (car type))))))
4890
4891         (when list
4892           ;; Get rid of the entries of the articles that have the
4893           ;; default score.
4894           (when (and (eq (cdr type) 'score)
4895                      gnus-save-score
4896                      list)
4897             (let* ((arts list)
4898                    (prev (cons nil list))
4899                    (all prev))
4900               (while arts
4901                 (if (or (not (consp (car arts)))
4902                         (= (cdar arts) gnus-summary-default-score))
4903                     (setcdr prev (cdr arts))
4904                   (setq prev arts))
4905                 (setq arts (cdr arts)))
4906               (setq list (cdr all)))))
4907
4908         (when (eq (cdr type) 'seen)
4909           (setq list 
4910                 (if list
4911                     (gnus-add-to-range list gnus-newsgroup-unseen)
4912                   (gnus-compress-sequence gnus-newsgroup-articles))))
4913
4914         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4915           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4916
4917         (when (and (gnus-check-backend-function
4918                     'request-set-mark gnus-newsgroup-name)
4919                    (not (gnus-article-unpropagatable-p (cdr type))))
4920           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4921                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4922                  (add (gnus-remove-from-range
4923                        (gnus-copy-sequence list) old)))
4924             (when add
4925               (push (list add 'add (list (cdr type))) delta-marks))
4926             (when del
4927               (push (list del 'del (list (cdr type))) delta-marks))))
4928
4929         (when list
4930           (push (cons (cdr type) list) newmarked)))
4931
4932       (when delta-marks
4933         (unless (gnus-check-group gnus-newsgroup-name)
4934           (error "Can't open server for %s" gnus-newsgroup-name))
4935         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4936
4937       ;; Enter these new marks into the info of the group.
4938       (if (nthcdr 3 info)
4939           (setcar (nthcdr 3 info) newmarked)
4940         ;; Add the marks lists to the end of the info.
4941         (when newmarked
4942           (setcdr (nthcdr 2 info) (list newmarked))))
4943
4944       ;; Cut off the end of the info if there's nothing else there.
4945       (let ((i 5))
4946         (while (and (> i 2)
4947                     (not (nth i info)))
4948           (when (nthcdr (decf i) info)
4949             (setcdr (nthcdr i info) nil)))))))
4950
4951 (defun gnus-set-mode-line (where)
4952   "Set the mode line of the article or summary buffers.
4953 If WHERE is `summary', the summary mode line format will be used."
4954   ;; Is this mode line one we keep updated?
4955   (when (and (memq where gnus-updated-mode-lines)
4956              (symbol-value
4957               (intern (format "gnus-%s-mode-line-format-spec" where))))
4958     (let (mode-string)
4959       (save-excursion
4960         ;; We evaluate this in the summary buffer since these
4961         ;; variables are buffer-local to that buffer.
4962         (set-buffer gnus-summary-buffer)
4963        ;; We bind all these variables that are used in the `eval' form
4964         ;; below.
4965         (let* ((mformat (symbol-value
4966                          (intern
4967                           (format "gnus-%s-mode-line-format-spec" where))))
4968                (gnus-tmp-group-name (gnus-group-decoded-name
4969                                      gnus-newsgroup-name))
4970                (gnus-tmp-article-number (or gnus-current-article 0))
4971                (gnus-tmp-unread gnus-newsgroup-unreads)
4972                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4973                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4974                (gnus-tmp-unread-and-unselected
4975                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4976                             (zerop gnus-tmp-unselected))
4977                        "")
4978                       ((zerop gnus-tmp-unselected)
4979                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4980                       (t (format "{%d(+%d) more}"
4981                                  gnus-tmp-unread-and-unticked
4982                                  gnus-tmp-unselected))))
4983                (gnus-tmp-subject
4984                 (if (and gnus-current-headers
4985                          (vectorp gnus-current-headers))
4986                     (gnus-mode-string-quote
4987                      (mail-header-subject gnus-current-headers))
4988                   ""))
4989                bufname-length max-len
4990                gnus-tmp-header) ;; passed as argument to any user-format-funcs
4991           (setq mode-string (eval mformat))
4992           (setq bufname-length (if (string-match "%b" mode-string)
4993                                    (- (length
4994                                        (buffer-name
4995                                         (if (eq where 'summary)
4996                                             nil
4997                                           (get-buffer gnus-article-buffer))))
4998                                       2)
4999                                  0))
5000           (setq max-len (max 4 (if gnus-mode-non-string-length
5001                                    (- (window-width)
5002                                       gnus-mode-non-string-length
5003                                       bufname-length)
5004                                  (length mode-string))))
5005           ;; We might have to chop a bit of the string off...
5006           (when (> (length mode-string) max-len)
5007             (setq mode-string
5008                   (concat (truncate-string-to-width mode-string (- max-len 3))
5009                           "...")))
5010           ;; Pad the mode string a bit.
5011           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5012       ;; Update the mode line.
5013       (setq mode-line-buffer-identification
5014             (gnus-mode-line-buffer-identification (list mode-string)))
5015       (set-buffer-modified-p t))))
5016
5017 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5018   "Go through the HEADERS list and add all Xrefs to a hash table.
5019 The resulting hash table is returned, or nil if no Xrefs were found."
5020   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5021          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5022          (xref-hashtb (gnus-make-hashtable))
5023          start group entry number xrefs header)
5024     (while headers
5025       (setq header (pop headers))
5026       (when (and (setq xrefs (mail-header-xref header))
5027                  (not (memq (setq number (mail-header-number header))
5028                             unreads)))
5029         (setq start 0)
5030         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5031           (setq start (match-end 0))
5032           (setq group (if prefix
5033                           (concat prefix (substring xrefs (match-beginning 1)
5034                                                     (match-end 1)))
5035                         (substring xrefs (match-beginning 1) (match-end 1))))
5036           (setq number
5037                 (string-to-int (substring xrefs (match-beginning 2)
5038                                           (match-end 2))))
5039           (if (setq entry (gnus-gethash group xref-hashtb))
5040               (setcdr entry (cons number (cdr entry)))
5041             (gnus-sethash group (cons number nil) xref-hashtb)))))
5042     (and start xref-hashtb)))
5043
5044 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5045   "Look through all the headers and mark the Xrefs as read."
5046   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5047         name entry info xref-hashtb idlist method nth4)
5048     (save-excursion
5049       (set-buffer gnus-group-buffer)
5050       (when (setq xref-hashtb
5051                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5052         (mapatoms
5053          (lambda (group)
5054            (unless (string= from-newsgroup (setq name (symbol-name group)))
5055              (setq idlist (symbol-value group))
5056              ;; Dead groups are not updated.
5057              (and (prog1
5058                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5059                             info (nth 2 entry))
5060                     (when (stringp (setq nth4 (gnus-info-method info)))
5061                       (setq nth4 (gnus-server-to-method nth4))))
5062                   ;; Only do the xrefs if the group has the same
5063                   ;; select method as the group we have just read.
5064                   (or (gnus-methods-equal-p
5065                        nth4 (gnus-find-method-for-group from-newsgroup))
5066                       virtual
5067                       (equal nth4 (setq method (gnus-find-method-for-group
5068                                                 from-newsgroup)))
5069                       (and (equal (car nth4) (car method))
5070                            (equal (nth 1 nth4) (nth 1 method))))
5071                   gnus-use-cross-reference
5072                   (or (not (eq gnus-use-cross-reference t))
5073                       virtual
5074                       ;; Only do cross-references on subscribed
5075                       ;; groups, if that is what is wanted.
5076                       (<= (gnus-info-level info) gnus-level-subscribed))
5077                   (gnus-group-make-articles-read name idlist))))
5078          xref-hashtb)))))
5079
5080 (defun gnus-compute-read-articles (group articles)
5081   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5082          (info (nth 2 entry))
5083          (active (gnus-active group))
5084          ninfo)
5085     (when entry
5086       ;; First peel off all invalid article numbers.
5087       (when active
5088         (let ((ids articles)
5089               id first)
5090           (while (setq id (pop ids))
5091             (when (and first (> id (cdr active)))
5092               ;; We'll end up in this situation in one particular
5093               ;; obscure situation.  If you re-scan a group and get
5094               ;; a new article that is cross-posted to a different
5095               ;; group that has not been re-scanned, you might get
5096               ;; crossposted article that has a higher number than
5097               ;; Gnus believes possible.  So we re-activate this
5098               ;; group as well.  This might mean doing the
5099               ;; crossposting thingy will *increase* the number
5100               ;; of articles in some groups.  Tsk, tsk.
5101               (setq active (or (gnus-activate-group group) active)))
5102             (when (or (> id (cdr active))
5103                       (< id (car active)))
5104               (setq articles (delq id articles))))))
5105       ;; If the read list is nil, we init it.
5106       (if (and active
5107                (null (gnus-info-read info))
5108                (> (car active) 1))
5109           (setq ninfo (cons 1 (1- (car active))))
5110         (setq ninfo (gnus-info-read info)))
5111       ;; Then we add the read articles to the range.
5112       (gnus-add-to-range
5113        ninfo (setq articles (sort articles '<))))))
5114
5115 (defun gnus-group-make-articles-read (group articles)
5116   "Update the info of GROUP to say that ARTICLES are read."
5117   (let* ((num 0)
5118          (entry (gnus-gethash group gnus-newsrc-hashtb))
5119          (info (nth 2 entry))
5120          (active (gnus-active group))
5121          range)
5122     (when entry
5123       (setq range (gnus-compute-read-articles group articles))
5124       (save-excursion
5125         (set-buffer gnus-group-buffer)
5126         (gnus-undo-register
5127           `(progn
5128              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5129              (gnus-info-set-read ',info ',(gnus-info-read info))
5130              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5131              (gnus-request-set-mark group (list (list ',range 'del '(read))))
5132              (gnus-group-update-group ,group t))))
5133       ;; Add the read articles to the range.
5134       (gnus-info-set-read info range)
5135       (gnus-request-set-mark group (list (list range 'add '(read))))
5136       ;; Then we have to re-compute how many unread
5137       ;; articles there are in this group.
5138       (when active
5139         (cond
5140          ((not range)
5141           (setq num (- (1+ (cdr active)) (car active))))
5142          ((not (listp (cdr range)))
5143           (setq num (- (cdr active) (- (1+ (cdr range))
5144                                        (car range)))))
5145          (t
5146           (while range
5147             (if (numberp (car range))
5148                 (setq num (1+ num))
5149               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5150             (setq range (cdr range)))
5151           (setq num (- (cdr active) num))))
5152         ;; Update the number of unread articles.
5153         (setcar entry num)
5154         ;; Update the group buffer.
5155         (gnus-group-update-group group t)))))
5156
5157 (defvar gnus-newsgroup-none-id 0)
5158
5159 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5160   (let ((cur nntp-server-buffer)
5161         (dependencies
5162          (or dependencies
5163              (save-excursion (set-buffer gnus-summary-buffer)
5164                              gnus-newsgroup-dependencies)))
5165         headers id end ref
5166         (mail-parse-charset gnus-newsgroup-charset)
5167         (mail-parse-ignored-charsets
5168          (save-excursion (condition-case nil
5169                              (set-buffer gnus-summary-buffer)
5170                            (error))
5171                          gnus-newsgroup-ignored-charsets)))
5172     (save-excursion
5173       (set-buffer nntp-server-buffer)
5174       ;; Translate all TAB characters into SPACE characters.
5175       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5176       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5177       (gnus-run-hooks 'gnus-parse-headers-hook)
5178       (let ((case-fold-search t)
5179             in-reply-to header p lines chars)
5180         (goto-char (point-min))
5181         ;; Search to the beginning of the next header.  Error messages
5182         ;; do not begin with 2 or 3.
5183         (while (re-search-forward "^[23][0-9]+ " nil t)
5184           (setq id nil
5185                 ref nil)
5186           ;; This implementation of this function, with nine
5187           ;; search-forwards instead of the one re-search-forward and
5188           ;; a case (which basically was the old function) is actually
5189           ;; about twice as fast, even though it looks messier.  You
5190           ;; can't have everything, I guess.  Speed and elegance
5191           ;; doesn't always go hand in hand.
5192           (setq
5193            header
5194            (vector
5195             ;; Number.
5196             (prog1
5197                 (read cur)
5198               (end-of-line)
5199               (setq p (point))
5200               (narrow-to-region (point)
5201                                 (or (and (search-forward "\n.\n" nil t)
5202                                          (- (point) 2))
5203                                     (point))))
5204             ;; Subject.
5205             (progn
5206               (goto-char p)
5207               (if (search-forward "\nsubject:" nil t)
5208                   (funcall gnus-decode-encoded-word-function
5209                            (nnheader-header-value))
5210                 "(none)"))
5211             ;; From.
5212             (progn
5213               (goto-char p)
5214               (if (search-forward "\nfrom:" nil t)
5215                   (funcall gnus-decode-encoded-word-function
5216                            (nnheader-header-value))
5217                 "(nobody)"))
5218             ;; Date.
5219             (progn
5220               (goto-char p)
5221               (if (search-forward "\ndate:" nil t)
5222                   (nnheader-header-value) ""))
5223             ;; Message-ID.
5224             (progn
5225               (goto-char p)
5226               (setq id (if (re-search-forward
5227                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5228                       ;; We do it this way to make sure the Message-ID
5229                            ;; is (somewhat) syntactically valid.
5230                            (buffer-substring (match-beginning 1)
5231                                              (match-end 1))
5232                        ;; If there was no message-id, we just fake one
5233                          ;; to make subsequent routines simpler.
5234                          (nnheader-generate-fake-message-id))))
5235             ;; References.
5236             (progn
5237               (goto-char p)
5238               (if (search-forward "\nreferences:" nil t)
5239                   (progn
5240                     (setq end (point))
5241                     (prog1
5242                         (nnheader-header-value)
5243                       (setq ref
5244                             (buffer-substring
5245                              (progn
5246                                (end-of-line)
5247                                (search-backward ">" end t)
5248                                (1+ (point)))
5249                              (progn
5250                                (search-backward "<" end t)
5251                                (point))))))
5252             ;; Get the references from the in-reply-to header if there
5253                 ;; were no references and the in-reply-to header looks
5254                 ;; promising.
5255                 (if (and (search-forward "\nin-reply-to:" nil t)
5256                          (setq in-reply-to (nnheader-header-value))
5257                          (string-match "<[^>]+>" in-reply-to))
5258                     (let (ref2)
5259                       (setq ref (substring in-reply-to (match-beginning 0)
5260                                            (match-end 0)))
5261                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5262                         (setq ref2 (substring in-reply-to (match-beginning 0)
5263                                               (match-end 0)))
5264                         (when (> (length ref2) (length ref))
5265                           (setq ref ref2)))
5266                       ref)
5267                   (setq ref nil))))
5268             ;; Chars.
5269             (progn
5270               (goto-char p)
5271               (if (search-forward "\nchars: " nil t)
5272                   (if (numberp (setq chars (ignore-errors (read cur))))
5273                       chars -1)
5274                 -1))
5275             ;; Lines.
5276             (progn
5277               (goto-char p)
5278               (if (search-forward "\nlines: " nil t)
5279                   (if (numberp (setq lines (ignore-errors (read cur))))
5280                       lines -1)
5281                 -1))
5282             ;; Xref.
5283             (progn
5284               (goto-char p)
5285               (and (search-forward "\nxref:" nil t)
5286                    (nnheader-header-value)))
5287             ;; Extra.
5288             (when gnus-extra-headers
5289               (let ((extra gnus-extra-headers)
5290                     out)
5291                 (while extra
5292                   (goto-char p)
5293                   (when (search-forward
5294                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5295                     (push (cons (car extra) (nnheader-header-value))
5296                           out))
5297                   (pop extra))
5298                 out))))
5299           (when (equal id ref)
5300             (setq ref nil))
5301
5302           (when gnus-alter-header-function
5303             (funcall gnus-alter-header-function header)
5304             (setq id (mail-header-id header)
5305                   ref (gnus-parent-id (mail-header-references header))))
5306
5307           (when (setq header
5308                       (gnus-dependencies-add-header
5309                        header dependencies force-new))
5310             (push header headers))
5311           (goto-char (point-max))
5312           (widen))
5313         (nreverse headers)))))
5314
5315 ;; Goes through the xover lines and returns a list of vectors
5316 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5317                                                   force-new dependencies
5318                                                   group also-fetch-heads)
5319   "Parse the news overview data in the server buffer.
5320 Return a list of headers that match SEQUENCE (see
5321 `nntp-retrieve-headers')."
5322   ;; Get the Xref when the users reads the articles since most/some
5323   ;; NNTP servers do not include Xrefs when using XOVER.
5324   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5325   (let ((mail-parse-charset gnus-newsgroup-charset)
5326         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5327         (cur nntp-server-buffer)
5328         (dependencies (or dependencies gnus-newsgroup-dependencies))
5329         (allp (cond
5330                ((eq gnus-read-all-available-headers t)
5331                 t)
5332                ((stringp gnus-read-all-available-headers)
5333                 (string-match gnus-read-all-available-headers group))
5334                (t
5335                 nil)))
5336         number headers header)
5337     (save-excursion
5338       (set-buffer nntp-server-buffer)
5339       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5340       ;; Allow the user to mangle the headers before parsing them.
5341       (gnus-run-hooks 'gnus-parse-headers-hook)
5342       (goto-char (point-min))
5343       (while (not (eobp))
5344         (condition-case ()
5345             (while (and (or sequence allp)
5346                         (not (eobp)))
5347               (setq number (read cur))
5348               (when (not allp)
5349                 (while (and sequence
5350                             (< (car sequence) number))
5351                   (setq sequence (cdr sequence))))
5352               (when (and (or allp
5353                              (and sequence
5354                                   (eq number (car sequence))))
5355                          (progn
5356                            (setq sequence (cdr sequence))
5357                            (setq header (inline
5358                                           (gnus-nov-parse-line
5359                                            number dependencies force-new)))))
5360                 (push header headers))
5361               (forward-line 1))
5362           (error
5363            (gnus-error 4 "Strange nov line (%d)"
5364                        (count-lines (point-min) (point)))))
5365         (forward-line 1))
5366       ;; A common bug in inn is that if you have posted an article and
5367       ;; then retrieves the active file, it will answer correctly --
5368       ;; the new article is included.  However, a NOV entry for the
5369       ;; article may not have been generated yet, so this may fail.
5370       ;; We work around this problem by retrieving the last few
5371       ;; headers using HEAD.
5372       (if (or (not also-fetch-heads)
5373               (not sequence))
5374           ;; We (probably) got all the headers.
5375           (nreverse headers)
5376         (let ((gnus-nov-is-evil t))
5377           (nconc
5378            (nreverse headers)
5379            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5380              (gnus-get-newsgroup-headers))))))))
5381
5382 (defun gnus-article-get-xrefs ()
5383   "Fill in the Xref value in `gnus-current-headers', if necessary.
5384 This is meant to be called in `gnus-article-internal-prepare-hook'."
5385   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5386                                  gnus-current-headers)))
5387     (or (not gnus-use-cross-reference)
5388         (not headers)
5389         (and (mail-header-xref headers)
5390              (not (string= (mail-header-xref headers) "")))
5391         (let ((case-fold-search t)
5392               xref)
5393           (save-restriction
5394             (nnheader-narrow-to-headers)
5395             (goto-char (point-min))
5396             (when (or (and (not (eobp))
5397                            (eq (downcase (char-after)) ?x)
5398                            (looking-at "Xref:"))
5399                       (search-forward "\nXref:" nil t))
5400               (goto-char (1+ (match-end 0)))
5401               (setq xref (buffer-substring (point)
5402                                            (progn (end-of-line) (point))))
5403               (mail-header-set-xref headers xref)))))))
5404
5405 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5406   "Find article ID and insert the summary line for that article.
5407 OLD-HEADER can either be a header or a line number to insert
5408 the subject line on."
5409   (let* ((line (and (numberp old-header) old-header))
5410          (old-header (and (vectorp old-header) old-header))
5411          (header (cond ((and old-header use-old-header)
5412                         old-header)
5413                        ((and (numberp id)
5414                              (gnus-number-to-header id))
5415                         (gnus-number-to-header id))
5416                        (t
5417                         (gnus-read-header id))))
5418          (number (and (numberp id) id))
5419          d)
5420     (when header
5421       ;; Rebuild the thread that this article is part of and go to the
5422       ;; article we have fetched.
5423       (when (and (not gnus-show-threads)
5424                  old-header)
5425         (when (and number
5426                    (setq d (gnus-data-find (mail-header-number old-header))))
5427           (goto-char (gnus-data-pos d))
5428           (gnus-data-remove
5429            number
5430            (- (gnus-point-at-bol)
5431               (prog1
5432                   (1+ (gnus-point-at-eol))
5433                 (gnus-delete-line))))))
5434       (when old-header
5435         (mail-header-set-number header (mail-header-number old-header)))
5436       (setq gnus-newsgroup-sparse
5437             (delq (setq number (mail-header-number header))
5438                   gnus-newsgroup-sparse))
5439       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5440       (push number gnus-newsgroup-limit)
5441       (gnus-rebuild-thread (mail-header-id header) line)
5442       (gnus-summary-goto-subject number nil t))
5443     (when (and (numberp number)
5444                (> number 0))
5445       ;; We have to update the boundaries even if we can't fetch the
5446       ;; article if ID is a number -- so that the next `P' or `N'
5447       ;; command will fetch the previous (or next) article even
5448       ;; if the one we tried to fetch this time has been canceled.
5449       (when (> number gnus-newsgroup-end)
5450         (setq gnus-newsgroup-end number))
5451       (when (< number gnus-newsgroup-begin)
5452         (setq gnus-newsgroup-begin number))
5453       (setq gnus-newsgroup-unselected
5454             (delq number gnus-newsgroup-unselected)))
5455     ;; Report back a success?
5456     (and header (mail-header-number header))))
5457
5458 ;;; Process/prefix in the summary buffer
5459
5460 (defun gnus-summary-work-articles (n)
5461   "Return a list of articles to be worked upon.
5462 The prefix argument, the list of process marked articles, and the
5463 current article will be taken into consideration."
5464   (save-excursion
5465     (set-buffer gnus-summary-buffer)
5466     (cond
5467      (n
5468       ;; A numerical prefix has been given.
5469       (setq n (prefix-numeric-value n))
5470       (let ((backward (< n 0))
5471             (n (abs (prefix-numeric-value n)))
5472             articles article)
5473         (save-excursion
5474           (while
5475               (and (> n 0)
5476                    (push (setq article (gnus-summary-article-number))
5477                          articles)
5478                    (if backward
5479                        (gnus-summary-find-prev nil article)
5480                      (gnus-summary-find-next nil article)))
5481             (decf n)))
5482         (nreverse articles)))
5483      ((and (gnus-region-active-p) (mark))
5484       (message "region active")
5485       ;; Work on the region between point and mark.
5486       (let ((max (max (point) (mark)))
5487             articles article)
5488         (save-excursion
5489           (goto-char (min (min (point) (mark))))
5490           (while
5491               (and
5492                (push (setq article (gnus-summary-article-number)) articles)
5493                (gnus-summary-find-next nil article)
5494                (< (point) max)))
5495           (nreverse articles))))
5496      (gnus-newsgroup-processable
5497       ;; There are process-marked articles present.
5498       ;; Save current state.
5499       (gnus-summary-save-process-mark)
5500       ;; Return the list.
5501       (reverse gnus-newsgroup-processable))
5502      (t
5503       ;; Just return the current article.
5504       (list (gnus-summary-article-number))))))
5505
5506 (defmacro gnus-summary-iterate (arg &rest forms)
5507   "Iterate over the process/prefixed articles and do FORMS.
5508 ARG is the interactive prefix given to the command.  FORMS will be
5509 executed with point over the summary line of the articles."
5510   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5511     `(let ((,articles (gnus-summary-work-articles ,arg)))
5512        (while ,articles
5513          (gnus-summary-goto-subject (car ,articles))
5514          ,@forms
5515          (pop ,articles)))))
5516
5517 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5518 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5519
5520 (defun gnus-summary-save-process-mark ()
5521   "Push the current set of process marked articles on the stack."
5522   (interactive)
5523   (push (copy-sequence gnus-newsgroup-processable)
5524         gnus-newsgroup-process-stack))
5525
5526 (defun gnus-summary-kill-process-mark ()
5527   "Push the current set of process marked articles on the stack and unmark."
5528   (interactive)
5529   (gnus-summary-save-process-mark)
5530   (gnus-summary-unmark-all-processable))
5531
5532 (defun gnus-summary-yank-process-mark ()
5533   "Pop the last process mark state off the stack and restore it."
5534   (interactive)
5535   (unless gnus-newsgroup-process-stack
5536     (error "Empty mark stack"))
5537   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5538
5539 (defun gnus-summary-process-mark-set (set)
5540   "Make SET into the current process marked articles."
5541   (gnus-summary-unmark-all-processable)
5542   (while set
5543     (gnus-summary-set-process-mark (pop set))))
5544
5545 ;;; Searching and stuff
5546
5547 (defun gnus-summary-search-group (&optional backward use-level)
5548   "Search for next unread newsgroup.
5549 If optional argument BACKWARD is non-nil, search backward instead."
5550   (save-excursion
5551     (set-buffer gnus-group-buffer)
5552     (when (gnus-group-search-forward
5553            backward nil (if use-level (gnus-group-group-level) nil))
5554       (gnus-group-group-name))))
5555
5556 (defun gnus-summary-best-group (&optional exclude-group)
5557   "Find the name of the best unread group.
5558 If EXCLUDE-GROUP, do not go to this group."
5559   (save-excursion
5560     (set-buffer gnus-group-buffer)
5561     (save-excursion
5562       (gnus-group-best-unread-group exclude-group))))
5563
5564 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5565   (if backward (gnus-summary-find-prev)
5566     (let* ((dummy (gnus-summary-article-intangible-p))
5567            (article (or article (gnus-summary-article-number)))
5568            (arts (gnus-data-find-list article))
5569            result)
5570       (when (and (not dummy)
5571                  (or (not gnus-summary-check-current)
5572                      (not unread)
5573                      (not (gnus-data-unread-p (car arts)))))
5574         (setq arts (cdr arts)))
5575       (when (setq result
5576                   (if unread
5577                       (progn
5578                         (while arts
5579                           (when (or (and undownloaded
5580                                          (eq gnus-undownloaded-mark
5581                                              (gnus-data-mark (car arts))))
5582                                     (gnus-data-unread-p (car arts)))
5583                             (setq result (car arts)
5584                                   arts nil))
5585                           (setq arts (cdr arts)))
5586                         result)
5587                     (car arts)))
5588         (goto-char (gnus-data-pos result))
5589         (gnus-data-number result)))))
5590
5591 (defun gnus-summary-find-prev (&optional unread article)
5592   (let* ((eobp (eobp))
5593          (article (or article (gnus-summary-article-number)))
5594          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5595          result)
5596     (when (and (not eobp)
5597                (or (not gnus-summary-check-current)
5598                    (not unread)
5599                    (not (gnus-data-unread-p (car arts)))))
5600       (setq arts (cdr arts)))
5601     (when (setq result
5602                 (if unread
5603                     (progn
5604                       (while arts
5605                         (when (gnus-data-unread-p (car arts))
5606                           (setq result (car arts)
5607                                 arts nil))
5608                         (setq arts (cdr arts)))
5609                       result)
5610                   (car arts)))
5611       (goto-char (gnus-data-pos result))
5612       (gnus-data-number result))))
5613
5614 (defun gnus-summary-find-subject (subject &optional unread backward article)
5615   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5616          (article (or article (gnus-summary-article-number)))
5617          (articles (gnus-data-list backward))
5618          (arts (gnus-data-find-list article articles))
5619          result)
5620     (when (or (not gnus-summary-check-current)
5621               (not unread)
5622               (not (gnus-data-unread-p (car arts))))
5623       (setq arts (cdr arts)))
5624     (while arts
5625       (and (or (not unread)
5626                (gnus-data-unread-p (car arts)))
5627            (vectorp (gnus-data-header (car arts)))
5628            (gnus-subject-equal
5629             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5630            (setq result (car arts)
5631                  arts nil))
5632       (setq arts (cdr arts)))
5633     (and result
5634          (goto-char (gnus-data-pos result))
5635          (gnus-data-number result))))
5636
5637 (defun gnus-summary-search-forward (&optional unread subject backward)
5638   "Search forward for an article.
5639 If UNREAD, look for unread articles.  If SUBJECT, look for
5640 articles with that subject.  If BACKWARD, search backward instead."
5641   (cond (subject (gnus-summary-find-subject subject unread backward))
5642         (backward (gnus-summary-find-prev unread))
5643         (t (gnus-summary-find-next unread))))
5644
5645 (defun gnus-recenter (&optional n)
5646   "Center point in window and redisplay frame.
5647 Also do horizontal recentering."
5648   (interactive "P")
5649   (when (and gnus-auto-center-summary
5650              (not (eq gnus-auto-center-summary 'vertical)))
5651     (gnus-horizontal-recenter))
5652   (recenter n))
5653
5654 (defun gnus-summary-recenter ()
5655   "Center point in the summary window.
5656 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5657 displayed, no centering will be performed."
5658   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5659 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5660   (interactive)
5661   (let* ((top (cond ((< (window-height) 4) 0)
5662                     ((< (window-height) 7) 1)
5663                     (t (if (numberp gnus-auto-center-summary)
5664                            gnus-auto-center-summary
5665                          2))))
5666          (height (1- (window-height)))
5667          (bottom (save-excursion (goto-char (point-max))
5668                                  (forward-line (- height))
5669                                  (point)))
5670          (window (get-buffer-window (current-buffer))))
5671     ;; The user has to want it.
5672     (when gnus-auto-center-summary
5673       (when (get-buffer-window gnus-article-buffer)
5674         ;; Only do recentering when the article buffer is displayed,
5675       ;; Set the window start to either `bottom', which is the biggest
5676         ;; possible valid number, or the second line from the top,
5677         ;; whichever is the least.
5678         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5679           (if (> bottom top-pos)
5680               ;; Keep the second line from the top visible
5681               (set-window-start window top-pos t)
5682             ;; Try to keep the bottom line visible; if it's partially
5683             ;; obscured, either scroll one more line to make it fully
5684             ;; visible, or revert to using TOP-POS.
5685             (save-excursion
5686               (goto-char (point-max))
5687               (forward-line -1)
5688               (let ((last-line-start (point)))
5689                 (goto-char bottom)
5690                 (set-window-start window (point) t)
5691                 (when (not (pos-visible-in-window-p last-line-start window))
5692                   (forward-line 1)
5693                   (set-window-start window (min (point) top-pos) t)))))))
5694       ;; Do horizontal recentering while we're at it.
5695       (when (and (get-buffer-window (current-buffer) t)
5696                  (not (eq gnus-auto-center-summary 'vertical)))
5697         (let ((selected (selected-window)))
5698           (select-window (get-buffer-window (current-buffer) t))
5699           (gnus-summary-position-point)
5700           (gnus-horizontal-recenter)
5701           (select-window selected))))))
5702
5703 (defun gnus-summary-jump-to-group (newsgroup)
5704   "Move point to NEWSGROUP in group mode buffer."
5705   ;; Keep update point of group mode buffer if visible.
5706   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5707       (save-window-excursion
5708         ;; Take care of tree window mode.
5709         (when (get-buffer-window gnus-group-buffer)
5710           (pop-to-buffer gnus-group-buffer))
5711         (gnus-group-jump-to-group newsgroup))
5712     (save-excursion
5713       ;; Take care of tree window mode.
5714       (if (get-buffer-window gnus-group-buffer)
5715           (pop-to-buffer gnus-group-buffer)
5716         (set-buffer gnus-group-buffer))
5717       (gnus-group-jump-to-group newsgroup))))
5718
5719 ;; This function returns a list of article numbers based on the
5720 ;; difference between the ranges of read articles in this group and
5721 ;; the range of active articles.
5722 (defun gnus-list-of-unread-articles (group)
5723   (let* ((read (gnus-info-read (gnus-get-info group)))
5724          (active (or (gnus-active group) (gnus-activate-group group)))
5725          (last (cdr active))
5726          first nlast unread)
5727     ;; If none are read, then all are unread.
5728     (if (not read)
5729         (setq first (car active))
5730       ;; If the range of read articles is a single range, then the
5731       ;; first unread article is the article after the last read
5732       ;; article.  Sounds logical, doesn't it?
5733       (if (and (not (listp (cdr read)))
5734                (or (< (car read) (car active))
5735                    (progn (setq read (list read))
5736                           nil)))
5737           (setq first (max (car active) (1+ (cdr read))))
5738         ;; `read' is a list of ranges.
5739         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5740                                   (caar read)))
5741                   1)
5742           (setq first (car active)))
5743         (while read
5744           (when first
5745             (while (< first nlast)
5746               (push first unread)
5747               (setq first (1+ first))))
5748           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5749           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5750           (setq read (cdr read)))))
5751     ;; And add the last unread articles.
5752     (while (<= first last)
5753       (push first unread)
5754       (setq first (1+ first)))
5755     ;; Return the list of unread articles.
5756     (delq 0 (nreverse unread))))
5757
5758 (defun gnus-list-of-read-articles (group)
5759   "Return a list of unread, unticked and non-dormant articles."
5760   (let* ((info (gnus-get-info group))
5761          (marked (gnus-info-marks info))
5762          (active (gnus-active group)))
5763     (and info active
5764          (gnus-set-difference
5765           (gnus-sorted-complement
5766            (gnus-uncompress-range active)
5767            (gnus-list-of-unread-articles group))
5768           (append
5769            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5770            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5771
5772 ;; Various summary commands
5773
5774 (defun gnus-summary-select-article-buffer ()
5775   "Reconfigure windows to show article buffer."
5776   (interactive)
5777   (if (not (gnus-buffer-live-p gnus-article-buffer))
5778       (error "There is no article buffer for this summary buffer")
5779     (gnus-configure-windows 'article)
5780     (select-window (get-buffer-window gnus-article-buffer))))
5781
5782 (defun gnus-summary-universal-argument (arg)
5783   "Perform any operation on all articles that are process/prefixed."
5784   (interactive "P")
5785   (let ((articles (gnus-summary-work-articles arg))
5786         func article)
5787     (if (eq
5788          (setq
5789           func
5790           (key-binding
5791            (read-key-sequence
5792             (substitute-command-keys
5793              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5794          'undefined)
5795         (gnus-error 1 "Undefined key")
5796       (save-excursion
5797         (while articles
5798           (gnus-summary-goto-subject (setq article (pop articles)))
5799           (let (gnus-newsgroup-processable)
5800             (command-execute func))
5801           (gnus-summary-remove-process-mark article)))))
5802   (gnus-summary-position-point))
5803
5804 (defun gnus-summary-toggle-truncation (&optional arg)
5805   "Toggle truncation of summary lines.
5806 With arg, turn line truncation on iff arg is positive."
5807   (interactive "P")
5808   (setq truncate-lines
5809         (if (null arg) (not truncate-lines)
5810           (> (prefix-numeric-value arg) 0)))
5811   (redraw-display))
5812
5813 (defun gnus-summary-reselect-current-group (&optional all rescan)
5814   "Exit and then reselect the current newsgroup.
5815 The prefix argument ALL means to select all articles."
5816   (interactive "P")
5817   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5818     (error "Ephemeral groups can't be reselected"))
5819   (let ((current-subject (gnus-summary-article-number))
5820         (group gnus-newsgroup-name))
5821     (setq gnus-newsgroup-begin nil)
5822     (gnus-summary-exit)
5823     ;; We have to adjust the point of group mode buffer because
5824     ;; point was moved to the next unread newsgroup by exiting.
5825     (gnus-summary-jump-to-group group)
5826     (when rescan
5827       (save-excursion
5828         (gnus-group-get-new-news-this-group 1)))
5829     (gnus-group-read-group all t)
5830     (gnus-summary-goto-subject current-subject nil t)))
5831
5832 (defun gnus-summary-rescan-group (&optional all)
5833   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5834   (interactive "P")
5835   (gnus-summary-reselect-current-group all t))
5836
5837 (defun gnus-summary-update-info (&optional non-destructive)
5838   (save-excursion
5839     (let ((group gnus-newsgroup-name))
5840       (when group
5841         (when gnus-newsgroup-kill-headers
5842           (setq gnus-newsgroup-killed
5843                 (gnus-compress-sequence
5844                  (nconc
5845                   (gnus-set-sorted-intersection
5846                    (gnus-uncompress-range gnus-newsgroup-killed)
5847                    (setq gnus-newsgroup-unselected
5848                          (sort gnus-newsgroup-unselected '<)))
5849                   (setq gnus-newsgroup-unreads
5850                         (sort gnus-newsgroup-unreads '<)))
5851                  t)))
5852         (unless (listp (cdr gnus-newsgroup-killed))
5853           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5854         (let ((headers gnus-newsgroup-headers))
5855           ;; Set the new ranges of read articles.
5856           (save-excursion
5857             (set-buffer gnus-group-buffer)
5858             (gnus-undo-force-boundary))
5859           (gnus-update-read-articles
5860            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5861           ;; Set the current article marks.
5862           (let ((gnus-newsgroup-scored
5863                  (if (and (not gnus-save-score)
5864                           (not non-destructive))
5865                      nil
5866                    gnus-newsgroup-scored)))
5867             (save-excursion
5868               (gnus-update-marks)))
5869           ;; Do the cross-ref thing.
5870           (when gnus-use-cross-reference
5871             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5872           ;; Do not switch windows but change the buffer to work.
5873           (set-buffer gnus-group-buffer)
5874           (unless (gnus-ephemeral-group-p group)
5875             (gnus-group-update-group group)))))))
5876
5877 (defun gnus-summary-save-newsrc (&optional force)
5878   "Save the current number of read/marked articles in the dribble buffer.
5879 The dribble buffer will then be saved.
5880 If FORCE (the prefix), also save the .newsrc file(s)."
5881   (interactive "P")
5882   (gnus-summary-update-info t)
5883   (if force
5884       (gnus-save-newsrc-file)
5885     (gnus-dribble-save)))
5886
5887 (defun gnus-summary-exit (&optional temporary)
5888   "Exit reading current newsgroup, and then return to group selection mode.
5889 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5890   (interactive)
5891   (gnus-set-global-variables)
5892   (when (gnus-buffer-live-p gnus-article-buffer)
5893     (save-excursion
5894       (set-buffer gnus-article-buffer)
5895       (mm-destroy-parts gnus-article-mime-handles)
5896       ;; Set it to nil for safety reason.
5897       (setq gnus-article-mime-handle-alist nil)
5898       (setq gnus-article-mime-handles nil)))
5899   (gnus-kill-save-kill-buffer)
5900   (gnus-async-halt-prefetch)
5901   (let* ((group gnus-newsgroup-name)
5902          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5903          (mode major-mode)
5904          (group-point nil)
5905          (buf (current-buffer)))
5906     (unless quit-config
5907       ;; Do adaptive scoring, and possibly save score files.
5908       (when gnus-newsgroup-adaptive
5909         (gnus-score-adaptive))
5910       (when gnus-use-scoring
5911         (gnus-score-save)))
5912     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5913     ;; If we have several article buffers, we kill them at exit.
5914     (unless gnus-single-article-buffer
5915       (gnus-kill-buffer gnus-original-article-buffer)
5916       (setq gnus-article-current nil))
5917     (when gnus-use-cache
5918       (gnus-cache-possibly-remove-articles)
5919       (gnus-cache-save-buffers))
5920     (gnus-async-prefetch-remove-group group)
5921     (when gnus-suppress-duplicates
5922       (gnus-dup-enter-articles))
5923     (when gnus-use-trees
5924       (gnus-tree-close group))
5925     (when gnus-use-cache
5926       (gnus-cache-write-active))
5927     ;; Remove entries for this group.
5928     (nnmail-purge-split-history (gnus-group-real-name group))
5929     ;; Make all changes in this group permanent.
5930     (unless quit-config
5931       (gnus-run-hooks 'gnus-exit-group-hook)
5932       (gnus-summary-update-info))
5933     (gnus-close-group group)
5934     ;; Make sure where we were, and go to next newsgroup.
5935     (set-buffer gnus-group-buffer)
5936     (unless quit-config
5937       (gnus-group-jump-to-group group))
5938     (gnus-run-hooks 'gnus-summary-exit-hook)
5939     (unless (or quit-config
5940                 ;; If this group has disappeared from the summary
5941                 ;; buffer, don't skip forwards.
5942                 (not (string= group (gnus-group-group-name))))
5943       (gnus-group-next-unread-group 1))
5944     (setq group-point (point))
5945     (if temporary
5946         nil                             ;Nothing to do.
5947       ;; If we have several article buffers, we kill them at exit.
5948       (unless gnus-single-article-buffer
5949         (gnus-kill-buffer gnus-article-buffer)
5950         (gnus-kill-buffer gnus-original-article-buffer)
5951         (setq gnus-article-current nil))
5952       (set-buffer buf)
5953       (if (not gnus-kill-summary-on-exit)
5954           (progn
5955             (gnus-deaden-summary)
5956             (setq mode nil))
5957        ;; We set all buffer-local variables to nil.  It is unclear why
5958         ;; this is needed, but if we don't, buffer-local variables are
5959         ;; not garbage-collected, it seems.  This would the lead to en
5960         ;; ever-growing Emacs.
5961         (gnus-summary-clear-local-variables)
5962         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5963           (gnus-summary-clear-local-variables))
5964         (when (get-buffer gnus-article-buffer)
5965           (bury-buffer gnus-article-buffer))
5966         ;; We clear the global counterparts of the buffer-local
5967         ;; variables as well, just to be on the safe side.
5968         (set-buffer gnus-group-buffer)
5969         (gnus-summary-clear-local-variables)
5970         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5971           (gnus-summary-clear-local-variables)))
5972       (setq gnus-current-select-method gnus-select-method)
5973       (pop-to-buffer gnus-group-buffer)
5974       (if (not quit-config)
5975           (progn
5976             (goto-char group-point)
5977             (gnus-configure-windows 'group 'force))
5978         (gnus-handle-ephemeral-exit quit-config))
5979       ;; Return to group mode buffer.
5980       (when (eq mode 'gnus-summary-mode)
5981         (gnus-kill-buffer buf))
5982       ;; Clear the current group name.
5983       (unless quit-config
5984         (setq gnus-newsgroup-name nil)))))
5985
5986 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5987 (defun gnus-summary-exit-no-update (&optional no-questions)
5988   "Quit reading current newsgroup without updating read article info."
5989   (interactive)
5990   (let* ((group gnus-newsgroup-name)
5991          (quit-config (gnus-group-quit-config group)))
5992     (when (or no-questions
5993               gnus-expert-user
5994               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5995       (gnus-async-halt-prefetch)
5996       (mapcar 'funcall
5997               (delq 'gnus-summary-expire-articles
5998                     (copy-sequence gnus-summary-prepare-exit-hook)))
5999       (when (gnus-buffer-live-p gnus-article-buffer)
6000         (save-excursion
6001           (set-buffer gnus-article-buffer)
6002           (mm-destroy-parts gnus-article-mime-handles)
6003           ;; Set it to nil for safety reason.
6004           (setq gnus-article-mime-handle-alist nil)
6005           (setq gnus-article-mime-handles nil)))
6006       ;; If we have several article buffers, we kill them at exit.
6007       (unless gnus-single-article-buffer
6008         (gnus-kill-buffer gnus-article-buffer)
6009         (gnus-kill-buffer gnus-original-article-buffer)
6010         (setq gnus-article-current nil))
6011       (if (not gnus-kill-summary-on-exit)
6012           (gnus-deaden-summary)
6013         (gnus-close-group group)
6014         (gnus-summary-clear-local-variables)
6015         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6016           (gnus-summary-clear-local-variables))
6017         (set-buffer gnus-group-buffer)
6018         (gnus-summary-clear-local-variables)
6019         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6020           (gnus-summary-clear-local-variables))
6021         (when (get-buffer gnus-summary-buffer)
6022           (kill-buffer gnus-summary-buffer)))
6023       (unless gnus-single-article-buffer
6024         (setq gnus-article-current nil))
6025       (when gnus-use-trees
6026         (gnus-tree-close group))
6027       (gnus-async-prefetch-remove-group group)
6028       (when (get-buffer gnus-article-buffer)
6029         (bury-buffer gnus-article-buffer))
6030       ;; Return to the group buffer.
6031       (gnus-configure-windows 'group 'force)
6032       ;; Clear the current group name.
6033       (setq gnus-newsgroup-name nil)
6034       (when (equal (gnus-group-group-name) group)
6035         (gnus-group-next-unread-group 1))
6036       (when quit-config
6037         (gnus-handle-ephemeral-exit quit-config)))))
6038
6039 (defun gnus-handle-ephemeral-exit (quit-config)
6040   "Handle movement when leaving an ephemeral group.
6041 The state which existed when entering the ephemeral is reset."
6042   (if (not (buffer-name (car quit-config)))
6043       (gnus-configure-windows 'group 'force)
6044     (set-buffer (car quit-config))
6045     (cond ((eq major-mode 'gnus-summary-mode)
6046            (gnus-set-global-variables))
6047           ((eq major-mode 'gnus-article-mode)
6048            (save-excursion
6049              ;; The `gnus-summary-buffer' variable may point
6050              ;; to the old summary buffer when using a single
6051              ;; article buffer.
6052              (unless (gnus-buffer-live-p gnus-summary-buffer)
6053                (set-buffer gnus-group-buffer))
6054              (set-buffer gnus-summary-buffer)
6055              (gnus-set-global-variables))))
6056     (if (or (eq (cdr quit-config) 'article)
6057             (eq (cdr quit-config) 'pick))
6058         (progn
6059           ;; The current article may be from the ephemeral group
6060           ;; thus it is best that we reload this article
6061           (gnus-summary-show-article)
6062           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6063               (gnus-configure-windows 'pick 'force)
6064             (gnus-configure-windows (cdr quit-config) 'force)))
6065       (gnus-configure-windows (cdr quit-config) 'force))
6066     (when (eq major-mode 'gnus-summary-mode)
6067       (gnus-summary-next-subject 1 nil t)
6068       (gnus-summary-recenter)
6069       (gnus-summary-position-point))))
6070
6071 ;;; Dead summaries.
6072
6073 (defvar gnus-dead-summary-mode-map nil)
6074
6075 (unless gnus-dead-summary-mode-map
6076   (setq gnus-dead-summary-mode-map (make-keymap))
6077   (suppress-keymap gnus-dead-summary-mode-map)
6078   (substitute-key-definition
6079    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6080   (let ((keys '("\C-d" "\r" "\177" [delete])))
6081     (while keys
6082       (define-key gnus-dead-summary-mode-map
6083         (pop keys) 'gnus-summary-wake-up-the-dead))))
6084
6085 (defvar gnus-dead-summary-mode nil
6086   "Minor mode for Gnus summary buffers.")
6087
6088 (defun gnus-dead-summary-mode (&optional arg)
6089   "Minor mode for Gnus summary buffers."
6090   (interactive "P")
6091   (when (eq major-mode 'gnus-summary-mode)
6092     (make-local-variable 'gnus-dead-summary-mode)
6093     (setq gnus-dead-summary-mode
6094           (if (null arg) (not gnus-dead-summary-mode)
6095             (> (prefix-numeric-value arg) 0)))
6096     (when gnus-dead-summary-mode
6097       (gnus-add-minor-mode
6098        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6099
6100 (defun gnus-deaden-summary ()
6101   "Make the current summary buffer into a dead summary buffer."
6102   ;; Kill any previous dead summary buffer.
6103   (when (and gnus-dead-summary
6104              (buffer-name gnus-dead-summary))
6105     (save-excursion
6106       (set-buffer gnus-dead-summary)
6107       (when gnus-dead-summary-mode
6108         (kill-buffer (current-buffer)))))
6109   ;; Make this the current dead summary.
6110   (setq gnus-dead-summary (current-buffer))
6111   (gnus-dead-summary-mode 1)
6112   (let ((name (buffer-name)))
6113     (when (string-match "Summary" name)
6114       (rename-buffer
6115        (concat (substring name 0 (match-beginning 0)) "Dead "
6116                (substring name (match-beginning 0)))
6117        t)
6118       (bury-buffer))))
6119
6120 (defun gnus-kill-or-deaden-summary (buffer)
6121   "Kill or deaden the summary BUFFER."
6122   (save-excursion
6123     (when (and (buffer-name buffer)
6124                (not gnus-single-article-buffer))
6125       (save-excursion
6126         (set-buffer buffer)
6127         (gnus-kill-buffer gnus-article-buffer)
6128         (gnus-kill-buffer gnus-original-article-buffer)))
6129     (cond (gnus-kill-summary-on-exit
6130            (when (and gnus-use-trees
6131                       (gnus-buffer-exists-p buffer))
6132              (save-excursion
6133                (set-buffer buffer)
6134                (gnus-tree-close gnus-newsgroup-name)))
6135            (gnus-kill-buffer buffer))
6136           ((gnus-buffer-exists-p buffer)
6137            (save-excursion
6138              (set-buffer buffer)
6139              (gnus-deaden-summary))))))
6140
6141 (defun gnus-summary-wake-up-the-dead (&rest args)
6142   "Wake up the dead summary buffer."
6143   (interactive)
6144   (gnus-dead-summary-mode -1)
6145   (let ((name (buffer-name)))
6146     (when (string-match "Dead " name)
6147       (rename-buffer
6148        (concat (substring name 0 (match-beginning 0))
6149                (substring name (match-end 0)))
6150        t)))
6151   (gnus-message 3 "This dead summary is now alive again"))
6152
6153 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6154 (defun gnus-summary-fetch-faq (&optional faq-dir)
6155   "Fetch the FAQ for the current group.
6156 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6157 in."
6158   (interactive
6159    (list
6160     (when current-prefix-arg
6161       (completing-read
6162        "Faq dir: " (and (listp gnus-group-faq-directory)
6163                         (mapcar (lambda (file) (list file))
6164                                 gnus-group-faq-directory))))))
6165   (let (gnus-faq-buffer)
6166     (when (setq gnus-faq-buffer
6167                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6168       (gnus-configure-windows 'summary-faq))))
6169
6170 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6171 (defun gnus-summary-describe-group (&optional force)
6172   "Describe the current newsgroup."
6173   (interactive "P")
6174   (gnus-group-describe-group force gnus-newsgroup-name))
6175
6176 (defun gnus-summary-describe-briefly ()
6177   "Describe summary mode commands briefly."
6178   (interactive)
6179   (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")))
6180
6181 ;; Walking around group mode buffer from summary mode.
6182
6183 (defun gnus-summary-next-group (&optional no-article target-group backward)
6184   "Exit current newsgroup and then select next unread newsgroup.
6185 If prefix argument NO-ARTICLE is non-nil, no article is selected
6186 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6187 previous group instead."
6188   (interactive "P")
6189   ;; Stop pre-fetching.
6190   (gnus-async-halt-prefetch)
6191   (let ((current-group gnus-newsgroup-name)
6192         (current-buffer (current-buffer))
6193         entered)
6194    ;; First we semi-exit this group to update Xrefs and all variables.
6195     ;; We can't do a real exit, because the window conf must remain
6196     ;; the same in case the user is prompted for info, and we don't
6197     ;; want the window conf to change before that...
6198     (gnus-summary-exit t)
6199     (while (not entered)
6200       ;; Then we find what group we are supposed to enter.
6201       (set-buffer gnus-group-buffer)
6202       (gnus-group-jump-to-group current-group)
6203       (setq target-group
6204             (or target-group
6205                 (if (eq gnus-keep-same-level 'best)
6206                     (gnus-summary-best-group gnus-newsgroup-name)
6207                   (gnus-summary-search-group backward gnus-keep-same-level))))
6208       (if (not target-group)
6209           ;; There are no further groups, so we return to the group
6210           ;; buffer.
6211           (progn
6212             (gnus-message 5 "Returning to the group buffer")
6213             (setq entered t)
6214             (when (gnus-buffer-live-p current-buffer)
6215               (set-buffer current-buffer)
6216               (gnus-summary-exit))
6217             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6218         ;; We try to enter the target group.
6219         (gnus-group-jump-to-group target-group)
6220         (let ((unreads (gnus-group-group-unread)))
6221           (if (and (or (eq t unreads)
6222                        (and unreads (not (zerop unreads))))
6223                    (gnus-summary-read-group
6224                     target-group nil no-article
6225                     (and (buffer-name current-buffer) current-buffer)
6226                     nil backward))
6227               (setq entered t)
6228             (setq current-group target-group
6229                   target-group nil)))))))
6230
6231 (defun gnus-summary-prev-group (&optional no-article)
6232   "Exit current newsgroup and then select previous unread newsgroup.
6233 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6234   (interactive "P")
6235   (gnus-summary-next-group no-article nil t))
6236
6237 ;; Walking around summary lines.
6238
6239 (defun gnus-summary-first-subject (&optional unread undownloaded)
6240   "Go to the first unread subject.
6241 If UNREAD is non-nil, go to the first unread article.
6242 Returns the article selected or nil if there are no unread articles."
6243   (interactive "P")
6244   (prog1
6245       (cond
6246        ;; Empty summary.
6247        ((null gnus-newsgroup-data)
6248         (gnus-message 3 "No articles in the group")
6249         nil)
6250        ;; Pick the first article.
6251        ((not unread)
6252         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6253         (gnus-data-number (car gnus-newsgroup-data)))
6254        ;; No unread articles.
6255        ((null gnus-newsgroup-unreads)
6256         (gnus-message 3 "No more unread articles")
6257         nil)
6258        ;; Find the first unread article.
6259        (t
6260         (let ((data gnus-newsgroup-data))
6261           (while (and data
6262                       (and (not (and undownloaded
6263                                      (eq gnus-undownloaded-mark
6264                                          (gnus-data-mark (car data)))))
6265                            (not (gnus-data-unread-p (car data)))))
6266             (setq data (cdr data)))
6267           (when data
6268             (goto-char (gnus-data-pos (car data)))
6269             (gnus-data-number (car data))))))
6270     (gnus-summary-position-point)))
6271
6272 (defun gnus-summary-next-subject (n &optional unread dont-display)
6273   "Go to next N'th summary line.
6274 If N is negative, go to the previous N'th subject line.
6275 If UNREAD is non-nil, only unread articles are selected.
6276 The difference between N and the actual number of steps taken is
6277 returned."
6278   (interactive "p")
6279   (let ((backward (< n 0))
6280         (n (abs n)))
6281     (while (and (> n 0)
6282                 (if backward
6283                     (gnus-summary-find-prev unread)
6284                   (gnus-summary-find-next unread)))
6285       (unless (zerop (setq n (1- n)))
6286         (gnus-summary-show-thread)))
6287     (when (/= 0 n)
6288       (gnus-message 7 "No more%s articles"
6289                     (if unread " unread" "")))
6290     (unless dont-display
6291       (gnus-summary-recenter)
6292       (gnus-summary-position-point))
6293     n))
6294
6295 (defun gnus-summary-next-unread-subject (n)
6296   "Go to next N'th unread summary line."
6297   (interactive "p")
6298   (gnus-summary-next-subject n t))
6299
6300 (defun gnus-summary-prev-subject (n &optional unread)
6301   "Go to previous N'th summary line.
6302 If optional argument UNREAD is non-nil, only unread article is selected."
6303   (interactive "p")
6304   (gnus-summary-next-subject (- n) unread))
6305
6306 (defun gnus-summary-prev-unread-subject (n)
6307   "Go to previous N'th unread summary line."
6308   (interactive "p")
6309   (gnus-summary-next-subject (- n) t))
6310
6311 (defun gnus-summary-goto-subject (article &optional force silent)
6312   "Go the subject line of ARTICLE.
6313 If FORCE, also allow jumping to articles not currently shown."
6314   (interactive "nArticle number: ")
6315   (let ((b (point))
6316         (data (gnus-data-find article)))
6317     ;; We read in the article if we have to.
6318     (and (not data)
6319          force
6320          (gnus-summary-insert-subject
6321           article
6322           (if (or (numberp force) (vectorp force)) force)
6323           t)
6324          (setq data (gnus-data-find article)))
6325     (goto-char b)
6326     (if (not data)
6327         (progn
6328           (unless silent
6329             (gnus-message 3 "Can't find article %d" article))
6330           nil)
6331       (let ((pt (gnus-data-pos data)))
6332         (goto-char pt)
6333         (gnus-summary-set-article-display-arrow pt))
6334       (gnus-summary-position-point)
6335       article)))
6336
6337 ;; Walking around summary lines with displaying articles.
6338
6339 (defun gnus-summary-expand-window (&optional arg)
6340   "Make the summary buffer take up the entire Emacs frame.
6341 Given a prefix, will force an `article' buffer configuration."
6342   (interactive "P")
6343   (if arg
6344       (gnus-configure-windows 'article 'force)
6345     (gnus-configure-windows 'summary 'force)))
6346
6347 (defun gnus-summary-display-article (article &optional all-header)
6348   "Display ARTICLE in article buffer."
6349   (when (gnus-buffer-live-p gnus-article-buffer)
6350     (with-current-buffer gnus-article-buffer
6351       (mm-enable-multibyte-mule4)))
6352   (gnus-set-global-variables)
6353   (when (gnus-buffer-live-p gnus-article-buffer)
6354     (with-current-buffer gnus-article-buffer
6355       (setq gnus-article-charset gnus-newsgroup-charset)
6356       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6357       (mm-enable-multibyte-mule4)))
6358   (if (null article)
6359       nil
6360     (prog1
6361         (if gnus-summary-display-article-function
6362             (funcall gnus-summary-display-article-function article all-header)
6363           (gnus-article-prepare article all-header))
6364       (gnus-run-hooks 'gnus-select-article-hook)
6365       (when (and gnus-current-article
6366                  (not (zerop gnus-current-article)))
6367         (gnus-summary-goto-subject gnus-current-article))
6368       (gnus-summary-recenter)
6369       (when (and gnus-use-trees gnus-show-threads)
6370         (gnus-possibly-generate-tree article)
6371         (gnus-highlight-selected-tree article))
6372       ;; Successfully display article.
6373       (gnus-article-set-window-start
6374        (cdr (assq article gnus-newsgroup-bookmarks))))))
6375
6376 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6377   "Select the current article.
6378 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6379 non-nil, the article will be re-fetched even if it already present in
6380 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6381 be displayed."
6382   ;; Make sure we are in the summary buffer to work around bbdb bug.
6383   (unless (eq major-mode 'gnus-summary-mode)
6384     (set-buffer gnus-summary-buffer))
6385   (let ((article (or article (gnus-summary-article-number)))
6386         (all-headers (not (not all-headers))) ;Must be T or NIL.
6387         gnus-summary-display-article-function)
6388     (and (not pseudo)
6389          (gnus-summary-article-pseudo-p article)
6390          (error "This is a pseudo-article"))
6391     (save-excursion
6392       (set-buffer gnus-summary-buffer)
6393       (if (or (and gnus-single-article-buffer
6394                    (or (null gnus-current-article)
6395                        (null gnus-article-current)
6396                        (null (get-buffer gnus-article-buffer))
6397                        (not (eq article (cdr gnus-article-current)))
6398                        (not (equal (car gnus-article-current)
6399                                    gnus-newsgroup-name))))
6400               (and (not gnus-single-article-buffer)
6401                    (or (null gnus-current-article)
6402                        (not (eq gnus-current-article article))))
6403               force)
6404        ;; The requested article is different from the current article.
6405           (progn
6406             (gnus-summary-display-article article all-headers)
6407             (when (gnus-buffer-live-p gnus-article-buffer)
6408               (with-current-buffer gnus-article-buffer
6409                 (if (not gnus-article-decoded-p) ;; a local variable
6410                     (mm-disable-multibyte-mule4))))
6411             (when (or all-headers gnus-show-all-headers)
6412               (gnus-article-show-all-headers))
6413             (gnus-article-set-window-start
6414              (cdr (assq article gnus-newsgroup-bookmarks)))
6415             article)
6416         (when (or all-headers gnus-show-all-headers)
6417           (gnus-article-show-all-headers))
6418         'old))))
6419
6420 (defun gnus-summary-force-verify-and-decrypt ()
6421   (interactive)
6422   (let ((mm-verify-option 'known)
6423         (mm-decrypt-option 'known))
6424     (gnus-summary-select-article nil 'force)))
6425
6426 (defun gnus-summary-set-current-mark (&optional current-mark)
6427   "Obsolete function."
6428   nil)
6429
6430 (defun gnus-summary-next-article (&optional unread subject backward push)
6431   "Select the next article.
6432 If UNREAD, only unread articles are selected.
6433 If SUBJECT, only articles with SUBJECT are selected.
6434 If BACKWARD, the previous article is selected instead of the next."
6435   (interactive "P")
6436   (cond
6437    ;; Is there such an article?
6438    ((and (gnus-summary-search-forward unread subject backward)
6439          (or (gnus-summary-display-article (gnus-summary-article-number))
6440              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6441     (gnus-summary-position-point))
6442    ;; If not, we try the first unread, if that is wanted.
6443    ((and subject
6444          gnus-auto-select-same
6445          (gnus-summary-first-unread-article))
6446     (gnus-summary-position-point)
6447     (gnus-message 6 "Wrapped"))
6448    ;; Try to get next/previous article not displayed in this group.
6449    ((and gnus-auto-extend-newsgroup
6450          (not unread) (not subject))
6451     (gnus-summary-goto-article
6452      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6453      nil (count-lines (point-min) (point))))
6454    ;; Go to next/previous group.
6455    (t
6456     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6457       (gnus-summary-jump-to-group gnus-newsgroup-name))
6458     (let ((cmd last-command-char)
6459           (point
6460            (save-excursion
6461              (set-buffer gnus-group-buffer)
6462              (point)))
6463           (group
6464            (if (eq gnus-keep-same-level 'best)
6465                (gnus-summary-best-group gnus-newsgroup-name)
6466              (gnus-summary-search-group backward gnus-keep-same-level))))
6467       ;; For some reason, the group window gets selected.  We change
6468       ;; it back.
6469       (select-window (get-buffer-window (current-buffer)))
6470       ;; Select next unread newsgroup automagically.
6471       (cond
6472        ((or (not gnus-auto-select-next)
6473             (not cmd))
6474         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6475        ((or (eq gnus-auto-select-next 'quietly)
6476             (and (eq gnus-auto-select-next 'slightly-quietly)
6477                  push)
6478             (and (eq gnus-auto-select-next 'almost-quietly)
6479                  (gnus-summary-last-article-p)))
6480         ;; Select quietly.
6481         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6482             (gnus-summary-exit)
6483           (gnus-message 7 "No more%s articles (%s)..."
6484                         (if unread " unread" "")
6485                         (if group (concat "selecting " group)
6486                           "exiting"))
6487           (gnus-summary-next-group nil group backward)))
6488        (t
6489         (when (gnus-key-press-event-p last-input-event)
6490           (gnus-summary-walk-group-buffer
6491            gnus-newsgroup-name cmd unread backward point))))))))
6492
6493 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6494   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6495                       (?\C-p (gnus-group-prev-unread-group 1))))
6496         (cursor-in-echo-area t)
6497         keve key group ended)
6498     (save-excursion
6499       (set-buffer gnus-group-buffer)
6500       (goto-char start)
6501       (setq group
6502             (if (eq gnus-keep-same-level 'best)
6503                 (gnus-summary-best-group gnus-newsgroup-name)
6504               (gnus-summary-search-group backward gnus-keep-same-level))))
6505     (while (not ended)
6506       (gnus-message
6507        5 "No more%s articles%s" (if unread " unread" "")
6508        (if (and group
6509                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6510            (format " (Type %s for %s [%s])"
6511                    (single-key-description cmd) group
6512                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6513          (format " (Type %s to exit %s)"
6514                  (single-key-description cmd)
6515                  gnus-newsgroup-name)))
6516       ;; Confirm auto selection.
6517       (setq key (car (setq keve (gnus-read-event-char))))
6518       (setq ended t)
6519       (cond
6520        ((assq key keystrokes)
6521         (let ((obuf (current-buffer)))
6522           (switch-to-buffer gnus-group-buffer)
6523           (when group
6524             (gnus-group-jump-to-group group))
6525           (eval (cadr (assq key keystrokes)))
6526           (setq group (gnus-group-group-name))
6527           (switch-to-buffer obuf))
6528         (setq ended nil))
6529        ((equal key cmd)
6530         (if (or (not group)
6531                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6532             (gnus-summary-exit)
6533           (gnus-summary-next-group nil group backward)))
6534        (t
6535         (push (cdr keve) unread-command-events))))))
6536
6537 (defun gnus-summary-next-unread-article ()
6538   "Select unread article after current one."
6539   (interactive)
6540   (gnus-summary-next-article
6541    (or (not (eq gnus-summary-goto-unread 'never))
6542        (gnus-summary-last-article-p (gnus-summary-article-number)))
6543    (and gnus-auto-select-same
6544         (gnus-summary-article-subject))))
6545
6546 (defun gnus-summary-prev-article (&optional unread subject)
6547   "Select the article after the current one.
6548 If UNREAD is non-nil, only unread articles are selected."
6549   (interactive "P")
6550   (gnus-summary-next-article unread subject t))
6551
6552 (defun gnus-summary-prev-unread-article ()
6553   "Select unread article before current one."
6554   (interactive)
6555   (gnus-summary-prev-article
6556    (or (not (eq gnus-summary-goto-unread 'never))
6557        (gnus-summary-first-article-p (gnus-summary-article-number)))
6558    (and gnus-auto-select-same
6559         (gnus-summary-article-subject))))
6560
6561 (defun gnus-summary-next-page (&optional lines circular)
6562   "Show next page of the selected article.
6563 If at the end of the current article, select the next article.
6564 LINES says how many lines should be scrolled up.
6565
6566 If CIRCULAR is non-nil, go to the start of the article instead of
6567 selecting the next article when reaching the end of the current
6568 article."
6569   (interactive "P")
6570   (setq gnus-summary-buffer (current-buffer))
6571   (gnus-set-global-variables)
6572   (let ((article (gnus-summary-article-number))
6573         (article-window (get-buffer-window gnus-article-buffer t))
6574         endp)
6575     ;; If the buffer is empty, we have no article.
6576     (unless article
6577       (error "No article to select"))
6578     (gnus-configure-windows 'article)
6579     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6580         (if (and (eq gnus-summary-goto-unread 'never)
6581                  (not (gnus-summary-last-article-p article)))
6582             (gnus-summary-next-article)
6583           (gnus-summary-next-unread-article))
6584       (if (or (null gnus-current-article)
6585               (null gnus-article-current)
6586               (/= article (cdr gnus-article-current))
6587               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6588           ;; Selected subject is different from current article's.
6589           (gnus-summary-display-article article)
6590         (when article-window
6591           (gnus-eval-in-buffer-window gnus-article-buffer
6592             (setq endp (gnus-article-next-page lines)))
6593           (when endp
6594             (cond (circular
6595                    (gnus-summary-beginning-of-article))
6596                   (lines
6597                    (gnus-message 3 "End of message"))
6598                   ((null lines)
6599                    (if (and (eq gnus-summary-goto-unread 'never)
6600                             (not (gnus-summary-last-article-p article)))
6601                        (gnus-summary-next-article)
6602                      (gnus-summary-next-unread-article))))))))
6603     (gnus-summary-recenter)
6604     (gnus-summary-position-point)))
6605
6606 (defun gnus-summary-prev-page (&optional lines move)
6607   "Show previous page of selected article.
6608 Argument LINES specifies lines to be scrolled down.
6609 If MOVE, move to the previous unread article if point is at
6610 the beginning of the buffer."
6611   (interactive "P")
6612   (let ((article (gnus-summary-article-number))
6613         (article-window (get-buffer-window gnus-article-buffer t))
6614         endp)
6615     (gnus-configure-windows 'article)
6616     (if (or (null gnus-current-article)
6617             (null gnus-article-current)
6618             (/= article (cdr gnus-article-current))
6619             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6620         ;; Selected subject is different from current article's.
6621         (gnus-summary-display-article article)
6622       (gnus-summary-recenter)
6623       (when article-window
6624         (gnus-eval-in-buffer-window gnus-article-buffer
6625           (setq endp (gnus-article-prev-page lines)))
6626         (when (and move endp)
6627           (cond (lines
6628                  (gnus-message 3 "Beginning of message"))
6629                 ((null lines)
6630                  (if (and (eq gnus-summary-goto-unread 'never)
6631                           (not (gnus-summary-first-article-p article)))
6632                      (gnus-summary-prev-article)
6633                    (gnus-summary-prev-unread-article))))))))
6634   (gnus-summary-position-point))
6635
6636 (defun gnus-summary-prev-page-or-article (&optional lines)
6637   "Show previous page of selected article.
6638 Argument LINES specifies lines to be scrolled down.
6639 If at the beginning of the article, go to the next article."
6640   (interactive "P")
6641   (gnus-summary-prev-page lines t))
6642
6643 (defun gnus-summary-scroll-up (lines)
6644   "Scroll up (or down) one line current article.
6645 Argument LINES specifies lines to be scrolled up (or down if negative)."
6646   (interactive "p")
6647   (gnus-configure-windows 'article)
6648   (gnus-summary-show-thread)
6649   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6650     (gnus-eval-in-buffer-window gnus-article-buffer
6651       (cond ((> lines 0)
6652              (when (gnus-article-next-page lines)
6653                (gnus-message 3 "End of message")))
6654             ((< lines 0)
6655              (gnus-article-prev-page (- lines))))))
6656   (gnus-summary-recenter)
6657   (gnus-summary-position-point))
6658
6659 (defun gnus-summary-scroll-down (lines)
6660   "Scroll down (or up) one line current article.
6661 Argument LINES specifies lines to be scrolled down (or up if negative)."
6662   (interactive "p")
6663   (gnus-summary-scroll-up (- lines)))
6664
6665 (defun gnus-summary-next-same-subject ()
6666   "Select next article which has the same subject as current one."
6667   (interactive)
6668   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6669
6670 (defun gnus-summary-prev-same-subject ()
6671   "Select previous article which has the same subject as current one."
6672   (interactive)
6673   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6674
6675 (defun gnus-summary-next-unread-same-subject ()
6676   "Select next unread article which has the same subject as current one."
6677   (interactive)
6678   (gnus-summary-next-article t (gnus-summary-article-subject)))
6679
6680 (defun gnus-summary-prev-unread-same-subject ()
6681   "Select previous unread article which has the same subject as current one."
6682   (interactive)
6683   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6684
6685 (defun gnus-summary-first-unread-article ()
6686   "Select the first unread article.
6687 Return nil if there are no unread articles."
6688   (interactive)
6689   (prog1
6690       (when (gnus-summary-first-subject t)
6691         (gnus-summary-show-thread)
6692         (gnus-summary-first-subject t)
6693         (gnus-summary-display-article (gnus-summary-article-number)))
6694     (gnus-summary-position-point)))
6695
6696 (defun gnus-summary-first-unread-subject ()
6697   "Place the point on the subject line of the first unread article.
6698 Return nil if there are no unread articles."
6699   (interactive)
6700   (prog1
6701       (when (gnus-summary-first-subject t)
6702         (gnus-summary-show-thread)
6703         (gnus-summary-first-subject t))
6704     (gnus-summary-position-point)))
6705
6706 (defun gnus-summary-first-article ()
6707   "Select the first article.
6708 Return nil if there are no articles."
6709   (interactive)
6710   (prog1
6711       (when (gnus-summary-first-subject)
6712         (gnus-summary-show-thread)
6713         (gnus-summary-first-subject)
6714         (gnus-summary-display-article (gnus-summary-article-number)))
6715     (gnus-summary-position-point)))
6716
6717 (defun gnus-summary-best-unread-article ()
6718   "Select the unread article with the highest score."
6719   (interactive)
6720   (let ((best -1000000)
6721         (data gnus-newsgroup-data)
6722         article score)
6723     (while data
6724       (and (gnus-data-unread-p (car data))
6725            (> (setq score
6726                     (gnus-summary-article-score (gnus-data-number (car data))))
6727               best)
6728            (setq best score
6729                  article (gnus-data-number (car data))))
6730       (setq data (cdr data)))
6731     (prog1
6732         (if article
6733             (gnus-summary-goto-article article)
6734           (error "No unread articles"))
6735       (gnus-summary-position-point))))
6736
6737 (defun gnus-summary-last-subject ()
6738   "Go to the last displayed subject line in the group."
6739   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6740     (when article
6741       (gnus-summary-goto-subject article))))
6742
6743 (defun gnus-summary-goto-article (article &optional all-headers force)
6744   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6745 If ALL-HEADERS is non-nil, no header lines are hidden.
6746 If FORCE, go to the article even if it isn't displayed.  If FORCE
6747 is a number, it is the line the article is to be displayed on."
6748   (interactive
6749    (list
6750     (completing-read
6751      "Article number or Message-ID: "
6752      (mapcar (lambda (number) (list (int-to-string number)))
6753              gnus-newsgroup-limit))
6754     current-prefix-arg
6755     t))
6756   (prog1
6757       (if (and (stringp article)
6758                (string-match "@" article))
6759           (gnus-summary-refer-article article)
6760         (when (stringp article)
6761           (setq article (string-to-number article)))
6762         (if (gnus-summary-goto-subject article force)
6763             (gnus-summary-display-article article all-headers)
6764           (gnus-message 4 "Couldn't go to article %s" article) nil))
6765     (gnus-summary-position-point)))
6766
6767 (defun gnus-summary-goto-last-article ()
6768   "Go to the previously read article."
6769   (interactive)
6770   (prog1
6771       (when gnus-last-article
6772         (gnus-summary-goto-article gnus-last-article nil t))
6773     (gnus-summary-position-point)))
6774
6775 (defun gnus-summary-pop-article (number)
6776   "Pop one article off the history and go to the previous.
6777 NUMBER articles will be popped off."
6778   (interactive "p")
6779   (let (to)
6780     (setq gnus-newsgroup-history
6781           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6782     (if to
6783         (gnus-summary-goto-article (car to) nil t)
6784       (error "Article history empty")))
6785   (gnus-summary-position-point))
6786
6787 ;; Summary commands and functions for limiting the summary buffer.
6788
6789 (defun gnus-summary-limit-to-articles (n)
6790   "Limit the summary buffer to the next N articles.
6791 If not given a prefix, use the process marked articles instead."
6792   (interactive "P")
6793   (prog1
6794       (let ((articles (gnus-summary-work-articles n)))
6795         (setq gnus-newsgroup-processable nil)
6796         (gnus-summary-limit articles))
6797     (gnus-summary-position-point)))
6798
6799 (defun gnus-summary-pop-limit (&optional total)
6800   "Restore the previous limit.
6801 If given a prefix, remove all limits."
6802   (interactive "P")
6803   (when total
6804     (setq gnus-newsgroup-limits
6805           (list (mapcar (lambda (h) (mail-header-number h))
6806                         gnus-newsgroup-headers))))
6807   (unless gnus-newsgroup-limits
6808     (error "No limit to pop"))
6809   (prog1
6810       (gnus-summary-limit nil 'pop)
6811     (gnus-summary-position-point)))
6812
6813 (defun gnus-summary-limit-to-subject (subject &optional header)
6814   "Limit the summary buffer to articles that have subjects that match a regexp."
6815   (interactive "sLimit to subject (regexp): ")
6816   (unless header
6817     (setq header "subject"))
6818   (when (not (equal "" subject))
6819     (prog1
6820         (let ((articles (gnus-summary-find-matching
6821                          (or header "subject") subject 'all)))
6822           (unless articles
6823             (error "Found no matches for \"%s\"" subject))
6824           (gnus-summary-limit articles))
6825       (gnus-summary-position-point))))
6826
6827 (defun gnus-summary-limit-to-author (from)
6828   "Limit the summary buffer to articles that have authors that match a regexp."
6829   (interactive "sLimit to author (regexp): ")
6830   (gnus-summary-limit-to-subject from "from"))
6831
6832 (defun gnus-summary-limit-to-age (age &optional younger-p)
6833   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6834 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6835 articles that are younger than AGE days."
6836   (interactive
6837    (let ((younger current-prefix-arg)
6838          (days-got nil)
6839          days)
6840      (while (not days-got)
6841        (setq days (if younger
6842                       (read-string "Limit to articles within (in days): ")
6843                     (read-string "Limit to articles older than (in days): ")))
6844        (when (> (length days) 0)
6845          (setq days (read days)))
6846        (if (numberp days)
6847            (setq days-got t)
6848          (message "Please enter a number.")
6849          (sleep-for 1)))
6850      (list days younger)))
6851   (prog1
6852       (let ((data gnus-newsgroup-data)
6853             (cutoff (days-to-time age))
6854             articles d date is-younger)
6855         (while (setq d (pop data))
6856           (when (and (vectorp (gnus-data-header d))
6857                      (setq date (mail-header-date (gnus-data-header d))))
6858             (setq is-younger (time-less-p
6859                               (time-since (condition-case ()
6860                                               (date-to-time date)
6861                                             (error '(0 0))))
6862                               cutoff))
6863             (when (if younger-p
6864                       is-younger
6865                     (not is-younger))
6866               (push (gnus-data-number d) articles))))
6867         (gnus-summary-limit (nreverse articles)))
6868     (gnus-summary-position-point)))
6869
6870 (defun gnus-summary-limit-to-extra (header regexp)
6871   "Limit the summary buffer to articles that match an 'extra' header."
6872   (interactive
6873    (let ((header
6874           (intern
6875            (gnus-completing-read
6876             (symbol-name (car gnus-extra-headers))
6877             "Limit extra header:"
6878             (mapcar (lambda (x)
6879                       (cons (symbol-name x) x))
6880                     gnus-extra-headers)
6881             nil
6882             t))))
6883      (list header
6884            (read-string (format "Limit to header %s (regexp): " header)))))
6885   (when (not (equal "" regexp))
6886     (prog1
6887         (let ((articles (gnus-summary-find-matching
6888                          (cons 'extra header) regexp 'all)))
6889           (unless articles
6890             (error "Found no matches for \"%s\"" regexp))
6891           (gnus-summary-limit articles))
6892       (gnus-summary-position-point))))
6893
6894 (defun gnus-summary-limit-to-display-predicate ()
6895   "Limit the summary buffer to the predicated in the `display' group parameter."
6896   (interactive)
6897   (unless gnus-newsgroup-display
6898     (error "There is no `display' group parameter"))
6899   (let (articles)
6900     (dolist (number gnus-newsgroup-articles)
6901       (when (funcall gnus-newsgroup-display)
6902         (push number articles)))
6903     (gnus-summary-limit articles))
6904   (gnus-summary-position-point))
6905
6906 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6907 (make-obsolete
6908  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6909
6910 (defun gnus-summary-limit-to-unread (&optional all)
6911   "Limit the summary buffer to articles that are not marked as read.
6912 If ALL is non-nil, limit strictly to unread articles."
6913   (interactive "P")
6914   (if all
6915       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6916     (gnus-summary-limit-to-marks
6917      ;; Concat all the marks that say that an article is read and have
6918      ;; those removed.
6919      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6920            gnus-killed-mark gnus-kill-file-mark
6921            gnus-low-score-mark gnus-expirable-mark
6922            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6923            gnus-duplicate-mark gnus-souped-mark)
6924      'reverse)))
6925
6926 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6927 (make-obsolete 'gnus-summary-delete-marked-with
6928                'gnus-summary-limit-exlude-marks)
6929
6930 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6931   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6932 If REVERSE, limit the summary buffer to articles that are marked
6933 with MARKS.  MARKS can either be a string of marks or a list of marks.
6934 Returns how many articles were removed."
6935   (interactive "sMarks: ")
6936   (gnus-summary-limit-to-marks marks t))
6937
6938 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6939   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6940 If REVERSE (the prefix), limit the summary buffer to articles that are
6941 not marked with MARKS.  MARKS can either be a string of marks or a
6942 list of marks.
6943 Returns how many articles were removed."
6944   (interactive "sMarks: \nP")
6945   (prog1
6946       (let ((data gnus-newsgroup-data)
6947             (marks (if (listp marks) marks
6948                      (append marks nil))) ; Transform to list.
6949             articles)
6950         (while data
6951           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6952                   (memq (gnus-data-mark (car data)) marks))
6953             (push (gnus-data-number (car data)) articles))
6954           (setq data (cdr data)))
6955         (gnus-summary-limit articles))
6956     (gnus-summary-position-point)))
6957
6958 (defun gnus-summary-limit-to-score (&optional score)
6959   "Limit to articles with score at or above SCORE."
6960   (interactive "P")
6961   (setq score (if score
6962                   (prefix-numeric-value score)
6963                 (or gnus-summary-default-score 0)))
6964   (let ((data gnus-newsgroup-data)
6965         articles)
6966     (while data
6967       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6968                 score)
6969         (push (gnus-data-number (car data)) articles))
6970       (setq data (cdr data)))
6971     (prog1
6972         (gnus-summary-limit articles)
6973       (gnus-summary-position-point))))
6974
6975 (defun gnus-summary-limit-include-thread (id)
6976   "Display all the hidden articles that is in the thread with ID in it.
6977 When called interactively, ID is the Message-ID of the current
6978 article."
6979   (interactive (list (mail-header-id (gnus-summary-article-header))))
6980   (let ((articles (gnus-articles-in-thread
6981                    (gnus-id-to-thread (gnus-root-id id)))))
6982     (prog1
6983         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6984       (gnus-summary-limit-include-matching-articles
6985        "subject"
6986        (regexp-quote (gnus-simplify-subject-re
6987                       (mail-header-subject (gnus-id-to-header id)))))
6988       (gnus-summary-position-point))))
6989
6990 (defun gnus-summary-limit-include-matching-articles (header regexp)
6991   "Display all the hidden articles that have HEADERs that match REGEXP."
6992   (interactive (list (read-string "Match on header: ")
6993                      (read-string "Regexp: ")))
6994   (let ((articles (gnus-find-matching-articles header regexp)))
6995     (prog1
6996         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6997       (gnus-summary-position-point))))
6998
6999 (defun gnus-summary-limit-include-dormant ()
7000   "Display all the hidden articles that are marked as dormant.
7001 Note that this command only works on a subset of the articles currently
7002 fetched for this group."
7003   (interactive)
7004   (unless gnus-newsgroup-dormant
7005     (error "There are no dormant articles in this group"))
7006   (prog1
7007       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7008     (gnus-summary-position-point)))
7009
7010 (defun gnus-summary-limit-exclude-dormant ()
7011   "Hide all dormant articles."
7012   (interactive)
7013   (prog1
7014       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7015     (gnus-summary-position-point)))
7016
7017 (defun gnus-summary-limit-exclude-childless-dormant ()
7018   "Hide all dormant articles that have no children."
7019   (interactive)
7020   (let ((data (gnus-data-list t))
7021         articles d children)
7022     ;; Find all articles that are either not dormant or have
7023     ;; children.
7024     (while (setq d (pop data))
7025       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7026                 (and (setq children
7027                            (gnus-article-children (gnus-data-number d)))
7028                      (let (found)
7029                        (while children
7030                          (when (memq (car children) articles)
7031                            (setq children nil
7032                                  found t))
7033                          (pop children))
7034                        found)))
7035         (push (gnus-data-number d) articles)))
7036     ;; Do the limiting.
7037     (prog1
7038         (gnus-summary-limit articles)
7039       (gnus-summary-position-point))))
7040
7041 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7042   "Mark all unread excluded articles as read.
7043 If ALL, mark even excluded ticked and dormants as read."
7044   (interactive "P")
7045   (let ((articles (gnus-sorted-complement
7046                    (sort
7047                     (mapcar (lambda (h) (mail-header-number h))
7048                             gnus-newsgroup-headers)
7049                     '<)
7050                    (sort gnus-newsgroup-limit '<)))
7051         article)
7052     (setq gnus-newsgroup-unreads
7053           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7054     (if all
7055         (setq gnus-newsgroup-dormant nil
7056               gnus-newsgroup-marked nil
7057               gnus-newsgroup-reads
7058               (nconc
7059                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7060                gnus-newsgroup-reads))
7061       (while (setq article (pop articles))
7062         (unless (or (memq article gnus-newsgroup-dormant)
7063                     (memq article gnus-newsgroup-marked))
7064           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7065
7066 (defun gnus-summary-limit (articles &optional pop)
7067   (if pop
7068       ;; We pop the previous limit off the stack and use that.
7069       (setq articles (car gnus-newsgroup-limits)
7070             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7071     ;; We use the new limit, so we push the old limit on the stack.
7072     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7073   ;; Set the limit.
7074   (setq gnus-newsgroup-limit articles)
7075   (let ((total (length gnus-newsgroup-data))
7076         (data (gnus-data-find-list (gnus-summary-article-number)))
7077         (gnus-summary-mark-below nil)   ; Inhibit this.
7078         found)
7079     ;; This will do all the work of generating the new summary buffer
7080     ;; according to the new limit.
7081     (gnus-summary-prepare)
7082     ;; Hide any threads, possibly.
7083     (and gnus-show-threads
7084          gnus-thread-hide-subtree
7085          (gnus-summary-hide-all-threads))
7086     ;; Try to return to the article you were at, or one in the
7087     ;; neighborhood.
7088     (when data
7089       ;; We try to find some article after the current one.
7090       (while data
7091         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7092           (setq data nil
7093                 found t))
7094         (setq data (cdr data))))
7095     (unless found
7096       ;; If there is no data, that means that we were after the last
7097       ;; article.  The same goes when we can't find any articles
7098       ;; after the current one.
7099       (goto-char (point-max))
7100       (gnus-summary-find-prev))
7101     (gnus-set-mode-line 'summary)
7102     ;; We return how many articles were removed from the summary
7103     ;; buffer as a result of the new limit.
7104     (- total (length gnus-newsgroup-data))))
7105
7106 (defsubst gnus-invisible-cut-children (threads)
7107   (let ((num 0))
7108     (while threads
7109       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7110         (incf num))
7111       (pop threads))
7112     (< num 2)))
7113
7114 (defsubst gnus-cut-thread (thread)
7115   "Go forwards in the thread until we find an article that we want to display."
7116   (when (or (eq gnus-fetch-old-headers 'some)
7117             (eq gnus-fetch-old-headers 'invisible)
7118             (numberp gnus-fetch-old-headers)
7119             (eq gnus-build-sparse-threads 'some)
7120             (eq gnus-build-sparse-threads 'more))
7121     ;; Deal with old-fetched headers and sparse threads.
7122     (while (and
7123             thread
7124             (or
7125              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7126              (gnus-summary-article-ancient-p
7127               (mail-header-number (car thread))))
7128             (if (or (<= (length (cdr thread)) 1)
7129                     (eq gnus-fetch-old-headers 'invisible))
7130                 (setq gnus-newsgroup-limit
7131                       (delq (mail-header-number (car thread))
7132                             gnus-newsgroup-limit)
7133                       thread (cadr thread))
7134               (when (gnus-invisible-cut-children (cdr thread))
7135                 (let ((th (cdr thread)))
7136                   (while th
7137                     (if (memq (mail-header-number (caar th))
7138                               gnus-newsgroup-limit)
7139                         (setq thread (car th)
7140                               th nil)
7141                       (setq th (cdr th))))))))))
7142   thread)
7143
7144 (defun gnus-cut-threads (threads)
7145   "Cut off all uninteresting articles from the beginning of threads."
7146   (when (or (eq gnus-fetch-old-headers 'some)
7147             (eq gnus-fetch-old-headers 'invisible)
7148             (numberp gnus-fetch-old-headers)
7149             (eq gnus-build-sparse-threads 'some)
7150             (eq gnus-build-sparse-threads 'more))
7151     (let ((th threads))
7152       (while th
7153         (setcar th (gnus-cut-thread (car th)))
7154         (setq th (cdr th)))))
7155   ;; Remove nixed out threads.
7156   (delq nil threads))
7157
7158 (defun gnus-summary-initial-limit (&optional show-if-empty)
7159   "Figure out what the initial limit is supposed to be on group entry.
7160 This entails weeding out unwanted dormants, low-scored articles,
7161 fetch-old-headers verbiage, and so on."
7162   ;; Most groups have nothing to remove.
7163   (if (or gnus-inhibit-limiting
7164           (and (null gnus-newsgroup-dormant)
7165                (eq gnus-newsgroup-display 'gnus-not-ignore)
7166                (not (eq gnus-fetch-old-headers 'some))
7167                (not (numberp gnus-fetch-old-headers))
7168                (not (eq gnus-fetch-old-headers 'invisible))
7169                (null gnus-summary-expunge-below)
7170                (not (eq gnus-build-sparse-threads 'some))
7171                (not (eq gnus-build-sparse-threads 'more))
7172                (null gnus-thread-expunge-below)
7173                (not gnus-use-nocem)))
7174       ()                                ; Do nothing.
7175     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7176     (setq gnus-newsgroup-limit nil)
7177     (mapatoms
7178      (lambda (node)
7179        (unless (car (symbol-value node))
7180          ;; These threads have no parents -- they are roots.
7181          (let ((nodes (cdr (symbol-value node)))
7182                thread)
7183            (while nodes
7184              (if (and gnus-thread-expunge-below
7185                       (< (gnus-thread-total-score (car nodes))
7186                          gnus-thread-expunge-below))
7187                  (gnus-expunge-thread (pop nodes))
7188                (setq thread (pop nodes))
7189                (gnus-summary-limit-children thread))))))
7190      gnus-newsgroup-dependencies)
7191     ;; If this limitation resulted in an empty group, we might
7192     ;; pop the previous limit and use it instead.
7193     (when (and (not gnus-newsgroup-limit)
7194                show-if-empty)
7195       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7196     gnus-newsgroup-limit))
7197
7198 (defun gnus-summary-limit-children (thread)
7199   "Return 1 if this subthread is visible and 0 if it is not."
7200   ;; First we get the number of visible children to this thread.  This
7201   ;; is done by recursing down the thread using this function, so this
7202   ;; will really go down to a leaf article first, before slowly
7203   ;; working its way up towards the root.
7204   (when thread
7205     (let ((children
7206            (if (cdr thread)
7207                (apply '+ (mapcar 'gnus-summary-limit-children
7208                                  (cdr thread)))
7209              0))
7210           (number (mail-header-number (car thread)))
7211           score)
7212       (if (and
7213            (not (memq number gnus-newsgroup-marked))
7214            (or
7215             ;; If this article is dormant and has absolutely no visible
7216             ;; children, then this article isn't visible.
7217             (and (memq number gnus-newsgroup-dormant)
7218                  (zerop children))
7219             ;; If this is "fetch-old-headered" and there is no
7220             ;; visible children, then we don't want this article.
7221             (and (or (eq gnus-fetch-old-headers 'some)
7222                      (numberp gnus-fetch-old-headers))
7223                  (gnus-summary-article-ancient-p number)
7224                  (zerop children))
7225             ;; If this is "fetch-old-headered" and `invisible', then
7226             ;; we don't want this article.
7227             (and (eq gnus-fetch-old-headers 'invisible)
7228                  (gnus-summary-article-ancient-p number))
7229             ;; If this is a sparsely inserted article with no children,
7230             ;; we don't want it.
7231             (and (eq gnus-build-sparse-threads 'some)
7232                  (gnus-summary-article-sparse-p number)
7233                  (zerop children))
7234             ;; If we use expunging, and this article is really
7235             ;; low-scored, then we don't want this article.
7236             (when (and gnus-summary-expunge-below
7237                        (< (setq score
7238                                 (or (cdr (assq number gnus-newsgroup-scored))
7239                                     gnus-summary-default-score))
7240                           gnus-summary-expunge-below))
7241               ;; We increase the expunge-tally here, but that has
7242               ;; nothing to do with the limits, really.
7243               (incf gnus-newsgroup-expunged-tally)
7244               ;; We also mark as read here, if that's wanted.
7245               (when (and gnus-summary-mark-below
7246                          (< score gnus-summary-mark-below))
7247                 (setq gnus-newsgroup-unreads
7248                       (delq number gnus-newsgroup-unreads))
7249                 (if gnus-newsgroup-auto-expire
7250                     (push number gnus-newsgroup-expirable)
7251                   (push (cons number gnus-low-score-mark)
7252                         gnus-newsgroup-reads)))
7253               t)
7254             ;; Do the `display' group parameter.
7255             (and gnus-newsgroup-display
7256                  (not (funcall gnus-newsgroup-display)))
7257             ;; Check NoCeM things.
7258             (if (and gnus-use-nocem
7259                      (gnus-nocem-unwanted-article-p
7260                       (mail-header-id (car thread))))
7261                 (progn
7262                   (setq gnus-newsgroup-unreads
7263                         (delq number gnus-newsgroup-unreads))
7264                   t))))
7265           ;; Nope, invisible article.
7266           0
7267         ;; Ok, this article is to be visible, so we add it to the limit
7268         ;; and return 1.
7269         (push number gnus-newsgroup-limit)
7270         1))))
7271
7272 (defun gnus-expunge-thread (thread)
7273   "Mark all articles in THREAD as read."
7274   (let* ((number (mail-header-number (car thread))))
7275     (incf gnus-newsgroup-expunged-tally)
7276     ;; We also mark as read here, if that's wanted.
7277     (setq gnus-newsgroup-unreads
7278           (delq number gnus-newsgroup-unreads))
7279     (if gnus-newsgroup-auto-expire
7280         (push number gnus-newsgroup-expirable)
7281       (push (cons number gnus-low-score-mark)
7282             gnus-newsgroup-reads)))
7283   ;; Go recursively through all subthreads.
7284   (mapcar 'gnus-expunge-thread (cdr thread)))
7285
7286 ;; Summary article oriented commands
7287
7288 (defun gnus-summary-refer-parent-article (n)
7289   "Refer parent article N times.
7290 If N is negative, go to ancestor -N instead.
7291 The difference between N and the number of articles fetched is returned."
7292   (interactive "p")
7293   (let ((skip 1)
7294         error header ref)
7295     (when (not (natnump n))
7296       (setq skip (abs n)
7297             n 1))
7298     (while (and (> n 0)
7299                 (not error))
7300       (setq header (gnus-summary-article-header))
7301       (if (and (eq (mail-header-number header)
7302                    (cdr gnus-article-current))
7303                (equal gnus-newsgroup-name
7304                       (car gnus-article-current)))
7305           ;; If we try to find the parent of the currently
7306           ;; displayed article, then we take a look at the actual
7307           ;; References header, since this is slightly more
7308           ;; reliable than the References field we got from the
7309           ;; server.
7310           (save-excursion
7311             (set-buffer gnus-original-article-buffer)
7312             (nnheader-narrow-to-headers)
7313             (unless (setq ref (message-fetch-field "references"))
7314               (setq ref (message-fetch-field "in-reply-to")))
7315             (widen))
7316         (setq ref
7317               ;; It's not the current article, so we take a bet on
7318               ;; the value we got from the server.
7319               (mail-header-references header)))
7320       (if (and ref
7321                (not (equal ref "")))
7322           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7323             (gnus-message 1 "Couldn't find parent"))
7324         (gnus-message 1 "No references in article %d"
7325                       (gnus-summary-article-number))
7326         (setq error t))
7327       (decf n))
7328     (gnus-summary-position-point)
7329     n))
7330
7331 (defun gnus-summary-refer-references ()
7332   "Fetch all articles mentioned in the References header.
7333 Return the number of articles fetched."
7334   (interactive)
7335   (let ((ref (mail-header-references (gnus-summary-article-header)))
7336         (current (gnus-summary-article-number))
7337         (n 0))
7338     (if (or (not ref)
7339             (equal ref ""))
7340         (error "No References in the current article")
7341       ;; For each Message-ID in the References header...
7342       (while (string-match "<[^>]*>" ref)
7343         (incf n)
7344         ;; ... fetch that article.
7345         (gnus-summary-refer-article
7346          (prog1 (match-string 0 ref)
7347            (setq ref (substring ref (match-end 0))))))
7348       (gnus-summary-goto-subject current)
7349       (gnus-summary-position-point)
7350       n)))
7351
7352 (defun gnus-summary-refer-thread (&optional limit)
7353   "Fetch all articles in the current thread.
7354 If LIMIT (the numerical prefix), fetch that many old headers instead
7355 of what's specified by the `gnus-refer-thread-limit' variable."
7356   (interactive "P")
7357   (let ((id (mail-header-id (gnus-summary-article-header)))
7358         (limit (if limit (prefix-numeric-value limit)
7359                  gnus-refer-thread-limit)))
7360     ;; We want to fetch LIMIT *old* headers, but we also have to
7361     ;; re-fetch all the headers in the current buffer, because many of
7362     ;; them may be undisplayed.  So we adjust LIMIT.
7363     (when (numberp limit)
7364       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7365     (unless (eq gnus-fetch-old-headers 'invisible)
7366       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7367       ;; Retrieve the headers and read them in.
7368       (if (eq (gnus-retrieve-headers
7369                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7370               'nov)
7371           (gnus-build-all-threads)
7372         (error "Can't fetch thread from backends that don't support NOV"))
7373       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7374     (gnus-summary-limit-include-thread id)))
7375
7376 (defun gnus-summary-refer-article (message-id)
7377   "Fetch an article specified by MESSAGE-ID."
7378   (interactive "sMessage-ID: ")
7379   (when (and (stringp message-id)
7380              (not (zerop (length message-id))))
7381     ;; Construct the correct Message-ID if necessary.
7382     ;; Suggested by tale@pawl.rpi.edu.
7383     (unless (string-match "^<" message-id)
7384       (setq message-id (concat "<" message-id)))
7385     (unless (string-match ">$" message-id)
7386       (setq message-id (concat message-id ">")))
7387     (let* ((header (gnus-id-to-header message-id))
7388            (sparse (and header
7389                         (gnus-summary-article-sparse-p
7390                          (mail-header-number header))
7391                         (memq (mail-header-number header)
7392                               gnus-newsgroup-limit)))
7393            number)
7394       (cond
7395        ;; If the article is present in the buffer we just go to it.
7396        ((and header
7397              (or (not (gnus-summary-article-sparse-p
7398                        (mail-header-number header)))
7399                  sparse))
7400         (prog1
7401             (gnus-summary-goto-article
7402              (mail-header-number header) nil t)
7403           (when sparse
7404             (gnus-summary-update-article (mail-header-number header)))))
7405        (t
7406         ;; We fetch the article.
7407         (catch 'found
7408           (dolist (gnus-override-method (gnus-refer-article-methods))
7409             (gnus-check-server gnus-override-method)
7410             ;; Fetch the header, and display the article.
7411             (when (setq number (gnus-summary-insert-subject message-id))
7412               (gnus-summary-select-article nil nil nil number)
7413               (throw 'found t)))
7414           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7415
7416 (defun gnus-refer-article-methods ()
7417   "Return a list of referrable methods."
7418   (cond
7419    ;; No method, so we default to current and native.
7420    ((null gnus-refer-article-method)
7421     (list gnus-current-select-method gnus-select-method))
7422    ;; Current.
7423    ((eq 'current gnus-refer-article-method)
7424     (list gnus-current-select-method))
7425    ;; List of select methods.
7426    ((not (and (symbolp (car gnus-refer-article-method))
7427               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7428     (let (out)
7429       (dolist (method gnus-refer-article-method)
7430         (push (if (eq 'current method)
7431                   gnus-current-select-method
7432                 method)
7433               out))
7434       (nreverse out)))
7435    ;; One single select method.
7436    (t
7437     (list gnus-refer-article-method))))
7438
7439 (defun gnus-summary-edit-parameters ()
7440   "Edit the group parameters of the current group."
7441   (interactive)
7442   (gnus-group-edit-group gnus-newsgroup-name 'params))
7443
7444 (defun gnus-summary-customize-parameters ()
7445   "Customize the group parameters of the current group."
7446   (interactive)
7447   (gnus-group-customize gnus-newsgroup-name))
7448
7449 (defun gnus-summary-enter-digest-group (&optional force)
7450   "Enter an nndoc group based on the current article.
7451 If FORCE, force a digest interpretation.  If not, try
7452 to guess what the document format is."
7453   (interactive "P")
7454   (let ((conf gnus-current-window-configuration))
7455     (save-excursion
7456       (gnus-summary-select-article))
7457     (setq gnus-current-window-configuration conf)
7458     (let* ((name (format "%s-%d"
7459                          (gnus-group-prefixed-name
7460                           gnus-newsgroup-name (list 'nndoc ""))
7461                          (save-excursion
7462                            (set-buffer gnus-summary-buffer)
7463                            gnus-current-article)))
7464            (ogroup gnus-newsgroup-name)
7465            (params (append (gnus-info-params (gnus-get-info ogroup))
7466                            (list (cons 'to-group ogroup))
7467                            (list (cons 'save-article-group ogroup))))
7468            (case-fold-search t)
7469            (buf (current-buffer))
7470            dig to-address)
7471       (save-excursion
7472         (set-buffer gnus-original-article-buffer)
7473         ;; Have the digest group inherit the main mail address of
7474         ;; the parent article.
7475         (when (setq to-address (or (message-fetch-field "reply-to")
7476                                    (message-fetch-field "from")))
7477           (setq params (append
7478                         (list (cons 'to-address
7479                                     (funcall gnus-decode-encoded-word-function
7480                                              to-address))))))
7481         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7482         (insert-buffer-substring gnus-original-article-buffer)
7483         ;; Remove lines that may lead nndoc to misinterpret the
7484         ;; document type.
7485         (narrow-to-region
7486          (goto-char (point-min))
7487          (or (search-forward "\n\n" nil t) (point)))
7488         (goto-char (point-min))
7489         (delete-matching-lines "^Path:\\|^From ")
7490         (widen))
7491       (unwind-protect
7492           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7493                     (gnus-newsgroup-ephemeral-ignored-charsets
7494                      gnus-newsgroup-ignored-charsets))
7495                 (gnus-group-read-ephemeral-group
7496                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7497                               (nndoc-article-type
7498                                ,(if force 'mbox 'guess))) t))
7499             ;; Make all postings to this group go to the parent group.
7500               (nconc (gnus-info-params (gnus-get-info name))
7501                      params)
7502             ;; Couldn't select this doc group.
7503             (switch-to-buffer buf)
7504             (gnus-set-global-variables)
7505             (gnus-configure-windows 'summary)
7506             (gnus-message 3 "Article couldn't be entered?"))
7507         (kill-buffer dig)))))
7508
7509 (defun gnus-summary-read-document (n)
7510   "Open a new group based on the current article(s).
7511 This will allow you to read digests and other similar
7512 documents as newsgroups.
7513 Obeys the standard process/prefix convention."
7514   (interactive "P")
7515   (let* ((articles (gnus-summary-work-articles n))
7516          (ogroup gnus-newsgroup-name)
7517          (params (append (gnus-info-params (gnus-get-info ogroup))
7518                          (list (cons 'to-group ogroup))))
7519          article group egroup groups vgroup)
7520     (while (setq article (pop articles))
7521       (setq group (format "%s-%d" gnus-newsgroup-name article))
7522       (gnus-summary-remove-process-mark article)
7523       (when (gnus-summary-display-article article)
7524         (save-excursion
7525           (with-temp-buffer
7526             (insert-buffer-substring gnus-original-article-buffer)
7527             ;; Remove some headers that may lead nndoc to make
7528             ;; the wrong guess.
7529             (message-narrow-to-head)
7530             (goto-char (point-min))
7531             (delete-matching-lines "^\\(Path\\):\\|^From ")
7532             (widen)
7533             (if (setq egroup
7534                       (gnus-group-read-ephemeral-group
7535                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7536                                      (nndoc-article-type guess))
7537                        t nil t))
7538                 (progn
7539             ;; Make all postings to this group go to the parent group.
7540                   (nconc (gnus-info-params (gnus-get-info egroup))
7541                          params)
7542                   (push egroup groups))
7543               ;; Couldn't select this doc group.
7544               (gnus-error 3 "Article couldn't be entered"))))))
7545     ;; Now we have selected all the documents.
7546     (cond
7547      ((not groups)
7548       (error "None of the articles could be interpreted as documents"))
7549      ((gnus-group-read-ephemeral-group
7550        (setq vgroup (format
7551                      "nnvirtual:%s-%s" gnus-newsgroup-name
7552                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7553        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7554        t
7555        (cons (current-buffer) 'summary)))
7556      (t
7557       (error "Couldn't select virtual nndoc group")))))
7558
7559 (defun gnus-summary-isearch-article (&optional regexp-p)
7560   "Do incremental search forward on the current article.
7561 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7562   (interactive "P")
7563   (gnus-summary-select-article)
7564   (gnus-configure-windows 'article)
7565   (gnus-eval-in-buffer-window gnus-article-buffer
7566     (save-restriction
7567       (widen)
7568       (isearch-forward regexp-p))))
7569
7570 (defun gnus-summary-search-article-forward (regexp &optional backward)
7571   "Search for an article containing REGEXP forward.
7572 If BACKWARD, search backward instead."
7573   (interactive
7574    (list (read-string
7575           (format "Search article %s (regexp%s): "
7576                   (if current-prefix-arg "backward" "forward")
7577                   (if gnus-last-search-regexp
7578                       (concat ", default " gnus-last-search-regexp)
7579                     "")))
7580          current-prefix-arg))
7581   (if (string-equal regexp "")
7582       (setq regexp (or gnus-last-search-regexp ""))
7583     (setq gnus-last-search-regexp regexp)
7584     (setq gnus-article-before-search gnus-current-article))
7585   ;; Intentionally set gnus-last-article.
7586   (setq gnus-last-article gnus-article-before-search)
7587   (let ((gnus-last-article gnus-last-article))
7588     (if (gnus-summary-search-article regexp backward)
7589         (gnus-summary-show-thread)
7590       (error "Search failed: \"%s\"" regexp))))
7591
7592 (defun gnus-summary-search-article-backward (regexp)
7593   "Search for an article containing REGEXP backward."
7594   (interactive
7595    (list (read-string
7596           (format "Search article backward (regexp%s): "
7597                   (if gnus-last-search-regexp
7598                       (concat ", default " gnus-last-search-regexp)
7599                     "")))))
7600   (gnus-summary-search-article-forward regexp 'backward))
7601
7602 (defun gnus-summary-search-article (regexp &optional backward)
7603   "Search for an article containing REGEXP.
7604 Optional argument BACKWARD means do search for backward.
7605 `gnus-select-article-hook' is not called during the search."
7606   ;; We have to require this here to make sure that the following
7607   ;; dynamic binding isn't shadowed by autoloading.
7608   (require 'gnus-async)
7609   (require 'gnus-art)
7610   (let ((gnus-select-article-hook nil)  ;Disable hook.
7611         (gnus-article-prepare-hook nil)
7612         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7613         (gnus-use-article-prefetch nil)
7614         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7615         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7616         (sum (current-buffer))
7617         (gnus-display-mime-function nil)
7618         (found nil)
7619         point)
7620     (gnus-save-hidden-threads
7621       (gnus-summary-select-article)
7622       (set-buffer gnus-article-buffer)
7623       (goto-char (window-point (get-buffer-window (current-buffer))))
7624       (when backward
7625         (forward-line -1))
7626       (while (not found)
7627         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7628         (if (if backward
7629                 (re-search-backward regexp nil t)
7630               (re-search-forward regexp nil t))
7631             ;; We found the regexp.
7632             (progn
7633               (setq found 'found)
7634               (beginning-of-line)
7635               (set-window-start
7636                (get-buffer-window (current-buffer))
7637                (point))
7638               (forward-line 1)
7639               (set-window-point
7640                (get-buffer-window (current-buffer))
7641                (point))
7642               (set-buffer sum)
7643               (setq point (point)))
7644           ;; We didn't find it, so we go to the next article.
7645           (set-buffer sum)
7646           (setq found 'not)
7647           (while (eq found 'not)
7648             (if (not (if backward (gnus-summary-find-prev)
7649                        (gnus-summary-find-next)))
7650                 ;; No more articles.
7651                 (setq found t)
7652               ;; Select the next article and adjust point.
7653               (unless (gnus-summary-article-sparse-p
7654                        (gnus-summary-article-number))
7655                 (setq found nil)
7656                 (gnus-summary-select-article)
7657                 (set-buffer gnus-article-buffer)
7658                 (widen)
7659                 (goto-char (if backward (point-max) (point-min))))))))
7660       (gnus-message 7 ""))
7661     ;; Return whether we found the regexp.
7662     (when (eq found 'found)
7663       (goto-char point)
7664       (gnus-summary-show-thread)
7665       (gnus-summary-goto-subject gnus-current-article)
7666       (gnus-summary-position-point)
7667       t)))
7668
7669 (defun gnus-find-matching-articles (header regexp)
7670   "Return a list of all articles that match REGEXP on HEADER.
7671 This search includes all articles in the current group that Gnus has
7672 fetched headers for, whether they are displayed or not."
7673   (let ((articles nil)
7674         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7675         (case-fold-search t))
7676     (dolist (header gnus-newsgroup-headers)
7677       (when (string-match regexp (funcall func header))
7678         (push (mail-header-number header) articles)))
7679     (nreverse articles)))
7680
7681 (defun gnus-summary-find-matching (header regexp &optional backward unread
7682                                           not-case-fold)
7683   "Return a list of all articles that match REGEXP on HEADER.
7684 The search stars on the current article and goes forwards unless
7685 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7686 If UNREAD is non-nil, only unread articles will
7687 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7688 in the comparisons."
7689   (let ((case-fold-search (not not-case-fold))
7690         articles d func)
7691     (if (consp header)
7692         (if (eq (car header) 'extra)
7693             (setq func
7694                   `(lambda (h)
7695                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7696                          "")))
7697           (error "%s is an invalid header" header))
7698       (unless (fboundp (intern (concat "mail-header-" header)))
7699         (error "%s is not a valid header" header))
7700       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7701     (dolist (d (if (eq backward 'all)
7702                    gnus-newsgroup-data
7703                  (gnus-data-find-list
7704                   (gnus-summary-article-number)
7705                   (gnus-data-list backward))))
7706       (when (and (or (not unread)       ; We want all articles...
7707                      (gnus-data-unread-p d)) ; Or just unreads.
7708                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7709                  (string-match regexp
7710                                (funcall func (gnus-data-header d)))) ; Match.
7711         (push (gnus-data-number d) articles))) ; Success!
7712     (nreverse articles)))
7713
7714 (defun gnus-summary-execute-command (header regexp command &optional backward)
7715   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7716 If HEADER is an empty string (or nil), the match is done on the entire
7717 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7718   (interactive
7719    (list (let ((completion-ignore-case t))
7720            (completing-read
7721             "Header name: "
7722             (mapcar (lambda (header) (list (format "%s" header)))
7723                     (append
7724                      '("Number" "Subject" "From" "Lines" "Date"
7725                        "Message-ID" "Xref" "References" "Body")
7726                      gnus-extra-headers))
7727             nil 'require-match))
7728          (read-string "Regexp: ")
7729          (read-key-sequence "Command: ")
7730          current-prefix-arg))
7731   (when (equal header "Body")
7732     (setq header ""))
7733   ;; Hidden thread subtrees must be searched as well.
7734   (gnus-summary-show-all-threads)
7735   ;; We don't want to change current point nor window configuration.
7736   (save-excursion
7737     (save-window-excursion
7738       (gnus-message 6 "Executing %s..." (key-description command))
7739 ;; We'd like to execute COMMAND interactively so as to give arguments.
7740       (gnus-execute header regexp
7741                     `(call-interactively ',(key-binding command))
7742                     backward)
7743       (gnus-message 6 "Executing %s...done" (key-description command)))))
7744
7745 (defun gnus-summary-beginning-of-article ()
7746   "Scroll the article back to the beginning."
7747   (interactive)
7748   (gnus-summary-select-article)
7749   (gnus-configure-windows 'article)
7750   (gnus-eval-in-buffer-window gnus-article-buffer
7751     (widen)
7752     (goto-char (point-min))
7753     (when gnus-page-broken
7754       (gnus-narrow-to-page))))
7755
7756 (defun gnus-summary-end-of-article ()
7757   "Scroll to the end of the article."
7758   (interactive)
7759   (gnus-summary-select-article)
7760   (gnus-configure-windows 'article)
7761   (gnus-eval-in-buffer-window gnus-article-buffer
7762     (widen)
7763     (goto-char (point-max))
7764     (recenter -3)
7765     (when gnus-page-broken
7766       (gnus-narrow-to-page))))
7767
7768 (defun gnus-summary-print-article (&optional filename n)
7769   "Generate and print a PostScript image of the N next (mail) articles.
7770
7771 If N is negative, print the N previous articles.  If N is nil and articles
7772 have been marked with the process mark, print these instead.
7773
7774 If the optional first argument FILENAME is nil, send the image to the
7775 printer.  If FILENAME is a string, save the PostScript image in a file with
7776 that name.  If FILENAME is a number, prompt the user for the name of the file
7777 to save in."
7778   (interactive (list (ps-print-preprint current-prefix-arg)))
7779   (dolist (article (gnus-summary-work-articles n))
7780     (gnus-summary-select-article nil nil 'pseudo article)
7781     (gnus-eval-in-buffer-window gnus-article-buffer
7782       (let ((buffer (generate-new-buffer " *print*")))
7783         (unwind-protect
7784             (progn
7785               (copy-to-buffer buffer (point-min) (point-max))
7786               (set-buffer buffer)
7787               (gnus-article-delete-invisible-text)
7788               (when (gnus-visual-p 'article-highlight 'highlight)
7789                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7790                 ;; highlight.
7791                 (let ((gnus-article-buffer buffer))
7792                   (gnus-article-highlight-citation t)
7793                   (gnus-article-highlight-signature)))
7794               (let ((ps-left-header
7795                      (list
7796                       (concat "("
7797                               (mail-header-subject gnus-current-headers) ")")
7798                       (concat "("
7799                               (mail-header-from gnus-current-headers) ")")))
7800                     (ps-right-header
7801                      (list
7802                       "/pagenumberstring load"
7803                       (concat "("
7804                               (mail-header-date gnus-current-headers) ")"))))
7805                 (gnus-run-hooks 'gnus-ps-print-hook)
7806                 (save-excursion
7807                   (if window-system
7808                       (ps-spool-buffer-with-faces)
7809                     (ps-spool-buffer)))))
7810           (kill-buffer buffer))))
7811     (gnus-summary-remove-process-mark article))
7812   (ps-despool filename))
7813
7814 (defun gnus-summary-show-article (&optional arg)
7815   "Force re-fetching of the current article.
7816 If ARG (the prefix) is a number, show the article with the charset
7817 defined in `gnus-summary-show-article-charset-alist', or the charset
7818 input.
7819 If ARG (the prefix) is non-nil and not a number, show the raw article
7820 without any article massaging functions being run."
7821   (interactive "P")
7822   (cond
7823    ((numberp arg)
7824     (gnus-summary-show-article t)
7825     (let ((gnus-newsgroup-charset
7826            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7827                (mm-read-coding-system
7828                 "View as charset: "
7829                 (save-excursion
7830                   (set-buffer gnus-article-buffer)
7831                   (let ((coding-systems
7832                          (detect-coding-region (point) (point-max))))
7833                     (or (car-safe coding-systems)
7834                         coding-systems))))))
7835           (gnus-newsgroup-ignored-charsets 'gnus-all))
7836       (gnus-summary-select-article nil 'force)
7837       (let ((deps gnus-newsgroup-dependencies)
7838             head header)
7839         (save-excursion
7840           (set-buffer gnus-original-article-buffer)
7841           (save-restriction
7842             (message-narrow-to-head)
7843             (setq head (buffer-string)))
7844           (with-temp-buffer
7845             (insert (format "211 %d Article retrieved.\n"
7846                             (cdr gnus-article-current)))
7847             (insert head)
7848             (insert ".\n")
7849             (let ((nntp-server-buffer (current-buffer)))
7850               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7851         (gnus-data-set-header
7852          (gnus-data-find (cdr gnus-article-current))
7853          header)
7854         (gnus-summary-update-article-line
7855          (cdr gnus-article-current) header)
7856         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7857           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7858    ((not arg)
7859     ;; Select the article the normal way.
7860     (gnus-summary-select-article nil 'force))
7861    (t
7862     ;; We have to require this here to make sure that the following
7863     ;; dynamic binding isn't shadowed by autoloading.
7864     (require 'gnus-async)
7865     (require 'gnus-art)
7866     ;; Bind the article treatment functions to nil.
7867     (let ((gnus-have-all-headers t)
7868           gnus-article-prepare-hook
7869           gnus-article-decode-hook
7870           gnus-display-mime-function
7871           gnus-break-pages)
7872       ;; Destroy any MIME parts.
7873       (when (gnus-buffer-live-p gnus-article-buffer)
7874         (save-excursion
7875           (set-buffer gnus-article-buffer)
7876           (mm-destroy-parts gnus-article-mime-handles)
7877           ;; Set it to nil for safety reason.
7878           (setq gnus-article-mime-handle-alist nil)
7879           (setq gnus-article-mime-handles nil)))
7880       (gnus-summary-select-article nil 'force))))
7881   (gnus-summary-goto-subject gnus-current-article)
7882   (gnus-summary-position-point))
7883
7884 (defun gnus-summary-show-raw-article ()
7885   "Show the raw article without any article massaging functions being run."
7886   (interactive)
7887   (gnus-summary-show-article t))
7888
7889 (defun gnus-summary-verbose-headers (&optional arg)
7890   "Toggle permanent full header display.
7891 If ARG is a positive number, turn header display on.
7892 If ARG is a negative number, turn header display off."
7893   (interactive "P")
7894   (setq gnus-show-all-headers
7895         (cond ((or (not (numberp arg))
7896                    (zerop arg))
7897                (not gnus-show-all-headers))
7898               ((natnump arg)
7899                t)))
7900   (gnus-summary-show-article))
7901
7902 (defun gnus-summary-toggle-header (&optional arg)
7903   "Show the headers if they are hidden, or hide them if they are shown.
7904 If ARG is a positive number, show the entire header.
7905 If ARG is a negative number, hide the unwanted header lines."
7906   (interactive "P")
7907   (save-excursion
7908     (set-buffer gnus-article-buffer)
7909     (save-restriction
7910       (let* ((buffer-read-only nil)
7911              (inhibit-point-motion-hooks t)
7912              hidden e)
7913         (setq hidden
7914               (if (numberp arg)
7915                   (>= arg 0)
7916                 (save-restriction
7917                   (article-narrow-to-head)
7918                   (gnus-article-hidden-text-p 'headers))))
7919         (goto-char (point-min))
7920         (when (search-forward "\n\n" nil t)
7921           (delete-region (point-min) (1- (point))))
7922         (goto-char (point-min))
7923         (save-excursion
7924           (set-buffer gnus-original-article-buffer)
7925           (goto-char (point-min))
7926           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7927         (insert-buffer-substring gnus-original-article-buffer 1 e)
7928         (save-restriction
7929           (narrow-to-region (point-min) (point))
7930           (article-decode-encoded-words)
7931           (if  hidden
7932               (let ((gnus-treat-hide-headers nil)
7933                     (gnus-treat-hide-boring-headers nil))
7934                 (setq gnus-article-wash-types
7935                       (delq 'headers gnus-article-wash-types))
7936                 (gnus-treat-article 'head))
7937             (gnus-treat-article 'head)))
7938         (gnus-set-mode-line 'article)))))
7939
7940 (defun gnus-summary-show-all-headers ()
7941   "Make all header lines visible."
7942   (interactive)
7943   (gnus-summary-toggle-header 1))
7944
7945 (defun gnus-summary-caesar-message (&optional arg)
7946   "Caesar rotate the current article by 13.
7947 The numerical prefix specifies how many places to rotate each letter
7948 forward."
7949   (interactive "P")
7950   (gnus-summary-select-article)
7951   (let ((mail-header-separator ""))
7952     (gnus-eval-in-buffer-window gnus-article-buffer
7953       (save-restriction
7954         (widen)
7955         (let ((start (window-start))
7956               buffer-read-only)
7957           (message-caesar-buffer-body arg)
7958           (set-window-start (get-buffer-window (current-buffer)) start))))))
7959
7960 (defun gnus-summary-stop-page-breaking ()
7961   "Stop page breaking in the current article."
7962   (interactive)
7963   (gnus-summary-select-article)
7964   (gnus-eval-in-buffer-window gnus-article-buffer
7965     (widen)
7966     (when (gnus-visual-p 'page-marker)
7967       (let ((buffer-read-only nil))
7968         (gnus-remove-text-with-property 'gnus-prev)
7969         (gnus-remove-text-with-property 'gnus-next))
7970       (setq gnus-page-broken nil))))
7971
7972 (defun gnus-summary-move-article (&optional n to-newsgroup
7973                                             select-method action)
7974   "Move the current article to a different newsgroup.
7975 If N is a positive number, move the N next articles.
7976 If N is a negative number, move the N previous articles.
7977 If N is nil and any articles have been marked with the process mark,
7978 move those articles instead.
7979 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7980 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7981 re-spool using this method.
7982
7983 For this function to work, both the current newsgroup and the
7984 newsgroup that you want to move to have to support the `request-move'
7985 and `request-accept' functions.
7986
7987 ACTION can be either `move' (the default), `crosspost' or `copy'."
7988   (interactive "P")
7989   (unless action
7990     (setq action 'move))
7991   ;; Check whether the source group supports the required functions.
7992   (cond ((and (eq action 'move)
7993               (not (gnus-check-backend-function
7994                     'request-move-article gnus-newsgroup-name)))
7995          (error "The current group does not support article moving"))
7996         ((and (eq action 'crosspost)
7997               (not (gnus-check-backend-function
7998                     'request-replace-article gnus-newsgroup-name)))
7999          (error "The current group does not support article editing")))
8000   (let ((articles (gnus-summary-work-articles n))
8001         (prefix (if (gnus-check-backend-function
8002                      'request-move-article gnus-newsgroup-name)
8003                     (gnus-group-real-prefix gnus-newsgroup-name)
8004                   ""))
8005         (names '((move "Move" "Moving")
8006                  (copy "Copy" "Copying")
8007                  (crosspost "Crosspost" "Crossposting")))
8008         (copy-buf (save-excursion
8009                     (nnheader-set-temp-buffer " *copy article*")))
8010         art-group to-method new-xref article to-groups)
8011     (unless (assq action names)
8012       (error "Unknown action %s" action))
8013     ;; Read the newsgroup name.
8014     (when (and (not to-newsgroup)
8015                (not select-method))
8016       (setq to-newsgroup
8017             (gnus-read-move-group-name
8018              (cadr (assq action names))
8019              (symbol-value (intern (format "gnus-current-%s-group" action)))
8020              articles prefix))
8021       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8022     (setq to-method (or select-method
8023                         (gnus-server-to-method
8024                          (gnus-group-method to-newsgroup))))
8025     ;; Check the method we are to move this article to...
8026     (unless (gnus-check-backend-function
8027              'request-accept-article (car to-method))
8028       (error "%s does not support article copying" (car to-method)))
8029     (unless (gnus-check-server to-method)
8030       (error "Can't open server %s" (car to-method)))
8031     (gnus-message 6 "%s to %s: %s..."
8032                   (caddr (assq action names))
8033                   (or (car select-method) to-newsgroup) articles)
8034     (while articles
8035       (setq article (pop articles))
8036       (setq
8037        art-group
8038        (cond
8039         ;; Move the article.
8040         ((eq action 'move)
8041          ;; Remove this article from future suppression.
8042          (gnus-dup-unsuppress-article article)
8043          (gnus-request-move-article
8044           article                       ; Article to move
8045           gnus-newsgroup-name           ; From newsgroup
8046           (nth 1 (gnus-find-method-for-group
8047                   gnus-newsgroup-name)) ; Server
8048           (list 'gnus-request-accept-article
8049                 to-newsgroup (list 'quote select-method)
8050                 (not articles) t)       ; Accept form
8051           (not articles)))              ; Only save nov last time
8052         ;; Copy the article.
8053         ((eq action 'copy)
8054          (save-excursion
8055            (set-buffer copy-buf)
8056            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8057              (gnus-request-accept-article
8058               to-newsgroup select-method (not articles) t))))
8059         ;; Crosspost the article.
8060         ((eq action 'crosspost)
8061          (let ((xref (message-tokenize-header
8062                       (mail-header-xref (gnus-summary-article-header article))
8063                       " ")))
8064            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8065                                   ":" article))
8066            (unless xref
8067              (setq xref (list (system-name))))
8068            (setq new-xref
8069                  (concat
8070                   (mapconcat 'identity
8071                              (delete "Xref:" (delete new-xref xref))
8072                              " ")
8073                   " " new-xref))
8074            (save-excursion
8075              (set-buffer copy-buf)
8076              ;; First put the article in the destination group.
8077              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8078              (when (consp (setq art-group
8079                                 (gnus-request-accept-article
8080                                  to-newsgroup select-method (not articles))))
8081                (setq new-xref (concat new-xref " " (car art-group)
8082                                       ":" (cdr art-group)))
8083                ;; Now we have the new Xrefs header, so we insert
8084                ;; it and replace the new article.
8085                (nnheader-replace-header "Xref" new-xref)
8086                (gnus-request-replace-article
8087                 (cdr art-group) to-newsgroup (current-buffer))
8088                art-group))))))
8089       (cond
8090        ((not art-group)
8091         (gnus-message 1 "Couldn't %s article %s: %s"
8092                       (cadr (assq action names)) article
8093                       (nnheader-get-report (car to-method))))
8094        ((eq art-group 'junk)
8095         (when (eq action 'move)
8096           (gnus-summary-mark-article article gnus-canceled-mark)
8097           (gnus-message 4 "Deleted article %s" article)))
8098        (t
8099         (let* ((pto-group (gnus-group-prefixed-name
8100                            (car art-group) to-method))
8101                (entry
8102                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8103                (info (nth 2 entry))
8104                (to-group (gnus-info-group info))
8105                to-marks)
8106           ;; Update the group that has been moved to.
8107           (when (and info
8108                      (memq action '(move copy)))
8109             (unless (member to-group to-groups)
8110               (push to-group to-groups))
8111
8112             (unless (memq article gnus-newsgroup-unreads)
8113               (push 'read to-marks)
8114               (gnus-info-set-read
8115                info (gnus-add-to-range (gnus-info-read info)
8116                                        (list (cdr art-group)))))
8117
8118             ;; See whether the article is to be put in the cache.
8119             (let ((marks gnus-article-mark-lists)
8120                   (to-article (cdr art-group)))
8121
8122               ;; Enter the article into the cache in the new group,
8123               ;; if that is required.
8124               (when gnus-use-cache
8125                 (gnus-cache-possibly-enter-article
8126                  to-group to-article
8127                  (memq article gnus-newsgroup-marked)
8128                  (memq article gnus-newsgroup-dormant)
8129                  (memq article gnus-newsgroup-unreads)))
8130
8131               (when gnus-preserve-marks
8132                 ;; Copy any marks over to the new group.
8133                 (when (and (equal to-group gnus-newsgroup-name)
8134                            (not (memq article gnus-newsgroup-unreads)))
8135                   ;; Mark this article as read in this group.
8136                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8137                   (setcdr (gnus-active to-group) to-article)
8138                   (setcdr gnus-newsgroup-active to-article))
8139
8140                 (while marks
8141                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8142                     (when (memq article (symbol-value
8143                                          (intern (format "gnus-newsgroup-%s"
8144                                                          (caar marks)))))
8145                       (push (cdar marks) to-marks)
8146                       ;; If the other group is the same as this group,
8147                       ;; then we have to add the mark to the list.
8148                       (when (equal to-group gnus-newsgroup-name)
8149                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8150                              (cons to-article
8151                                    (symbol-value
8152                                     (intern (format "gnus-newsgroup-%s"
8153                                                     (caar marks)))))))
8154                       ;; Copy the marks to other group.
8155                       (gnus-add-marked-articles
8156                        to-group (cdar marks) (list to-article) info)))
8157                   (setq marks (cdr marks)))
8158
8159                 (gnus-request-set-mark to-group (list (list (list to-article)
8160                                                             'add
8161                                                             to-marks))))
8162
8163               (gnus-dribble-enter
8164                (concat "(gnus-group-set-info '"
8165                        (gnus-prin1-to-string (gnus-get-info to-group))
8166                        ")"))))
8167
8168           ;; Update the Xref header in this article to point to
8169           ;; the new crossposted article we have just created.
8170           (when (eq action 'crosspost)
8171             (save-excursion
8172               (set-buffer copy-buf)
8173               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8174               (nnheader-replace-header "Xref" new-xref)
8175               (gnus-request-replace-article
8176                article gnus-newsgroup-name (current-buffer)))))
8177
8178         ;;;!!!Why is this necessary?
8179         (set-buffer gnus-summary-buffer)
8180
8181         (gnus-summary-goto-subject article)
8182         (when (eq action 'move)
8183           (gnus-summary-mark-article article gnus-canceled-mark))))
8184       (gnus-summary-remove-process-mark article))
8185     ;; Re-activate all groups that have been moved to.
8186     (save-excursion
8187       (set-buffer gnus-group-buffer)
8188       (let ((gnus-group-marked to-groups))
8189         (gnus-group-get-new-news-this-group nil t)))
8190
8191     (gnus-kill-buffer copy-buf)
8192     (gnus-summary-position-point)
8193     (gnus-set-mode-line 'summary)))
8194
8195 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8196   "Move the current article to a different newsgroup.
8197 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8198 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8199 re-spool using this method."
8200   (interactive "P")
8201   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8202
8203 (defun gnus-summary-crosspost-article (&optional n)
8204   "Crosspost the current article to some other group."
8205   (interactive "P")
8206   (gnus-summary-move-article n nil nil 'crosspost))
8207
8208 (defcustom gnus-summary-respool-default-method nil
8209   "Default method type for respooling an article.
8210 If nil, use to the current newsgroup method."
8211   :type 'symbol
8212   :group 'gnus-summary-mail)
8213
8214 (defun gnus-summary-respool-article (&optional n method)
8215   "Respool the current article.
8216 The article will be squeezed through the mail spooling process again,
8217 which means that it will be put in some mail newsgroup or other
8218 depending on `nnmail-split-methods'.
8219 If N is a positive number, respool the N next articles.
8220 If N is a negative number, respool the N previous articles.
8221 If N is nil and any articles have been marked with the process mark,
8222 respool those articles instead.
8223
8224 Respooling can be done both from mail groups and \"real\" newsgroups.
8225 In the former case, the articles in question will be moved from the
8226 current group into whatever groups they are destined to.  In the
8227 latter case, they will be copied into the relevant groups."
8228   (interactive
8229    (list current-prefix-arg
8230          (let* ((methods (gnus-methods-using 'respool))
8231                 (methname
8232                  (symbol-name (or gnus-summary-respool-default-method
8233                                   (car (gnus-find-method-for-group
8234                                         gnus-newsgroup-name)))))
8235                 (method
8236                  (gnus-completing-read
8237                   methname "What backend do you want to use when respooling?"
8238                   methods nil t nil 'gnus-mail-method-history))
8239                 ms)
8240            (cond
8241             ((zerop (length (setq ms (gnus-servers-using-backend
8242                                       (intern method)))))
8243              (list (intern method) ""))
8244             ((= 1 (length ms))
8245              (car ms))
8246             (t
8247              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8248                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8249                            ms-alist))))))))
8250   (unless method
8251     (error "No method given for respooling"))
8252   (if (assoc (symbol-name
8253               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8254              (gnus-methods-using 'respool))
8255       (gnus-summary-move-article n nil method)
8256     (gnus-summary-copy-article n nil method)))
8257
8258 (defun gnus-summary-import-article (file &optional edit)
8259   "Import an arbitrary file into a mail newsgroup."
8260   (interactive "fImport file: \nP")
8261   (let ((group gnus-newsgroup-name)
8262         (now (current-time))
8263         atts lines group-art)
8264     (unless (gnus-check-backend-function 'request-accept-article group)
8265       (error "%s does not support article importing" group))
8266     (or (file-readable-p file)
8267         (not (file-regular-p file))
8268         (error "Can't read %s" file))
8269     (save-excursion
8270       (set-buffer (gnus-get-buffer-create " *import file*"))
8271       (erase-buffer)
8272       (nnheader-insert-file-contents file)
8273       (goto-char (point-min))
8274       (if (nnheader-article-p)
8275           (save-restriction
8276             (goto-char (point-min))
8277             (search-forward "\n\n" nil t)
8278             (narrow-to-region (point-min) (1- (point)))
8279             (goto-char (point-min))
8280             (unless (re-search-forward "^date:" nil t)
8281               (goto-char (point-max))
8282               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8283        ;; This doesn't look like an article, so we fudge some headers.
8284         (setq atts (file-attributes file)
8285               lines (count-lines (point-min) (point-max)))
8286         (insert "From: " (read-string "From: ") "\n"
8287                 "Subject: " (read-string "Subject: ") "\n"
8288                 "Date: " (message-make-date (nth 5 atts)) "\n"
8289                 "Message-ID: " (message-make-message-id) "\n"
8290                 "Lines: " (int-to-string lines) "\n"
8291                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8292       (setq group-art (gnus-request-accept-article group nil t))
8293       (kill-buffer (current-buffer)))
8294     (setq gnus-newsgroup-active (gnus-activate-group group))
8295     (forward-line 1)
8296     (gnus-summary-goto-article (cdr group-art) nil t)
8297     (when edit
8298       (gnus-summary-edit-article))))
8299
8300 (defun gnus-summary-create-article ()
8301   "Create an article in a mail newsgroup."
8302   (interactive)
8303   (let ((group gnus-newsgroup-name)
8304         (now (current-time))
8305         group-art)
8306     (unless (gnus-check-backend-function 'request-accept-article group)
8307       (error "%s does not support article importing" group))
8308     (save-excursion
8309       (set-buffer (gnus-get-buffer-create " *import file*"))
8310       (erase-buffer)
8311       (goto-char (point-min))
8312       ;; This doesn't look like an article, so we fudge some headers.
8313       (insert "From: " (read-string "From: ") "\n"
8314               "Subject: " (read-string "Subject: ") "\n"
8315               "Date: " (message-make-date now) "\n"
8316               "Message-ID: " (message-make-message-id) "\n")
8317       (setq group-art (gnus-request-accept-article group nil t))
8318       (kill-buffer (current-buffer)))
8319     (setq gnus-newsgroup-active (gnus-activate-group group))
8320     (forward-line 1)
8321     (gnus-summary-goto-article (cdr group-art) nil t)
8322     (gnus-summary-edit-article)))
8323
8324 (defun gnus-summary-article-posted-p ()
8325   "Say whether the current (mail) article is available from news as well.
8326 This will be the case if the article has both been mailed and posted."
8327   (interactive)
8328   (let ((id (mail-header-references (gnus-summary-article-header)))
8329         (gnus-override-method (car (gnus-refer-article-methods))))
8330     (if (gnus-request-head id "")
8331         (gnus-message 2 "The current message was found on %s"
8332                       gnus-override-method)
8333       (gnus-message 2 "The current message couldn't be found on %s"
8334                     gnus-override-method)
8335       nil)))
8336
8337 (defun gnus-summary-expire-articles (&optional now)
8338   "Expire all articles that are marked as expirable in the current group."
8339   (interactive)
8340   (when (gnus-check-backend-function
8341          'request-expire-articles gnus-newsgroup-name)
8342     ;; This backend supports expiry.
8343     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8344            (expirable (if total
8345                           (progn
8346                             ;; We need to update the info for
8347                             ;; this group for `gnus-list-of-read-articles'
8348                             ;; to give us the right answer.
8349                             (gnus-run-hooks 'gnus-exit-group-hook)
8350                             (gnus-summary-update-info)
8351                             (gnus-list-of-read-articles gnus-newsgroup-name))
8352                         (setq gnus-newsgroup-expirable
8353                               (sort gnus-newsgroup-expirable '<))))
8354            (expiry-wait (if now 'immediate
8355                           (gnus-group-find-parameter
8356                            gnus-newsgroup-name 'expiry-wait)))
8357            (nnmail-expiry-target
8358             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8359                 nnmail-expiry-target))
8360            es)
8361       (when expirable
8362         ;; There are expirable articles in this group, so we run them
8363         ;; through the expiry process.
8364         (gnus-message 6 "Expiring articles...")
8365         (unless (gnus-check-group gnus-newsgroup-name)
8366           (error "Can't open server for %s" gnus-newsgroup-name))
8367         ;; The list of articles that weren't expired is returned.
8368         (save-excursion
8369           (if expiry-wait
8370               (let ((nnmail-expiry-wait-function nil)
8371                     (nnmail-expiry-wait expiry-wait))
8372                 (setq es (gnus-request-expire-articles
8373                           expirable gnus-newsgroup-name)))
8374             (setq es (gnus-request-expire-articles
8375                       expirable gnus-newsgroup-name)))
8376           (unless total
8377             (setq gnus-newsgroup-expirable es))
8378           ;; We go through the old list of expirable, and mark all
8379           ;; really expired articles as nonexistent.
8380           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8381             (let ((gnus-use-cache nil))
8382               (while expirable
8383                 (unless (memq (car expirable) es)
8384                   (when (gnus-data-find (car expirable))
8385                     (gnus-summary-mark-article
8386                      (car expirable) gnus-canceled-mark)))
8387                 (setq expirable (cdr expirable))))))
8388         (gnus-message 6 "Expiring articles...done")))))
8389
8390 (defun gnus-summary-expire-articles-now ()
8391   "Expunge all expirable articles in the current group.
8392 This means that *all* articles that are marked as expirable will be
8393 deleted forever, right now."
8394   (interactive)
8395   (or gnus-expert-user
8396       (gnus-yes-or-no-p
8397        "Are you really, really, really sure you want to delete all these messages? ")
8398       (error "Phew!"))
8399   (gnus-summary-expire-articles t))
8400
8401 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8402 (defun gnus-summary-delete-article (&optional n)
8403   "Delete the N next (mail) articles.
8404 This command actually deletes articles.  This is not a marking
8405 command.  The article will disappear forever from your life, never to
8406 return.
8407 If N is negative, delete backwards.
8408 If N is nil and articles have been marked with the process mark,
8409 delete these instead."
8410   (interactive "P")
8411   (unless (gnus-check-backend-function 'request-expire-articles
8412                                        gnus-newsgroup-name)
8413     (error "The current newsgroup does not support article deletion"))
8414   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8415     (error "Couldn't open server"))
8416   ;; Compute the list of articles to delete.
8417   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8418         not-deleted)
8419     (if (and gnus-novice-user
8420              (not (gnus-yes-or-no-p
8421                    (format "Do you really want to delete %s forever? "
8422                            (if (> (length articles) 1)
8423                                (format "these %s articles" (length articles))
8424                              "this article")))))
8425         ()
8426       ;; Delete the articles.
8427       (setq not-deleted (gnus-request-expire-articles
8428                          articles gnus-newsgroup-name 'force))
8429       (while articles
8430         (gnus-summary-remove-process-mark (car articles))
8431         ;; The backend might not have been able to delete the article
8432         ;; after all.
8433         (unless (memq (car articles) not-deleted)
8434           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8435         (setq articles (cdr articles)))
8436       (when not-deleted
8437         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8438     (gnus-summary-position-point)
8439     (gnus-set-mode-line 'summary)
8440     not-deleted))
8441
8442 (defun gnus-summary-edit-article (&optional arg)
8443   "Edit the current article.
8444 This will have permanent effect only in mail groups.
8445 If ARG is nil, edit the decoded articles.
8446 If ARG is 1, edit the raw articles.
8447 If ARG is 2, edit the raw articles even in read-only groups.
8448 If ARG is 3, edit the articles with the current handles.
8449 Otherwise, allow editing of articles even in read-only
8450 groups."
8451   (interactive "P")
8452   (let (force raw current-handles)
8453     (cond
8454      ((null arg))
8455      ((eq arg 1) (setq raw t))
8456      ((eq arg 2) (setq raw t
8457                        force t))
8458      ((eq arg 3) (setq current-handles
8459                        (and (gnus-buffer-live-p gnus-article-buffer)
8460                             (with-current-buffer gnus-article-buffer
8461                               (prog1
8462                                   gnus-article-mime-handles
8463                                 (setq gnus-article-mime-handles nil))))))
8464      (t (setq force t)))
8465     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8466         (error "Can't edit the raw article in group nndraft:drafts"))
8467     (save-excursion
8468       (set-buffer gnus-summary-buffer)
8469       (let ((mail-parse-charset gnus-newsgroup-charset)
8470             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8471         (gnus-set-global-variables)
8472         (when (and (not force)
8473                    (gnus-group-read-only-p))
8474           (error "The current newsgroup does not support article editing"))
8475         (gnus-summary-show-article t)
8476         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8477           (with-current-buffer gnus-article-buffer
8478             (mm-enable-multibyte-mule4)))
8479         (if (equal gnus-newsgroup-name "nndraft:drafts")
8480             (setq raw t))
8481         (gnus-article-edit-article
8482          (if raw 'ignore
8483            `(lambda ()
8484               (let ((mbl mml-buffer-list))
8485                 (setq mml-buffer-list nil)
8486                 (mime-to-mml ,'current-handles)
8487                 (let ((mbl1 mml-buffer-list))
8488                   (setq mml-buffer-list mbl)
8489                   (set (make-local-variable 'mml-buffer-list) mbl1))
8490                 (make-local-hook 'kill-buffer-hook)
8491                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8492          `(lambda (no-highlight)
8493             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8494                   (message-options message-options)
8495                   (message-options-set-recipient)
8496                   (mail-parse-ignored-charsets
8497                    ',gnus-newsgroup-ignored-charsets))
8498               ,(if (not raw) '(progn
8499                                 (mml-to-mime)
8500                                 (mml-destroy-buffers)
8501                                 (remove-hook 'kill-buffer-hook
8502                                              'mml-destroy-buffers t)
8503                                 (kill-local-variable 'mml-buffer-list)))
8504               (gnus-summary-edit-article-done
8505                ,(or (mail-header-references gnus-current-headers) "")
8506                ,(gnus-group-read-only-p)
8507                ,gnus-summary-buffer no-highlight))))))))
8508
8509 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8510
8511 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8512                                                  no-highlight)
8513   "Make edits to the current article permanent."
8514   (interactive)
8515   (save-excursion
8516    ;; The buffer restriction contains the entire article if it exists.
8517     (when (article-goto-body)
8518       (let ((lines (count-lines (point) (point-max)))
8519             (length (- (point-max) (point)))
8520             (case-fold-search t)
8521             (body (copy-marker (point))))
8522         (goto-char (point-min))
8523         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8524           (delete-region (match-beginning 1) (match-end 1))
8525           (insert (number-to-string length)))
8526         (goto-char (point-min))
8527         (when (re-search-forward
8528                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8529           (delete-region (match-beginning 1) (match-end 1))
8530           (insert (number-to-string length)))
8531         (goto-char (point-min))
8532         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8533           (delete-region (match-beginning 1) (match-end 1))
8534           (insert (number-to-string lines))))))
8535   ;; Replace the article.
8536   (let ((buf (current-buffer)))
8537     (with-temp-buffer
8538       (insert-buffer-substring buf)
8539
8540       (if (and (not read-only)
8541                (not (gnus-request-replace-article
8542                      (cdr gnus-article-current) (car gnus-article-current)
8543                      (current-buffer) t)))
8544           (error "Couldn't replace article")
8545         ;; Update the summary buffer.
8546         (if (and references
8547                  (equal (message-tokenize-header references " ")
8548                         (message-tokenize-header
8549                          (or (message-fetch-field "references") "") " ")))
8550             ;; We only have to update this line.
8551             (save-excursion
8552               (save-restriction
8553                 (message-narrow-to-head)
8554                 (let ((head (buffer-string))
8555                       header)
8556                   (with-temp-buffer
8557                     (insert (format "211 %d Article retrieved.\n"
8558                                     (cdr gnus-article-current)))
8559                     (insert head)
8560                     (insert ".\n")
8561                     (let ((nntp-server-buffer (current-buffer)))
8562                       (setq header (car (gnus-get-newsgroup-headers
8563                                          (save-excursion
8564                                            (set-buffer gnus-summary-buffer)
8565                                            gnus-newsgroup-dependencies)
8566                                          t))))
8567                     (save-excursion
8568                       (set-buffer gnus-summary-buffer)
8569                       (gnus-data-set-header
8570                        (gnus-data-find (cdr gnus-article-current))
8571                        header)
8572                       (gnus-summary-update-article-line
8573                        (cdr gnus-article-current) header)
8574                       (if (gnus-summary-goto-subject
8575                            (cdr gnus-article-current) nil t)
8576                           (gnus-summary-update-secondary-mark
8577                            (cdr gnus-article-current))))))))
8578           ;; Update threads.
8579           (set-buffer (or buffer gnus-summary-buffer))
8580           (gnus-summary-update-article (cdr gnus-article-current))
8581           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8582               (gnus-summary-update-secondary-mark
8583                (cdr gnus-article-current))))
8584         ;; Prettify the article buffer again.
8585         (unless no-highlight
8586           (save-excursion
8587             (set-buffer gnus-article-buffer)
8588             ;;;!!! Fix this -- article should be rehighlighted.
8589             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8590             (set-buffer gnus-original-article-buffer)
8591             (gnus-request-article
8592              (cdr gnus-article-current)
8593              (car gnus-article-current) (current-buffer))))
8594         ;; Prettify the summary buffer line.
8595         (when (gnus-visual-p 'summary-highlight 'highlight)
8596           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8597
8598 (defun gnus-summary-edit-wash (key)
8599   "Perform editing command KEY in the article buffer."
8600   (interactive
8601    (list
8602     (progn
8603       (message "%s" (concat (this-command-keys) "- "))
8604       (read-char))))
8605   (message "")
8606   (gnus-summary-edit-article)
8607   (execute-kbd-macro (concat (this-command-keys) key))
8608   (gnus-article-edit-done))
8609
8610
8611 (defun gnus-summary-toggle-smiley (&optional arg)
8612   "Toggle the display of smilies as small graphical icons."
8613   (interactive "P")
8614   (save-excursion
8615     (set-buffer gnus-article-buffer)
8616     (gnus-smiley-display arg)
8617     ))
8618
8619 ;;; Respooling
8620
8621 (defun gnus-summary-respool-query (&optional silent trace)
8622   "Query where the respool algorithm would put this article."
8623   (interactive)
8624   (let (gnus-mark-article-hook)
8625     (gnus-summary-select-article)
8626     (save-excursion
8627       (set-buffer gnus-original-article-buffer)
8628       (save-restriction
8629         (message-narrow-to-head)
8630         (let ((groups (nnmail-article-group 'identity trace)))
8631           (unless silent
8632             (if groups
8633                 (message "This message would go to %s"
8634                          (mapconcat 'car groups ", "))
8635               (message "This message would go to no groups"))
8636             groups))))))
8637
8638 (defun gnus-summary-respool-trace ()
8639   "Trace where the respool algorithm would put this article.
8640 Display a buffer showing all fancy splitting patterns which matched."
8641   (interactive)
8642   (gnus-summary-respool-query nil t))
8643
8644 ;; Summary marking commands.
8645
8646 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8647   "Mark articles which has the same subject as read, and then select the next.
8648 If UNMARK is positive, remove any kind of mark.
8649 If UNMARK is negative, tick articles."
8650   (interactive "P")
8651   (when unmark
8652     (setq unmark (prefix-numeric-value unmark)))
8653   (let ((count
8654          (gnus-summary-mark-same-subject
8655           (gnus-summary-article-subject) unmark)))
8656     ;; Select next unread article.  If auto-select-same mode, should
8657     ;; select the first unread article.
8658     (gnus-summary-next-article t (and gnus-auto-select-same
8659                                       (gnus-summary-article-subject)))
8660     (gnus-message 7 "%d article%s marked as %s"
8661                   count (if (= count 1) " is" "s are")
8662                   (if unmark "unread" "read"))))
8663
8664 (defun gnus-summary-kill-same-subject (&optional unmark)
8665   "Mark articles which has the same subject as read.
8666 If UNMARK is positive, remove any kind of mark.
8667 If UNMARK is negative, tick articles."
8668   (interactive "P")
8669   (when unmark
8670     (setq unmark (prefix-numeric-value unmark)))
8671   (let ((count
8672          (gnus-summary-mark-same-subject
8673           (gnus-summary-article-subject) unmark)))
8674     ;; If marked as read, go to next unread subject.
8675     (when (null unmark)
8676       ;; Go to next unread subject.
8677       (gnus-summary-next-subject 1 t))
8678     (gnus-message 7 "%d articles are marked as %s"
8679                   count (if unmark "unread" "read"))))
8680
8681 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8682   "Mark articles with same SUBJECT as read, and return marked number.
8683 If optional argument UNMARK is positive, remove any kinds of marks.
8684 If optional argument UNMARK is negative, mark articles as unread instead."
8685   (let ((count 1))
8686     (save-excursion
8687       (cond
8688        ((null unmark)                   ; Mark as read.
8689         (while (and
8690                 (progn
8691                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8692                   (gnus-summary-show-thread) t)
8693                 (gnus-summary-find-subject subject))
8694           (setq count (1+ count))))
8695        ((> unmark 0)                    ; Tick.
8696         (while (and
8697                 (progn
8698                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8699                   (gnus-summary-show-thread) t)
8700                 (gnus-summary-find-subject subject))
8701           (setq count (1+ count))))
8702        (t                               ; Mark as unread.
8703         (while (and
8704                 (progn
8705                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8706                   (gnus-summary-show-thread) t)
8707                 (gnus-summary-find-subject subject))
8708           (setq count (1+ count)))))
8709       (gnus-set-mode-line 'summary)
8710       ;; Return the number of marked articles.
8711       count)))
8712
8713 (defun gnus-summary-mark-as-processable (n &optional unmark)
8714   "Set the process mark on the next N articles.
8715 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8716 the process mark instead.  The difference between N and the actual
8717 number of articles marked is returned."
8718   (interactive "P")
8719   (if (and (null n) (gnus-region-active-p))
8720       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8721     (setq n (prefix-numeric-value n))
8722     (let ((backward (< n 0))
8723           (n (abs n)))
8724       (while (and
8725               (> n 0)
8726               (if unmark
8727                   (gnus-summary-remove-process-mark
8728                    (gnus-summary-article-number))
8729                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8730               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8731         (setq n (1- n)))
8732       (when (/= 0 n)
8733         (gnus-message 7 "No more articles"))
8734       (gnus-summary-recenter)
8735       (gnus-summary-position-point)
8736       n)))
8737
8738 (defun gnus-summary-unmark-as-processable (n)
8739   "Remove the process mark from the next N articles.
8740 If N is negative, unmark backward instead.  The difference between N and
8741 the actual number of articles unmarked is returned."
8742   (interactive "P")
8743   (gnus-summary-mark-as-processable n t))
8744
8745 (defun gnus-summary-unmark-all-processable ()
8746   "Remove the process mark from all articles."
8747   (interactive)
8748   (save-excursion
8749     (while gnus-newsgroup-processable
8750       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8751   (gnus-summary-position-point))
8752
8753 (defun gnus-summary-add-mark (article type)
8754   "Mark ARTICLE with a mark of TYPE."
8755   (let ((vtype (car (assq type gnus-article-mark-lists)))
8756         var)
8757     (if (not vtype)
8758         (error "No such mark type: %s" type)
8759       (setq var (intern (format "gnus-newsgroup-%s" type)))
8760       (set var (cons article (symbol-value var)))
8761       (if (memq type '(processable cached replied forwarded recent saved))
8762           (gnus-summary-update-secondary-mark article)
8763         ;;; !!! This is bogus.  We should find out what primary
8764         ;;; !!! mark we want to set.
8765         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8766
8767 (defun gnus-summary-mark-as-expirable (n)
8768   "Mark N articles forward as expirable.
8769 If N is negative, mark backward instead.  The difference between N and
8770 the actual number of articles marked is returned."
8771   (interactive "p")
8772   (gnus-summary-mark-forward n gnus-expirable-mark))
8773
8774 (defun gnus-summary-mark-article-as-replied (article)
8775   "Mark ARTICLE as replied to and update the summary line.
8776 ARTICLE can also be a list of articles."
8777   (interactive (list (gnus-summary-article-number)))
8778   (let ((articles (if (listp article) article (list article))))
8779     (dolist (article articles)
8780       (push article gnus-newsgroup-replied)
8781       (let ((buffer-read-only nil))
8782         (when (gnus-summary-goto-subject article nil t)
8783           (gnus-summary-update-secondary-mark article))))))
8784
8785 (defun gnus-summary-mark-article-as-forwarded (article)
8786   "Mark ARTICLE as forwarded and update the summary line.
8787 ARTICLE can also be a list of articles."
8788   (let ((articles (if (listp article) article (list article))))
8789     (dolist (article articles)
8790       (push article gnus-newsgroup-forwarded)
8791       (let ((buffer-read-only nil))
8792         (when (gnus-summary-goto-subject article nil t)
8793           (gnus-summary-update-secondary-mark article))))))
8794
8795 (defun gnus-summary-set-bookmark (article)
8796   "Set a bookmark in current article."
8797   (interactive (list (gnus-summary-article-number)))
8798   (when (or (not (get-buffer gnus-article-buffer))
8799             (not gnus-current-article)
8800             (not gnus-article-current)
8801             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8802     (error "No current article selected"))
8803   ;; Remove old bookmark, if one exists.
8804   (let ((old (assq article gnus-newsgroup-bookmarks)))
8805     (when old
8806       (setq gnus-newsgroup-bookmarks
8807             (delq old gnus-newsgroup-bookmarks))))
8808   ;; Set the new bookmark, which is on the form
8809   ;; (article-number . line-number-in-body).
8810   (push
8811    (cons article
8812          (save-excursion
8813            (set-buffer gnus-article-buffer)
8814            (count-lines
8815             (min (point)
8816                  (save-excursion
8817                    (goto-char (point-min))
8818                    (search-forward "\n\n" nil t)
8819                    (point)))
8820             (point))))
8821    gnus-newsgroup-bookmarks)
8822   (gnus-message 6 "A bookmark has been added to the current article."))
8823
8824 (defun gnus-summary-remove-bookmark (article)
8825   "Remove the bookmark from the current article."
8826   (interactive (list (gnus-summary-article-number)))
8827   ;; Remove old bookmark, if one exists.
8828   (let ((old (assq article gnus-newsgroup-bookmarks)))
8829     (if old
8830         (progn
8831           (setq gnus-newsgroup-bookmarks
8832                 (delq old gnus-newsgroup-bookmarks))
8833           (gnus-message 6 "Removed bookmark."))
8834       (gnus-message 6 "No bookmark in current article."))))
8835
8836 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8837 (defun gnus-summary-mark-as-dormant (n)
8838   "Mark N articles forward as dormant.
8839 If N is negative, mark backward instead.  The difference between N and
8840 the actual number of articles marked is returned."
8841   (interactive "p")
8842   (gnus-summary-mark-forward n gnus-dormant-mark))
8843
8844 (defun gnus-summary-set-process-mark (article)
8845   "Set the process mark on ARTICLE and update the summary line."
8846   (setq gnus-newsgroup-processable
8847         (cons article
8848               (delq article gnus-newsgroup-processable)))
8849   (when (gnus-summary-goto-subject article)
8850     (gnus-summary-show-thread)
8851     (gnus-summary-goto-subject article)
8852     (gnus-summary-update-secondary-mark article)))
8853
8854 (defun gnus-summary-remove-process-mark (article)
8855   "Remove the process mark from ARTICLE and update the summary line."
8856   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8857   (when (gnus-summary-goto-subject article)
8858     (gnus-summary-show-thread)
8859     (gnus-summary-goto-subject article)
8860     (gnus-summary-update-secondary-mark article)))
8861
8862 (defun gnus-summary-set-saved-mark (article)
8863   "Set the process mark on ARTICLE and update the summary line."
8864   (push article gnus-newsgroup-saved)
8865   (when (gnus-summary-goto-subject article)
8866     (gnus-summary-update-secondary-mark article)))
8867
8868 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8869   "Mark N articles as read forwards.
8870 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8871 The difference between N and the actual number of articles marked is
8872 returned.
8873 Iff NO-EXPIRE, auto-expiry will be inhibited."
8874   (interactive "p")
8875   (gnus-summary-show-thread)
8876   (let ((backward (< n 0))
8877         (gnus-summary-goto-unread
8878          (and gnus-summary-goto-unread
8879               (not (eq gnus-summary-goto-unread 'never))
8880               (not (memq mark (list gnus-unread-mark
8881                                     gnus-ticked-mark gnus-dormant-mark)))))
8882         (n (abs n))
8883         (mark (or mark gnus-del-mark)))
8884     (while (and (> n 0)
8885                 (gnus-summary-mark-article nil mark no-expire)
8886                 (zerop (gnus-summary-next-subject
8887                         (if backward -1 1)
8888                         (and gnus-summary-goto-unread
8889                              (not (eq gnus-summary-goto-unread 'never)))
8890                         t)))
8891       (setq n (1- n)))
8892     (when (/= 0 n)
8893       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8894     (gnus-summary-recenter)
8895     (gnus-summary-position-point)
8896     (gnus-set-mode-line 'summary)
8897     n))
8898
8899 (defun gnus-summary-mark-article-as-read (mark)
8900   "Mark the current article quickly as read with MARK."
8901   (let ((article (gnus-summary-article-number)))
8902     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8903     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8904     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8905     (push (cons article mark) gnus-newsgroup-reads)
8906     ;; Possibly remove from cache, if that is used.
8907     (when gnus-use-cache
8908       (gnus-cache-enter-remove-article article))
8909     ;; Allow the backend to change the mark.
8910     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8911     ;; Check for auto-expiry.
8912     (when (and gnus-newsgroup-auto-expire
8913                (memq mark gnus-auto-expirable-marks))
8914       (setq mark gnus-expirable-mark)
8915       ;; Let the backend know about the mark change.
8916       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8917       (push article gnus-newsgroup-expirable))
8918     ;; Set the mark in the buffer.
8919     (gnus-summary-update-mark mark 'unread)
8920     t))
8921
8922 (defun gnus-summary-mark-article-as-unread (mark)
8923   "Mark the current article quickly as unread with MARK."
8924   (let* ((article (gnus-summary-article-number))
8925          (old-mark (gnus-summary-article-mark article)))
8926     ;; Allow the backend to change the mark.
8927     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8928     (if (eq mark old-mark)
8929         t
8930       (if (<= article 0)
8931           (progn
8932             (gnus-error 1 "Can't mark negative article numbers")
8933             nil)
8934         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8935         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8936         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8937         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8938         (cond ((= mark gnus-ticked-mark)
8939                (push article gnus-newsgroup-marked))
8940               ((= mark gnus-dormant-mark)
8941                (push article gnus-newsgroup-dormant))
8942               (t
8943                (push article gnus-newsgroup-unreads)))
8944         (gnus-pull article gnus-newsgroup-reads)
8945
8946         ;; See whether the article is to be put in the cache.
8947         (and gnus-use-cache
8948              (vectorp (gnus-summary-article-header article))
8949              (save-excursion
8950                (gnus-cache-possibly-enter-article
8951                 gnus-newsgroup-name article
8952                 (= mark gnus-ticked-mark)
8953                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8954
8955         ;; Fix the mark.
8956         (gnus-summary-update-mark mark 'unread)
8957         t))))
8958
8959 (defun gnus-summary-mark-article (&optional article mark no-expire)
8960   "Mark ARTICLE with MARK.  MARK can be any character.
8961 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8962 `??' (dormant) and `?E' (expirable).
8963 If MARK is nil, then the default character `?r' is used.
8964 If ARTICLE is nil, then the article on the current line will be
8965 marked.
8966 Iff NO-EXPIRE, auto-expiry will be inhibited."
8967   ;; The mark might be a string.
8968   (when (stringp mark)
8969     (setq mark (aref mark 0)))
8970   ;; If no mark is given, then we check auto-expiring.
8971   (when (null mark)
8972     (setq mark gnus-del-mark))
8973   (when (and (not no-expire)
8974              gnus-newsgroup-auto-expire
8975              (memq mark gnus-auto-expirable-marks))
8976     (setq mark gnus-expirable-mark))
8977   (let ((article (or article (gnus-summary-article-number)))
8978         (old-mark (gnus-summary-article-mark article)))
8979     ;; Allow the backend to change the mark.
8980     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8981     (if (eq mark old-mark)
8982         t
8983       (unless article
8984         (error "No article on current line"))
8985       (if (not (if (or (= mark gnus-unread-mark)
8986                        (= mark gnus-ticked-mark)
8987                        (= mark gnus-dormant-mark))
8988                    (gnus-mark-article-as-unread article mark)
8989                  (gnus-mark-article-as-read article mark)))
8990           t
8991         ;; See whether the article is to be put in the cache.
8992         (and gnus-use-cache
8993              (not (= mark gnus-canceled-mark))
8994              (vectorp (gnus-summary-article-header article))
8995              (save-excursion
8996                (gnus-cache-possibly-enter-article
8997                 gnus-newsgroup-name article
8998                 (= mark gnus-ticked-mark)
8999                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9000
9001         (when (gnus-summary-goto-subject article nil t)
9002           (let ((buffer-read-only nil))
9003             (gnus-summary-show-thread)
9004             ;; Fix the mark.
9005             (gnus-summary-update-mark mark 'unread)
9006             t))))))
9007
9008 (defun gnus-summary-update-secondary-mark (article)
9009   "Update the secondary (read, process, cache) mark."
9010   (gnus-summary-update-mark
9011    (cond ((memq article gnus-newsgroup-processable)
9012           gnus-process-mark)
9013          ((memq article gnus-newsgroup-cached)
9014           gnus-cached-mark)
9015          ((memq article gnus-newsgroup-replied)
9016           gnus-replied-mark)
9017          ((memq article gnus-newsgroup-forwarded)
9018           gnus-forwarded-mark)
9019          ((memq article gnus-newsgroup-saved)
9020           gnus-saved-mark)
9021          ((memq article gnus-newsgroup-recent)
9022           gnus-recent-mark)
9023          ((memq article gnus-newsgroup-unseen)
9024           gnus-unseen-mark)
9025          (t gnus-no-mark))
9026    'replied)
9027   (when (gnus-visual-p 'summary-highlight 'highlight)
9028     (gnus-run-hooks 'gnus-summary-update-hook))
9029   t)
9030
9031 (defun gnus-summary-update-mark (mark type)
9032   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9033         (buffer-read-only nil))
9034     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9035     (when forward
9036       (when (looking-at "\r")
9037         (incf forward))
9038       (when (<= (+ forward (point)) (point-max))
9039         ;; Go to the right position on the line.
9040         (goto-char (+ forward (point)))
9041         ;; Replace the old mark with the new mark.
9042         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9043         ;; Optionally update the marks by some user rule.
9044         (when (eq type 'unread)
9045           (gnus-data-set-mark
9046            (gnus-data-find (gnus-summary-article-number)) mark)
9047           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9048
9049 (defun gnus-mark-article-as-read (article &optional mark)
9050   "Enter ARTICLE in the pertinent lists and remove it from others."
9051   ;; Make the article expirable.
9052   (let ((mark (or mark gnus-del-mark)))
9053     (if (= mark gnus-expirable-mark)
9054         (push article gnus-newsgroup-expirable)
9055       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9056     ;; Remove from unread and marked lists.
9057     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9058     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9059     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9060     (push (cons article mark) gnus-newsgroup-reads)
9061     ;; Possibly remove from cache, if that is used.
9062     (when gnus-use-cache
9063       (gnus-cache-enter-remove-article article))
9064     t))
9065
9066 (defun gnus-mark-article-as-unread (article &optional mark)
9067   "Enter ARTICLE in the pertinent lists and remove it from others."
9068   (let ((mark (or mark gnus-ticked-mark)))
9069     (if (<= article 0)
9070         (progn
9071           (gnus-error 1 "Can't mark negative article numbers")
9072           nil)
9073       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9074             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9075             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9076             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9077
9078       ;; Unsuppress duplicates?
9079       (when gnus-suppress-duplicates
9080         (gnus-dup-unsuppress-article article))
9081
9082       (cond ((= mark gnus-ticked-mark)
9083              (push article gnus-newsgroup-marked))
9084             ((= mark gnus-dormant-mark)
9085              (push article gnus-newsgroup-dormant))
9086             (t
9087              (push article gnus-newsgroup-unreads)))
9088       (gnus-pull article gnus-newsgroup-reads)
9089       t)))
9090
9091 (defalias 'gnus-summary-mark-as-unread-forward
9092   'gnus-summary-tick-article-forward)
9093 (make-obsolete 'gnus-summary-mark-as-unread-forward
9094                'gnus-summary-tick-article-forward)
9095 (defun gnus-summary-tick-article-forward (n)
9096   "Tick N articles forwards.
9097 If N is negative, tick backwards instead.
9098 The difference between N and the number of articles ticked is returned."
9099   (interactive "p")
9100   (gnus-summary-mark-forward n gnus-ticked-mark))
9101
9102 (defalias 'gnus-summary-mark-as-unread-backward
9103   'gnus-summary-tick-article-backward)
9104 (make-obsolete 'gnus-summary-mark-as-unread-backward
9105                'gnus-summary-tick-article-backward)
9106 (defun gnus-summary-tick-article-backward (n)
9107   "Tick N articles backwards.
9108 The difference between N and the number of articles ticked is returned."
9109   (interactive "p")
9110   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9111
9112 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9113 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9114 (defun gnus-summary-tick-article (&optional article clear-mark)
9115   "Mark current article as unread.
9116 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9117 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9118   (interactive)
9119   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9120                                        gnus-ticked-mark)))
9121
9122 (defun gnus-summary-mark-as-read-forward (n)
9123   "Mark N articles as read forwards.
9124 If N is negative, mark backwards instead.
9125 The difference between N and the actual number of articles marked is
9126 returned."
9127   (interactive "p")
9128   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9129
9130 (defun gnus-summary-mark-as-read-backward (n)
9131   "Mark the N articles as read backwards.
9132 The difference between N and the actual number of articles marked is
9133 returned."
9134   (interactive "p")
9135   (gnus-summary-mark-forward
9136    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9137
9138 (defun gnus-summary-mark-as-read (&optional article mark)
9139   "Mark current article as read.
9140 ARTICLE specifies the article to be marked as read.
9141 MARK specifies a string to be inserted at the beginning of the line."
9142   (gnus-summary-mark-article article mark))
9143
9144 (defun gnus-summary-clear-mark-forward (n)
9145   "Clear marks from N articles forward.
9146 If N is negative, clear backward instead.
9147 The difference between N and the number of marks cleared is returned."
9148   (interactive "p")
9149   (gnus-summary-mark-forward n gnus-unread-mark))
9150
9151 (defun gnus-summary-clear-mark-backward (n)
9152   "Clear marks from N articles backward.
9153 The difference between N and the number of marks cleared is returned."
9154   (interactive "p")
9155   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9156
9157 (defun gnus-summary-mark-unread-as-read ()
9158   "Intended to be used by `gnus-summary-mark-article-hook'."
9159   (when (memq gnus-current-article gnus-newsgroup-unreads)
9160     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9161
9162 (defun gnus-summary-mark-read-and-unread-as-read ()
9163   "Intended to be used by `gnus-summary-mark-article-hook'."
9164   (let ((mark (gnus-summary-article-mark)))
9165     (when (or (gnus-unread-mark-p mark)
9166               (gnus-read-mark-p mark))
9167       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9168
9169 (defun gnus-summary-mark-unread-as-ticked ()
9170   "Intended to be used by `gnus-summary-mark-article-hook'."
9171   (when (memq gnus-current-article gnus-newsgroup-unreads)
9172     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9173
9174 (defun gnus-summary-mark-region-as-read (point mark all)
9175   "Mark all unread articles between point and mark as read.
9176 If given a prefix, mark all articles between point and mark as read,
9177 even ticked and dormant ones."
9178   (interactive "r\nP")
9179   (save-excursion
9180     (let (article)
9181       (goto-char point)
9182       (beginning-of-line)
9183       (while (and
9184               (< (point) mark)
9185               (progn
9186                 (when (or all
9187                           (memq (setq article (gnus-summary-article-number))
9188                                 gnus-newsgroup-unreads))
9189                   (gnus-summary-mark-article article gnus-del-mark))
9190                 t)
9191               (gnus-summary-find-next))))))
9192
9193 (defun gnus-summary-mark-below (score mark)
9194   "Mark articles with score less than SCORE with MARK."
9195   (interactive "P\ncMark: ")
9196   (setq score (if score
9197                   (prefix-numeric-value score)
9198                 (or gnus-summary-default-score 0)))
9199   (save-excursion
9200     (set-buffer gnus-summary-buffer)
9201     (goto-char (point-min))
9202     (while
9203         (progn
9204           (and (< (gnus-summary-article-score) score)
9205                (gnus-summary-mark-article nil mark))
9206           (gnus-summary-find-next)))))
9207
9208 (defun gnus-summary-kill-below (&optional score)
9209   "Mark articles with score below SCORE as read."
9210   (interactive "P")
9211   (gnus-summary-mark-below score gnus-killed-mark))
9212
9213 (defun gnus-summary-clear-above (&optional score)
9214   "Clear all marks from articles with score above SCORE."
9215   (interactive "P")
9216   (gnus-summary-mark-above score gnus-unread-mark))
9217
9218 (defun gnus-summary-tick-above (&optional score)
9219   "Tick all articles with score above SCORE."
9220   (interactive "P")
9221   (gnus-summary-mark-above score gnus-ticked-mark))
9222
9223 (defun gnus-summary-mark-above (score mark)
9224   "Mark articles with score over SCORE with MARK."
9225   (interactive "P\ncMark: ")
9226   (setq score (if score
9227                   (prefix-numeric-value score)
9228                 (or gnus-summary-default-score 0)))
9229   (save-excursion
9230     (set-buffer gnus-summary-buffer)
9231     (goto-char (point-min))
9232     (while (and (progn
9233                   (when (> (gnus-summary-article-score) score)
9234                     (gnus-summary-mark-article nil mark))
9235                   t)
9236                 (gnus-summary-find-next)))))
9237
9238 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9239 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9240 (defun gnus-summary-limit-include-expunged (&optional no-error)
9241   "Display all the hidden articles that were expunged for low scores."
9242   (interactive)
9243   (let ((buffer-read-only nil))
9244     (let ((scored gnus-newsgroup-scored)
9245           headers h)
9246       (while scored
9247         (unless (gnus-summary-article-header (caar scored))
9248           (and (setq h (gnus-number-to-header (caar scored)))
9249                (< (cdar scored) gnus-summary-expunge-below)
9250                (push h headers)))
9251         (setq scored (cdr scored)))
9252       (if (not headers)
9253           (when (not no-error)
9254             (error "No expunged articles hidden"))
9255         (goto-char (point-min))
9256         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9257         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9258         (mapcar (lambda (x) (push (mail-header-number x)
9259                                   gnus-newsgroup-limit))
9260                 headers)
9261         (gnus-summary-prepare-unthreaded (nreverse headers))
9262         (goto-char (point-min))
9263         (gnus-summary-position-point)
9264         t))))
9265
9266 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9267   "Mark all unread articles in this newsgroup as read.
9268 If prefix argument ALL is non-nil, ticked and dormant articles will
9269 also be marked as read.
9270 If QUIETLY is non-nil, no questions will be asked.
9271 If TO-HERE is non-nil, it should be a point in the buffer.  All
9272 articles before (after, if REVERSE is set) this point will be marked as read.
9273 Note that this function will only catch up the unread article
9274 in the current summary buffer limitation.
9275 The number of articles marked as read is returned."
9276   (interactive "P")
9277   (prog1
9278       (save-excursion
9279         (when (or quietly
9280                   (not gnus-interactive-catchup) ;Without confirmation?
9281                   gnus-expert-user
9282                   (gnus-y-or-n-p
9283                    (if all
9284                        "Mark absolutely all articles as read? "
9285                      "Mark all unread articles as read? ")))
9286           (if (and not-mark
9287                    (not gnus-newsgroup-adaptive)
9288                    (not gnus-newsgroup-auto-expire)
9289                    (not gnus-suppress-duplicates)
9290                    (or (not gnus-use-cache)
9291                        (eq gnus-use-cache 'passive)))
9292               (progn
9293                 (when all
9294                   (setq gnus-newsgroup-marked nil
9295                         gnus-newsgroup-dormant nil))
9296                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9297             ;; We actually mark all articles as canceled, which we
9298             ;; have to do when using auto-expiry or adaptive scoring.
9299             (gnus-summary-show-all-threads)
9300             (if (and to-here reverse)
9301                 (progn
9302                   (goto-char to-here)
9303                   (while (and
9304                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9305                           (gnus-summary-find-next (not all) nil nil t))))
9306               (when (gnus-summary-first-subject (not all) t)
9307                 (while (and
9308                         (if to-here (< (point) to-here) t)
9309                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9310                         (gnus-summary-find-next (not all) nil nil t)))))
9311             (gnus-set-mode-line 'summary))
9312           t))
9313     (gnus-summary-position-point)))
9314
9315 (defun gnus-summary-catchup-to-here (&optional all)
9316   "Mark all unticked articles before the current one as read.
9317 If ALL is non-nil, also mark ticked and dormant articles as read."
9318   (interactive "P")
9319   (save-excursion
9320     (gnus-save-hidden-threads
9321       (let ((beg (point)))
9322         ;; We check that there are unread articles.
9323         (when (or all (gnus-summary-find-prev))
9324           (gnus-summary-catchup all t beg)))))
9325   (gnus-summary-position-point))
9326
9327 (defun gnus-summary-catchup-from-here (&optional all)
9328   "Mark all unticked articles after the current one as read.
9329 If ALL is non-nil, also mark ticked and dormant articles as read."
9330   (interactive "P")
9331   (save-excursion
9332     (gnus-save-hidden-threads
9333       (let ((beg (point)))
9334         ;; We check that there are unread articles.
9335         (when (or all (gnus-summary-find-next))
9336           (gnus-summary-catchup all t beg nil t)))))
9337
9338   (gnus-summary-position-point))
9339 (defun gnus-summary-catchup-all (&optional quietly)
9340   "Mark all articles in this newsgroup as read."
9341   (interactive "P")
9342   (gnus-summary-catchup t quietly))
9343
9344 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9345   "Mark all unread articles in this group as read, then exit.
9346 If prefix argument ALL is non-nil, all articles are marked as read.
9347 If QUIETLY is non-nil, no questions will be asked."
9348   (interactive "P")
9349   (when (gnus-summary-catchup all quietly nil 'fast)
9350     ;; Select next newsgroup or exit.
9351     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9352              (eq gnus-auto-select-next 'quietly))
9353         (gnus-summary-next-group nil)
9354       (gnus-summary-exit))))
9355
9356 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9357   "Mark all articles in this newsgroup as read, and then exit."
9358   (interactive "P")
9359   (gnus-summary-catchup-and-exit t quietly))
9360
9361 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9362   "Mark all articles in this group as read and select the next group.
9363 If given a prefix, mark all articles, unread as well as ticked, as
9364 read."
9365   (interactive "P")
9366   (save-excursion
9367     (gnus-summary-catchup all))
9368   (gnus-summary-next-group))
9369
9370 ;;;
9371 ;;; with article
9372 ;;;
9373
9374 (defmacro gnus-with-article (article &rest forms)
9375   "Select ARTICLE and perform FORMS in the original article buffer.
9376 Then replace the article with the result."
9377   `(progn
9378      ;; We don't want the article to be marked as read.
9379      (let (gnus-mark-article-hook)
9380        (gnus-summary-select-article t t nil ,article))
9381      (set-buffer gnus-original-article-buffer)
9382      ,@forms
9383      (if (not (gnus-check-backend-function
9384                'request-replace-article (car gnus-article-current)))
9385          (gnus-message 5 "Read-only group; not replacing")
9386        (unless (gnus-request-replace-article
9387                 ,article (car gnus-article-current)
9388                 (current-buffer) t)
9389          (error "Couldn't replace article")))
9390      ;; The cache and backlog have to be flushed somewhat.
9391      (when gnus-keep-backlog
9392        (gnus-backlog-remove-article
9393         (car gnus-article-current) (cdr gnus-article-current)))
9394      (when gnus-use-cache
9395        (gnus-cache-update-article
9396         (car gnus-article-current) (cdr gnus-article-current)))))
9397
9398 (put 'gnus-with-article 'lisp-indent-function 1)
9399 (put 'gnus-with-article 'edebug-form-spec '(form body))
9400
9401 ;; Thread-based commands.
9402
9403 (defun gnus-summary-articles-in-thread (&optional article)
9404   "Return a list of all articles in the current thread.
9405 If ARTICLE is non-nil, return all articles in the thread that starts
9406 with that article."
9407   (let* ((article (or article (gnus-summary-article-number)))
9408          (data (gnus-data-find-list article))
9409          (top-level (gnus-data-level (car data)))
9410          (top-subject
9411           (cond ((null gnus-thread-operation-ignore-subject)
9412                  (gnus-simplify-subject-re
9413                   (mail-header-subject (gnus-data-header (car data)))))
9414                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9415                  (gnus-simplify-subject-fuzzy
9416                   (mail-header-subject (gnus-data-header (car data)))))
9417                 (t nil)))
9418          (end-point (save-excursion
9419                       (if (gnus-summary-go-to-next-thread)
9420                           (point) (point-max))))
9421          articles)
9422     (while (and data
9423                 (< (gnus-data-pos (car data)) end-point))
9424       (when (or (not top-subject)
9425                 (string= top-subject
9426                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9427                              (gnus-simplify-subject-fuzzy
9428                               (mail-header-subject
9429                                (gnus-data-header (car data))))
9430                            (gnus-simplify-subject-re
9431                             (mail-header-subject
9432                              (gnus-data-header (car data)))))))
9433         (push (gnus-data-number (car data)) articles))
9434       (unless (and (setq data (cdr data))
9435                    (> (gnus-data-level (car data)) top-level))
9436         (setq data nil)))
9437     ;; Return the list of articles.
9438     (nreverse articles)))
9439
9440 (defun gnus-summary-rethread-current ()
9441   "Rethread the thread the current article is part of."
9442   (interactive)
9443   (let* ((gnus-show-threads t)
9444          (article (gnus-summary-article-number))
9445          (id (mail-header-id (gnus-summary-article-header)))
9446          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9447     (unless id
9448       (error "No article on the current line"))
9449     (gnus-rebuild-thread id)
9450     (gnus-summary-goto-subject article)))
9451
9452 (defun gnus-summary-reparent-thread ()
9453   "Make the current article child of the marked (or previous) article.
9454
9455 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9456 is non-nil or the Subject: of both articles are the same."
9457   (interactive)
9458   (unless (not (gnus-group-read-only-p))
9459     (error "The current newsgroup does not support article editing"))
9460   (unless (<= (length gnus-newsgroup-processable) 1)
9461     (error "No more than one article may be marked"))
9462   (save-window-excursion
9463     (let ((gnus-article-buffer " *reparent*")
9464           (current-article (gnus-summary-article-number))
9465           ;; First grab the marked article, otherwise one line up.
9466           (parent-article (if (not (null gnus-newsgroup-processable))
9467                               (car gnus-newsgroup-processable)
9468                             (save-excursion
9469                               (if (eq (forward-line -1) 0)
9470                                   (gnus-summary-article-number)
9471                                 (error "Beginning of summary buffer"))))))
9472       (unless (not (eq current-article parent-article))
9473         (error "An article may not be self-referential"))
9474       (let ((message-id (mail-header-id
9475                          (gnus-summary-article-header parent-article))))
9476         (unless (and message-id (not (equal message-id "")))
9477           (error "No message-id in desired parent"))
9478         (gnus-with-article current-article
9479           (save-restriction
9480             (goto-char (point-min))
9481             (message-narrow-to-head)
9482             (if (re-search-forward "^References: " nil t)
9483                 (progn
9484                   (re-search-forward "^[^ \t]" nil t)
9485                   (forward-line -1)
9486                   (end-of-line)
9487                   (insert " " message-id))
9488               (insert "References: " message-id "\n"))))
9489         (set-buffer gnus-summary-buffer)
9490         (gnus-summary-unmark-all-processable)
9491         (gnus-summary-update-article current-article)
9492         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9493             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9494         (gnus-summary-rethread-current)
9495         (gnus-message 3 "Article %d is now the child of article %d"
9496                       current-article parent-article)))))
9497
9498 (defun gnus-summary-toggle-threads (&optional arg)
9499   "Toggle showing conversation threads.
9500 If ARG is positive number, turn showing conversation threads on."
9501   (interactive "P")
9502   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9503     (setq gnus-show-threads
9504           (if (null arg) (not gnus-show-threads)
9505             (> (prefix-numeric-value arg) 0)))
9506     (gnus-summary-prepare)
9507     (gnus-summary-goto-subject current)
9508     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9509     (gnus-summary-position-point)))
9510
9511 (defun gnus-summary-show-all-threads ()
9512   "Show all threads."
9513   (interactive)
9514   (save-excursion
9515     (let ((buffer-read-only nil))
9516       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9517   (gnus-summary-position-point))
9518
9519 (defun gnus-summary-show-thread ()
9520   "Show thread subtrees.
9521 Returns nil if no thread was there to be shown."
9522   (interactive)
9523   (let ((buffer-read-only nil)
9524         (orig (point))
9525         ;; first goto end then to beg, to have point at beg after let
9526         (end (progn (end-of-line) (point)))
9527         (beg (progn (beginning-of-line) (point))))
9528     (prog1
9529         ;; Any hidden lines here?
9530         (search-forward "\r" end t)
9531       (subst-char-in-region beg end ?\^M ?\n t)
9532       (goto-char orig)
9533       (gnus-summary-position-point))))
9534
9535 (defun gnus-summary-hide-all-threads ()
9536   "Hide all thread subtrees."
9537   (interactive)
9538   (save-excursion
9539     (goto-char (point-min))
9540     (gnus-summary-hide-thread)
9541     (while (zerop (gnus-summary-next-thread 1 t))
9542       (gnus-summary-hide-thread)))
9543   (gnus-summary-position-point))
9544
9545 (defun gnus-summary-hide-thread ()
9546   "Hide thread subtrees.
9547 Returns nil if no threads were there to be hidden."
9548   (interactive)
9549   (let ((buffer-read-only nil)
9550         (start (point))
9551         (article (gnus-summary-article-number)))
9552     (goto-char start)
9553     ;; Go forward until either the buffer ends or the subthread
9554     ;; ends.
9555     (when (and (not (eobp))
9556                (or (zerop (gnus-summary-next-thread 1 t))
9557                    (goto-char (point-max))))
9558       (prog1
9559           (if (and (> (point) start)
9560                    (search-backward "\n" start t))
9561               (progn
9562                 (subst-char-in-region start (point) ?\n ?\^M)
9563                 (gnus-summary-goto-subject article))
9564             (goto-char start)
9565             nil)))))
9566
9567 (defun gnus-summary-go-to-next-thread (&optional previous)
9568   "Go to the same level (or less) next thread.
9569 If PREVIOUS is non-nil, go to previous thread instead.
9570 Return the article number moved to, or nil if moving was impossible."
9571   (let ((level (gnus-summary-thread-level))
9572         (way (if previous -1 1))
9573         (beg (point)))
9574     (forward-line way)
9575     (while (and (not (eobp))
9576                 (< level (gnus-summary-thread-level)))
9577       (forward-line way))
9578     (if (eobp)
9579         (progn
9580           (goto-char beg)
9581           nil)
9582       (setq beg (point))
9583       (prog1
9584           (gnus-summary-article-number)
9585         (goto-char beg)))))
9586
9587 (defun gnus-summary-next-thread (n &optional silent)
9588   "Go to the same level next N'th thread.
9589 If N is negative, search backward instead.
9590 Returns the difference between N and the number of skips actually
9591 done.
9592
9593 If SILENT, don't output messages."
9594   (interactive "p")
9595   (let ((backward (< n 0))
9596         (n (abs n)))
9597     (while (and (> n 0)
9598                 (gnus-summary-go-to-next-thread backward))
9599       (decf n))
9600     (unless silent
9601       (gnus-summary-position-point))
9602     (when (and (not silent) (/= 0 n))
9603       (gnus-message 7 "No more threads"))
9604     n))
9605
9606 (defun gnus-summary-prev-thread (n)
9607   "Go to the same level previous N'th thread.
9608 Returns the difference between N and the number of skips actually
9609 done."
9610   (interactive "p")
9611   (gnus-summary-next-thread (- n)))
9612
9613 (defun gnus-summary-go-down-thread ()
9614   "Go down one level in the current thread."
9615   (let ((children (gnus-summary-article-children)))
9616     (when children
9617       (gnus-summary-goto-subject (car children)))))
9618
9619 (defun gnus-summary-go-up-thread ()
9620   "Go up one level in the current thread."
9621   (let ((parent (gnus-summary-article-parent)))
9622     (when parent
9623       (gnus-summary-goto-subject parent))))
9624
9625 (defun gnus-summary-down-thread (n)
9626   "Go down thread N steps.
9627 If N is negative, go up instead.
9628 Returns the difference between N and how many steps down that were
9629 taken."
9630   (interactive "p")
9631   (let ((up (< n 0))
9632         (n (abs n)))
9633     (while (and (> n 0)
9634                 (if up (gnus-summary-go-up-thread)
9635                   (gnus-summary-go-down-thread)))
9636       (setq n (1- n)))
9637     (gnus-summary-position-point)
9638     (when (/= 0 n)
9639       (gnus-message 7 "Can't go further"))
9640     n))
9641
9642 (defun gnus-summary-up-thread (n)
9643   "Go up thread N steps.
9644 If N is negative, go down instead.
9645 Returns the difference between N and how many steps down that were
9646 taken."
9647   (interactive "p")
9648   (gnus-summary-down-thread (- n)))
9649
9650 (defun gnus-summary-top-thread ()
9651   "Go to the top of the thread."
9652   (interactive)
9653   (while (gnus-summary-go-up-thread))
9654   (gnus-summary-article-number))
9655
9656 (defun gnus-summary-kill-thread (&optional unmark)
9657   "Mark articles under current thread as read.
9658 If the prefix argument is positive, remove any kinds of marks.
9659 If the prefix argument is negative, tick articles instead."
9660   (interactive "P")
9661   (when unmark
9662     (setq unmark (prefix-numeric-value unmark)))
9663   (let ((articles (gnus-summary-articles-in-thread)))
9664     (save-excursion
9665       ;; Expand the thread.
9666       (gnus-summary-show-thread)
9667       ;; Mark all the articles.
9668       (while articles
9669         (gnus-summary-goto-subject (car articles))
9670         (cond ((null unmark)
9671                (gnus-summary-mark-article-as-read gnus-killed-mark))
9672               ((> unmark 0)
9673                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9674               (t
9675                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9676         (setq articles (cdr articles))))
9677     ;; Hide killed subtrees.
9678     (and (null unmark)
9679          gnus-thread-hide-killed
9680          (gnus-summary-hide-thread))
9681     ;; If marked as read, go to next unread subject.
9682     (when (null unmark)
9683       ;; Go to next unread subject.
9684       (gnus-summary-next-subject 1 t)))
9685   (gnus-set-mode-line 'summary))
9686
9687 ;; Summary sorting commands
9688
9689 (defun gnus-summary-sort-by-number (&optional reverse)
9690   "Sort the summary buffer by article number.
9691 Argument REVERSE means reverse order."
9692   (interactive "P")
9693   (gnus-summary-sort 'number reverse))
9694
9695 (defun gnus-summary-sort-by-author (&optional reverse)
9696   "Sort the summary buffer by author name alphabetically.
9697 If `case-fold-search' is non-nil, case of letters is ignored.
9698 Argument REVERSE means reverse order."
9699   (interactive "P")
9700   (gnus-summary-sort 'author reverse))
9701
9702 (defun gnus-summary-sort-by-subject (&optional reverse)
9703   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9704 If `case-fold-search' is non-nil, case of letters is ignored.
9705 Argument REVERSE means reverse order."
9706   (interactive "P")
9707   (gnus-summary-sort 'subject reverse))
9708
9709 (defun gnus-summary-sort-by-date (&optional reverse)
9710   "Sort the summary buffer by date.
9711 Argument REVERSE means reverse order."
9712   (interactive "P")
9713   (gnus-summary-sort 'date reverse))
9714
9715 (defun gnus-summary-sort-by-score (&optional reverse)
9716   "Sort the summary buffer by score.
9717 Argument REVERSE means reverse order."
9718   (interactive "P")
9719   (gnus-summary-sort 'score reverse))
9720
9721 (defun gnus-summary-sort-by-lines (&optional reverse)
9722   "Sort the summary buffer by the number of lines.
9723 Argument REVERSE means reverse order."
9724   (interactive "P")
9725   (gnus-summary-sort 'lines reverse))
9726
9727 (defun gnus-summary-sort-by-chars (&optional reverse)
9728   "Sort the summary buffer by article length.
9729 Argument REVERSE means reverse order."
9730   (interactive "P")
9731   (gnus-summary-sort 'chars reverse))
9732
9733 (defun gnus-summary-sort-by-original (&optional reverse)
9734   "Sort the summary buffer using the default sorting method.
9735 Argument REVERSE means reverse order."
9736   (interactive "P")
9737   (let* ((buffer-read-only)
9738          (gnus-summary-prepare-hook nil))
9739     ;; We do the sorting by regenerating the threads.
9740     (gnus-summary-prepare)
9741     ;; Hide subthreads if needed.
9742     (when (and gnus-show-threads gnus-thread-hide-subtree)
9743       (gnus-summary-hide-all-threads))))
9744
9745 (defun gnus-summary-sort (predicate reverse)
9746   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9747   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9748          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9749          (gnus-thread-sort-functions
9750           (if (not reverse)
9751               thread
9752             `(lambda (t1 t2)
9753                (,thread t2 t1))))
9754          (gnus-sort-gathered-threads-function
9755           gnus-thread-sort-functions)
9756          (gnus-article-sort-functions
9757           (if (not reverse)
9758               article
9759             `(lambda (t1 t2)
9760                (,article t2 t1))))
9761          (buffer-read-only)
9762          (gnus-summary-prepare-hook nil))
9763     ;; We do the sorting by regenerating the threads.
9764     (gnus-summary-prepare)
9765     ;; Hide subthreads if needed.
9766     (when (and gnus-show-threads gnus-thread-hide-subtree)
9767       (gnus-summary-hide-all-threads))))
9768
9769 ;; Summary saving commands.
9770
9771 (defun gnus-summary-save-article (&optional n not-saved)
9772   "Save the current article using the default saver function.
9773 If N is a positive number, save the N next articles.
9774 If N is a negative number, save the N previous articles.
9775 If N is nil and any articles have been marked with the process mark,
9776 save those articles instead.
9777 The variable `gnus-default-article-saver' specifies the saver function."
9778   (interactive "P")
9779   (let* ((articles (gnus-summary-work-articles n))
9780          (save-buffer (save-excursion
9781                         (nnheader-set-temp-buffer " *Gnus Save*")))
9782          (num (length articles))
9783          header file)
9784     (dolist (article articles)
9785       (setq header (gnus-summary-article-header article))
9786       (if (not (vectorp header))
9787           ;; This is a pseudo-article.
9788           (if (assq 'name header)
9789               (gnus-copy-file (cdr (assq 'name header)))
9790             (gnus-message 1 "Article %d is unsaveable" article))
9791         ;; This is a real article.
9792         (save-window-excursion
9793           (gnus-summary-select-article t nil nil article))
9794         (save-excursion
9795           (set-buffer save-buffer)
9796           (erase-buffer)
9797           (insert-buffer-substring gnus-original-article-buffer))
9798         (setq file (gnus-article-save save-buffer file num))
9799         (gnus-summary-remove-process-mark article)
9800         (unless not-saved
9801           (gnus-summary-set-saved-mark article))))
9802     (gnus-kill-buffer save-buffer)
9803     (gnus-summary-position-point)
9804     (gnus-set-mode-line 'summary)
9805     n))
9806
9807 (defun gnus-summary-pipe-output (&optional arg)
9808   "Pipe the current article to a subprocess.
9809 If N is a positive number, pipe the N next articles.
9810 If N is a negative number, pipe the N previous articles.
9811 If N is nil and any articles have been marked with the process mark,
9812 pipe those articles instead."
9813   (interactive "P")
9814   (require 'gnus-art)
9815   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9816     (gnus-summary-save-article arg t))
9817   (let ((buffer (get-buffer "*Shell Command Output*")))
9818     (if (and buffer
9819              (with-current-buffer buffer (> (point-max) (point-min))))
9820         (gnus-configure-windows 'pipe))))
9821
9822 (defun gnus-summary-save-article-mail (&optional arg)
9823   "Append the current article to an mail file.
9824 If N is a positive number, save the N next articles.
9825 If N is a negative number, save the N previous articles.
9826 If N is nil and any articles have been marked with the process mark,
9827 save those articles instead."
9828   (interactive "P")
9829   (require 'gnus-art)
9830   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9831     (gnus-summary-save-article arg)))
9832
9833 (defun gnus-summary-save-article-rmail (&optional arg)
9834   "Append the current article to an rmail file.
9835 If N is a positive number, save the N next articles.
9836 If N is a negative number, save the N previous articles.
9837 If N is nil and any articles have been marked with the process mark,
9838 save those articles instead."
9839   (interactive "P")
9840   (require 'gnus-art)
9841   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9842     (gnus-summary-save-article arg)))
9843
9844 (defun gnus-summary-save-article-file (&optional arg)
9845   "Append the current article to a file.
9846 If N is a positive number, save the N next articles.
9847 If N is a negative number, save the N previous articles.
9848 If N is nil and any articles have been marked with the process mark,
9849 save those articles instead."
9850   (interactive "P")
9851   (require 'gnus-art)
9852   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9853     (gnus-summary-save-article arg)))
9854
9855 (defun gnus-summary-write-article-file (&optional arg)
9856   "Write the current article to a file, deleting the previous file.
9857 If N is a positive number, save the N next articles.
9858 If N is a negative number, save the N previous articles.
9859 If N is nil and any articles have been marked with the process mark,
9860 save those articles instead."
9861   (interactive "P")
9862   (require 'gnus-art)
9863   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9864     (gnus-summary-save-article arg)))
9865
9866 (defun gnus-summary-save-article-body-file (&optional arg)
9867   "Append the current article body to a file.
9868 If N is a positive number, save the N next articles.
9869 If N is a negative number, save the N previous articles.
9870 If N is nil and any articles have been marked with the process mark,
9871 save those articles instead."
9872   (interactive "P")
9873   (require 'gnus-art)
9874   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9875     (gnus-summary-save-article arg)))
9876
9877 (defun gnus-summary-pipe-message (program)
9878   "Pipe the current article through PROGRAM."
9879   (interactive "sProgram: ")
9880   (gnus-summary-select-article)
9881   (let ((mail-header-separator ""))
9882     (gnus-eval-in-buffer-window gnus-article-buffer
9883       (save-restriction
9884         (widen)
9885         (let ((start (window-start))
9886               buffer-read-only)
9887           (message-pipe-buffer-body program)
9888           (set-window-start (get-buffer-window (current-buffer)) start))))))
9889
9890 (defun gnus-get-split-value (methods)
9891   "Return a value based on the split METHODS."
9892   (let (split-name method result match)
9893     (when methods
9894       (save-excursion
9895         (set-buffer gnus-original-article-buffer)
9896         (save-restriction
9897           (nnheader-narrow-to-headers)
9898           (while (and methods (not split-name))
9899             (goto-char (point-min))
9900             (setq method (pop methods))
9901             (setq match (car method))
9902             (when (cond
9903                    ((stringp match)
9904                     ;; Regular expression.
9905                     (ignore-errors
9906                       (re-search-forward match nil t)))
9907                    ((gnus-functionp match)
9908                     ;; Function.
9909                     (save-restriction
9910                       (widen)
9911                       (setq result (funcall match gnus-newsgroup-name))))
9912                    ((consp match)
9913                     ;; Form.
9914                     (save-restriction
9915                       (widen)
9916                       (setq result (eval match)))))
9917               (setq split-name (cdr method))
9918               (cond ((stringp result)
9919                      (push (expand-file-name
9920                             result gnus-article-save-directory)
9921                            split-name))
9922                     ((consp result)
9923                      (setq split-name (append result split-name)))))))))
9924     (nreverse split-name)))
9925
9926 (defun gnus-valid-move-group-p (group)
9927   (and (boundp group)
9928        (symbol-name group)
9929        (symbol-value group)
9930        (gnus-get-function (gnus-find-method-for-group
9931                            (symbol-name group)) 'request-accept-article t)))
9932
9933 (defun gnus-read-move-group-name (prompt default articles prefix)
9934   "Read a group name."
9935   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9936          (minibuffer-confirm-incomplete nil) ; XEmacs
9937          (prom
9938           (format "%s %s to:"
9939                   prompt
9940                   (if (> (length articles) 1)
9941                       (format "these %d articles" (length articles))
9942                     "this article")))
9943          (to-newsgroup
9944           (cond
9945            ((null split-name)
9946             (gnus-completing-read default prom
9947                                   gnus-active-hashtb
9948                                   'gnus-valid-move-group-p
9949                                   nil prefix
9950                                   'gnus-group-history))
9951            ((= 1 (length split-name))
9952             (gnus-completing-read (car split-name) prom
9953                                   gnus-active-hashtb
9954                                   'gnus-valid-move-group-p
9955                                   nil nil
9956                                   'gnus-group-history))
9957            (t
9958             (gnus-completing-read nil prom
9959                                   (mapcar (lambda (el) (list el))
9960                                           (nreverse split-name))
9961                                   nil nil nil
9962                                   'gnus-group-history))))
9963          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9964     (when to-newsgroup
9965       (if (or (string= to-newsgroup "")
9966               (string= to-newsgroup prefix))
9967           (setq to-newsgroup default))
9968       (unless to-newsgroup
9969         (error "No group name entered"))
9970       (or (gnus-active to-newsgroup)
9971           (gnus-activate-group to-newsgroup nil nil to-method)
9972           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9973                                      to-newsgroup))
9974               (or (and (gnus-request-create-group to-newsgroup to-method)
9975                        (gnus-activate-group
9976                         to-newsgroup nil nil to-method)
9977                        (gnus-subscribe-group to-newsgroup))
9978                   (error "Couldn't create group %s" to-newsgroup)))
9979           (error "No such group: %s" to-newsgroup)))
9980     to-newsgroup))
9981
9982 (defun gnus-summary-save-parts (type dir n &optional reverse)
9983   "Save parts matching TYPE to DIR.
9984 If REVERSE, save parts that do not match TYPE."
9985   (interactive
9986    (list (read-string "Save parts of type: "
9987                       (or (car gnus-summary-save-parts-type-history)
9988                           gnus-summary-save-parts-default-mime)
9989                       'gnus-summary-save-parts-type-history)
9990          (setq gnus-summary-save-parts-last-directory
9991                (read-file-name "Save to directory: "
9992                                gnus-summary-save-parts-last-directory
9993                                nil t))
9994          current-prefix-arg))
9995   (gnus-summary-iterate n
9996     (let ((gnus-display-mime-function nil)
9997           (gnus-inhibit-treatment t))
9998       (gnus-summary-select-article))
9999     (save-excursion
10000       (set-buffer gnus-article-buffer)
10001       (let ((handles (or gnus-article-mime-handles
10002                          (mm-dissect-buffer) (mm-uu-dissect))))
10003         (when handles
10004           (gnus-summary-save-parts-1 type dir handles reverse)
10005           (unless gnus-article-mime-handles ;; Don't destroy this case.
10006             (mm-destroy-parts handles)))))))
10007
10008 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10009   (if (stringp (car handle))
10010       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10011               (cdr handle))
10012     (when (if reverse
10013               (not (string-match type (mm-handle-media-type handle)))
10014             (string-match type (mm-handle-media-type handle)))
10015       (let ((file (expand-file-name
10016                    (file-name-nondirectory
10017                     (or
10018                      (mail-content-type-get
10019                       (mm-handle-disposition handle) 'filename)
10020                      (concat gnus-newsgroup-name
10021                              "." (number-to-string
10022                                   (cdr gnus-article-current)))))
10023                    dir)))
10024         (unless (file-exists-p file)
10025           (mm-save-part-to-file handle file))))))
10026
10027 ;; Summary extract commands
10028
10029 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10030   (let ((buffer-read-only nil)
10031         (article (gnus-summary-article-number))
10032         after-article b e)
10033     (unless (gnus-summary-goto-subject article)
10034       (error "No such article: %d" article))
10035     (gnus-summary-position-point)
10036     ;; If all commands are to be bunched up on one line, we collect
10037     ;; them here.
10038     (unless gnus-view-pseudos-separately
10039       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10040             files action)
10041         (while ps
10042           (setq action (cdr (assq 'action (car ps))))
10043           (setq files (list (cdr (assq 'name (car ps)))))
10044           (while (and ps (cdr ps)
10045                       (string= (or action "1")
10046                                (or (cdr (assq 'action (cadr ps))) "2")))
10047             (push (cdr (assq 'name (cadr ps))) files)
10048             (setcdr ps (cddr ps)))
10049           (when files
10050             (when (not (string-match "%s" action))
10051               (push " " files))
10052             (push " " files)
10053             (when (assq 'execute (car ps))
10054               (setcdr (assq 'execute (car ps))
10055                       (funcall (if (string-match "%s" action)
10056                                    'format 'concat)
10057                                action
10058                                (mapconcat
10059                                 (lambda (f)
10060                                   (if (equal f " ")
10061                                       f
10062                                     (mm-quote-arg f)))
10063                                 files " ")))))
10064           (setq ps (cdr ps)))))
10065     (if (and gnus-view-pseudos (not not-view))
10066         (while pslist
10067           (when (assq 'execute (car pslist))
10068             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10069                                   (eq gnus-view-pseudos 'not-confirm)))
10070           (setq pslist (cdr pslist)))
10071       (save-excursion
10072         (while pslist
10073           (setq after-article (or (cdr (assq 'article (car pslist)))
10074                                   (gnus-summary-article-number)))
10075           (gnus-summary-goto-subject after-article)
10076           (forward-line 1)
10077           (setq b (point))
10078           (insert "    " (file-name-nondirectory
10079                           (cdr (assq 'name (car pslist))))
10080                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10081           (setq e (point))
10082           (forward-line -1)             ; back to `b'
10083           (gnus-add-text-properties
10084            b (1- e) (list 'gnus-number gnus-reffed-article-number
10085                           gnus-mouse-face-prop gnus-mouse-face))
10086           (gnus-data-enter
10087            after-article gnus-reffed-article-number
10088            gnus-unread-mark b (car pslist) 0 (- e b))
10089           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10090           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10091           (setq pslist (cdr pslist)))))))
10092
10093 (defun gnus-pseudos< (p1 p2)
10094   (let ((c1 (cdr (assq 'action p1)))
10095         (c2 (cdr (assq 'action p2))))
10096     (and c1 c2 (string< c1 c2))))
10097
10098 (defun gnus-request-pseudo-article (props)
10099   (cond ((assq 'execute props)
10100          (gnus-execute-command (cdr (assq 'execute props)))))
10101   (let ((gnus-current-article (gnus-summary-article-number)))
10102     (gnus-run-hooks 'gnus-mark-article-hook)))
10103
10104 (defun gnus-execute-command (command &optional automatic)
10105   (save-excursion
10106     (gnus-article-setup-buffer)
10107     (set-buffer gnus-article-buffer)
10108     (setq buffer-read-only nil)
10109     (let ((command (if automatic command
10110                      (read-string "Command: " (cons command 0)))))
10111       (erase-buffer)
10112       (insert "$ " command "\n\n")
10113       (if gnus-view-pseudo-asynchronously
10114           (start-process "gnus-execute" (current-buffer) shell-file-name
10115                          shell-command-switch command)
10116         (call-process shell-file-name nil t nil
10117                       shell-command-switch command)))))
10118
10119 ;; Summary kill commands.
10120
10121 (defun gnus-summary-edit-global-kill (article)
10122   "Edit the \"global\" kill file."
10123   (interactive (list (gnus-summary-article-number)))
10124   (gnus-group-edit-global-kill article))
10125
10126 (defun gnus-summary-edit-local-kill ()
10127   "Edit a local kill file applied to the current newsgroup."
10128   (interactive)
10129   (setq gnus-current-headers (gnus-summary-article-header))
10130   (gnus-group-edit-local-kill
10131    (gnus-summary-article-number) gnus-newsgroup-name))
10132
10133 ;;; Header reading.
10134
10135 (defun gnus-read-header (id &optional header)
10136   "Read the headers of article ID and enter them into the Gnus system."
10137   (let ((group gnus-newsgroup-name)
10138         (gnus-override-method
10139          (or
10140           gnus-override-method
10141           (and (gnus-news-group-p gnus-newsgroup-name)
10142                (car (gnus-refer-article-methods)))))
10143         where)
10144     ;; First we check to see whether the header in question is already
10145     ;; fetched.
10146     (if (stringp id)
10147         ;; This is a Message-ID.
10148         (setq header (or header (gnus-id-to-header id)))
10149       ;; This is an article number.
10150       (setq header (or header (gnus-summary-article-header id))))
10151     (if (and header
10152              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10153         ;; We have found the header.
10154         header
10155       ;; If this is a sparse article, we have to nix out its
10156       ;; previous entry in the thread hashtb.
10157       (when (and header
10158                  (gnus-summary-article-sparse-p (mail-header-number header)))
10159         (let* ((parent (gnus-parent-id (mail-header-references header)))
10160                (thread (and parent (gnus-id-to-thread parent))))
10161           (when thread
10162             (delq (assq header thread) thread))))
10163       ;; We have to really fetch the header to this article.
10164       (save-excursion
10165         (set-buffer nntp-server-buffer)
10166         (when (setq where (gnus-request-head id group))
10167           (nnheader-fold-continuation-lines)
10168           (goto-char (point-max))
10169           (insert ".\n")
10170           (goto-char (point-min))
10171           (insert "211 ")
10172           (princ (cond
10173                   ((numberp id) id)
10174                   ((cdr where) (cdr where))
10175                   (header (mail-header-number header))
10176                   (t gnus-reffed-article-number))
10177                  (current-buffer))
10178           (insert " Article retrieved.\n"))
10179         (if (or (not where)
10180                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10181             ()                          ; Malformed head.
10182           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10183             (when (and (stringp id)
10184                        (not (string= (gnus-group-real-name group)
10185                                      (car where))))
10186               ;; If we fetched by Message-ID and the article came
10187               ;; from a different group, we fudge some bogus article
10188               ;; numbers for this article.
10189               (mail-header-set-number header gnus-reffed-article-number))
10190             (save-excursion
10191               (set-buffer gnus-summary-buffer)
10192               (decf gnus-reffed-article-number)
10193               (gnus-remove-header (mail-header-number header))
10194               (push header gnus-newsgroup-headers)
10195               (setq gnus-current-headers header)
10196               (push (mail-header-number header) gnus-newsgroup-limit)))
10197           header)))))
10198
10199 (defun gnus-remove-header (number)
10200   "Remove header NUMBER from `gnus-newsgroup-headers'."
10201   (if (and gnus-newsgroup-headers
10202            (= number (mail-header-number (car gnus-newsgroup-headers))))
10203       (pop gnus-newsgroup-headers)
10204     (let ((headers gnus-newsgroup-headers))
10205       (while (and (cdr headers)
10206                   (not (= number (mail-header-number (cadr headers)))))
10207         (pop headers))
10208       (when (cdr headers)
10209         (setcdr headers (cddr headers))))))
10210
10211 ;;;
10212 ;;; summary highlights
10213 ;;;
10214
10215 (defun gnus-highlight-selected-summary ()
10216   "Highlight selected article in summary buffer."
10217   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10218   (when gnus-summary-selected-face
10219     (save-excursion
10220       (let* ((beg (progn (beginning-of-line) (point)))
10221              (end (progn (end-of-line) (point)))
10222              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10223              (from (if (get-text-property beg gnus-mouse-face-prop)
10224                        beg
10225                      (or (next-single-property-change
10226                           beg gnus-mouse-face-prop nil end)
10227                          beg)))
10228              (to
10229               (if (= from end)
10230                   (- from 2)
10231                 (or (next-single-property-change
10232                      from gnus-mouse-face-prop nil end)
10233                     end))))
10234         ;; If no mouse-face prop on line we will have to = from = end,
10235         ;; so we highlight the entire line instead.
10236         (when (= (+ to 2) from)
10237           (setq from beg)
10238           (setq to end))
10239         (if gnus-newsgroup-selected-overlay
10240             ;; Move old overlay.
10241             (gnus-move-overlay
10242              gnus-newsgroup-selected-overlay from to (current-buffer))
10243           ;; Create new overlay.
10244           (gnus-overlay-put
10245            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10246            'face gnus-summary-selected-face))))))
10247
10248 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10249 (defun gnus-summary-highlight-line ()
10250   "Highlight current line according to `gnus-summary-highlight'."
10251   (let* ((list gnus-summary-highlight)
10252          (p (point))
10253          (end (progn (end-of-line) (point)))
10254          ;; now find out where the line starts and leave point there.
10255          (beg (progn (beginning-of-line) (point)))
10256          (article (gnus-summary-article-number))
10257          (score (or (cdr (assq (or article gnus-current-article)
10258                                gnus-newsgroup-scored))
10259                     gnus-summary-default-score 0))
10260          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10261          (inhibit-read-only t))
10262     ;; Eval the cars of the lists until we find a match.
10263     (let ((default gnus-summary-default-score)
10264           (default-high gnus-summary-default-high-score)
10265           (default-low gnus-summary-default-low-score))
10266       (while (and list
10267                   (not (eval (caar list))))
10268         (setq list (cdr list))))
10269     (let ((face (cdar list)))
10270       (unless (eq face (get-text-property beg 'face))
10271         (gnus-put-text-property-excluding-characters-with-faces
10272          beg end 'face
10273          (setq face (if (boundp face) (symbol-value face) face)))
10274         (when gnus-summary-highlight-line-function
10275           (funcall gnus-summary-highlight-line-function article face))))
10276     (goto-char p)))
10277
10278 (defun gnus-update-read-articles (group unread &optional compute)
10279   "Update the list of read articles in GROUP."
10280   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10281          (entry (gnus-gethash group gnus-newsrc-hashtb))
10282          (info (nth 2 entry))
10283          (prev 1)
10284          (unread (sort (copy-sequence unread) '<))
10285          read)
10286     (if (or (not info) (not active))
10287         ;; There is no info on this group if it was, in fact,
10288         ;; killed.  Gnus stores no information on killed groups, so
10289         ;; there's nothing to be done.
10290         ;; One could store the information somewhere temporarily,
10291         ;; perhaps...  Hmmm...
10292         ()
10293       ;; Remove any negative articles numbers.
10294       (while (and unread (< (car unread) 0))
10295         (setq unread (cdr unread)))
10296       ;; Remove any expired article numbers
10297       (while (and unread (< (car unread) (car active)))
10298         (setq unread (cdr unread)))
10299       ;; Compute the ranges of read articles by looking at the list of
10300       ;; unread articles.
10301       (while unread
10302         (when (/= (car unread) prev)
10303           (push (if (= prev (1- (car unread))) prev
10304                   (cons prev (1- (car unread))))
10305                 read))
10306         (setq prev (1+ (car unread)))
10307         (setq unread (cdr unread)))
10308       (when (<= prev (cdr active))
10309         (push (cons prev (cdr active)) read))
10310       (setq read (if (> (length read) 1) (nreverse read) read))
10311       (if compute
10312           read
10313         (save-excursion
10314           (let (setmarkundo)
10315             ;; Propagate the read marks to the backend.
10316             (when (gnus-check-backend-function 'request-set-mark group)
10317               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10318                     (add (gnus-remove-from-range read (gnus-info-read info))))
10319                 (when (or add del)
10320                   (unless (gnus-check-group group)
10321                     (error "Can't open server for %s" group))
10322                   (gnus-request-set-mark
10323                    group (delq nil (list (if add (list add 'add '(read)))
10324                                          (if del (list del 'del '(read))))))
10325                   (setq setmarkundo
10326                         `(gnus-request-set-mark
10327                           ,group
10328                           ',(delq nil (list
10329                                        (if del (list del 'add '(read)))
10330                                        (if add (list add 'del '(read))))))))))
10331             (set-buffer gnus-group-buffer)
10332             (gnus-undo-register
10333               `(progn
10334                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10335                  (gnus-info-set-read ',info ',(gnus-info-read info))
10336                  (gnus-get-unread-articles-in-group ',info
10337                                                     (gnus-active ,group))
10338                  (gnus-group-update-group ,group t)
10339                  ,setmarkundo))))
10340         ;; Enter this list into the group info.
10341         (gnus-info-set-read info read)
10342         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10343         (gnus-get-unread-articles-in-group info (gnus-active group))
10344         t))))
10345
10346 (defun gnus-offer-save-summaries ()
10347   "Offer to save all active summary buffers."
10348   (let (buffers)
10349     ;; Go through all buffers and find all summaries.
10350     (dolist (buffer (buffer-list))
10351       (when (and (setq buffer (buffer-name buffer))
10352                  (string-match "Summary" buffer)
10353                  (save-excursion
10354                    (set-buffer buffer)
10355                    ;; We check that this is, indeed, a summary buffer.
10356                    (and (eq major-mode 'gnus-summary-mode)
10357                         ;; Also make sure this isn't bogus.
10358                         gnus-newsgroup-prepared
10359                         ;; Also make sure that this isn't a
10360                         ;; dead summary buffer.
10361                         (not gnus-dead-summary-mode))))
10362         (push buffer buffers)))
10363     ;; Go through all these summary buffers and offer to save them.
10364     (when buffers
10365       (save-excursion
10366         (map-y-or-n-p
10367          "Update summary buffer %s? "
10368          (lambda (buf)
10369            (switch-to-buffer buf)
10370            (gnus-summary-exit))
10371          buffers)))))
10372
10373 (defun gnus-summary-setup-default-charset ()
10374   "Setup newsgroup default charset."
10375   (if (equal gnus-newsgroup-name "nndraft:drafts")
10376       (setq gnus-newsgroup-charset nil)
10377     (let* ((ignored-charsets
10378             (or gnus-newsgroup-ephemeral-ignored-charsets
10379                 (append
10380                  (and gnus-newsgroup-name
10381                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10382                  gnus-newsgroup-ignored-charsets))))
10383       (setq gnus-newsgroup-charset
10384             (or gnus-newsgroup-ephemeral-charset
10385                 (and gnus-newsgroup-name
10386                      (gnus-parameter-charset gnus-newsgroup-name))
10387                 gnus-default-charset))
10388       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10389            ignored-charsets))))
10390
10391 ;;;
10392 ;;; Mime Commands
10393 ;;;
10394
10395 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10396   "Display the current article buffer fully MIME-buttonized.
10397 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10398 treated as multipart/mixed."
10399   (interactive "P")
10400   (require 'gnus-art)
10401   (let ((gnus-unbuttonized-mime-types nil)
10402         (gnus-mime-display-multipart-as-mixed show-all-parts))
10403     (gnus-summary-show-article)))
10404
10405 (defun gnus-summary-repair-multipart (article)
10406   "Add a Content-Type header to a multipart article without one."
10407   (interactive (list (gnus-summary-article-number)))
10408   (gnus-with-article article
10409     (message-narrow-to-head)
10410     (message-remove-header "Mime-Version")
10411     (goto-char (point-max))
10412     (insert "Mime-Version: 1.0\n")
10413     (widen)
10414     (when (search-forward "\n--" nil t)
10415       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10416         (message-narrow-to-head)
10417         (message-remove-header "Content-Type")
10418         (goto-char (point-max))
10419         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10420                         separator))
10421         (widen))))
10422   (let (gnus-mark-article-hook)
10423     (gnus-summary-select-article t t nil article)))
10424
10425 (defun gnus-summary-toggle-display-buttonized ()
10426   "Toggle the buttonizing of the article buffer."
10427   (interactive)
10428   (require 'gnus-art)
10429   (if (setq gnus-inhibit-mime-unbuttonizing
10430             (not gnus-inhibit-mime-unbuttonizing))
10431       (let ((gnus-unbuttonized-mime-types nil))
10432         (gnus-summary-show-article))
10433     (gnus-summary-show-article)))
10434
10435 ;;;
10436 ;;; Generic summary marking commands
10437 ;;;
10438
10439 (defvar gnus-summary-marking-alist
10440   '((read gnus-del-mark "d")
10441     (unread gnus-unread-mark "u")
10442     (ticked gnus-ticked-mark "!")
10443     (dormant gnus-dormant-mark "?")
10444     (expirable gnus-expirable-mark "e"))
10445   "An alist of names/marks/keystrokes.")
10446
10447 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10448 (defvar gnus-summary-mark-map)
10449
10450 (defun gnus-summary-make-all-marking-commands ()
10451   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10452   (dolist (elem gnus-summary-marking-alist)
10453     (apply 'gnus-summary-make-marking-command elem)))
10454
10455 (defun gnus-summary-make-marking-command (name mark keystroke)
10456   (let ((map (make-sparse-keymap)))
10457     (define-key gnus-summary-generic-mark-map keystroke map)
10458     (dolist (lway `((next "next" next nil "n")
10459                     (next-unread "next unread" next t "N")
10460                     (prev "previous" prev nil "p")
10461                     (prev-unread "previous unread" prev t "P")
10462                     (nomove "" nil nil ,keystroke)))
10463       (let ((func (gnus-summary-make-marking-command-1
10464                    mark (car lway) lway name)))
10465         (setq func (eval func))
10466         (define-key map (nth 4 lway) func)))))
10467
10468 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10469   `(defun ,(intern
10470             (format "gnus-summary-put-mark-as-%s%s"
10471                     name (if (eq way 'nomove)
10472                              ""
10473                            (concat "-" (symbol-name way)))))
10474      (n)
10475      ,(format
10476        "Mark the current article as %s%s.
10477 If N, the prefix, then repeat N times.
10478 If N is negative, move in reverse order.
10479 The difference between N and the actual number of articles marked is
10480 returned."
10481        name (cadr lway))
10482      (interactive "p")
10483      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10484
10485 (defun gnus-summary-generic-mark (n mark move unread)
10486   "Mark N articles with MARK."
10487   (unless (eq major-mode 'gnus-summary-mode)
10488     (error "This command can only be used in the summary buffer"))
10489   (gnus-summary-show-thread)
10490   (let ((nummove
10491          (cond
10492           ((eq move 'next) 1)
10493           ((eq move 'prev) -1)
10494           (t 0))))
10495     (if (zerop nummove)
10496         (setq n 1)
10497       (when (< n 0)
10498         (setq n (abs n)
10499               nummove (* -1 nummove))))
10500     (while (and (> n 0)
10501                 (gnus-summary-mark-article nil mark)
10502                 (zerop (gnus-summary-next-subject nummove unread t)))
10503       (setq n (1- n)))
10504     (when (/= 0 n)
10505       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10506     (gnus-summary-recenter)
10507     (gnus-summary-position-point)
10508     (gnus-set-mode-line 'summary)
10509     n))
10510
10511 (defun gnus-summary-insert-articles (articles)
10512   (when (setq articles
10513               (gnus-set-difference articles
10514                                    (mapcar (lambda (h) (mail-header-number h))
10515                                            gnus-newsgroup-headers)))
10516     (setq gnus-newsgroup-headers
10517           (merge 'list
10518                  gnus-newsgroup-headers
10519                  (gnus-fetch-headers articles)
10520                  'gnus-article-sort-by-number))
10521     ;; Suppress duplicates?
10522     (when gnus-suppress-duplicates
10523       (gnus-dup-suppress-articles))
10524
10525     ;; We might want to build some more threads first.
10526     (when (and gnus-fetch-old-headers
10527                (eq gnus-headers-retrieved-by 'nov))
10528       (if (eq gnus-fetch-old-headers 'invisible)
10529           (gnus-build-all-threads)
10530         (gnus-build-old-threads)))
10531     ;; Let the Gnus agent mark articles as read.
10532     (when gnus-agent
10533       (gnus-agent-get-undownloaded-list))
10534     ;; Remove list identifiers from subject
10535     (when gnus-list-identifiers
10536       (gnus-summary-remove-list-identifiers))
10537     ;; First and last article in this newsgroup.
10538     (when gnus-newsgroup-headers
10539       (setq gnus-newsgroup-begin
10540             (mail-header-number (car gnus-newsgroup-headers))
10541             gnus-newsgroup-end
10542             (mail-header-number
10543              (gnus-last-element gnus-newsgroup-headers))))
10544     (when gnus-use-scoring
10545       (gnus-possibly-score-headers))))
10546
10547 (defun gnus-summary-insert-old-articles (&optional all)
10548   "Insert all old articles in this group.
10549 If ALL is non-nil, already read articles become readable.
10550 If ALL is a number, fetch this number of articles."
10551   (interactive "P")
10552   (prog1
10553       (let ((old (mapcar 'car gnus-newsgroup-data))
10554             (i (car gnus-newsgroup-active))
10555             older len)
10556         (while (<= i (cdr gnus-newsgroup-active))
10557           (or (memq i old) (push i older))
10558           (incf i))
10559         (setq len (length older))
10560         (cond
10561          ((null older) nil)
10562          ((numberp all)
10563           (if (< all len)
10564               (setq older (subseq older 0 all))))
10565          (all nil)
10566          (t
10567           (if (and (numberp gnus-large-newsgroup)
10568                    (> len gnus-large-newsgroup))
10569               (let ((input
10570                      (read-string
10571                       (format
10572                        "How many articles from %s (default %d): "
10573                        (gnus-limit-string
10574                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10575                        len))))
10576                 (unless (string-match "^[ \t]*$" input)
10577                   (setq all (string-to-number input))
10578                   (if (< all len)
10579                       (setq older (subseq older 0 all))))))))
10580         (if (not older)
10581             (message "No old news.")
10582           (gnus-summary-insert-articles older)
10583           (gnus-summary-limit (gnus-union older old))))
10584     (gnus-summary-position-point)))
10585
10586 (defun gnus-summary-insert-new-articles ()
10587   "Insert all new articles in this group."
10588   (interactive)
10589   (prog1
10590       (let ((old (mapcar 'car gnus-newsgroup-data))
10591             (old-active gnus-newsgroup-active)
10592             (nnmail-fetched-sources (list t))
10593             i new)
10594         (setq gnus-newsgroup-active
10595               (gnus-activate-group gnus-newsgroup-name 'scan))
10596         (setq i (1+ (cdr old-active)))
10597         (while (<= i (cdr gnus-newsgroup-active))
10598           (push i new)
10599           (incf i))
10600         (if (not new)
10601             (message "No gnus is bad news.")
10602           (setq new (nreverse new))
10603           (gnus-summary-insert-articles new)
10604           (setq gnus-newsgroup-unreads
10605                 (append gnus-newsgroup-unreads new))
10606           (gnus-summary-limit (gnus-union old new))))
10607     (gnus-summary-position-point)))
10608
10609 (gnus-summary-make-all-marking-commands)
10610
10611 (gnus-ems-redefine)
10612
10613 (provide 'gnus-sum)
10614
10615 (run-hooks 'gnus-sum-load-hook)
10616
10617 ;;; gnus-sum.el ends here