* gnus-start.el (gnus-close-all-servers): New function.
[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-zcore-fuzz 0
203   "*Fuzziness factor for the zcore in the summary buffer.
204 Articles with scores closer than this to `gnus-summary-default-score'
205 will not be marked."
206   :group 'gnus-summary-format
207   :type 'integer)
208
209 (defcustom gnus-simplify-subject-fuzzy-regexp nil
210   "*Strings to be removed when doing fuzzy matches.
211 This can either be a regular expression or list of regular expressions
212 that will be removed from subject strings if fuzzy subject
213 simplification is selected."
214   :group 'gnus-thread
215   :type '(repeat regexp))
216
217 (defcustom gnus-show-threads t
218   "*If non-nil, display threads in summary mode."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-hide-subtree nil
223   "*If non-nil, hide all threads initially.
224 If threads are hidden, you have to run the command
225 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
226 to expose hidden threads."
227   :group 'gnus-thread
228   :type 'boolean)
229
230 (defcustom gnus-thread-hide-killed t
231   "*If non-nil, hide killed threads automatically."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-ignore-subject t
236   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
237 If nil, articles that have different subjects from their parents will
238 start separate threads."
239   :group 'gnus-thread
240   :type 'boolean)
241
242 (defcustom gnus-thread-operation-ignore-subject t
243   "*If non-nil, subjects will be ignored when doing thread commands.
244 This affects commands like `gnus-summary-kill-thread' and
245 `gnus-summary-lower-thread'.
246
247 If this variable is nil, articles in the same thread with different
248 subjects will not be included in the operation in question.  If this
249 variable is `fuzzy', only articles that have subjects that are fuzzily
250 equal will be included."
251   :group 'gnus-thread
252   :type '(choice (const :tag "off" nil)
253                  (const fuzzy)
254                  (sexp :tag "on" t)))
255
256 (defcustom gnus-thread-indent-level 4
257   "*Number that says how much each sub-thread should be indented."
258   :group 'gnus-thread
259   :type 'integer)
260
261 (defcustom gnus-auto-extend-newsgroup t
262   "*If non-nil, extend newsgroup forward and backward when requested."
263   :group 'gnus-summary-choose
264   :type 'boolean)
265
266 (defcustom gnus-auto-select-first t
267   "*If nil, don't select the first unread article when entering a group.
268 If this variable is `best', select the highest-scored unread article
269 in the group.  If t, select the first unread article.
270
271 This variable can also be a function to place point on a likely
272 subject line.  Useful values include `gnus-summary-first-unread-subject',
273 `gnus-summary-first-unread-article' and
274 `gnus-summary-best-unread-article'.
275
276 If you want to prevent automatic selection of the first unread article
277 in some newsgroups, set the variable to nil in
278 `gnus-select-group-hook'."
279   :group 'gnus-group-select
280   :type '(choice (const :tag "none" nil)
281                  (const best)
282                  (sexp :menu-tag "first" t)
283                  (function-item gnus-summary-first-unread-subject)
284                  (function-item gnus-summary-first-unread-article)
285                  (function-item gnus-summary-best-unread-article)))
286
287 (defcustom gnus-auto-select-next t
288   "*If non-nil, offer to go to the next group from the end of the previous.
289 If the value is t and the next newsgroup is empty, Gnus will exit
290 summary mode and go back to group mode.  If the value is neither nil
291 nor t, Gnus will select the following unread newsgroup.  In
292 particular, if the value is the symbol `quietly', the next unread
293 newsgroup will be selected without any confirmation, and if it is
294 `almost-quietly', the next group will be selected without any
295 confirmation if you are located on the last article in the group.
296 Finally, if this variable is `slightly-quietly', the `Z n' command
297 will go to the next group without confirmation."
298   :group 'gnus-summary-maneuvering
299   :type '(choice (const :tag "off" nil)
300                  (const quietly)
301                  (const almost-quietly)
302                  (const slightly-quietly)
303                  (sexp :menu-tag "on" t)))
304
305 (defcustom gnus-auto-select-same nil
306   "*If non-nil, select the next article with the same subject.
307 If there are no more articles with the same subject, go to
308 the first unread article."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-summary-check-current nil
313   "*If non-nil, consider the current article when moving.
314 The \"unread\" movement commands will stay on the same line if the
315 current article is unread."
316   :group 'gnus-summary-maneuvering
317   :type 'boolean)
318
319 (defcustom gnus-auto-center-summary t
320   "*If non-nil, always center the current summary buffer.
321 In particular, if `vertical' do only vertical recentering.  If non-nil
322 and non-`vertical', do both horizontal and vertical recentering."
323   :group 'gnus-summary-maneuvering
324   :type '(choice (const :tag "none" nil)
325                  (const vertical)
326                  (integer :tag "height")
327                  (sexp :menu-tag "both" t)))
328
329 (defcustom gnus-show-all-headers nil
330   "*If non-nil, don't hide any headers."
331   :group 'gnus-article-hiding
332   :group 'gnus-article-headers
333   :type 'boolean)
334
335 (defcustom gnus-summary-ignore-duplicates nil
336   "*If non-nil, ignore articles with identical Message-ID headers."
337   :group 'gnus-summary
338   :type 'boolean)
339
340 (defcustom gnus-single-article-buffer t
341   "*If non-nil, display all articles in the same buffer.
342 If nil, each group will get its own article buffer."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-break-pages t
347   "*If non-nil, do page breaking on articles.
348 The page delimiter is specified by the `gnus-page-delimiter'
349 variable."
350   :group 'gnus-article-various
351   :type 'boolean)
352
353 (defcustom gnus-move-split-methods nil
354   "*Variable used to suggest where articles are to be moved to.
355 It uses the same syntax as the `gnus-split-methods' variable.
356 However, whereas `gnus-split-methods' specifies file names as targets,
357 this variable specifies group names."
358   :group 'gnus-summary-mail
359   :type '(repeat (choice (list :value (fun) function)
360                          (cons :value ("" "") regexp (repeat string))
361                          (sexp :value nil))))
362
363 (defcustom gnus-unread-mark ?  ;Whitespace
364   "*Mark used for unread articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-ticked-mark ?!
369   "*Mark used for ticked articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-dormant-mark ??
374   "*Mark used for dormant articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-del-mark ?r
379   "*Mark used for del'd articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-read-mark ?R
384   "*Mark used for read articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-expirable-mark ?E
389   "*Mark used for expirable articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-killed-mark ?K
394   "*Mark used for killed articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-souped-mark ?F
399   "*Mark used for souped articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-kill-file-mark ?X
404   "*Mark used for articles killed by kill files."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-low-score-mark ?Y
409   "*Mark used for articles with a low score."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-catchup-mark ?C
414   "*Mark used for articles that are caught up."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-replied-mark ?A
419   "*Mark used for articles that have been replied to."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-forwarded-mark ?O
424   "*Mark used for articles that have been forwarded."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-cached-mark ?*
429   "*Mark used for articles that are in the cache."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-saved-mark ?S
434   "*Mark used for articles that have been saved to."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-no-mark ?  ;Whitespace
439   "*Mark used for articles that have no other secondary mark."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-ancient-mark ?O
444   "*Mark used for ancient articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-sparse-mark ?Q
449   "*Mark used for sparsely reffed articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-canceled-mark ?G
454   "*Mark used for canceled articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-duplicate-mark ?M
459   "*Mark used for duplicate articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-undownloaded-mark ?@
464   "*Mark used for articles that weren't downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-downloadable-mark ?%
469   "*Mark used for articles that are to be downloaded."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-unsendable-mark ?=
474   "*Mark used for articles that won't be sent."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-over-mark ?+
479   "*Score mark used for articles with high scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-score-below-mark ?-
484   "*Score mark used for articles with low scores."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-empty-thread-mark ?  ;Whitespace
489   "*There is no thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-not-empty-thread-mark ?=
494   "*There is a thread under the article."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-view-pseudo-asynchronously nil
499   "*If non-nil, Gnus will view pseudo-articles asynchronously."
500   :group 'gnus-extract-view
501   :type 'boolean)
502
503 (defcustom gnus-auto-expirable-marks
504   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
505         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
506         gnus-souped-mark gnus-duplicate-mark)
507   "*The list of marks converted into expiration if a group is auto-expirable."
508   :version "21.1"
509   :group 'gnus-summary
510   :type '(repeat character))
511
512 (defcustom gnus-inhibit-user-auto-expire t
513   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
514   :version "21.1"
515   :group 'gnus-summary
516   :type 'boolean)
517
518 (defcustom gnus-view-pseudos nil
519   "*If `automatic', pseudo-articles will be viewed automatically.
520 If `not-confirm', pseudos will be viewed automatically, and the user
521 will not be asked to confirm the command."
522   :group 'gnus-extract-view
523   :type '(choice (const :tag "off" nil)
524                  (const automatic)
525                  (const not-confirm)))
526
527 (defcustom gnus-view-pseudos-separately t
528   "*If non-nil, one pseudo-article will be created for each file to be viewed.
529 If nil, all files that use the same viewing command will be given as a
530 list of parameters to that command."
531   :group 'gnus-extract-view
532   :type 'boolean)
533
534 (defcustom gnus-insert-pseudo-articles t
535   "*If non-nil, insert pseudo-articles when decoding articles."
536   :group 'gnus-extract-view
537   :type 'boolean)
538
539 (defcustom gnus-summary-dummy-line-format
540   "  %(:                          :%) %S\n"
541   "*The format specification for the dummy roots in the summary buffer.
542 It works along the same lines as a normal formatting string,
543 with some simple extensions.
544
545 %S  The subject"
546   :group 'gnus-threading
547   :type 'string)
548
549 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
550   "*The format specification for the summary mode line.
551 It works along the same lines as a normal formatting string,
552 with some simple extensions:
553
554 %G  Group name
555 %p  Unprefixed group name
556 %A  Current article number
557 %z  Current article score
558 %V  Gnus version
559 %U  Number of unread articles in the group
560 %e  Number of unselected articles in the group
561 %Z  A string with unread/unselected article counts
562 %g  Shortish group name
563 %S  Subject of the current article
564 %u  User-defined spec
565 %s  Current score file name
566 %d  Number of dormant articles
567 %r  Number of articles that have been marked as read in this session
568 %E  Number of articles expunged by the score files"
569   :group 'gnus-summary-format
570   :type 'string)
571
572 (defcustom gnus-list-identifiers nil
573   "Regexp that matches list identifiers to be removed from subject.
574 This can also be a list of regexps."
575   :version "21.1"
576   :group 'gnus-summary-format
577   :group 'gnus-article-hiding
578   :type '(choice (const :tag "none" nil)
579                  (regexp :value ".*")
580                  (repeat :value (".*") regexp)))
581
582 (defcustom gnus-summary-mark-below 0
583   "*Mark all articles with a score below this variable as read.
584 This variable is local to each summary buffer and usually set by the
585 score file."
586   :group 'gnus-score-default
587   :type 'integer)
588
589 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
590   "*List of functions used for sorting articles in the summary buffer.
591
592 Each function takes two articles and returns non-nil if the first
593 article should be sorted before the other.  If you use more than one
594 function, the primary sort function should be the last.  You should
595 probably always include `gnus-article-sort-by-number' in the list of
596 sorting functions -- preferably first.  Also note that sorting by date
597 is often much slower than sorting by number, and the sorting order is
598 very similar.  (Sorting by date means sorting by the time the message
599 was sent, sorting by number means sorting by arrival time.)
600
601 Ready-made functions include `gnus-article-sort-by-number',
602 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
603 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
604
605 When threading is turned on, the variable `gnus-thread-sort-functions'
606 controls how articles are sorted."
607   :group 'gnus-summary-sort
608   :type '(repeat (choice (function-item gnus-article-sort-by-number)
609                          (function-item gnus-article-sort-by-author)
610                          (function-item gnus-article-sort-by-subject)
611                          (function-item gnus-article-sort-by-date)
612                          (function-item gnus-article-sort-by-score)
613                          (function :tag "other"))))
614
615 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
616   "*List of functions used for sorting threads in the summary buffer.
617 By default, threads are sorted by article number.
618
619 Each function takes two threads and returns non-nil if the first
620 thread should be sorted before the other.  If you use more than one
621 function, the primary sort function should be the last.  You should
622 probably always include `gnus-thread-sort-by-number' in the list of
623 sorting functions -- preferably first.  Also note that sorting by date
624 is often much slower than sorting by number, and the sorting order is
625 very similar.  (Sorting by date means sorting by the time the message
626 was sent, sorting by number means sorting by arrival time.)
627
628 Ready-made functions include `gnus-thread-sort-by-number',
629 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
630 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
631 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
632
633 When threading is turned off, the variable
634 `gnus-article-sort-functions' controls how articles are sorted."
635   :group 'gnus-summary-sort
636   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
637                          (function-item gnus-thread-sort-by-author)
638                          (function-item gnus-thread-sort-by-subject)
639                          (function-item gnus-thread-sort-by-date)
640                          (function-item gnus-thread-sort-by-score)
641                          (function-item gnus-thread-sort-by-total-score)
642                          (function :tag "other"))))
643
644 (defcustom gnus-thread-score-function '+
645   "*Function used for calculating the total score of a thread.
646
647 The function is called with the scores of the article and each
648 subthread and should then return the score of the thread.
649
650 Some functions you can use are `+', `max', or `min'."
651   :group 'gnus-summary-sort
652   :type 'function)
653
654 (defcustom gnus-summary-expunge-below nil
655   "All articles that have a score less than this variable will be expunged.
656 This variable is local to the summary buffers."
657   :group 'gnus-score-default
658   :type '(choice (const :tag "off" nil)
659                  integer))
660
661 (defcustom gnus-thread-expunge-below nil
662   "All threads that have a total score less than this variable will be expunged.
663 See `gnus-thread-score-function' for en explanation of what a
664 \"thread score\" is.
665
666 This variable is local to the summary buffers."
667   :group 'gnus-threading
668   :group 'gnus-score-default
669   :type '(choice (const :tag "off" nil)
670                  integer))
671
672 (defcustom gnus-summary-mode-hook nil
673   "*A hook for Gnus summary mode.
674 This hook is run before any variables are set in the summary buffer."
675   :options '(turn-on-gnus-mailing-list-mode)
676   :group 'gnus-summary-various
677   :type 'hook)
678
679 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
680 (when (featurep 'xemacs)
681   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
682   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
683   (add-hook 'gnus-summary-mode-hook
684             'gnus-xmas-switch-horizontal-scrollbar-off))
685
686 (defcustom gnus-summary-menu-hook nil
687   "*Hook run after the creation of the summary mode menu."
688   :group 'gnus-summary-visual
689   :type 'hook)
690
691 (defcustom gnus-summary-exit-hook nil
692   "*A hook called on exit from the summary buffer.
693 It will be called with point in the group buffer."
694   :group 'gnus-summary-exit
695   :type 'hook)
696
697 (defcustom gnus-summary-prepare-hook nil
698   "*A hook called after the summary buffer has been generated.
699 If you want to modify the summary buffer, you can use this hook."
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 (defcustom gnus-summary-prepared-hook nil
704   "*A hook called as the last thing after the summary buffer has been generated."
705   :group 'gnus-summary-various
706   :type 'hook)
707
708 (defcustom gnus-summary-generate-hook nil
709   "*A hook run just before generating the summary buffer.
710 This hook is commonly used to customize threading variables and the
711 like."
712   :group 'gnus-summary-various
713   :type 'hook)
714
715 (defcustom gnus-select-group-hook nil
716   "*A hook called when a newsgroup is selected.
717
718 If you'd like to simplify subjects like the
719 `gnus-summary-next-same-subject' command does, you can use the
720 following hook:
721
722  (setq gnus-select-group-hook
723       (list
724         (lambda ()
725           (mapcar (lambda (header)
726                      (mail-header-set-subject
727                       header
728                       (gnus-simplify-subject
729                        (mail-header-subject header) 're-only)))
730                   gnus-newsgroup-headers))))"
731   :group 'gnus-group-select
732   :type 'hook)
733
734 (defcustom gnus-select-article-hook nil
735   "*A hook called when an article is selected."
736   :group 'gnus-summary-choose
737   :type 'hook)
738
739 (defcustom gnus-visual-mark-article-hook
740   (list 'gnus-highlight-selected-summary)
741   "*Hook run after selecting an article in the summary buffer.
742 It is meant to be used for highlighting the article in some way.  It
743 is not run if `gnus-visual' is nil."
744   :group 'gnus-summary-visual
745   :type 'hook)
746
747 (defcustom gnus-parse-headers-hook nil
748   "*A hook called before parsing the headers."
749   :group 'gnus-various
750   :type 'hook)
751
752 (defcustom gnus-exit-group-hook nil
753   "*A hook called when exiting summary mode.
754 This hook is not called from the non-updating exit commands like `Q'."
755   :group 'gnus-various
756   :type 'hook)
757
758 (defcustom gnus-summary-update-hook
759   (list 'gnus-summary-highlight-line)
760   "*A hook called when a summary line is changed.
761 The hook will not be called if `gnus-visual' is nil.
762
763 The default function `gnus-summary-highlight-line' will
764 highlight the line according to the `gnus-summary-highlight'
765 variable."
766   :group 'gnus-summary-visual
767   :type 'hook)
768
769 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
770   "*A hook called when an article is selected for the first time.
771 The hook is intended to mark an article as read (or unread)
772 automatically when it is selected."
773   :group 'gnus-summary-choose
774   :type 'hook)
775
776 (defcustom gnus-group-no-more-groups-hook nil
777   "*A hook run when returning to group mode having no more (unread) groups."
778   :group 'gnus-group-select
779   :type 'hook)
780
781 (defcustom gnus-ps-print-hook nil
782   "*A hook run before ps-printing something from Gnus."
783   :group 'gnus-summary
784   :type 'hook)
785
786 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
787   "Face used for highlighting the current article in the summary buffer."
788   :group 'gnus-summary-visual
789   :type 'face)
790
791 (defcustom gnus-summary-highlight
792   '(((= mark gnus-canceled-mark)
793      . gnus-summary-cancelled-face)
794     ((and (> score default)
795           (or (= mark gnus-dormant-mark)
796               (= mark gnus-ticked-mark)))
797      . gnus-summary-high-ticked-face)
798     ((and (< score default)
799           (or (= mark gnus-dormant-mark)
800               (= mark gnus-ticked-mark)))
801      . gnus-summary-low-ticked-face)
802     ((or (= mark gnus-dormant-mark)
803          (= mark gnus-ticked-mark))
804      . gnus-summary-normal-ticked-face)
805     ((and (> score default) (= mark gnus-ancient-mark))
806      . gnus-summary-high-ancient-face)
807     ((and (< score default) (= mark gnus-ancient-mark))
808      . gnus-summary-low-ancient-face)
809     ((= mark gnus-ancient-mark)
810      . gnus-summary-normal-ancient-face)
811     ((and (> score default) (= mark gnus-unread-mark))
812      . gnus-summary-high-unread-face)
813     ((and (< score default) (= mark gnus-unread-mark))
814      . gnus-summary-low-unread-face)
815     ((= mark gnus-unread-mark)
816      . gnus-summary-normal-unread-face)
817     ((and (> score default) (memq mark (list gnus-downloadable-mark
818                                              gnus-undownloaded-mark)))
819      . gnus-summary-high-unread-face)
820     ((and (< score default) (memq mark (list gnus-downloadable-mark
821                                              gnus-undownloaded-mark)))
822      . gnus-summary-low-unread-face)
823     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
824      . gnus-summary-normal-unread-face)
825     ((> score default)
826      . gnus-summary-high-read-face)
827     ((< score default)
828      . gnus-summary-low-read-face)
829     (t
830      . gnus-summary-normal-read-face))
831   "*Controls the highlighting of summary buffer lines.
832
833 A list of (FORM . FACE) pairs.  When deciding how a a particular
834 summary line should be displayed, each form is evaluated.  The content
835 of the face field after the first true form is used.  You can change
836 how those summary lines are displayed, by editing the face field.
837
838 You can use the following variables in the FORM field.
839
840 score:   The articles score
841 default: The default article score.
842 below:   The score below which articles are automatically marked as read.
843 mark:    The articles mark."
844   :group 'gnus-summary-visual
845   :type '(repeat (cons (sexp :tag "Form" nil)
846                        face)))
847
848 (defcustom gnus-alter-header-function nil
849   "Function called to allow alteration of article header structures.
850 The function is called with one parameter, the article header vector,
851 which it may alter in any way.")
852
853 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
854   "Variable that says which function should be used to decode a string with encoded words.")
855
856 (defcustom gnus-extra-headers nil
857   "*Extra headers to parse."
858   :version "21.1"
859   :group 'gnus-summary
860   :type '(repeat symbol))
861
862 (defcustom gnus-ignored-from-addresses
863   (and user-mail-address (regexp-quote user-mail-address))
864   "*Regexp of From headers that may be suppressed in favor of To headers."
865   :version "21.1"
866   :group 'gnus-summary
867   :type 'regexp)
868
869 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
870   "List of charsets that should be ignored.
871 When these charsets are used in the \"charset\" parameter, the
872 default charset will be used instead."
873   :version "21.1"
874   :type '(repeat symbol)
875   :group 'gnus-charset)
876
877 (gnus-define-group-parameter
878  ignored-charsets
879  :type list
880  :function-document
881  "Return the ignored charsets of GROUP."
882  :variable gnus-group-ignored-charsets-alist
883  :variable-default 
884  '(("alt\\.chinese\\.text" iso-8859-1))
885  :variable-document
886  "Alist of regexps (to match group names) and charsets that should be ignored.
887 When these charsets are used in the \"charset\" parameter, the
888 default charset will be used instead."
889  :variable-group gnus-charset
890  :variable-type '(repeat (cons (regexp :tag "Group")
891                                (repeat symbol)))
892  :parameter-type '(choice :tag "Ignored charsets" 
893                           :value nil
894                           (repeat (symbol)))
895  :parameter-document       "\
896 List of charsets that should be ignored.
897
898 When these charsets are used in the \"charset\" parameter, the
899 default charset will be used instead.")
900
901 (defcustom gnus-group-highlight-words-alist nil
902   "Alist of group regexps and highlight regexps.
903 This variable uses the same syntax as `gnus-emphasis-alist'."
904   :version "21.1"
905   :type '(repeat (cons (regexp :tag "Group")
906                        (repeat (list (regexp :tag "Highlight regexp")
907                                      (number :tag "Group for entire word" 0)
908                                      (number :tag "Group for displayed part" 0)
909                                      (symbol :tag "Face"
910                                              gnus-emphasis-highlight-words)))))
911   :group 'gnus-summary-visual)
912
913 (defcustom gnus-summary-show-article-charset-alist
914   nil
915   "Alist of number and charset.
916 The article will be shown with the charset corresponding to the
917 numbered argument.
918 For example: ((1 . cn-gb-2312) (2 . big5))."
919   :version "21.1"
920   :type '(repeat (cons (number :tag "Argument" 1)
921                        (symbol :tag "Charset")))
922   :group 'gnus-charset)
923
924 (defcustom gnus-preserve-marks t
925   "Whether marks are preserved when moving, copying and respooling messages."
926   :version "21.1"
927   :type 'boolean
928   :group 'gnus-summary-marks)
929
930 (defcustom gnus-alter-articles-to-read-function nil
931   "Function to be called to alter the list of articles to be selected."
932   :type '(choice (const nil) function)
933   :group 'gnus-summary)
934
935 (defcustom gnus-orphan-score nil
936   "*All orphans get this score added.  Set in the score file."
937   :group 'gnus-score-default
938   :type '(choice (const nil)
939                  integer))
940
941 (defcustom gnus-summary-save-parts-default-mime "image/.*"
942   "*A regexp to match MIME parts when saving multiple parts of a message
943 with gnus-summary-save-parts (X m). This regexp will be used by default
944 when prompting the user for which type of files to save."
945   :group 'gnus-summary
946   :type 'regexp)
947
948
949 (defcustom gnus-summary-save-parts-default-mime "image/.*"
950   "*A regexp to match MIME parts when saving multiple parts of a message
951 with gnus-summary-save-parts (X m). This regexp will be used by default
952 when prompting the user for which type of files to save."
953   :group 'gnus-summary
954   :type 'regexp)
955
956
957 ;;; Internal variables
958
959 (defvar gnus-article-mime-handles nil)
960 (defvar gnus-article-decoded-p nil)
961 (defvar gnus-article-charset nil)
962 (defvar gnus-article-ignored-charsets nil)
963 (defvar gnus-scores-exclude-files nil)
964 (defvar gnus-page-broken nil)
965 (defvar gnus-inhibit-mime-unbuttonizing nil)
966
967 (defvar gnus-original-article nil)
968 (defvar gnus-article-internal-prepare-hook nil)
969 (defvar gnus-newsgroup-process-stack nil)
970
971 (defvar gnus-thread-indent-array nil)
972 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
973 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
974   "Function called to sort the articles within a thread after it has been gathered together.")
975
976 (defvar gnus-summary-save-parts-type-history nil)
977 (defvar gnus-summary-save-parts-last-directory nil)
978
979 (defvar gnus-summary-save-parts-type-history nil)
980 (defvar gnus-summary-save-parts-last-directory nil)
981
982 ;; Avoid highlighting in kill files.
983 (defvar gnus-summary-inhibit-highlight nil)
984 (defvar gnus-newsgroup-selected-overlay nil)
985 (defvar gnus-inhibit-limiting nil)
986 (defvar gnus-newsgroup-adaptive-score-file nil)
987 (defvar gnus-current-score-file nil)
988 (defvar gnus-current-move-group nil)
989 (defvar gnus-current-copy-group nil)
990 (defvar gnus-current-crosspost-group nil)
991
992 (defvar gnus-newsgroup-dependencies nil)
993 (defvar gnus-newsgroup-adaptive nil)
994 (defvar gnus-summary-display-article-function nil)
995 (defvar gnus-summary-highlight-line-function nil
996   "Function called after highlighting a summary line.")
997
998 (defvar gnus-summary-line-format-alist
999   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1000     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1001     (?s gnus-tmp-subject-or-nil ?s)
1002     (?n gnus-tmp-name ?s)
1003     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1004         ?s)
1005     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1006             gnus-tmp-from) ?s)
1007     (?F gnus-tmp-from ?s)
1008     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1009     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1010     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1011     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1012     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1013     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1014     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1015     (?L gnus-tmp-lines ?s)
1016     (?I gnus-tmp-indentation ?s)
1017     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1018     (?R gnus-tmp-replied ?c)
1019     (?\[ gnus-tmp-opening-bracket ?c)
1020     (?\] gnus-tmp-closing-bracket ?c)
1021     (?\> (make-string gnus-tmp-level ? ) ?s)
1022     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1023     (?i gnus-tmp-score ?d)
1024     (?z gnus-tmp-score-char ?c)
1025     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1026     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1027     (?U gnus-tmp-unread ?c)
1028     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1029     (?t (gnus-summary-number-of-articles-in-thread
1030          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1031         ?d)
1032     (?e (gnus-summary-number-of-articles-in-thread
1033          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1034         ?c)
1035     (?u gnus-tmp-user-defined ?s)
1036     (?P (gnus-pick-line-number) ?d))
1037   "An alist of format specifications that can appear in summary lines.
1038 These are paired with what variables they correspond with, along with
1039 the type of the variable (string, integer, character, etc).")
1040
1041 (defvar gnus-summary-dummy-line-format-alist
1042   `((?S gnus-tmp-subject ?s)
1043     (?N gnus-tmp-number ?d)
1044     (?u gnus-tmp-user-defined ?s)))
1045
1046 (defvar gnus-summary-mode-line-format-alist
1047   `((?G gnus-tmp-group-name ?s)
1048     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1049     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1050     (?A gnus-tmp-article-number ?d)
1051     (?Z gnus-tmp-unread-and-unselected ?s)
1052     (?V gnus-version ?s)
1053     (?U gnus-tmp-unread-and-unticked ?d)
1054     (?S gnus-tmp-subject ?s)
1055     (?e gnus-tmp-unselected ?d)
1056     (?u gnus-tmp-user-defined ?s)
1057     (?d (length gnus-newsgroup-dormant) ?d)
1058     (?t (length gnus-newsgroup-marked) ?d)
1059     (?r (length gnus-newsgroup-reads) ?d)
1060     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1061     (?E gnus-newsgroup-expunged-tally ?d)
1062     (?s (gnus-current-score-file-nondirectory) ?s)))
1063
1064 (defvar gnus-last-search-regexp nil
1065   "Default regexp for article search command.")
1066
1067 (defvar gnus-last-shell-command nil
1068   "Default shell command on article.")
1069
1070 (defvar gnus-newsgroup-begin nil)
1071 (defvar gnus-newsgroup-end nil)
1072 (defvar gnus-newsgroup-last-rmail nil)
1073 (defvar gnus-newsgroup-last-mail nil)
1074 (defvar gnus-newsgroup-last-folder nil)
1075 (defvar gnus-newsgroup-last-file nil)
1076 (defvar gnus-newsgroup-auto-expire nil)
1077 (defvar gnus-newsgroup-active nil)
1078
1079 (defvar gnus-newsgroup-data nil)
1080 (defvar gnus-newsgroup-data-reverse nil)
1081 (defvar gnus-newsgroup-limit nil)
1082 (defvar gnus-newsgroup-limits nil)
1083
1084 (defvar gnus-newsgroup-unreads nil
1085   "List of unread articles in the current newsgroup.")
1086
1087 (defvar gnus-newsgroup-unselected nil
1088   "List of unselected unread articles in the current newsgroup.")
1089
1090 (defvar gnus-newsgroup-reads nil
1091   "Alist of read articles and article marks in the current newsgroup.")
1092
1093 (defvar gnus-newsgroup-expunged-tally nil)
1094
1095 (defvar gnus-newsgroup-marked nil
1096   "List of ticked articles in the current newsgroup (a subset of unread art).")
1097
1098 (defvar gnus-newsgroup-killed nil
1099   "List of ranges of articles that have been through the scoring process.")
1100
1101 (defvar gnus-newsgroup-cached nil
1102   "List of articles that come from the article cache.")
1103
1104 (defvar gnus-newsgroup-saved nil
1105   "List of articles that have been saved.")
1106
1107 (defvar gnus-newsgroup-kill-headers nil)
1108
1109 (defvar gnus-newsgroup-replied nil
1110   "List of articles that have been replied to in the current newsgroup.")
1111
1112 (defvar gnus-newsgroup-forwarded nil
1113   "List of articles that have been forwarded in the current newsgroup.")
1114
1115 (defvar gnus-newsgroup-expirable nil
1116   "List of articles in the current newsgroup that can be expired.")
1117
1118 (defvar gnus-newsgroup-processable nil
1119   "List of articles in the current newsgroup that can be processed.")
1120
1121 (defvar gnus-newsgroup-downloadable nil
1122   "List of articles in the current newsgroup that can be processed.")
1123
1124 (defvar gnus-newsgroup-undownloaded nil
1125   "List of articles in the current newsgroup that haven't been downloaded..")
1126
1127 (defvar gnus-newsgroup-unsendable nil
1128   "List of articles in the current newsgroup that won't be sent.")
1129
1130 (defvar gnus-newsgroup-bookmarks nil
1131   "List of articles in the current newsgroup that have bookmarks.")
1132
1133 (defvar gnus-newsgroup-dormant nil
1134   "List of dormant articles in the current newsgroup.")
1135
1136 (defvar gnus-newsgroup-scored nil
1137   "List of scored articles in the current newsgroup.")
1138
1139 (defvar gnus-newsgroup-headers nil
1140   "List of article headers in the current newsgroup.")
1141
1142 (defvar gnus-newsgroup-threads nil)
1143
1144 (defvar gnus-newsgroup-prepared nil
1145   "Whether the current group has been prepared properly.")
1146
1147 (defvar gnus-newsgroup-ancient nil
1148   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1149
1150 (defvar gnus-newsgroup-sparse nil)
1151
1152 (defvar gnus-current-article nil)
1153 (defvar gnus-article-current nil)
1154 (defvar gnus-current-headers nil)
1155 (defvar gnus-have-all-headers nil)
1156 (defvar gnus-last-article nil)
1157 (defvar gnus-newsgroup-history nil)
1158 (defvar gnus-newsgroup-charset nil)
1159 (defvar gnus-newsgroup-ephemeral-charset nil)
1160 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1161
1162 (defvar gnus-article-before-search nil)
1163
1164 (defconst gnus-summary-local-variables
1165   '(gnus-newsgroup-name
1166     gnus-newsgroup-begin gnus-newsgroup-end
1167     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1168     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1169     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1170     gnus-newsgroup-unselected gnus-newsgroup-marked
1171     gnus-newsgroup-reads gnus-newsgroup-saved
1172     gnus-newsgroup-replied gnus-newsgroup-forwarded
1173     gnus-newsgroup-expirable
1174     gnus-newsgroup-processable gnus-newsgroup-killed
1175     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1176     gnus-newsgroup-unsendable
1177     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1178     gnus-newsgroup-headers gnus-newsgroup-threads
1179     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1180     gnus-current-article gnus-current-headers gnus-have-all-headers
1181     gnus-last-article gnus-article-internal-prepare-hook
1182     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1183     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1184     gnus-thread-expunge-below
1185     gnus-score-alist gnus-current-score-file
1186     (gnus-summary-expunge-below . global)
1187     (gnus-summary-mark-below . global)
1188     (gnus-orphan-score . global)
1189     gnus-newsgroup-active gnus-scores-exclude-files
1190     gnus-newsgroup-history gnus-newsgroup-ancient
1191     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1192     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1193     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1194     (gnus-newsgroup-expunged-tally . 0)
1195     gnus-cache-removable-articles gnus-newsgroup-cached
1196     gnus-newsgroup-data gnus-newsgroup-data-reverse
1197     gnus-newsgroup-limit gnus-newsgroup-limits
1198     gnus-newsgroup-charset)
1199   "Variables that are buffer-local to the summary buffers.")
1200
1201 (defvar gnus-newsgroup-variables nil
1202   "Variables that have separate values in the newsgroups.")
1203
1204 ;; Byte-compiler warning.
1205 (eval-when-compile (defvar gnus-article-mode-map))
1206
1207 ;; MIME stuff.
1208
1209 (defvar gnus-decode-encoded-word-methods
1210   '(mail-decode-encoded-word-string)
1211   "List of methods used to decode encoded words.
1212
1213 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1214 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1215 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1216 whose names match REGEXP.
1217
1218 For example:
1219 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1220  mail-decode-encoded-word-string
1221  (\"chinese\" . rfc1843-decode-string))")
1222
1223 (defvar gnus-decode-encoded-word-methods-cache nil)
1224
1225 (defun gnus-multi-decode-encoded-word-string (string)
1226   "Apply the functions from `gnus-encoded-word-methods' that match."
1227   (unless (and gnus-decode-encoded-word-methods-cache
1228                (eq gnus-newsgroup-name
1229                    (car gnus-decode-encoded-word-methods-cache)))
1230     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1231     (mapcar (lambda (x)
1232               (if (symbolp x)
1233                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1234                 (if (and gnus-newsgroup-name
1235                          (string-match (car x) gnus-newsgroup-name))
1236                     (nconc gnus-decode-encoded-word-methods-cache
1237                            (list (cdr x))))))
1238           gnus-decode-encoded-word-methods))
1239   (let ((xlist gnus-decode-encoded-word-methods-cache))
1240     (pop xlist)
1241     (while xlist
1242       (setq string (funcall (pop xlist) string))))
1243   string)
1244
1245 ;; Subject simplification.
1246
1247 (defun gnus-simplify-whitespace (str)
1248   "Remove excessive whitespace from STR."
1249   (let ((mystr str))
1250     ;; Multiple spaces.
1251     (while (string-match "[ \t][ \t]+" mystr)
1252       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1253                           " "
1254                           (substring mystr (match-end 0)))))
1255     ;; Leading spaces.
1256     (when (string-match "^[ \t]+" mystr)
1257       (setq mystr (substring mystr (match-end 0))))
1258     ;; Trailing spaces.
1259     (when (string-match "[ \t]+$" mystr)
1260       (setq mystr (substring mystr 0 (match-beginning 0))))
1261     mystr))
1262
1263 (defsubst gnus-simplify-subject-re (subject)
1264   "Remove \"Re:\" from subject lines."
1265   (if (string-match message-subject-re-regexp subject)
1266       (substring subject (match-end 0))
1267     subject))
1268
1269 (defun gnus-simplify-subject (subject &optional re-only)
1270   "Remove `Re:' and words in parentheses.
1271 If RE-ONLY is non-nil, strip leading `Re:'s only."
1272   (let ((case-fold-search t))           ;Ignore case.
1273     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1274     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1275       (setq subject (substring subject (match-end 0))))
1276     ;; Remove uninteresting prefixes.
1277     (when (and (not re-only)
1278                gnus-simplify-ignored-prefixes
1279                (string-match gnus-simplify-ignored-prefixes subject))
1280       (setq subject (substring subject (match-end 0))))
1281     ;; Remove words in parentheses from end.
1282     (unless re-only
1283       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1284         (setq subject (substring subject 0 (match-beginning 0)))))
1285     ;; Return subject string.
1286     subject))
1287
1288 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1289 ;; all whitespace.
1290 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1291   (goto-char (point-min))
1292   (while (re-search-forward regexp nil t)
1293     (replace-match (or newtext ""))))
1294
1295 (defun gnus-simplify-buffer-fuzzy ()
1296   "Simplify string in the buffer fuzzily.
1297 The string in the accessible portion of the current buffer is simplified.
1298 It is assumed to be a single-line subject.
1299 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1300 matter is removed.  Additional things can be deleted by setting
1301 `gnus-simplify-subject-fuzzy-regexp'."
1302   (let ((case-fold-search t)
1303         (modified-tick))
1304     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1305
1306     (while (not (eq modified-tick (buffer-modified-tick)))
1307       (setq modified-tick (buffer-modified-tick))
1308       (cond
1309        ((listp gnus-simplify-subject-fuzzy-regexp)
1310         (mapcar 'gnus-simplify-buffer-fuzzy-step
1311                 gnus-simplify-subject-fuzzy-regexp))
1312        (gnus-simplify-subject-fuzzy-regexp
1313         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1314       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1315       (gnus-simplify-buffer-fuzzy-step
1316        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1317       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1318
1319     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1320     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1321     (gnus-simplify-buffer-fuzzy-step " $")
1322     (gnus-simplify-buffer-fuzzy-step "^ +")))
1323
1324 (defun gnus-simplify-subject-fuzzy (subject)
1325   "Simplify a subject string fuzzily.
1326 See `gnus-simplify-buffer-fuzzy' for details."
1327   (save-excursion
1328     (gnus-set-work-buffer)
1329     (let ((case-fold-search t))
1330       ;; Remove uninteresting prefixes.
1331       (when (and gnus-simplify-ignored-prefixes
1332                  (string-match gnus-simplify-ignored-prefixes subject))
1333         (setq subject (substring subject (match-end 0))))
1334       (insert subject)
1335       (inline (gnus-simplify-buffer-fuzzy))
1336       (buffer-string))))
1337
1338 (defsubst gnus-simplify-subject-fully (subject)
1339   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1340   (cond
1341    (gnus-simplify-subject-functions
1342     (gnus-map-function gnus-simplify-subject-functions subject))
1343    ((null gnus-summary-gather-subject-limit)
1344     (gnus-simplify-subject-re subject))
1345    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1346     (gnus-simplify-subject-fuzzy subject))
1347    ((numberp gnus-summary-gather-subject-limit)
1348     (gnus-limit-string (gnus-simplify-subject-re subject)
1349                        gnus-summary-gather-subject-limit))
1350    (t
1351     subject)))
1352
1353 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1354   "Check whether two subjects are equal.
1355 If optional argument simple-first is t, first argument is already
1356 simplified."
1357   (cond
1358    ((null simple-first)
1359     (equal (gnus-simplify-subject-fully s1)
1360            (gnus-simplify-subject-fully s2)))
1361    (t
1362     (equal s1
1363            (gnus-simplify-subject-fully s2)))))
1364
1365 (defun gnus-summary-bubble-group ()
1366   "Increase the score of the current group.
1367 This is a handy function to add to `gnus-summary-exit-hook' to
1368 increase the score of each group you read."
1369   (gnus-group-add-score gnus-newsgroup-name))
1370
1371 \f
1372 ;;;
1373 ;;; Gnus summary mode
1374 ;;;
1375
1376 (put 'gnus-summary-mode 'mode-class 'special)
1377
1378 (defvar gnus-article-commands-menu)
1379
1380 (when t
1381   ;; Non-orthogonal keys
1382
1383   (gnus-define-keys gnus-summary-mode-map
1384     " " gnus-summary-next-page
1385     "\177" gnus-summary-prev-page
1386     [delete] gnus-summary-prev-page
1387     [backspace] gnus-summary-prev-page
1388     "\r" gnus-summary-scroll-up
1389     "\M-\r" gnus-summary-scroll-down
1390     "n" gnus-summary-next-unread-article
1391     "p" gnus-summary-prev-unread-article
1392     "N" gnus-summary-next-article
1393     "P" gnus-summary-prev-article
1394     "\M-\C-n" gnus-summary-next-same-subject
1395     "\M-\C-p" gnus-summary-prev-same-subject
1396     "\M-n" gnus-summary-next-unread-subject
1397     "\M-p" gnus-summary-prev-unread-subject
1398     "." gnus-summary-first-unread-article
1399     "," gnus-summary-best-unread-article
1400     "\M-s" gnus-summary-search-article-forward
1401     "\M-r" gnus-summary-search-article-backward
1402     "<" gnus-summary-beginning-of-article
1403     ">" gnus-summary-end-of-article
1404     "j" gnus-summary-goto-article
1405     "^" gnus-summary-refer-parent-article
1406     "\M-^" gnus-summary-refer-article
1407     "u" gnus-summary-tick-article-forward
1408     "!" gnus-summary-tick-article-forward
1409     "U" gnus-summary-tick-article-backward
1410     "d" gnus-summary-mark-as-read-forward
1411     "D" gnus-summary-mark-as-read-backward
1412     "E" gnus-summary-mark-as-expirable
1413     "\M-u" gnus-summary-clear-mark-forward
1414     "\M-U" gnus-summary-clear-mark-backward
1415     "k" gnus-summary-kill-same-subject-and-select
1416     "\C-k" gnus-summary-kill-same-subject
1417     "\M-\C-k" gnus-summary-kill-thread
1418     "\M-\C-l" gnus-summary-lower-thread
1419     "e" gnus-summary-edit-article
1420     "#" gnus-summary-mark-as-processable
1421     "\M-#" gnus-summary-unmark-as-processable
1422     "\M-\C-t" gnus-summary-toggle-threads
1423     "\M-\C-s" gnus-summary-show-thread
1424     "\M-\C-h" gnus-summary-hide-thread
1425     "\M-\C-f" gnus-summary-next-thread
1426     "\M-\C-b" gnus-summary-prev-thread
1427     [(meta down)] gnus-summary-next-thread
1428     [(meta up)] gnus-summary-prev-thread
1429     "\M-\C-u" gnus-summary-up-thread
1430     "\M-\C-d" gnus-summary-down-thread
1431     "&" gnus-summary-execute-command
1432     "c" gnus-summary-catchup-and-exit
1433     "\C-w" gnus-summary-mark-region-as-read
1434     "\C-t" gnus-summary-toggle-truncation
1435     "?" gnus-summary-mark-as-dormant
1436     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1437     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1438     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1439     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1440     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1441     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1442     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1443     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1444     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1445     "=" gnus-summary-expand-window
1446     "\C-x\C-s" gnus-summary-reselect-current-group
1447     "\M-g" gnus-summary-rescan-group
1448     "w" gnus-summary-stop-page-breaking
1449     "\C-c\C-r" gnus-summary-caesar-message
1450     "f" gnus-summary-followup
1451     "F" gnus-summary-followup-with-original
1452     "C" gnus-summary-cancel-article
1453     "r" gnus-summary-reply
1454     "R" gnus-summary-reply-with-original
1455     "\C-c\C-f" gnus-summary-mail-forward
1456     "o" gnus-summary-save-article
1457     "\C-o" gnus-summary-save-article-mail
1458     "|" gnus-summary-pipe-output
1459     "\M-k" gnus-summary-edit-local-kill
1460     "\M-K" gnus-summary-edit-global-kill
1461     ;; "V" gnus-version
1462     "\C-c\C-d" gnus-summary-describe-group
1463     "q" gnus-summary-exit
1464     "Q" gnus-summary-exit-no-update
1465     "\C-c\C-i" gnus-info-find-node
1466     gnus-mouse-2 gnus-mouse-pick-article
1467     "m" gnus-summary-mail-other-window
1468     "a" gnus-summary-post-news
1469     "x" gnus-summary-limit-to-unread
1470     "s" gnus-summary-isearch-article
1471     "t" gnus-summary-toggle-header
1472     "g" gnus-summary-show-article
1473     "l" gnus-summary-goto-last-article
1474     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1475     "\C-d" gnus-summary-enter-digest-group
1476     "\M-\C-d" gnus-summary-read-document
1477     "\M-\C-e" gnus-summary-edit-parameters
1478     "\M-\C-a" gnus-summary-customize-parameters
1479     "\C-c\C-b" gnus-bug
1480     "*" gnus-cache-enter-article
1481     "\M-*" gnus-cache-remove-article
1482     "\M-&" gnus-summary-universal-argument
1483     "\C-l" gnus-recenter
1484     "I" gnus-summary-increase-score
1485     "L" gnus-summary-lower-score
1486     "\M-i" gnus-symbolic-argument
1487     "h" gnus-summary-select-article-buffer
1488
1489     "b" gnus-article-view-part
1490     "\M-t" gnus-summary-toggle-display-buttonized
1491
1492     "V" gnus-summary-score-map
1493     "X" gnus-uu-extract-map
1494     "S" gnus-summary-send-map)
1495
1496   ;; Sort of orthogonal keymap
1497   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1498     "t" gnus-summary-tick-article-forward
1499     "!" gnus-summary-tick-article-forward
1500     "d" gnus-summary-mark-as-read-forward
1501     "r" gnus-summary-mark-as-read-forward
1502     "c" gnus-summary-clear-mark-forward
1503     " " gnus-summary-clear-mark-forward
1504     "e" gnus-summary-mark-as-expirable
1505     "x" gnus-summary-mark-as-expirable
1506     "?" gnus-summary-mark-as-dormant
1507     "b" gnus-summary-set-bookmark
1508     "B" gnus-summary-remove-bookmark
1509     "#" gnus-summary-mark-as-processable
1510     "\M-#" gnus-summary-unmark-as-processable
1511     "S" gnus-summary-limit-include-expunged
1512     "C" gnus-summary-catchup
1513     "H" gnus-summary-catchup-to-here
1514     "\C-c" gnus-summary-catchup-all
1515     "k" gnus-summary-kill-same-subject-and-select
1516     "K" gnus-summary-kill-same-subject
1517     "P" gnus-uu-mark-map)
1518
1519   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1520     "c" gnus-summary-clear-above
1521     "u" gnus-summary-tick-above
1522     "m" gnus-summary-mark-above
1523     "k" gnus-summary-kill-below)
1524
1525   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1526     "/" gnus-summary-limit-to-subject
1527     "n" gnus-summary-limit-to-articles
1528     "w" gnus-summary-pop-limit
1529     "s" gnus-summary-limit-to-subject
1530     "a" gnus-summary-limit-to-author
1531     "u" gnus-summary-limit-to-unread
1532     "m" gnus-summary-limit-to-marks
1533     "M" gnus-summary-limit-exclude-marks
1534     "v" gnus-summary-limit-to-score
1535     "*" gnus-summary-limit-include-cached
1536     "D" gnus-summary-limit-include-dormant
1537     "T" gnus-summary-limit-include-thread
1538     "d" gnus-summary-limit-exclude-dormant
1539     "t" gnus-summary-limit-to-age
1540     "x" gnus-summary-limit-to-extra
1541     "E" gnus-summary-limit-include-expunged
1542     "c" gnus-summary-limit-exclude-childless-dormant
1543     "C" gnus-summary-limit-mark-excluded-as-read
1544     "o" gnus-summary-insert-old-articles
1545     "N" gnus-summary-insert-new-articles)
1546
1547   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1548     "n" gnus-summary-next-unread-article
1549     "p" gnus-summary-prev-unread-article
1550     "N" gnus-summary-next-article
1551     "P" gnus-summary-prev-article
1552     "\C-n" gnus-summary-next-same-subject
1553     "\C-p" gnus-summary-prev-same-subject
1554     "\M-n" gnus-summary-next-unread-subject
1555     "\M-p" gnus-summary-prev-unread-subject
1556     "f" gnus-summary-first-unread-article
1557     "b" gnus-summary-best-unread-article
1558     "j" gnus-summary-goto-article
1559     "g" gnus-summary-goto-subject
1560     "l" gnus-summary-goto-last-article
1561     "o" gnus-summary-pop-article)
1562
1563   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1564     "k" gnus-summary-kill-thread
1565     "l" gnus-summary-lower-thread
1566     "i" gnus-summary-raise-thread
1567     "T" gnus-summary-toggle-threads
1568     "t" gnus-summary-rethread-current
1569     "^" gnus-summary-reparent-thread
1570     "s" gnus-summary-show-thread
1571     "S" gnus-summary-show-all-threads
1572     "h" gnus-summary-hide-thread
1573     "H" gnus-summary-hide-all-threads
1574     "n" gnus-summary-next-thread
1575     "p" gnus-summary-prev-thread
1576     "u" gnus-summary-up-thread
1577     "o" gnus-summary-top-thread
1578     "d" gnus-summary-down-thread
1579     "#" gnus-uu-mark-thread
1580     "\M-#" gnus-uu-unmark-thread)
1581
1582   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1583     "g" gnus-summary-prepare
1584     "c" gnus-summary-insert-cached-articles)
1585
1586   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1587     "c" gnus-summary-catchup-and-exit
1588     "C" gnus-summary-catchup-all-and-exit
1589     "E" gnus-summary-exit-no-update
1590     "Q" gnus-summary-exit
1591     "Z" gnus-summary-exit
1592     "n" gnus-summary-catchup-and-goto-next-group
1593     "R" gnus-summary-reselect-current-group
1594     "G" gnus-summary-rescan-group
1595     "N" gnus-summary-next-group
1596     "s" gnus-summary-save-newsrc
1597     "P" gnus-summary-prev-group)
1598
1599   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1600     " " gnus-summary-next-page
1601     "n" gnus-summary-next-page
1602     "\177" gnus-summary-prev-page
1603     [delete] gnus-summary-prev-page
1604     "p" gnus-summary-prev-page
1605     "\r" gnus-summary-scroll-up
1606     "\M-\r" gnus-summary-scroll-down
1607     "<" gnus-summary-beginning-of-article
1608     ">" gnus-summary-end-of-article
1609     "b" gnus-summary-beginning-of-article
1610     "e" gnus-summary-end-of-article
1611     "^" gnus-summary-refer-parent-article
1612     "r" gnus-summary-refer-parent-article
1613     "D" gnus-summary-enter-digest-group
1614     "R" gnus-summary-refer-references
1615     "T" gnus-summary-refer-thread
1616     "g" gnus-summary-show-article
1617     "s" gnus-summary-isearch-article
1618     "P" gnus-summary-print-article
1619     "M" gnus-mailing-list-insinuate
1620     "t" gnus-article-babel)
1621
1622   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1623     "b" gnus-article-add-buttons
1624     "B" gnus-article-add-buttons-to-head
1625     "o" gnus-article-treat-overstrike
1626     "e" gnus-article-emphasize
1627     "w" gnus-article-fill-cited-article
1628     "Q" gnus-article-fill-long-lines
1629     "C" gnus-article-capitalize-sentences
1630     "c" gnus-article-remove-cr
1631     "q" gnus-article-de-quoted-unreadable
1632     "6" gnus-article-de-base64-unreadable
1633     "Z" gnus-article-decode-HZ
1634     "h" gnus-article-wash-html
1635     "s" gnus-summary-force-verify-and-decrypt
1636     "f" gnus-article-display-x-face
1637     "l" gnus-summary-stop-page-breaking
1638     "r" gnus-summary-caesar-message
1639     "t" gnus-summary-toggle-header
1640     "v" gnus-summary-verbose-headers
1641     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1642     "p" gnus-article-verify-x-pgp-sig
1643     "d" gnus-article-treat-dumbquotes)
1644
1645   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1646     "a" gnus-article-hide
1647     "h" gnus-article-hide-headers
1648     "b" gnus-article-hide-boring-headers
1649     "s" gnus-article-hide-signature
1650     "c" gnus-article-hide-citation
1651     "C" gnus-article-hide-citation-in-followups
1652     "l" gnus-article-hide-list-identifiers
1653     "p" gnus-article-hide-pgp
1654     "B" gnus-article-strip-banner
1655     "P" gnus-article-hide-pem
1656     "\C-c" gnus-article-hide-citation-maybe)
1657
1658   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1659     "a" gnus-article-highlight
1660     "h" gnus-article-highlight-headers
1661     "c" gnus-article-highlight-citation
1662     "s" gnus-article-highlight-signature)
1663
1664   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1665     "w" gnus-article-decode-mime-words
1666     "c" gnus-article-decode-charset
1667     "v" gnus-mime-view-all-parts
1668     "b" gnus-article-view-part)
1669
1670   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1671     "z" gnus-article-date-ut
1672     "u" gnus-article-date-ut
1673     "l" gnus-article-date-local
1674     "p" gnus-article-date-english
1675     "e" gnus-article-date-lapsed
1676     "o" gnus-article-date-original
1677     "i" gnus-article-date-iso8601
1678     "s" gnus-article-date-user)
1679
1680   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1681     "t" gnus-article-remove-trailing-blank-lines
1682     "l" gnus-article-strip-leading-blank-lines
1683     "m" gnus-article-strip-multiple-blank-lines
1684     "a" gnus-article-strip-blank-lines
1685     "A" gnus-article-strip-all-blank-lines
1686     "s" gnus-article-strip-leading-space
1687     "e" gnus-article-strip-trailing-space
1688     "w" gnus-article-remove-leading-whitespace)
1689
1690   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1691     "v" gnus-version
1692     "f" gnus-summary-fetch-faq
1693     "d" gnus-summary-describe-group
1694     "h" gnus-summary-describe-briefly
1695     "i" gnus-info-find-node)
1696
1697   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1698     "e" gnus-summary-expire-articles
1699     "\M-\C-e" gnus-summary-expire-articles-now
1700     "\177" gnus-summary-delete-article
1701     [delete] gnus-summary-delete-article
1702     [backspace] gnus-summary-delete-article
1703     "m" gnus-summary-move-article
1704     "r" gnus-summary-respool-article
1705     "w" gnus-summary-edit-article
1706     "c" gnus-summary-copy-article
1707     "B" gnus-summary-crosspost-article
1708     "q" gnus-summary-respool-query
1709     "t" gnus-summary-respool-trace
1710     "i" gnus-summary-import-article
1711     "I" gnus-summary-create-article
1712     "p" gnus-summary-article-posted-p)
1713
1714   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1715     "o" gnus-summary-save-article
1716     "m" gnus-summary-save-article-mail
1717     "F" gnus-summary-write-article-file
1718     "r" gnus-summary-save-article-rmail
1719     "f" gnus-summary-save-article-file
1720     "b" gnus-summary-save-article-body-file
1721     "h" gnus-summary-save-article-folder
1722     "v" gnus-summary-save-article-vm
1723     "p" gnus-summary-pipe-output
1724     "s" gnus-soup-add-article)
1725
1726   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1727     "b" gnus-summary-display-buttonized
1728     "m" gnus-summary-repair-multipart
1729     "v" gnus-article-view-part
1730     "o" gnus-article-save-part
1731     "c" gnus-article-copy-part
1732     "C" gnus-article-view-part-as-charset
1733     "e" gnus-article-externalize-part
1734     "E" gnus-article-encrypt-body
1735     "i" gnus-article-inline-part
1736     "|" gnus-article-pipe-part))
1737
1738 (defvar gnus-article-post-menu nil)
1739
1740 (defun gnus-summary-make-menu-bar ()
1741   (gnus-turn-off-edit-menu 'summary)
1742
1743   (unless (boundp 'gnus-summary-misc-menu)
1744
1745     (easy-menu-define
1746      gnus-summary-kill-menu gnus-summary-mode-map ""
1747      (cons
1748       "Score"
1749       (nconc
1750        (list
1751         ["Customize" gnus-score-customize t])
1752        (gnus-make-score-map 'increase)
1753        (gnus-make-score-map 'lower)
1754        '(("Mark"
1755           ["Kill below" gnus-summary-kill-below t]
1756           ["Mark above" gnus-summary-mark-above t]
1757           ["Tick above" gnus-summary-tick-above t]
1758           ["Clear above" gnus-summary-clear-above t])
1759          ["Current score" gnus-summary-current-score t]
1760          ["Set score" gnus-summary-set-score t]
1761          ["Switch current score file..." gnus-score-change-score-file t]
1762          ["Set mark below..." gnus-score-set-mark-below t]
1763          ["Set expunge below..." gnus-score-set-expunge-below t]
1764          ["Edit current score file" gnus-score-edit-current-scores t]
1765          ["Edit score file" gnus-score-edit-file t]
1766          ["Trace score" gnus-score-find-trace t]
1767          ["Find words" gnus-score-find-favourite-words t]
1768          ["Rescore buffer" gnus-summary-rescore t]
1769          ["Increase score..." gnus-summary-increase-score t]
1770          ["Lower score..." gnus-summary-lower-score t]))))
1771
1772     ;; Define both the Article menu in the summary buffer and the equivalent
1773     ;; Commands menu in the article buffer here for consistency.
1774     (let ((innards
1775            `(("Hide"
1776               ["All" gnus-article-hide t]
1777               ["Headers" gnus-article-hide-headers t]
1778               ["Signature" gnus-article-hide-signature t]
1779               ["Citation" gnus-article-hide-citation t]
1780               ["List identifiers" gnus-article-hide-list-identifiers t]
1781               ["PGP" gnus-article-hide-pgp t]
1782               ["Banner" gnus-article-strip-banner t]
1783               ["Boring headers" gnus-article-hide-boring-headers t])
1784              ("Highlight"
1785               ["All" gnus-article-highlight t]
1786               ["Headers" gnus-article-highlight-headers t]
1787               ["Signature" gnus-article-highlight-signature t]
1788               ["Citation" gnus-article-highlight-citation t])
1789              ("MIME"
1790               ["Words" gnus-article-decode-mime-words t]
1791               ["Charset" gnus-article-decode-charset t]
1792               ["QP" gnus-article-de-quoted-unreadable t]
1793               ["Base64" gnus-article-de-base64-unreadable t]
1794               ["View all" gnus-mime-view-all-parts t]
1795               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1796               ["Encrypt body" gnus-article-encrypt-body t])
1797              ("Date"
1798               ["Local" gnus-article-date-local t]
1799               ["ISO8601" gnus-article-date-iso8601 t]
1800               ["UT" gnus-article-date-ut t]
1801               ["Original" gnus-article-date-original t]
1802               ["Lapsed" gnus-article-date-lapsed t]
1803               ["User-defined" gnus-article-date-user t])
1804              ("Washing"
1805               ("Remove Blanks"
1806                ["Leading" gnus-article-strip-leading-blank-lines t]
1807                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1808                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1809                ["All of the above" gnus-article-strip-blank-lines t]
1810                ["All" gnus-article-strip-all-blank-lines t]
1811                ["Leading space" gnus-article-strip-leading-space t]
1812                ["Trailing space" gnus-article-strip-trailing-space t]
1813                ["Leading space in headers" 
1814                 gnus-article-remove-leading-whitespace t])
1815               ["Overstrike" gnus-article-treat-overstrike t]
1816               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1817               ["Emphasis" gnus-article-emphasize t]
1818               ["Word wrap" gnus-article-fill-cited-article t]
1819               ["Fill long lines" gnus-article-fill-long-lines t]
1820               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1821               ["CR" gnus-article-remove-cr t]
1822               ["Show X-Face" gnus-article-display-x-face t]
1823               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1824               ["Base64" gnus-article-de-base64-unreadable t]
1825               ["Rot 13" gnus-summary-caesar-message
1826                ,@(if (featurep 'xemacs) '(t)
1827                    '(:help "\"Caesar rotate\" article by 13"))]
1828               ["Unix pipe" gnus-summary-pipe-message t]
1829               ["Add buttons" gnus-article-add-buttons t]
1830               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1831               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1832               ["Verbose header" gnus-summary-verbose-headers t]
1833               ["Toggle header" gnus-summary-toggle-header t]
1834               ["Html" gnus-article-wash-html t]
1835               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1836               ["HZ" gnus-article-decode-HZ t])
1837              ("Output"
1838               ["Save in default format" gnus-summary-save-article
1839                ,@(if (featurep 'xemacs) '(t)
1840                    '(:help "Save article using default method"))]
1841               ["Save in file" gnus-summary-save-article-file
1842                ,@(if (featurep 'xemacs) '(t)
1843                    '(:help "Save article in file"))]
1844               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1845               ["Save in MH folder" gnus-summary-save-article-folder t]
1846               ["Save in VM folder" gnus-summary-save-article-vm t]
1847               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1848               ["Save body in file" gnus-summary-save-article-body-file t]
1849               ["Pipe through a filter" gnus-summary-pipe-output t]
1850               ["Add to SOUP packet" gnus-soup-add-article t]
1851               ["Print" gnus-summary-print-article t])
1852              ("Backend"
1853               ["Respool article..." gnus-summary-respool-article t]
1854               ["Move article..." gnus-summary-move-article
1855                (gnus-check-backend-function
1856                 'request-move-article gnus-newsgroup-name)]
1857               ["Copy article..." gnus-summary-copy-article t]
1858               ["Crosspost article..." gnus-summary-crosspost-article
1859                (gnus-check-backend-function
1860                 'request-replace-article gnus-newsgroup-name)]
1861               ["Import file..." gnus-summary-import-article t]
1862               ["Create article..." gnus-summary-create-article t]
1863               ["Check if posted" gnus-summary-article-posted-p t]
1864               ["Edit article" gnus-summary-edit-article
1865                (not (gnus-group-read-only-p))]
1866               ["Delete article" gnus-summary-delete-article
1867                (gnus-check-backend-function
1868                 'request-expire-articles gnus-newsgroup-name)]
1869               ["Query respool" gnus-summary-respool-query t]
1870               ["Trace respool" gnus-summary-respool-trace t]
1871               ["Delete expirable articles" gnus-summary-expire-articles-now
1872                (gnus-check-backend-function
1873                 'request-expire-articles gnus-newsgroup-name)])
1874              ("Extract"
1875               ["Uudecode" gnus-uu-decode-uu
1876                ,@(if (featurep 'xemacs) '(t)
1877                    '(:help "Decode uuencoded article(s)"))]
1878               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1879               ["Unshar" gnus-uu-decode-unshar t]
1880               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1881               ["Save" gnus-uu-decode-save t]
1882               ["Binhex" gnus-uu-decode-binhex t]
1883               ["Postscript" gnus-uu-decode-postscript t])
1884              ("Cache"
1885               ["Enter article" gnus-cache-enter-article t]
1886               ["Remove article" gnus-cache-remove-article t])
1887              ["Translate" gnus-article-babel t]
1888              ["Select article buffer" gnus-summary-select-article-buffer t]
1889              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1890              ["Isearch article..." gnus-summary-isearch-article t]
1891              ["Beginning of the article" gnus-summary-beginning-of-article t]
1892              ["End of the article" gnus-summary-end-of-article t]
1893              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1894              ["Fetch referenced articles" gnus-summary-refer-references t]
1895              ["Fetch current thread" gnus-summary-refer-thread t]
1896              ["Fetch article with id..." gnus-summary-refer-article t]
1897              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1898              ["Redisplay" gnus-summary-show-article t])))
1899       (easy-menu-define
1900        gnus-summary-article-menu gnus-summary-mode-map ""
1901        (cons "Article" innards))
1902
1903       (if (not (keymapp gnus-summary-article-menu))
1904           (easy-menu-define
1905             gnus-article-commands-menu gnus-article-mode-map ""
1906             (cons "Commands" innards))
1907         ;; in Emacs, don't share menu.
1908         (setq gnus-article-commands-menu 
1909               (copy-keymap gnus-summary-article-menu))
1910         (define-key gnus-article-mode-map [menu-bar commands]
1911           (cons "Commands" gnus-article-commands-menu))))
1912
1913     (easy-menu-define
1914      gnus-summary-thread-menu gnus-summary-mode-map ""
1915      '("Threads"
1916        ["Toggle threading" gnus-summary-toggle-threads t]
1917        ["Hide threads" gnus-summary-hide-all-threads t]
1918        ["Show threads" gnus-summary-show-all-threads t]
1919        ["Hide thread" gnus-summary-hide-thread t]
1920        ["Show thread" gnus-summary-show-thread t]
1921        ["Go to next thread" gnus-summary-next-thread t]
1922        ["Go to previous thread" gnus-summary-prev-thread t]
1923        ["Go down thread" gnus-summary-down-thread t]
1924        ["Go up thread" gnus-summary-up-thread t]
1925        ["Top of thread" gnus-summary-top-thread t]
1926        ["Mark thread as read" gnus-summary-kill-thread t]
1927        ["Lower thread score" gnus-summary-lower-thread t]
1928        ["Raise thread score" gnus-summary-raise-thread t]
1929        ["Rethread current" gnus-summary-rethread-current t]))
1930
1931     (easy-menu-define
1932      gnus-summary-post-menu gnus-summary-mode-map ""
1933      `("Post"
1934        ["Post an article" gnus-summary-post-news
1935         ,@(if (featurep 'xemacs) '(t)
1936             '(:help "Post an article"))]
1937        ["Followup" gnus-summary-followup
1938         ,@(if (featurep 'xemacs) '(t)
1939             '(:help "Post followup to this article"))]
1940        ["Followup and yank" gnus-summary-followup-with-original
1941         ,@(if (featurep 'xemacs) '(t)
1942             '(:help "Post followup to this article, quoting its contents"))]
1943        ["Supersede article" gnus-summary-supersede-article t]
1944        ["Cancel article" gnus-summary-cancel-article
1945         ,@(if (featurep 'xemacs) '(t)
1946             '(:help "Cancel an article you posted"))]
1947        ["Reply" gnus-summary-reply t]
1948        ["Reply and yank" gnus-summary-reply-with-original t]
1949        ["Wide reply" gnus-summary-wide-reply t]
1950        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1951         ,@(if (featurep 'xemacs) '(t)
1952             '(:help "Mail a reply, quoting this article"))]
1953        ["Mail forward" gnus-summary-mail-forward t]
1954        ["Post forward" gnus-summary-post-forward t]
1955        ["Digest and mail" gnus-uu-digest-mail-forward t]
1956        ["Digest and post" gnus-uu-digest-post-forward t]
1957        ["Resend message" gnus-summary-resend-message t]
1958        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1959        ["Send a mail" gnus-summary-mail-other-window t]
1960        ["Uuencode and post" gnus-uu-post-news
1961         ,@(if (featurep 'xemacs) '(t)
1962             '(:help "Post a uuencoded article"))]
1963        ["Followup via news" gnus-summary-followup-to-mail t]
1964        ["Followup via news and yank"
1965         gnus-summary-followup-to-mail-with-original t]
1966        ;;("Draft"
1967        ;;["Send" gnus-summary-send-draft t]
1968        ;;["Send bounced" gnus-resend-bounced-mail t])
1969        ))
1970
1971     (cond 
1972      ((not (keymapp gnus-summary-post-menu))
1973       (setq gnus-article-post-menu gnus-summary-post-menu))
1974      ((not gnus-article-post-menu)
1975       ;; Don't share post menu.
1976       (setq gnus-article-post-menu
1977             (copy-keymap gnus-summary-post-menu))))
1978     (define-key gnus-article-mode-map [menu-bar post]
1979       (cons "Post" gnus-article-post-menu))
1980
1981     (easy-menu-define
1982      gnus-summary-misc-menu gnus-summary-mode-map ""
1983      `("Misc"
1984        ("Mark Read"
1985         ["Mark as read" gnus-summary-mark-as-read-forward t]
1986         ["Mark same subject and select"
1987          gnus-summary-kill-same-subject-and-select t]
1988         ["Mark same subject" gnus-summary-kill-same-subject t]
1989         ["Catchup" gnus-summary-catchup
1990          ,@(if (featurep 'xemacs) '(t)
1991              '(:help "Mark unread articles in this group as read"))]
1992         ["Catchup all" gnus-summary-catchup-all t]
1993         ["Catchup to here" gnus-summary-catchup-to-here t]
1994         ["Catchup region" gnus-summary-mark-region-as-read t]
1995         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1996        ("Mark Various"
1997         ["Tick" gnus-summary-tick-article-forward t]
1998         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1999         ["Remove marks" gnus-summary-clear-mark-forward t]
2000         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2001         ["Set bookmark" gnus-summary-set-bookmark t]
2002         ["Remove bookmark" gnus-summary-remove-bookmark t])
2003        ("Mark Limit"
2004         ["Marks..." gnus-summary-limit-to-marks t]
2005         ["Subject..." gnus-summary-limit-to-subject t]
2006         ["Author..." gnus-summary-limit-to-author t]
2007         ["Age..." gnus-summary-limit-to-age t]
2008         ["Extra..." gnus-summary-limit-to-extra t]
2009         ["Score" gnus-summary-limit-to-score t]
2010         ["Unread" gnus-summary-limit-to-unread t]
2011         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2012         ["Articles" gnus-summary-limit-to-articles t]
2013         ["Pop limit" gnus-summary-pop-limit t]
2014         ["Show dormant" gnus-summary-limit-include-dormant t]
2015         ["Hide childless dormant"
2016          gnus-summary-limit-exclude-childless-dormant t]
2017         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2018         ["Hide marked" gnus-summary-limit-exclude-marks t]
2019         ["Show expunged" gnus-summary-limit-include-expunged t])
2020        ("Process Mark"
2021         ["Set mark" gnus-summary-mark-as-processable t]
2022         ["Remove mark" gnus-summary-unmark-as-processable t]
2023         ["Remove all marks" gnus-summary-unmark-all-processable t]
2024         ["Mark above" gnus-uu-mark-over t]
2025         ["Mark series" gnus-uu-mark-series t]
2026         ["Mark region" gnus-uu-mark-region t]
2027         ["Unmark region" gnus-uu-unmark-region t]
2028         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2029         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2030         ["Mark all" gnus-uu-mark-all t]
2031         ["Mark buffer" gnus-uu-mark-buffer t]
2032         ["Mark sparse" gnus-uu-mark-sparse t]
2033         ["Mark thread" gnus-uu-mark-thread t]
2034         ["Unmark thread" gnus-uu-unmark-thread t]
2035         ("Process Mark Sets"
2036          ["Kill" gnus-summary-kill-process-mark t]
2037          ["Yank" gnus-summary-yank-process-mark
2038           gnus-newsgroup-process-stack]
2039          ["Save" gnus-summary-save-process-mark t]))
2040        ("Scroll article"
2041         ["Page forward" gnus-summary-next-page
2042          ,@(if (featurep 'xemacs) '(t)
2043              '(:help "Show next page of article"))]
2044         ["Page backward" gnus-summary-prev-page
2045          ,@(if (featurep 'xemacs) '(t)
2046              '(:help "Show previous page of article"))]
2047         ["Line forward" gnus-summary-scroll-up t])
2048        ("Move"
2049         ["Next unread article" gnus-summary-next-unread-article t]
2050         ["Previous unread article" gnus-summary-prev-unread-article t]
2051         ["Next article" gnus-summary-next-article t]
2052         ["Previous article" gnus-summary-prev-article t]
2053         ["Next unread subject" gnus-summary-next-unread-subject t]
2054         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2055         ["Next article same subject" gnus-summary-next-same-subject t]
2056         ["Previous article same subject" gnus-summary-prev-same-subject t]
2057         ["First unread article" gnus-summary-first-unread-article t]
2058         ["Best unread article" gnus-summary-best-unread-article t]
2059         ["Go to subject number..." gnus-summary-goto-subject t]
2060         ["Go to article number..." gnus-summary-goto-article t]
2061         ["Go to the last article" gnus-summary-goto-last-article t]
2062         ["Pop article off history" gnus-summary-pop-article t])
2063        ("Sort"
2064         ["Sort by number" gnus-summary-sort-by-number t]
2065         ["Sort by author" gnus-summary-sort-by-author t]
2066         ["Sort by subject" gnus-summary-sort-by-subject t]
2067         ["Sort by date" gnus-summary-sort-by-date t]
2068         ["Sort by score" gnus-summary-sort-by-score t]
2069         ["Sort by lines" gnus-summary-sort-by-lines t]
2070         ["Sort by characters" gnus-summary-sort-by-chars t]
2071         ["Original sort" gnus-summary-sort-by-original t])
2072        ("Help"
2073         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2074         ["Describe group" gnus-summary-describe-group t]
2075         ["Read manual" gnus-info-find-node t])
2076        ("Modes"
2077         ["Pick and read" gnus-pick-mode t]
2078         ["Binary" gnus-binary-mode t])
2079        ("Regeneration"
2080         ["Regenerate" gnus-summary-prepare t]
2081         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2082         ["Toggle threading" gnus-summary-toggle-threads t])
2083        ["See old articles" gnus-summary-insert-old-articles t]
2084        ["See new articles" gnus-summary-insert-new-articles t]
2085        ["Filter articles..." gnus-summary-execute-command t]
2086        ["Run command on subjects..." gnus-summary-universal-argument t]
2087        ["Search articles forward..." gnus-summary-search-article-forward t]
2088        ["Search articles backward..." gnus-summary-search-article-backward t]
2089        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2090        ["Expand window" gnus-summary-expand-window t]
2091        ["Expire expirable articles" gnus-summary-expire-articles
2092         (gnus-check-backend-function
2093          'request-expire-articles gnus-newsgroup-name)]
2094        ["Edit local kill file" gnus-summary-edit-local-kill t]
2095        ["Edit main kill file" gnus-summary-edit-global-kill t]
2096        ["Edit group parameters" gnus-summary-edit-parameters t]
2097        ["Customize group parameters" gnus-summary-customize-parameters t]
2098        ["Send a bug report" gnus-bug t]
2099        ("Exit"
2100         ["Catchup and exit" gnus-summary-catchup-and-exit
2101          ,@(if (featurep 'xemacs) '(t)
2102              '(:help "Mark unread articles in this group as read, then exit"))]
2103         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2104         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2105         ["Exit group" gnus-summary-exit
2106          ,@(if (featurep 'xemacs) '(t)
2107              '(:help "Exit current group, return to group selection mode"))]
2108         ["Exit group without updating" gnus-summary-exit-no-update t]
2109         ["Exit and goto next group" gnus-summary-next-group t]
2110         ["Exit and goto prev group" gnus-summary-prev-group t]
2111         ["Reselect group" gnus-summary-reselect-current-group t]
2112         ["Rescan group" gnus-summary-rescan-group t]
2113         ["Update dribble" gnus-summary-save-newsrc t])))
2114
2115     (gnus-run-hooks 'gnus-summary-menu-hook)))
2116
2117 (defvar gnus-summary-tool-bar-map nil)
2118
2119 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2120 (defun gnus-summary-make-tool-bar ()
2121   (if (and (fboundp 'tool-bar-add-item-from-menu)
2122            (default-value 'tool-bar-mode)
2123            (not gnus-summary-tool-bar-map))
2124       (setq gnus-summary-tool-bar-map
2125             (let ((tool-bar-map (make-sparse-keymap))
2126                   (load-path (mm-image-load-path)))
2127               (tool-bar-add-item-from-menu
2128                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2129               (tool-bar-add-item-from-menu
2130                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2131               (tool-bar-add-item-from-menu
2132                'gnus-summary-post-news "post" gnus-summary-mode-map)
2133               (tool-bar-add-item-from-menu
2134                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2135               (tool-bar-add-item-from-menu
2136                'gnus-summary-followup "followup" gnus-summary-mode-map)
2137               (tool-bar-add-item-from-menu
2138                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2139               (tool-bar-add-item-from-menu
2140                'gnus-summary-reply "reply" gnus-summary-mode-map)
2141               (tool-bar-add-item-from-menu
2142                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2143               (tool-bar-add-item-from-menu
2144                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2145               (tool-bar-add-item-from-menu
2146                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2147               (tool-bar-add-item-from-menu
2148                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2149               (tool-bar-add-item-from-menu
2150                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2151               (tool-bar-add-item-from-menu
2152                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2153               (tool-bar-add-item-from-menu
2154                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2155               (tool-bar-add-item-from-menu
2156                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2157               tool-bar-map)))
2158   (if gnus-summary-tool-bar-map
2159       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2160
2161 (defun gnus-score-set-default (var value)
2162   "A version of set that updates the GNU Emacs menu-bar."
2163   (set var value)
2164   ;; It is the message that forces the active status to be updated.
2165   (message ""))
2166
2167 (defun gnus-make-score-map (type)
2168   "Make a summary score map of type TYPE."
2169   (if t
2170       nil
2171     (let ((headers '(("author" "from" string)
2172                      ("subject" "subject" string)
2173                      ("article body" "body" string)
2174                      ("article head" "head" string)
2175                      ("xref" "xref" string)
2176                      ("extra header" "extra" string)
2177                      ("lines" "lines" number)
2178                      ("followups to author" "followup" string)))
2179           (types '((number ("less than" <)
2180                            ("greater than" >)
2181                            ("equal" =))
2182                    (string ("substring" s)
2183                            ("exact string" e)
2184                            ("fuzzy string" f)
2185                            ("regexp" r))))
2186           (perms '(("temporary" (current-time-string))
2187                    ("permanent" nil)
2188                    ("immediate" now)))
2189           header)
2190       (list
2191        (apply
2192         'nconc
2193         (list
2194          (if (eq type 'lower)
2195              "Lower score"
2196            "Increase score"))
2197         (let (outh)
2198           (while headers
2199             (setq header (car headers))
2200             (setq outh
2201                   (cons
2202                    (apply
2203                     'nconc
2204                     (list (car header))
2205                     (let ((ts (cdr (assoc (nth 2 header) types)))
2206                           outt)
2207                       (while ts
2208                         (setq outt
2209                               (cons
2210                                (apply
2211                                 'nconc
2212                                 (list (caar ts))
2213                                 (let ((ps perms)
2214                                       outp)
2215                                   (while ps
2216                                     (setq outp
2217                                           (cons
2218                                            (vector
2219                                             (caar ps)
2220                                             (list
2221                                              'gnus-summary-score-entry
2222                                              (nth 1 header)
2223                                              (if (or (string= (nth 1 header)
2224                                                               "head")
2225                                                      (string= (nth 1 header)
2226                                                               "body"))
2227                                                  ""
2228                                                (list 'gnus-summary-header
2229                                                      (nth 1 header)))
2230                                              (list 'quote (nth 1 (car ts)))
2231                                              (list 'gnus-score-delta-default
2232                                                    nil)
2233                                              (nth 1 (car ps))
2234                                              t)
2235                                             t)
2236                                            outp))
2237                                     (setq ps (cdr ps)))
2238                                   (list (nreverse outp))))
2239                                outt))
2240                         (setq ts (cdr ts)))
2241                       (list (nreverse outt))))
2242                    outh))
2243             (setq headers (cdr headers)))
2244           (list (nreverse outh))))))))
2245
2246 \f
2247
2248 (defun gnus-summary-mode (&optional group)
2249   "Major mode for reading articles.
2250
2251 All normal editing commands are switched off.
2252 \\<gnus-summary-mode-map>
2253 Each line in this buffer represents one article.  To read an
2254 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2255 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2256 respectively.
2257
2258 You can also post articles and send mail from this buffer.  To
2259 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2260 of an article, type `\\[gnus-summary-reply]'.
2261
2262 There are approx. one gazillion commands you can execute in this
2263 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2264
2265 The following commands are available:
2266
2267 \\{gnus-summary-mode-map}"
2268   (interactive)
2269   (kill-all-local-variables)
2270   (when (gnus-visual-p 'summary-menu 'menu)
2271     (gnus-summary-make-menu-bar)
2272     (gnus-summary-make-tool-bar))
2273   (gnus-summary-make-local-variables)
2274   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2275     (gnus-summary-make-local-variables))
2276   (gnus-make-thread-indent-array)
2277   (gnus-simplify-mode-line)
2278   (setq major-mode 'gnus-summary-mode)
2279   (setq mode-name "Summary")
2280   (make-local-variable 'minor-mode-alist)
2281   (use-local-map gnus-summary-mode-map)
2282   (buffer-disable-undo)
2283   (setq buffer-read-only t)             ;Disable modification
2284   (setq truncate-lines t)
2285   (setq selective-display t)
2286   (setq selective-display-ellipses t)   ;Display `...'
2287   (gnus-summary-set-display-table)
2288   (gnus-set-default-directory)
2289   (setq gnus-newsgroup-name group)
2290   (make-local-variable 'gnus-summary-line-format)
2291   (make-local-variable 'gnus-summary-line-format-spec)
2292   (make-local-variable 'gnus-summary-dummy-line-format)
2293   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2294   (make-local-variable 'gnus-summary-mark-positions)
2295   (make-local-hook 'pre-command-hook)
2296   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2297   (gnus-run-hooks 'gnus-summary-mode-hook)
2298   (turn-on-gnus-mailing-list-mode)
2299   (mm-enable-multibyte-mule4)
2300   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2301   (gnus-update-summary-mark-positions))
2302
2303 (defun gnus-summary-make-local-variables ()
2304   "Make all the local summary buffer variables."
2305   (let (global)
2306     (dolist (local gnus-summary-local-variables)
2307       (if (consp local)
2308           (progn
2309             (if (eq (cdr local) 'global)
2310                 ;; Copy the global value of the variable.
2311                 (setq global (symbol-value (car local)))
2312               ;; Use the value from the list.
2313               (setq global (eval (cdr local))))
2314             (set (make-local-variable (car local)) global))
2315         ;; Simple nil-valued local variable.
2316         (set (make-local-variable local) nil)))))
2317
2318 (defun gnus-summary-clear-local-variables ()
2319   (let ((locals gnus-summary-local-variables))
2320     (while locals
2321       (if (consp (car locals))
2322           (and (vectorp (caar locals))
2323                (set (caar locals) nil))
2324         (and (vectorp (car locals))
2325              (set (car locals) nil)))
2326       (setq locals (cdr locals)))))
2327
2328 ;; Summary data functions.
2329
2330 (defmacro gnus-data-number (data)
2331   `(car ,data))
2332
2333 (defmacro gnus-data-set-number (data number)
2334   `(setcar ,data ,number))
2335
2336 (defmacro gnus-data-mark (data)
2337   `(nth 1 ,data))
2338
2339 (defmacro gnus-data-set-mark (data mark)
2340   `(setcar (nthcdr 1 ,data) ,mark))
2341
2342 (defmacro gnus-data-pos (data)
2343   `(nth 2 ,data))
2344
2345 (defmacro gnus-data-set-pos (data pos)
2346   `(setcar (nthcdr 2 ,data) ,pos))
2347
2348 (defmacro gnus-data-header (data)
2349   `(nth 3 ,data))
2350
2351 (defmacro gnus-data-set-header (data header)
2352   `(setf (nth 3 ,data) ,header))
2353
2354 (defmacro gnus-data-level (data)
2355   `(nth 4 ,data))
2356
2357 (defmacro gnus-data-unread-p (data)
2358   `(= (nth 1 ,data) gnus-unread-mark))
2359
2360 (defmacro gnus-data-read-p (data)
2361   `(/= (nth 1 ,data) gnus-unread-mark))
2362
2363 (defmacro gnus-data-pseudo-p (data)
2364   `(consp (nth 3 ,data)))
2365
2366 (defmacro gnus-data-find (number)
2367   `(assq ,number gnus-newsgroup-data))
2368
2369 (defmacro gnus-data-find-list (number &optional data)
2370   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2371      (memq (assq ,number bdata)
2372            bdata)))
2373
2374 (defmacro gnus-data-make (number mark pos header level)
2375   `(list ,number ,mark ,pos ,header ,level))
2376
2377 (defun gnus-data-enter (after-article number mark pos header level offset)
2378   (let ((data (gnus-data-find-list after-article)))
2379     (unless data
2380       (error "No such article: %d" after-article))
2381     (setcdr data (cons (gnus-data-make number mark pos header level)
2382                        (cdr data)))
2383     (setq gnus-newsgroup-data-reverse nil)
2384     (gnus-data-update-list (cddr data) offset)))
2385
2386 (defun gnus-data-enter-list (after-article list &optional offset)
2387   (when list
2388     (let ((data (and after-article (gnus-data-find-list after-article)))
2389           (ilist list))
2390       (if (not (or data
2391                    after-article))
2392           (let ((odata gnus-newsgroup-data))
2393             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2394             (when offset
2395               (gnus-data-update-list odata offset)))
2396         ;; Find the last element in the list to be spliced into the main
2397         ;; list.
2398         (while (cdr list)
2399           (setq list (cdr list)))
2400         (if (not data)
2401             (progn
2402               (setcdr list gnus-newsgroup-data)
2403               (setq gnus-newsgroup-data ilist)
2404               (when offset
2405                 (gnus-data-update-list (cdr list) offset)))
2406           (setcdr list (cdr data))
2407           (setcdr data ilist)
2408           (when offset
2409             (gnus-data-update-list (cdr list) offset))))
2410       (setq gnus-newsgroup-data-reverse nil))))
2411
2412 (defun gnus-data-remove (article &optional offset)
2413   (let ((data gnus-newsgroup-data))
2414     (if (= (gnus-data-number (car data)) article)
2415         (progn
2416           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2417                 gnus-newsgroup-data-reverse nil)
2418           (when offset
2419             (gnus-data-update-list gnus-newsgroup-data offset)))
2420       (while (cdr data)
2421         (when (= (gnus-data-number (cadr data)) article)
2422           (setcdr data (cddr data))
2423           (when offset
2424             (gnus-data-update-list (cdr data) offset))
2425           (setq data nil
2426                 gnus-newsgroup-data-reverse nil))
2427         (setq data (cdr data))))))
2428
2429 (defmacro gnus-data-list (backward)
2430   `(if ,backward
2431        (or gnus-newsgroup-data-reverse
2432            (setq gnus-newsgroup-data-reverse
2433                  (reverse gnus-newsgroup-data)))
2434      gnus-newsgroup-data))
2435
2436 (defun gnus-data-update-list (data offset)
2437   "Add OFFSET to the POS of all data entries in DATA."
2438   (setq gnus-newsgroup-data-reverse nil)
2439   (while data
2440     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2441     (setq data (cdr data))))
2442
2443 (defun gnus-summary-article-pseudo-p (article)
2444   "Say whether this article is a pseudo article or not."
2445   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2446
2447 (defmacro gnus-summary-article-sparse-p (article)
2448   "Say whether this article is a sparse article or not."
2449   `(memq ,article gnus-newsgroup-sparse))
2450
2451 (defmacro gnus-summary-article-ancient-p (article)
2452   "Say whether this article is a sparse article or not."
2453   `(memq ,article gnus-newsgroup-ancient))
2454
2455 (defun gnus-article-parent-p (number)
2456   "Say whether this article is a parent or not."
2457   (let ((data (gnus-data-find-list number)))
2458     (and (cdr data)                     ; There has to be an article after...
2459          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2460             (gnus-data-level (nth 1 data))))))
2461
2462 (defun gnus-article-children (number)
2463   "Return a list of all children to NUMBER."
2464   (let* ((data (gnus-data-find-list number))
2465          (level (gnus-data-level (car data)))
2466          children)
2467     (setq data (cdr data))
2468     (while (and data
2469                 (= (gnus-data-level (car data)) (1+ level)))
2470       (push (gnus-data-number (car data)) children)
2471       (setq data (cdr data)))
2472     children))
2473
2474 (defmacro gnus-summary-skip-intangible ()
2475   "If the current article is intangible, then jump to a different article."
2476   '(let ((to (get-text-property (point) 'gnus-intangible)))
2477      (and to (gnus-summary-goto-subject to))))
2478
2479 (defmacro gnus-summary-article-intangible-p ()
2480   "Say whether this article is intangible or not."
2481   '(get-text-property (point) 'gnus-intangible))
2482
2483 (defun gnus-article-read-p (article)
2484   "Say whether ARTICLE is read or not."
2485   (not (or (memq article gnus-newsgroup-marked)
2486            (memq article gnus-newsgroup-unreads)
2487            (memq article gnus-newsgroup-unselected)
2488            (memq article gnus-newsgroup-dormant))))
2489
2490 ;; Some summary mode macros.
2491
2492 (defmacro gnus-summary-article-number ()
2493   "The article number of the article on the current line.
2494 If there isn's an article number here, then we return the current
2495 article number."
2496   '(progn
2497      (gnus-summary-skip-intangible)
2498      (or (get-text-property (point) 'gnus-number)
2499          (gnus-summary-last-subject))))
2500
2501 (defmacro gnus-summary-article-header (&optional number)
2502   "Return the header of article NUMBER."
2503   `(gnus-data-header (gnus-data-find
2504                       ,(or number '(gnus-summary-article-number)))))
2505
2506 (defmacro gnus-summary-thread-level (&optional number)
2507   "Return the level of thread that starts with article NUMBER."
2508   `(if (and (eq gnus-summary-make-false-root 'dummy)
2509             (get-text-property (point) 'gnus-intangible))
2510        0
2511      (gnus-data-level (gnus-data-find
2512                        ,(or number '(gnus-summary-article-number))))))
2513
2514 (defmacro gnus-summary-article-mark (&optional number)
2515   "Return the mark of article NUMBER."
2516   `(gnus-data-mark (gnus-data-find
2517                     ,(or number '(gnus-summary-article-number)))))
2518
2519 (defmacro gnus-summary-article-pos (&optional number)
2520   "Return the position of the line of article NUMBER."
2521   `(gnus-data-pos (gnus-data-find
2522                    ,(or number '(gnus-summary-article-number)))))
2523
2524 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2525 (defmacro gnus-summary-article-subject (&optional number)
2526   "Return current subject string or nil if nothing."
2527   `(let ((headers
2528           ,(if number
2529                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2530              '(gnus-data-header (assq (gnus-summary-article-number)
2531                                       gnus-newsgroup-data)))))
2532      (and headers
2533           (vectorp headers)
2534           (mail-header-subject headers))))
2535
2536 (defmacro gnus-summary-article-score (&optional number)
2537   "Return current article score."
2538   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2539                   gnus-newsgroup-scored))
2540        gnus-summary-default-score 0))
2541
2542 (defun gnus-summary-article-children (&optional number)
2543   "Return a list of article numbers that are children of article NUMBER."
2544   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2545          (level (gnus-data-level (car data)))
2546          l children)
2547     (while (and (setq data (cdr data))
2548                 (> (setq l (gnus-data-level (car data))) level))
2549       (and (= (1+ level) l)
2550            (push (gnus-data-number (car data))
2551                  children)))
2552     (nreverse children)))
2553
2554 (defun gnus-summary-article-parent (&optional number)
2555   "Return the article number of the parent of article NUMBER."
2556   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2557                                     (gnus-data-list t)))
2558          (level (gnus-data-level (car data))))
2559     (if (zerop level)
2560         ()                              ; This is a root.
2561       ;; We search until we find an article with a level less than
2562       ;; this one.  That function has to be the parent.
2563       (while (and (setq data (cdr data))
2564                   (not (< (gnus-data-level (car data)) level))))
2565       (and data (gnus-data-number (car data))))))
2566
2567 (defun gnus-unread-mark-p (mark)
2568   "Say whether MARK is the unread mark."
2569   (= mark gnus-unread-mark))
2570
2571 (defun gnus-read-mark-p (mark)
2572   "Say whether MARK is one of the marks that mark as read.
2573 This is all marks except unread, ticked, dormant, and expirable."
2574   (not (or (= mark gnus-unread-mark)
2575            (= mark gnus-ticked-mark)
2576            (= mark gnus-dormant-mark)
2577            (= mark gnus-expirable-mark))))
2578
2579 (defmacro gnus-article-mark (number)
2580   "Return the MARK of article NUMBER.
2581 This macro should only be used when computing the mark the \"first\"
2582 time; i.e., when generating the summary lines.  After that,
2583 `gnus-summary-article-mark' should be used to examine the
2584 marks of articles."
2585   `(cond
2586     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2587     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2588     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2589     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2590     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2591     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2592     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2593     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2594            gnus-ancient-mark))))
2595
2596 ;; Saving hidden threads.
2597
2598 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2599 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2600
2601 (defmacro gnus-save-hidden-threads (&rest forms)
2602   "Save hidden threads, eval FORMS, and restore the hidden threads."
2603   (let ((config (make-symbol "config")))
2604     `(let ((,config (gnus-hidden-threads-configuration)))
2605        (unwind-protect
2606            (save-excursion
2607              ,@forms)
2608          (gnus-restore-hidden-threads-configuration ,config)))))
2609
2610 (defun gnus-data-compute-positions ()
2611   "Compute the positions of all articles."
2612   (setq gnus-newsgroup-data-reverse nil)
2613   (let ((data gnus-newsgroup-data))
2614     (save-excursion
2615       (gnus-save-hidden-threads
2616         (gnus-summary-show-all-threads)
2617         (goto-char (point-min))
2618         (while data
2619           (while (get-text-property (point) 'gnus-intangible)
2620             (forward-line 1))
2621           (gnus-data-set-pos (car data) (+ (point) 3))
2622           (setq data (cdr data))
2623           (forward-line 1))))))
2624
2625 (defun gnus-hidden-threads-configuration ()
2626   "Return the current hidden threads configuration."
2627   (save-excursion
2628     (let (config)
2629       (goto-char (point-min))
2630       (while (search-forward "\r" nil t)
2631         (push (1- (point)) config))
2632       config)))
2633
2634 (defun gnus-restore-hidden-threads-configuration (config)
2635   "Restore hidden threads configuration from CONFIG."
2636   (save-excursion
2637     (let (point buffer-read-only)
2638       (while (setq point (pop config))
2639         (when (and (< point (point-max))
2640                    (goto-char point)
2641                    (eq (char-after) ?\n))
2642           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2643
2644 ;; Various summary mode internalish functions.
2645
2646 (defun gnus-mouse-pick-article (e)
2647   (interactive "e")
2648   (mouse-set-point e)
2649   (gnus-summary-next-page nil t))
2650
2651 (defun gnus-summary-set-display-table ()
2652   "Change the display table.
2653 Odd characters have a tendency to mess
2654 up nicely formatted displays - we make all possible glyphs
2655 display only a single character."
2656
2657   ;; We start from the standard display table, if any.
2658   (let ((table (or (copy-sequence standard-display-table)
2659                    (make-display-table)))
2660         (i 32))
2661     ;; Nix out all the control chars...
2662     (while (>= (setq i (1- i)) 0)
2663       (aset table i [??]))
2664     ;; ... but not newline and cr, of course.  (cr is necessary for the
2665     ;; selective display).
2666     (aset table ?\n nil)
2667     (aset table ?\r nil)
2668     ;; We keep TAB as well.
2669     (aset table ?\t nil)
2670     ;; We nix out any glyphs over 126 that are not set already.
2671     (let ((i 256))
2672       (while (>= (setq i (1- i)) 127)
2673         ;; Only modify if the entry is nil.
2674         (unless (aref table i)
2675           (aset table i [??]))))
2676     (setq buffer-display-table table)))
2677
2678 (defun gnus-summary-buffer-name (group)
2679   "Return the summary buffer name of GROUP."
2680   (concat "*Summary " group "*"))
2681
2682 (defun gnus-summary-setup-buffer (group)
2683   "Initialize summary buffer."
2684   (let ((buffer (gnus-summary-buffer-name group)))
2685     (if (get-buffer buffer)
2686         (progn
2687           (set-buffer buffer)
2688           (setq gnus-summary-buffer (current-buffer))
2689           (not gnus-newsgroup-prepared))
2690       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2691       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2692       (gnus-summary-mode group)
2693       (when gnus-carpal
2694         (gnus-carpal-setup-buffer 'summary))
2695       (unless gnus-single-article-buffer
2696         (make-local-variable 'gnus-article-buffer)
2697         (make-local-variable 'gnus-article-current)
2698         (make-local-variable 'gnus-original-article-buffer))
2699       (setq gnus-newsgroup-name group)
2700       ;; Set any local variables in the group parameters.
2701       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2702       t)))
2703
2704 (defun gnus-set-global-variables ()
2705   "Set the global equivalents of the buffer-local variables.
2706 They are set to the latest values they had.  These reflect the summary
2707 buffer that was in action when the last article was fetched."
2708   (when (eq major-mode 'gnus-summary-mode)
2709     (setq gnus-summary-buffer (current-buffer))
2710     (let ((name gnus-newsgroup-name)
2711           (marked gnus-newsgroup-marked)
2712           (unread gnus-newsgroup-unreads)
2713           (headers gnus-current-headers)
2714           (data gnus-newsgroup-data)
2715           (summary gnus-summary-buffer)
2716           (article-buffer gnus-article-buffer)
2717           (original gnus-original-article-buffer)
2718           (gac gnus-article-current)
2719           (reffed gnus-reffed-article-number)
2720           (score-file gnus-current-score-file)
2721           (default-charset gnus-newsgroup-charset)
2722           vlist)
2723       (let ((locals gnus-newsgroup-variables))
2724         (while locals
2725           (if (consp (car locals))
2726               (push (eval (caar locals)) vlist)
2727             (push (eval (car locals)) vlist))
2728           (setq locals (cdr locals)))
2729         (setq vlist (nreverse vlist)))
2730       (save-excursion
2731         (set-buffer gnus-group-buffer)
2732         (setq gnus-newsgroup-name name
2733               gnus-newsgroup-marked marked
2734               gnus-newsgroup-unreads unread
2735               gnus-current-headers headers
2736               gnus-newsgroup-data data
2737               gnus-article-current gac
2738               gnus-summary-buffer summary
2739               gnus-article-buffer article-buffer
2740               gnus-original-article-buffer original
2741               gnus-reffed-article-number reffed
2742               gnus-current-score-file score-file
2743               gnus-newsgroup-charset default-charset)
2744         (let ((locals gnus-newsgroup-variables))
2745           (while locals
2746             (if (consp (car locals))
2747                 (set (caar locals) (pop vlist))
2748               (set (car locals) (pop vlist)))
2749             (setq locals (cdr locals))))
2750         ;; The article buffer also has local variables.
2751         (when (gnus-buffer-live-p gnus-article-buffer)
2752           (set-buffer gnus-article-buffer)
2753           (setq gnus-summary-buffer summary))))))
2754
2755 (defun gnus-summary-article-unread-p (article)
2756   "Say whether ARTICLE is unread or not."
2757   (memq article gnus-newsgroup-unreads))
2758
2759 (defun gnus-summary-first-article-p (&optional article)
2760   "Return whether ARTICLE is the first article in the buffer."
2761   (if (not (setq article (or article (gnus-summary-article-number))))
2762       nil
2763     (eq article (caar gnus-newsgroup-data))))
2764
2765 (defun gnus-summary-last-article-p (&optional article)
2766   "Return whether ARTICLE is the last article in the buffer."
2767   (if (not (setq article (or article (gnus-summary-article-number))))
2768       ;; All non-existent numbers are the last article.  :-)
2769       t
2770     (not (cdr (gnus-data-find-list article)))))
2771
2772 (defun gnus-make-thread-indent-array ()
2773   (let ((n 200))
2774     (unless (and gnus-thread-indent-array
2775                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2776       (setq gnus-thread-indent-array (make-vector 201 "")
2777             gnus-thread-indent-array-level gnus-thread-indent-level)
2778       (while (>= n 0)
2779         (aset gnus-thread-indent-array n
2780               (make-string (* n gnus-thread-indent-level) ? ))
2781         (setq n (1- n))))))
2782
2783 (defun gnus-update-summary-mark-positions ()
2784   "Compute where the summary marks are to go."
2785   (save-excursion
2786     (when (gnus-buffer-exists-p gnus-summary-buffer)
2787       (set-buffer gnus-summary-buffer))
2788     (let ((gnus-replied-mark 129)
2789           (gnus-score-below-mark 130)
2790           (gnus-score-over-mark 130)
2791           (gnus-download-mark 131)
2792           (spec gnus-summary-line-format-spec)
2793           gnus-visual pos)
2794       (save-excursion
2795         (gnus-set-work-buffer)
2796         (let ((gnus-summary-line-format-spec spec)
2797               (gnus-newsgroup-downloadable '((0 . t))))
2798           (gnus-summary-insert-line
2799            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2800           (goto-char (point-min))
2801           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2802                                              (- (point) 2)))))
2803           (goto-char (point-min))
2804           (push (cons 'replied (and (search-forward "\201" nil t)
2805                                     (- (point) 2)))
2806                 pos)
2807           (goto-char (point-min))
2808           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2809                 pos)
2810           (goto-char (point-min))
2811           (push (cons 'download
2812                       (and (search-forward "\203" nil t) (- (point) 2)))
2813                 pos)))
2814       (setq gnus-summary-mark-positions pos))))
2815
2816 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2817   "Insert a dummy root in the summary buffer."
2818   (beginning-of-line)
2819   (gnus-add-text-properties
2820    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2821    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2822
2823 (defun gnus-summary-from-or-to-or-newsgroups (header)
2824   (let ((to (cdr (assq 'To (mail-header-extra header))))
2825         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2826         (mail-parse-charset gnus-newsgroup-charset)
2827         (mail-parse-ignored-charsets
2828          (save-excursion (set-buffer gnus-summary-buffer)
2829                          gnus-newsgroup-ignored-charsets)))
2830     (cond
2831      ((and to
2832            gnus-ignored-from-addresses
2833            (string-match gnus-ignored-from-addresses
2834                          (mail-header-from header)))
2835       (concat "-> "
2836               (or (car (funcall gnus-extract-address-components
2837                                 (funcall
2838                                  gnus-decode-encoded-word-function to)))
2839                   (funcall gnus-decode-encoded-word-function to))))
2840      ((and newsgroups
2841            gnus-ignored-from-addresses
2842            (string-match gnus-ignored-from-addresses
2843                          (mail-header-from header)))
2844       (concat "=> " newsgroups))
2845      (t
2846       (or (car (funcall gnus-extract-address-components
2847                         (mail-header-from header)))
2848           (mail-header-from header))))))
2849
2850 (defun gnus-summary-insert-line (gnus-tmp-header
2851                                  gnus-tmp-level gnus-tmp-current
2852                                  gnus-tmp-unread gnus-tmp-replied
2853                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2854                                  &optional gnus-tmp-dummy gnus-tmp-score
2855                                  gnus-tmp-process)
2856   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2857          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2858          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2859          (gnus-tmp-score-char
2860           (if (or (null gnus-summary-default-score)
2861                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2862                       gnus-summary-zcore-fuzz))
2863               ?  ;Whitespace
2864             (if (< gnus-tmp-score gnus-summary-default-score)
2865                 gnus-score-below-mark gnus-score-over-mark)))
2866          (gnus-tmp-replied
2867           (cond (gnus-tmp-process gnus-process-mark)
2868                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2869                  gnus-cached-mark)
2870                 (gnus-tmp-replied gnus-replied-mark)
2871                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2872                  gnus-saved-mark)
2873                 (t gnus-no-mark)))
2874          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2875          (gnus-tmp-name
2876           (cond
2877            ((string-match "<[^>]+> *$" gnus-tmp-from)
2878             (let ((beg (match-beginning 0)))
2879               (or (and (string-match "^\".+\"" gnus-tmp-from)
2880                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2881                   (substring gnus-tmp-from 0 beg))))
2882            ((string-match "(.+)" gnus-tmp-from)
2883             (substring gnus-tmp-from
2884                        (1+ (match-beginning 0)) (1- (match-end 0))))
2885            (t gnus-tmp-from)))
2886          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2887          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2888          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2889          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2890          (buffer-read-only nil))
2891     (when (string= gnus-tmp-name "")
2892       (setq gnus-tmp-name gnus-tmp-from))
2893     (unless (numberp gnus-tmp-lines)
2894       (setq gnus-tmp-lines -1))
2895     (when (= gnus-tmp-lines -1)
2896       (setq gnus-tmp-lines "?"))
2897     (gnus-put-text-property
2898      (point)
2899      (progn (eval gnus-summary-line-format-spec) (point))
2900      'gnus-number gnus-tmp-number)
2901     (when (gnus-visual-p 'summary-highlight 'highlight)
2902       (forward-line -1)
2903       (gnus-run-hooks 'gnus-summary-update-hook)
2904       (forward-line 1))))
2905
2906 (defun gnus-summary-update-line (&optional dont-update)
2907   "Update summary line after change."
2908   (when (and gnus-summary-default-score
2909              (not gnus-summary-inhibit-highlight))
2910     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2911            (article (gnus-summary-article-number))
2912            (score (gnus-summary-article-score article)))
2913       (unless dont-update
2914         (if (and gnus-summary-mark-below
2915                  (< (gnus-summary-article-score)
2916                     gnus-summary-mark-below))
2917             ;; This article has a low score, so we mark it as read.
2918             (when (memq article gnus-newsgroup-unreads)
2919               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2920           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2921             ;; This article was previously marked as read on account
2922             ;; of a low score, but now it has risen, so we mark it as
2923             ;; unread.
2924             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2925         (gnus-summary-update-mark
2926          (if (or (null gnus-summary-default-score)
2927                  (<= (abs (- score gnus-summary-default-score))
2928                      gnus-summary-zcore-fuzz))
2929              ?  ;Whitespace
2930            (if (< score gnus-summary-default-score)
2931                gnus-score-below-mark gnus-score-over-mark))
2932          'score))
2933       ;; Do visual highlighting.
2934       (when (gnus-visual-p 'summary-highlight 'highlight)
2935         (gnus-run-hooks 'gnus-summary-update-hook)))))
2936
2937 (defvar gnus-tmp-new-adopts nil)
2938
2939 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2940   "Return the number of articles in THREAD.
2941 This may be 0 in some cases -- if none of the articles in
2942 the thread are to be displayed."
2943   (let* ((number
2944           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2945           (cond
2946            ((not (listp thread))
2947             1)
2948            ((and (consp thread) (cdr thread))
2949             (apply
2950              '+ 1 (mapcar
2951                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2952            ((null thread)
2953             1)
2954            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2955             1)
2956            (t 0))))
2957     (when (and level (zerop level) gnus-tmp-new-adopts)
2958       (incf number
2959             (apply '+ (mapcar
2960                        'gnus-summary-number-of-articles-in-thread
2961                        gnus-tmp-new-adopts))))
2962     (if char
2963         (if (> number 1) gnus-not-empty-thread-mark
2964           gnus-empty-thread-mark)
2965       number)))
2966
2967 (defun gnus-summary-set-local-parameters (group)
2968   "Go through the local params of GROUP and set all variable specs in that list."
2969   (let ((params (gnus-group-find-parameter group))
2970         elem)
2971     (while params
2972       (setq elem (car params)
2973             params (cdr params))
2974       (and (consp elem)                 ; Has to be a cons.
2975            (consp (cdr elem))           ; The cdr has to be a list.
2976            (symbolp (car elem))         ; Has to be a symbol in there.
2977            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2978            (ignore-errors               ; So we set it.
2979              (make-local-variable (car elem))
2980              (set (car elem) (eval (nth 1 elem))))))))
2981
2982 (defun gnus-summary-read-group (group &optional show-all no-article
2983                                       kill-buffer no-display backward
2984                                       select-articles)
2985   "Start reading news in newsgroup GROUP.
2986 If SHOW-ALL is non-nil, already read articles are also listed.
2987 If NO-ARTICLE is non-nil, no article is selected initially.
2988 If NO-DISPLAY, don't generate a summary buffer."
2989   (let (result)
2990     (while (and group
2991                 (null (setq result
2992                             (let ((gnus-auto-select-next nil))
2993                               (or (gnus-summary-read-group-1
2994                                    group show-all no-article
2995                                    kill-buffer no-display
2996                                    select-articles)
2997                                   (setq show-all nil
2998                                         select-articles nil)))))
2999                 (eq gnus-auto-select-next 'quietly))
3000       (set-buffer gnus-group-buffer)
3001       ;; The entry function called above goes to the next
3002       ;; group automatically, so we go two groups back
3003       ;; if we are searching for the previous group.
3004       (when backward
3005         (gnus-group-prev-unread-group 2))
3006       (if (not (equal group (gnus-group-group-name)))
3007           (setq group (gnus-group-group-name))
3008         (setq group nil)))
3009     result))
3010
3011 (defun gnus-summary-read-group-1 (group show-all no-article
3012                                         kill-buffer no-display
3013                                         &optional select-articles)
3014   ;; Killed foreign groups can't be entered.
3015   ;;  (when (and (not (gnus-group-native-p group))
3016   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3017   ;;    (error "Dead non-native groups can't be entered"))
3018   (gnus-message 5 "Retrieving newsgroup: %s..." 
3019                 (gnus-group-decoded-name group))
3020   (let* ((new-group (gnus-summary-setup-buffer group))
3021          (quit-config (gnus-group-quit-config group))
3022          (did-select (and new-group (gnus-select-newsgroup
3023                                      group show-all select-articles))))
3024     (cond
3025      ;; This summary buffer exists already, so we just select it.
3026      ((not new-group)
3027       (gnus-set-global-variables)
3028       (when kill-buffer
3029         (gnus-kill-or-deaden-summary kill-buffer))
3030       (gnus-configure-windows 'summary 'force)
3031       (gnus-set-mode-line 'summary)
3032       (gnus-summary-position-point)
3033       (message "")
3034       t)
3035      ;; We couldn't select this group.
3036      ((null did-select)
3037       (when (and (eq major-mode 'gnus-summary-mode)
3038                  (not (equal (current-buffer) kill-buffer)))
3039         (kill-buffer (current-buffer))
3040         (if (not quit-config)
3041             (progn
3042               ;; Update the info -- marks might need to be removed,
3043               ;; for instance.
3044               (gnus-summary-update-info)
3045               (set-buffer gnus-group-buffer)
3046               (gnus-group-jump-to-group group)
3047               (gnus-group-next-unread-group 1))
3048           (gnus-handle-ephemeral-exit quit-config)))
3049       (let ((grpinfo (gnus-get-info group)))
3050         (if (null (gnus-info-read grpinfo))
3051             (gnus-message 3 "Group %s contains no messages" 
3052                           (gnus-group-decoded-name group))
3053           (gnus-message 3 "Can't select group")))
3054       nil)
3055      ;; The user did a `C-g' while prompting for number of articles,
3056      ;; so we exit this group.
3057      ((eq did-select 'quit)
3058       (and (eq major-mode 'gnus-summary-mode)
3059            (not (equal (current-buffer) kill-buffer))
3060            (kill-buffer (current-buffer)))
3061       (when kill-buffer
3062         (gnus-kill-or-deaden-summary kill-buffer))
3063       (if (not quit-config)
3064           (progn
3065             (set-buffer gnus-group-buffer)
3066             (gnus-group-jump-to-group group)
3067             (gnus-group-next-unread-group 1)
3068             (gnus-configure-windows 'group 'force))
3069         (gnus-handle-ephemeral-exit quit-config))
3070       ;; Finally signal the quit.
3071       (signal 'quit nil))
3072      ;; The group was successfully selected.
3073      (t
3074       (gnus-set-global-variables)
3075       ;; Save the active value in effect when the group was entered.
3076       (setq gnus-newsgroup-active
3077             (gnus-copy-sequence
3078              (gnus-active gnus-newsgroup-name)))
3079       ;; You can change the summary buffer in some way with this hook.
3080       (gnus-run-hooks 'gnus-select-group-hook)
3081       (gnus-update-format-specifications
3082        nil 'summary 'summary-mode 'summary-dummy)
3083       (gnus-update-summary-mark-positions)
3084       ;; Do score processing.
3085       (when gnus-use-scoring
3086         (gnus-possibly-score-headers))
3087       ;; Check whether to fill in the gaps in the threads.
3088       (when gnus-build-sparse-threads
3089         (gnus-build-sparse-threads))
3090       ;; Find the initial limit.
3091       (if gnus-show-threads
3092           (if show-all
3093               (let ((gnus-newsgroup-dormant nil))
3094                 (gnus-summary-initial-limit show-all))
3095             (gnus-summary-initial-limit show-all))
3096         ;; When untreaded, all articles are always shown.
3097         (setq gnus-newsgroup-limit
3098               (mapcar
3099                (lambda (header) (mail-header-number header))
3100                gnus-newsgroup-headers)))
3101       ;; Generate the summary buffer.
3102       (unless no-display
3103         (gnus-summary-prepare))
3104       (when gnus-use-trees
3105         (gnus-tree-open group)
3106         (setq gnus-summary-highlight-line-function
3107               'gnus-tree-highlight-article))
3108       ;; If the summary buffer is empty, but there are some low-scored
3109       ;; articles or some excluded dormants, we include these in the
3110       ;; buffer.
3111       (when (and (zerop (buffer-size))
3112                  (not no-display))
3113         (cond (gnus-newsgroup-dormant
3114                (gnus-summary-limit-include-dormant))
3115               ((and gnus-newsgroup-scored show-all)
3116                (gnus-summary-limit-include-expunged t))))
3117       ;; Function `gnus-apply-kill-file' must be called in this hook.
3118       (gnus-run-hooks 'gnus-apply-kill-hook)
3119       (if (and (zerop (buffer-size))
3120                (not no-display))
3121           (progn
3122             ;; This newsgroup is empty.
3123             (gnus-summary-catchup-and-exit nil t)
3124             (gnus-message 6 "No unread news")
3125             (when kill-buffer
3126               (gnus-kill-or-deaden-summary kill-buffer))
3127             ;; Return nil from this function.
3128             nil)
3129         ;; Hide conversation thread subtrees.  We cannot do this in
3130         ;; gnus-summary-prepare-hook since kill processing may not
3131         ;; work with hidden articles.
3132         (and gnus-show-threads
3133              gnus-thread-hide-subtree
3134              (gnus-summary-hide-all-threads))
3135         (when kill-buffer
3136           (gnus-kill-or-deaden-summary kill-buffer))
3137         ;; Show first unread article if requested.
3138         (if (and (not no-article)
3139                  (not no-display)
3140                  gnus-newsgroup-unreads
3141                  gnus-auto-select-first)
3142             (progn
3143               (gnus-configure-windows 'summary)
3144               (cond
3145                ((eq gnus-auto-select-first 'best)
3146                 (gnus-summary-best-unread-article))
3147                ((eq gnus-auto-select-first t)
3148                 (gnus-summary-first-unread-article))
3149                ((gnus-functionp gnus-auto-select-first)
3150                 (funcall gnus-auto-select-first))))
3151           ;; Don't select any articles, just move point to the first
3152           ;; article in the group.
3153           (goto-char (point-min))
3154           (gnus-summary-position-point)
3155           (gnus-configure-windows 'summary 'force)
3156           (gnus-set-mode-line 'summary))
3157         (when (get-buffer-window gnus-group-buffer t)
3158           ;; Gotta use windows, because recenter does weird stuff if
3159           ;; the current buffer ain't the displayed window.
3160           (let ((owin (selected-window)))
3161             (select-window (get-buffer-window gnus-group-buffer t))
3162             (when (gnus-group-goto-group group)
3163               (recenter))
3164             (select-window owin)))
3165         ;; Mark this buffer as "prepared".
3166         (setq gnus-newsgroup-prepared t)
3167         (gnus-run-hooks 'gnus-summary-prepared-hook)
3168         t)))))
3169
3170 (defun gnus-summary-prepare ()
3171   "Generate the summary buffer."
3172   (interactive)
3173   (let ((buffer-read-only nil))
3174     (erase-buffer)
3175     (setq gnus-newsgroup-data nil
3176           gnus-newsgroup-data-reverse nil)
3177     (gnus-run-hooks 'gnus-summary-generate-hook)
3178     ;; Generate the buffer, either with threads or without.
3179     (when gnus-newsgroup-headers
3180       (gnus-summary-prepare-threads
3181        (if gnus-show-threads
3182            (gnus-sort-gathered-threads
3183             (funcall gnus-summary-thread-gathering-function
3184                      (gnus-sort-threads
3185                       (gnus-cut-threads (gnus-make-threads)))))
3186          ;; Unthreaded display.
3187          (gnus-sort-articles gnus-newsgroup-headers))))
3188     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3189     ;; Call hooks for modifying summary buffer.
3190     (goto-char (point-min))
3191     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3192
3193 (defsubst gnus-general-simplify-subject (subject)
3194   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3195   (setq subject
3196         (cond
3197          ;; Truncate the subject.
3198          (gnus-simplify-subject-functions
3199           (gnus-map-function gnus-simplify-subject-functions subject))
3200          ((numberp gnus-summary-gather-subject-limit)
3201           (setq subject (gnus-simplify-subject-re subject))
3202           (if (> (length subject) gnus-summary-gather-subject-limit)
3203               (substring subject 0 gnus-summary-gather-subject-limit)
3204             subject))
3205          ;; Fuzzily simplify it.
3206          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3207           (gnus-simplify-subject-fuzzy subject))
3208          ;; Just remove the leading "Re:".
3209          (t
3210           (gnus-simplify-subject-re subject))))
3211
3212   (if (and gnus-summary-gather-exclude-subject
3213            (string-match gnus-summary-gather-exclude-subject subject))
3214       nil                               ; This article shouldn't be gathered
3215     subject))
3216
3217 (defun gnus-summary-simplify-subject-query ()
3218   "Query where the respool algorithm would put this article."
3219   (interactive)
3220   (gnus-summary-select-article)
3221   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3222
3223 (defun gnus-gather-threads-by-subject (threads)
3224   "Gather threads by looking at Subject headers."
3225   (if (not gnus-summary-make-false-root)
3226       threads
3227     (let ((hashtb (gnus-make-hashtable 1024))
3228           (prev threads)
3229           (result threads)
3230           subject hthread whole-subject)
3231       (while threads
3232         (setq subject (gnus-general-simplify-subject
3233                        (setq whole-subject (mail-header-subject
3234                                             (caar threads)))))
3235         (when subject
3236           (if (setq hthread (gnus-gethash subject hashtb))
3237               (progn
3238                 ;; We enter a dummy root into the thread, if we
3239                 ;; haven't done that already.
3240                 (unless (stringp (caar hthread))
3241                   (setcar hthread (list whole-subject (car hthread))))
3242                 ;; We add this new gathered thread to this gathered
3243                 ;; thread.
3244                 (setcdr (car hthread)
3245                         (nconc (cdar hthread) (list (car threads))))
3246                 ;; Remove it from the list of threads.
3247                 (setcdr prev (cdr threads))
3248                 (setq threads prev))
3249             ;; Enter this thread into the hash table.
3250             (gnus-sethash subject threads hashtb)))
3251         (setq prev threads)
3252         (setq threads (cdr threads)))
3253       result)))
3254
3255 (defun gnus-gather-threads-by-references (threads)
3256   "Gather threads by looking at References headers."
3257   (let ((idhashtb (gnus-make-hashtable 1024))
3258         (thhashtb (gnus-make-hashtable 1024))
3259         (prev threads)
3260         (result threads)
3261         ids references id gthread gid entered ref)
3262     (while threads
3263       (when (setq references (mail-header-references (caar threads)))
3264         (setq id (mail-header-id (caar threads))
3265               ids (gnus-split-references references)
3266               entered nil)
3267         (while (setq ref (pop ids))
3268           (setq ids (delete ref ids))
3269           (if (not (setq gid (gnus-gethash ref idhashtb)))
3270               (progn
3271                 (gnus-sethash ref id idhashtb)
3272                 (gnus-sethash id threads thhashtb))
3273             (setq gthread (gnus-gethash gid thhashtb))
3274             (unless entered
3275               ;; We enter a dummy root into the thread, if we
3276               ;; haven't done that already.
3277               (unless (stringp (caar gthread))
3278                 (setcar gthread (list (mail-header-subject (caar gthread))
3279                                       (car gthread))))
3280               ;; We add this new gathered thread to this gathered
3281               ;; thread.
3282               (setcdr (car gthread)
3283                       (nconc (cdar gthread) (list (car threads)))))
3284             ;; Add it into the thread hash table.
3285             (gnus-sethash id gthread thhashtb)
3286             (setq entered t)
3287             ;; Remove it from the list of threads.
3288             (setcdr prev (cdr threads))
3289             (setq threads prev))))
3290       (setq prev threads)
3291       (setq threads (cdr threads)))
3292     result))
3293
3294 (defun gnus-sort-gathered-threads (threads)
3295   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3296   (let ((result threads))
3297     (while threads
3298       (when (stringp (caar threads))
3299         (setcdr (car threads)
3300                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3301       (setq threads (cdr threads)))
3302     result))
3303
3304 (defun gnus-thread-loop-p (root thread)
3305   "Say whether ROOT is in THREAD."
3306   (let ((stack (list thread))
3307         (infloop 0)
3308         th)
3309     (while (setq thread (pop stack))
3310       (setq th (cdr thread))
3311       (while (and th
3312                   (not (eq (caar th) root)))
3313         (pop th))
3314       (if th
3315           ;; We have found a loop.
3316           (let (ref-dep)
3317             (setcdr thread (delq (car th) (cdr thread)))
3318             (if (boundp (setq ref-dep (intern "none"
3319                                               gnus-newsgroup-dependencies)))
3320                 (setcdr (symbol-value ref-dep)
3321                         (nconc (cdr (symbol-value ref-dep))
3322                                (list (car th))))
3323               (set ref-dep (list nil (car th))))
3324             (setq infloop 1
3325                   stack nil))
3326         ;; Push all the subthreads onto the stack.
3327         (push (cdr thread) stack)))
3328     infloop))
3329
3330 (defun gnus-make-threads ()
3331   "Go through the dependency hashtb and find the roots.  Return all threads."
3332   (let (threads)
3333     (while (catch 'infloop
3334              (mapatoms
3335               (lambda (refs)
3336                 ;; Deal with self-referencing References loops.
3337                 (when (and (car (symbol-value refs))
3338                            (not (zerop
3339                                  (apply
3340                                   '+
3341                                   (mapcar
3342                                    (lambda (thread)
3343                                      (gnus-thread-loop-p
3344                                       (car (symbol-value refs)) thread))
3345                                    (cdr (symbol-value refs)))))))
3346                   (setq threads nil)
3347                   (throw 'infloop t))
3348                 (unless (car (symbol-value refs))
3349                   ;; These threads do not refer back to any other articles,
3350                   ;; so they're roots.
3351                   (setq threads (append (cdr (symbol-value refs)) threads))))
3352               gnus-newsgroup-dependencies)))
3353     threads))
3354
3355 ;; Build the thread tree.
3356 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3357   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3358
3359 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3360 if it was already present.
3361
3362 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3363 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3364 Message-IDs will be renamed be renamed to a unique Message-ID before
3365 being entered.
3366
3367 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3368   (let* ((id (mail-header-id header))
3369          (id-dep (and id (intern id dependencies)))
3370          ref ref-dep ref-header)
3371     ;; Enter this `header' in the `dependencies' table.
3372     (cond
3373      ((not id-dep)
3374       (setq header nil))
3375      ;; The first two cases do the normal part: enter a new `header'
3376      ;; in the `dependencies' table.
3377      ((not (boundp id-dep))
3378       (set id-dep (list header)))
3379      ((null (car (symbol-value id-dep)))
3380       (setcar (symbol-value id-dep) header))
3381
3382      ;; From here the `header' was already present in the
3383      ;; `dependencies' table.
3384      (force-new
3385       ;; Overrides an existing entry;
3386       ;; just set the header part of the entry.
3387       (setcar (symbol-value id-dep) header))
3388
3389      ;; Renames the existing `header' to a unique Message-ID.
3390      ((not gnus-summary-ignore-duplicates)
3391       ;; An article with this Message-ID has already been seen.
3392       ;; We rename the Message-ID.
3393       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3394            (list header))
3395       (mail-header-set-id header id))
3396
3397      ;; The last case ignores an existing entry, except it adds any
3398      ;; additional Xrefs (in case the two articles came from different
3399      ;; servers.
3400      ;; Also sets `header' to `nil' meaning that the `dependencies'
3401      ;; table was *not* modified.
3402      (t
3403       (mail-header-set-xref
3404        (car (symbol-value id-dep))
3405        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3406                    "")
3407                (or (mail-header-xref header) "")))
3408       (setq header nil)))
3409
3410     (when header
3411       ;; First check if that we are not creating a References loop.
3412       (setq ref (gnus-parent-id (mail-header-references header)))
3413       (while (and ref
3414                   (setq ref-dep (intern-soft ref dependencies))
3415                   (boundp ref-dep)
3416                   (setq ref-header (car (symbol-value ref-dep))))
3417         (if (string= id ref)
3418             ;; Yuk!  This is a reference loop.  Make the article be a
3419             ;; root article.
3420             (progn
3421               (mail-header-set-references (car (symbol-value id-dep)) "none")
3422               (setq ref nil))
3423           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3424       (setq ref (gnus-parent-id (mail-header-references header)))
3425       (setq ref-dep (intern (or ref "none") dependencies))
3426       (if (boundp ref-dep)
3427           (setcdr (symbol-value ref-dep)
3428                   (nconc (cdr (symbol-value ref-dep))
3429                          (list (symbol-value id-dep))))
3430         (set ref-dep (list nil (symbol-value id-dep)))))
3431     header))
3432
3433 (defun gnus-build-sparse-threads ()
3434   (let ((headers gnus-newsgroup-headers)
3435         (mail-parse-charset gnus-newsgroup-charset)
3436         (gnus-summary-ignore-duplicates t)
3437         header references generation relations
3438         subject child end new-child date)
3439     ;; First we create an alist of generations/relations, where
3440     ;; generations is how much we trust the relation, and the relation
3441     ;; is parent/child.
3442     (gnus-message 7 "Making sparse threads...")
3443     (save-excursion
3444       (nnheader-set-temp-buffer " *gnus sparse threads*")
3445       (while (setq header (pop headers))
3446         (when (and (setq references (mail-header-references header))
3447                    (not (string= references "")))
3448           (insert references)
3449           (setq child (mail-header-id header)
3450                 subject (mail-header-subject header)
3451                 date (mail-header-date header)
3452                 generation 0)
3453           (while (search-backward ">" nil t)
3454             (setq end (1+ (point)))
3455             (when (search-backward "<" nil t)
3456               (setq new-child (buffer-substring (point) end))
3457               (push (list (incf generation)
3458                           child (setq child new-child)
3459                           subject date)
3460                     relations)))
3461           (when child
3462             (push (list (1+ generation) child nil subject) relations))
3463           (erase-buffer)))
3464       (kill-buffer (current-buffer)))
3465     ;; Sort over trustworthiness.
3466     (mapcar
3467      (lambda (relation)
3468        (when (gnus-dependencies-add-header
3469               (make-full-mail-header
3470                gnus-reffed-article-number
3471                (nth 3 relation) "" (or (nth 4 relation) "")
3472                (nth 1 relation)
3473                (or (nth 2 relation) "") 0 0 "")
3474               gnus-newsgroup-dependencies nil)
3475          (push gnus-reffed-article-number gnus-newsgroup-limit)
3476          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3477          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3478                gnus-newsgroup-reads)
3479          (decf gnus-reffed-article-number)))
3480      (sort relations 'car-less-than-car))
3481     (gnus-message 7 "Making sparse threads...done")))
3482
3483 (defun gnus-build-old-threads ()
3484   ;; Look at all the articles that refer back to old articles, and
3485   ;; fetch the headers for the articles that aren't there.  This will
3486   ;; build complete threads - if the roots haven't been expired by the
3487   ;; server, that is.
3488   (let ((mail-parse-charset gnus-newsgroup-charset)
3489         id heads)
3490     (mapatoms
3491      (lambda (refs)
3492        (when (not (car (symbol-value refs)))
3493          (setq heads (cdr (symbol-value refs)))
3494          (while heads
3495            (if (memq (mail-header-number (caar heads))
3496                      gnus-newsgroup-dormant)
3497                (setq heads (cdr heads))
3498              (setq id (symbol-name refs))
3499              (while (and (setq id (gnus-build-get-header id))
3500                          (not (car (gnus-id-to-thread id)))))
3501              (setq heads nil)))))
3502      gnus-newsgroup-dependencies)))
3503
3504 ;; This function has to be called with point after the article number
3505 ;; on the beginning of the line.
3506 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3507   (let ((eol (gnus-point-at-eol))
3508         (buffer (current-buffer))
3509         header)
3510
3511     ;; overview: [num subject from date id refs chars lines misc]
3512     (unwind-protect
3513         (progn
3514           (narrow-to-region (point) eol)
3515           (unless (eobp)
3516             (forward-char))
3517
3518           (setq header
3519                 (make-full-mail-header
3520                  number                 ; number
3521                  (funcall gnus-decode-encoded-word-function
3522                           (nnheader-nov-field)) ; subject
3523                  (funcall gnus-decode-encoded-word-function
3524                           (nnheader-nov-field)) ; from
3525                  (nnheader-nov-field)   ; date
3526                  (nnheader-nov-read-message-id) ; id
3527                  (nnheader-nov-field)   ; refs
3528                  (nnheader-nov-read-integer) ; chars
3529                  (nnheader-nov-read-integer) ; lines
3530                  (unless (eobp)
3531                    (if (looking-at "Xref: ")
3532                        (goto-char (match-end 0)))
3533                    (nnheader-nov-field)) ; Xref
3534                  (nnheader-nov-parse-extra)))) ; extra
3535
3536       (widen))
3537
3538     (when gnus-alter-header-function
3539       (funcall gnus-alter-header-function header))
3540     (gnus-dependencies-add-header header dependencies force-new)))
3541
3542 (defun gnus-build-get-header (id)
3543   "Look through the buffer of NOV lines and find the header to ID.
3544 Enter this line into the dependencies hash table, and return
3545 the id of the parent article (if any)."
3546   (let ((deps gnus-newsgroup-dependencies)
3547         found header)
3548     (prog1
3549         (save-excursion
3550           (set-buffer nntp-server-buffer)
3551           (let ((case-fold-search nil))
3552             (goto-char (point-min))
3553             (while (and (not found)
3554                         (search-forward id nil t))
3555               (beginning-of-line)
3556               (setq found (looking-at
3557                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3558                                    (regexp-quote id))))
3559               (or found (beginning-of-line 2)))
3560             (when found
3561               (beginning-of-line)
3562               (and
3563                (setq header (gnus-nov-parse-line
3564                              (read (current-buffer)) deps))
3565                (gnus-parent-id (mail-header-references header))))))
3566       (when header
3567         (let ((number (mail-header-number header)))
3568           (push number gnus-newsgroup-limit)
3569           (push header gnus-newsgroup-headers)
3570           (if (memq number gnus-newsgroup-unselected)
3571               (progn
3572                 (push number gnus-newsgroup-unreads)
3573                 (setq gnus-newsgroup-unselected
3574                       (delq number gnus-newsgroup-unselected)))
3575             (push number gnus-newsgroup-ancient)))))))
3576
3577 (defun gnus-build-all-threads ()
3578   "Read all the headers."
3579   (let ((gnus-summary-ignore-duplicates t)
3580         (mail-parse-charset gnus-newsgroup-charset)
3581         (dependencies gnus-newsgroup-dependencies)
3582         header article)
3583     (save-excursion
3584       (set-buffer nntp-server-buffer)
3585       (let ((case-fold-search nil))
3586         (goto-char (point-min))
3587         (while (not (eobp))
3588           (ignore-errors
3589             (setq article (read (current-buffer))
3590                   header (gnus-nov-parse-line article dependencies)))
3591           (when header
3592             (save-excursion
3593               (set-buffer gnus-summary-buffer)
3594               (push header gnus-newsgroup-headers)
3595               (if (memq (setq article (mail-header-number header))
3596                         gnus-newsgroup-unselected)
3597                   (progn
3598                     (push article gnus-newsgroup-unreads)
3599                     (setq gnus-newsgroup-unselected
3600                           (delq article gnus-newsgroup-unselected)))
3601                 (push article gnus-newsgroup-ancient)))
3602             (forward-line 1)))))))
3603
3604 (defun gnus-summary-update-article-line (article header)
3605   "Update the line for ARTICLE using HEADERS."
3606   (let* ((id (mail-header-id header))
3607          (thread (gnus-id-to-thread id)))
3608     (unless thread
3609       (error "Article in no thread"))
3610     ;; Update the thread.
3611     (setcar thread header)
3612     (gnus-summary-goto-subject article)
3613     (let* ((datal (gnus-data-find-list article))
3614            (data (car datal))
3615            (length (when (cdr datal)
3616                      (- (gnus-data-pos data)
3617                         (gnus-data-pos (cadr datal)))))
3618            (buffer-read-only nil)
3619            (level (gnus-summary-thread-level)))
3620       (gnus-delete-line)
3621       (gnus-summary-insert-line
3622        header level nil (gnus-article-mark article)
3623        (memq article gnus-newsgroup-replied)
3624        (memq article gnus-newsgroup-expirable)
3625        ;; Only insert the Subject string when it's different
3626        ;; from the previous Subject string.
3627        (if (and
3628             gnus-show-threads
3629             (gnus-subject-equal
3630              (condition-case ()
3631                  (mail-header-subject
3632                   (gnus-data-header
3633                    (cadr
3634                     (gnus-data-find-list
3635                      article
3636                      (gnus-data-list t)))))
3637                ;; Error on the side of excessive subjects.
3638                (error ""))
3639              (mail-header-subject header)))
3640            ""
3641          (mail-header-subject header))
3642        nil (cdr (assq article gnus-newsgroup-scored))
3643        (memq article gnus-newsgroup-processable))
3644       (when length
3645         (gnus-data-update-list
3646          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3647
3648 (defun gnus-summary-update-article (article &optional iheader)
3649   "Update ARTICLE in the summary buffer."
3650   (set-buffer gnus-summary-buffer)
3651   (let* ((header (gnus-summary-article-header article))
3652          (id (mail-header-id header))
3653          (data (gnus-data-find article))
3654          (thread (gnus-id-to-thread id))
3655          (references (mail-header-references header))
3656          (parent
3657           (gnus-id-to-thread
3658            (or (gnus-parent-id
3659                 (when (and references
3660                            (not (equal "" references)))
3661                   references))
3662                "none")))
3663          (buffer-read-only nil)
3664          (old (car thread)))
3665     (when thread
3666       (unless iheader
3667         (setcar thread nil)
3668         (when parent
3669           (delq thread parent)))
3670       (if (gnus-summary-insert-subject id header)
3671           ;; Set the (possibly) new article number in the data structure.
3672           (gnus-data-set-number data (gnus-id-to-article id))
3673         (setcar thread old)
3674         nil))))
3675
3676 (defun gnus-rebuild-thread (id &optional line)
3677   "Rebuild the thread containing ID.
3678 If LINE, insert the rebuilt thread starting on line LINE."
3679   (let ((buffer-read-only nil)
3680         old-pos current thread data)
3681     (if (not gnus-show-threads)
3682         (setq thread (list (car (gnus-id-to-thread id))))
3683       ;; Get the thread this article is part of.
3684       (setq thread (gnus-remove-thread id)))
3685     (setq old-pos (gnus-point-at-bol))
3686     (setq current (save-excursion
3687                     (and (zerop (forward-line -1))
3688                          (gnus-summary-article-number))))
3689     ;; If this is a gathered thread, we have to go some re-gathering.
3690     (when (stringp (car thread))
3691       (let ((subject (car thread))
3692             roots thr)
3693         (setq thread (cdr thread))
3694         (while thread
3695           (unless (memq (setq thr (gnus-id-to-thread
3696                                    (gnus-root-id
3697                                     (mail-header-id (caar thread)))))
3698                         roots)
3699             (push thr roots))
3700           (setq thread (cdr thread)))
3701         ;; We now have all (unique) roots.
3702         (if (= (length roots) 1)
3703             ;; All the loose roots are now one solid root.
3704             (setq thread (car roots))
3705           (setq thread (cons subject (gnus-sort-threads roots))))))
3706     (let (threads)
3707       ;; We then insert this thread into the summary buffer.
3708       (when line
3709         (goto-char (point-min))
3710         (forward-line (1- line)))
3711       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3712         (if gnus-show-threads
3713             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3714           (gnus-summary-prepare-unthreaded thread))
3715         (setq data (nreverse gnus-newsgroup-data))
3716         (setq threads gnus-newsgroup-threads))
3717       ;; We splice the new data into the data structure.
3718       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3719       ;;!!! then we want to insert at the beginning of the buffer.
3720       ;;!!! That happens to be true with Gnus now, but that may
3721       ;;!!! change in the future.  Perhaps.
3722       (gnus-data-enter-list
3723        (if line nil current) data (- (point) old-pos))
3724       (setq gnus-newsgroup-threads
3725             (nconc threads gnus-newsgroup-threads))
3726       (gnus-data-compute-positions))))
3727
3728 (defun gnus-number-to-header (number)
3729   "Return the header for article NUMBER."
3730   (let ((headers gnus-newsgroup-headers))
3731     (while (and headers
3732                 (not (= number (mail-header-number (car headers)))))
3733       (pop headers))
3734     (when headers
3735       (car headers))))
3736
3737 (defun gnus-parent-headers (in-headers &optional generation)
3738   "Return the headers of the GENERATIONeth parent of HEADERS."
3739   (unless generation
3740     (setq generation 1))
3741   (let ((parent t)
3742         (headers in-headers)
3743         references)
3744     (while (and parent
3745                 (not (zerop generation))
3746                 (setq references (mail-header-references headers)))
3747       (setq headers (if (and references
3748                              (setq parent (gnus-parent-id references)))
3749                         (car (gnus-id-to-thread parent))
3750                       nil))
3751       (decf generation))
3752     (and (not (eq headers in-headers))
3753          headers)))
3754
3755 (defun gnus-id-to-thread (id)
3756   "Return the (sub-)thread where ID appears."
3757   (gnus-gethash id gnus-newsgroup-dependencies))
3758
3759 (defun gnus-id-to-article (id)
3760   "Return the article number of ID."
3761   (let ((thread (gnus-id-to-thread id)))
3762     (when (and thread
3763                (car thread))
3764       (mail-header-number (car thread)))))
3765
3766 (defun gnus-id-to-header (id)
3767   "Return the article headers of ID."
3768   (car (gnus-id-to-thread id)))
3769
3770 (defun gnus-article-displayed-root-p (article)
3771   "Say whether ARTICLE is a root(ish) article."
3772   (let ((level (gnus-summary-thread-level article))
3773         (refs (mail-header-references  (gnus-summary-article-header article)))
3774         particle)
3775     (cond
3776      ((null level) nil)
3777      ((zerop level) t)
3778      ((null refs) t)
3779      ((null (gnus-parent-id refs)) t)
3780      ((and (= 1 level)
3781            (null (setq particle (gnus-id-to-article
3782                                  (gnus-parent-id refs))))
3783            (null (gnus-summary-thread-level particle)))))))
3784
3785 (defun gnus-root-id (id)
3786   "Return the id of the root of the thread where ID appears."
3787   (let (last-id prev)
3788     (while (and id (setq prev (car (gnus-id-to-thread id))))
3789       (setq last-id id
3790             id (gnus-parent-id (mail-header-references prev))))
3791     last-id))
3792
3793 (defun gnus-articles-in-thread (thread)
3794   "Return the list of articles in THREAD."
3795   (cons (mail-header-number (car thread))
3796         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3797
3798 (defun gnus-remove-thread (id &optional dont-remove)
3799   "Remove the thread that has ID in it."
3800   (let (headers thread last-id)
3801     ;; First go up in this thread until we find the root.
3802     (setq last-id (gnus-root-id id)
3803           headers (message-flatten-list (gnus-id-to-thread last-id)))
3804     ;; We have now found the real root of this thread.  It might have
3805     ;; been gathered into some loose thread, so we have to search
3806     ;; through the threads to find the thread we wanted.
3807     (let ((threads gnus-newsgroup-threads)
3808           sub)
3809       (while threads
3810         (setq sub (car threads))
3811         (if (stringp (car sub))
3812             ;; This is a gathered thread, so we look at the roots
3813             ;; below it to find whether this article is in this
3814             ;; gathered root.
3815             (progn
3816               (setq sub (cdr sub))
3817               (while sub
3818                 (when (member (caar sub) headers)
3819                   (setq thread (car threads)
3820                         threads nil
3821                         sub nil))
3822                 (setq sub (cdr sub))))
3823           ;; It's an ordinary thread, so we check it.
3824           (when (eq (car sub) (car headers))
3825             (setq thread sub
3826                   threads nil)))
3827         (setq threads (cdr threads)))
3828       ;; If this article is in no thread, then it's a root.
3829       (if thread
3830           (unless dont-remove
3831             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3832         (setq thread (gnus-id-to-thread last-id)))
3833       (when thread
3834         (prog1
3835             thread                      ; We return this thread.
3836           (unless dont-remove
3837             (if (stringp (car thread))
3838                 (progn
3839                   ;; If we use dummy roots, then we have to remove the
3840                   ;; dummy root as well.
3841                   (when (eq gnus-summary-make-false-root 'dummy)
3842                     ;; We go to the dummy root by going to
3843                     ;; the first sub-"thread", and then one line up.
3844                     (gnus-summary-goto-article
3845                      (mail-header-number (caadr thread)))
3846                     (forward-line -1)
3847                     (gnus-delete-line)
3848                     (gnus-data-compute-positions))
3849                   (setq thread (cdr thread))
3850                   (while thread
3851                     (gnus-remove-thread-1 (car thread))
3852                     (setq thread (cdr thread))))
3853               (gnus-remove-thread-1 thread))))))))
3854
3855 (defun gnus-remove-thread-1 (thread)
3856   "Remove the thread THREAD recursively."
3857   (let ((number (mail-header-number (pop thread)))
3858         d)
3859     (setq thread (reverse thread))
3860     (while thread
3861       (gnus-remove-thread-1 (pop thread)))
3862     (when (setq d (gnus-data-find number))
3863       (goto-char (gnus-data-pos d))
3864       (gnus-summary-show-thread)
3865       (gnus-data-remove
3866        number
3867        (- (gnus-point-at-bol)
3868           (prog1
3869               (1+ (gnus-point-at-eol))
3870             (gnus-delete-line)))))))
3871
3872 (defun gnus-sort-threads-1 (threads func)
3873   (sort (mapcar (lambda (thread)
3874                   (cons (car thread)
3875                         (and (cdr thread)
3876                              (gnus-sort-threads-1 (cdr thread) func))))
3877                 threads) func))
3878
3879 (defun gnus-sort-threads (threads)
3880   "Sort THREADS."
3881   (if (not gnus-thread-sort-functions)
3882       threads
3883     (gnus-message 8 "Sorting threads...")
3884     (prog1
3885         (gnus-sort-threads-1
3886          threads
3887          (gnus-make-sort-function gnus-thread-sort-functions))
3888       (gnus-message 8 "Sorting threads...done"))))
3889
3890 (defun gnus-sort-articles (articles)
3891   "Sort ARTICLES."
3892   (when gnus-article-sort-functions
3893     (gnus-message 7 "Sorting articles...")
3894     (prog1
3895         (setq gnus-newsgroup-headers
3896               (sort articles (gnus-make-sort-function
3897                               gnus-article-sort-functions)))
3898       (gnus-message 7 "Sorting articles...done"))))
3899
3900 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3901 (defmacro gnus-thread-header (thread)
3902   "Return header of first article in THREAD.
3903 Note that THREAD must never, ever be anything else than a variable -
3904 using some other form will lead to serious barfage."
3905   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3906   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3907   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3908         (vector thread) 2))
3909
3910 (defsubst gnus-article-sort-by-number (h1 h2)
3911   "Sort articles by article number."
3912   (< (mail-header-number h1)
3913      (mail-header-number h2)))
3914
3915 (defun gnus-thread-sort-by-number (h1 h2)
3916   "Sort threads by root article number."
3917   (gnus-article-sort-by-number
3918    (gnus-thread-header h1) (gnus-thread-header h2)))
3919
3920 (defsubst gnus-article-sort-by-lines (h1 h2)
3921   "Sort articles by article Lines header."
3922   (< (mail-header-lines h1)
3923      (mail-header-lines h2)))
3924
3925 (defun gnus-thread-sort-by-lines (h1 h2)
3926   "Sort threads by root article Lines header."
3927   (gnus-article-sort-by-lines
3928    (gnus-thread-header h1) (gnus-thread-header h2)))
3929
3930 (defsubst gnus-article-sort-by-chars (h1 h2)
3931   "Sort articles by octet length."
3932   (< (mail-header-chars h1)
3933      (mail-header-chars h2)))
3934
3935 (defun gnus-thread-sort-by-chars (h1 h2)
3936   "Sort threads by root article octet length."
3937   (gnus-article-sort-by-chars
3938    (gnus-thread-header h1) (gnus-thread-header h2)))
3939
3940 (defsubst gnus-article-sort-by-author (h1 h2)
3941   "Sort articles by root author."
3942   (string-lessp
3943    (let ((extract (funcall
3944                    gnus-extract-address-components
3945                    (mail-header-from h1))))
3946      (or (car extract) (cadr extract) ""))
3947    (let ((extract (funcall
3948                    gnus-extract-address-components
3949                    (mail-header-from h2))))
3950      (or (car extract) (cadr extract) ""))))
3951
3952 (defun gnus-thread-sort-by-author (h1 h2)
3953   "Sort threads by root author."
3954   (gnus-article-sort-by-author
3955    (gnus-thread-header h1)  (gnus-thread-header h2)))
3956
3957 (defsubst gnus-article-sort-by-subject (h1 h2)
3958   "Sort articles by root subject."
3959   (string-lessp
3960    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3961    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3962
3963 (defun gnus-thread-sort-by-subject (h1 h2)
3964   "Sort threads by root subject."
3965   (gnus-article-sort-by-subject
3966    (gnus-thread-header h1) (gnus-thread-header h2)))
3967
3968 (defsubst gnus-article-sort-by-date (h1 h2)
3969   "Sort articles by root article date."
3970   (time-less-p
3971    (gnus-date-get-time (mail-header-date h1))
3972    (gnus-date-get-time (mail-header-date h2))))
3973
3974 (defun gnus-thread-sort-by-date (h1 h2)
3975   "Sort threads by root article date."
3976   (gnus-article-sort-by-date
3977    (gnus-thread-header h1) (gnus-thread-header h2)))
3978
3979 (defsubst gnus-article-sort-by-score (h1 h2)
3980   "Sort articles by root article score.
3981 Unscored articles will be counted as having a score of zero."
3982   (> (or (cdr (assq (mail-header-number h1)
3983                     gnus-newsgroup-scored))
3984          gnus-summary-default-score 0)
3985      (or (cdr (assq (mail-header-number h2)
3986                     gnus-newsgroup-scored))
3987          gnus-summary-default-score 0)))
3988
3989 (defun gnus-thread-sort-by-score (h1 h2)
3990   "Sort threads by root article score."
3991   (gnus-article-sort-by-score
3992    (gnus-thread-header h1) (gnus-thread-header h2)))
3993
3994 (defun gnus-thread-sort-by-total-score (h1 h2)
3995   "Sort threads by the sum of all scores in the thread.
3996 Unscored articles will be counted as having a score of zero."
3997   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3998
3999 (defun gnus-thread-total-score (thread)
4000   ;; This function find the total score of THREAD.
4001   (cond ((null thread)
4002          0)
4003         ((consp thread)
4004          (if (stringp (car thread))
4005              (apply gnus-thread-score-function 0
4006                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4007            (gnus-thread-total-score-1 thread)))
4008         (t
4009          (gnus-thread-total-score-1 (list thread)))))
4010
4011 (defun gnus-thread-total-score-1 (root)
4012   ;; This function find the total score of the thread below ROOT.
4013   (setq root (car root))
4014   (apply gnus-thread-score-function
4015          (or (append
4016               (mapcar 'gnus-thread-total-score
4017                       (cdr (gnus-id-to-thread (mail-header-id root))))
4018               (when (> (mail-header-number root) 0)
4019                 (list (or (cdr (assq (mail-header-number root)
4020                                      gnus-newsgroup-scored))
4021                           gnus-summary-default-score 0))))
4022              (list gnus-summary-default-score)
4023              '(0))))
4024
4025 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4026 (defvar gnus-tmp-prev-subject nil)
4027 (defvar gnus-tmp-false-parent nil)
4028 (defvar gnus-tmp-root-expunged nil)
4029 (defvar gnus-tmp-dummy-line nil)
4030
4031 (eval-when-compile (defvar gnus-tmp-header))
4032 (defun gnus-extra-header (type &optional header)
4033   "Return the extra header of TYPE."
4034   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4035       ""))
4036
4037 (defun gnus-summary-prepare-threads (threads)
4038   "Prepare summary buffer from THREADS and indentation LEVEL.
4039 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4040 or a straight list of headers."
4041   (gnus-message 7 "Generating summary...")
4042
4043   (setq gnus-newsgroup-threads threads)
4044   (beginning-of-line)
4045
4046   (let ((gnus-tmp-level 0)
4047         (default-score (or gnus-summary-default-score 0))
4048         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4049         thread number subject stack state gnus-tmp-gathered beg-match
4050         new-roots gnus-tmp-new-adopts thread-end
4051         gnus-tmp-header gnus-tmp-unread
4052         gnus-tmp-replied gnus-tmp-subject-or-nil
4053         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4054         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4055         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4056
4057     (setq gnus-tmp-prev-subject nil)
4058
4059     (if (vectorp (car threads))
4060         ;; If this is a straight (sic) list of headers, then a
4061         ;; threaded summary display isn't required, so we just create
4062         ;; an unthreaded one.
4063         (gnus-summary-prepare-unthreaded threads)
4064
4065       ;; Do the threaded display.
4066
4067       (while (or threads stack gnus-tmp-new-adopts new-roots)
4068
4069         (if (and (= gnus-tmp-level 0)
4070                  (or (not stack)
4071                      (= (caar stack) 0))
4072                  (not gnus-tmp-false-parent)
4073                  (or gnus-tmp-new-adopts new-roots))
4074             (if gnus-tmp-new-adopts
4075                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4076                       thread (list (car gnus-tmp-new-adopts))
4077                       gnus-tmp-header (caar thread)
4078                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4079               (when new-roots
4080                 (setq thread (list (car new-roots))
4081                       gnus-tmp-header (caar thread)
4082                       new-roots (cdr new-roots))))
4083
4084           (if threads
4085               ;; If there are some threads, we do them before the
4086               ;; threads on the stack.
4087               (setq thread threads
4088                     gnus-tmp-header (caar thread))
4089             ;; There were no current threads, so we pop something off
4090             ;; the stack.
4091             (setq state (car stack)
4092                   gnus-tmp-level (car state)
4093                   thread (cdr state)
4094                   stack (cdr stack)
4095                   gnus-tmp-header (caar thread))))
4096
4097         (setq gnus-tmp-false-parent nil)
4098         (setq gnus-tmp-root-expunged nil)
4099         (setq thread-end nil)
4100
4101         (if (stringp gnus-tmp-header)
4102             ;; The header is a dummy root.
4103             (cond
4104              ((eq gnus-summary-make-false-root 'adopt)
4105               ;; We let the first article adopt the rest.
4106               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4107                                                (cddar thread)))
4108               (setq gnus-tmp-gathered
4109                     (nconc (mapcar
4110                             (lambda (h) (mail-header-number (car h)))
4111                             (cddar thread))
4112                            gnus-tmp-gathered))
4113               (setq thread (cons (list (caar thread)
4114                                        (cadar thread))
4115                                  (cdr thread)))
4116               (setq gnus-tmp-level -1
4117                     gnus-tmp-false-parent t))
4118              ((eq gnus-summary-make-false-root 'empty)
4119               ;; We print adopted articles with empty subject fields.
4120               (setq gnus-tmp-gathered
4121                     (nconc (mapcar
4122                             (lambda (h) (mail-header-number (car h)))
4123                             (cddar thread))
4124                            gnus-tmp-gathered))
4125               (setq gnus-tmp-level -1))
4126              ((eq gnus-summary-make-false-root 'dummy)
4127               ;; We remember that we probably want to output a dummy
4128               ;; root.
4129               (setq gnus-tmp-dummy-line gnus-tmp-header)
4130               (setq gnus-tmp-prev-subject gnus-tmp-header))
4131              (t
4132               ;; We do not make a root for the gathered
4133               ;; sub-threads at all.
4134               (setq gnus-tmp-level -1)))
4135
4136           (setq number (mail-header-number gnus-tmp-header)
4137                 subject (mail-header-subject gnus-tmp-header))
4138
4139           (cond
4140            ;; If the thread has changed subject, we might want to make
4141            ;; this subthread into a root.
4142            ((and (null gnus-thread-ignore-subject)
4143                  (not (zerop gnus-tmp-level))
4144                  gnus-tmp-prev-subject
4145                  (not (inline
4146                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4147             (setq new-roots (nconc new-roots (list (car thread)))
4148                   thread-end t
4149                   gnus-tmp-header nil))
4150            ;; If the article lies outside the current limit,
4151            ;; then we do not display it.
4152            ((not (memq number gnus-newsgroup-limit))
4153             (setq gnus-tmp-gathered
4154                   (nconc (mapcar
4155                           (lambda (h) (mail-header-number (car h)))
4156                           (cdar thread))
4157                          gnus-tmp-gathered))
4158             (setq gnus-tmp-new-adopts (if (cdar thread)
4159                                           (append gnus-tmp-new-adopts
4160                                                   (cdar thread))
4161                                         gnus-tmp-new-adopts)
4162                   thread-end t
4163                   gnus-tmp-header nil)
4164             (when (zerop gnus-tmp-level)
4165               (setq gnus-tmp-root-expunged t)))
4166            ;; Perhaps this article is to be marked as read?
4167            ((and gnus-summary-mark-below
4168                  (< (or (cdr (assq number gnus-newsgroup-scored))
4169                         default-score)
4170                     gnus-summary-mark-below)
4171                  ;; Don't touch sparse articles.
4172                  (not (gnus-summary-article-sparse-p number))
4173                  (not (gnus-summary-article-ancient-p number)))
4174             (setq gnus-newsgroup-unreads
4175                   (delq number gnus-newsgroup-unreads))
4176             (if gnus-newsgroup-auto-expire
4177                 (push number gnus-newsgroup-expirable)
4178               (push (cons number gnus-low-score-mark)
4179                     gnus-newsgroup-reads))))
4180
4181           (when gnus-tmp-header
4182             ;; We may have an old dummy line to output before this
4183             ;; article.
4184             (when (and gnus-tmp-dummy-line
4185                        (gnus-subject-equal
4186                         gnus-tmp-dummy-line
4187                         (mail-header-subject gnus-tmp-header)))
4188               (gnus-summary-insert-dummy-line
4189                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4190               (setq gnus-tmp-dummy-line nil))
4191
4192             ;; Compute the mark.
4193             (setq gnus-tmp-unread (gnus-article-mark number))
4194
4195             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4196                                   gnus-tmp-header gnus-tmp-level)
4197                   gnus-newsgroup-data)
4198
4199             ;; Actually insert the line.
4200             (setq
4201              gnus-tmp-subject-or-nil
4202              (cond
4203               ((and gnus-thread-ignore-subject
4204                     gnus-tmp-prev-subject
4205                     (not (inline (gnus-subject-equal
4206                                   gnus-tmp-prev-subject subject))))
4207                subject)
4208               ((zerop gnus-tmp-level)
4209                (if (and (eq gnus-summary-make-false-root 'empty)
4210                         (memq number gnus-tmp-gathered)
4211                         gnus-tmp-prev-subject
4212                         (inline (gnus-subject-equal
4213                                  gnus-tmp-prev-subject subject)))
4214                    gnus-summary-same-subject
4215                  subject))
4216               (t gnus-summary-same-subject)))
4217             (if (and (eq gnus-summary-make-false-root 'adopt)
4218                      (= gnus-tmp-level 1)
4219                      (memq number gnus-tmp-gathered))
4220                 (setq gnus-tmp-opening-bracket ?\<
4221                       gnus-tmp-closing-bracket ?\>)
4222               (setq gnus-tmp-opening-bracket ?\[
4223                     gnus-tmp-closing-bracket ?\]))
4224             (setq
4225              gnus-tmp-indentation
4226              (aref gnus-thread-indent-array gnus-tmp-level)
4227              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4228              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4229                                 gnus-summary-default-score 0)
4230              gnus-tmp-score-char
4231              (if (or (null gnus-summary-default-score)
4232                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4233                          gnus-summary-zcore-fuzz))
4234                  ?  ;Whitespace
4235                (if (< gnus-tmp-score gnus-summary-default-score)
4236                    gnus-score-below-mark gnus-score-over-mark))
4237              gnus-tmp-replied
4238              (cond ((memq number gnus-newsgroup-processable)
4239                     gnus-process-mark)
4240                    ((memq number gnus-newsgroup-cached)
4241                     gnus-cached-mark)
4242                    ((memq number gnus-newsgroup-replied)
4243                     gnus-replied-mark)
4244                    ((memq number gnus-newsgroup-forwarded)
4245                     gnus-forwarded-mark)
4246                    ((memq number gnus-newsgroup-saved)
4247                     gnus-saved-mark)
4248                    (t gnus-no-mark))
4249              gnus-tmp-from (mail-header-from gnus-tmp-header)
4250              gnus-tmp-name
4251              (cond
4252               ((string-match "<[^>]+> *$" gnus-tmp-from)
4253                (setq beg-match (match-beginning 0))
4254                (or (and (string-match "^\".+\"" gnus-tmp-from)
4255                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4256                    (substring gnus-tmp-from 0 beg-match)))
4257               ((string-match "(.+)" gnus-tmp-from)
4258                (substring gnus-tmp-from
4259                           (1+ (match-beginning 0)) (1- (match-end 0))))
4260               (t gnus-tmp-from)))
4261             (when (string= gnus-tmp-name "")
4262               (setq gnus-tmp-name gnus-tmp-from))
4263             (unless (numberp gnus-tmp-lines)
4264               (setq gnus-tmp-lines -1))
4265             (when (= gnus-tmp-lines -1)
4266               (setq gnus-tmp-lines "?"))
4267             (gnus-put-text-property
4268              (point)
4269              (progn (eval gnus-summary-line-format-spec) (point))
4270              'gnus-number number)
4271             (when gnus-visual-p
4272               (forward-line -1)
4273               (gnus-run-hooks 'gnus-summary-update-hook)
4274               (forward-line 1))
4275
4276             (setq gnus-tmp-prev-subject subject)))
4277
4278         (when (nth 1 thread)
4279           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4280         (incf gnus-tmp-level)
4281         (setq threads (if thread-end nil (cdar thread)))
4282         (unless threads
4283           (setq gnus-tmp-level 0)))))
4284   (gnus-message 7 "Generating summary...done"))
4285
4286 (defun gnus-summary-prepare-unthreaded (headers)
4287   "Generate an unthreaded summary buffer based on HEADERS."
4288   (let (header number mark)
4289
4290     (beginning-of-line)
4291
4292     (while headers
4293       ;; We may have to root out some bad articles...
4294       (when (memq (setq number (mail-header-number
4295                                 (setq header (pop headers))))
4296                   gnus-newsgroup-limit)
4297         ;; Mark article as read when it has a low score.
4298         (when (and gnus-summary-mark-below
4299                    (< (or (cdr (assq number gnus-newsgroup-scored))
4300                           gnus-summary-default-score 0)
4301                       gnus-summary-mark-below)
4302                    (not (gnus-summary-article-ancient-p number)))
4303           (setq gnus-newsgroup-unreads
4304                 (delq number gnus-newsgroup-unreads))
4305           (if gnus-newsgroup-auto-expire
4306               (push number gnus-newsgroup-expirable)
4307             (push (cons number gnus-low-score-mark)
4308                   gnus-newsgroup-reads)))
4309
4310         (setq mark (gnus-article-mark number))
4311         (push (gnus-data-make number mark (1+ (point)) header 0)
4312               gnus-newsgroup-data)
4313         (gnus-summary-insert-line
4314          header 0 number
4315          mark (memq number gnus-newsgroup-replied)
4316          (memq number gnus-newsgroup-expirable)
4317          (mail-header-subject header) nil
4318          (cdr (assq number gnus-newsgroup-scored))
4319          (memq number gnus-newsgroup-processable))))))
4320
4321 (defun gnus-summary-remove-list-identifiers ()
4322   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4323   (let ((regexp (if (consp gnus-list-identifiers)
4324                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4325                   gnus-list-identifiers))
4326         changed subject)
4327     (when regexp
4328       (dolist (header gnus-newsgroup-headers)
4329         (setq subject (mail-header-subject header)
4330               changed nil)
4331         (while (string-match
4332                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4333                 subject)
4334           (setq subject
4335                 (concat (substring subject 0 (match-beginning 2))
4336                         (substring subject (match-end 0)))
4337                 changed t))
4338         (when (and changed
4339                    (string-match
4340                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4341           (setq subject
4342                 (concat (substring subject 0 (match-beginning 1))
4343                         (substring subject (match-end 1)))))
4344         (when changed
4345           (mail-header-set-subject header subject))))))
4346
4347 (defun gnus-fetch-headers (articles)
4348   "Fetch headers of ARTICLES."
4349   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4350     (gnus-message 5 "Fetching headers for %s..." name)
4351     (prog1
4352         (if (eq 'nov
4353                 (setq gnus-headers-retrieved-by
4354                       (gnus-retrieve-headers
4355                        articles gnus-newsgroup-name
4356                        ;; We might want to fetch old headers, but
4357                        ;; not if there is only 1 article.
4358                        (and (or (and
4359                                  (not (eq gnus-fetch-old-headers 'some))
4360                                  (not (numberp gnus-fetch-old-headers)))
4361                                 (> (length articles) 1))
4362                           gnus-fetch-old-headers))))
4363             (gnus-get-newsgroup-headers-xover
4364              articles nil nil gnus-newsgroup-name t)
4365           (gnus-get-newsgroup-headers))
4366       (gnus-message 5 "Fetching headers for %s...done" name))))
4367
4368 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4369   "Select newsgroup GROUP.
4370 If READ-ALL is non-nil, all articles in the group are selected.
4371 If SELECT-ARTICLES, only select those articles from GROUP."
4372   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4373          ;;!!! Dirty hack; should be removed.
4374          (gnus-summary-ignore-duplicates
4375           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4376               t
4377             gnus-summary-ignore-duplicates))
4378          (info (nth 2 entry))
4379          articles fetched-articles cached)
4380
4381     (unless (gnus-check-server
4382              (setq gnus-current-select-method
4383                    (gnus-find-method-for-group group)))
4384       (error "Couldn't open server"))
4385
4386     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4387         (gnus-activate-group group)     ; Or we can activate it...
4388         (progn                          ; Or we bug out.
4389           (when (equal major-mode 'gnus-summary-mode)
4390             (kill-buffer (current-buffer)))
4391           (error "Couldn't activate group %s: %s"
4392                  group (gnus-status-message group))))
4393
4394     (unless (gnus-request-group group t)
4395       (when (equal major-mode 'gnus-summary-mode)
4396         (kill-buffer (current-buffer)))
4397       (error "Couldn't request group %s: %s"
4398              group (gnus-status-message group)))
4399
4400     (setq gnus-newsgroup-name group)
4401     (setq gnus-newsgroup-unselected nil)
4402     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4403     (gnus-summary-setup-default-charset)
4404
4405     ;; Adjust and set lists of article marks.
4406     (when info
4407       (gnus-adjust-marked-articles info))
4408
4409     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4410     (when (gnus-virtual-group-p group)
4411       (setq cached gnus-newsgroup-cached))
4412
4413     (setq gnus-newsgroup-unreads
4414           (gnus-set-difference
4415            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4416            gnus-newsgroup-dormant))
4417
4418     (setq gnus-newsgroup-processable nil)
4419
4420     (gnus-update-read-articles group gnus-newsgroup-unreads)
4421
4422     (if (setq articles select-articles)
4423         (setq gnus-newsgroup-unselected
4424               (gnus-sorted-intersection
4425                gnus-newsgroup-unreads
4426                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4427       (setq articles (gnus-articles-to-read group read-all)))
4428
4429     (cond
4430      ((null articles)
4431       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4432       'quit)
4433      ((eq articles 0) nil)
4434      (t
4435       ;; Init the dependencies hash table.
4436       (setq gnus-newsgroup-dependencies
4437             (gnus-make-hashtable (length articles)))
4438       (gnus-set-global-variables)
4439       ;; Retrieve the headers and read them in.
4440       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4441
4442       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4443       (when cached
4444         (setq gnus-newsgroup-cached cached))
4445
4446       ;; Suppress duplicates?
4447       (when gnus-suppress-duplicates
4448         (gnus-dup-suppress-articles))
4449
4450       ;; Set the initial limit.
4451       (setq gnus-newsgroup-limit (copy-sequence articles))
4452       ;; Remove canceled articles from the list of unread articles.
4453       (setq gnus-newsgroup-unreads
4454             (gnus-set-sorted-intersection
4455              gnus-newsgroup-unreads
4456              (setq fetched-articles
4457                    (mapcar (lambda (headers) (mail-header-number headers))
4458                            gnus-newsgroup-headers))))
4459       ;; Removed marked articles that do not exist.
4460       (gnus-update-missing-marks
4461        (gnus-sorted-complement fetched-articles articles))
4462       ;; We might want to build some more threads first.
4463       (when (and gnus-fetch-old-headers
4464                  (eq gnus-headers-retrieved-by 'nov))
4465         (if (eq gnus-fetch-old-headers 'invisible)
4466             (gnus-build-all-threads)
4467           (gnus-build-old-threads)))
4468       ;; Let the Gnus agent mark articles as read.
4469       (when gnus-agent
4470         (gnus-agent-get-undownloaded-list))
4471       ;; Remove list identifiers from subject
4472       (when gnus-list-identifiers
4473         (gnus-summary-remove-list-identifiers))
4474       ;; Check whether auto-expire is to be done in this group.
4475       (setq gnus-newsgroup-auto-expire
4476             (gnus-group-auto-expirable-p group))
4477       ;; Set up the article buffer now, if necessary.
4478       (unless gnus-single-article-buffer
4479         (gnus-article-setup-buffer))
4480       ;; First and last article in this newsgroup.
4481       (when gnus-newsgroup-headers
4482         (setq gnus-newsgroup-begin
4483               (mail-header-number (car gnus-newsgroup-headers))
4484               gnus-newsgroup-end
4485               (mail-header-number
4486                (gnus-last-element gnus-newsgroup-headers))))
4487       ;; GROUP is successfully selected.
4488       (or gnus-newsgroup-headers t)))))
4489
4490 (defun gnus-articles-to-read (group &optional read-all)
4491   "Find out what articles the user wants to read."
4492   (let* ((articles
4493           ;; Select all articles if `read-all' is non-nil, or if there
4494           ;; are no unread articles.
4495           (if (or read-all
4496                   (and (zerop (length gnus-newsgroup-marked))
4497                        (zerop (length gnus-newsgroup-unreads)))
4498                   (eq (gnus-group-find-parameter group 'display)
4499                       'all))
4500               (or
4501                (gnus-uncompress-range (gnus-active group))
4502                (gnus-cache-articles-in-group group))
4503             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4504                           (copy-sequence gnus-newsgroup-unreads))
4505                   '<)))
4506          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4507          (scored (length scored-list))
4508          (number (length articles))
4509          (marked (+ (length gnus-newsgroup-marked)
4510                     (length gnus-newsgroup-dormant)))
4511          (select
4512           (cond
4513            ((numberp read-all)
4514             read-all)
4515            (t
4516             (condition-case ()
4517                 (cond
4518                  ((and (or (<= scored marked) (= scored number))
4519                        (numberp gnus-large-newsgroup)
4520                        (> number gnus-large-newsgroup))
4521                   (let ((input
4522                          (read-string
4523                           (format
4524                            "How many articles from %s (default %d): "
4525                            (gnus-limit-string gnus-newsgroup-name 35)
4526                            number))))
4527                     (if (string-match "^[ \t]*$" input) number input)))
4528                  ((and (> scored marked) (< scored number)
4529                        (> (- scored number) 20))
4530                   (let ((input
4531                          (read-string
4532                           (format "%s %s (%d scored, %d total): "
4533                                   "How many articles from"
4534                                   group scored number))))
4535                     (if (string-match "^[ \t]*$" input)
4536                         number input)))
4537                  (t number))
4538               (quit
4539                (message "Quit getting the articles to read")
4540                nil))))))
4541     (setq select (if (stringp select) (string-to-number select) select))
4542     (if (or (null select) (zerop select))
4543         select
4544       (if (and (not (zerop scored)) (<= (abs select) scored))
4545           (progn
4546             (setq articles (sort scored-list '<))
4547             (setq number (length articles)))
4548         (setq articles (copy-sequence articles)))
4549
4550       (when (< (abs select) number)
4551         (if (< select 0)
4552             ;; Select the N oldest articles.
4553             (setcdr (nthcdr (1- (abs select)) articles) nil)
4554           ;; Select the N most recent articles.
4555           (setq articles (nthcdr (- number select) articles))))
4556       (setq gnus-newsgroup-unselected
4557             (gnus-sorted-intersection
4558              gnus-newsgroup-unreads
4559              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4560       (when gnus-alter-articles-to-read-function
4561         (setq gnus-newsgroup-unreads
4562               (sort
4563                (funcall gnus-alter-articles-to-read-function
4564                         gnus-newsgroup-name gnus-newsgroup-unreads)
4565                '<)))
4566       articles)))
4567
4568 (defun gnus-killed-articles (killed articles)
4569   (let (out)
4570     (while articles
4571       (when (inline (gnus-member-of-range (car articles) killed))
4572         (push (car articles) out))
4573       (setq articles (cdr articles)))
4574     out))
4575
4576 (defun gnus-uncompress-marks (marks)
4577   "Uncompress the mark ranges in MARKS."
4578   (let ((uncompressed '(score bookmark))
4579         out)
4580     (while marks
4581       (if (memq (caar marks) uncompressed)
4582           (push (car marks) out)
4583         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4584       (setq marks (cdr marks)))
4585     out))
4586
4587 (defun gnus-adjust-marked-articles (info)
4588   "Set all article lists and remove all marks that are no longer valid."
4589   (let* ((marked-lists (gnus-info-marks info))
4590          (active (gnus-active (gnus-info-group info)))
4591          (min (car active))
4592          (max (cdr active))
4593          (types gnus-article-mark-lists)
4594          (uncompressed '(score bookmark killed))
4595          marks var articles article mark)
4596
4597     (while marked-lists
4598       (setq marks (pop marked-lists))
4599       (set (setq var (intern (format "gnus-newsgroup-%s"
4600                                      (car (rassq (setq mark (car marks))
4601                                                  types)))))
4602            (if (memq (car marks) uncompressed) (cdr marks)
4603              (gnus-uncompress-range (cdr marks))))
4604
4605       (setq articles (symbol-value var))
4606
4607       ;; All articles have to be subsets of the active articles.
4608       (cond
4609        ;; Adjust "simple" lists.
4610        ((memq mark '(tick dormant expire reply save))
4611         (while articles
4612           (when (or (< (setq article (pop articles)) min) (> article max))
4613             (set var (delq article (symbol-value var))))))
4614        ;; Adjust assocs.
4615        ((memq mark uncompressed)
4616         (when (not (listp (cdr (symbol-value var))))
4617           (set var (list (symbol-value var))))
4618         (when (not (listp (cdr articles)))
4619           (setq articles (list articles)))
4620         (while articles
4621           (when (or (not (consp (setq article (pop articles))))
4622                     (< (car article) min)
4623                     (> (car article) max))
4624             (set var (delq article (symbol-value var))))))))))
4625
4626 (defun gnus-update-missing-marks (missing)
4627   "Go through the list of MISSING articles and remove them from the mark lists."
4628   (when missing
4629     (let ((types gnus-article-mark-lists)
4630           var m)
4631       ;; Go through all types.
4632       (while types
4633         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4634         (when (symbol-value var)
4635           ;; This list has articles.  So we delete all missing articles
4636           ;; from it.
4637           (setq m missing)
4638           (while m
4639             (set var (delq (pop m) (symbol-value var)))))))))
4640
4641 (defun gnus-update-marks ()
4642   "Enter the various lists of marked articles into the newsgroup info list."
4643   (let ((types gnus-article-mark-lists)
4644         (info (gnus-get-info gnus-newsgroup-name))
4645         (uncompressed '(score bookmark killed))
4646         type list newmarked symbol delta-marks)
4647     (when info
4648       ;; Add all marks lists to the list of marks lists.
4649       (while (setq type (pop types))
4650         (setq list (symbol-value
4651                     (setq symbol
4652                           (intern (format "gnus-newsgroup-%s"
4653                                           (car type))))))
4654
4655         (when list
4656           ;; Get rid of the entries of the articles that have the
4657           ;; default score.
4658           (when (and (eq (cdr type) 'score)
4659                      gnus-save-score
4660                      list)
4661             (let* ((arts list)
4662                    (prev (cons nil list))
4663                    (all prev))
4664               (while arts
4665                 (if (or (not (consp (car arts)))
4666                         (= (cdar arts) gnus-summary-default-score))
4667                     (setcdr prev (cdr arts))
4668                   (setq prev arts))
4669                 (setq arts (cdr arts)))
4670               (setq list (cdr all)))))
4671
4672         (unless (memq (cdr type) uncompressed)
4673           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4674
4675         (when (gnus-check-backend-function
4676                'request-set-mark gnus-newsgroup-name)
4677           ;; propagate flags to server, with the following exceptions:
4678           ;; uncompressed:s are not proper flags (they are cons cells)
4679           ;; cache is a internal gnus flag
4680           ;; download are local to one gnus installation (well)
4681           ;; unsend are for nndraft groups only
4682           ;; xxx: generality of this?  this suits nnimap anyway
4683           (unless (memq (cdr type) (append '(cache download unsend)
4684                                            uncompressed))
4685             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4686                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4687                    (add (gnus-remove-from-range
4688                          (gnus-copy-sequence list) old)))
4689               (when add
4690                 (push (list add 'add (list (cdr type))) delta-marks))
4691               (when del
4692                 (push (list del 'del (list (cdr type))) delta-marks)))))
4693
4694         (when list
4695           (push (cons (cdr type) list) newmarked)))
4696
4697       (when delta-marks
4698         (unless (gnus-check-group gnus-newsgroup-name)
4699           (error "Can't open server for %s" gnus-newsgroup-name))
4700         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4701
4702       ;; Enter these new marks into the info of the group.
4703       (if (nthcdr 3 info)
4704           (setcar (nthcdr 3 info) newmarked)
4705         ;; Add the marks lists to the end of the info.
4706         (when newmarked
4707           (setcdr (nthcdr 2 info) (list newmarked))))
4708
4709       ;; Cut off the end of the info if there's nothing else there.
4710       (let ((i 5))
4711         (while (and (> i 2)
4712                     (not (nth i info)))
4713           (when (nthcdr (decf i) info)
4714             (setcdr (nthcdr i info) nil)))))))
4715
4716 (defun gnus-set-mode-line (where)
4717   "Set the mode line of the article or summary buffers.
4718 If WHERE is `summary', the summary mode line format will be used."
4719   ;; Is this mode line one we keep updated?
4720   (when (and (memq where gnus-updated-mode-lines)
4721              (symbol-value
4722               (intern (format "gnus-%s-mode-line-format-spec" where))))
4723     (let (mode-string)
4724       (save-excursion
4725         ;; We evaluate this in the summary buffer since these
4726         ;; variables are buffer-local to that buffer.
4727         (set-buffer gnus-summary-buffer)
4728         ;; We bind all these variables that are used in the `eval' form
4729         ;; below.
4730         (let* ((mformat (symbol-value
4731                          (intern
4732                           (format "gnus-%s-mode-line-format-spec" where))))
4733                (gnus-tmp-group-name (gnus-group-decoded-name 
4734                                      gnus-newsgroup-name))
4735                (gnus-tmp-article-number (or gnus-current-article 0))
4736                (gnus-tmp-unread gnus-newsgroup-unreads)
4737                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4738                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4739                (gnus-tmp-unread-and-unselected
4740                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4741                             (zerop gnus-tmp-unselected))
4742                        "")
4743                       ((zerop gnus-tmp-unselected)
4744                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4745                       (t (format "{%d(+%d) more}"
4746                                  gnus-tmp-unread-and-unticked
4747                                  gnus-tmp-unselected))))
4748                (gnus-tmp-subject
4749                 (if (and gnus-current-headers
4750                          (vectorp gnus-current-headers))
4751                     (gnus-mode-string-quote
4752                      (mail-header-subject gnus-current-headers))
4753                   ""))
4754                bufname-length max-len
4755                gnus-tmp-header);; passed as argument to any user-format-funcs
4756           (setq mode-string (eval mformat))
4757           (setq bufname-length (if (string-match "%b" mode-string)
4758                                    (- (length
4759                                        (buffer-name
4760                                         (if (eq where 'summary)
4761                                             nil
4762                                           (get-buffer gnus-article-buffer))))
4763                                       2)
4764                                  0))
4765           (setq max-len (max 4 (if gnus-mode-non-string-length
4766                                    (- (window-width)
4767                                       gnus-mode-non-string-length
4768                                       bufname-length)
4769                                  (length mode-string))))
4770           ;; We might have to chop a bit of the string off...
4771           (when (> (length mode-string) max-len)
4772             (setq mode-string
4773                   (concat (truncate-string-to-width mode-string (- max-len 3))
4774                           "...")))
4775           ;; Pad the mode string a bit.
4776           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4777       ;; Update the mode line.
4778       (setq mode-line-buffer-identification
4779             (gnus-mode-line-buffer-identification (list mode-string)))
4780       (set-buffer-modified-p t))))
4781
4782 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4783   "Go through the HEADERS list and add all Xrefs to a hash table.
4784 The resulting hash table is returned, or nil if no Xrefs were found."
4785   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4786          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4787          (xref-hashtb (gnus-make-hashtable))
4788          start group entry number xrefs header)
4789     (while headers
4790       (setq header (pop headers))
4791       (when (and (setq xrefs (mail-header-xref header))
4792                  (not (memq (setq number (mail-header-number header))
4793                             unreads)))
4794         (setq start 0)
4795         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4796           (setq start (match-end 0))
4797           (setq group (if prefix
4798                           (concat prefix (substring xrefs (match-beginning 1)
4799                                                     (match-end 1)))
4800                         (substring xrefs (match-beginning 1) (match-end 1))))
4801           (setq number
4802                 (string-to-int (substring xrefs (match-beginning 2)
4803                                           (match-end 2))))
4804           (if (setq entry (gnus-gethash group xref-hashtb))
4805               (setcdr entry (cons number (cdr entry)))
4806             (gnus-sethash group (cons number nil) xref-hashtb)))))
4807     (and start xref-hashtb)))
4808
4809 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4810   "Look through all the headers and mark the Xrefs as read."
4811   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4812         name entry info xref-hashtb idlist method nth4)
4813     (save-excursion
4814       (set-buffer gnus-group-buffer)
4815       (when (setq xref-hashtb
4816                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4817         (mapatoms
4818          (lambda (group)
4819            (unless (string= from-newsgroup (setq name (symbol-name group)))
4820              (setq idlist (symbol-value group))
4821              ;; Dead groups are not updated.
4822              (and (prog1
4823                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4824                             info (nth 2 entry))
4825                     (when (stringp (setq nth4 (gnus-info-method info)))
4826                       (setq nth4 (gnus-server-to-method nth4))))
4827                   ;; Only do the xrefs if the group has the same
4828                   ;; select method as the group we have just read.
4829                   (or (gnus-methods-equal-p
4830                        nth4 (gnus-find-method-for-group from-newsgroup))
4831                       virtual
4832                       (equal nth4 (setq method (gnus-find-method-for-group
4833                                                 from-newsgroup)))
4834                       (and (equal (car nth4) (car method))
4835                            (equal (nth 1 nth4) (nth 1 method))))
4836                   gnus-use-cross-reference
4837                   (or (not (eq gnus-use-cross-reference t))
4838                       virtual
4839                       ;; Only do cross-references on subscribed
4840                       ;; groups, if that is what is wanted.
4841                       (<= (gnus-info-level info) gnus-level-subscribed))
4842                   (gnus-group-make-articles-read name idlist))))
4843          xref-hashtb)))))
4844
4845 (defun gnus-compute-read-articles (group articles)
4846   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4847          (info (nth 2 entry))
4848          (active (gnus-active group))
4849          ninfo)
4850     (when entry
4851       ;; First peel off all invalid article numbers.
4852       (when active
4853         (let ((ids articles)
4854               id first)
4855           (while (setq id (pop ids))
4856             (when (and first (> id (cdr active)))
4857               ;; We'll end up in this situation in one particular
4858               ;; obscure situation.  If you re-scan a group and get
4859               ;; a new article that is cross-posted to a different
4860               ;; group that has not been re-scanned, you might get
4861               ;; crossposted article that has a higher number than
4862               ;; Gnus believes possible.  So we re-activate this
4863               ;; group as well.  This might mean doing the
4864               ;; crossposting thingy will *increase* the number
4865               ;; of articles in some groups.  Tsk, tsk.
4866               (setq active (or (gnus-activate-group group) active)))
4867             (when (or (> id (cdr active))
4868                       (< id (car active)))
4869               (setq articles (delq id articles))))))
4870       ;; If the read list is nil, we init it.
4871       (if (and active
4872                (null (gnus-info-read info))
4873                (> (car active) 1))
4874           (setq ninfo (cons 1 (1- (car active))))
4875         (setq ninfo (gnus-info-read info)))
4876       ;; Then we add the read articles to the range.
4877       (gnus-add-to-range
4878        ninfo (setq articles (sort articles '<))))))
4879
4880 (defun gnus-group-make-articles-read (group articles)
4881   "Update the info of GROUP to say that ARTICLES are read."
4882   (let* ((num 0)
4883          (entry (gnus-gethash group gnus-newsrc-hashtb))
4884          (info (nth 2 entry))
4885          (active (gnus-active group))
4886          range)
4887     (when entry
4888       (setq range (gnus-compute-read-articles group articles))
4889       (save-excursion
4890         (set-buffer gnus-group-buffer)
4891         (gnus-undo-register
4892           `(progn
4893              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4894              (gnus-info-set-read ',info ',(gnus-info-read info))
4895              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4896              (gnus-group-update-group ,group t))))
4897       ;; Add the read articles to the range.
4898       (gnus-info-set-read info range)
4899       ;; Then we have to re-compute how many unread
4900       ;; articles there are in this group.
4901       (when active
4902         (cond
4903          ((not range)
4904           (setq num (- (1+ (cdr active)) (car active))))
4905          ((not (listp (cdr range)))
4906           (setq num (- (cdr active) (- (1+ (cdr range))
4907                                        (car range)))))
4908          (t
4909           (while range
4910             (if (numberp (car range))
4911                 (setq num (1+ num))
4912               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4913             (setq range (cdr range)))
4914           (setq num (- (cdr active) num))))
4915         ;; Update the number of unread articles.
4916         (setcar entry num)
4917         ;; Update the group buffer.
4918         (gnus-group-update-group group t)))))
4919
4920 (defvar gnus-newsgroup-none-id 0)
4921
4922 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4923   (let ((cur nntp-server-buffer)
4924         (dependencies
4925          (or dependencies
4926              (save-excursion (set-buffer gnus-summary-buffer)
4927                              gnus-newsgroup-dependencies)))
4928         headers id end ref
4929         (mail-parse-charset gnus-newsgroup-charset)
4930         (mail-parse-ignored-charsets
4931          (save-excursion (condition-case nil
4932                              (set-buffer gnus-summary-buffer)
4933                            (error))
4934                          gnus-newsgroup-ignored-charsets)))
4935     (save-excursion
4936       (set-buffer nntp-server-buffer)
4937       ;; Translate all TAB characters into SPACE characters.
4938       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4939       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4940       (gnus-run-hooks 'gnus-parse-headers-hook)
4941       (let ((case-fold-search t)
4942             in-reply-to header p lines chars)
4943         (goto-char (point-min))
4944         ;; Search to the beginning of the next header.  Error messages
4945         ;; do not begin with 2 or 3.
4946         (while (re-search-forward "^[23][0-9]+ " nil t)
4947           (setq id nil
4948                 ref nil)
4949           ;; This implementation of this function, with nine
4950           ;; search-forwards instead of the one re-search-forward and
4951           ;; a case (which basically was the old function) is actually
4952           ;; about twice as fast, even though it looks messier.  You
4953           ;; can't have everything, I guess.  Speed and elegance
4954           ;; doesn't always go hand in hand.
4955           (setq
4956            header
4957            (vector
4958             ;; Number.
4959             (prog1
4960                 (read cur)
4961               (end-of-line)
4962               (setq p (point))
4963               (narrow-to-region (point)
4964                                 (or (and (search-forward "\n.\n" nil t)
4965                                          (- (point) 2))
4966                                     (point))))
4967             ;; Subject.
4968             (progn
4969               (goto-char p)
4970               (if (search-forward "\nsubject: " nil t)
4971                   (funcall gnus-decode-encoded-word-function
4972                            (nnheader-header-value))
4973                 "(none)"))
4974             ;; From.
4975             (progn
4976               (goto-char p)
4977               (if (or (search-forward "\nfrom: " nil t)
4978                       (search-forward "\nfrom:" nil t))
4979                   (funcall gnus-decode-encoded-word-function
4980                            (nnheader-header-value))
4981                 "(nobody)"))
4982             ;; Date.
4983             (progn
4984               (goto-char p)
4985               (if (search-forward "\ndate: " nil t)
4986                   (nnheader-header-value) ""))
4987             ;; Message-ID.
4988             (progn
4989               (goto-char p)
4990               (setq id (if (re-search-forward
4991                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4992                            ;; We do it this way to make sure the Message-ID
4993                            ;; is (somewhat) syntactically valid.
4994                            (buffer-substring (match-beginning 1)
4995                                              (match-end 1))
4996                          ;; If there was no message-id, we just fake one
4997                          ;; to make subsequent routines simpler.
4998                          (nnheader-generate-fake-message-id))))
4999             ;; References.
5000             (progn
5001               (goto-char p)
5002               (if (search-forward "\nreferences: " nil t)
5003                   (progn
5004                     (setq end (point))
5005                     (prog1
5006                         (nnheader-header-value)
5007                       (setq ref
5008                             (buffer-substring
5009                              (progn
5010                                (end-of-line)
5011                                (search-backward ">" end t)
5012                                (1+ (point)))
5013                              (progn
5014                                (search-backward "<" end t)
5015                                (point))))))
5016                 ;; Get the references from the in-reply-to header if there
5017                 ;; were no references and the in-reply-to header looks
5018                 ;; promising.
5019                 (if (and (search-forward "\nin-reply-to: " nil t)
5020                          (setq in-reply-to (nnheader-header-value))
5021                          (string-match "<[^>]+>" in-reply-to))
5022                     (let (ref2)
5023                       (setq ref (substring in-reply-to (match-beginning 0)
5024                                            (match-end 0)))
5025                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5026                         (setq ref2 (substring in-reply-to (match-beginning 0)
5027                                               (match-end 0)))
5028                         (when (> (length ref2) (length ref))
5029                           (setq ref ref2)))
5030                       ref)
5031                   (setq ref nil))))
5032             ;; Chars.
5033             (progn
5034               (goto-char p)
5035               (if (search-forward "\nchars: " nil t)
5036                   (if (numberp (setq chars (ignore-errors (read cur))))
5037                       chars -1)
5038                 -1))
5039             ;; Lines.
5040             (progn
5041               (goto-char p)
5042               (if (search-forward "\nlines: " nil t)
5043                   (if (numberp (setq lines (ignore-errors (read cur))))
5044                       lines -1)
5045                 -1))
5046             ;; Xref.
5047             (progn
5048               (goto-char p)
5049               (and (search-forward "\nxref: " nil t)
5050                    (nnheader-header-value)))
5051             ;; Extra.
5052             (when gnus-extra-headers
5053               (let ((extra gnus-extra-headers)
5054                     out)
5055                 (while extra
5056                   (goto-char p)
5057                   (when (search-forward
5058                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5059                     (push (cons (car extra) (nnheader-header-value))
5060                           out))
5061                   (pop extra))
5062                 out))))
5063           (when (equal id ref)
5064             (setq ref nil))
5065
5066           (when gnus-alter-header-function
5067             (funcall gnus-alter-header-function header)
5068             (setq id (mail-header-id header)
5069                   ref (gnus-parent-id (mail-header-references header))))
5070
5071           (when (setq header
5072                       (gnus-dependencies-add-header
5073                        header dependencies force-new))
5074             (push header headers))
5075           (goto-char (point-max))
5076           (widen))
5077         (nreverse headers)))))
5078
5079 ;; Goes through the xover lines and returns a list of vectors
5080 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5081                                                   force-new dependencies
5082                                                   group also-fetch-heads)
5083   "Parse the news overview data in the server buffer.
5084 Return a list of headers that match SEQUENCE (see
5085 `nntp-retrieve-headers')."
5086   ;; Get the Xref when the users reads the articles since most/some
5087   ;; NNTP servers do not include Xrefs when using XOVER.
5088   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5089   (let ((mail-parse-charset gnus-newsgroup-charset)
5090         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5091         (cur nntp-server-buffer)
5092         (dependencies (or dependencies gnus-newsgroup-dependencies))
5093         number headers header)
5094     (save-excursion
5095       (set-buffer nntp-server-buffer)
5096       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5097       ;; Allow the user to mangle the headers before parsing them.
5098       (gnus-run-hooks 'gnus-parse-headers-hook)
5099       (goto-char (point-min))
5100       (while (not (eobp))
5101         (condition-case ()
5102             (while (and sequence (not (eobp)))
5103               (setq number (read cur))
5104               (while (and sequence
5105                           (< (car sequence) number))
5106                 (setq sequence (cdr sequence)))
5107               (and sequence
5108                    (eq number (car sequence))
5109                    (progn
5110                      (setq sequence (cdr sequence))
5111                      (setq header (inline
5112                                     (gnus-nov-parse-line
5113                                      number dependencies force-new))))
5114                    (push header headers))
5115               (forward-line 1))
5116           (error
5117            (gnus-error 4 "Strange nov line (%d)"
5118                        (count-lines (point-min) (point)))))
5119         (forward-line 1))
5120       ;; A common bug in inn is that if you have posted an article and
5121       ;; then retrieves the active file, it will answer correctly --
5122       ;; the new article is included.  However, a NOV entry for the
5123       ;; article may not have been generated yet, so this may fail.
5124       ;; We work around this problem by retrieving the last few
5125       ;; headers using HEAD.
5126       (if (or (not also-fetch-heads)
5127               (not sequence))
5128           ;; We (probably) got all the headers.
5129           (nreverse headers)
5130         (let ((gnus-nov-is-evil t))
5131           (nconc
5132            (nreverse headers)
5133            (when (gnus-retrieve-headers sequence group)
5134              (gnus-get-newsgroup-headers))))))))
5135
5136 (defun gnus-article-get-xrefs ()
5137   "Fill in the Xref value in `gnus-current-headers', if necessary.
5138 This is meant to be called in `gnus-article-internal-prepare-hook'."
5139   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5140                                  gnus-current-headers)))
5141     (or (not gnus-use-cross-reference)
5142         (not headers)
5143         (and (mail-header-xref headers)
5144              (not (string= (mail-header-xref headers) "")))
5145         (let ((case-fold-search t)
5146               xref)
5147           (save-restriction
5148             (nnheader-narrow-to-headers)
5149             (goto-char (point-min))
5150             (when (or (and (not (eobp))
5151                            (eq (downcase (char-after)) ?x)
5152                            (looking-at "Xref:"))
5153                       (search-forward "\nXref:" nil t))
5154               (goto-char (1+ (match-end 0)))
5155               (setq xref (buffer-substring (point)
5156                                            (progn (end-of-line) (point))))
5157               (mail-header-set-xref headers xref)))))))
5158
5159 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5160   "Find article ID and insert the summary line for that article.
5161 OLD-HEADER can either be a header or a line number to insert
5162 the subject line on."
5163   (let* ((line (and (numberp old-header) old-header))
5164          (old-header (and (vectorp old-header) old-header))
5165          (header (cond ((and old-header use-old-header)
5166                         old-header)
5167                        ((and (numberp id)
5168                              (gnus-number-to-header id))
5169                         (gnus-number-to-header id))
5170                        (t
5171                         (gnus-read-header id))))
5172          (number (and (numberp id) id))
5173          d)
5174     (when header
5175       ;; Rebuild the thread that this article is part of and go to the
5176       ;; article we have fetched.
5177       (when (and (not gnus-show-threads)
5178                  old-header)
5179         (when (and number
5180                    (setq d (gnus-data-find (mail-header-number old-header))))
5181           (goto-char (gnus-data-pos d))
5182           (gnus-data-remove
5183            number
5184            (- (gnus-point-at-bol)
5185               (prog1
5186                   (1+ (gnus-point-at-eol))
5187                 (gnus-delete-line))))))
5188       (when old-header
5189         (mail-header-set-number header (mail-header-number old-header)))
5190       (setq gnus-newsgroup-sparse
5191             (delq (setq number (mail-header-number header))
5192                   gnus-newsgroup-sparse))
5193       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5194       (push number gnus-newsgroup-limit)
5195       (gnus-rebuild-thread (mail-header-id header) line)
5196       (gnus-summary-goto-subject number nil t))
5197     (when (and (numberp number)
5198                (> number 0))
5199       ;; We have to update the boundaries even if we can't fetch the
5200       ;; article if ID is a number -- so that the next `P' or `N'
5201       ;; command will fetch the previous (or next) article even
5202       ;; if the one we tried to fetch this time has been canceled.
5203       (when (> number gnus-newsgroup-end)
5204         (setq gnus-newsgroup-end number))
5205       (when (< number gnus-newsgroup-begin)
5206         (setq gnus-newsgroup-begin number))
5207       (setq gnus-newsgroup-unselected
5208             (delq number gnus-newsgroup-unselected)))
5209     ;; Report back a success?
5210     (and header (mail-header-number header))))
5211
5212 ;;; Process/prefix in the summary buffer
5213
5214 (defun gnus-summary-work-articles (n)
5215   "Return a list of articles to be worked upon.
5216 The prefix argument, the list of process marked articles, and the
5217 current article will be taken into consideration."
5218   (save-excursion
5219     (set-buffer gnus-summary-buffer)
5220     (cond
5221      (n
5222       ;; A numerical prefix has been given.
5223       (setq n (prefix-numeric-value n))
5224       (let ((backward (< n 0))
5225             (n (abs (prefix-numeric-value n)))
5226             articles article)
5227         (save-excursion
5228           (while
5229               (and (> n 0)
5230                    (push (setq article (gnus-summary-article-number))
5231                          articles)
5232                    (if backward
5233                        (gnus-summary-find-prev nil article)
5234                      (gnus-summary-find-next nil article)))
5235             (decf n)))
5236         (nreverse articles)))
5237      ((and (gnus-region-active-p) (mark))
5238       (message "region active")
5239       ;; Work on the region between point and mark.
5240       (let ((max (max (point) (mark)))
5241             articles article)
5242         (save-excursion
5243           (goto-char (min (min (point) (mark))))
5244           (while
5245               (and
5246                (push (setq article (gnus-summary-article-number)) articles)
5247                (gnus-summary-find-next nil article)
5248                (< (point) max)))
5249           (nreverse articles))))
5250      (gnus-newsgroup-processable
5251       ;; There are process-marked articles present.
5252       ;; Save current state.
5253       (gnus-summary-save-process-mark)
5254       ;; Return the list.
5255       (reverse gnus-newsgroup-processable))
5256      (t
5257       ;; Just return the current article.
5258       (list (gnus-summary-article-number))))))
5259
5260 (defmacro gnus-summary-iterate (arg &rest forms)
5261   "Iterate over the process/prefixed articles and do FORMS.
5262 ARG is the interactive prefix given to the command.  FORMS will be
5263 executed with point over the summary line of the articles."
5264   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5265     `(let ((,articles (gnus-summary-work-articles ,arg)))
5266        (while ,articles
5267          (gnus-summary-goto-subject (car ,articles))
5268          ,@forms
5269          (pop ,articles)))))
5270
5271 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5272 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5273
5274 (defun gnus-summary-save-process-mark ()
5275   "Push the current set of process marked articles on the stack."
5276   (interactive)
5277   (push (copy-sequence gnus-newsgroup-processable)
5278         gnus-newsgroup-process-stack))
5279
5280 (defun gnus-summary-kill-process-mark ()
5281   "Push the current set of process marked articles on the stack and unmark."
5282   (interactive)
5283   (gnus-summary-save-process-mark)
5284   (gnus-summary-unmark-all-processable))
5285
5286 (defun gnus-summary-yank-process-mark ()
5287   "Pop the last process mark state off the stack and restore it."
5288   (interactive)
5289   (unless gnus-newsgroup-process-stack
5290     (error "Empty mark stack"))
5291   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5292
5293 (defun gnus-summary-process-mark-set (set)
5294   "Make SET into the current process marked articles."
5295   (gnus-summary-unmark-all-processable)
5296   (while set
5297     (gnus-summary-set-process-mark (pop set))))
5298
5299 ;;; Searching and stuff
5300
5301 (defun gnus-summary-search-group (&optional backward use-level)
5302   "Search for next unread newsgroup.
5303 If optional argument BACKWARD is non-nil, search backward instead."
5304   (save-excursion
5305     (set-buffer gnus-group-buffer)
5306     (when (gnus-group-search-forward
5307            backward nil (if use-level (gnus-group-group-level) nil))
5308       (gnus-group-group-name))))
5309
5310 (defun gnus-summary-best-group (&optional exclude-group)
5311   "Find the name of the best unread group.
5312 If EXCLUDE-GROUP, do not go to this group."
5313   (save-excursion
5314     (set-buffer gnus-group-buffer)
5315     (save-excursion
5316       (gnus-group-best-unread-group exclude-group))))
5317
5318 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5319   (if backward (gnus-summary-find-prev)
5320     (let* ((dummy (gnus-summary-article-intangible-p))
5321            (article (or article (gnus-summary-article-number)))
5322            (arts (gnus-data-find-list article))
5323            result)
5324       (when (and (not dummy)
5325                  (or (not gnus-summary-check-current)
5326                      (not unread)
5327                      (not (gnus-data-unread-p (car arts)))))
5328         (setq arts (cdr arts)))
5329       (when (setq result
5330                   (if unread
5331                       (progn
5332                         (while arts
5333                           (when (or (and undownloaded
5334                                          (eq gnus-undownloaded-mark
5335                                              (gnus-data-mark (car arts))))
5336                                     (gnus-data-unread-p (car arts)))
5337                             (setq result (car arts)
5338                                   arts nil))
5339                           (setq arts (cdr arts)))
5340                         result)
5341                     (car arts)))
5342         (goto-char (gnus-data-pos result))
5343         (gnus-data-number result)))))
5344
5345 (defun gnus-summary-find-prev (&optional unread article)
5346   (let* ((eobp (eobp))
5347          (article (or article (gnus-summary-article-number)))
5348          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5349          result)
5350     (when (and (not eobp)
5351                (or (not gnus-summary-check-current)
5352                    (not unread)
5353                    (not (gnus-data-unread-p (car arts)))))
5354       (setq arts (cdr arts)))
5355     (when (setq result
5356                 (if unread
5357                     (progn
5358                       (while arts
5359                         (when (gnus-data-unread-p (car arts))
5360                           (setq result (car arts)
5361                                 arts nil))
5362                         (setq arts (cdr arts)))
5363                       result)
5364                   (car arts)))
5365       (goto-char (gnus-data-pos result))
5366       (gnus-data-number result))))
5367
5368 (defun gnus-summary-find-subject (subject &optional unread backward article)
5369   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5370          (article (or article (gnus-summary-article-number)))
5371          (articles (gnus-data-list backward))
5372          (arts (gnus-data-find-list article articles))
5373          result)
5374     (when (or (not gnus-summary-check-current)
5375               (not unread)
5376               (not (gnus-data-unread-p (car arts))))
5377       (setq arts (cdr arts)))
5378     (while arts
5379       (and (or (not unread)
5380                (gnus-data-unread-p (car arts)))
5381            (vectorp (gnus-data-header (car arts)))
5382            (gnus-subject-equal
5383             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5384            (setq result (car arts)
5385                  arts nil))
5386       (setq arts (cdr arts)))
5387     (and result
5388          (goto-char (gnus-data-pos result))
5389          (gnus-data-number result))))
5390
5391 (defun gnus-summary-search-forward (&optional unread subject backward)
5392   "Search forward for an article.
5393 If UNREAD, look for unread articles.  If SUBJECT, look for
5394 articles with that subject.  If BACKWARD, search backward instead."
5395   (cond (subject (gnus-summary-find-subject subject unread backward))
5396         (backward (gnus-summary-find-prev unread))
5397         (t (gnus-summary-find-next unread))))
5398
5399 (defun gnus-recenter (&optional n)
5400   "Center point in window and redisplay frame.
5401 Also do horizontal recentering."
5402   (interactive "P")
5403   (when (and gnus-auto-center-summary
5404              (not (eq gnus-auto-center-summary 'vertical)))
5405     (gnus-horizontal-recenter))
5406   (recenter n))
5407
5408 (defun gnus-summary-recenter ()
5409   "Center point in the summary window.
5410 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5411 displayed, no centering will be performed."
5412   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5413   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5414   (interactive)
5415   (let* ((top (cond ((< (window-height) 4) 0)
5416                     ((< (window-height) 7) 1)
5417                     (t (if (numberp gnus-auto-center-summary)
5418                            gnus-auto-center-summary
5419                          2))))
5420          (height (1- (window-height)))
5421          (bottom (save-excursion (goto-char (point-max))
5422                                  (forward-line (- height))
5423                                  (point)))
5424          (window (get-buffer-window (current-buffer))))
5425     ;; The user has to want it.
5426     (when gnus-auto-center-summary
5427       (when (get-buffer-window gnus-article-buffer)
5428         ;; Only do recentering when the article buffer is displayed,
5429         ;; Set the window start to either `bottom', which is the biggest
5430         ;; possible valid number, or the second line from the top,
5431         ;; whichever is the least.
5432         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5433           (if (> bottom top-pos)
5434               ;; Keep the second line from the top visible
5435               (set-window-start window top-pos t)
5436             ;; Try to keep the bottom line visible; if it's partially
5437             ;; obscured, either scroll one more line to make it fully
5438             ;; visible, or revert to using TOP-POS.
5439             (save-excursion
5440               (goto-char (point-max))
5441               (forward-line -1)
5442               (let ((last-line-start (point)))
5443                 (goto-char bottom)
5444                 (set-window-start window (point) t)
5445                 (when (not (pos-visible-in-window-p last-line-start window))
5446                   (forward-line 1)
5447                   (set-window-start window (min (point) top-pos) t)))))))
5448       ;; Do horizontal recentering while we're at it.
5449       (when (and (get-buffer-window (current-buffer) t)
5450                  (not (eq gnus-auto-center-summary 'vertical)))
5451         (let ((selected (selected-window)))
5452           (select-window (get-buffer-window (current-buffer) t))
5453           (gnus-summary-position-point)
5454           (gnus-horizontal-recenter)
5455           (select-window selected))))))
5456
5457 (defun gnus-summary-jump-to-group (newsgroup)
5458   "Move point to NEWSGROUP in group mode buffer."
5459   ;; Keep update point of group mode buffer if visible.
5460   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5461       (save-window-excursion
5462         ;; Take care of tree window mode.
5463         (when (get-buffer-window gnus-group-buffer)
5464           (pop-to-buffer gnus-group-buffer))
5465         (gnus-group-jump-to-group newsgroup))
5466     (save-excursion
5467       ;; Take care of tree window mode.
5468       (if (get-buffer-window gnus-group-buffer)
5469           (pop-to-buffer gnus-group-buffer)
5470         (set-buffer gnus-group-buffer))
5471       (gnus-group-jump-to-group newsgroup))))
5472
5473 ;; This function returns a list of article numbers based on the
5474 ;; difference between the ranges of read articles in this group and
5475 ;; the range of active articles.
5476 (defun gnus-list-of-unread-articles (group)
5477   (let* ((read (gnus-info-read (gnus-get-info group)))
5478          (active (or (gnus-active group) (gnus-activate-group group)))
5479          (last (cdr active))
5480          first nlast unread)
5481     ;; If none are read, then all are unread.
5482     (if (not read)
5483         (setq first (car active))
5484       ;; If the range of read articles is a single range, then the
5485       ;; first unread article is the article after the last read
5486       ;; article.  Sounds logical, doesn't it?
5487       (if (and (not (listp (cdr read)))
5488                (or (< (car read) (car active))
5489                    (progn (setq read (list read))
5490                           nil)))
5491           (setq first (max (car active) (1+ (cdr read))))
5492         ;; `read' is a list of ranges.
5493         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5494                                   (caar read)))
5495                   1)
5496           (setq first (car active)))
5497         (while read
5498           (when first
5499             (while (< first nlast)
5500               (push first unread)
5501               (setq first (1+ first))))
5502           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5503           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5504           (setq read (cdr read)))))
5505     ;; And add the last unread articles.
5506     (while (<= first last)
5507       (push first unread)
5508       (setq first (1+ first)))
5509     ;; Return the list of unread articles.
5510     (delq 0 (nreverse unread))))
5511
5512 (defun gnus-list-of-read-articles (group)
5513   "Return a list of unread, unticked and non-dormant articles."
5514   (let* ((info (gnus-get-info group))
5515          (marked (gnus-info-marks info))
5516          (active (gnus-active group)))
5517     (and info active
5518          (gnus-set-difference
5519           (gnus-sorted-complement
5520            (gnus-uncompress-range active)
5521            (gnus-list-of-unread-articles group))
5522           (append
5523            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5524            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5525
5526 ;; Various summary commands
5527
5528 (defun gnus-summary-select-article-buffer ()
5529   "Reconfigure windows to show article buffer."
5530   (interactive)
5531   (if (not (gnus-buffer-live-p gnus-article-buffer))
5532       (error "There is no article buffer for this summary buffer")
5533     (gnus-configure-windows 'article)
5534     (select-window (get-buffer-window gnus-article-buffer))))
5535
5536 (defun gnus-summary-universal-argument (arg)
5537   "Perform any operation on all articles that are process/prefixed."
5538   (interactive "P")
5539   (let ((articles (gnus-summary-work-articles arg))
5540         func article)
5541     (if (eq
5542          (setq
5543           func
5544           (key-binding
5545            (read-key-sequence
5546             (substitute-command-keys
5547              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5548          'undefined)
5549         (gnus-error 1 "Undefined key")
5550       (save-excursion
5551         (while articles
5552           (gnus-summary-goto-subject (setq article (pop articles)))
5553           (let (gnus-newsgroup-processable)
5554             (command-execute func))
5555           (gnus-summary-remove-process-mark article)))))
5556   (gnus-summary-position-point))
5557
5558 (defun gnus-summary-toggle-truncation (&optional arg)
5559   "Toggle truncation of summary lines.
5560 With arg, turn line truncation on iff arg is positive."
5561   (interactive "P")
5562   (setq truncate-lines
5563         (if (null arg) (not truncate-lines)
5564           (> (prefix-numeric-value arg) 0)))
5565   (redraw-display))
5566
5567 (defun gnus-summary-reselect-current-group (&optional all rescan)
5568   "Exit and then reselect the current newsgroup.
5569 The prefix argument ALL means to select all articles."
5570   (interactive "P")
5571   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5572     (error "Ephemeral groups can't be reselected"))
5573   (let ((current-subject (gnus-summary-article-number))
5574         (group gnus-newsgroup-name))
5575     (setq gnus-newsgroup-begin nil)
5576     (gnus-summary-exit)
5577     ;; We have to adjust the point of group mode buffer because
5578     ;; point was moved to the next unread newsgroup by exiting.
5579     (gnus-summary-jump-to-group group)
5580     (when rescan
5581       (save-excursion
5582         (gnus-group-get-new-news-this-group 1)))
5583     (gnus-group-read-group all t)
5584     (gnus-summary-goto-subject current-subject nil t)))
5585
5586 (defun gnus-summary-rescan-group (&optional all)
5587   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5588   (interactive "P")
5589   (gnus-summary-reselect-current-group all t))
5590
5591 (defun gnus-summary-update-info (&optional non-destructive)
5592   (save-excursion
5593     (let ((group gnus-newsgroup-name))
5594       (when group
5595         (when gnus-newsgroup-kill-headers
5596           (setq gnus-newsgroup-killed
5597                 (gnus-compress-sequence
5598                  (nconc
5599                   (gnus-set-sorted-intersection
5600                    (gnus-uncompress-range gnus-newsgroup-killed)
5601                    (setq gnus-newsgroup-unselected
5602                          (sort gnus-newsgroup-unselected '<)))
5603                   (setq gnus-newsgroup-unreads
5604                         (sort gnus-newsgroup-unreads '<)))
5605                  t)))
5606         (unless (listp (cdr gnus-newsgroup-killed))
5607           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5608         (let ((headers gnus-newsgroup-headers))
5609           ;; Set the new ranges of read articles.
5610           (save-excursion
5611             (set-buffer gnus-group-buffer)
5612             (gnus-undo-force-boundary))
5613           (gnus-update-read-articles
5614            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5615           ;; Set the current article marks.
5616           (let ((gnus-newsgroup-scored
5617                  (if (and (not gnus-save-score)
5618                           (not non-destructive))
5619                      nil
5620                    gnus-newsgroup-scored)))
5621             (save-excursion
5622               (gnus-update-marks)))
5623           ;; Do the cross-ref thing.
5624           (when gnus-use-cross-reference
5625             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5626           ;; Do not switch windows but change the buffer to work.
5627           (set-buffer gnus-group-buffer)
5628           (unless (gnus-ephemeral-group-p group)
5629             (gnus-group-update-group group)))))))
5630
5631 (defun gnus-summary-save-newsrc (&optional force)
5632   "Save the current number of read/marked articles in the dribble buffer.
5633 The dribble buffer will then be saved.
5634 If FORCE (the prefix), also save the .newsrc file(s)."
5635   (interactive "P")
5636   (gnus-summary-update-info t)
5637   (if force
5638       (gnus-save-newsrc-file)
5639     (gnus-dribble-save)))
5640
5641 (defun gnus-summary-exit (&optional temporary)
5642   "Exit reading current newsgroup, and then return to group selection mode.
5643 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5644   (interactive)
5645   (gnus-set-global-variables)
5646   (when (gnus-buffer-live-p gnus-article-buffer)
5647     (save-excursion
5648       (set-buffer gnus-article-buffer)
5649       (mm-destroy-parts gnus-article-mime-handles)
5650       ;; Set it to nil for safety reason.
5651       (setq gnus-article-mime-handle-alist nil)
5652       (setq gnus-article-mime-handles nil)))
5653   (gnus-kill-save-kill-buffer)
5654   (gnus-async-halt-prefetch)
5655   (let* ((group gnus-newsgroup-name)
5656          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5657          (mode major-mode)
5658          (group-point nil)
5659          (buf (current-buffer)))
5660     (unless quit-config
5661       ;; Do adaptive scoring, and possibly save score files.
5662       (when gnus-newsgroup-adaptive
5663         (gnus-score-adaptive))
5664       (when gnus-use-scoring
5665         (gnus-score-save)))
5666     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5667     ;; If we have several article buffers, we kill them at exit.
5668     (unless gnus-single-article-buffer
5669       (gnus-kill-buffer gnus-original-article-buffer)
5670       (setq gnus-article-current nil))
5671     (when gnus-use-cache
5672       (gnus-cache-possibly-remove-articles)
5673       (gnus-cache-save-buffers))
5674     (gnus-async-prefetch-remove-group group)
5675     (when gnus-suppress-duplicates
5676       (gnus-dup-enter-articles))
5677     (when gnus-use-trees
5678       (gnus-tree-close group))
5679     (when gnus-use-cache
5680       (gnus-cache-write-active))
5681     ;; Remove entries for this group.
5682     (nnmail-purge-split-history (gnus-group-real-name group))
5683     ;; Make all changes in this group permanent.
5684     (unless quit-config
5685       (gnus-run-hooks 'gnus-exit-group-hook)
5686       (gnus-summary-update-info))
5687     (gnus-close-group group)
5688     ;; Make sure where we were, and go to next newsgroup.
5689     (set-buffer gnus-group-buffer)
5690     (unless quit-config
5691       (gnus-group-jump-to-group group))
5692     (gnus-run-hooks 'gnus-summary-exit-hook)
5693     (unless (or quit-config
5694                 ;; If this group has disappeared from the summary
5695                 ;; buffer, don't skip forwards.
5696                 (not (string= group (gnus-group-group-name))))
5697       (gnus-group-next-unread-group 1))
5698     (setq group-point (point))
5699     (if temporary
5700         nil                             ;Nothing to do.
5701       ;; If we have several article buffers, we kill them at exit.
5702       (unless gnus-single-article-buffer
5703         (gnus-kill-buffer gnus-article-buffer)
5704         (gnus-kill-buffer gnus-original-article-buffer)
5705         (setq gnus-article-current nil))
5706       (set-buffer buf)
5707       (if (not gnus-kill-summary-on-exit)
5708           (gnus-deaden-summary)
5709         ;; We set all buffer-local variables to nil.  It is unclear why
5710         ;; this is needed, but if we don't, buffer-local variables are
5711         ;; not garbage-collected, it seems.  This would the lead to en
5712         ;; ever-growing Emacs.
5713         (gnus-summary-clear-local-variables)
5714         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5715           (gnus-summary-clear-local-variables))
5716         (when (get-buffer gnus-article-buffer)
5717           (bury-buffer gnus-article-buffer))
5718         ;; We clear the global counterparts of the buffer-local
5719         ;; variables as well, just to be on the safe side.
5720         (set-buffer gnus-group-buffer)
5721         (gnus-summary-clear-local-variables)
5722         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5723           (gnus-summary-clear-local-variables))
5724         ;; Return to group mode buffer.
5725         (when (eq mode 'gnus-summary-mode)
5726           (gnus-kill-buffer buf)))
5727       (setq gnus-current-select-method gnus-select-method)
5728       (pop-to-buffer gnus-group-buffer)
5729       (if (not quit-config)
5730           (progn
5731             (goto-char group-point)
5732             (gnus-configure-windows 'group 'force))
5733         (gnus-handle-ephemeral-exit quit-config))
5734       ;; Clear the current group name.
5735       (unless quit-config
5736         (setq gnus-newsgroup-name nil)))))
5737
5738 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5739 (defun gnus-summary-exit-no-update (&optional no-questions)
5740   "Quit reading current newsgroup without updating read article info."
5741   (interactive)
5742   (let* ((group gnus-newsgroup-name)
5743          (quit-config (gnus-group-quit-config group)))
5744     (when (or no-questions
5745               gnus-expert-user
5746               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5747       (gnus-async-halt-prefetch)
5748       (mapcar 'funcall
5749               (delq 'gnus-summary-expire-articles
5750                     (copy-sequence gnus-summary-prepare-exit-hook)))
5751       (when (gnus-buffer-live-p gnus-article-buffer)
5752         (save-excursion
5753           (set-buffer gnus-article-buffer)
5754           (mm-destroy-parts gnus-article-mime-handles)
5755           ;; Set it to nil for safety reason.
5756           (setq gnus-article-mime-handle-alist nil)
5757           (setq gnus-article-mime-handles nil)))
5758       ;; If we have several article buffers, we kill them at exit.
5759       (unless gnus-single-article-buffer
5760         (gnus-kill-buffer gnus-article-buffer)
5761         (gnus-kill-buffer gnus-original-article-buffer)
5762         (setq gnus-article-current nil))
5763       (if (not gnus-kill-summary-on-exit)
5764           (gnus-deaden-summary)
5765         (gnus-close-group group)
5766         (gnus-summary-clear-local-variables)
5767         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5768           (gnus-summary-clear-local-variables))
5769         (set-buffer gnus-group-buffer)
5770         (gnus-summary-clear-local-variables)
5771         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5772           (gnus-summary-clear-local-variables))
5773         (when (get-buffer gnus-summary-buffer)
5774           (kill-buffer gnus-summary-buffer)))
5775       (unless gnus-single-article-buffer
5776         (setq gnus-article-current nil))
5777       (when gnus-use-trees
5778         (gnus-tree-close group))
5779       (gnus-async-prefetch-remove-group group)
5780       (when (get-buffer gnus-article-buffer)
5781         (bury-buffer gnus-article-buffer))
5782       ;; Return to the group buffer.
5783       (gnus-configure-windows 'group 'force)
5784       ;; Clear the current group name.
5785       (setq gnus-newsgroup-name nil)
5786       (when (equal (gnus-group-group-name) group)
5787         (gnus-group-next-unread-group 1))
5788       (when quit-config
5789         (gnus-handle-ephemeral-exit quit-config)))))
5790
5791 (defun gnus-handle-ephemeral-exit (quit-config)
5792   "Handle movement when leaving an ephemeral group.
5793 The state which existed when entering the ephemeral is reset."
5794   (if (not (buffer-name (car quit-config)))
5795       (gnus-configure-windows 'group 'force)
5796     (set-buffer (car quit-config))
5797     (cond ((eq major-mode 'gnus-summary-mode)
5798            (gnus-set-global-variables))
5799           ((eq major-mode 'gnus-article-mode)
5800            (save-excursion
5801              ;; The `gnus-summary-buffer' variable may point
5802              ;; to the old summary buffer when using a single
5803              ;; article buffer.
5804              (unless (gnus-buffer-live-p gnus-summary-buffer)
5805                (set-buffer gnus-group-buffer))
5806              (set-buffer gnus-summary-buffer)
5807              (gnus-set-global-variables))))
5808     (if (or (eq (cdr quit-config) 'article)
5809             (eq (cdr quit-config) 'pick))
5810         (progn
5811           ;; The current article may be from the ephemeral group
5812           ;; thus it is best that we reload this article
5813           (gnus-summary-show-article)
5814           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5815               (gnus-configure-windows 'pick 'force)
5816             (gnus-configure-windows (cdr quit-config) 'force)))
5817       (gnus-configure-windows (cdr quit-config) 'force))
5818     (when (eq major-mode 'gnus-summary-mode)
5819       (gnus-summary-next-subject 1 nil t)
5820       (gnus-summary-recenter)
5821       (gnus-summary-position-point))))
5822
5823 ;;; Dead summaries.
5824
5825 (defvar gnus-dead-summary-mode-map nil)
5826
5827 (unless gnus-dead-summary-mode-map
5828   (setq gnus-dead-summary-mode-map (make-keymap))
5829   (suppress-keymap gnus-dead-summary-mode-map)
5830   (substitute-key-definition
5831    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5832   (let ((keys '("\C-d" "\r" "\177" [delete])))
5833     (while keys
5834       (define-key gnus-dead-summary-mode-map
5835         (pop keys) 'gnus-summary-wake-up-the-dead))))
5836
5837 (defvar gnus-dead-summary-mode nil
5838   "Minor mode for Gnus summary buffers.")
5839
5840 (defun gnus-dead-summary-mode (&optional arg)
5841   "Minor mode for Gnus summary buffers."
5842   (interactive "P")
5843   (when (eq major-mode 'gnus-summary-mode)
5844     (make-local-variable 'gnus-dead-summary-mode)
5845     (setq gnus-dead-summary-mode
5846           (if (null arg) (not gnus-dead-summary-mode)
5847             (> (prefix-numeric-value arg) 0)))
5848     (when gnus-dead-summary-mode
5849       (gnus-add-minor-mode
5850        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5851
5852 (defun gnus-deaden-summary ()
5853   "Make the current summary buffer into a dead summary buffer."
5854   ;; Kill any previous dead summary buffer.
5855   (when (and gnus-dead-summary
5856              (buffer-name gnus-dead-summary))
5857     (save-excursion
5858       (set-buffer gnus-dead-summary)
5859       (when gnus-dead-summary-mode
5860         (kill-buffer (current-buffer)))))
5861   ;; Make this the current dead summary.
5862   (setq gnus-dead-summary (current-buffer))
5863   (gnus-dead-summary-mode 1)
5864   (let ((name (buffer-name)))
5865     (when (string-match "Summary" name)
5866       (rename-buffer
5867        (concat (substring name 0 (match-beginning 0)) "Dead "
5868                (substring name (match-beginning 0)))
5869        t)
5870       (bury-buffer))))
5871
5872 (defun gnus-kill-or-deaden-summary (buffer)
5873   "Kill or deaden the summary BUFFER."
5874   (save-excursion
5875     (when (and (buffer-name buffer)
5876                (not gnus-single-article-buffer))
5877       (save-excursion
5878         (set-buffer buffer)
5879         (gnus-kill-buffer gnus-article-buffer)
5880         (gnus-kill-buffer gnus-original-article-buffer)))
5881     (cond (gnus-kill-summary-on-exit
5882            (when (and gnus-use-trees
5883                       (gnus-buffer-exists-p buffer))
5884              (save-excursion
5885                (set-buffer buffer)
5886                (gnus-tree-close gnus-newsgroup-name)))
5887            (gnus-kill-buffer buffer))
5888           ((gnus-buffer-exists-p buffer)
5889            (save-excursion
5890              (set-buffer buffer)
5891              (gnus-deaden-summary))))))
5892
5893 (defun gnus-summary-wake-up-the-dead (&rest args)
5894   "Wake up the dead summary buffer."
5895   (interactive)
5896   (gnus-dead-summary-mode -1)
5897   (let ((name (buffer-name)))
5898     (when (string-match "Dead " name)
5899       (rename-buffer
5900        (concat (substring name 0 (match-beginning 0))
5901                (substring name (match-end 0)))
5902        t)))
5903   (gnus-message 3 "This dead summary is now alive again"))
5904
5905 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5906 (defun gnus-summary-fetch-faq (&optional faq-dir)
5907   "Fetch the FAQ for the current group.
5908 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5909 in."
5910   (interactive
5911    (list
5912     (when current-prefix-arg
5913       (completing-read
5914        "Faq dir: " (and (listp gnus-group-faq-directory)
5915                         (mapcar (lambda (file) (list file))
5916                                 gnus-group-faq-directory))))))
5917   (let (gnus-faq-buffer)
5918     (when (setq gnus-faq-buffer
5919                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5920       (gnus-configure-windows 'summary-faq))))
5921
5922 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5923 (defun gnus-summary-describe-group (&optional force)
5924   "Describe the current newsgroup."
5925   (interactive "P")
5926   (gnus-group-describe-group force gnus-newsgroup-name))
5927
5928 (defun gnus-summary-describe-briefly ()
5929   "Describe summary mode commands briefly."
5930   (interactive)
5931   (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")))
5932
5933 ;; Walking around group mode buffer from summary mode.
5934
5935 (defun gnus-summary-next-group (&optional no-article target-group backward)
5936   "Exit current newsgroup and then select next unread newsgroup.
5937 If prefix argument NO-ARTICLE is non-nil, no article is selected
5938 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5939 previous group instead."
5940   (interactive "P")
5941   ;; Stop pre-fetching.
5942   (gnus-async-halt-prefetch)
5943   (let ((current-group gnus-newsgroup-name)
5944         (current-buffer (current-buffer))
5945         entered)
5946     ;; First we semi-exit this group to update Xrefs and all variables.
5947     ;; We can't do a real exit, because the window conf must remain
5948     ;; the same in case the user is prompted for info, and we don't
5949     ;; want the window conf to change before that...
5950     (gnus-summary-exit t)
5951     (while (not entered)
5952       ;; Then we find what group we are supposed to enter.
5953       (set-buffer gnus-group-buffer)
5954       (gnus-group-jump-to-group current-group)
5955       (setq target-group
5956             (or target-group
5957                 (if (eq gnus-keep-same-level 'best)
5958                     (gnus-summary-best-group gnus-newsgroup-name)
5959                   (gnus-summary-search-group backward gnus-keep-same-level))))
5960       (if (not target-group)
5961           ;; There are no further groups, so we return to the group
5962           ;; buffer.
5963           (progn
5964             (gnus-message 5 "Returning to the group buffer")
5965             (setq entered t)
5966             (when (gnus-buffer-live-p current-buffer)
5967               (set-buffer current-buffer)
5968               (gnus-summary-exit))
5969             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5970         ;; We try to enter the target group.
5971         (gnus-group-jump-to-group target-group)
5972         (let ((unreads (gnus-group-group-unread)))
5973           (if (and (or (eq t unreads)
5974                        (and unreads (not (zerop unreads))))
5975                    (gnus-summary-read-group
5976                     target-group nil no-article
5977                     (and (buffer-name current-buffer) current-buffer)
5978                     nil backward))
5979               (setq entered t)
5980             (setq current-group target-group
5981                   target-group nil)))))))
5982
5983 (defun gnus-summary-prev-group (&optional no-article)
5984   "Exit current newsgroup and then select previous unread newsgroup.
5985 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5986   (interactive "P")
5987   (gnus-summary-next-group no-article nil t))
5988
5989 ;; Walking around summary lines.
5990
5991 (defun gnus-summary-first-subject (&optional unread undownloaded)
5992   "Go to the first unread subject.
5993 If UNREAD is non-nil, go to the first unread article.
5994 Returns the article selected or nil if there are no unread articles."
5995   (interactive "P")
5996   (prog1
5997       (cond
5998        ;; Empty summary.
5999        ((null gnus-newsgroup-data)
6000         (gnus-message 3 "No articles in the group")
6001         nil)
6002        ;; Pick the first article.
6003        ((not unread)
6004         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6005         (gnus-data-number (car gnus-newsgroup-data)))
6006        ;; No unread articles.
6007        ((null gnus-newsgroup-unreads)
6008         (gnus-message 3 "No more unread articles")
6009         nil)
6010        ;; Find the first unread article.
6011        (t
6012         (let ((data gnus-newsgroup-data))
6013           (while (and data
6014                       (and (not (and undownloaded
6015                                      (eq gnus-undownloaded-mark
6016                                          (gnus-data-mark (car data)))))
6017                            (not (gnus-data-unread-p (car data)))))
6018             (setq data (cdr data)))
6019           (when data
6020             (goto-char (gnus-data-pos (car data)))
6021             (gnus-data-number (car data))))))
6022     (gnus-summary-position-point)))
6023
6024 (defun gnus-summary-next-subject (n &optional unread dont-display)
6025   "Go to next N'th summary line.
6026 If N is negative, go to the previous N'th subject line.
6027 If UNREAD is non-nil, only unread articles are selected.
6028 The difference between N and the actual number of steps taken is
6029 returned."
6030   (interactive "p")
6031   (let ((backward (< n 0))
6032         (n (abs n)))
6033     (while (and (> n 0)
6034                 (if backward
6035                     (gnus-summary-find-prev unread)
6036                   (gnus-summary-find-next unread)))
6037       (unless (zerop (setq n (1- n)))
6038         (gnus-summary-show-thread)))
6039     (when (/= 0 n)
6040       (gnus-message 7 "No more%s articles"
6041                     (if unread " unread" "")))
6042     (unless dont-display
6043       (gnus-summary-recenter)
6044       (gnus-summary-position-point))
6045     n))
6046
6047 (defun gnus-summary-next-unread-subject (n)
6048   "Go to next N'th unread summary line."
6049   (interactive "p")
6050   (gnus-summary-next-subject n t))
6051
6052 (defun gnus-summary-prev-subject (n &optional unread)
6053   "Go to previous N'th summary line.
6054 If optional argument UNREAD is non-nil, only unread article is selected."
6055   (interactive "p")
6056   (gnus-summary-next-subject (- n) unread))
6057
6058 (defun gnus-summary-prev-unread-subject (n)
6059   "Go to previous N'th unread summary line."
6060   (interactive "p")
6061   (gnus-summary-next-subject (- n) t))
6062
6063 (defun gnus-summary-goto-subject (article &optional force silent)
6064   "Go the subject line of ARTICLE.
6065 If FORCE, also allow jumping to articles not currently shown."
6066   (interactive "nArticle number: ")
6067   (let ((b (point))
6068         (data (gnus-data-find article)))
6069     ;; We read in the article if we have to.
6070     (and (not data)
6071          force
6072          (gnus-summary-insert-subject
6073           article
6074           (if (or (numberp force) (vectorp force)) force)
6075           t)
6076          (setq data (gnus-data-find article)))
6077     (goto-char b)
6078     (if (not data)
6079         (progn
6080           (unless silent
6081             (gnus-message 3 "Can't find article %d" article))
6082           nil)
6083       (goto-char (gnus-data-pos data))
6084       (gnus-summary-position-point)
6085       article)))
6086
6087 ;; Walking around summary lines with displaying articles.
6088
6089 (defun gnus-summary-expand-window (&optional arg)
6090   "Make the summary buffer take up the entire Emacs frame.
6091 Given a prefix, will force an `article' buffer configuration."
6092   (interactive "P")
6093   (if arg
6094       (gnus-configure-windows 'article 'force)
6095     (gnus-configure-windows 'summary 'force)))
6096
6097 (defun gnus-summary-display-article (article &optional all-header)
6098   "Display ARTICLE in article buffer."
6099   (when (gnus-buffer-live-p gnus-article-buffer)
6100     (with-current-buffer gnus-article-buffer
6101       (mm-enable-multibyte-mule4)))
6102   (gnus-set-global-variables)
6103   (when (gnus-buffer-live-p gnus-article-buffer)
6104     (with-current-buffer gnus-article-buffer
6105       (setq gnus-article-charset gnus-newsgroup-charset)
6106       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6107       (mm-enable-multibyte-mule4)))
6108   (if (null article)
6109       nil
6110     (prog1
6111         (if gnus-summary-display-article-function
6112             (funcall gnus-summary-display-article-function article all-header)
6113           (gnus-article-prepare article all-header))
6114       (gnus-run-hooks 'gnus-select-article-hook)
6115       (when (and gnus-current-article
6116                  (not (zerop gnus-current-article)))
6117         (gnus-summary-goto-subject gnus-current-article))
6118       (gnus-summary-recenter)
6119       (when (and gnus-use-trees gnus-show-threads)
6120         (gnus-possibly-generate-tree article)
6121         (gnus-highlight-selected-tree article))
6122       ;; Successfully display article.
6123       (gnus-article-set-window-start
6124        (cdr (assq article gnus-newsgroup-bookmarks))))))
6125
6126 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6127   "Select the current article.
6128 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6129 non-nil, the article will be re-fetched even if it already present in
6130 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6131 be displayed."
6132   ;; Make sure we are in the summary buffer to work around bbdb bug.
6133   (unless (eq major-mode 'gnus-summary-mode)
6134     (set-buffer gnus-summary-buffer))
6135   (let ((article (or article (gnus-summary-article-number)))
6136         (all-headers (not (not all-headers))) ;Must be T or NIL.
6137         gnus-summary-display-article-function)
6138     (and (not pseudo)
6139          (gnus-summary-article-pseudo-p article)
6140          (error "This is a pseudo-article"))
6141     (save-excursion
6142       (set-buffer gnus-summary-buffer)
6143       (if (or (and gnus-single-article-buffer
6144                    (or (null gnus-current-article)
6145                        (null gnus-article-current)
6146                        (null (get-buffer gnus-article-buffer))
6147                        (not (eq article (cdr gnus-article-current)))
6148                        (not (equal (car gnus-article-current)
6149                                    gnus-newsgroup-name))))
6150               (and (not gnus-single-article-buffer)
6151                    (or (null gnus-current-article)
6152                        (not (eq gnus-current-article article))))
6153               force)
6154           ;; The requested article is different from the current article.
6155           (progn
6156             (gnus-summary-display-article article all-headers)
6157             (when (gnus-buffer-live-p gnus-article-buffer)
6158                (with-current-buffer gnus-article-buffer
6159                 (if (not gnus-article-decoded-p) ;; a local variable
6160                     (mm-disable-multibyte-mule4))))
6161             (when (or all-headers gnus-show-all-headers)
6162               (gnus-article-show-all-headers))
6163             (gnus-article-set-window-start
6164              (cdr (assq article gnus-newsgroup-bookmarks)))
6165             article)
6166         (when (or all-headers gnus-show-all-headers)
6167           (gnus-article-show-all-headers))
6168         'old))))
6169
6170 (defun gnus-summary-force-verify-and-decrypt ()
6171   (interactive)
6172   (let ((mm-verify-option 'known)
6173         (mm-decrypt-option 'known))
6174     (gnus-summary-select-article nil 'force)))
6175
6176 (defun gnus-summary-set-current-mark (&optional current-mark)
6177   "Obsolete function."
6178   nil)
6179
6180 (defun gnus-summary-next-article (&optional unread subject backward push)
6181   "Select the next article.
6182 If UNREAD, only unread articles are selected.
6183 If SUBJECT, only articles with SUBJECT are selected.
6184 If BACKWARD, the previous article is selected instead of the next."
6185   (interactive "P")
6186   (cond
6187    ;; Is there such an article?
6188    ((and (gnus-summary-search-forward unread subject backward)
6189          (or (gnus-summary-display-article (gnus-summary-article-number))
6190              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6191     (gnus-summary-position-point))
6192    ;; If not, we try the first unread, if that is wanted.
6193    ((and subject
6194          gnus-auto-select-same
6195          (gnus-summary-first-unread-article))
6196     (gnus-summary-position-point)
6197     (gnus-message 6 "Wrapped"))
6198    ;; Try to get next/previous article not displayed in this group.
6199    ((and gnus-auto-extend-newsgroup
6200          (not unread) (not subject))
6201     (gnus-summary-goto-article
6202      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6203      nil (count-lines (point-min) (point))))
6204    ;; Go to next/previous group.
6205    (t
6206     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6207       (gnus-summary-jump-to-group gnus-newsgroup-name))
6208     (let ((cmd last-command-char)
6209           (point
6210            (save-excursion
6211              (set-buffer gnus-group-buffer)
6212              (point)))
6213           (group
6214            (if (eq gnus-keep-same-level 'best)
6215                (gnus-summary-best-group gnus-newsgroup-name)
6216              (gnus-summary-search-group backward gnus-keep-same-level))))
6217       ;; For some reason, the group window gets selected.  We change
6218       ;; it back.
6219       (select-window (get-buffer-window (current-buffer)))
6220       ;; Select next unread newsgroup automagically.
6221       (cond
6222        ((or (not gnus-auto-select-next)
6223             (not cmd))
6224         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6225        ((or (eq gnus-auto-select-next 'quietly)
6226             (and (eq gnus-auto-select-next 'slightly-quietly)
6227                  push)
6228             (and (eq gnus-auto-select-next 'almost-quietly)
6229                  (gnus-summary-last-article-p)))
6230         ;; Select quietly.
6231         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6232             (gnus-summary-exit)
6233           (gnus-message 7 "No more%s articles (%s)..."
6234                         (if unread " unread" "")
6235                         (if group (concat "selecting " group)
6236                           "exiting"))
6237           (gnus-summary-next-group nil group backward)))
6238        (t
6239         (when (gnus-key-press-event-p last-input-event)
6240           (gnus-summary-walk-group-buffer
6241            gnus-newsgroup-name cmd unread backward point))))))))
6242
6243 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6244   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6245                       (?\C-p (gnus-group-prev-unread-group 1))))
6246         (cursor-in-echo-area t)
6247         keve key group ended)
6248     (save-excursion
6249       (set-buffer gnus-group-buffer)
6250       (goto-char start)
6251       (setq group
6252             (if (eq gnus-keep-same-level 'best)
6253                 (gnus-summary-best-group gnus-newsgroup-name)
6254               (gnus-summary-search-group backward gnus-keep-same-level))))
6255     (while (not ended)
6256       (gnus-message
6257        5 "No more%s articles%s" (if unread " unread" "")
6258        (if (and group
6259                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6260            (format " (Type %s for %s [%s])"
6261                    (single-key-description cmd) group
6262                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6263          (format " (Type %s to exit %s)"
6264                  (single-key-description cmd)
6265                  gnus-newsgroup-name)))
6266       ;; Confirm auto selection.
6267       (setq key (car (setq keve (gnus-read-event-char))))
6268       (setq ended t)
6269       (cond
6270        ((assq key keystrokes)
6271         (let ((obuf (current-buffer)))
6272           (switch-to-buffer gnus-group-buffer)
6273           (when group
6274             (gnus-group-jump-to-group group))
6275           (eval (cadr (assq key keystrokes)))
6276           (setq group (gnus-group-group-name))
6277           (switch-to-buffer obuf))
6278         (setq ended nil))
6279        ((equal key cmd)
6280         (if (or (not group)
6281                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6282             (gnus-summary-exit)
6283           (gnus-summary-next-group nil group backward)))
6284        (t
6285         (push (cdr keve) unread-command-events))))))
6286
6287 (defun gnus-summary-next-unread-article ()
6288   "Select unread article after current one."
6289   (interactive)
6290   (gnus-summary-next-article
6291    (or (not (eq gnus-summary-goto-unread 'never))
6292        (gnus-summary-last-article-p (gnus-summary-article-number)))
6293    (and gnus-auto-select-same
6294         (gnus-summary-article-subject))))
6295
6296 (defun gnus-summary-prev-article (&optional unread subject)
6297   "Select the article after the current one.
6298 If UNREAD is non-nil, only unread articles are selected."
6299   (interactive "P")
6300   (gnus-summary-next-article unread subject t))
6301
6302 (defun gnus-summary-prev-unread-article ()
6303   "Select unread article before current one."
6304   (interactive)
6305   (gnus-summary-prev-article
6306    (or (not (eq gnus-summary-goto-unread 'never))
6307        (gnus-summary-first-article-p (gnus-summary-article-number)))
6308    (and gnus-auto-select-same
6309         (gnus-summary-article-subject))))
6310
6311 (defun gnus-summary-next-page (&optional lines circular)
6312   "Show next page of the selected article.
6313 If at the end of the current article, select the next article.
6314 LINES says how many lines should be scrolled up.
6315
6316 If CIRCULAR is non-nil, go to the start of the article instead of
6317 selecting the next article when reaching the end of the current
6318 article."
6319   (interactive "P")
6320   (setq gnus-summary-buffer (current-buffer))
6321   (gnus-set-global-variables)
6322   (let ((article (gnus-summary-article-number))
6323         (article-window (get-buffer-window gnus-article-buffer t))
6324         endp)
6325     ;; If the buffer is empty, we have no article.
6326     (unless article
6327       (error "No article to select"))
6328     (gnus-configure-windows 'article)
6329     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6330         (if (and (eq gnus-summary-goto-unread 'never)
6331                  (not (gnus-summary-last-article-p article)))
6332             (gnus-summary-next-article)
6333           (gnus-summary-next-unread-article))
6334       (if (or (null gnus-current-article)
6335               (null gnus-article-current)
6336               (/= article (cdr gnus-article-current))
6337               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6338           ;; Selected subject is different from current article's.
6339           (gnus-summary-display-article article)
6340         (when article-window
6341           (gnus-eval-in-buffer-window gnus-article-buffer
6342             (setq endp (gnus-article-next-page lines)))
6343           (when endp
6344             (cond (circular
6345                    (gnus-summary-beginning-of-article))
6346                   (lines
6347                    (gnus-message 3 "End of message"))
6348                   ((null lines)
6349                    (if (and (eq gnus-summary-goto-unread 'never)
6350                             (not (gnus-summary-last-article-p article)))
6351                        (gnus-summary-next-article)
6352                      (gnus-summary-next-unread-article))))))))
6353     (gnus-summary-recenter)
6354     (gnus-summary-position-point)))
6355
6356 (defun gnus-summary-prev-page (&optional lines move)
6357   "Show previous page of selected article.
6358 Argument LINES specifies lines to be scrolled down.
6359 If MOVE, move to the previous unread article if point is at
6360 the beginning of the buffer."
6361   (interactive "P")
6362   (let ((article (gnus-summary-article-number))
6363         (article-window (get-buffer-window gnus-article-buffer t))
6364         endp)
6365     (gnus-configure-windows 'article)
6366     (if (or (null gnus-current-article)
6367             (null gnus-article-current)
6368             (/= article (cdr gnus-article-current))
6369             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6370         ;; Selected subject is different from current article's.
6371         (gnus-summary-display-article article)
6372       (gnus-summary-recenter)
6373       (when article-window
6374         (gnus-eval-in-buffer-window gnus-article-buffer
6375           (setq endp (gnus-article-prev-page lines)))
6376         (when (and move endp)
6377           (cond (lines
6378                  (gnus-message 3 "Beginning of message"))
6379                 ((null lines)
6380                  (if (and (eq gnus-summary-goto-unread 'never)
6381                           (not (gnus-summary-first-article-p article)))
6382                      (gnus-summary-prev-article)
6383                    (gnus-summary-prev-unread-article))))))))
6384   (gnus-summary-position-point))
6385
6386 (defun gnus-summary-prev-page-or-article (&optional lines)
6387   "Show previous page of selected article.
6388 Argument LINES specifies lines to be scrolled down.
6389 If at the beginning of the article, go to the next article."
6390   (interactive "P")
6391   (gnus-summary-prev-page lines t))
6392
6393 (defun gnus-summary-scroll-up (lines)
6394   "Scroll up (or down) one line current article.
6395 Argument LINES specifies lines to be scrolled up (or down if negative)."
6396   (interactive "p")
6397   (gnus-configure-windows 'article)
6398   (gnus-summary-show-thread)
6399   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6400     (gnus-eval-in-buffer-window gnus-article-buffer
6401       (cond ((> lines 0)
6402              (when (gnus-article-next-page lines)
6403                (gnus-message 3 "End of message")))
6404             ((< lines 0)
6405              (gnus-article-prev-page (- lines))))))
6406   (gnus-summary-recenter)
6407   (gnus-summary-position-point))
6408
6409 (defun gnus-summary-scroll-down (lines)
6410   "Scroll down (or up) one line current article.
6411 Argument LINES specifies lines to be scrolled down (or up if negative)."
6412   (interactive "p")
6413   (gnus-summary-scroll-up (- lines)))
6414
6415 (defun gnus-summary-next-same-subject ()
6416   "Select next article which has the same subject as current one."
6417   (interactive)
6418   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6419
6420 (defun gnus-summary-prev-same-subject ()
6421   "Select previous article which has the same subject as current one."
6422   (interactive)
6423   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6424
6425 (defun gnus-summary-next-unread-same-subject ()
6426   "Select next unread article which has the same subject as current one."
6427   (interactive)
6428   (gnus-summary-next-article t (gnus-summary-article-subject)))
6429
6430 (defun gnus-summary-prev-unread-same-subject ()
6431   "Select previous unread article which has the same subject as current one."
6432   (interactive)
6433   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6434
6435 (defun gnus-summary-first-unread-article ()
6436   "Select the first unread article.
6437 Return nil if there are no unread articles."
6438   (interactive)
6439   (prog1
6440       (when (gnus-summary-first-subject t)
6441         (gnus-summary-show-thread)
6442         (gnus-summary-first-subject t)
6443         (gnus-summary-display-article (gnus-summary-article-number)))
6444     (gnus-summary-position-point)))
6445
6446 (defun gnus-summary-first-unread-subject ()
6447   "Place the point on the subject line of the first unread article.
6448 Return nil if there are no unread articles."
6449   (interactive)
6450   (prog1
6451       (when (gnus-summary-first-subject t)
6452         (gnus-summary-show-thread)
6453         (gnus-summary-first-subject t))
6454     (gnus-summary-position-point)))
6455
6456 (defun gnus-summary-first-article ()
6457   "Select the first article.
6458 Return nil if there are no articles."
6459   (interactive)
6460   (prog1
6461       (when (gnus-summary-first-subject)
6462         (gnus-summary-show-thread)
6463         (gnus-summary-first-subject)
6464         (gnus-summary-display-article (gnus-summary-article-number)))
6465     (gnus-summary-position-point)))
6466
6467 (defun gnus-summary-best-unread-article ()
6468   "Select the unread article with the highest score."
6469   (interactive)
6470   (let ((best -1000000)
6471         (data gnus-newsgroup-data)
6472         article score)
6473     (while data
6474       (and (gnus-data-unread-p (car data))
6475            (> (setq score
6476                     (gnus-summary-article-score (gnus-data-number (car data))))
6477               best)
6478            (setq best score
6479                  article (gnus-data-number (car data))))
6480       (setq data (cdr data)))
6481     (prog1
6482         (if article
6483             (gnus-summary-goto-article article)
6484           (error "No unread articles"))
6485       (gnus-summary-position-point))))
6486
6487 (defun gnus-summary-last-subject ()
6488   "Go to the last displayed subject line in the group."
6489   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6490     (when article
6491       (gnus-summary-goto-subject article))))
6492
6493 (defun gnus-summary-goto-article (article &optional all-headers force)
6494   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6495 If ALL-HEADERS is non-nil, no header lines are hidden.
6496 If FORCE, go to the article even if it isn't displayed.  If FORCE
6497 is a number, it is the line the article is to be displayed on."
6498   (interactive
6499    (list
6500     (completing-read
6501      "Article number or Message-ID: "
6502      (mapcar (lambda (number) (list (int-to-string number)))
6503              gnus-newsgroup-limit))
6504     current-prefix-arg
6505     t))
6506   (prog1
6507       (if (and (stringp article)
6508                (string-match "@" article))
6509           (gnus-summary-refer-article article)
6510         (when (stringp article)
6511           (setq article (string-to-number article)))
6512         (if (gnus-summary-goto-subject article force)
6513             (gnus-summary-display-article article all-headers)
6514           (gnus-message 4 "Couldn't go to article %s" article) nil))
6515     (gnus-summary-position-point)))
6516
6517 (defun gnus-summary-goto-last-article ()
6518   "Go to the previously read article."
6519   (interactive)
6520   (prog1
6521       (when gnus-last-article
6522         (gnus-summary-goto-article gnus-last-article nil t))
6523     (gnus-summary-position-point)))
6524
6525 (defun gnus-summary-pop-article (number)
6526   "Pop one article off the history and go to the previous.
6527 NUMBER articles will be popped off."
6528   (interactive "p")
6529   (let (to)
6530     (setq gnus-newsgroup-history
6531           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6532     (if to
6533         (gnus-summary-goto-article (car to) nil t)
6534       (error "Article history empty")))
6535   (gnus-summary-position-point))
6536
6537 ;; Summary commands and functions for limiting the summary buffer.
6538
6539 (defun gnus-summary-limit-to-articles (n)
6540   "Limit the summary buffer to the next N articles.
6541 If not given a prefix, use the process marked articles instead."
6542   (interactive "P")
6543   (prog1
6544       (let ((articles (gnus-summary-work-articles n)))
6545         (setq gnus-newsgroup-processable nil)
6546         (gnus-summary-limit articles))
6547     (gnus-summary-position-point)))
6548
6549 (defun gnus-summary-pop-limit (&optional total)
6550   "Restore the previous limit.
6551 If given a prefix, remove all limits."
6552   (interactive "P")
6553   (when total
6554     (setq gnus-newsgroup-limits
6555           (list (mapcar (lambda (h) (mail-header-number h))
6556                         gnus-newsgroup-headers))))
6557   (unless gnus-newsgroup-limits
6558     (error "No limit to pop"))
6559   (prog1
6560       (gnus-summary-limit nil 'pop)
6561     (gnus-summary-position-point)))
6562
6563 (defun gnus-summary-limit-to-subject (subject &optional header)
6564   "Limit the summary buffer to articles that have subjects that match a regexp."
6565   (interactive "sLimit to subject (regexp): ")
6566   (unless header
6567     (setq header "subject"))
6568   (when (not (equal "" subject))
6569     (prog1
6570         (let ((articles (gnus-summary-find-matching
6571                          (or header "subject") subject 'all)))
6572           (unless articles
6573             (error "Found no matches for \"%s\"" subject))
6574           (gnus-summary-limit articles))
6575       (gnus-summary-position-point))))
6576
6577 (defun gnus-summary-limit-to-author (from)
6578   "Limit the summary buffer to articles that have authors that match a regexp."
6579   (interactive "sLimit to author (regexp): ")
6580   (gnus-summary-limit-to-subject from "from"))
6581
6582 (defun gnus-summary-limit-to-age (age &optional younger-p)
6583   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6584 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6585 articles that are younger than AGE days."
6586   (interactive
6587    (let ((younger current-prefix-arg)
6588          (days-got nil)
6589          days)
6590      (while (not days-got)
6591        (setq days (if younger
6592                       (read-string "Limit to articles within (in days): ")
6593                     (read-string "Limit to articles older than (in days): ")))
6594        (when (> (length days) 0)
6595          (setq days (read days)))
6596        (if (numberp days)
6597            (setq days-got t)
6598          (message "Please enter a number.")
6599          (sleep-for 1)))
6600      (list days younger)))
6601   (prog1
6602       (let ((data gnus-newsgroup-data)
6603             (cutoff (days-to-time age))
6604             articles d date is-younger)
6605         (while (setq d (pop data))
6606           (when (and (vectorp (gnus-data-header d))
6607                      (setq date (mail-header-date (gnus-data-header d))))
6608             (setq is-younger (time-less-p
6609                               (time-since (condition-case ()
6610                                               (date-to-time date)
6611                                             (error '(0 0))))
6612                               cutoff))
6613             (when (if younger-p
6614                       is-younger
6615                     (not is-younger))
6616               (push (gnus-data-number d) articles))))
6617         (gnus-summary-limit (nreverse articles)))
6618     (gnus-summary-position-point)))
6619
6620 (defun gnus-summary-limit-to-extra (header regexp)
6621   "Limit the summary buffer to articles that match an 'extra' header."
6622   (interactive
6623    (let ((header
6624           (intern
6625            (gnus-completing-read
6626             (symbol-name (car gnus-extra-headers))
6627             "Limit extra header:"
6628             (mapcar (lambda (x)
6629                       (cons (symbol-name x) x))
6630                     gnus-extra-headers)
6631             nil
6632             t))))
6633      (list header
6634            (read-string (format "Limit to header %s (regexp): " header)))))
6635   (when (not (equal "" regexp))
6636     (prog1
6637         (let ((articles (gnus-summary-find-matching
6638                          (cons 'extra header) regexp 'all)))
6639           (unless articles
6640             (error "Found no matches for \"%s\"" regexp))
6641           (gnus-summary-limit articles))
6642       (gnus-summary-position-point))))
6643
6644 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6645 (make-obsolete
6646  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6647
6648 (defun gnus-summary-limit-to-unread (&optional all)
6649   "Limit the summary buffer to articles that are not marked as read.
6650 If ALL is non-nil, limit strictly to unread articles."
6651   (interactive "P")
6652   (if all
6653       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6654     (gnus-summary-limit-to-marks
6655      ;; Concat all the marks that say that an article is read and have
6656      ;; those removed.
6657      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6658            gnus-killed-mark gnus-kill-file-mark
6659            gnus-low-score-mark gnus-expirable-mark
6660            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6661            gnus-duplicate-mark gnus-souped-mark)
6662      'reverse)))
6663
6664 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6665 (make-obsolete 'gnus-summary-delete-marked-with
6666                'gnus-summary-limit-exlude-marks)
6667
6668 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6669   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6670 If REVERSE, limit the summary buffer to articles that are marked
6671 with MARKS.  MARKS can either be a string of marks or a list of marks.
6672 Returns how many articles were removed."
6673   (interactive "sMarks: ")
6674   (gnus-summary-limit-to-marks marks t))
6675
6676 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6677   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6678 If REVERSE (the prefix), limit the summary buffer to articles that are
6679 not marked with MARKS.  MARKS can either be a string of marks or a
6680 list of marks.
6681 Returns how many articles were removed."
6682   (interactive "sMarks: \nP")
6683   (prog1
6684       (let ((data gnus-newsgroup-data)
6685             (marks (if (listp marks) marks
6686                      (append marks nil))) ; Transform to list.
6687             articles)
6688         (while data
6689           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6690                   (memq (gnus-data-mark (car data)) marks))
6691             (push (gnus-data-number (car data)) articles))
6692           (setq data (cdr data)))
6693         (gnus-summary-limit articles))
6694     (gnus-summary-position-point)))
6695
6696 (defun gnus-summary-limit-to-score (&optional score)
6697   "Limit to articles with score at or above SCORE."
6698   (interactive "P")
6699   (setq score (if score
6700                   (prefix-numeric-value score)
6701                 (or gnus-summary-default-score 0)))
6702   (let ((data gnus-newsgroup-data)
6703         articles)
6704     (while data
6705       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6706                 score)
6707         (push (gnus-data-number (car data)) articles))
6708       (setq data (cdr data)))
6709     (prog1
6710         (gnus-summary-limit articles)
6711       (gnus-summary-position-point))))
6712
6713 (defun gnus-summary-limit-include-thread (id)
6714   "Display all the hidden articles that in the current thread."
6715   (interactive (list (mail-header-id (gnus-summary-article-header))))
6716   (let ((articles (gnus-articles-in-thread
6717                    (gnus-id-to-thread (gnus-root-id id)))))
6718     (prog1
6719         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6720       (gnus-summary-position-point))))
6721
6722 (defun gnus-summary-limit-include-dormant ()
6723   "Display all the hidden articles that are marked as dormant.
6724 Note that this command only works on a subset of the articles currently
6725 fetched for this group."
6726   (interactive)
6727   (unless gnus-newsgroup-dormant
6728     (error "There are no dormant articles in this group"))
6729   (prog1
6730       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6731     (gnus-summary-position-point)))
6732
6733 (defun gnus-summary-limit-exclude-dormant ()
6734   "Hide all dormant articles."
6735   (interactive)
6736   (prog1
6737       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6738     (gnus-summary-position-point)))
6739
6740 (defun gnus-summary-limit-exclude-childless-dormant ()
6741   "Hide all dormant articles that have no children."
6742   (interactive)
6743   (let ((data (gnus-data-list t))
6744         articles d children)
6745     ;; Find all articles that are either not dormant or have
6746     ;; children.
6747     (while (setq d (pop data))
6748       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6749                 (and (setq children
6750                            (gnus-article-children (gnus-data-number d)))
6751                      (let (found)
6752                        (while children
6753                          (when (memq (car children) articles)
6754                            (setq children nil
6755                                  found t))
6756                          (pop children))
6757                        found)))
6758         (push (gnus-data-number d) articles)))
6759     ;; Do the limiting.
6760     (prog1
6761         (gnus-summary-limit articles)
6762       (gnus-summary-position-point))))
6763
6764 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6765   "Mark all unread excluded articles as read.
6766 If ALL, mark even excluded ticked and dormants as read."
6767   (interactive "P")
6768   (let ((articles (gnus-sorted-complement
6769                    (sort
6770                     (mapcar (lambda (h) (mail-header-number h))
6771                             gnus-newsgroup-headers)
6772                     '<)
6773                    (sort gnus-newsgroup-limit '<)))
6774         article)
6775     (setq gnus-newsgroup-unreads
6776           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6777     (if all
6778         (setq gnus-newsgroup-dormant nil
6779               gnus-newsgroup-marked nil
6780               gnus-newsgroup-reads
6781               (nconc
6782                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6783                gnus-newsgroup-reads))
6784       (while (setq article (pop articles))
6785         (unless (or (memq article gnus-newsgroup-dormant)
6786                     (memq article gnus-newsgroup-marked))
6787           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6788
6789 (defun gnus-summary-limit (articles &optional pop)
6790   (if pop
6791       ;; We pop the previous limit off the stack and use that.
6792       (setq articles (car gnus-newsgroup-limits)
6793             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6794     ;; We use the new limit, so we push the old limit on the stack.
6795     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6796   ;; Set the limit.
6797   (setq gnus-newsgroup-limit articles)
6798   (let ((total (length gnus-newsgroup-data))
6799         (data (gnus-data-find-list (gnus-summary-article-number)))
6800         (gnus-summary-mark-below nil)   ; Inhibit this.
6801         found)
6802     ;; This will do all the work of generating the new summary buffer
6803     ;; according to the new limit.
6804     (gnus-summary-prepare)
6805     ;; Hide any threads, possibly.
6806     (and gnus-show-threads
6807          gnus-thread-hide-subtree
6808          (gnus-summary-hide-all-threads))
6809     ;; Try to return to the article you were at, or one in the
6810     ;; neighborhood.
6811     (when data
6812       ;; We try to find some article after the current one.
6813       (while data
6814         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6815           (setq data nil
6816                 found t))
6817         (setq data (cdr data))))
6818     (unless found
6819       ;; If there is no data, that means that we were after the last
6820       ;; article.  The same goes when we can't find any articles
6821       ;; after the current one.
6822       (goto-char (point-max))
6823       (gnus-summary-find-prev))
6824     (gnus-set-mode-line 'summary)
6825     ;; We return how many articles were removed from the summary
6826     ;; buffer as a result of the new limit.
6827     (- total (length gnus-newsgroup-data))))
6828
6829 (defsubst gnus-invisible-cut-children (threads)
6830   (let ((num 0))
6831     (while threads
6832       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6833         (incf num))
6834       (pop threads))
6835     (< num 2)))
6836
6837 (defsubst gnus-cut-thread (thread)
6838   "Go forwards in the thread until we find an article that we want to display."
6839   (when (or (eq gnus-fetch-old-headers 'some)
6840             (eq gnus-fetch-old-headers 'invisible)
6841             (numberp gnus-fetch-old-headers)
6842             (eq gnus-build-sparse-threads 'some)
6843             (eq gnus-build-sparse-threads 'more))
6844     ;; Deal with old-fetched headers and sparse threads.
6845     (while (and
6846             thread
6847             (or
6848              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6849              (gnus-summary-article-ancient-p
6850               (mail-header-number (car thread))))
6851             (if (or (<= (length (cdr thread)) 1)
6852                     (eq gnus-fetch-old-headers 'invisible))
6853                 (setq gnus-newsgroup-limit
6854                       (delq (mail-header-number (car thread))
6855                             gnus-newsgroup-limit)
6856                       thread (cadr thread))
6857               (when (gnus-invisible-cut-children (cdr thread))
6858                 (let ((th (cdr thread)))
6859                   (while th
6860                     (if (memq (mail-header-number (caar th))
6861                               gnus-newsgroup-limit)
6862                         (setq thread (car th)
6863                               th nil)
6864                       (setq th (cdr th))))))))))
6865   thread)
6866
6867 (defun gnus-cut-threads (threads)
6868   "Cut off all uninteresting articles from the beginning of threads."
6869   (when (or (eq gnus-fetch-old-headers 'some)
6870             (eq gnus-fetch-old-headers 'invisible)
6871             (numberp gnus-fetch-old-headers)
6872             (eq gnus-build-sparse-threads 'some)
6873             (eq gnus-build-sparse-threads 'more))
6874     (let ((th threads))
6875       (while th
6876         (setcar th (gnus-cut-thread (car th)))
6877         (setq th (cdr th)))))
6878   ;; Remove nixed out threads.
6879   (delq nil threads))
6880
6881 (defun gnus-summary-initial-limit (&optional show-if-empty)
6882   "Figure out what the initial limit is supposed to be on group entry.
6883 This entails weeding out unwanted dormants, low-scored articles,
6884 fetch-old-headers verbiage, and so on."
6885   ;; Most groups have nothing to remove.
6886   (if (or gnus-inhibit-limiting
6887           (and (null gnus-newsgroup-dormant)
6888                (not (eq gnus-fetch-old-headers 'some))
6889                (not (numberp gnus-fetch-old-headers))
6890                (not (eq gnus-fetch-old-headers 'invisible))
6891                (null gnus-summary-expunge-below)
6892                (not (eq gnus-build-sparse-threads 'some))
6893                (not (eq gnus-build-sparse-threads 'more))
6894                (null gnus-thread-expunge-below)
6895                (not gnus-use-nocem)))
6896       ()                                ; Do nothing.
6897     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6898     (setq gnus-newsgroup-limit nil)
6899     (mapatoms
6900      (lambda (node)
6901        (unless (car (symbol-value node))
6902          ;; These threads have no parents -- they are roots.
6903          (let ((nodes (cdr (symbol-value node)))
6904                thread)
6905            (while nodes
6906              (if (and gnus-thread-expunge-below
6907                       (< (gnus-thread-total-score (car nodes))
6908                          gnus-thread-expunge-below))
6909                  (gnus-expunge-thread (pop nodes))
6910                (setq thread (pop nodes))
6911                (gnus-summary-limit-children thread))))))
6912      gnus-newsgroup-dependencies)
6913     ;; If this limitation resulted in an empty group, we might
6914     ;; pop the previous limit and use it instead.
6915     (when (and (not gnus-newsgroup-limit)
6916                show-if-empty)
6917       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6918     gnus-newsgroup-limit))
6919
6920 (defun gnus-summary-limit-children (thread)
6921   "Return 1 if this subthread is visible and 0 if it is not."
6922   ;; First we get the number of visible children to this thread.  This
6923   ;; is done by recursing down the thread using this function, so this
6924   ;; will really go down to a leaf article first, before slowly
6925   ;; working its way up towards the root.
6926   (when thread
6927     (let ((children
6928            (if (cdr thread)
6929                (apply '+ (mapcar 'gnus-summary-limit-children
6930                                  (cdr thread)))
6931              0))
6932           (number (mail-header-number (car thread)))
6933           score)
6934       (if (and
6935            (not (memq number gnus-newsgroup-marked))
6936            (or
6937             ;; If this article is dormant and has absolutely no visible
6938             ;; children, then this article isn't visible.
6939             (and (memq number gnus-newsgroup-dormant)
6940                  (zerop children))
6941             ;; If this is "fetch-old-headered" and there is no
6942             ;; visible children, then we don't want this article.
6943             (and (or (eq gnus-fetch-old-headers 'some)
6944                      (numberp gnus-fetch-old-headers))
6945                  (gnus-summary-article-ancient-p number)
6946                  (zerop children))
6947             ;; If this is "fetch-old-headered" and `invisible', then
6948             ;; we don't want this article.
6949             (and (eq gnus-fetch-old-headers 'invisible)
6950                  (gnus-summary-article-ancient-p number))
6951             ;; If this is a sparsely inserted article with no children,
6952             ;; we don't want it.
6953             (and (eq gnus-build-sparse-threads 'some)
6954                  (gnus-summary-article-sparse-p number)
6955                  (zerop children))
6956             ;; If we use expunging, and this article is really
6957             ;; low-scored, then we don't want this article.
6958             (when (and gnus-summary-expunge-below
6959                        (< (setq score
6960                                 (or (cdr (assq number gnus-newsgroup-scored))
6961                                     gnus-summary-default-score))
6962                           gnus-summary-expunge-below))
6963               ;; We increase the expunge-tally here, but that has
6964               ;; nothing to do with the limits, really.
6965               (incf gnus-newsgroup-expunged-tally)
6966               ;; We also mark as read here, if that's wanted.
6967               (when (and gnus-summary-mark-below
6968                          (< score gnus-summary-mark-below))
6969                 (setq gnus-newsgroup-unreads
6970                       (delq number gnus-newsgroup-unreads))
6971                 (if gnus-newsgroup-auto-expire
6972                     (push number gnus-newsgroup-expirable)
6973                   (push (cons number gnus-low-score-mark)
6974                         gnus-newsgroup-reads)))
6975               t)
6976             ;; Check NoCeM things.
6977             (if (and gnus-use-nocem
6978                      (gnus-nocem-unwanted-article-p
6979                       (mail-header-id (car thread))))
6980                 (progn
6981                   (setq gnus-newsgroup-unreads
6982                         (delq number gnus-newsgroup-unreads))
6983                   t))))
6984           ;; Nope, invisible article.
6985           0
6986         ;; Ok, this article is to be visible, so we add it to the limit
6987         ;; and return 1.
6988         (push number gnus-newsgroup-limit)
6989         1))))
6990
6991 (defun gnus-expunge-thread (thread)
6992   "Mark all articles in THREAD as read."
6993   (let* ((number (mail-header-number (car thread))))
6994     (incf gnus-newsgroup-expunged-tally)
6995     ;; We also mark as read here, if that's wanted.
6996     (setq gnus-newsgroup-unreads
6997           (delq number gnus-newsgroup-unreads))
6998     (if gnus-newsgroup-auto-expire
6999         (push number gnus-newsgroup-expirable)
7000       (push (cons number gnus-low-score-mark)
7001             gnus-newsgroup-reads)))
7002   ;; Go recursively through all subthreads.
7003   (mapcar 'gnus-expunge-thread (cdr thread)))
7004
7005 ;; Summary article oriented commands
7006
7007 (defun gnus-summary-refer-parent-article (n)
7008   "Refer parent article N times.
7009 If N is negative, go to ancestor -N instead.
7010 The difference between N and the number of articles fetched is returned."
7011   (interactive "p")
7012   (let ((skip 1)
7013         error header ref)
7014     (when (not (natnump n))
7015       (setq skip (abs n)
7016             n 1))
7017     (while (and (> n 0)
7018                 (not error))
7019       (setq header (gnus-summary-article-header))
7020       (if (and (eq (mail-header-number header)
7021                    (cdr gnus-article-current))
7022                (equal gnus-newsgroup-name
7023                       (car gnus-article-current)))
7024           ;; If we try to find the parent of the currently
7025           ;; displayed article, then we take a look at the actual
7026           ;; References header, since this is slightly more
7027           ;; reliable than the References field we got from the
7028           ;; server.
7029           (save-excursion
7030             (set-buffer gnus-original-article-buffer)
7031             (nnheader-narrow-to-headers)
7032             (unless (setq ref (message-fetch-field "references"))
7033               (setq ref (message-fetch-field "in-reply-to")))
7034             (widen))
7035         (setq ref
7036               ;; It's not the current article, so we take a bet on
7037               ;; the value we got from the server.
7038               (mail-header-references header)))
7039       (if (and ref
7040                (not (equal ref "")))
7041           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7042             (gnus-message 1 "Couldn't find parent"))
7043         (gnus-message 1 "No references in article %d"
7044                       (gnus-summary-article-number))
7045         (setq error t))
7046       (decf n))
7047     (gnus-summary-position-point)
7048     n))
7049
7050 (defun gnus-summary-refer-references ()
7051   "Fetch all articles mentioned in the References header.
7052 Return the number of articles fetched."
7053   (interactive)
7054   (let ((ref (mail-header-references (gnus-summary-article-header)))
7055         (current (gnus-summary-article-number))
7056         (n 0))
7057     (if (or (not ref)
7058             (equal ref ""))
7059         (error "No References in the current article")
7060       ;; For each Message-ID in the References header...
7061       (while (string-match "<[^>]*>" ref)
7062         (incf n)
7063         ;; ... fetch that article.
7064         (gnus-summary-refer-article
7065          (prog1 (match-string 0 ref)
7066            (setq ref (substring ref (match-end 0))))))
7067       (gnus-summary-goto-subject current)
7068       (gnus-summary-position-point)
7069       n)))
7070
7071 (defun gnus-summary-refer-thread (&optional limit)
7072   "Fetch all articles in the current thread.
7073 If LIMIT (the numerical prefix), fetch that many old headers instead
7074 of what's specified by the `gnus-refer-thread-limit' variable."
7075   (interactive "P")
7076   (let ((id (mail-header-id (gnus-summary-article-header)))
7077         (limit (if limit (prefix-numeric-value limit)
7078                  gnus-refer-thread-limit)))
7079     ;; We want to fetch LIMIT *old* headers, but we also have to
7080     ;; re-fetch all the headers in the current buffer, because many of
7081     ;; them may be undisplayed.  So we adjust LIMIT.
7082     (when (numberp limit)
7083       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7084     (unless (eq gnus-fetch-old-headers 'invisible)
7085       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7086       ;; Retrieve the headers and read them in.
7087       (if (eq (gnus-retrieve-headers
7088                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7089               'nov)
7090           (gnus-build-all-threads)
7091         (error "Can't fetch thread from backends that don't support NOV"))
7092       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7093     (gnus-summary-limit-include-thread id)))
7094
7095 (defun gnus-summary-refer-article (message-id)
7096   "Fetch an article specified by MESSAGE-ID."
7097   (interactive "sMessage-ID: ")
7098   (when (and (stringp message-id)
7099              (not (zerop (length message-id))))
7100     ;; Construct the correct Message-ID if necessary.
7101     ;; Suggested by tale@pawl.rpi.edu.
7102     (unless (string-match "^<" message-id)
7103       (setq message-id (concat "<" message-id)))
7104     (unless (string-match ">$" message-id)
7105       (setq message-id (concat message-id ">")))
7106     (let* ((header (gnus-id-to-header message-id))
7107            (sparse (and header
7108                         (gnus-summary-article-sparse-p
7109                          (mail-header-number header))
7110                         (memq (mail-header-number header)
7111                               gnus-newsgroup-limit)))
7112            number)
7113       (cond
7114        ;; If the article is present in the buffer we just go to it.
7115        ((and header
7116              (or (not (gnus-summary-article-sparse-p
7117                        (mail-header-number header)))
7118                  sparse))
7119         (prog1
7120             (gnus-summary-goto-article
7121              (mail-header-number header) nil t)
7122           (when sparse
7123             (gnus-summary-update-article (mail-header-number header)))))
7124        (t
7125         ;; We fetch the article.
7126         (catch 'found
7127           (dolist (gnus-override-method (gnus-refer-article-methods))
7128             (gnus-check-server gnus-override-method)
7129             ;; Fetch the header, and display the article.
7130             (when (setq number (gnus-summary-insert-subject message-id))
7131               (gnus-summary-select-article nil nil nil number)
7132               (throw 'found t)))
7133           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7134
7135 (defun gnus-refer-article-methods ()
7136   "Return a list of referrable methods."
7137   (cond
7138    ;; No method, so we default to current and native.
7139    ((null gnus-refer-article-method)
7140     (list gnus-current-select-method gnus-select-method))
7141    ;; Current.
7142    ((eq 'current gnus-refer-article-method)
7143     (list gnus-current-select-method))
7144    ;; List of select methods.
7145    ((not (and (symbolp (car gnus-refer-article-method))
7146               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7147     (let (out)
7148       (dolist (method gnus-refer-article-method)
7149         (push (if (eq 'current method)
7150                   gnus-current-select-method
7151                 method)
7152               out))
7153       (nreverse out)))
7154    ;; One single select method.
7155    (t
7156     (list gnus-refer-article-method))))
7157
7158 (defun gnus-summary-edit-parameters ()
7159   "Edit the group parameters of the current group."
7160   (interactive)
7161   (gnus-group-edit-group gnus-newsgroup-name 'params))
7162
7163 (defun gnus-summary-customize-parameters ()
7164   "Customize the group parameters of the current group."
7165   (interactive)
7166   (gnus-group-customize gnus-newsgroup-name))
7167
7168 (defun gnus-summary-enter-digest-group (&optional force)
7169   "Enter an nndoc group based on the current article.
7170 If FORCE, force a digest interpretation.  If not, try
7171 to guess what the document format is."
7172   (interactive "P")
7173   (let ((conf gnus-current-window-configuration))
7174     (save-excursion
7175       (gnus-summary-select-article))
7176     (setq gnus-current-window-configuration conf)
7177     (let* ((name (format "%s-%d"
7178                          (gnus-group-prefixed-name
7179                           gnus-newsgroup-name (list 'nndoc ""))
7180                          (save-excursion
7181                            (set-buffer gnus-summary-buffer)
7182                            gnus-current-article)))
7183            (ogroup gnus-newsgroup-name)
7184            (params (append (gnus-info-params (gnus-get-info ogroup))
7185                            (list (cons 'to-group ogroup))
7186                            (list (cons 'save-article-group ogroup))))
7187            (case-fold-search t)
7188            (buf (current-buffer))
7189            dig to-address)
7190       (save-excursion
7191         (set-buffer gnus-original-article-buffer)
7192         ;; Have the digest group inherit the main mail address of
7193         ;; the parent article.
7194         (when (setq to-address (or (message-fetch-field "reply-to")
7195                                    (message-fetch-field "from")))
7196           (setq params (append
7197                         (list (cons 'to-address
7198                                     (funcall gnus-decode-encoded-word-function
7199                                              to-address))))))
7200         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7201         (insert-buffer-substring gnus-original-article-buffer)
7202         ;; Remove lines that may lead nndoc to misinterpret the
7203         ;; document type.
7204         (narrow-to-region
7205          (goto-char (point-min))
7206          (or (search-forward "\n\n" nil t) (point)))
7207         (goto-char (point-min))
7208         (delete-matching-lines "^Path:\\|^From ")
7209         (widen))
7210       (unwind-protect
7211           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7212                     (gnus-newsgroup-ephemeral-ignored-charsets
7213                      gnus-newsgroup-ignored-charsets))
7214                 (gnus-group-read-ephemeral-group
7215                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7216                               (nndoc-article-type
7217                                ,(if force 'mbox 'guess))) t))
7218               ;; Make all postings to this group go to the parent group.
7219               (nconc (gnus-info-params (gnus-get-info name))
7220                      params)
7221             ;; Couldn't select this doc group.
7222             (switch-to-buffer buf)
7223             (gnus-set-global-variables)
7224             (gnus-configure-windows 'summary)
7225             (gnus-message 3 "Article couldn't be entered?"))
7226         (kill-buffer dig)))))
7227
7228 (defun gnus-summary-read-document (n)
7229   "Open a new group based on the current article(s).
7230 This will allow you to read digests and other similar
7231 documents as newsgroups.
7232 Obeys the standard process/prefix convention."
7233   (interactive "P")
7234   (let* ((articles (gnus-summary-work-articles n))
7235          (ogroup gnus-newsgroup-name)
7236          (params (append (gnus-info-params (gnus-get-info ogroup))
7237                          (list (cons 'to-group ogroup))))
7238          article group egroup groups vgroup)
7239     (while (setq article (pop articles))
7240       (setq group (format "%s-%d" gnus-newsgroup-name article))
7241       (gnus-summary-remove-process-mark article)
7242       (when (gnus-summary-display-article article)
7243         (save-excursion
7244           (with-temp-buffer
7245             (insert-buffer-substring gnus-original-article-buffer)
7246             ;; Remove some headers that may lead nndoc to make
7247             ;; the wrong guess.
7248             (message-narrow-to-head)
7249             (goto-char (point-min))
7250             (delete-matching-lines "^\\(Path\\):\\|^From ")
7251             (widen)
7252             (if (setq egroup
7253                       (gnus-group-read-ephemeral-group
7254                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7255                                      (nndoc-article-type guess))
7256                        t nil t))
7257                 (progn
7258                   ;; Make all postings to this group go to the parent group.
7259                   (nconc (gnus-info-params (gnus-get-info egroup))
7260                          params)
7261                   (push egroup groups))
7262               ;; Couldn't select this doc group.
7263               (gnus-error 3 "Article couldn't be entered"))))))
7264     ;; Now we have selected all the documents.
7265     (cond
7266      ((not groups)
7267       (error "None of the articles could be interpreted as documents"))
7268      ((gnus-group-read-ephemeral-group
7269        (setq vgroup (format
7270                      "nnvirtual:%s-%s" gnus-newsgroup-name
7271                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7272        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7273        t
7274        (cons (current-buffer) 'summary)))
7275      (t
7276       (error "Couldn't select virtual nndoc group")))))
7277
7278 (defun gnus-summary-isearch-article (&optional regexp-p)
7279   "Do incremental search forward on the current article.
7280 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7281   (interactive "P")
7282   (gnus-summary-select-article)
7283   (gnus-configure-windows 'article)
7284   (gnus-eval-in-buffer-window gnus-article-buffer
7285     (save-restriction
7286       (widen)
7287       (isearch-forward regexp-p))))
7288
7289 (defun gnus-summary-search-article-forward (regexp &optional backward)
7290   "Search for an article containing REGEXP forward.
7291 If BACKWARD, search backward instead."
7292   (interactive
7293    (list (read-string
7294           (format "Search article %s (regexp%s): "
7295                   (if current-prefix-arg "backward" "forward")
7296                   (if gnus-last-search-regexp
7297                       (concat ", default " gnus-last-search-regexp)
7298                     "")))
7299          current-prefix-arg))
7300   (if (string-equal regexp "")
7301       (setq regexp (or gnus-last-search-regexp ""))
7302     (setq gnus-last-search-regexp regexp)
7303     (setq gnus-article-before-search gnus-current-article))
7304   ;; Intentionally set gnus-last-article.
7305   (setq gnus-last-article gnus-article-before-search)
7306   (let ((gnus-last-article gnus-last-article))
7307     (if (gnus-summary-search-article regexp backward)
7308         (gnus-summary-show-thread)
7309       (error "Search failed: \"%s\"" regexp))))
7310
7311 (defun gnus-summary-search-article-backward (regexp)
7312   "Search for an article containing REGEXP backward."
7313   (interactive
7314    (list (read-string
7315           (format "Search article backward (regexp%s): "
7316                   (if gnus-last-search-regexp
7317                       (concat ", default " gnus-last-search-regexp)
7318                     "")))))
7319   (gnus-summary-search-article-forward regexp 'backward))
7320
7321 (defun gnus-summary-search-article (regexp &optional backward)
7322   "Search for an article containing REGEXP.
7323 Optional argument BACKWARD means do search for backward.
7324 `gnus-select-article-hook' is not called during the search."
7325   ;; We have to require this here to make sure that the following
7326   ;; dynamic binding isn't shadowed by autoloading.
7327   (require 'gnus-async)
7328   (require 'gnus-art)
7329   (let ((gnus-select-article-hook nil)  ;Disable hook.
7330         (gnus-article-prepare-hook nil)
7331         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7332         (gnus-use-article-prefetch nil)
7333         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7334         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7335         (sum (current-buffer))
7336         (gnus-display-mime-function nil)
7337         (found nil)
7338         point)
7339     (gnus-save-hidden-threads
7340       (gnus-summary-select-article)
7341       (set-buffer gnus-article-buffer)
7342       (goto-char (window-point (get-buffer-window (current-buffer))))
7343       (when backward
7344         (forward-line -1))
7345       (while (not found)
7346         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7347         (if (if backward
7348                 (re-search-backward regexp nil t)
7349               (re-search-forward regexp nil t))
7350             ;; We found the regexp.
7351             (progn
7352               (setq found 'found)
7353               (beginning-of-line)
7354               (set-window-start
7355                (get-buffer-window (current-buffer))
7356                (point))
7357               (forward-line 1)
7358               (set-window-point
7359                (get-buffer-window (current-buffer))
7360                (point))
7361               (set-buffer sum)
7362               (setq point (point)))
7363           ;; We didn't find it, so we go to the next article.
7364           (set-buffer sum)
7365           (setq found 'not)
7366           (while (eq found 'not)
7367             (if (not (if backward (gnus-summary-find-prev)
7368                        (gnus-summary-find-next)))
7369                 ;; No more articles.
7370                 (setq found t)
7371               ;; Select the next article and adjust point.
7372               (unless (gnus-summary-article-sparse-p
7373                        (gnus-summary-article-number))
7374                 (setq found nil)
7375                 (gnus-summary-select-article)
7376                 (set-buffer gnus-article-buffer)
7377                 (widen)
7378                 (goto-char (if backward (point-max) (point-min))))))))
7379       (gnus-message 7 ""))
7380     ;; Return whether we found the regexp.
7381     (when (eq found 'found)
7382       (goto-char point)
7383       (gnus-summary-show-thread)
7384       (gnus-summary-goto-subject gnus-current-article)
7385       (gnus-summary-position-point)
7386       t)))
7387
7388 (defun gnus-summary-find-matching (header regexp &optional backward unread
7389                                           not-case-fold)
7390   "Return a list of all articles that match REGEXP on HEADER.
7391 The search stars on the current article and goes forwards unless
7392 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7393 If UNREAD is non-nil, only unread articles will
7394 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7395 in the comparisons."
7396   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7397                 (gnus-data-find-list
7398                  (gnus-summary-article-number) (gnus-data-list backward))))
7399         (case-fold-search (not not-case-fold))
7400         articles d func)
7401     (if (consp header)
7402         (if (eq (car header) 'extra)
7403             (setq func
7404                   `(lambda (h)
7405                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7406                          "")))
7407           (error "%s is an invalid header" header))
7408       (unless (fboundp (intern (concat "mail-header-" header)))
7409         (error "%s is not a valid header" header))
7410       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7411     (while data
7412       (setq d (car data))
7413       (and (or (not unread)             ; We want all articles...
7414                (gnus-data-unread-p d))  ; Or just unreads.
7415            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7416            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7417            (push (gnus-data-number d) articles)) ; Success!
7418       (setq data (cdr data)))
7419     (nreverse articles)))
7420
7421 (defun gnus-summary-execute-command (header regexp command &optional backward)
7422   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7423 If HEADER is an empty string (or nil), the match is done on the entire
7424 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7425   (interactive
7426    (list (let ((completion-ignore-case t))
7427            (completing-read
7428             "Header name: "
7429             (mapcar (lambda (string) (list string))
7430                     '("Number" "Subject" "From" "Lines" "Date"
7431                       "Message-ID" "Xref" "References" "Body"))
7432             nil 'require-match))
7433          (read-string "Regexp: ")
7434          (read-key-sequence "Command: ")
7435          current-prefix-arg))
7436   (when (equal header "Body")
7437     (setq header ""))
7438   ;; Hidden thread subtrees must be searched as well.
7439   (gnus-summary-show-all-threads)
7440   ;; We don't want to change current point nor window configuration.
7441   (save-excursion
7442     (save-window-excursion
7443       (gnus-message 6 "Executing %s..." (key-description command))
7444       ;; We'd like to execute COMMAND interactively so as to give arguments.
7445       (gnus-execute header regexp
7446                     `(call-interactively ',(key-binding command))
7447                     backward)
7448       (gnus-message 6 "Executing %s...done" (key-description command)))))
7449
7450 (defun gnus-summary-beginning-of-article ()
7451   "Scroll the article back to the beginning."
7452   (interactive)
7453   (gnus-summary-select-article)
7454   (gnus-configure-windows 'article)
7455   (gnus-eval-in-buffer-window gnus-article-buffer
7456     (widen)
7457     (goto-char (point-min))
7458     (when gnus-page-broken
7459       (gnus-narrow-to-page))))
7460
7461 (defun gnus-summary-end-of-article ()
7462   "Scroll to the end of the article."
7463   (interactive)
7464   (gnus-summary-select-article)
7465   (gnus-configure-windows 'article)
7466   (gnus-eval-in-buffer-window gnus-article-buffer
7467     (widen)
7468     (goto-char (point-max))
7469     (recenter -3)
7470     (when gnus-page-broken
7471       (gnus-narrow-to-page))))
7472
7473 (defun gnus-summary-print-article (&optional filename n)
7474   "Generate and print a PostScript image of the N next (mail) articles.
7475
7476 If N is negative, print the N previous articles.  If N is nil and articles
7477 have been marked with the process mark, print these instead.
7478
7479 If the optional first argument FILENAME is nil, send the image to the
7480 printer.  If FILENAME is a string, save the PostScript image in a file with
7481 that name.  If FILENAME is a number, prompt the user for the name of the file
7482 to save in."
7483   (interactive (list (ps-print-preprint current-prefix-arg)))
7484   (dolist (article (gnus-summary-work-articles n))
7485     (gnus-summary-select-article nil nil 'pseudo article)
7486     (gnus-eval-in-buffer-window gnus-article-buffer
7487       (let ((buffer (generate-new-buffer " *print*")))
7488         (unwind-protect
7489             (progn
7490               (copy-to-buffer buffer (point-min) (point-max))
7491               (set-buffer buffer)
7492               (gnus-article-delete-invisible-text)
7493               (when (gnus-visual-p 'article-highlight 'highlight)
7494                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7495                 ;; highlight.
7496                 (let ((gnus-article-buffer buffer))
7497                   (gnus-article-highlight-citation t)
7498                   (gnus-article-highlight-signature)))
7499               (let ((ps-left-header
7500                      (list
7501                       (concat "("
7502                               (mail-header-subject gnus-current-headers) ")")
7503                       (concat "("
7504                               (mail-header-from gnus-current-headers) ")")))
7505                     (ps-right-header
7506                      (list
7507                       "/pagenumberstring load"
7508                       (concat "("
7509                               (mail-header-date gnus-current-headers) ")"))))
7510                 (gnus-run-hooks 'gnus-ps-print-hook)
7511                 (save-excursion
7512                   (if window-system
7513                       (ps-spool-buffer-with-faces)
7514                     (ps-spool-buffer)))))
7515           (kill-buffer buffer))))
7516     (gnus-summary-remove-process-mark article))
7517   (ps-despool filename))
7518
7519 (defun gnus-summary-show-article (&optional arg)
7520   "Force re-fetching of the current article.
7521 If ARG (the prefix) is a number, show the article with the charset
7522 defined in `gnus-summary-show-article-charset-alist', or the charset
7523 inputed.
7524 If ARG (the prefix) is non-nil and not a number, show the raw article
7525 without any article massaging functions being run."
7526   (interactive "P")
7527   (cond
7528    ((numberp arg)
7529     (let ((gnus-newsgroup-charset
7530            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7531                (read-coding-system "Charset: ")))
7532           (gnus-newsgroup-ignored-charsets 'gnus-all))
7533       (gnus-summary-select-article nil 'force)
7534       (let ((deps gnus-newsgroup-dependencies)
7535             head header)
7536         (save-excursion
7537           (set-buffer gnus-original-article-buffer)
7538           (save-restriction
7539             (message-narrow-to-head)
7540             (setq head (buffer-string)))
7541           (with-temp-buffer
7542             (insert (format "211 %d Article retrieved.\n"
7543                             (cdr gnus-article-current)))
7544             (insert head)
7545             (insert ".\n")
7546             (let ((nntp-server-buffer (current-buffer)))
7547               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7548         (gnus-data-set-header
7549          (gnus-data-find (cdr gnus-article-current))
7550          header)
7551         (gnus-summary-update-article-line
7552          (cdr gnus-article-current) header))))
7553    ((not arg)
7554     ;; Select the article the normal way.
7555     (gnus-summary-select-article nil 'force))
7556    (t
7557     ;; We have to require this here to make sure that the following
7558     ;; dynamic binding isn't shadowed by autoloading.
7559     (require 'gnus-async)
7560     (require 'gnus-art)
7561     ;; Bind the article treatment functions to nil.
7562     (let ((gnus-have-all-headers t)
7563           gnus-article-prepare-hook
7564           gnus-article-decode-hook
7565           gnus-display-mime-function
7566           gnus-break-pages)
7567       ;; Destroy any MIME parts.
7568       (when (gnus-buffer-live-p gnus-article-buffer)
7569         (save-excursion
7570           (set-buffer gnus-article-buffer)
7571           (mm-destroy-parts gnus-article-mime-handles)
7572           ;; Set it to nil for safety reason.
7573           (setq gnus-article-mime-handle-alist nil)
7574           (setq gnus-article-mime-handles nil)))
7575       (gnus-summary-select-article nil 'force))))
7576   (gnus-summary-goto-subject gnus-current-article)
7577   (gnus-summary-position-point))
7578
7579 (defun gnus-summary-verbose-headers (&optional arg)
7580   "Toggle permanent full header display.
7581 If ARG is a positive number, turn header display on.
7582 If ARG is a negative number, turn header display off."
7583   (interactive "P")
7584   (setq gnus-show-all-headers
7585         (cond ((or (not (numberp arg))
7586                    (zerop arg))
7587                (not gnus-show-all-headers))
7588               ((natnump arg)
7589                t)))
7590   (gnus-summary-show-article))
7591
7592 (defun gnus-summary-toggle-header (&optional arg)
7593   "Show the headers if they are hidden, or hide them if they are shown.
7594 If ARG is a positive number, show the entire header.
7595 If ARG is a negative number, hide the unwanted header lines."
7596   (interactive "P")
7597   (save-excursion
7598     (set-buffer gnus-article-buffer)
7599     (save-restriction
7600       (let* ((buffer-read-only nil)
7601              (inhibit-point-motion-hooks t)
7602              hidden e)
7603         (setq hidden
7604               (if (numberp arg)
7605                   (>= arg 0)
7606                 (save-restriction
7607                   (article-narrow-to-head)
7608                   (gnus-article-hidden-text-p 'headers))))
7609         (goto-char (point-min))
7610         (when (search-forward "\n\n" nil t)
7611           (delete-region (point-min) (1- (point))))
7612         (goto-char (point-min))
7613         (save-excursion
7614           (set-buffer gnus-original-article-buffer)
7615           (goto-char (point-min))
7616           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7617         (insert-buffer-substring gnus-original-article-buffer 1 e)
7618         (save-restriction
7619           (narrow-to-region (point-min) (point))
7620           (article-decode-encoded-words)
7621           (if  hidden
7622               (let ((gnus-treat-hide-headers nil)
7623                     (gnus-treat-hide-boring-headers nil))
7624                 (setq gnus-article-wash-types
7625                       (delq 'headers gnus-article-wash-types))
7626                 (gnus-treat-article 'head))
7627             (gnus-treat-article 'head)))
7628         (gnus-set-mode-line 'article)))))
7629
7630 (defun gnus-summary-show-all-headers ()
7631   "Make all header lines visible."
7632   (interactive)
7633   (gnus-summary-toggle-header 1))
7634
7635 (defun gnus-summary-caesar-message (&optional arg)
7636   "Caesar rotate the current article by 13.
7637 The numerical prefix specifies how many places to rotate each letter
7638 forward."
7639   (interactive "P")
7640   (gnus-summary-select-article)
7641   (let ((mail-header-separator ""))
7642     (gnus-eval-in-buffer-window gnus-article-buffer
7643       (save-restriction
7644         (widen)
7645         (let ((start (window-start))
7646               buffer-read-only)
7647           (message-caesar-buffer-body arg)
7648           (set-window-start (get-buffer-window (current-buffer)) start))))))
7649
7650 (defun gnus-summary-stop-page-breaking ()
7651   "Stop page breaking in the current article."
7652   (interactive)
7653   (gnus-summary-select-article)
7654   (gnus-eval-in-buffer-window gnus-article-buffer
7655     (widen)
7656     (when (gnus-visual-p 'page-marker)
7657       (let ((buffer-read-only nil))
7658         (gnus-remove-text-with-property 'gnus-prev)
7659         (gnus-remove-text-with-property 'gnus-next))
7660       (setq gnus-page-broken nil))))
7661
7662 (defun gnus-summary-move-article (&optional n to-newsgroup
7663                                             select-method action)
7664   "Move the current article to a different newsgroup.
7665 If N is a positive number, move the N next articles.
7666 If N is a negative number, move the N previous articles.
7667 If N is nil and any articles have been marked with the process mark,
7668 move those articles instead.
7669 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7670 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7671 re-spool using this method.
7672
7673 For this function to work, both the current newsgroup and the
7674 newsgroup that you want to move to have to support the `request-move'
7675 and `request-accept' functions.
7676
7677 ACTION can be either `move' (the default), `crosspost' or `copy'."
7678   (interactive "P")
7679   (unless action
7680     (setq action 'move))
7681   ;; Disable marking as read.
7682   (let (gnus-mark-article-hook)
7683     (save-window-excursion
7684       (gnus-summary-select-article)))
7685   ;; Check whether the source group supports the required functions.
7686   (cond ((and (eq action 'move)
7687               (not (gnus-check-backend-function
7688                     'request-move-article gnus-newsgroup-name)))
7689          (error "The current group does not support article moving"))
7690         ((and (eq action 'crosspost)
7691               (not (gnus-check-backend-function
7692                     'request-replace-article gnus-newsgroup-name)))
7693          (error "The current group does not support article editing")))
7694   (let ((articles (gnus-summary-work-articles n))
7695         (prefix (if (gnus-check-backend-function
7696                     'request-move-article gnus-newsgroup-name)
7697                     (gnus-group-real-prefix gnus-newsgroup-name)
7698                   ""))
7699         (names '((move "Move" "Moving")
7700                  (copy "Copy" "Copying")
7701                  (crosspost "Crosspost" "Crossposting")))
7702         (copy-buf (save-excursion
7703                     (nnheader-set-temp-buffer " *copy article*")))
7704         art-group to-method new-xref article to-groups)
7705     (unless (assq action names)
7706       (error "Unknown action %s" action))
7707     ;; Read the newsgroup name.
7708     (when (and (not to-newsgroup)
7709                (not select-method))
7710       (setq to-newsgroup
7711             (gnus-read-move-group-name
7712              (cadr (assq action names))
7713              (symbol-value (intern (format "gnus-current-%s-group" action)))
7714              articles prefix))
7715       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7716     (setq to-method (or select-method
7717                         (gnus-server-to-method
7718                          (gnus-group-method to-newsgroup))))
7719     ;; Check the method we are to move this article to...
7720     (unless (gnus-check-backend-function
7721              'request-accept-article (car to-method))
7722       (error "%s does not support article copying" (car to-method)))
7723     (unless (gnus-check-server to-method)
7724       (error "Can't open server %s" (car to-method)))
7725     (gnus-message 6 "%s to %s: %s..."
7726                   (caddr (assq action names))
7727                   (or (car select-method) to-newsgroup) articles)
7728     (while articles
7729       (setq article (pop articles))
7730       (setq
7731        art-group
7732        (cond
7733         ;; Move the article.
7734         ((eq action 'move)
7735          ;; Remove this article from future suppression.
7736          (gnus-dup-unsuppress-article article)
7737          (gnus-request-move-article
7738           article                       ; Article to move
7739           gnus-newsgroup-name           ; From newsgroup
7740           (nth 1 (gnus-find-method-for-group
7741                   gnus-newsgroup-name)) ; Server
7742           (list 'gnus-request-accept-article
7743                 to-newsgroup (list 'quote select-method)
7744                 (not articles) t)       ; Accept form
7745           (not articles)))              ; Only save nov last time
7746         ;; Copy the article.
7747         ((eq action 'copy)
7748          (save-excursion
7749            (set-buffer copy-buf)
7750            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7751              (gnus-request-accept-article
7752               to-newsgroup select-method (not articles) t))))
7753         ;; Crosspost the article.
7754         ((eq action 'crosspost)
7755          (let ((xref (message-tokenize-header
7756                       (mail-header-xref (gnus-summary-article-header article))
7757                       " ")))
7758            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7759                                   ":" article))
7760            (unless xref
7761              (setq xref (list (system-name))))
7762            (setq new-xref
7763                  (concat
7764                   (mapconcat 'identity
7765                              (delete "Xref:" (delete new-xref xref))
7766                              " ")
7767                   " " new-xref))
7768            (save-excursion
7769              (set-buffer copy-buf)
7770              ;; First put the article in the destination group.
7771              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7772              (when (consp (setq art-group
7773                                 (gnus-request-accept-article
7774                                  to-newsgroup select-method (not articles))))
7775                (setq new-xref (concat new-xref " " (car art-group)
7776                                       ":" (cdr art-group)))
7777                ;; Now we have the new Xrefs header, so we insert
7778                ;; it and replace the new article.
7779                (nnheader-replace-header "Xref" new-xref)
7780                (gnus-request-replace-article
7781                 (cdr art-group) to-newsgroup (current-buffer))
7782                art-group))))))
7783       (cond
7784        ((not art-group)
7785         (gnus-message 1 "Couldn't %s article %s: %s"
7786                       (cadr (assq action names)) article
7787                       (nnheader-get-report (car to-method))))
7788        ((eq art-group 'junk)
7789         (when (eq action 'move)
7790           (gnus-summary-mark-article article gnus-canceled-mark)
7791           (gnus-message 4 "Deleted article %s" article)))
7792        (t
7793         (let* ((pto-group (gnus-group-prefixed-name
7794                            (car art-group) to-method))
7795                (entry
7796                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7797                (info (nth 2 entry))
7798                (to-group (gnus-info-group info))
7799                to-marks)
7800           ;; Update the group that has been moved to.
7801           (when (and info
7802                      (memq action '(move copy)))
7803             (unless (member to-group to-groups)
7804               (push to-group to-groups))
7805
7806             (unless (memq article gnus-newsgroup-unreads)
7807               (push 'read to-marks)
7808               (gnus-info-set-read
7809                info (gnus-add-to-range (gnus-info-read info)
7810                                        (list (cdr art-group)))))
7811
7812             ;; See whether the article is to be put in the cache.
7813             (let ((marks gnus-article-mark-lists)
7814                   (to-article (cdr art-group)))
7815
7816               ;; Enter the article into the cache in the new group,
7817               ;; if that is required.
7818               (when gnus-use-cache
7819                 (gnus-cache-possibly-enter-article
7820                  to-group to-article
7821                  (memq article gnus-newsgroup-marked)
7822                  (memq article gnus-newsgroup-dormant)
7823                  (memq article gnus-newsgroup-unreads)))
7824
7825               (when gnus-preserve-marks
7826                 ;; Copy any marks over to the new group.
7827                 (when (and (equal to-group gnus-newsgroup-name)
7828                            (not (memq article gnus-newsgroup-unreads)))
7829                   ;; Mark this article as read in this group.
7830                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7831                   (setcdr (gnus-active to-group) to-article)
7832                   (setcdr gnus-newsgroup-active to-article))
7833
7834                 (while marks
7835                   (when (memq article (symbol-value
7836                                        (intern (format "gnus-newsgroup-%s"
7837                                                        (caar marks)))))
7838                     (push (cdar marks) to-marks)
7839                     ;; If the other group is the same as this group,
7840                     ;; then we have to add the mark to the list.
7841                     (when (equal to-group gnus-newsgroup-name)
7842                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7843                            (cons to-article
7844                                  (symbol-value
7845                                   (intern (format "gnus-newsgroup-%s"
7846                                                   (caar marks)))))))
7847                     ;; Copy the marks to other group.
7848                     (gnus-add-marked-articles
7849                      to-group (cdar marks) (list to-article) info))
7850                   (setq marks (cdr marks)))
7851
7852                 (gnus-request-set-mark to-group (list (list (list to-article)
7853                                                             'set
7854                                                             to-marks))))
7855
7856               (gnus-dribble-enter
7857                (concat "(gnus-group-set-info '"
7858                        (gnus-prin1-to-string (gnus-get-info to-group))
7859                        ")"))))
7860
7861           ;; Update the Xref header in this article to point to
7862           ;; the new crossposted article we have just created.
7863           (when (eq action 'crosspost)
7864             (save-excursion
7865               (set-buffer copy-buf)
7866               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7867               (nnheader-replace-header "Xref" new-xref)
7868               (gnus-request-replace-article
7869                article gnus-newsgroup-name (current-buffer)))))
7870
7871         ;;;!!!Why is this necessary?
7872         (set-buffer gnus-summary-buffer)
7873
7874         (gnus-summary-goto-subject article)
7875         (when (eq action 'move)
7876           (gnus-summary-mark-article article gnus-canceled-mark))))
7877       (gnus-summary-remove-process-mark article))
7878     ;; Re-activate all groups that have been moved to.
7879     (while to-groups
7880       (save-excursion
7881         (set-buffer gnus-group-buffer)
7882         (when (gnus-group-goto-group (car to-groups) t)
7883           (gnus-group-get-new-news-this-group 1 t))
7884         (pop to-groups)))
7885
7886     (gnus-kill-buffer copy-buf)
7887     (gnus-summary-position-point)
7888     (gnus-set-mode-line 'summary)))
7889
7890 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7891   "Move the current article to a different newsgroup.
7892 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7893 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7894 re-spool using this method."
7895   (interactive "P")
7896   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7897
7898 (defun gnus-summary-crosspost-article (&optional n)
7899   "Crosspost the current article to some other group."
7900   (interactive "P")
7901   (gnus-summary-move-article n nil nil 'crosspost))
7902
7903 (defcustom gnus-summary-respool-default-method nil
7904   "Default method for respooling an article.
7905 If nil, use to the current newsgroup method."
7906   :type '(choice (gnus-select-method :value (nnml ""))
7907                  (const nil))
7908   :group 'gnus-summary-mail)
7909
7910 (defun gnus-summary-respool-article (&optional n method)
7911   "Respool the current article.
7912 The article will be squeezed through the mail spooling process again,
7913 which means that it will be put in some mail newsgroup or other
7914 depending on `nnmail-split-methods'.
7915 If N is a positive number, respool the N next articles.
7916 If N is a negative number, respool the N previous articles.
7917 If N is nil and any articles have been marked with the process mark,
7918 respool those articles instead.
7919
7920 Respooling can be done both from mail groups and \"real\" newsgroups.
7921 In the former case, the articles in question will be moved from the
7922 current group into whatever groups they are destined to.  In the
7923 latter case, they will be copied into the relevant groups."
7924   (interactive
7925    (list current-prefix-arg
7926          (let* ((methods (gnus-methods-using 'respool))
7927                 (methname
7928                  (symbol-name (or gnus-summary-respool-default-method
7929                                   (car (gnus-find-method-for-group
7930                                         gnus-newsgroup-name)))))
7931                 (method
7932                  (gnus-completing-read
7933                   methname "What backend do you want to use when respooling?"
7934                   methods nil t nil 'gnus-mail-method-history))
7935                 ms)
7936            (cond
7937             ((zerop (length (setq ms (gnus-servers-using-backend
7938                                       (intern method)))))
7939              (list (intern method) ""))
7940             ((= 1 (length ms))
7941              (car ms))
7942             (t
7943              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7944                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7945                            ms-alist))))))))
7946   (unless method
7947     (error "No method given for respooling"))
7948   (if (assoc (symbol-name
7949               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7950              (gnus-methods-using 'respool))
7951       (gnus-summary-move-article n nil method)
7952     (gnus-summary-copy-article n nil method)))
7953
7954 (defun gnus-summary-import-article (file &optional edit)
7955   "Import an arbitrary file into a mail newsgroup."
7956   (interactive "fImport file: \nP")
7957   (let ((group gnus-newsgroup-name)
7958         (now (current-time))
7959         atts lines group-art)
7960     (unless (gnus-check-backend-function 'request-accept-article group)
7961       (error "%s does not support article importing" group))
7962     (or (file-readable-p file)
7963         (not (file-regular-p file))
7964         (error "Can't read %s" file))
7965     (save-excursion
7966       (set-buffer (gnus-get-buffer-create " *import file*"))
7967       (erase-buffer)
7968       (nnheader-insert-file-contents file)
7969       (goto-char (point-min))
7970       (unless (nnheader-article-p)
7971         ;; This doesn't look like an article, so we fudge some headers.
7972         (setq atts (file-attributes file)
7973               lines (count-lines (point-min) (point-max)))
7974         (insert "From: " (read-string "From: ") "\n"
7975                 "Subject: " (read-string "Subject: ") "\n"
7976                 "Date: " (message-make-date (nth 5 atts)) "\n"
7977                 "Message-ID: " (message-make-message-id) "\n"
7978                 "Lines: " (int-to-string lines) "\n"
7979                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7980       (setq group-art (gnus-request-accept-article group nil t))
7981       (kill-buffer (current-buffer)))
7982     (setq gnus-newsgroup-active (gnus-activate-group group))
7983     (forward-line 1)
7984     (gnus-summary-goto-article (cdr group-art) nil t)
7985     (when edit
7986       (gnus-summary-edit-article))))
7987
7988 (defun gnus-summary-create-article ()
7989   "Create an article in a mail newsgroup."
7990   (interactive)
7991   (let ((group gnus-newsgroup-name)
7992         (now (current-time))
7993         group-art)
7994     (unless (gnus-check-backend-function 'request-accept-article group)
7995       (error "%s does not support article importing" group))
7996     (save-excursion
7997       (set-buffer (gnus-get-buffer-create " *import file*"))
7998       (erase-buffer)
7999       (goto-char (point-min))
8000       ;; This doesn't look like an article, so we fudge some headers.
8001       (insert "From: " (read-string "From: ") "\n"
8002               "Subject: " (read-string "Subject: ") "\n"
8003               "Date: " (message-make-date now) "\n"
8004               "Message-ID: " (message-make-message-id) "\n")
8005       (setq group-art (gnus-request-accept-article group nil t))
8006       (kill-buffer (current-buffer)))
8007     (setq gnus-newsgroup-active (gnus-activate-group group))
8008     (forward-line 1)
8009     (gnus-summary-goto-article (cdr group-art) nil t)
8010     (gnus-summary-edit-article)))
8011
8012 (defun gnus-summary-article-posted-p ()
8013   "Say whether the current (mail) article is available from news as well.
8014 This will be the case if the article has both been mailed and posted."
8015   (interactive)
8016   (let ((id (mail-header-references (gnus-summary-article-header)))
8017         (gnus-override-method (car (gnus-refer-article-methods))))
8018     (if (gnus-request-head id "")
8019         (gnus-message 2 "The current message was found on %s"
8020                       gnus-override-method)
8021       (gnus-message 2 "The current message couldn't be found on %s"
8022                     gnus-override-method)
8023       nil)))
8024
8025 (defun gnus-summary-expire-articles (&optional now)
8026   "Expire all articles that are marked as expirable in the current group."
8027   (interactive)
8028   (when (gnus-check-backend-function
8029          'request-expire-articles gnus-newsgroup-name)
8030     ;; This backend supports expiry.
8031     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8032            (expirable (if total
8033                           (progn
8034                             ;; We need to update the info for
8035                             ;; this group for `gnus-list-of-read-articles'
8036                             ;; to give us the right answer.
8037                             (gnus-run-hooks 'gnus-exit-group-hook)
8038                             (gnus-summary-update-info)
8039                             (gnus-list-of-read-articles gnus-newsgroup-name))
8040                         (setq gnus-newsgroup-expirable
8041                               (sort gnus-newsgroup-expirable '<))))
8042            (expiry-wait (if now 'immediate
8043                           (gnus-group-find-parameter
8044                            gnus-newsgroup-name 'expiry-wait)))
8045            (nnmail-expiry-target
8046             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8047                 nnmail-expiry-target))
8048            es)
8049       (when expirable
8050         ;; There are expirable articles in this group, so we run them
8051         ;; through the expiry process.
8052         (gnus-message 6 "Expiring articles...")
8053         (unless (gnus-check-group gnus-newsgroup-name)
8054           (error "Can't open server for %s" gnus-newsgroup-name))
8055         ;; The list of articles that weren't expired is returned.
8056         (save-excursion
8057           (if expiry-wait
8058               (let ((nnmail-expiry-wait-function nil)
8059                     (nnmail-expiry-wait expiry-wait))
8060                 (setq es (gnus-request-expire-articles
8061                           expirable gnus-newsgroup-name)))
8062             (setq es (gnus-request-expire-articles
8063                       expirable gnus-newsgroup-name)))
8064           (unless total
8065             (setq gnus-newsgroup-expirable es))
8066           ;; We go through the old list of expirable, and mark all
8067           ;; really expired articles as nonexistent.
8068           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8069             (let ((gnus-use-cache nil))
8070               (while expirable
8071                 (unless (memq (car expirable) es)
8072                   (when (gnus-data-find (car expirable))
8073                     (gnus-summary-mark-article
8074                      (car expirable) gnus-canceled-mark)))
8075                 (setq expirable (cdr expirable))))))
8076         (gnus-message 6 "Expiring articles...done")))))
8077
8078 (defun gnus-summary-expire-articles-now ()
8079   "Expunge all expirable articles in the current group.
8080 This means that *all* articles that are marked as expirable will be
8081 deleted forever, right now."
8082   (interactive)
8083   (or gnus-expert-user
8084       (gnus-yes-or-no-p
8085        "Are you really, really, really sure you want to delete all these messages? ")
8086       (error "Phew!"))
8087   (gnus-summary-expire-articles t))
8088
8089 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8090 (defun gnus-summary-delete-article (&optional n)
8091   "Delete the N next (mail) articles.
8092 This command actually deletes articles.  This is not a marking
8093 command.  The article will disappear forever from your life, never to
8094 return.
8095 If N is negative, delete backwards.
8096 If N is nil and articles have been marked with the process mark,
8097 delete these instead."
8098   (interactive "P")
8099   (unless (gnus-check-backend-function 'request-expire-articles
8100                                        gnus-newsgroup-name)
8101     (error "The current newsgroup does not support article deletion"))
8102   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8103     (error "Couldn't open server"))
8104   ;; Compute the list of articles to delete.
8105   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8106         not-deleted)
8107     (if (and gnus-novice-user
8108              (not (gnus-yes-or-no-p
8109                    (format "Do you really want to delete %s forever? "
8110                            (if (> (length articles) 1)
8111                                (format "these %s articles" (length articles))
8112                              "this article")))))
8113         ()
8114       ;; Delete the articles.
8115       (setq not-deleted (gnus-request-expire-articles
8116                          articles gnus-newsgroup-name 'force))
8117       (while articles
8118         (gnus-summary-remove-process-mark (car articles))
8119         ;; The backend might not have been able to delete the article
8120         ;; after all.
8121         (unless (memq (car articles) not-deleted)
8122           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8123         (setq articles (cdr articles)))
8124       (when not-deleted
8125         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8126     (gnus-summary-position-point)
8127     (gnus-set-mode-line 'summary)
8128     not-deleted))
8129
8130 (defun gnus-summary-edit-article (&optional arg)
8131   "Edit the current article.
8132 This will have permanent effect only in mail groups.
8133 If ARG is nil, edit the decoded articles.
8134 If ARG is 1, edit the raw articles.
8135 If ARG is 2, edit the raw articles even in read-only groups.
8136 If ARG is 3, edit the articles with the current handles.
8137 Otherwise, allow editing of articles even in read-only
8138 groups."
8139   (interactive "P")
8140   (let (force raw current-handles)
8141     (cond
8142      ((null arg))
8143      ((eq arg 1) (setq raw t))
8144      ((eq arg 2) (setq raw t
8145                        force t))
8146      ((eq arg 3) (setq current-handles
8147                        (and (gnus-buffer-live-p gnus-article-buffer)
8148                             (with-current-buffer gnus-article-buffer
8149                               (prog1
8150                                   gnus-article-mime-handles
8151                                   (setq gnus-article-mime-handles nil))))))
8152      (t (setq force t)))
8153     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8154         (error "Can't edit the raw article in group nndraft:drafts."))
8155     (save-excursion
8156       (set-buffer gnus-summary-buffer)
8157       (let ((mail-parse-charset gnus-newsgroup-charset)
8158             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8159         (gnus-set-global-variables)
8160         (when (and (not force)
8161                    (gnus-group-read-only-p))
8162           (error "The current newsgroup does not support article editing"))
8163         (gnus-summary-show-article t)
8164         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8165           (with-current-buffer gnus-article-buffer
8166             (mm-enable-multibyte-mule4)))
8167         (if (equal gnus-newsgroup-name "nndraft:drafts")
8168             (setq raw t))
8169         (gnus-article-edit-article
8170          (if raw 'ignore
8171            `(lambda ()
8172               (let ((mbl mml-buffer-list))
8173                 (setq mml-buffer-list nil)
8174                 (mime-to-mml ,'current-handles)
8175                 (make-local-hook 'kill-buffer-hook)
8176                 (let ((mbl1 mml-buffer-list))
8177                   (setq mml-buffer-list mbl)
8178                   (set (make-local-variable 'mml-buffer-list) mbl1))
8179                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8180          `(lambda (no-highlight)
8181             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8182                   (message-options message-options)
8183                   (message-options-set-recipient)
8184                   (mail-parse-ignored-charsets
8185                    ',gnus-newsgroup-ignored-charsets))
8186               ,(if (not raw) '(progn
8187                                 (mml-to-mime)
8188                                 (mml-destroy-buffers)
8189                                 (remove-hook 'kill-buffer-hook
8190                                              'mml-destroy-buffers t)
8191                                 (kill-local-variable 'mml-buffer-list)))
8192               (gnus-summary-edit-article-done
8193                ,(or (mail-header-references gnus-current-headers) "")
8194                ,(gnus-group-read-only-p)
8195                ,gnus-summary-buffer no-highlight))))))))
8196
8197 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8198
8199 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8200                                                  no-highlight)
8201   "Make edits to the current article permanent."
8202   (interactive)
8203   (save-excursion
8204     ;; The buffer restriction contains the entire article if it exists.
8205     (when (article-goto-body)
8206       (let ((lines (count-lines (point) (point-max)))
8207             (length (- (point-max) (point)))
8208             (case-fold-search t)
8209             (body (copy-marker (point))))
8210         (goto-char (point-min))
8211         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8212           (delete-region (match-beginning 1) (match-end 1))
8213           (insert (number-to-string length)))
8214         (goto-char (point-min))
8215         (when (re-search-forward
8216                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8217           (delete-region (match-beginning 1) (match-end 1))
8218           (insert (number-to-string length)))
8219         (goto-char (point-min))
8220         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8221           (delete-region (match-beginning 1) (match-end 1))
8222           (insert (number-to-string lines))))))
8223   ;; Replace the article.
8224   (let ((buf (current-buffer)))
8225     (with-temp-buffer
8226       (insert-buffer-substring buf)
8227
8228       (if (and (not read-only)
8229                (not (gnus-request-replace-article
8230                      (cdr gnus-article-current) (car gnus-article-current)
8231                      (current-buffer) t)))
8232           (error "Couldn't replace article")
8233         ;; Update the summary buffer.
8234         (if (and references
8235                  (equal (message-tokenize-header references " ")
8236                         (message-tokenize-header
8237                          (or (message-fetch-field "references") "") " ")))
8238             ;; We only have to update this line.
8239             (save-excursion
8240               (save-restriction
8241                 (message-narrow-to-head)
8242                 (let ((head (buffer-string))
8243                       header)
8244                   (with-temp-buffer
8245                     (insert (format "211 %d Article retrieved.\n"
8246                                     (cdr gnus-article-current)))
8247                     (insert head)
8248                     (insert ".\n")
8249                     (let ((nntp-server-buffer (current-buffer)))
8250                       (setq header (car (gnus-get-newsgroup-headers
8251                                          (save-excursion
8252                                            (set-buffer gnus-summary-buffer)
8253                                            gnus-newsgroup-dependencies)
8254                                          t))))
8255                     (save-excursion
8256                       (set-buffer gnus-summary-buffer)
8257                       (gnus-data-set-header
8258                        (gnus-data-find (cdr gnus-article-current))
8259                        header)
8260                       (gnus-summary-update-article-line
8261                        (cdr gnus-article-current) header))))))
8262           ;; Update threads.
8263           (set-buffer (or buffer gnus-summary-buffer))
8264           (gnus-summary-update-article (cdr gnus-article-current)))
8265         ;; Prettify the article buffer again.
8266         (unless no-highlight
8267           (save-excursion
8268             (set-buffer gnus-article-buffer)
8269             ;;;!!! Fix this -- article should be rehighlighted.
8270             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8271             (set-buffer gnus-original-article-buffer)
8272             (gnus-request-article
8273              (cdr gnus-article-current)
8274              (car gnus-article-current) (current-buffer))))
8275         ;; Prettify the summary buffer line.
8276         (when (gnus-visual-p 'summary-highlight 'highlight)
8277           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8278
8279 (defun gnus-summary-edit-wash (key)
8280   "Perform editing command KEY in the article buffer."
8281   (interactive
8282    (list
8283     (progn
8284       (message "%s" (concat (this-command-keys) "- "))
8285       (read-char))))
8286   (message "")
8287   (gnus-summary-edit-article)
8288   (execute-kbd-macro (concat (this-command-keys) key))
8289   (gnus-article-edit-done))
8290
8291 ;;; Respooling
8292
8293 (defun gnus-summary-respool-query (&optional silent trace)
8294   "Query where the respool algorithm would put this article."
8295   (interactive)
8296   (let (gnus-mark-article-hook)
8297     (gnus-summary-select-article)
8298     (save-excursion
8299       (set-buffer gnus-original-article-buffer)
8300       (save-restriction
8301         (message-narrow-to-head)
8302         (let ((groups (nnmail-article-group 'identity trace)))
8303           (unless silent
8304             (if groups
8305                 (message "This message would go to %s"
8306                          (mapconcat 'car groups ", "))
8307               (message "This message would go to no groups"))
8308             groups))))))
8309
8310 (defun gnus-summary-respool-trace ()
8311   "Trace where the respool algorithm would put this article.
8312 Display a buffer showing all fancy splitting patterns which matched."
8313   (interactive)
8314   (gnus-summary-respool-query nil t))
8315
8316 ;; Summary marking commands.
8317
8318 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8319   "Mark articles which has the same subject as read, and then select the next.
8320 If UNMARK is positive, remove any kind of mark.
8321 If UNMARK is negative, tick articles."
8322   (interactive "P")
8323   (when unmark
8324     (setq unmark (prefix-numeric-value unmark)))
8325   (let ((count
8326          (gnus-summary-mark-same-subject
8327           (gnus-summary-article-subject) unmark)))
8328     ;; Select next unread article.  If auto-select-same mode, should
8329     ;; select the first unread article.
8330     (gnus-summary-next-article t (and gnus-auto-select-same
8331                                       (gnus-summary-article-subject)))
8332     (gnus-message 7 "%d article%s marked as %s"
8333                   count (if (= count 1) " is" "s are")
8334                   (if unmark "unread" "read"))))
8335
8336 (defun gnus-summary-kill-same-subject (&optional unmark)
8337   "Mark articles which has the same subject as read.
8338 If UNMARK is positive, remove any kind of mark.
8339 If UNMARK is negative, tick articles."
8340   (interactive "P")
8341   (when unmark
8342     (setq unmark (prefix-numeric-value unmark)))
8343   (let ((count
8344          (gnus-summary-mark-same-subject
8345           (gnus-summary-article-subject) unmark)))
8346     ;; If marked as read, go to next unread subject.
8347     (when (null unmark)
8348       ;; Go to next unread subject.
8349       (gnus-summary-next-subject 1 t))
8350     (gnus-message 7 "%d articles are marked as %s"
8351                   count (if unmark "unread" "read"))))
8352
8353 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8354   "Mark articles with same SUBJECT as read, and return marked number.
8355 If optional argument UNMARK is positive, remove any kinds of marks.
8356 If optional argument UNMARK is negative, mark articles as unread instead."
8357   (let ((count 1))
8358     (save-excursion
8359       (cond
8360        ((null unmark)                   ; Mark as read.
8361         (while (and
8362                 (progn
8363                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8364                   (gnus-summary-show-thread) t)
8365                 (gnus-summary-find-subject subject))
8366           (setq count (1+ count))))
8367        ((> unmark 0)                    ; Tick.
8368         (while (and
8369                 (progn
8370                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8371                   (gnus-summary-show-thread) t)
8372                 (gnus-summary-find-subject subject))
8373           (setq count (1+ count))))
8374        (t                               ; Mark as unread.
8375         (while (and
8376                 (progn
8377                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8378                   (gnus-summary-show-thread) t)
8379                 (gnus-summary-find-subject subject))
8380           (setq count (1+ count)))))
8381       (gnus-set-mode-line 'summary)
8382       ;; Return the number of marked articles.
8383       count)))
8384
8385 (defun gnus-summary-mark-as-processable (n &optional unmark)
8386   "Set the process mark on the next N articles.
8387 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8388 the process mark instead.  The difference between N and the actual
8389 number of articles marked is returned."
8390   (interactive "P")
8391   (if (and (null n) (gnus-region-active-p))
8392       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8393     (setq n (prefix-numeric-value n))
8394     (let ((backward (< n 0))
8395           (n (abs n)))
8396       (while (and
8397               (> n 0)
8398               (if unmark
8399                 (gnus-summary-remove-process-mark
8400                  (gnus-summary-article-number))
8401                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8402               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8403         (setq n (1- n)))
8404       (when (/= 0 n)
8405         (gnus-message 7 "No more articles"))
8406       (gnus-summary-recenter)
8407       (gnus-summary-position-point)
8408       n)))
8409
8410 (defun gnus-summary-unmark-as-processable (n)
8411   "Remove the process mark from the next N articles.
8412 If N is negative, unmark backward instead.  The difference between N and
8413 the actual number of articles unmarked is returned."
8414   (interactive "P")
8415   (gnus-summary-mark-as-processable n t))
8416
8417 (defun gnus-summary-unmark-all-processable ()
8418   "Remove the process mark from all articles."
8419   (interactive)
8420   (save-excursion
8421     (while gnus-newsgroup-processable
8422       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8423   (gnus-summary-position-point))
8424
8425 (defun gnus-summary-add-mark (article type)
8426   "Mark ARTICLE with a mark of TYPE."
8427   (let ((vtype (car (assq type gnus-article-mark-lists)))
8428         var)
8429     (if (not vtype)
8430         (error "No such mark type: %s" type)
8431       (setq var (intern (format "gnus-newsgroup-%s" type)))
8432       (set var (cons article (symbol-value var)))
8433       (if (memq type '(processable cached replied forwarded saved))
8434           (gnus-summary-update-secondary-mark article)
8435         ;;; !!! This is bobus.  We should find out what primary
8436         ;;; !!! mark we want to set.
8437         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8438
8439 (defun gnus-summary-mark-as-expirable (n)
8440   "Mark N articles forward as expirable.
8441 If N is negative, mark backward instead.  The difference between N and
8442 the actual number of articles marked is returned."
8443   (interactive "p")
8444   (gnus-summary-mark-forward n gnus-expirable-mark))
8445
8446 (defun gnus-summary-mark-article-as-replied (article)
8447   "Mark ARTICLE as replied to and update the summary line.
8448 ARTICLE can also be a list of articles."
8449   (interactive (list (gnus-summary-article-number)))
8450   (let ((articles (if (listp article) article (list article))))
8451     (dolist (article articles)
8452       (push article gnus-newsgroup-replied)
8453       (let ((buffer-read-only nil))
8454         (when (gnus-summary-goto-subject article nil t)
8455           (gnus-summary-update-secondary-mark article))))))
8456
8457 (defun gnus-summary-mark-article-as-forwarded (article)
8458   "Mark ARTICLE as forwarded and update the summary line.
8459 ARTICLE can also be a list of articles."
8460   (let ((articles (if (listp article) article (list article))))
8461     (dolist (article articles)
8462       (push article gnus-newsgroup-forwarded)
8463       (let ((buffer-read-only nil))
8464         (when (gnus-summary-goto-subject article nil t)
8465           (gnus-summary-update-secondary-mark article))))))
8466
8467 (defun gnus-summary-set-bookmark (article)
8468   "Set a bookmark in current article."
8469   (interactive (list (gnus-summary-article-number)))
8470   (when (or (not (get-buffer gnus-article-buffer))
8471             (not gnus-current-article)
8472             (not gnus-article-current)
8473             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8474     (error "No current article selected"))
8475   ;; Remove old bookmark, if one exists.
8476   (let ((old (assq article gnus-newsgroup-bookmarks)))
8477     (when old
8478       (setq gnus-newsgroup-bookmarks
8479             (delq old gnus-newsgroup-bookmarks))))
8480   ;; Set the new bookmark, which is on the form
8481   ;; (article-number . line-number-in-body).
8482   (push
8483    (cons article
8484          (save-excursion
8485            (set-buffer gnus-article-buffer)
8486            (count-lines
8487             (min (point)
8488                  (save-excursion
8489                    (goto-char (point-min))
8490                    (search-forward "\n\n" nil t)
8491                    (point)))
8492             (point))))
8493    gnus-newsgroup-bookmarks)
8494   (gnus-message 6 "A bookmark has been added to the current article."))
8495
8496 (defun gnus-summary-remove-bookmark (article)
8497   "Remove the bookmark from the current article."
8498   (interactive (list (gnus-summary-article-number)))
8499   ;; Remove old bookmark, if one exists.
8500   (let ((old (assq article gnus-newsgroup-bookmarks)))
8501     (if old
8502         (progn
8503           (setq gnus-newsgroup-bookmarks
8504                 (delq old gnus-newsgroup-bookmarks))
8505           (gnus-message 6 "Removed bookmark."))
8506       (gnus-message 6 "No bookmark in current article."))))
8507
8508 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8509 (defun gnus-summary-mark-as-dormant (n)
8510   "Mark N articles forward as dormant.
8511 If N is negative, mark backward instead.  The difference between N and
8512 the actual number of articles marked is returned."
8513   (interactive "p")
8514   (gnus-summary-mark-forward n gnus-dormant-mark))
8515
8516 (defun gnus-summary-set-process-mark (article)
8517   "Set the process mark on ARTICLE and update the summary line."
8518   (setq gnus-newsgroup-processable
8519         (cons article
8520               (delq article gnus-newsgroup-processable)))
8521   (when (gnus-summary-goto-subject article)
8522     (gnus-summary-show-thread)
8523     (gnus-summary-goto-subject article)
8524     (gnus-summary-update-secondary-mark article)))
8525
8526 (defun gnus-summary-remove-process-mark (article)
8527   "Remove the process mark from ARTICLE and update the summary line."
8528   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8529   (when (gnus-summary-goto-subject article)
8530     (gnus-summary-show-thread)
8531     (gnus-summary-goto-subject article)
8532     (gnus-summary-update-secondary-mark article)))
8533
8534 (defun gnus-summary-set-saved-mark (article)
8535   "Set the process mark on ARTICLE and update the summary line."
8536   (push article gnus-newsgroup-saved)
8537   (when (gnus-summary-goto-subject article)
8538     (gnus-summary-update-secondary-mark article)))
8539
8540 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8541   "Mark N articles as read forwards.
8542 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8543 The difference between N and the actual number of articles marked is
8544 returned.
8545 Iff NO-EXPIRE, auto-expiry will be inhibited."
8546   (interactive "p")
8547   (gnus-summary-show-thread)
8548   (let ((backward (< n 0))
8549         (gnus-summary-goto-unread
8550          (and gnus-summary-goto-unread
8551               (not (eq gnus-summary-goto-unread 'never))
8552               (not (memq mark (list gnus-unread-mark
8553                                     gnus-ticked-mark gnus-dormant-mark)))))
8554         (n (abs n))
8555         (mark (or mark gnus-del-mark)))
8556     (while (and (> n 0)
8557                 (gnus-summary-mark-article nil mark no-expire)
8558                 (zerop (gnus-summary-next-subject
8559                         (if backward -1 1)
8560                         (and gnus-summary-goto-unread
8561                              (not (eq gnus-summary-goto-unread 'never)))
8562                         t)))
8563       (setq n (1- n)))
8564     (when (/= 0 n)
8565       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8566     (gnus-summary-recenter)
8567     (gnus-summary-position-point)
8568     (gnus-set-mode-line 'summary)
8569     n))
8570
8571 (defun gnus-summary-mark-article-as-read (mark)
8572   "Mark the current article quickly as read with MARK."
8573   (let ((article (gnus-summary-article-number)))
8574     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8575     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8576     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8577     (push (cons article mark) gnus-newsgroup-reads)
8578     ;; Possibly remove from cache, if that is used.
8579     (when gnus-use-cache
8580       (gnus-cache-enter-remove-article article))
8581     ;; Allow the backend to change the mark.
8582     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8583     ;; Check for auto-expiry.
8584     (when (and gnus-newsgroup-auto-expire
8585                (memq mark gnus-auto-expirable-marks))
8586       (setq mark gnus-expirable-mark)
8587       ;; Let the backend know about the mark change.
8588       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8589       (push article gnus-newsgroup-expirable))
8590     ;; Set the mark in the buffer.
8591     (gnus-summary-update-mark mark 'unread)
8592     t))
8593
8594 (defun gnus-summary-mark-article-as-unread (mark)
8595   "Mark the current article quickly as unread with MARK."
8596   (let* ((article (gnus-summary-article-number))
8597          (old-mark (gnus-summary-article-mark article)))
8598     ;; Allow the backend to change the mark.
8599     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8600     (if (eq mark old-mark)
8601         t
8602       (if (<= article 0)
8603           (progn
8604             (gnus-error 1 "Can't mark negative article numbers")
8605             nil)
8606         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8607         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8608         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8609         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8610         (cond ((= mark gnus-ticked-mark)
8611                (push article gnus-newsgroup-marked))
8612               ((= mark gnus-dormant-mark)
8613                (push article gnus-newsgroup-dormant))
8614               (t
8615                (push article gnus-newsgroup-unreads)))
8616         (gnus-pull article gnus-newsgroup-reads)
8617
8618         ;; See whether the article is to be put in the cache.
8619         (and gnus-use-cache
8620              (vectorp (gnus-summary-article-header article))
8621              (save-excursion
8622                (gnus-cache-possibly-enter-article
8623                 gnus-newsgroup-name article
8624                 (= mark gnus-ticked-mark)
8625                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8626
8627         ;; Fix the mark.
8628         (gnus-summary-update-mark mark 'unread)
8629         t))))
8630
8631 (defun gnus-summary-mark-article (&optional article mark no-expire)
8632   "Mark ARTICLE with MARK.  MARK can be any character.
8633 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8634 `??' (dormant) and `?E' (expirable).
8635 If MARK is nil, then the default character `?r' is used.
8636 If ARTICLE is nil, then the article on the current line will be
8637 marked.
8638 Iff NO-EXPIRE, auto-expiry will be inhibited."
8639   ;; The mark might be a string.
8640   (when (stringp mark)
8641     (setq mark (aref mark 0)))
8642   ;; If no mark is given, then we check auto-expiring.
8643   (when (null mark)
8644     (setq mark gnus-del-mark))
8645   (when (and (not no-expire)
8646              gnus-newsgroup-auto-expire
8647              (memq mark gnus-auto-expirable-marks))
8648     (setq mark gnus-expirable-mark))
8649   (let ((article (or article (gnus-summary-article-number)))
8650         (old-mark (gnus-summary-article-mark article)))
8651     ;; Allow the backend to change the mark.
8652     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8653     (if (eq mark old-mark)
8654         t
8655       (unless article
8656         (error "No article on current line"))
8657       (if (not (if (or (= mark gnus-unread-mark)
8658                        (= mark gnus-ticked-mark)
8659                        (= mark gnus-dormant-mark))
8660                    (gnus-mark-article-as-unread article mark)
8661                  (gnus-mark-article-as-read article mark)))
8662           t
8663         ;; See whether the article is to be put in the cache.
8664         (and gnus-use-cache
8665              (not (= mark gnus-canceled-mark))
8666              (vectorp (gnus-summary-article-header article))
8667              (save-excursion
8668                (gnus-cache-possibly-enter-article
8669                 gnus-newsgroup-name article
8670                 (= mark gnus-ticked-mark)
8671                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8672
8673         (when (gnus-summary-goto-subject article nil t)
8674           (let ((buffer-read-only nil))
8675             (gnus-summary-show-thread)
8676             ;; Fix the mark.
8677             (gnus-summary-update-mark mark 'unread)
8678             t))))))
8679
8680 (defun gnus-summary-update-secondary-mark (article)
8681   "Update the secondary (read, process, cache) mark."
8682   (gnus-summary-update-mark
8683    (cond ((memq article gnus-newsgroup-processable)
8684           gnus-process-mark)
8685          ((memq article gnus-newsgroup-cached)
8686           gnus-cached-mark)
8687          ((memq article gnus-newsgroup-replied)
8688           gnus-replied-mark)
8689          ((memq article gnus-newsgroup-forwarded)
8690           gnus-forwarded-mark)
8691          ((memq article gnus-newsgroup-saved)
8692           gnus-saved-mark)
8693          (t gnus-no-mark))
8694    'replied)
8695   (when (gnus-visual-p 'summary-highlight 'highlight)
8696     (gnus-run-hooks 'gnus-summary-update-hook))
8697   t)
8698
8699 (defun gnus-summary-update-mark (mark type)
8700   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8701         (buffer-read-only nil))
8702     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8703     (when forward
8704       (when (looking-at "\r")
8705         (incf forward))
8706       (when (<= (+ forward (point)) (point-max))
8707         ;; Go to the right position on the line.
8708         (goto-char (+ forward (point)))
8709         ;; Replace the old mark with the new mark.
8710         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8711         ;; Optionally update the marks by some user rule.
8712         (when (eq type 'unread)
8713           (gnus-data-set-mark
8714            (gnus-data-find (gnus-summary-article-number)) mark)
8715           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8716
8717 (defun gnus-mark-article-as-read (article &optional mark)
8718   "Enter ARTICLE in the pertinent lists and remove it from others."
8719   ;; Make the article expirable.
8720   (let ((mark (or mark gnus-del-mark)))
8721     (if (= mark gnus-expirable-mark)
8722         (push article gnus-newsgroup-expirable)
8723       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8724     ;; Remove from unread and marked lists.
8725     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8726     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8727     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8728     (push (cons article mark) gnus-newsgroup-reads)
8729     ;; Possibly remove from cache, if that is used.
8730     (when gnus-use-cache
8731       (gnus-cache-enter-remove-article article))
8732     t))
8733
8734 (defun gnus-mark-article-as-unread (article &optional mark)
8735   "Enter ARTICLE in the pertinent lists and remove it from others."
8736   (let ((mark (or mark gnus-ticked-mark)))
8737     (if (<= article 0)
8738         (progn
8739           (gnus-error 1 "Can't mark negative article numbers")
8740           nil)
8741       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8742             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8743             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8744             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8745
8746       ;; Unsuppress duplicates?
8747       (when gnus-suppress-duplicates
8748         (gnus-dup-unsuppress-article article))
8749
8750       (cond ((= mark gnus-ticked-mark)
8751              (push article gnus-newsgroup-marked))
8752             ((= mark gnus-dormant-mark)
8753              (push article gnus-newsgroup-dormant))
8754             (t
8755              (push article gnus-newsgroup-unreads)))
8756       (gnus-pull article gnus-newsgroup-reads)
8757       t)))
8758
8759 (defalias 'gnus-summary-mark-as-unread-forward
8760   'gnus-summary-tick-article-forward)
8761 (make-obsolete 'gnus-summary-mark-as-unread-forward
8762                'gnus-summary-tick-article-forward)
8763 (defun gnus-summary-tick-article-forward (n)
8764   "Tick N articles forwards.
8765 If N is negative, tick backwards instead.
8766 The difference between N and the number of articles ticked is returned."
8767   (interactive "p")
8768   (gnus-summary-mark-forward n gnus-ticked-mark))
8769
8770 (defalias 'gnus-summary-mark-as-unread-backward
8771   'gnus-summary-tick-article-backward)
8772 (make-obsolete 'gnus-summary-mark-as-unread-backward
8773                'gnus-summary-tick-article-backward)
8774 (defun gnus-summary-tick-article-backward (n)
8775   "Tick N articles backwards.
8776 The difference between N and the number of articles ticked is returned."
8777   (interactive "p")
8778   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8779
8780 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8781 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8782 (defun gnus-summary-tick-article (&optional article clear-mark)
8783   "Mark current article as unread.
8784 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8785 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8786   (interactive)
8787   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8788                                        gnus-ticked-mark)))
8789
8790 (defun gnus-summary-mark-as-read-forward (n)
8791   "Mark N articles as read forwards.
8792 If N is negative, mark backwards instead.
8793 The difference between N and the actual number of articles marked is
8794 returned."
8795   (interactive "p")
8796   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8797
8798 (defun gnus-summary-mark-as-read-backward (n)
8799   "Mark the N articles as read backwards.
8800 The difference between N and the actual number of articles marked is
8801 returned."
8802   (interactive "p")
8803   (gnus-summary-mark-forward
8804    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8805
8806 (defun gnus-summary-mark-as-read (&optional article mark)
8807   "Mark current article as read.
8808 ARTICLE specifies the article to be marked as read.
8809 MARK specifies a string to be inserted at the beginning of the line."
8810   (gnus-summary-mark-article article mark))
8811
8812 (defun gnus-summary-clear-mark-forward (n)
8813   "Clear marks from N articles forward.
8814 If N is negative, clear backward instead.
8815 The difference between N and the number of marks cleared is returned."
8816   (interactive "p")
8817   (gnus-summary-mark-forward n gnus-unread-mark))
8818
8819 (defun gnus-summary-clear-mark-backward (n)
8820   "Clear marks from N articles backward.
8821 The difference between N and the number of marks cleared is returned."
8822   (interactive "p")
8823   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8824
8825 (defun gnus-summary-mark-unread-as-read ()
8826   "Intended to be used by `gnus-summary-mark-article-hook'."
8827   (when (memq gnus-current-article gnus-newsgroup-unreads)
8828     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8829
8830 (defun gnus-summary-mark-read-and-unread-as-read ()
8831   "Intended to be used by `gnus-summary-mark-article-hook'."
8832   (let ((mark (gnus-summary-article-mark)))
8833     (when (or (gnus-unread-mark-p mark)
8834               (gnus-read-mark-p mark))
8835       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8836
8837 (defun gnus-summary-mark-unread-as-ticked ()
8838    "Intended to be used by `gnus-summary-mark-article-hook'."
8839   (when (memq gnus-current-article gnus-newsgroup-unreads)
8840     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8841
8842 (defun gnus-summary-mark-region-as-read (point mark all)
8843   "Mark all unread articles between point and mark as read.
8844 If given a prefix, mark all articles between point and mark as read,
8845 even ticked and dormant ones."
8846   (interactive "r\nP")
8847   (save-excursion
8848     (let (article)
8849       (goto-char point)
8850       (beginning-of-line)
8851       (while (and
8852               (< (point) mark)
8853               (progn
8854                 (when (or all
8855                           (memq (setq article (gnus-summary-article-number))
8856                                 gnus-newsgroup-unreads))
8857                   (gnus-summary-mark-article article gnus-del-mark))
8858                 t)
8859               (gnus-summary-find-next))))))
8860
8861 (defun gnus-summary-mark-below (score mark)
8862   "Mark articles with score less than SCORE with MARK."
8863   (interactive "P\ncMark: ")
8864   (setq score (if score
8865                   (prefix-numeric-value score)
8866                 (or gnus-summary-default-score 0)))
8867   (save-excursion
8868     (set-buffer gnus-summary-buffer)
8869     (goto-char (point-min))
8870     (while
8871         (progn
8872           (and (< (gnus-summary-article-score) score)
8873                (gnus-summary-mark-article nil mark))
8874           (gnus-summary-find-next)))))
8875
8876 (defun gnus-summary-kill-below (&optional score)
8877   "Mark articles with score below SCORE as read."
8878   (interactive "P")
8879   (gnus-summary-mark-below score gnus-killed-mark))
8880
8881 (defun gnus-summary-clear-above (&optional score)
8882   "Clear all marks from articles with score above SCORE."
8883   (interactive "P")
8884   (gnus-summary-mark-above score gnus-unread-mark))
8885
8886 (defun gnus-summary-tick-above (&optional score)
8887   "Tick all articles with score above SCORE."
8888   (interactive "P")
8889   (gnus-summary-mark-above score gnus-ticked-mark))
8890
8891 (defun gnus-summary-mark-above (score mark)
8892   "Mark articles with score over SCORE with MARK."
8893   (interactive "P\ncMark: ")
8894   (setq score (if score
8895                   (prefix-numeric-value score)
8896                 (or gnus-summary-default-score 0)))
8897   (save-excursion
8898     (set-buffer gnus-summary-buffer)
8899     (goto-char (point-min))
8900     (while (and (progn
8901                   (when (> (gnus-summary-article-score) score)
8902                     (gnus-summary-mark-article nil mark))
8903                   t)
8904                 (gnus-summary-find-next)))))
8905
8906 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8907 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8908 (defun gnus-summary-limit-include-expunged (&optional no-error)
8909   "Display all the hidden articles that were expunged for low scores."
8910   (interactive)
8911   (let ((buffer-read-only nil))
8912     (let ((scored gnus-newsgroup-scored)
8913           headers h)
8914       (while scored
8915         (unless (gnus-summary-article-header (caar scored))
8916           (and (setq h (gnus-number-to-header (caar scored)))
8917                (< (cdar scored) gnus-summary-expunge-below)
8918                (push h headers)))
8919         (setq scored (cdr scored)))
8920       (if (not headers)
8921           (when (not no-error)
8922             (error "No expunged articles hidden"))
8923         (goto-char (point-min))
8924         (push gnus-newsgroup-limit gnus-newsgroup-limits)
8925         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
8926         (mapcar (lambda (x) (push (mail-header-number x) 
8927                                   gnus-newsgroup-limit))
8928                 headers)
8929         (gnus-summary-prepare-unthreaded (nreverse headers))
8930         (goto-char (point-min))
8931         (gnus-summary-position-point)
8932         t))))
8933
8934 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8935   "Mark all unread articles in this newsgroup as read.
8936 If prefix argument ALL is non-nil, ticked and dormant articles will
8937 also be marked as read.
8938 If QUIETLY is non-nil, no questions will be asked.
8939 If TO-HERE is non-nil, it should be a point in the buffer.  All
8940 articles before this point will be marked as read.
8941 Note that this function will only catch up the unread article
8942 in the current summary buffer limitation.
8943 The number of articles marked as read is returned."
8944   (interactive "P")
8945   (prog1
8946       (save-excursion
8947         (when (or quietly
8948                   (not gnus-interactive-catchup) ;Without confirmation?
8949                   gnus-expert-user
8950                   (gnus-y-or-n-p
8951                    (if all
8952                        "Mark absolutely all articles as read? "
8953                      "Mark all unread articles as read? ")))
8954           (if (and not-mark
8955                    (not gnus-newsgroup-adaptive)
8956                    (not gnus-newsgroup-auto-expire)
8957                    (not gnus-suppress-duplicates)
8958                    (or (not gnus-use-cache)
8959                        (eq gnus-use-cache 'passive)))
8960               (progn
8961                 (when all
8962                   (setq gnus-newsgroup-marked nil
8963                         gnus-newsgroup-dormant nil))
8964                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8965             ;; We actually mark all articles as canceled, which we
8966             ;; have to do when using auto-expiry or adaptive scoring.
8967             (gnus-summary-show-all-threads)
8968             (when (gnus-summary-first-subject (not all) t)
8969               (while (and
8970                       (if to-here (< (point) to-here) t)
8971                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8972                       (gnus-summary-find-next (not all) nil nil t))))
8973             (gnus-set-mode-line 'summary))
8974           t))
8975     (gnus-summary-position-point)))
8976
8977 (defun gnus-summary-catchup-to-here (&optional all)
8978   "Mark all unticked articles before the current one as read.
8979 If ALL is non-nil, also mark ticked and dormant articles as read."
8980   (interactive "P")
8981   (save-excursion
8982     (gnus-save-hidden-threads
8983       (let ((beg (point)))
8984         ;; We check that there are unread articles.
8985         (when (or all (gnus-summary-find-prev))
8986           (gnus-summary-catchup all t beg)))))
8987   (gnus-summary-position-point))
8988
8989 (defun gnus-summary-catchup-all (&optional quietly)
8990   "Mark all articles in this newsgroup as read."
8991   (interactive "P")
8992   (gnus-summary-catchup t quietly))
8993
8994 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8995   "Mark all unread articles in this group as read, then exit.
8996 If prefix argument ALL is non-nil, all articles are marked as read.
8997 If QUIETLY is non-nil, no questions will be asked."
8998   (interactive "P")
8999   (when (gnus-summary-catchup all quietly nil 'fast)
9000     ;; Select next newsgroup or exit.
9001     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9002              (eq gnus-auto-select-next 'quietly))
9003         (gnus-summary-next-group nil)
9004       (gnus-summary-exit))))
9005
9006 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9007   "Mark all articles in this newsgroup as read, and then exit."
9008   (interactive "P")
9009   (gnus-summary-catchup-and-exit t quietly))
9010
9011 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9012   "Mark all articles in this group as read and select the next group.
9013 If given a prefix, mark all articles, unread as well as ticked, as
9014 read."
9015   (interactive "P")
9016   (save-excursion
9017     (gnus-summary-catchup all))
9018   (gnus-summary-next-group))
9019
9020 ;;;
9021 ;;; with article
9022 ;;;
9023
9024 (defmacro gnus-with-article (article &rest forms)
9025   "Select ARTICLE and perform FORMS in the original article buffer.
9026 Then replace the article with the result."
9027   `(progn
9028      ;; We don't want the article to be marked as read.
9029      (let (gnus-mark-article-hook)
9030        (gnus-summary-select-article t t nil ,article))
9031      (set-buffer gnus-original-article-buffer)
9032      ,@forms
9033      (if (not (gnus-check-backend-function
9034                'request-replace-article (car gnus-article-current)))
9035          (gnus-message 5 "Read-only group; not replacing")
9036        (unless (gnus-request-replace-article
9037                 ,article (car gnus-article-current)
9038                 (current-buffer) t)
9039          (error "Couldn't replace article")))
9040      ;; The cache and backlog have to be flushed somewhat.
9041      (when gnus-keep-backlog
9042        (gnus-backlog-remove-article
9043         (car gnus-article-current) (cdr gnus-article-current)))
9044      (when gnus-use-cache
9045        (gnus-cache-update-article
9046         (car gnus-article-current) (cdr gnus-article-current)))))
9047
9048 (put 'gnus-with-article 'lisp-indent-function 1)
9049 (put 'gnus-with-article 'edebug-form-spec '(form body))
9050
9051 ;; Thread-based commands.
9052
9053 (defun gnus-summary-articles-in-thread (&optional article)
9054   "Return a list of all articles in the current thread.
9055 If ARTICLE is non-nil, return all articles in the thread that starts
9056 with that article."
9057   (let* ((article (or article (gnus-summary-article-number)))
9058          (data (gnus-data-find-list article))
9059          (top-level (gnus-data-level (car data)))
9060          (top-subject
9061           (cond ((null gnus-thread-operation-ignore-subject)
9062                  (gnus-simplify-subject-re
9063                   (mail-header-subject (gnus-data-header (car data)))))
9064                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9065                  (gnus-simplify-subject-fuzzy
9066                   (mail-header-subject (gnus-data-header (car data)))))
9067                 (t nil)))
9068          (end-point (save-excursion
9069                       (if (gnus-summary-go-to-next-thread)
9070                           (point) (point-max))))
9071          articles)
9072     (while (and data
9073                 (< (gnus-data-pos (car data)) end-point))
9074       (when (or (not top-subject)
9075                 (string= top-subject
9076                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9077                              (gnus-simplify-subject-fuzzy
9078                               (mail-header-subject
9079                                (gnus-data-header (car data))))
9080                            (gnus-simplify-subject-re
9081                             (mail-header-subject
9082                              (gnus-data-header (car data)))))))
9083         (push (gnus-data-number (car data)) articles))
9084       (unless (and (setq data (cdr data))
9085                    (> (gnus-data-level (car data)) top-level))
9086         (setq data nil)))
9087     ;; Return the list of articles.
9088     (nreverse articles)))
9089
9090 (defun gnus-summary-rethread-current ()
9091   "Rethread the thread the current article is part of."
9092   (interactive)
9093   (let* ((gnus-show-threads t)
9094          (article (gnus-summary-article-number))
9095          (id (mail-header-id (gnus-summary-article-header)))
9096          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9097     (unless id
9098       (error "No article on the current line"))
9099     (gnus-rebuild-thread id)
9100     (gnus-summary-goto-subject article)))
9101
9102 (defun gnus-summary-reparent-thread ()
9103   "Make the current article child of the marked (or previous) article.
9104
9105 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9106 is non-nil or the Subject: of both articles are the same."
9107   (interactive)
9108   (unless (not (gnus-group-read-only-p))
9109     (error "The current newsgroup does not support article editing"))
9110   (unless (<= (length gnus-newsgroup-processable) 1)
9111     (error "No more than one article may be marked"))
9112   (save-window-excursion
9113     (let ((gnus-article-buffer " *reparent*")
9114           (current-article (gnus-summary-article-number))
9115           ;; First grab the marked article, otherwise one line up.
9116           (parent-article (if (not (null gnus-newsgroup-processable))
9117                               (car gnus-newsgroup-processable)
9118                             (save-excursion
9119                               (if (eq (forward-line -1) 0)
9120                                   (gnus-summary-article-number)
9121                                 (error "Beginning of summary buffer"))))))
9122       (unless (not (eq current-article parent-article))
9123         (error "An article may not be self-referential"))
9124       (let ((message-id (mail-header-id
9125                          (gnus-summary-article-header parent-article))))
9126         (unless (and message-id (not (equal message-id "")))
9127           (error "No message-id in desired parent"))
9128         (gnus-with-article current-article
9129           (save-restriction
9130             (goto-char (point-min))
9131             (message-narrow-to-head)
9132             (if (re-search-forward "^References: " nil t)
9133                 (progn
9134                   (re-search-forward "^[^ \t]" nil t)
9135                   (forward-line -1)
9136                   (end-of-line)
9137                   (insert " " message-id))
9138               (insert "References: " message-id "\n"))))
9139         (set-buffer gnus-summary-buffer)
9140         (gnus-summary-unmark-all-processable)
9141         (gnus-summary-update-article current-article)
9142         (gnus-summary-rethread-current)
9143         (gnus-message 3 "Article %d is now the child of article %d"
9144                       current-article parent-article)))))
9145
9146 (defun gnus-summary-toggle-threads (&optional arg)
9147   "Toggle showing conversation threads.
9148 If ARG is positive number, turn showing conversation threads on."
9149   (interactive "P")
9150   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9151     (setq gnus-show-threads
9152           (if (null arg) (not gnus-show-threads)
9153             (> (prefix-numeric-value arg) 0)))
9154     (gnus-summary-prepare)
9155     (gnus-summary-goto-subject current)
9156     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9157     (gnus-summary-position-point)))
9158
9159 (defun gnus-summary-show-all-threads ()
9160   "Show all threads."
9161   (interactive)
9162   (save-excursion
9163     (let ((buffer-read-only nil))
9164       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9165   (gnus-summary-position-point))
9166
9167 (defun gnus-summary-show-thread ()
9168   "Show thread subtrees.
9169 Returns nil if no thread was there to be shown."
9170   (interactive)
9171   (let ((buffer-read-only nil)
9172         (orig (point))
9173         ;; first goto end then to beg, to have point at beg after let
9174         (end (progn (end-of-line) (point)))
9175         (beg (progn (beginning-of-line) (point))))
9176     (prog1
9177         ;; Any hidden lines here?
9178         (search-forward "\r" end t)
9179       (subst-char-in-region beg end ?\^M ?\n t)
9180       (goto-char orig)
9181       (gnus-summary-position-point))))
9182
9183 (defun gnus-summary-hide-all-threads ()
9184   "Hide all thread subtrees."
9185   (interactive)
9186   (save-excursion
9187     (goto-char (point-min))
9188     (gnus-summary-hide-thread)
9189     (while (zerop (gnus-summary-next-thread 1 t))
9190       (gnus-summary-hide-thread)))
9191   (gnus-summary-position-point))
9192
9193 (defun gnus-summary-hide-thread ()
9194   "Hide thread subtrees.
9195 Returns nil if no threads were there to be hidden."
9196   (interactive)
9197   (let ((buffer-read-only nil)
9198         (start (point))
9199         (article (gnus-summary-article-number)))
9200     (goto-char start)
9201     ;; Go forward until either the buffer ends or the subthread
9202     ;; ends.
9203     (when (and (not (eobp))
9204                (or (zerop (gnus-summary-next-thread 1 t))
9205                    (goto-char (point-max))))
9206       (prog1
9207           (if (and (> (point) start)
9208                    (search-backward "\n" start t))
9209               (progn
9210                 (subst-char-in-region start (point) ?\n ?\^M)
9211                 (gnus-summary-goto-subject article))
9212             (goto-char start)
9213             nil)))))
9214
9215 (defun gnus-summary-go-to-next-thread (&optional previous)
9216   "Go to the same level (or less) next thread.
9217 If PREVIOUS is non-nil, go to previous thread instead.
9218 Return the article number moved to, or nil if moving was impossible."
9219   (let ((level (gnus-summary-thread-level))
9220         (way (if previous -1 1))
9221         (beg (point)))
9222     (forward-line way)
9223     (while (and (not (eobp))
9224                 (< level (gnus-summary-thread-level)))
9225       (forward-line way))
9226     (if (eobp)
9227         (progn
9228           (goto-char beg)
9229           nil)
9230       (setq beg (point))
9231       (prog1
9232           (gnus-summary-article-number)
9233         (goto-char beg)))))
9234
9235 (defun gnus-summary-next-thread (n &optional silent)
9236   "Go to the same level next N'th thread.
9237 If N is negative, search backward instead.
9238 Returns the difference between N and the number of skips actually
9239 done.
9240
9241 If SILENT, don't output messages."
9242   (interactive "p")
9243   (let ((backward (< n 0))
9244         (n (abs n)))
9245     (while (and (> n 0)
9246                 (gnus-summary-go-to-next-thread backward))
9247       (decf n))
9248     (unless silent
9249       (gnus-summary-position-point))
9250     (when (and (not silent) (/= 0 n))
9251       (gnus-message 7 "No more threads"))
9252     n))
9253
9254 (defun gnus-summary-prev-thread (n)
9255   "Go to the same level previous N'th thread.
9256 Returns the difference between N and the number of skips actually
9257 done."
9258   (interactive "p")
9259   (gnus-summary-next-thread (- n)))
9260
9261 (defun gnus-summary-go-down-thread ()
9262   "Go down one level in the current thread."
9263   (let ((children (gnus-summary-article-children)))
9264     (when children
9265       (gnus-summary-goto-subject (car children)))))
9266
9267 (defun gnus-summary-go-up-thread ()
9268   "Go up one level in the current thread."
9269   (let ((parent (gnus-summary-article-parent)))
9270     (when parent
9271       (gnus-summary-goto-subject parent))))
9272
9273 (defun gnus-summary-down-thread (n)
9274   "Go down thread N steps.
9275 If N is negative, go up instead.
9276 Returns the difference between N and how many steps down that were
9277 taken."
9278   (interactive "p")
9279   (let ((up (< n 0))
9280         (n (abs n)))
9281     (while (and (> n 0)
9282                 (if up (gnus-summary-go-up-thread)
9283                   (gnus-summary-go-down-thread)))
9284       (setq n (1- n)))
9285     (gnus-summary-position-point)
9286     (when (/= 0 n)
9287       (gnus-message 7 "Can't go further"))
9288     n))
9289
9290 (defun gnus-summary-up-thread (n)
9291   "Go up thread N steps.
9292 If N is negative, go down instead.
9293 Returns the difference between N and how many steps down that were
9294 taken."
9295   (interactive "p")
9296   (gnus-summary-down-thread (- n)))
9297
9298 (defun gnus-summary-top-thread ()
9299   "Go to the top of the thread."
9300   (interactive)
9301   (while (gnus-summary-go-up-thread))
9302   (gnus-summary-article-number))
9303
9304 (defun gnus-summary-kill-thread (&optional unmark)
9305   "Mark articles under current thread as read.
9306 If the prefix argument is positive, remove any kinds of marks.
9307 If the prefix argument is negative, tick articles instead."
9308   (interactive "P")
9309   (when unmark
9310     (setq unmark (prefix-numeric-value unmark)))
9311   (let ((articles (gnus-summary-articles-in-thread)))
9312     (save-excursion
9313       ;; Expand the thread.
9314       (gnus-summary-show-thread)
9315       ;; Mark all the articles.
9316       (while articles
9317         (gnus-summary-goto-subject (car articles))
9318         (cond ((null unmark)
9319                (gnus-summary-mark-article-as-read gnus-killed-mark))
9320               ((> unmark 0)
9321                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9322               (t
9323                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9324         (setq articles (cdr articles))))
9325     ;; Hide killed subtrees.
9326     (and (null unmark)
9327          gnus-thread-hide-killed
9328          (gnus-summary-hide-thread))
9329     ;; If marked as read, go to next unread subject.
9330     (when (null unmark)
9331       ;; Go to next unread subject.
9332       (gnus-summary-next-subject 1 t)))
9333   (gnus-set-mode-line 'summary))
9334
9335 ;; Summary sorting commands
9336
9337 (defun gnus-summary-sort-by-number (&optional reverse)
9338   "Sort the summary buffer by article number.
9339 Argument REVERSE means reverse order."
9340   (interactive "P")
9341   (gnus-summary-sort 'number reverse))
9342
9343 (defun gnus-summary-sort-by-author (&optional reverse)
9344   "Sort the summary buffer by author name alphabetically.
9345 If `case-fold-search' is non-nil, case of letters is ignored.
9346 Argument REVERSE means reverse order."
9347   (interactive "P")
9348   (gnus-summary-sort 'author reverse))
9349
9350 (defun gnus-summary-sort-by-subject (&optional reverse)
9351   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9352 If `case-fold-search' is non-nil, case of letters is ignored.
9353 Argument REVERSE means reverse order."
9354   (interactive "P")
9355   (gnus-summary-sort 'subject reverse))
9356
9357 (defun gnus-summary-sort-by-date (&optional reverse)
9358   "Sort the summary buffer by date.
9359 Argument REVERSE means reverse order."
9360   (interactive "P")
9361   (gnus-summary-sort 'date reverse))
9362
9363 (defun gnus-summary-sort-by-score (&optional reverse)
9364   "Sort the summary buffer by score.
9365 Argument REVERSE means reverse order."
9366   (interactive "P")
9367   (gnus-summary-sort 'score reverse))
9368
9369 (defun gnus-summary-sort-by-lines (&optional reverse)
9370   "Sort the summary buffer by the number of lines.
9371 Argument REVERSE means reverse order."
9372   (interactive "P")
9373   (gnus-summary-sort 'lines reverse))
9374
9375 (defun gnus-summary-sort-by-chars (&optional reverse)
9376   "Sort the summary buffer by article length.
9377 Argument REVERSE means reverse order."
9378   (interactive "P")
9379   (gnus-summary-sort 'chars reverse))
9380
9381 (defun gnus-summary-sort-by-original (&optional reverse)
9382   "Sort the summary buffer using the default sorting method.
9383 Argument REVERSE means reverse order."
9384   (interactive "P")
9385   (let* ((buffer-read-only)
9386          (gnus-summary-prepare-hook nil))
9387     ;; We do the sorting by regenerating the threads.
9388     (gnus-summary-prepare)
9389     ;; Hide subthreads if needed.
9390     (when (and gnus-show-threads gnus-thread-hide-subtree)
9391       (gnus-summary-hide-all-threads))))
9392
9393 (defun gnus-summary-sort (predicate reverse)
9394   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9395   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9396          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9397          (gnus-thread-sort-functions
9398           (if (not reverse)
9399               thread
9400             `(lambda (t1 t2)
9401                (,thread t2 t1))))
9402          (gnus-sort-gathered-threads-function
9403           gnus-thread-sort-functions)
9404          (gnus-article-sort-functions
9405           (if (not reverse)
9406               article
9407             `(lambda (t1 t2)
9408                (,article t2 t1))))
9409          (buffer-read-only)
9410          (gnus-summary-prepare-hook nil))
9411     ;; We do the sorting by regenerating the threads.
9412     (gnus-summary-prepare)
9413     ;; Hide subthreads if needed.
9414     (when (and gnus-show-threads gnus-thread-hide-subtree)
9415       (gnus-summary-hide-all-threads))))
9416
9417 ;; Summary saving commands.
9418
9419 (defun gnus-summary-save-article (&optional n not-saved)
9420   "Save the current article using the default saver function.
9421 If N is a positive number, save the N next articles.
9422 If N is a negative number, save the N previous articles.
9423 If N is nil and any articles have been marked with the process mark,
9424 save those articles instead.
9425 The variable `gnus-default-article-saver' specifies the saver function."
9426   (interactive "P")
9427   (let* ((articles (gnus-summary-work-articles n))
9428          (save-buffer (save-excursion
9429                         (nnheader-set-temp-buffer " *Gnus Save*")))
9430          (num (length articles))
9431          header file)
9432     (dolist (article articles)
9433       (setq header (gnus-summary-article-header article))
9434       (if (not (vectorp header))
9435           ;; This is a pseudo-article.
9436           (if (assq 'name header)
9437               (gnus-copy-file (cdr (assq 'name header)))
9438             (gnus-message 1 "Article %d is unsaveable" article))
9439         ;; This is a real article.
9440         (save-window-excursion
9441           (gnus-summary-select-article t nil nil article))
9442         (save-excursion
9443           (set-buffer save-buffer)
9444           (erase-buffer)
9445           (insert-buffer-substring gnus-original-article-buffer))
9446         (setq file (gnus-article-save save-buffer file num))
9447         (gnus-summary-remove-process-mark article)
9448         (unless not-saved
9449           (gnus-summary-set-saved-mark article))))
9450     (gnus-kill-buffer save-buffer)
9451     (gnus-summary-position-point)
9452     (gnus-set-mode-line 'summary)
9453     n))
9454
9455 (defun gnus-summary-pipe-output (&optional arg)
9456   "Pipe the current article to a subprocess.
9457 If N is a positive number, pipe the N next articles.
9458 If N is a negative number, pipe the N previous articles.
9459 If N is nil and any articles have been marked with the process mark,
9460 pipe those articles instead."
9461   (interactive "P")
9462   (require 'gnus-art)
9463   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9464     (gnus-summary-save-article arg t))
9465   (gnus-configure-windows 'pipe))
9466
9467 (defun gnus-summary-save-article-mail (&optional arg)
9468   "Append the current article to an mail file.
9469 If N is a positive number, save the N next articles.
9470 If N is a negative number, save the N previous articles.
9471 If N is nil and any articles have been marked with the process mark,
9472 save those articles instead."
9473   (interactive "P")
9474   (require 'gnus-art)
9475   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9476     (gnus-summary-save-article arg)))
9477
9478 (defun gnus-summary-save-article-rmail (&optional arg)
9479   "Append the current article to an rmail file.
9480 If N is a positive number, save the N next articles.
9481 If N is a negative number, save the N previous articles.
9482 If N is nil and any articles have been marked with the process mark,
9483 save those articles instead."
9484   (interactive "P")
9485   (require 'gnus-art)
9486   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9487     (gnus-summary-save-article arg)))
9488
9489 (defun gnus-summary-save-article-file (&optional arg)
9490   "Append the current article to a file.
9491 If N is a positive number, save the N next articles.
9492 If N is a negative number, save the N previous articles.
9493 If N is nil and any articles have been marked with the process mark,
9494 save those articles instead."
9495   (interactive "P")
9496   (require 'gnus-art)
9497   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9498     (gnus-summary-save-article arg)))
9499
9500 (defun gnus-summary-write-article-file (&optional arg)
9501   "Write the current article to a file, deleting the previous file.
9502 If N is a positive number, save the N next articles.
9503 If N is a negative number, save the N previous articles.
9504 If N is nil and any articles have been marked with the process mark,
9505 save those articles instead."
9506   (interactive "P")
9507   (require 'gnus-art)
9508   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9509     (gnus-summary-save-article arg)))
9510
9511 (defun gnus-summary-save-article-body-file (&optional arg)
9512   "Append the current article body to a file.
9513 If N is a positive number, save the N next articles.
9514 If N is a negative number, save the N previous articles.
9515 If N is nil and any articles have been marked with the process mark,
9516 save those articles instead."
9517   (interactive "P")
9518   (require 'gnus-art)
9519   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9520     (gnus-summary-save-article arg)))
9521
9522 (defun gnus-summary-pipe-message (program)
9523   "Pipe the current article through PROGRAM."
9524   (interactive "sProgram: ")
9525   (gnus-summary-select-article)
9526   (let ((mail-header-separator ""))
9527     (gnus-eval-in-buffer-window gnus-article-buffer
9528       (save-restriction
9529         (widen)
9530         (let ((start (window-start))
9531               buffer-read-only)
9532           (message-pipe-buffer-body program)
9533           (set-window-start (get-buffer-window (current-buffer)) start))))))
9534
9535 (defun gnus-get-split-value (methods)
9536   "Return a value based on the split METHODS."
9537   (let (split-name method result match)
9538     (when methods
9539       (save-excursion
9540         (set-buffer gnus-original-article-buffer)
9541         (save-restriction
9542           (nnheader-narrow-to-headers)
9543           (while (and methods (not split-name))
9544             (goto-char (point-min))
9545             (setq method (pop methods))
9546             (setq match (car method))
9547             (when (cond
9548                    ((stringp match)
9549                     ;; Regular expression.
9550                     (ignore-errors
9551                       (re-search-forward match nil t)))
9552                    ((gnus-functionp match)
9553                     ;; Function.
9554                     (save-restriction
9555                       (widen)
9556                       (setq result (funcall match gnus-newsgroup-name))))
9557                    ((consp match)
9558                     ;; Form.
9559                     (save-restriction
9560                       (widen)
9561                       (setq result (eval match)))))
9562               (setq split-name (cdr method))
9563               (cond ((stringp result)
9564                      (push (expand-file-name
9565                             result gnus-article-save-directory)
9566                            split-name))
9567                     ((consp result)
9568                      (setq split-name (append result split-name)))))))))
9569     (nreverse split-name)))
9570
9571 (defun gnus-valid-move-group-p (group)
9572   (and (boundp group)
9573        (symbol-name group)
9574        (symbol-value group)
9575        (gnus-get-function (gnus-find-method-for-group
9576                            (symbol-name group)) 'request-accept-article t)))
9577
9578 (defun gnus-read-move-group-name (prompt default articles prefix)
9579   "Read a group name."
9580   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9581          (minibuffer-confirm-incomplete nil) ; XEmacs
9582          (prom
9583           (format "%s %s to:"
9584                   prompt
9585                   (if (> (length articles) 1)
9586                       (format "these %d articles" (length articles))
9587                     "this article")))
9588          (to-newsgroup
9589           (cond
9590            ((null split-name)
9591             (gnus-completing-read default prom
9592                                   gnus-active-hashtb
9593                                   'gnus-valid-move-group-p
9594                                   nil prefix
9595                                   'gnus-group-history))
9596            ((= 1 (length split-name))
9597             (gnus-completing-read (car split-name) prom
9598                                   gnus-active-hashtb
9599                                   'gnus-valid-move-group-p
9600                                   nil nil
9601                                   'gnus-group-history))
9602            (t
9603             (gnus-completing-read nil prom
9604                                   (mapcar (lambda (el) (list el))
9605                                           (nreverse split-name))
9606                                   nil nil nil
9607                                   'gnus-group-history))))
9608          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9609     (when to-newsgroup
9610       (if (or (string= to-newsgroup "")
9611               (string= to-newsgroup prefix))
9612           (setq to-newsgroup default))
9613       (unless to-newsgroup
9614         (error "No group name entered"))
9615       (or (gnus-active to-newsgroup)
9616           (gnus-activate-group to-newsgroup nil nil to-method)
9617           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9618                                      to-newsgroup))
9619               (or (and (gnus-request-create-group to-newsgroup to-method)
9620                        (gnus-activate-group
9621                         to-newsgroup nil nil to-method)
9622                        (gnus-subscribe-group to-newsgroup))
9623                   (error "Couldn't create group %s" to-newsgroup)))
9624           (error "No such group: %s" to-newsgroup)))
9625     to-newsgroup))
9626
9627 (defun gnus-summary-save-parts (type dir n &optional reverse)
9628   "Save parts matching TYPE to DIR.
9629 If REVERSE, save parts that do not match TYPE."
9630   (interactive
9631    (list (read-string "Save parts of type: "
9632                       (or (car gnus-summary-save-parts-type-history)
9633                           gnus-summary-save-parts-default-mime)
9634                       'gnus-summary-save-parts-type-history)
9635          (setq gnus-summary-save-parts-last-directory
9636                (read-file-name "Save to directory: "
9637                                gnus-summary-save-parts-last-directory
9638                                nil t))
9639          current-prefix-arg))
9640   (gnus-summary-iterate n
9641     (let ((gnus-display-mime-function nil)
9642           (gnus-inhibit-treatment t))
9643       (gnus-summary-select-article))
9644     (save-excursion
9645       (set-buffer gnus-article-buffer)
9646       (let ((handles (or gnus-article-mime-handles
9647                          (mm-dissect-buffer) (mm-uu-dissect))))
9648         (when handles
9649           (gnus-summary-save-parts-1 type dir handles reverse)
9650           (unless gnus-article-mime-handles ;; Don't destroy this case.
9651             (mm-destroy-parts handles)))))))
9652
9653 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9654   (if (stringp (car handle))
9655       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9656               (cdr handle))
9657     (when (if reverse
9658               (not (string-match type (mm-handle-media-type handle)))
9659             (string-match type (mm-handle-media-type handle)))
9660       (let ((file (expand-file-name
9661                    (file-name-nondirectory
9662                     (or
9663                      (mail-content-type-get
9664                       (mm-handle-disposition handle) 'filename)
9665                      (concat gnus-newsgroup-name
9666                              "." (number-to-string
9667                                   (cdr gnus-article-current)))))
9668                    dir)))
9669         (unless (file-exists-p file)
9670           (mm-save-part-to-file handle file))))))
9671
9672 ;; Summary extract commands
9673
9674 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9675   (let ((buffer-read-only nil)
9676         (article (gnus-summary-article-number))
9677         after-article b e)
9678     (unless (gnus-summary-goto-subject article)
9679       (error "No such article: %d" article))
9680     (gnus-summary-position-point)
9681     ;; If all commands are to be bunched up on one line, we collect
9682     ;; them here.
9683     (unless gnus-view-pseudos-separately
9684       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9685             files action)
9686         (while ps
9687           (setq action (cdr (assq 'action (car ps))))
9688           (setq files (list (cdr (assq 'name (car ps)))))
9689           (while (and ps (cdr ps)
9690                       (string= (or action "1")
9691                                (or (cdr (assq 'action (cadr ps))) "2")))
9692             (push (cdr (assq 'name (cadr ps))) files)
9693             (setcdr ps (cddr ps)))
9694           (when files
9695             (when (not (string-match "%s" action))
9696               (push " " files))
9697             (push " " files)
9698             (when (assq 'execute (car ps))
9699               (setcdr (assq 'execute (car ps))
9700                       (funcall (if (string-match "%s" action)
9701                                    'format 'concat)
9702                                action
9703                                (mapconcat
9704                                 (lambda (f)
9705                                   (if (equal f " ")
9706                                       f
9707                                     (mm-quote-arg f)))
9708                                 files " ")))))
9709           (setq ps (cdr ps)))))
9710     (if (and gnus-view-pseudos (not not-view))
9711         (while pslist
9712           (when (assq 'execute (car pslist))
9713             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9714                                   (eq gnus-view-pseudos 'not-confirm)))
9715           (setq pslist (cdr pslist)))
9716       (save-excursion
9717         (while pslist
9718           (setq after-article (or (cdr (assq 'article (car pslist)))
9719                                   (gnus-summary-article-number)))
9720           (gnus-summary-goto-subject after-article)
9721           (forward-line 1)
9722           (setq b (point))
9723           (insert "    " (file-name-nondirectory
9724                           (cdr (assq 'name (car pslist))))
9725                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9726           (setq e (point))
9727           (forward-line -1)             ; back to `b'
9728           (gnus-add-text-properties
9729            b (1- e) (list 'gnus-number gnus-reffed-article-number
9730                           gnus-mouse-face-prop gnus-mouse-face))
9731           (gnus-data-enter
9732            after-article gnus-reffed-article-number
9733            gnus-unread-mark b (car pslist) 0 (- e b))
9734           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9735           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9736           (setq pslist (cdr pslist)))))))
9737
9738 (defun gnus-pseudos< (p1 p2)
9739   (let ((c1 (cdr (assq 'action p1)))
9740         (c2 (cdr (assq 'action p2))))
9741     (and c1 c2 (string< c1 c2))))
9742
9743 (defun gnus-request-pseudo-article (props)
9744   (cond ((assq 'execute props)
9745          (gnus-execute-command (cdr (assq 'execute props)))))
9746   (let ((gnus-current-article (gnus-summary-article-number)))
9747     (gnus-run-hooks 'gnus-mark-article-hook)))
9748
9749 (defun gnus-execute-command (command &optional automatic)
9750   (save-excursion
9751     (gnus-article-setup-buffer)
9752     (set-buffer gnus-article-buffer)
9753     (setq buffer-read-only nil)
9754     (let ((command (if automatic command
9755                      (read-string "Command: " (cons command 0)))))
9756       (erase-buffer)
9757       (insert "$ " command "\n\n")
9758       (if gnus-view-pseudo-asynchronously
9759           (start-process "gnus-execute" (current-buffer) shell-file-name
9760                          shell-command-switch command)
9761         (call-process shell-file-name nil t nil
9762                       shell-command-switch command)))))
9763
9764 ;; Summary kill commands.
9765
9766 (defun gnus-summary-edit-global-kill (article)
9767   "Edit the \"global\" kill file."
9768   (interactive (list (gnus-summary-article-number)))
9769   (gnus-group-edit-global-kill article))
9770
9771 (defun gnus-summary-edit-local-kill ()
9772   "Edit a local kill file applied to the current newsgroup."
9773   (interactive)
9774   (setq gnus-current-headers (gnus-summary-article-header))
9775   (gnus-group-edit-local-kill
9776    (gnus-summary-article-number) gnus-newsgroup-name))
9777
9778 ;;; Header reading.
9779
9780 (defun gnus-read-header (id &optional header)
9781   "Read the headers of article ID and enter them into the Gnus system."
9782   (let ((group gnus-newsgroup-name)
9783         (gnus-override-method
9784          (or
9785           gnus-override-method
9786           (and (gnus-news-group-p gnus-newsgroup-name)
9787                (car (gnus-refer-article-methods)))))
9788         where)
9789     ;; First we check to see whether the header in question is already
9790     ;; fetched.
9791     (if (stringp id)
9792         ;; This is a Message-ID.
9793         (setq header (or header (gnus-id-to-header id)))
9794       ;; This is an article number.
9795       (setq header (or header (gnus-summary-article-header id))))
9796     (if (and header
9797              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9798         ;; We have found the header.
9799         header
9800       ;; If this is a sparse article, we have to nix out its
9801       ;; previous entry in the thread hashtb.
9802       (when (and header
9803                  (gnus-summary-article-sparse-p (mail-header-number header)))
9804         (let* ((parent (gnus-parent-id (mail-header-references header)))
9805                (thread (and parent (gnus-id-to-thread parent))))
9806           (when thread
9807             (delq (assq header thread) thread))))
9808       ;; We have to really fetch the header to this article.
9809       (save-excursion
9810         (set-buffer nntp-server-buffer)
9811         (when (setq where (gnus-request-head id group))
9812           (nnheader-fold-continuation-lines)
9813           (goto-char (point-max))
9814           (insert ".\n")
9815           (goto-char (point-min))
9816           (insert "211 ")
9817           (princ (cond
9818                   ((numberp id) id)
9819                   ((cdr where) (cdr where))
9820                   (header (mail-header-number header))
9821                   (t gnus-reffed-article-number))
9822                  (current-buffer))
9823           (insert " Article retrieved.\n"))
9824         (if (or (not where)
9825                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9826             ()                          ; Malformed head.
9827           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9828             (when (and (stringp id)
9829                        (not (string= (gnus-group-real-name group)
9830                                      (car where))))
9831               ;; If we fetched by Message-ID and the article came
9832               ;; from a different group, we fudge some bogus article
9833               ;; numbers for this article.
9834               (mail-header-set-number header gnus-reffed-article-number))
9835             (save-excursion
9836               (set-buffer gnus-summary-buffer)
9837               (decf gnus-reffed-article-number)
9838               (gnus-remove-header (mail-header-number header))
9839               (push header gnus-newsgroup-headers)
9840               (setq gnus-current-headers header)
9841               (push (mail-header-number header) gnus-newsgroup-limit)))
9842           header)))))
9843
9844 (defun gnus-remove-header (number)
9845   "Remove header NUMBER from `gnus-newsgroup-headers'."
9846   (if (and gnus-newsgroup-headers
9847            (= number (mail-header-number (car gnus-newsgroup-headers))))
9848       (pop gnus-newsgroup-headers)
9849     (let ((headers gnus-newsgroup-headers))
9850       (while (and (cdr headers)
9851                   (not (= number (mail-header-number (cadr headers)))))
9852         (pop headers))
9853       (when (cdr headers)
9854         (setcdr headers (cddr headers))))))
9855
9856 ;;;
9857 ;;; summary highlights
9858 ;;;
9859
9860 (defun gnus-highlight-selected-summary ()
9861   "Highlight selected article in summary buffer."
9862   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9863   (when gnus-summary-selected-face
9864     (save-excursion
9865       (let* ((beg (progn (beginning-of-line) (point)))
9866              (end (progn (end-of-line) (point)))
9867              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9868              (from (if (get-text-property beg gnus-mouse-face-prop)
9869                        beg
9870                      (or (next-single-property-change
9871                           beg gnus-mouse-face-prop nil end)
9872                          beg)))
9873              (to
9874               (if (= from end)
9875                   (- from 2)
9876                 (or (next-single-property-change
9877                      from gnus-mouse-face-prop nil end)
9878                     end))))
9879         ;; If no mouse-face prop on line we will have to = from = end,
9880         ;; so we highlight the entire line instead.
9881         (when (= (+ to 2) from)
9882           (setq from beg)
9883           (setq to end))
9884         (if gnus-newsgroup-selected-overlay
9885             ;; Move old overlay.
9886             (gnus-move-overlay
9887              gnus-newsgroup-selected-overlay from to (current-buffer))
9888           ;; Create new overlay.
9889           (gnus-overlay-put
9890            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9891            'face gnus-summary-selected-face))))))
9892
9893 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9894 (defun gnus-summary-highlight-line ()
9895   "Highlight current line according to `gnus-summary-highlight'."
9896   (let* ((list gnus-summary-highlight)
9897          (p (point))
9898          (end (progn (end-of-line) (point)))
9899          ;; now find out where the line starts and leave point there.
9900          (beg (progn (beginning-of-line) (point)))
9901          (article (gnus-summary-article-number))
9902          (score (or (cdr (assq (or article gnus-current-article)
9903                                gnus-newsgroup-scored))
9904                     gnus-summary-default-score 0))
9905          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9906          (inhibit-read-only t))
9907     ;; Eval the cars of the lists until we find a match.
9908     (let ((default gnus-summary-default-score))
9909       (while (and list
9910                   (not (eval (caar list))))
9911         (setq list (cdr list))))
9912     (let ((face (cdar list)))
9913       (unless (eq face (get-text-property beg 'face))
9914         (gnus-put-text-property-excluding-characters-with-faces
9915          beg end 'face
9916          (setq face (if (boundp face) (symbol-value face) face)))
9917         (when gnus-summary-highlight-line-function
9918           (funcall gnus-summary-highlight-line-function article face))))
9919     (goto-char p)))
9920
9921 (defun gnus-update-read-articles (group unread &optional compute)
9922   "Update the list of read articles in GROUP."
9923   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9924          (entry (gnus-gethash group gnus-newsrc-hashtb))
9925          (info (nth 2 entry))
9926          (prev 1)
9927          (unread (sort (copy-sequence unread) '<))
9928          read)
9929     (if (or (not info) (not active))
9930         ;; There is no info on this group if it was, in fact,
9931         ;; killed.  Gnus stores no information on killed groups, so
9932         ;; there's nothing to be done.
9933         ;; One could store the information somewhere temporarily,
9934         ;; perhaps...  Hmmm...
9935         ()
9936       ;; Remove any negative articles numbers.
9937       (while (and unread (< (car unread) 0))
9938         (setq unread (cdr unread)))
9939       ;; Remove any expired article numbers
9940       (while (and unread (< (car unread) (car active)))
9941         (setq unread (cdr unread)))
9942       ;; Compute the ranges of read articles by looking at the list of
9943       ;; unread articles.
9944       (while unread
9945         (when (/= (car unread) prev)
9946           (push (if (= prev (1- (car unread))) prev
9947                   (cons prev (1- (car unread))))
9948                 read))
9949         (setq prev (1+ (car unread)))
9950         (setq unread (cdr unread)))
9951       (when (<= prev (cdr active))
9952         (push (cons prev (cdr active)) read))
9953       (setq read (if (> (length read) 1) (nreverse read) read))
9954       (if compute
9955           read
9956         (save-excursion
9957           (let (setmarkundo)
9958             ;; Propagate the read marks to the backend.
9959             (when (gnus-check-backend-function 'request-set-mark group)
9960               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9961                     (add (gnus-remove-from-range read (gnus-info-read info))))
9962                 (when (or add del)
9963                   (unless (gnus-check-group group)
9964                     (error "Can't open server for %s" group))
9965                   (gnus-request-set-mark
9966                    group (delq nil (list (if add (list add 'add '(read)))
9967                                          (if del (list del 'del '(read))))))
9968                   (setq setmarkundo
9969                         `(gnus-request-set-mark
9970                           ,group
9971                           ',(delq nil (list
9972                                        (if del (list del 'add '(read)))
9973                                        (if add (list add 'del '(read))))))))))
9974             (set-buffer gnus-group-buffer)
9975             (gnus-undo-register
9976               `(progn
9977                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9978                  (gnus-info-set-read ',info ',(gnus-info-read info))
9979                  (gnus-get-unread-articles-in-group ',info
9980                                                     (gnus-active ,group))
9981                  (gnus-group-update-group ,group t)
9982                  ,setmarkundo))))
9983         ;; Enter this list into the group info.
9984         (gnus-info-set-read info read)
9985         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9986         (gnus-get-unread-articles-in-group info (gnus-active group))
9987         t))))
9988
9989 (defun gnus-offer-save-summaries ()
9990   "Offer to save all active summary buffers."
9991   (save-excursion
9992     (let ((buflist (buffer-list))
9993           buffers bufname)
9994       ;; Go through all buffers and find all summaries.
9995       (while buflist
9996         (and (setq bufname (buffer-name (car buflist)))
9997              (string-match "Summary" bufname)
9998              (save-excursion
9999                (set-buffer bufname)
10000                ;; We check that this is, indeed, a summary buffer.
10001                (and (eq major-mode 'gnus-summary-mode)
10002                     ;; Also make sure this isn't bogus.
10003                     gnus-newsgroup-prepared
10004                     ;; Also make sure that this isn't a dead summary buffer.
10005                     (not gnus-dead-summary-mode)))
10006              (push bufname buffers))
10007         (setq buflist (cdr buflist)))
10008       ;; Go through all these summary buffers and offer to save them.
10009       (when buffers
10010         (map-y-or-n-p
10011          "Update summary buffer %s? "
10012          (lambda (buf)
10013            (switch-to-buffer buf)
10014            (gnus-summary-exit))
10015          buffers)))))
10016
10017 (defun gnus-summary-setup-default-charset ()
10018   "Setup newsgroup default charset."
10019   (if (equal gnus-newsgroup-name "nndraft:drafts")
10020       (setq gnus-newsgroup-charset nil)
10021     (let* ((ignored-charsets
10022             (or gnus-newsgroup-ephemeral-ignored-charsets
10023                 (append
10024                  (and gnus-newsgroup-name
10025                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10026                  gnus-newsgroup-ignored-charsets))))
10027       (setq gnus-newsgroup-charset
10028             (or gnus-newsgroup-ephemeral-charset
10029                 (and gnus-newsgroup-name
10030                      (gnus-parameter-charset gnus-newsgroup-name))
10031                 gnus-default-charset))
10032       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10033            ignored-charsets))))
10034
10035 ;;;
10036 ;;; Mime Commands
10037 ;;;
10038
10039 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10040   "Display the current article buffer fully MIME-buttonized.
10041 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10042 treated as multipart/mixed."
10043   (interactive "P")
10044   (require 'gnus-art)
10045   (let ((gnus-unbuttonized-mime-types nil)
10046         (gnus-mime-display-multipart-as-mixed show-all-parts))
10047     (gnus-summary-show-article)))
10048
10049 (defun gnus-summary-repair-multipart (article)
10050   "Add a Content-Type header to a multipart article without one."
10051   (interactive (list (gnus-summary-article-number)))
10052   (gnus-with-article article
10053     (message-narrow-to-head)
10054     (message-remove-header "Mime-Version")
10055     (goto-char (point-max))
10056     (insert "Mime-Version: 1.0\n")
10057     (widen)
10058     (when (search-forward "\n--" nil t)
10059       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10060         (message-narrow-to-head)
10061         (message-remove-header "Content-Type")
10062         (goto-char (point-max))
10063         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10064                         separator))
10065         (widen))))
10066   (let (gnus-mark-article-hook)
10067     (gnus-summary-select-article t t nil article)))
10068
10069 (defun gnus-summary-toggle-display-buttonized ()
10070   "Toggle the buttonizing of the article buffer."
10071   (interactive)
10072   (require 'gnus-art)
10073   (if (setq gnus-inhibit-mime-unbuttonizing
10074             (not gnus-inhibit-mime-unbuttonizing))
10075       (let ((gnus-unbuttonized-mime-types nil))
10076         (gnus-summary-show-article))
10077     (gnus-summary-show-article)))
10078
10079 ;;;
10080 ;;; Generic summary marking commands
10081 ;;;
10082
10083 (defvar gnus-summary-marking-alist
10084   '((read gnus-del-mark "d")
10085     (unread gnus-unread-mark "u")
10086     (ticked gnus-ticked-mark "!")
10087     (dormant gnus-dormant-mark "?")
10088     (expirable gnus-expirable-mark "e"))
10089   "An alist of names/marks/keystrokes.")
10090
10091 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10092 (defvar gnus-summary-mark-map)
10093
10094 (defun gnus-summary-make-all-marking-commands ()
10095   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10096   (dolist (elem gnus-summary-marking-alist)
10097     (apply 'gnus-summary-make-marking-command elem)))
10098
10099 (defun gnus-summary-make-marking-command (name mark keystroke)
10100   (let ((map (make-sparse-keymap)))
10101     (define-key gnus-summary-generic-mark-map keystroke map)
10102     (dolist (lway `((next "next" next nil "n")
10103                     (next-unread "next unread" next t "N")
10104                     (prev "previous" prev nil "p")
10105                     (prev-unread "previous unread" prev t "P")
10106                     (nomove "" nil nil ,keystroke)))
10107       (let ((func (gnus-summary-make-marking-command-1
10108                    mark (car lway) lway name)))
10109         (setq func (eval func))
10110         (define-key map (nth 4 lway) func)))))
10111
10112 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10113   `(defun ,(intern
10114             (format "gnus-summary-put-mark-as-%s%s"
10115                     name (if (eq way 'nomove)
10116                              ""
10117                            (concat "-" (symbol-name way)))))
10118      (n)
10119      ,(format
10120        "Mark the current article as %s%s.
10121 If N, the prefix, then repeat N times.
10122 If N is negative, move in reverse order.
10123 The difference between N and the actual number of articles marked is
10124 returned."
10125        name (cadr lway))
10126      (interactive "p")
10127      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10128
10129 (defun gnus-summary-generic-mark (n mark move unread)
10130   "Mark N articles with MARK."
10131   (unless (eq major-mode 'gnus-summary-mode)
10132     (error "This command can only be used in the summary buffer"))
10133   (gnus-summary-show-thread)
10134   (let ((nummove
10135          (cond
10136           ((eq move 'next) 1)
10137           ((eq move 'prev) -1)
10138           (t 0))))
10139     (if (zerop nummove)
10140         (setq n 1)
10141       (when (< n 0)
10142         (setq n (abs n)
10143               nummove (* -1 nummove))))
10144     (while (and (> n 0)
10145                 (gnus-summary-mark-article nil mark)
10146                 (zerop (gnus-summary-next-subject nummove unread t)))
10147       (setq n (1- n)))
10148     (when (/= 0 n)
10149       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10150     (gnus-summary-recenter)
10151     (gnus-summary-position-point)
10152     (gnus-set-mode-line 'summary)
10153     n))
10154
10155 (defun gnus-summary-insert-articles (articles)
10156   (when (setq articles
10157               (gnus-set-difference articles
10158                                    (mapcar (lambda (h) (mail-header-number h))
10159                                            gnus-newsgroup-headers)))
10160     (setq gnus-newsgroup-headers 
10161           (merge 'list
10162                  gnus-newsgroup-headers
10163                  (gnus-fetch-headers articles)
10164                  'gnus-article-sort-by-number))
10165     ;; Suppress duplicates?
10166     (when gnus-suppress-duplicates
10167       (gnus-dup-suppress-articles))
10168     
10169     ;; We might want to build some more threads first.
10170     (when (and gnus-fetch-old-headers
10171                (eq gnus-headers-retrieved-by 'nov))
10172       (if (eq gnus-fetch-old-headers 'invisible)
10173         (gnus-build-all-threads)
10174         (gnus-build-old-threads)))
10175     ;; Let the Gnus agent mark articles as read.
10176     (when gnus-agent
10177       (gnus-agent-get-undownloaded-list))
10178     ;; Remove list identifiers from subject
10179     (when gnus-list-identifiers
10180       (gnus-summary-remove-list-identifiers))
10181     ;; First and last article in this newsgroup.
10182     (when gnus-newsgroup-headers
10183       (setq gnus-newsgroup-begin
10184             (mail-header-number (car gnus-newsgroup-headers))
10185             gnus-newsgroup-end
10186             (mail-header-number
10187              (gnus-last-element gnus-newsgroup-headers))))
10188     (when gnus-use-scoring
10189       (gnus-possibly-score-headers))))
10190
10191 (defun gnus-summary-insert-old-articles (&optional all)
10192   "Insert all old articles in this group.
10193 If ALL is non-nil, already read articles become readable.
10194 If ALL is a number, fetch this number of articles."
10195   (interactive "P")
10196   (prog1
10197       (let ((old (mapcar 'car gnus-newsgroup-data))
10198             (i (car gnus-newsgroup-active))
10199             older len)
10200         (while (<= i (cdr gnus-newsgroup-active))
10201           (or (memq i old) (push i older))
10202           (incf i))
10203         (setq len (length older))
10204         (cond 
10205          ((null older) nil)
10206          ((numberp all) 
10207           (if (< all len)
10208               (setq older (subseq older 0 all))))
10209          (all nil)
10210          (t
10211           (if (and (numberp gnus-large-newsgroup)
10212                    (> len gnus-large-newsgroup))
10213               (let ((input
10214                      (read-string
10215                       (format
10216                        "How many articles from %s (default %d): "
10217                        (gnus-limit-string 
10218                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10219                        len))))
10220                 (unless (string-match "^[ \t]*$" input) 
10221                   (setq all (string-to-number input))
10222                   (if (< all len)
10223                       (setq older (subseq older 0 all))))))))
10224         (if (not older)
10225             (message "No old news.")
10226           (gnus-summary-insert-articles older)
10227           (gnus-summary-limit (gnus-union older old))))
10228     (gnus-summary-position-point)))
10229
10230 (defun gnus-summary-insert-new-articles ()
10231   "Insert all new articles in this group."
10232   (interactive)
10233   (prog1
10234       (let ((old (mapcar 'car gnus-newsgroup-data))
10235             (old-active gnus-newsgroup-active)
10236             (nnmail-fetched-sources (list t))
10237             i new)
10238         (setq gnus-newsgroup-active 
10239               (gnus-activate-group gnus-newsgroup-name 'scan))
10240         (setq i (1+ (cdr old-active)))
10241         (while (<= i (cdr gnus-newsgroup-active))
10242           (push i new)
10243           (incf i))
10244         (if (not new)
10245             (message "No gnus is bad news.")
10246           (gnus-summary-insert-articles new)
10247           (setq gnus-newsgroup-unreads
10248                 (append gnus-newsgroup-unreads new))
10249           (gnus-summary-limit (gnus-union old new))))
10250     (gnus-summary-position-point)))
10251
10252 (gnus-summary-make-all-marking-commands)
10253
10254 (gnus-ems-redefine)
10255
10256 (provide 'gnus-sum)
10257
10258 (run-hooks 'gnus-sum-load-hook)
10259
10260 ;;; gnus-sum.el ends here